From 60e8a845cafb9630caa034b886368160a12dcd64 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Jul 2018 18:02:13 +0300 Subject: simplify codegen --- tools/src/main.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'tools/src/main.rs') diff --git a/tools/src/main.rs b/tools/src/main.rs index 3290d1dce..7d7b2afc0 100644 --- a/tools/src/main.rs +++ b/tools/src/main.rs @@ -6,7 +6,7 @@ extern crate tera; extern crate walkdir; extern crate tools; -use std::{collections::HashSet, fs, path::Path}; +use std::{collections::{HashSet, HashMap}, fs, path::Path}; use clap::{App, Arg, SubCommand}; use tools::{collect_tests, Test}; @@ -63,9 +63,26 @@ fn update(path: &Path, contents: &str, verify: bool) -> Result<()> { fn get_kinds() -> Result { let grammar = grammar()?; let template = fs::read_to_string(SYNTAX_KINDS_TEMPLATE)?; - let ret = tera::Tera::one_off(&template, &grammar, false) + let mut tera = tera::Tera::default(); + tera.add_raw_template("grammar", &template) .map_err(|e| format_err!("template error: {:?}", e))?; - Ok(ret) + tera.register_global_function("concat", Box::new(concat)); + let ret = tera.render("grammar", &grammar) + .map_err(|e| format_err!("template error: {:?}", e))?; + return Ok(ret); + + fn concat(args: HashMap) -> tera::Result { + let mut elements = Vec::new(); + for &key in ["a", "b", "c"].iter() { + let val = match args.get(key) { + Some(val) => val, + None => continue, + }; + let val = val.as_array().unwrap(); + elements.extend(val.iter().cloned()); + } + Ok(tera::Value::Array(elements)) + } } fn grammar() -> Result { -- cgit v1.2.3