Pass entire AST to code generator

This commit is contained in:
Dirkjan Ochtman 2017-02-04 11:19:47 +01:00
parent 69238e55d3
commit 3478395ca9
5 changed files with 7 additions and 4 deletions

1
Cargo.lock generated
View File

@ -12,6 +12,7 @@ version = "0.1.0"
dependencies = [
"htmlescape 0.3.1 (git+https://github.com/veddan/rust-htmlescape)",
"nom 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.11.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]

View File

@ -10,3 +10,4 @@ workspace = ".."
[dependencies]
htmlescape = { git = "https://github.com/veddan/rust-htmlescape" }
nom = "2.0"
syn = "0.11"

View File

@ -1,6 +1,7 @@
use parser::{Conds, Expr, Node, Nodes, Target};
use std::str;
use std::collections::HashSet;
use syn;
struct Generator {
buf: String,
@ -170,9 +171,9 @@ impl Generator {
}
pub fn generate(ctx_name: &str, tokens: &Vec<Node>) -> String {
pub fn generate(ast: &syn::DeriveInput, tokens: &Vec<Node>) -> String {
let mut gen = Generator::new();
gen.init(ctx_name);
gen.init(ast.ident.as_ref());
gen.handle(tokens);
gen.finalize();
gen.result()

View File

@ -1,5 +1,6 @@
#[macro_use]
extern crate nom;
extern crate syn;
pub trait Template {
fn render(&self) -> String;

View File

@ -56,9 +56,8 @@ pub fn derive_template(input: TokenStream) -> TokenStream {
_ => panic!("#[derive(Template)] can only be used with structs"),
};
let name = &ast.ident;
let path = get_path_from_attrs(&ast.attrs);
let src = get_template_source(&path);
let tokens = askama::parser::parse(&src);
askama::generator::generate(name.as_ref(), &tokens).parse().unwrap()
askama::generator::generate(&ast, &tokens).parse().unwrap()
}