aboutsummaryrefslogtreecommitdiff
path: root/crates/tools
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-10-16 18:28:43 +0100
committerAleksey Kladov <[email protected]>2018-10-16 18:31:30 +0100
commit514aa3cf853c3c4beef8f827c12328d626f977de (patch)
tree2a10336c3825ad821960c5169b78df17f7bd5f5c /crates/tools
parent1216878f7be20dd0e652fb8cdc395009fdcfae07 (diff)
extract teraron
Diffstat (limited to 'crates/tools')
-rw-r--r--crates/tools/Cargo.toml4
-rw-r--r--crates/tools/src/lib.rs50
-rw-r--r--crates/tools/src/main.rs45
3 files changed, 27 insertions, 72 deletions
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 <[email protected]>"]
6publish = false 6publish = false
7 7
8[dependencies] 8[dependencies]
9ron = "0.3.0" 9teraron = { path = "../teraron"}
10walkdir = "2.1.3" 10walkdir = "2.1.3"
11itertools = "0.7.8" 11itertools = "0.7.8"
12tera = "0.11"
13clap = "2.32.0" 12clap = "2.32.0"
14failure = "0.1.1" 13failure = "0.1.1"
15heck = "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 @@
1extern crate itertools; 1extern crate itertools;
2#[macro_use] 2#[macro_use]
3extern crate failure; 3extern crate failure;
4extern crate heck;
5extern crate ron;
6extern crate tera;
7 4
8use heck::{CamelCase, ShoutySnakeCase, SnakeCase};
9use itertools::Itertools; 5use itertools::Itertools;
10use std::{ 6use std::{
11 collections::HashMap,
12 fs, 7 fs,
13 path::{Path, PathBuf}, 8 path::{Path, PathBuf},
14}; 9};
15 10
16pub type Result<T> = ::std::result::Result<T, failure::Error>; 11pub type Result<T> = ::std::result::Result<T, failure::Error>;
17 12
18const GRAMMAR: &str = "ra_syntax/src/grammar.ron"; 13pub const GRAMMAR: &str = "ra_syntax/src/grammar.ron";
19pub const SYNTAX_KINDS: &str = "ra_syntax/src/syntax_kinds/generated.rs"; 14pub const SYNTAX_KINDS: &str = "ra_syntax/src/syntax_kinds/generated.rs.tera";
20pub const SYNTAX_KINDS_TEMPLATE: &str = "ra_syntax/src/syntax_kinds/generated.rs.tera"; 15pub const AST: &str = "ra_syntax/src/ast/generated.rs.tera";
21pub const AST: &str = "ra_syntax/src/ast/generated.rs";
22pub const AST_TEMPLATE: &str = "ra_syntax/src/ast/generated.rs.tera";
23 16
24#[derive(Debug)] 17#[derive(Debug)]
25pub struct Test { 18pub struct Test {
@@ -76,43 +69,6 @@ pub fn update(path: &Path, contents: &str, verify: bool) -> Result<()> {
76 Ok(()) 69 Ok(())
77} 70}
78 71
79pub fn render_template(template: &Path) -> Result<String> {
80 let grammar: ron::value::Value = {
81 let text = fs::read_to_string(project_root().join(GRAMMAR))?;
82 ron::de::from_str(&text)?
83 };
84 let template = fs::read_to_string(template)?;
85 let mut tera = tera::Tera::default();
86 tera.add_raw_template("grammar", &template)
87 .map_err(|e| format_err!("template error: {:?}", e))?;
88 tera.register_function("concat", Box::new(concat));
89 tera.register_filter("camel", |arg, _| {
90 Ok(arg.as_str().unwrap().to_camel_case().into())
91 });
92 tera.register_filter("snake", |arg, _| {
93 Ok(arg.as_str().unwrap().to_snake_case().into())
94 });
95 tera.register_filter("SCREAM", |arg, _| {
96 Ok(arg.as_str().unwrap().to_shouty_snake_case().into())
97 });
98 let ret = tera
99 .render("grammar", &grammar)
100 .map_err(|e| format_err!("template error: {:?}", e))?;
101 return Ok(ret);
102
103 fn concat(args: HashMap<String, tera::Value>) -> tera::Result<tera::Value> {
104 let mut elements = Vec::new();
105 for &key in ["a", "b", "c"].iter() {
106 let val = match args.get(key) {
107 Some(val) => val,
108 None => continue,
109 };
110 let val = val.as_array().unwrap();
111 elements.extend(val.iter().cloned());
112 }
113 Ok(tera::Value::Array(elements))
114 }
115}
116 72
117pub fn project_root() -> PathBuf { 73pub fn project_root() -> PathBuf {
118 Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()) 74 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;
3extern crate failure; 3extern crate failure;
4extern crate tools; 4extern crate tools;
5extern crate walkdir; 5extern crate walkdir;
6extern crate teraron;
6 7
7use clap::{App, Arg, SubCommand}; 8use clap::{App, Arg, SubCommand};
8use std::{ 9use std::{
@@ -12,9 +13,9 @@ use std::{
12 process::Command, 13 process::Command,
13}; 14};
14use tools::{ 15use tools::{
15 collect_tests, project_root, render_template, update, Result, Test, AST, AST_TEMPLATE, 16 collect_tests, project_root, Result, Test, AST, SYNTAX_KINDS, GRAMMAR,
16 SYNTAX_KINDS, SYNTAX_KINDS_TEMPLATE,
17}; 17};
18use teraron::{Mode, Verify, Overwrite};
18 19
19const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar"; 20const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar";
20const INLINE_TESTS_DIR: &str = "./crates/ra_syntax/tests/data/parser/inline"; 21const INLINE_TESTS_DIR: &str = "./crates/ra_syntax/tests/data/parser/inline";
@@ -32,35 +33,35 @@ fn main() -> Result<()> {
32 .subcommand(SubCommand::with_name("gen-tests")) 33 .subcommand(SubCommand::with_name("gen-tests"))
33 .subcommand(SubCommand::with_name("install-code")) 34 .subcommand(SubCommand::with_name("install-code"))
34 .get_matches(); 35 .get_matches();
36 let mode = if matches.is_present("verify") {
37 Verify
38 } else {
39 Overwrite
40 };
35 match matches.subcommand() { 41 match matches.subcommand() {
36 ("install-code", _) => install_code_extension()?, 42 ("install-code", _) => install_code_extension()?,
37 (name, Some(matches)) => run_gen_command(name, matches.is_present("verify"))?, 43 ("gen-tests", _) => gen_tests(mode)?,
38 _ => unreachable!(), 44 ("gen-kinds", _) => {
39 } 45 let grammar = project_root().join(GRAMMAR);
40 Ok(()) 46 let syntax_kinds = project_root().join(SYNTAX_KINDS);
41} 47 let ast = project_root().join(AST);
42 48 teraron::generate(
43fn run_gen_command(name: &str, verify: bool) -> Result<()> { 49 &syntax_kinds,
44 match name { 50 &grammar,
45 "gen-kinds" => { 51 mode,
46 update(
47 &project_root().join(SYNTAX_KINDS),
48 &render_template(&project_root().join(SYNTAX_KINDS_TEMPLATE))?,
49 verify,
50 )?; 52 )?;
51 update( 53 teraron::generate(
52 &project_root().join(AST), 54 &ast,
53 &render_template(&project_root().join(AST_TEMPLATE))?, 55 &grammar,
54 verify, 56 mode,
55 )?; 57 )?;
56 } 58 }
57 "gen-tests" => gen_tests(verify)?,
58 _ => unreachable!(), 59 _ => unreachable!(),
59 } 60 }
60 Ok(()) 61 Ok(())
61} 62}
62 63
63fn gen_tests(verify: bool) -> Result<()> { 64fn gen_tests(mode: Mode) -> Result<()> {
64 let tests = tests_from_dir(Path::new(GRAMMAR_DIR))?; 65 let tests = tests_from_dir(Path::new(GRAMMAR_DIR))?;
65 66
66 let inline_tests_dir = Path::new(INLINE_TESTS_DIR); 67 let inline_tests_dir = Path::new(INLINE_TESTS_DIR);
@@ -83,7 +84,7 @@ fn gen_tests(verify: bool) -> Result<()> {
83 inline_tests_dir.join(file_name) 84 inline_tests_dir.join(file_name)
84 } 85 }
85 }; 86 };
86 update(&path, &test.text, verify)?; 87 teraron::update(&path, &test.text, mode)?;
87 } 88 }
88 Ok(()) 89 Ok(())
89} 90}