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 --- Cargo.lock | 3 +- askama/Cargo.toml | 1 + askama/src/generator.rs | 111 +++++++++++++++++++++++++++++++++++++++++ askama/src/lib.rs | 5 ++ askama/src/parser.rs | 69 +++++++++++++++++++++++++ askama_derive/Cargo.toml | 2 +- askama_derive/src/generator.rs | 111 ----------------------------------------- askama_derive/src/lib.rs | 10 ++-- askama_derive/src/parser.rs | 69 ------------------------- 9 files changed, 192 insertions(+), 189 deletions(-) create mode 100644 askama/src/generator.rs create mode 100644 askama/src/parser.rs delete mode 100644 askama_derive/src/generator.rs delete mode 100644 askama_derive/src/parser.rs diff --git a/Cargo.lock b/Cargo.lock index 0897e33..53811e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,13 +11,14 @@ name = "askama" version = "0.1.0" dependencies = [ "htmlescape 0.3.1 (git+https://github.com/veddan/rust-htmlescape)", + "nom 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "askama_derive" version = "0.1.0" dependencies = [ - "nom 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "askama 0.1.0", "syn 0.10.5 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/askama/Cargo.toml b/askama/Cargo.toml index 404f7be..13d718a 100644 --- a/askama/Cargo.toml +++ b/askama/Cargo.toml @@ -6,3 +6,4 @@ workspace = ".." [dependencies] htmlescape = { git = "https://github.com/veddan/rust-htmlescape" } +nom = "2.0" diff --git a/askama/src/generator.rs b/askama/src/generator.rs new file mode 100644 index 0000000..0e80265 --- /dev/null +++ b/askama/src/generator.rs @@ -0,0 +1,111 @@ +use parser::{Expr, Node}; +use std::str; + +struct Generator { + buf: String, + indent: u8, + start: bool, +} + +impl Generator { + + fn new() -> Generator { + Generator { buf: String::new(), indent: 0, start: true } + } + + fn init(&mut self, name: &str) { + self.write("impl askama::Template for "); + self.write(name); + self.writeln(" {"); + self.indent(); + self.writeln("fn render(&self) -> String {"); + self.indent(); + self.writeln("let mut buf = String::new();"); + } + + fn indent(&mut self) { + self.indent += 1; + } + + fn dedent(&mut self) { + self.indent -= 1; + } + + fn write(&mut self, s: &str) { + if self.start { + for _ in 0..(self.indent * 4) { + self.buf.push(' '); + } + self.start = false; + } + self.buf.push_str(s); + } + + fn writeln(&mut self, s: &str) { + if s.is_empty() { + return; + } + self.write(s); + self.buf.push('\n'); + self.start = true; + } + + fn visit_var(&mut self, s: &[u8]) { + self.write(&format!("self.{}", str::from_utf8(s).unwrap())); + } + + fn visit_filter(&mut self, name: &str, val: &Expr) { + self.write(&format!("askama::filters::{}(&", name)); + self.visit_expr(val); + self.write(")"); + } + + fn visit_expr(&mut self, expr: &Expr) { + match expr { + &Expr::Var(s) => self.visit_var(s), + &Expr::Filter(name, ref val) => self.visit_filter(name, &val), + } + } + + fn write_lit(&mut self, s: &[u8]) { + self.write("buf.push_str("); + self.write(&format!("{:#?}", str::from_utf8(s).unwrap())); + self.writeln(");"); + } + + fn write_expr(&mut self, s: &Expr) { + self.write("std::fmt::Write::write_fmt(&mut buf, format_args!(\"{}\", "); + self.visit_expr(s); + self.writeln(")).unwrap();"); + } + + fn handle(&mut self, tokens: &Vec) { + for n in tokens { + match n { + &Node::Lit(val) => { self.write_lit(val); }, + &Node::Expr(ref val) => { self.write_expr(&val); }, + } + } + } + + fn finalize(&mut self) { + self.writeln("buf"); + self.dedent(); + self.writeln("}"); + self.dedent(); + self.writeln("}"); + } + + fn result(self) -> String { + self.buf + } + +} + +pub fn generate(ctx_name: &str, tokens: &Vec) -> String { + let mut gen = Generator::new(); + gen.init(ctx_name); + gen.handle(tokens); + gen.finalize(); + gen.result() +} diff --git a/askama/src/lib.rs b/askama/src/lib.rs index 5ce214b..04caf76 100644 --- a/askama/src/lib.rs +++ b/askama/src/lib.rs @@ -1,7 +1,12 @@ #![feature(proc_macro)] +#[macro_use] +extern crate nom; + pub trait Template { fn render(&self) -> String; } pub mod filters; +pub mod generator; +pub mod parser; diff --git a/askama/src/parser.rs b/askama/src/parser.rs new file mode 100644 index 0000000..cd93971 --- /dev/null +++ b/askama/src/parser.rs @@ -0,0 +1,69 @@ +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"), + } +} diff --git a/askama_derive/Cargo.toml b/askama_derive/Cargo.toml index e9bfa66..400cbd3 100644 --- a/askama_derive/Cargo.toml +++ b/askama_derive/Cargo.toml @@ -7,5 +7,5 @@ authors = ["Dirkjan Ochtman "] proc-macro = true [dependencies] +askama = { path = "../askama" } syn = "0.10" -nom = "2.0" diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs deleted file mode 100644 index 0e80265..0000000 --- a/askama_derive/src/generator.rs +++ /dev/null @@ -1,111 +0,0 @@ -use parser::{Expr, Node}; -use std::str; - -struct Generator { - buf: String, - indent: u8, - start: bool, -} - -impl Generator { - - fn new() -> Generator { - Generator { buf: String::new(), indent: 0, start: true } - } - - fn init(&mut self, name: &str) { - self.write("impl askama::Template for "); - self.write(name); - self.writeln(" {"); - self.indent(); - self.writeln("fn render(&self) -> String {"); - self.indent(); - self.writeln("let mut buf = String::new();"); - } - - fn indent(&mut self) { - self.indent += 1; - } - - fn dedent(&mut self) { - self.indent -= 1; - } - - fn write(&mut self, s: &str) { - if self.start { - for _ in 0..(self.indent * 4) { - self.buf.push(' '); - } - self.start = false; - } - self.buf.push_str(s); - } - - fn writeln(&mut self, s: &str) { - if s.is_empty() { - return; - } - self.write(s); - self.buf.push('\n'); - self.start = true; - } - - fn visit_var(&mut self, s: &[u8]) { - self.write(&format!("self.{}", str::from_utf8(s).unwrap())); - } - - fn visit_filter(&mut self, name: &str, val: &Expr) { - self.write(&format!("askama::filters::{}(&", name)); - self.visit_expr(val); - self.write(")"); - } - - fn visit_expr(&mut self, expr: &Expr) { - match expr { - &Expr::Var(s) => self.visit_var(s), - &Expr::Filter(name, ref val) => self.visit_filter(name, &val), - } - } - - fn write_lit(&mut self, s: &[u8]) { - self.write("buf.push_str("); - self.write(&format!("{:#?}", str::from_utf8(s).unwrap())); - self.writeln(");"); - } - - fn write_expr(&mut self, s: &Expr) { - self.write("std::fmt::Write::write_fmt(&mut buf, format_args!(\"{}\", "); - self.visit_expr(s); - self.writeln(")).unwrap();"); - } - - fn handle(&mut self, tokens: &Vec) { - for n in tokens { - match n { - &Node::Lit(val) => { self.write_lit(val); }, - &Node::Expr(ref val) => { self.write_expr(&val); }, - } - } - } - - fn finalize(&mut self) { - self.writeln("buf"); - self.dedent(); - self.writeln("}"); - self.dedent(); - self.writeln("}"); - } - - fn result(self) -> String { - self.buf - } - -} - -pub fn generate(ctx_name: &str, tokens: &Vec) -> String { - let mut gen = Generator::new(); - gen.init(ctx_name); - gen.handle(tokens); - gen.finalize(); - gen.result() -} diff --git a/askama_derive/src/lib.rs b/askama_derive/src/lib.rs index 3b8fe55..8613ead 100644 --- a/askama_derive/src/lib.rs +++ b/askama_derive/src/lib.rs @@ -1,13 +1,9 @@ #![feature(proc_macro, proc_macro_lib)] -#[macro_use] -extern crate nom; +extern crate askama; extern crate proc_macro; extern crate syn; -mod generator; -mod parser; - use proc_macro::TokenStream; use std::fs::File; use std::io::Read; @@ -65,6 +61,6 @@ pub fn derive_template(input: TokenStream) -> TokenStream { let name = &ast.ident; let path = get_path_from_attrs(&ast.attrs); let src = get_template_source(&path); - let tokens = parser::parse(&src); - generator::generate(name.as_ref(), &tokens).parse().unwrap() + let tokens = askama::parser::parse(&src); + askama::generator::generate(name.as_ref(), &tokens).parse().unwrap() } 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