aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2016-12-24 10:48:43 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2016-12-24 10:48:43 +0100
commit760752e79da94511862cb1cc4d73e881d77ccbf9 (patch)
tree6166e33392a3ebe12ebca6ac3231b000e4521e51
parent99a83551f555021602bb950d98a5f63dbe4c2f35 (diff)
downloadaskama-760752e79da94511862cb1cc4d73e881d77ccbf9.tar.gz
askama-760752e79da94511862cb1cc4d73e881d77ccbf9.tar.bz2
askama-760752e79da94511862cb1cc4d73e881d77ccbf9.zip
Return AST nodes from parser
Diffstat (limited to '')
-rw-r--r--askama_codegen/src/generator.rs20
-rw-r--r--askama_codegen/src/parser.rs23
2 files changed, 25 insertions, 18 deletions
diff --git a/askama_codegen/src/generator.rs b/askama_codegen/src/generator.rs
index 8c61f70..7689af5 100644
--- a/askama_codegen/src/generator.rs
+++ b/askama_codegen/src/generator.rs
@@ -1,21 +1,21 @@
+use parser::Node;
use std::str;
-pub fn generate(ctx_name: &str, tokens: &Vec<&[u8]>) -> String {
+pub fn generate(ctx_name: &str, tokens: &Vec<Node>) -> String {
let mut code = String::new();
code.push_str("impl askama::Template for ");
code.push_str(ctx_name);
code.push_str(" {\n");
code.push_str(" fn render(&self) -> String {\n");
code.push_str(" let mut buf = String::new();\n");
- code.push_str(" buf.push_str(\"");
- code.push_str(str::from_utf8(tokens[0]).unwrap());
- code.push_str("\");\n");
- code.push_str(" buf.push_str(&self.");
- code.push_str(str::from_utf8(tokens[1]).unwrap());
- code.push_str(");\n");
- code.push_str(" buf.push_str(\"");
- code.push_str(str::from_utf8(tokens[2]).unwrap());
- code.push_str("\");\n");
+ for n in tokens {
+ code.push_str(" buf.push_str(");
+ code.push_str(&match n {
+ &Node::Lit(val) => format!("{:#?}", str::from_utf8(val).unwrap()),
+ &Node::Expr(val) => format!("&self.{}", str::from_utf8(val).unwrap()),
+ });
+ code.push_str(");\n");
+ }
code.push_str(" buf");
code.push_str(" }\n");
code.push_str("}\n\n");
diff --git a/askama_codegen/src/parser.rs b/askama_codegen/src/parser.rs
index 6ff4388..b7a712b 100644
--- a/askama_codegen/src/parser.rs
+++ b/askama_codegen/src/parser.rs
@@ -1,28 +1,35 @@
use nom::{self, IResult};
-fn take_content(i: &[u8]) -> IResult<&[u8], &[u8]> {
+pub enum Node<'a> {
+ Lit(&'a [u8]),
+ Expr(&'a [u8]),
+}
+
+fn take_content(i: &[u8]) -> IResult<&[u8], Node> {
if i.len() < 1 || i[0] == b'{' {
return IResult::Error(error_position!(nom::ErrorKind::TakeUntil, i));
}
for (j, c) in i.iter().enumerate() {
if *c == b'{' {
if i.len() < j + 2 {
- return IResult::Done(&i[..0], &i[..]);
+ return IResult::Done(&i[..0], Node::Lit(&i[..]));
} else if i[j + 1] == '{' as u8 {
- return IResult::Done(&i[j..], &i[..j]);
+ return IResult::Done(&i[j..], Node::Lit(&i[..j]));
} else if i[j + 1] == '%' as u8 {
- return IResult::Done(&i[j..], &i[..j]);
+ return IResult::Done(&i[j..], Node::Lit(&i[..j]));
}
}
}
- IResult::Done(&i[..0], &i[..])
+ IResult::Done(&i[..0], Node::Lit(&i[..]))
}
-named!(var_expr, delimited!(tag!("{{"), take_until!("}}"), tag!("}}")));
+named!(expr_str, delimited!(tag!("{{"), take_until!("}}"), tag!("}}")));
+
+named!(expr_node<Node>, map!(expr_str, Node::Expr));
-named!(parse_template< Vec<&[u8]> >, many1!(alt!(take_content | var_expr)));
+named!(parse_template< Vec<Node> >, many1!(alt!(take_content | expr_node)));
-pub fn parse<'a>(src: &'a str) -> Vec<&'a [u8]> {
+pub fn parse<'a>(src: &'a str) -> Vec<Node> {
match parse_template(src.as_bytes()) {
IResult::Done(_, res) => res,
_ => panic!("problems parsing template source"),