From 5d193ce822fab739f889a70c1defa402ddea8eab Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 7 Jan 2017 20:33:29 +0100 Subject: Move parser and generator code into askama crate --- askama_derive/src/parser.rs | 69 --------------------------------------------- 1 file changed, 69 deletions(-) delete mode 100644 askama_derive/src/parser.rs (limited to 'askama_derive/src/parser.rs') diff --git a/askama_derive/src/parser.rs b/askama_derive/src/parser.rs deleted file mode 100644 index cd93971..0000000 --- a/askama_derive/src/parser.rs +++ /dev/null @@ -1,69 +0,0 @@ -use nom::{self, IResult}; -use std::str; - -pub enum Expr<'a> { - Var(&'a [u8]), - Filter(&'a str, Box>), -} - -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, map!(nom::alphanumeric, Expr::Var)); - -fn expr_filtered(i: &[u8]) -> IResult<&[u8], Expr> { - let (mut left, mut expr) = match expr_var(i) { - IResult::Error(err) => { return IResult::Error(err); }, - IResult::Incomplete(needed) => { return IResult::Incomplete(needed); }, - IResult::Done(left, res) => (left, res), - }; - while left[0] == b'|' { - match nom::alphanumeric(&left[1..]) { - IResult::Error(err) => { - return IResult::Error(err); - }, - IResult::Incomplete(needed) => { - return IResult::Incomplete(needed); - }, - IResult::Done(new_left, res) => { - left = new_left; - expr = Expr::Filter(str::from_utf8(res).unwrap(), Box::new(expr)); - }, - }; - } - return IResult::Done(left, expr); -} - -named!(expr_node, map!( - delimited!(tag_s!("{{"), ws!(expr_filtered), tag_s!("}}")), - Node::Expr)); - -named!(parse_template< Vec >, many1!(alt!(take_content | expr_node))); - -pub fn parse<'a>(src: &'a str) -> Vec { - match parse_template(src.as_bytes()) { - IResult::Done(_, res) => res, - IResult::Error(err) => panic!("problems parsing template source: {}", err), - IResult::Incomplete(_) => panic!("parsing incomplete"), - } -} -- cgit