aboutsummaryrefslogtreecommitdiffstats
path: root/askama_derive/src/parser.rs
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-01-03 10:01:16 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-01-03 10:01:16 +0100
commit4c8c773c84a48963e892c72f38f37bcb99b6eb74 (patch)
tree7ea8dd95e81bd40c1b24bb9143cbe865005b1ae0 /askama_derive/src/parser.rs
parent3e7983d7af8d19393507e50778b818f8dcf24b91 (diff)
downloadaskama-4c8c773c84a48963e892c72f38f37bcb99b6eb74.tar.gz
askama-4c8c773c84a48963e892c72f38f37bcb99b6eb74.tar.bz2
askama-4c8c773c84a48963e892c72f38f37bcb99b6eb74.zip
Rename askama_codegen to askama_derive
This appears to be best practice for crates using macros 1.1.
Diffstat (limited to 'askama_derive/src/parser.rs')
-rw-r--r--askama_derive/src/parser.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/askama_derive/src/parser.rs b/askama_derive/src/parser.rs
new file mode 100644
index 0000000..5f3f8c5
--- /dev/null
+++ b/askama_derive/src/parser.rs
@@ -0,0 +1,43 @@
+use nom::{self, IResult};
+
+pub enum Expr<'a> {
+ Var(&'a [u8]),
+}
+
+pub enum Node<'a> {
+ Lit(&'a [u8]),
+ Expr(Expr<'a>),
+}
+
+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], Node::Lit(&i[..]));
+ } else if i[j + 1] == '{' as u8 {
+ return IResult::Done(&i[j..], Node::Lit(&i[..j]));
+ } else if i[j + 1] == '%' as u8 {
+ return IResult::Done(&i[j..], Node::Lit(&i[..j]));
+ }
+ }
+ }
+ IResult::Done(&i[..0], Node::Lit(&i[..]))
+}
+
+named!(expr_var<Expr>, map!(ws!(nom::alphanumeric), Expr::Var));
+
+named!(any_expr<Expr>, delimited!(tag!("{{"), expr_var, tag!("}}")));
+
+named!(expr_node<Node>, map!(any_expr, Node::Expr));
+
+named!(parse_template< Vec<Node> >, many1!(alt!(take_content | expr_node)));
+
+pub fn parse<'a>(src: &'a str) -> Vec<Node> {
+ match parse_template(src.as_bytes()) {
+ IResult::Done(_, res) => res,
+ _ => panic!("problems parsing template source"),
+ }
+}