aboutsummaryrefslogtreecommitdiffstats
path: root/askama_codegen/src/parser.rs
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2016-12-24 10:08:05 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2016-12-24 10:08:05 +0100
commit8f0aae265cb3471b2472cb95c820746df7213bed (patch)
tree5a3328e96a4965d3965b4a7537b3b7f5aebfd05b /askama_codegen/src/parser.rs
parentb83ad3582fef454983fd491e02e3d243a1d0a52d (diff)
downloadaskama-8f0aae265cb3471b2472cb95c820746df7213bed.tar.gz
askama-8f0aae265cb3471b2472cb95c820746df7213bed.tar.bz2
askama-8f0aae265cb3471b2472cb95c820746df7213bed.zip
Move template parser into separate module
Diffstat (limited to '')
-rw-r--r--askama_codegen/src/parser.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/askama_codegen/src/parser.rs b/askama_codegen/src/parser.rs
new file mode 100644
index 0000000..6ff4388
--- /dev/null
+++ b/askama_codegen/src/parser.rs
@@ -0,0 +1,30 @@
+use nom::{self, IResult};
+
+fn take_content(i: &[u8]) -> IResult<&[u8], &[u8]> {
+ 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[..]);
+ } else if i[j + 1] == '{' as u8 {
+ return IResult::Done(&i[j..], &i[..j]);
+ } else if i[j + 1] == '%' as u8 {
+ return IResult::Done(&i[j..], &i[..j]);
+ }
+ }
+ }
+ IResult::Done(&i[..0], &i[..])
+}
+
+named!(var_expr, delimited!(tag!("{{"), take_until!("}}"), tag!("}}")));
+
+named!(parse_template< Vec<&[u8]> >, many1!(alt!(take_content | var_expr)));
+
+pub fn parse<'a>(src: &'a str) -> Vec<&'a [u8]> {
+ match parse_template(src.as_bytes()) {
+ IResult::Done(_, res) => res,
+ _ => panic!("problems parsing template source"),
+ }
+}