From 514aa3cf853c3c4beef8f827c12328d626f977de Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 16 Oct 2018 20:28:43 +0300 Subject: extract teraron --- crates/tools/Cargo.toml | 4 +--- crates/tools/src/lib.rs | 50 +++--------------------------------------------- crates/tools/src/main.rs | 45 ++++++++++++++++++++++--------------------- 3 files changed, 27 insertions(+), 72 deletions(-) (limited to 'crates/tools') diff --git a/crates/tools/Cargo.toml b/crates/tools/Cargo.toml index e2fecc60d..ffd0ab1a5 100644 --- a/crates/tools/Cargo.toml +++ b/crates/tools/Cargo.toml @@ -6,10 +6,8 @@ authors = ["Aleksey Kladov "] publish = false [dependencies] -ron = "0.3.0" +teraron = { path = "../teraron"} walkdir = "2.1.3" itertools = "0.7.8" -tera = "0.11" clap = "2.32.0" failure = "0.1.1" -heck = "0.3.0" diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs index 5d5d372bb..63ede5315 100644 --- a/crates/tools/src/lib.rs +++ b/crates/tools/src/lib.rs @@ -1,25 +1,18 @@ extern crate itertools; #[macro_use] extern crate failure; -extern crate heck; -extern crate ron; -extern crate tera; -use heck::{CamelCase, ShoutySnakeCase, SnakeCase}; use itertools::Itertools; use std::{ - collections::HashMap, fs, path::{Path, PathBuf}, }; pub type Result = ::std::result::Result; -const GRAMMAR: &str = "ra_syntax/src/grammar.ron"; -pub const SYNTAX_KINDS: &str = "ra_syntax/src/syntax_kinds/generated.rs"; -pub const SYNTAX_KINDS_TEMPLATE: &str = "ra_syntax/src/syntax_kinds/generated.rs.tera"; -pub const AST: &str = "ra_syntax/src/ast/generated.rs"; -pub const AST_TEMPLATE: &str = "ra_syntax/src/ast/generated.rs.tera"; +pub const GRAMMAR: &str = "ra_syntax/src/grammar.ron"; +pub const SYNTAX_KINDS: &str = "ra_syntax/src/syntax_kinds/generated.rs.tera"; +pub const AST: &str = "ra_syntax/src/ast/generated.rs.tera"; #[derive(Debug)] pub struct Test { @@ -76,43 +69,6 @@ pub fn update(path: &Path, contents: &str, verify: bool) -> Result<()> { Ok(()) } -pub fn render_template(template: &Path) -> Result { - let grammar: ron::value::Value = { - let text = fs::read_to_string(project_root().join(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_function("concat", Box::new(concat)); - tera.register_filter("camel", |arg, _| { - Ok(arg.as_str().unwrap().to_camel_case().into()) - }); - tera.register_filter("snake", |arg, _| { - Ok(arg.as_str().unwrap().to_snake_case().into()) - }); - tera.register_filter("SCREAM", |arg, _| { - Ok(arg.as_str().unwrap().to_shouty_snake_case().into()) - }); - 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)) - } -} pub fn project_root() -> PathBuf { Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()) diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs index b662d78df..1bbc43123 100644 --- a/crates/tools/src/main.rs +++ b/crates/tools/src/main.rs @@ -3,6 +3,7 @@ extern crate clap; extern crate failure; extern crate tools; extern crate walkdir; +extern crate teraron; use clap::{App, Arg, SubCommand}; use std::{ @@ -12,9 +13,9 @@ use std::{ process::Command, }; use tools::{ - collect_tests, project_root, render_template, update, Result, Test, AST, AST_TEMPLATE, - SYNTAX_KINDS, SYNTAX_KINDS_TEMPLATE, + collect_tests, project_root, Result, Test, AST, SYNTAX_KINDS, GRAMMAR, }; +use teraron::{Mode, Verify, Overwrite}; const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar"; const INLINE_TESTS_DIR: &str = "./crates/ra_syntax/tests/data/parser/inline"; @@ -32,35 +33,35 @@ fn main() -> Result<()> { .subcommand(SubCommand::with_name("gen-tests")) .subcommand(SubCommand::with_name("install-code")) .get_matches(); + let mode = if matches.is_present("verify") { + Verify + } else { + Overwrite + }; match matches.subcommand() { ("install-code", _) => install_code_extension()?, - (name, Some(matches)) => run_gen_command(name, matches.is_present("verify"))?, - _ => unreachable!(), - } - Ok(()) -} - -fn run_gen_command(name: &str, verify: bool) -> Result<()> { - match name { - "gen-kinds" => { - update( - &project_root().join(SYNTAX_KINDS), - &render_template(&project_root().join(SYNTAX_KINDS_TEMPLATE))?, - verify, + ("gen-tests", _) => gen_tests(mode)?, + ("gen-kinds", _) => { + let grammar = project_root().join(GRAMMAR); + let syntax_kinds = project_root().join(SYNTAX_KINDS); + let ast = project_root().join(AST); + teraron::generate( + &syntax_kinds, + &grammar, + mode, )?; - update( - &project_root().join(AST), - &render_template(&project_root().join(AST_TEMPLATE))?, - verify, + teraron::generate( + &ast, + &grammar, + mode, )?; } - "gen-tests" => gen_tests(verify)?, _ => unreachable!(), } Ok(()) } -fn gen_tests(verify: bool) -> Result<()> { +fn gen_tests(mode: Mode) -> Result<()> { let tests = tests_from_dir(Path::new(GRAMMAR_DIR))?; let inline_tests_dir = Path::new(INLINE_TESTS_DIR); @@ -83,7 +84,7 @@ fn gen_tests(verify: bool) -> Result<()> { inline_tests_dir.join(file_name) } }; - update(&path, &test.text, verify)?; + teraron::update(&path, &test.text, mode)?; } Ok(()) } -- cgit v1.2.3