From d8b2a5efc0e5de3b0d72f29ccc86185f0827c9d3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 9 Aug 2018 17:43:39 +0300 Subject: Generate AST --- tools/src/main.rs | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) (limited to 'tools/src') diff --git a/tools/src/main.rs b/tools/src/main.rs index 714158f55..b5c966f74 100644 --- a/tools/src/main.rs +++ b/tools/src/main.rs @@ -23,6 +23,8 @@ const INLINE_TESTS_DIR: &str = "tests/data/parser/inline"; const GRAMMAR: &str = "./src/grammar.ron"; const SYNTAX_KINDS: &str = "./src/syntax_kinds/generated.rs"; const SYNTAX_KINDS_TEMPLATE: &str = "./src/syntax_kinds/generated.rs.tera"; +const AST: &str = "./src/ast/generated.rs"; +const AST_TEMPLATE: &str = "./src/ast/generated.rs.tera"; fn main() -> Result<()> { let matches = App::new("tasks") @@ -47,10 +49,16 @@ fn main() -> Result<()> { fn run_gen_command(name: &str, verify: bool) -> Result<()> { match name { - "gen-kinds" => update(Path::new(SYNTAX_KINDS), &get_kinds()?, verify), - "gen-tests" => gen_tests(verify), + "gen-kinds" => { + update(Path::new(SYNTAX_KINDS), &render_template(SYNTAX_KINDS_TEMPLATE)?, verify)?; + update(Path::new(AST), &render_template(AST_TEMPLATE)?, verify)?; + }, + "gen-tests" => { + gen_tests(verify)? + }, _ => unreachable!(), } + Ok(()) } fn update(path: &Path, contents: &str, verify: bool) -> Result<()> { @@ -68,13 +76,30 @@ fn update(path: &Path, contents: &str, verify: bool) -> Result<()> { Ok(()) } -fn get_kinds() -> Result { - let grammar = grammar()?; - let template = fs::read_to_string(SYNTAX_KINDS_TEMPLATE)?; +fn render_template(template: &str) -> Result { + let grammar: ron::value::Value = { + let text = fs::read_to_string(GRAMMAR)?; + ron::de::from_str(&text)? + }; + let template = fs::read_to_string(template)?; let mut tera = tera::Tera::default(); tera.add_raw_template("grammar", &template) .map_err(|e| format_err!("template error: {:?}", e))?; tera.register_global_function("concat", Box::new(concat)); + tera.register_filter("camel", |arg, _| { + Ok(arg.as_str().unwrap() + .split("_") + .flat_map(|word| { + word.chars() + .next().unwrap() + .to_uppercase() + .chain( + word.chars().skip(1).flat_map(|c| c.to_lowercase()) + ) + }) + .collect::() + .into()) + }); let ret = tera .render("grammar", &grammar) .map_err(|e| format_err!("template error: {:?}", e))?; @@ -94,12 +119,6 @@ fn get_kinds() -> Result { } } -fn grammar() -> Result { - let text = fs::read_to_string(GRAMMAR)?; - let ret = ron::de::from_str(&text)?; - Ok(ret) -} - fn gen_tests(verify: bool) -> Result<()> { let tests = tests_from_dir(Path::new(GRAMMAR_DIR))?; -- cgit v1.2.3