diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-01-03 09:56:08 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-01-03 09:56:08 +0100 |
commit | 3e7983d7af8d19393507e50778b818f8dcf24b91 (patch) | |
tree | ca7d45b89cf8fd5c9ba581a63a4a6b1b67f06471 /askama_codegen/src/parser.rs | |
parent | d82a8fdf39783e3bfae6b2ea9eb08f011ba195b7 (diff) | |
download | askama-3e7983d7af8d19393507e50778b818f8dcf24b91.tar.gz askama-3e7983d7af8d19393507e50778b818f8dcf24b91.tar.bz2 askama-3e7983d7af8d19393507e50778b818f8dcf24b91.zip |
Extend parser to allow different expression types
Diffstat (limited to 'askama_codegen/src/parser.rs')
-rw-r--r-- | askama_codegen/src/parser.rs | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/askama_codegen/src/parser.rs b/askama_codegen/src/parser.rs index b7a712b..5f3f8c5 100644 --- a/askama_codegen/src/parser.rs +++ b/askama_codegen/src/parser.rs @@ -1,8 +1,12 @@ use nom::{self, IResult}; +pub enum Expr<'a> { + Var(&'a [u8]), +} + pub enum Node<'a> { Lit(&'a [u8]), - Expr(&'a [u8]), + Expr(Expr<'a>), } fn take_content(i: &[u8]) -> IResult<&[u8], Node> { @@ -23,9 +27,11 @@ fn take_content(i: &[u8]) -> IResult<&[u8], Node> { IResult::Done(&i[..0], Node::Lit(&i[..])) } -named!(expr_str, delimited!(tag!("{{"), take_until!("}}"), tag!("}}"))); +named!(expr_var<Expr>, map!(ws!(nom::alphanumeric), Expr::Var)); + +named!(any_expr<Expr>, delimited!(tag!("{{"), expr_var, tag!("}}"))); -named!(expr_node<Node>, map!(expr_str, Node::Expr)); +named!(expr_node<Node>, map!(any_expr, Node::Expr)); named!(parse_template< Vec<Node> >, many1!(alt!(take_content | expr_node))); |