From 760752e79da94511862cb1cc4d73e881d77ccbf9 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 24 Dec 2016 10:48:43 +0100 Subject: Return AST nodes from parser --- askama_codegen/src/parser.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'askama_codegen/src/parser.rs') 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, map!(expr_str, Node::Expr)); -named!(parse_template< Vec<&[u8]> >, many1!(alt!(take_content | var_expr))); +named!(parse_template< Vec >, many1!(alt!(take_content | expr_node))); -pub fn parse<'a>(src: &'a str) -> Vec<&'a [u8]> { +pub fn parse<'a>(src: &'a str) -> Vec { match parse_template(src.as_bytes()) { IResult::Done(_, res) => res, _ => panic!("problems parsing template source"), -- cgit