From 038975b348b03dd3063d14cabd30fa71b010baea Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 19 Aug 2019 12:26:34 +0300 Subject: plug new boilerplate_gen into ra_tools --- crates/ra_tools/src/boilerplate_gen.rs | 34 ++++++++++++++++++++-------------- crates/ra_tools/src/lib.rs | 2 +- crates/ra_tools/src/main.rs | 6 +++--- 3 files changed, 24 insertions(+), 18 deletions(-) (limited to 'crates/ra_tools/src') diff --git a/crates/ra_tools/src/boilerplate_gen.rs b/crates/ra_tools/src/boilerplate_gen.rs index 5547d6969..7ef51e82a 100644 --- a/crates/ra_tools/src/boilerplate_gen.rs +++ b/crates/ra_tools/src/boilerplate_gen.rs @@ -2,7 +2,6 @@ use std::{ collections::BTreeMap, fs, io::Write, - path::Path, process::{Command, Stdio}, }; @@ -12,7 +11,7 @@ use quote::{format_ident, quote}; use ron; use serde::Deserialize; -use crate::{project_root, Mode, Result, AST, GRAMMAR, SYNTAX_KINDS}; +use crate::{project_root, update, Mode, Result, AST, GRAMMAR, SYNTAX_KINDS}; pub fn generate_boilerplate(mode: Mode) -> Result<()> { let grammar = project_root().join(GRAMMAR); @@ -21,11 +20,14 @@ pub fn generate_boilerplate(mode: Mode) -> Result<()> { ron::de::from_str(&text)? }; - let _syntax_kinds = project_root().join(SYNTAX_KINDS); - let _ast = project_root().join(AST); + let syntax_kinds_file = project_root().join(SYNTAX_KINDS); + let syntax_kinds = generate_syntax_kinds(&grammar)?; + update(syntax_kinds_file.as_path(), &syntax_kinds, mode)?; + + let ast_file = project_root().join(AST); + let ast = generate_ast(&grammar)?; + update(ast_file.as_path(), &ast, mode)?; - let ast = generate_syntax_kinds(&grammar)?; - println!("{}", ast); Ok(()) } @@ -172,10 +174,14 @@ fn generate_syntax_kinds(grammar: &Grammar) -> Result { .chain(grammar.multi_byte_tokens.iter().map(|(_token, name)| format_ident!("{}", name))) .collect::>(); - let keywords_values = + let full_keywords_values = &grammar.keywords; + let full_keywords = + full_keywords_values.iter().map(|kw| format_ident!("{}_KW", kw.to_shouty_snake_case())); + + let all_keywords_values = grammar.keywords.iter().chain(grammar.contextual_keywords.iter()).collect::>(); - let keywords_idents = keywords_values.iter().map(|kw| format_ident!("{}", kw)); - let keywords = keywords_values + let all_keywords_idents = all_keywords_values.iter().map(|kw| format_ident!("{}", kw)); + let all_keywords = all_keywords_values .iter() .map(|name| format_ident!("{}_KW", name.to_shouty_snake_case())) .collect::>(); @@ -202,7 +208,7 @@ fn generate_syntax_kinds(grammar: &Grammar) -> Result { #[doc(hidden)] EOF, #(#punctuation,)* - #(#keywords,)* + #(#all_keywords,)* #(#literals,)* #(#tokens,)* #(#nodes,)* @@ -229,7 +235,7 @@ fn generate_syntax_kinds(grammar: &Grammar) -> Result { impl SyntaxKind { pub fn is_keyword(self) -> bool { match self { - #(#keywords)|* => true, + #(#all_keywords)|* => true, _ => false, } } @@ -251,7 +257,7 @@ fn generate_syntax_kinds(grammar: &Grammar) -> Result { pub(crate) fn info(self) -> &'static SyntaxInfo { match self { #(#punctuation => &SyntaxInfo { name: stringify!(#punctuation) },)* - #(#keywords => &SyntaxInfo { name: stringify!(#keywords) },)* + #(#all_keywords => &SyntaxInfo { name: stringify!(#all_keywords) },)* #(#literals => &SyntaxInfo { name: stringify!(#literals) },)* #(#tokens => &SyntaxInfo { name: stringify!(#tokens) },)* #(#nodes => &SyntaxInfo { name: stringify!(#nodes) },)* @@ -263,7 +269,7 @@ fn generate_syntax_kinds(grammar: &Grammar) -> Result { pub fn from_keyword(ident: &str) -> Option { let kw = match ident { - #(#keywords_values => #keywords,)* + #(#full_keywords_values => #full_keywords,)* _ => return None, }; Some(kw) @@ -281,7 +287,7 @@ fn generate_syntax_kinds(grammar: &Grammar) -> Result { #[macro_export] macro_rules! T { #((#punctuation_values) => { $crate::SyntaxKind::#punctuation };)* - #((#keywords_idents) => { $crate::SyntaxKind::#keywords };)* + #((#all_keywords_idents) => { $crate::SyntaxKind::#all_keywords };)* } }; diff --git a/crates/ra_tools/src/lib.rs b/crates/ra_tools/src/lib.rs index f1829d55a..d47660369 100644 --- a/crates/ra_tools/src/lib.rs +++ b/crates/ra_tools/src/lib.rs @@ -20,7 +20,7 @@ const GRAMMAR_DIR: &str = "crates/ra_parser/src/grammar"; const OK_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/ok"; const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/err"; -pub const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs.tera"; +pub const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs"; pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs"; const TOOLCHAIN: &str = "stable"; diff --git a/crates/ra_tools/src/main.rs b/crates/ra_tools/src/main.rs index 54d96e446..03cb9d5a7 100644 --- a/crates/ra_tools/src/main.rs +++ b/crates/ra_tools/src/main.rs @@ -1,8 +1,8 @@ use clap::{App, Arg, SubCommand}; use core::str; use ra_tools::{ - gen_tests, generate, install_format_hook, run, run_clippy, run_fuzzer, run_rustfmt, Cmd, - Overwrite, Result, + gen_tests, generate_boilerplate, install_format_hook, run, run_clippy, run_fuzzer, run_rustfmt, + Cmd, Overwrite, Result, }; use std::{env, path::PathBuf}; @@ -49,7 +49,7 @@ fn main() -> Result<()> { install(opts)? } ("gen-tests", _) => gen_tests(Overwrite)?, - ("gen-syntax", _) => generate(Overwrite)?, + ("gen-syntax", _) => generate_boilerplate(Overwrite)?, ("format", _) => run_rustfmt(Overwrite)?, ("format-hook", _) => install_format_hook()?, ("lint", _) => run_clippy()?, -- cgit v1.2.3