From 14fd9e72a7e5a742996ca1b7d86234b51e6e84a5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 18 Aug 2019 21:33:31 +0300 Subject: add quote dep --- crates/ra_tools/Cargo.toml | 1 + crates/ra_tools/src/codegen.rs | 1 + crates/ra_tools/src/lib.rs | 2 ++ 3 files changed, 4 insertions(+) create mode 100644 crates/ra_tools/src/codegen.rs (limited to 'crates/ra_tools') diff --git a/crates/ra_tools/Cargo.toml b/crates/ra_tools/Cargo.toml index 9c5430992..a70079b95 100644 --- a/crates/ra_tools/Cargo.toml +++ b/crates/ra_tools/Cargo.toml @@ -10,3 +10,4 @@ teraron = "0.1.0" walkdir = "2.1.3" itertools = "0.8.0" clap = "2.32.0" +quote = "1.0.2" diff --git a/crates/ra_tools/src/codegen.rs b/crates/ra_tools/src/codegen.rs new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/crates/ra_tools/src/codegen.rs @@ -0,0 +1 @@ + diff --git a/crates/ra_tools/src/lib.rs b/crates/ra_tools/src/lib.rs index bb7845f7d..96290ecc4 100644 --- a/crates/ra_tools/src/lib.rs +++ b/crates/ra_tools/src/lib.rs @@ -1,3 +1,5 @@ +mod codegen; + use std::{ collections::HashMap, error::Error, -- cgit v1.2.3 From 839d9cce89adcae23dfd108acf231dca00724f08 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 18 Aug 2019 21:34:55 +0300 Subject: codegen boilerplate --- crates/ra_tools/src/codegen.rs | 6 ++++++ crates/ra_tools/src/lib.rs | 2 ++ 2 files changed, 8 insertions(+) (limited to 'crates/ra_tools') diff --git a/crates/ra_tools/src/codegen.rs b/crates/ra_tools/src/codegen.rs index 8b1378917..edb01aceb 100644 --- a/crates/ra_tools/src/codegen.rs +++ b/crates/ra_tools/src/codegen.rs @@ -1 +1,7 @@ +use std::path::Path; +use crate::{Mode, Result}; + +pub fn generate_ast(grammar_src: &Path, dst: &Path, mode: Mode) -> Result<()> { + Ok(()) +} diff --git a/crates/ra_tools/src/lib.rs b/crates/ra_tools/src/lib.rs index 96290ecc4..2cbb07ebf 100644 --- a/crates/ra_tools/src/lib.rs +++ b/crates/ra_tools/src/lib.rs @@ -13,6 +13,8 @@ use itertools::Itertools; pub use teraron::{Mode, Overwrite, Verify}; +pub use self::codegen::generate_ast; + pub type Result = std::result::Result>; pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron"; -- cgit v1.2.3 From 229d7943d8471eb114f961ab419c318736772da4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 18 Aug 2019 21:44:24 +0300 Subject: switch to new codegen --- crates/ra_tools/Cargo.toml | 1 + crates/ra_tools/src/codegen.rs | 20 +++++++++++++++++--- crates/ra_tools/src/lib.rs | 13 ++----------- 3 files changed, 20 insertions(+), 14 deletions(-) (limited to 'crates/ra_tools') diff --git a/crates/ra_tools/Cargo.toml b/crates/ra_tools/Cargo.toml index a70079b95..02bab8f52 100644 --- a/crates/ra_tools/Cargo.toml +++ b/crates/ra_tools/Cargo.toml @@ -11,3 +11,4 @@ walkdir = "2.1.3" itertools = "0.8.0" clap = "2.32.0" quote = "1.0.2" +ron = "0.5.1" diff --git a/crates/ra_tools/src/codegen.rs b/crates/ra_tools/src/codegen.rs index edb01aceb..405fb623c 100644 --- a/crates/ra_tools/src/codegen.rs +++ b/crates/ra_tools/src/codegen.rs @@ -1,7 +1,21 @@ -use std::path::Path; +use std::{fs, path::Path}; -use crate::{Mode, Result}; +use ron; -pub fn generate_ast(grammar_src: &Path, dst: &Path, mode: Mode) -> Result<()> { +use crate::{project_root, Mode, Result, AST, GRAMMAR}; + +pub fn generate(mode: Mode) -> Result<()> { + let grammar = project_root().join(GRAMMAR); + // let syntax_kinds = project_root().join(SYNTAX_KINDS); + let ast = project_root().join(AST); + generate_ast(&grammar, &ast, mode) +} + +fn generate_ast(grammar_src: &Path, dst: &Path, mode: Mode) -> Result<()> { + let src: ron::Value = { + let text = fs::read_to_string(grammar_src)?; + ron::de::from_str(&text)? + }; + eprintln!("{:?}", src); Ok(()) } diff --git a/crates/ra_tools/src/lib.rs b/crates/ra_tools/src/lib.rs index 2cbb07ebf..de8c472d1 100644 --- a/crates/ra_tools/src/lib.rs +++ b/crates/ra_tools/src/lib.rs @@ -13,7 +13,7 @@ use itertools::Itertools; pub use teraron::{Mode, Overwrite, Verify}; -pub use self::codegen::generate_ast; +pub use self::codegen::generate; pub type Result = std::result::Result>; @@ -23,7 +23,7 @@ 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 AST: &str = "crates/ra_syntax/src/ast/generated.rs.tera"; +pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs"; const TOOLCHAIN: &str = "stable"; #[derive(Debug)] @@ -70,15 +70,6 @@ pub fn collect_tests(s: &str) -> Vec<(usize, Test)> { res } -pub fn generate(mode: Mode) -> Result<()> { - 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)?; - teraron::generate(&ast, &grammar, mode)?; - Ok(()) -} - pub fn project_root() -> PathBuf { Path::new(&env!("CARGO_MANIFEST_DIR")).ancestors().nth(2).unwrap().to_path_buf() } -- cgit v1.2.3 From d545a5c75cb181758dd745b031eacfd7fc8a6929 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 18 Aug 2019 21:54:51 +0300 Subject: deserialize grammar --- crates/ra_tools/Cargo.toml | 1 + crates/ra_tools/src/codegen.rs | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) (limited to 'crates/ra_tools') diff --git a/crates/ra_tools/Cargo.toml b/crates/ra_tools/Cargo.toml index 02bab8f52..ab9fa5d86 100644 --- a/crates/ra_tools/Cargo.toml +++ b/crates/ra_tools/Cargo.toml @@ -12,3 +12,4 @@ itertools = "0.8.0" clap = "2.32.0" quote = "1.0.2" ron = "0.5.1" +serde = { version = "1.0.0", features = ["derive"] } diff --git a/crates/ra_tools/src/codegen.rs b/crates/ra_tools/src/codegen.rs index 405fb623c..f0a54808a 100644 --- a/crates/ra_tools/src/codegen.rs +++ b/crates/ra_tools/src/codegen.rs @@ -1,6 +1,8 @@ -use std::{fs, path::Path}; +use std::{collections::BTreeMap, fs, path::Path}; +use quote::quote; use ron; +use serde::Deserialize; use crate::{project_root, Mode, Result, AST, GRAMMAR}; @@ -12,10 +14,38 @@ pub fn generate(mode: Mode) -> Result<()> { } fn generate_ast(grammar_src: &Path, dst: &Path, mode: Mode) -> Result<()> { - let src: ron::Value = { + let src: Grammar = { let text = fs::read_to_string(grammar_src)?; ron::de::from_str(&text)? }; - eprintln!("{:?}", src); + eprintln!("{:#?}", src); Ok(()) } + +#[derive(Deserialize, Debug)] +struct Grammar { + single_byte_tokens: Vec<(String, String)>, + multi_byte_tokens: Vec<(String, String)>, + keywords: Vec, + contextual_keywords: Vec, + literals: Vec, + tokens: Vec, + ast: BTreeMap, +} + +#[derive(Deserialize, Debug)] +struct AstNode { + #[serde(default)] + traits: Vec, + #[serde(default)] + collections: Vec, + #[serde(default)] + options: Vec, +} + +#[derive(Deserialize, Debug)] +#[serde(untagged)] +enum Attr { + Type(String), + NameType(String, String), +} -- cgit v1.2.3 From 8cefdb5527d011d7d5ca2902791b7c3da0276fec Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 18 Aug 2019 23:11:08 +0300 Subject: use quote! macro to generate grammar We already use syn"e elsewhere (transitively), so it make sense to cut down on the number of technologies and get rid of tera --- crates/ra_tools/Cargo.toml | 1 + crates/ra_tools/src/codegen.rs | 165 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 153 insertions(+), 13 deletions(-) (limited to 'crates/ra_tools') diff --git a/crates/ra_tools/Cargo.toml b/crates/ra_tools/Cargo.toml index ab9fa5d86..1bb6fb71c 100644 --- a/crates/ra_tools/Cargo.toml +++ b/crates/ra_tools/Cargo.toml @@ -12,4 +12,5 @@ itertools = "0.8.0" clap = "2.32.0" quote = "1.0.2" ron = "0.5.1" +heck = "0.3.0" serde = { version = "1.0.0", features = ["derive"] } diff --git a/crates/ra_tools/src/codegen.rs b/crates/ra_tools/src/codegen.rs index f0a54808a..e14092704 100644 --- a/crates/ra_tools/src/codegen.rs +++ b/crates/ra_tools/src/codegen.rs @@ -1,27 +1,162 @@ -use std::{collections::BTreeMap, fs, path::Path}; +use std::{ + collections::BTreeMap, + fs, + io::Write, + path::Path, + process::{Command, Stdio}, +}; -use quote::quote; +use heck::{ShoutySnakeCase, SnakeCase}; +use quote::{format_ident, quote}; use ron; use serde::Deserialize; -use crate::{project_root, Mode, Result, AST, GRAMMAR}; +use crate::{project_root, Mode, Result, AST, GRAMMAR, SYNTAX_KINDS}; pub fn generate(mode: Mode) -> Result<()> { let grammar = project_root().join(GRAMMAR); - // let syntax_kinds = project_root().join(SYNTAX_KINDS); - let ast = project_root().join(AST); - generate_ast(&grammar, &ast, mode) -} - -fn generate_ast(grammar_src: &Path, dst: &Path, mode: Mode) -> Result<()> { - let src: Grammar = { - let text = fs::read_to_string(grammar_src)?; + let grammar: Grammar = { + let text = fs::read_to_string(grammar)?; ron::de::from_str(&text)? }; - eprintln!("{:#?}", src); + + let _syntax_kinds = project_root().join(SYNTAX_KINDS); + let _ast = project_root().join(AST); + + let ast = generate_ast(&grammar)?; + println!("{}", ast); Ok(()) } +fn generate_ast(grammar: &Grammar) -> Result { + let nodes = grammar.ast.iter().map(|(name, ast_node)| { + let variants = + ast_node.variants.iter().map(|var| format_ident!("{}", var)).collect::>(); + let name = format_ident!("{}", name); + + let kinds = if variants.is_empty() { vec![name.clone()] } else { variants.clone() } + .into_iter() + .map(|name| format_ident!("{}", name.to_string().to_shouty_snake_case())) + .collect::>(); + + let variants = if variants.is_empty() { + None + } else { + let kind_enum = format_ident!("{}Kind", name); + Some(quote!( + pub enum #kind_enum { + #(#variants(#variants),)* + } + + #( + impl From<#variants> for #name { + fn from(node: #variants) -> #name { + #name { syntax: node.syntax } + } + } + )* + + impl #name { + pub fn kind(&self) -> #kind_enum { + let syntax = self.syntax.clone(); + match syntax.kind() { + #( + #kinds => + #kind_enum::#variants(#variants { syntax }), + )* + _ => unreachable!(), + } + } + } + )) + }; + + let traits = ast_node.traits.iter().map(|trait_name| { + let trait_name = format_ident!("{}", trait_name); + quote!(impl ast::#trait_name for #name {}) + }); + + let collections = ast_node.collections.iter().map(|(name, kind)| { + let method_name = format_ident!("{}", name); + let kind = format_ident!("{}", kind); + quote! { + pub fn #method_name(&self) -> AstChildren<#kind> { + AstChildren::new(&self.syntax) + } + } + }); + + let options = ast_node.options.iter().map(|attr| { + let method_name = match attr { + Attr::Type(t) => format_ident!("{}", t.to_snake_case()), + Attr::NameType(n, _) => format_ident!("{}", n), + }; + let ty = match attr { + Attr::Type(t) | Attr::NameType(_, t) => format_ident!("{}", t), + }; + quote! { + pub fn #method_name(&self) -> Option<#ty> { + AstChildren::new(&self.syntax).next() + } + } + }); + + quote! { + #[derive(Debug, Clone, PartialEq, Eq, Hash)] + pub struct #name { + pub(crate) syntax: SyntaxNode, + } + + impl AstNode for #name { + fn can_cast(kind: SyntaxKind) -> bool { + match kind { + #(#kinds)|* => true, + _ => false, + } + } + fn cast(syntax: SyntaxNode) -> Option { + if Self::can_cast(syntax.kind()) { Some(Self { syntax }) } else { None } + } + fn syntax(&self) -> &SyntaxNode { &self.syntax } + } + + #variants + + #(#traits)* + + impl #name { + #(#collections)* + #(#options)* + } + } + }); + + let ast = quote! { + use crate::{ + SyntaxNode, SyntaxKind::{self, *}, + ast::{self, AstNode, AstChildren}, + }; + + #(#nodes)* + }; + + let pretty = reformat(ast)?; + Ok(pretty) +} + +fn reformat(text: impl std::fmt::Display) -> Result { + let mut rustfmt = Command::new("rustfmt") + .arg("--config-path") + .arg(project_root().join("rustfmt.toml")) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn()?; + write!(rustfmt.stdin.take().unwrap(), "{}", text)?; + let output = rustfmt.wait_with_output()?; + let stdout = String::from_utf8(output.stdout)?; + Ok(stdout) +} + #[derive(Deserialize, Debug)] struct Grammar { single_byte_tokens: Vec<(String, String)>, @@ -35,10 +170,14 @@ struct Grammar { #[derive(Deserialize, Debug)] struct AstNode { + #[serde(default)] + #[serde(rename = "enum")] + variants: Vec, + #[serde(default)] traits: Vec, #[serde(default)] - collections: Vec, + collections: Vec<(String, String)>, #[serde(default)] options: Vec, } -- cgit v1.2.3 From 39f50e7bd7a7aa573e6777ca764c020f93aeb205 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 18 Aug 2019 23:36:22 +0300 Subject: use new quote-generated ast --- crates/ra_tools/src/codegen.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'crates/ra_tools') diff --git a/crates/ra_tools/src/codegen.rs b/crates/ra_tools/src/codegen.rs index e14092704..6fd672290 100644 --- a/crates/ra_tools/src/codegen.rs +++ b/crates/ra_tools/src/codegen.rs @@ -154,7 +154,8 @@ fn reformat(text: impl std::fmt::Display) -> Result { write!(rustfmt.stdin.take().unwrap(), "{}", text)?; let output = rustfmt.wait_with_output()?; let stdout = String::from_utf8(output.stdout)?; - Ok(stdout) + let preamble = "Generated file, do not edit by hand, see `crate/ra_tools/src/codegen`"; + Ok(format!("// {}\n\n{}", preamble, stdout)) } #[derive(Deserialize, Debug)] -- cgit v1.2.3 From 7d29cf12256921d63860402a2e7cd2936c22e01e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 19 Aug 2019 11:02:04 +0300 Subject: use quote! to generate syntax kinds --- crates/ra_tools/Cargo.toml | 1 + crates/ra_tools/src/codegen.rs | 147 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 147 insertions(+), 1 deletion(-) (limited to 'crates/ra_tools') diff --git a/crates/ra_tools/Cargo.toml b/crates/ra_tools/Cargo.toml index 1bb6fb71c..a40a452aa 100644 --- a/crates/ra_tools/Cargo.toml +++ b/crates/ra_tools/Cargo.toml @@ -11,6 +11,7 @@ walkdir = "2.1.3" itertools = "0.8.0" clap = "2.32.0" quote = "1.0.2" +proc-macro2 = "1.0.1" ron = "0.5.1" heck = "0.3.0" serde = { version = "1.0.0", features = ["derive"] } diff --git a/crates/ra_tools/src/codegen.rs b/crates/ra_tools/src/codegen.rs index 6fd672290..c7f37e20c 100644 --- a/crates/ra_tools/src/codegen.rs +++ b/crates/ra_tools/src/codegen.rs @@ -7,6 +7,7 @@ use std::{ }; use heck::{ShoutySnakeCase, SnakeCase}; +use proc_macro2::{Punct, Spacing}; use quote::{format_ident, quote}; use ron; use serde::Deserialize; @@ -23,7 +24,7 @@ pub fn generate(mode: Mode) -> Result<()> { let _syntax_kinds = project_root().join(SYNTAX_KINDS); let _ast = project_root().join(AST); - let ast = generate_ast(&grammar)?; + let ast = generate_syntax_kinds(&grammar)?; println!("{}", ast); Ok(()) } @@ -144,6 +145,149 @@ fn generate_ast(grammar: &Grammar) -> Result { Ok(pretty) } +fn generate_syntax_kinds(grammar: &Grammar) -> Result { + let single_byte_tokens_values = + grammar.single_byte_tokens.iter().map(|(token, _name)| token.chars().next().unwrap()); + let single_byte_tokens = grammar + .single_byte_tokens + .iter() + .map(|(_token, name)| format_ident!("{}", name)) + .collect::>(); + + let punctuation_values = + grammar.single_byte_tokens.iter().chain(grammar.multi_byte_tokens.iter()).map( + |(token, _name)| { + if "{}[]()".contains(token) { + let c = token.chars().next().unwrap(); + quote! { #c } + } else { + let cs = token.chars().map(|c| Punct::new(c, Spacing::Joint)); + quote! { #(#cs)* } + } + }, + ); + let punctuation = single_byte_tokens + .clone() + .into_iter() + .chain(grammar.multi_byte_tokens.iter().map(|(_token, name)| format_ident!("{}", name))) + .collect::>(); + + let 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 + .iter() + .map(|name| format_ident!("{}_KW", name.to_shouty_snake_case())) + .collect::>(); + + let literals = + grammar.literals.iter().map(|name| format_ident!("{}", name)).collect::>(); + + let tokens = grammar.tokens.iter().map(|name| format_ident!("{}", name)).collect::>(); + + let nodes = grammar.nodes.iter().map(|name| format_ident!("{}", name)).collect::>(); + + let ast = quote! { + #![allow(bad_style, missing_docs, unreachable_pub)] + use super::SyntaxInfo; + + /// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`. + #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] + #[repr(u16)] + pub enum SyntaxKind { + // Technical SyntaxKinds: they appear temporally during parsing, + // but never end up in the final tree + #[doc(hidden)] + TOMBSTONE, + #[doc(hidden)] + EOF, + #(#punctuation,)* + #(#keywords,)* + #(#literals,)* + #(#tokens,)* + #(#nodes,)* + + // Technical kind so that we can cast from u16 safely + #[doc(hidden)] + __LAST, + } + use self::SyntaxKind::*; + + impl From for SyntaxKind { + fn from(d: u16) -> SyntaxKind { + assert!(d <= (__LAST as u16)); + unsafe { std::mem::transmute::(d) } + } + } + + impl From for u16 { + fn from(k: SyntaxKind) -> u16 { + k as u16 + } + } + + impl SyntaxKind { + pub fn is_keyword(self) -> bool { + match self { + #(#keywords)|* => true, + _ => false, + } + } + + pub fn is_punct(self) -> bool { + match self { + #(#punctuation)|* => true, + _ => false, + } + } + + pub fn is_literal(self) -> bool { + match self { + #(#literals)|* => true, + _ => false, + } + } + + pub(crate) fn info(self) -> &'static SyntaxInfo { + match self { + #(#punctuation => &SyntaxInfo { name: stringify!(#punctuation) },)* + #(#keywords => &SyntaxInfo { name: stringify!(#keywords) },)* + #(#literals => &SyntaxInfo { name: stringify!(#literals) },)* + #(#tokens => &SyntaxInfo { name: stringify!(#tokens) },)* + #(#nodes => &SyntaxInfo { name: stringify!(#nodes) },)* + TOMBSTONE => &SyntaxInfo { name: "TOMBSTONE" }, + EOF => &SyntaxInfo { name: "EOF" }, + __LAST => &SyntaxInfo { name: "__LAST" }, + } + } + + pub fn from_keyword(ident: &str) -> Option { + let kw = match ident { + #(#keywords_values => #keywords,)* + _ => return None, + }; + Some(kw) + } + + pub fn from_char(c: char) -> Option { + let tok = match c { + #(#single_byte_tokens_values => #single_byte_tokens,)* + _ => return None, + }; + Some(tok) + } + } + + #[macro_export] + macro_rules! T { + #((#punctuation_values) => { $crate::SyntaxKind::#punctuation };)* + #((#keywords_idents) => { $crate::SyntaxKind::#keywords };)* + } + }; + + reformat(ast) +} + fn reformat(text: impl std::fmt::Display) -> Result { let mut rustfmt = Command::new("rustfmt") .arg("--config-path") @@ -166,6 +310,7 @@ struct Grammar { contextual_keywords: Vec, literals: Vec, tokens: Vec, + nodes: Vec, ast: BTreeMap, } -- cgit v1.2.3 From 5633829a1684fb622d44d1aeb3baede4e7c0ff26 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 19 Aug 2019 11:05:49 +0300 Subject: drop tera dependency --- crates/ra_tools/Cargo.toml | 1 - crates/ra_tools/src/lib.rs | 28 +++++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) (limited to 'crates/ra_tools') diff --git a/crates/ra_tools/Cargo.toml b/crates/ra_tools/Cargo.toml index a40a452aa..4c9aa1cc3 100644 --- a/crates/ra_tools/Cargo.toml +++ b/crates/ra_tools/Cargo.toml @@ -6,7 +6,6 @@ authors = ["rust-analyzer developers"] publish = false [dependencies] -teraron = "0.1.0" walkdir = "2.1.3" itertools = "0.8.0" clap = "2.32.0" diff --git a/crates/ra_tools/src/lib.rs b/crates/ra_tools/src/lib.rs index de8c472d1..c02dee953 100644 --- a/crates/ra_tools/src/lib.rs +++ b/crates/ra_tools/src/lib.rs @@ -11,8 +11,6 @@ use std::{ use itertools::Itertools; -pub use teraron::{Mode, Overwrite, Verify}; - pub use self::codegen::generate; pub type Result = std::result::Result>; @@ -26,6 +24,13 @@ pub const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs.te pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs"; const TOOLCHAIN: &str = "stable"; +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Mode { + Overwrite, + Verify, +} +pub use Mode::*; + #[derive(Debug)] pub struct Test { pub name: String, @@ -222,7 +227,7 @@ pub fn gen_tests(mode: Mode) -> Result<()> { tests_dir.join(file_name) } }; - teraron::update(&path, &test.text, mode)?; + update(&path, &test.text, mode)?; } Ok(()) } @@ -306,3 +311,20 @@ fn existing_tests(dir: &Path, ok: bool) -> Result Result<()> { + match fs::read_to_string(path) { + Ok(ref old_contents) if old_contents == contents => { + return Ok(()); + } + _ => (), + } + if mode == Verify { + Err(format!("`{}` is not up-to-date", path.display()))?; + } + eprintln!("updating {}", path.display()); + fs::write(path, contents)?; + Ok(()) +} -- cgit v1.2.3 From aa250ff612b10c02bfbc197557adfd4c70e01e43 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 19 Aug 2019 11:39:12 +0300 Subject: rename codegen -> boilerplate_gen --- crates/ra_tools/src/boilerplate_gen.rs | 336 +++++++++++++++++++++++++++++++++ crates/ra_tools/src/codegen.rs | 336 --------------------------------- crates/ra_tools/src/lib.rs | 4 +- 3 files changed, 338 insertions(+), 338 deletions(-) create mode 100644 crates/ra_tools/src/boilerplate_gen.rs delete mode 100644 crates/ra_tools/src/codegen.rs (limited to 'crates/ra_tools') diff --git a/crates/ra_tools/src/boilerplate_gen.rs b/crates/ra_tools/src/boilerplate_gen.rs new file mode 100644 index 000000000..5547d6969 --- /dev/null +++ b/crates/ra_tools/src/boilerplate_gen.rs @@ -0,0 +1,336 @@ +use std::{ + collections::BTreeMap, + fs, + io::Write, + path::Path, + process::{Command, Stdio}, +}; + +use heck::{ShoutySnakeCase, SnakeCase}; +use proc_macro2::{Punct, Spacing}; +use quote::{format_ident, quote}; +use ron; +use serde::Deserialize; + +use crate::{project_root, Mode, Result, AST, GRAMMAR, SYNTAX_KINDS}; + +pub fn generate_boilerplate(mode: Mode) -> Result<()> { + let grammar = project_root().join(GRAMMAR); + let grammar: Grammar = { + let text = fs::read_to_string(grammar)?; + ron::de::from_str(&text)? + }; + + let _syntax_kinds = project_root().join(SYNTAX_KINDS); + let _ast = project_root().join(AST); + + let ast = generate_syntax_kinds(&grammar)?; + println!("{}", ast); + Ok(()) +} + +fn generate_ast(grammar: &Grammar) -> Result { + let nodes = grammar.ast.iter().map(|(name, ast_node)| { + let variants = + ast_node.variants.iter().map(|var| format_ident!("{}", var)).collect::>(); + let name = format_ident!("{}", name); + + let kinds = if variants.is_empty() { vec![name.clone()] } else { variants.clone() } + .into_iter() + .map(|name| format_ident!("{}", name.to_string().to_shouty_snake_case())) + .collect::>(); + + let variants = if variants.is_empty() { + None + } else { + let kind_enum = format_ident!("{}Kind", name); + Some(quote!( + pub enum #kind_enum { + #(#variants(#variants),)* + } + + #( + impl From<#variants> for #name { + fn from(node: #variants) -> #name { + #name { syntax: node.syntax } + } + } + )* + + impl #name { + pub fn kind(&self) -> #kind_enum { + let syntax = self.syntax.clone(); + match syntax.kind() { + #( + #kinds => + #kind_enum::#variants(#variants { syntax }), + )* + _ => unreachable!(), + } + } + } + )) + }; + + let traits = ast_node.traits.iter().map(|trait_name| { + let trait_name = format_ident!("{}", trait_name); + quote!(impl ast::#trait_name for #name {}) + }); + + let collections = ast_node.collections.iter().map(|(name, kind)| { + let method_name = format_ident!("{}", name); + let kind = format_ident!("{}", kind); + quote! { + pub fn #method_name(&self) -> AstChildren<#kind> { + AstChildren::new(&self.syntax) + } + } + }); + + let options = ast_node.options.iter().map(|attr| { + let method_name = match attr { + Attr::Type(t) => format_ident!("{}", t.to_snake_case()), + Attr::NameType(n, _) => format_ident!("{}", n), + }; + let ty = match attr { + Attr::Type(t) | Attr::NameType(_, t) => format_ident!("{}", t), + }; + quote! { + pub fn #method_name(&self) -> Option<#ty> { + AstChildren::new(&self.syntax).next() + } + } + }); + + quote! { + #[derive(Debug, Clone, PartialEq, Eq, Hash)] + pub struct #name { + pub(crate) syntax: SyntaxNode, + } + + impl AstNode for #name { + fn can_cast(kind: SyntaxKind) -> bool { + match kind { + #(#kinds)|* => true, + _ => false, + } + } + fn cast(syntax: SyntaxNode) -> Option { + if Self::can_cast(syntax.kind()) { Some(Self { syntax }) } else { None } + } + fn syntax(&self) -> &SyntaxNode { &self.syntax } + } + + #variants + + #(#traits)* + + impl #name { + #(#collections)* + #(#options)* + } + } + }); + + let ast = quote! { + use crate::{ + SyntaxNode, SyntaxKind::{self, *}, + ast::{self, AstNode, AstChildren}, + }; + + #(#nodes)* + }; + + let pretty = reformat(ast)?; + Ok(pretty) +} + +fn generate_syntax_kinds(grammar: &Grammar) -> Result { + let single_byte_tokens_values = + grammar.single_byte_tokens.iter().map(|(token, _name)| token.chars().next().unwrap()); + let single_byte_tokens = grammar + .single_byte_tokens + .iter() + .map(|(_token, name)| format_ident!("{}", name)) + .collect::>(); + + let punctuation_values = + grammar.single_byte_tokens.iter().chain(grammar.multi_byte_tokens.iter()).map( + |(token, _name)| { + if "{}[]()".contains(token) { + let c = token.chars().next().unwrap(); + quote! { #c } + } else { + let cs = token.chars().map(|c| Punct::new(c, Spacing::Joint)); + quote! { #(#cs)* } + } + }, + ); + let punctuation = single_byte_tokens + .clone() + .into_iter() + .chain(grammar.multi_byte_tokens.iter().map(|(_token, name)| format_ident!("{}", name))) + .collect::>(); + + let 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 + .iter() + .map(|name| format_ident!("{}_KW", name.to_shouty_snake_case())) + .collect::>(); + + let literals = + grammar.literals.iter().map(|name| format_ident!("{}", name)).collect::>(); + + let tokens = grammar.tokens.iter().map(|name| format_ident!("{}", name)).collect::>(); + + let nodes = grammar.nodes.iter().map(|name| format_ident!("{}", name)).collect::>(); + + let ast = quote! { + #![allow(bad_style, missing_docs, unreachable_pub)] + use super::SyntaxInfo; + + /// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`. + #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] + #[repr(u16)] + pub enum SyntaxKind { + // Technical SyntaxKinds: they appear temporally during parsing, + // but never end up in the final tree + #[doc(hidden)] + TOMBSTONE, + #[doc(hidden)] + EOF, + #(#punctuation,)* + #(#keywords,)* + #(#literals,)* + #(#tokens,)* + #(#nodes,)* + + // Technical kind so that we can cast from u16 safely + #[doc(hidden)] + __LAST, + } + use self::SyntaxKind::*; + + impl From for SyntaxKind { + fn from(d: u16) -> SyntaxKind { + assert!(d <= (__LAST as u16)); + unsafe { std::mem::transmute::(d) } + } + } + + impl From for u16 { + fn from(k: SyntaxKind) -> u16 { + k as u16 + } + } + + impl SyntaxKind { + pub fn is_keyword(self) -> bool { + match self { + #(#keywords)|* => true, + _ => false, + } + } + + pub fn is_punct(self) -> bool { + match self { + #(#punctuation)|* => true, + _ => false, + } + } + + pub fn is_literal(self) -> bool { + match self { + #(#literals)|* => true, + _ => false, + } + } + + pub(crate) fn info(self) -> &'static SyntaxInfo { + match self { + #(#punctuation => &SyntaxInfo { name: stringify!(#punctuation) },)* + #(#keywords => &SyntaxInfo { name: stringify!(#keywords) },)* + #(#literals => &SyntaxInfo { name: stringify!(#literals) },)* + #(#tokens => &SyntaxInfo { name: stringify!(#tokens) },)* + #(#nodes => &SyntaxInfo { name: stringify!(#nodes) },)* + TOMBSTONE => &SyntaxInfo { name: "TOMBSTONE" }, + EOF => &SyntaxInfo { name: "EOF" }, + __LAST => &SyntaxInfo { name: "__LAST" }, + } + } + + pub fn from_keyword(ident: &str) -> Option { + let kw = match ident { + #(#keywords_values => #keywords,)* + _ => return None, + }; + Some(kw) + } + + pub fn from_char(c: char) -> Option { + let tok = match c { + #(#single_byte_tokens_values => #single_byte_tokens,)* + _ => return None, + }; + Some(tok) + } + } + + #[macro_export] + macro_rules! T { + #((#punctuation_values) => { $crate::SyntaxKind::#punctuation };)* + #((#keywords_idents) => { $crate::SyntaxKind::#keywords };)* + } + }; + + reformat(ast) +} + +fn reformat(text: impl std::fmt::Display) -> Result { + let mut rustfmt = Command::new("rustfmt") + .arg("--config-path") + .arg(project_root().join("rustfmt.toml")) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn()?; + write!(rustfmt.stdin.take().unwrap(), "{}", text)?; + let output = rustfmt.wait_with_output()?; + let stdout = String::from_utf8(output.stdout)?; + let preamble = "Generated file, do not edit by hand, see `crate/ra_tools/src/codegen`"; + Ok(format!("// {}\n\n{}", preamble, stdout)) +} + +#[derive(Deserialize, Debug)] +struct Grammar { + single_byte_tokens: Vec<(String, String)>, + multi_byte_tokens: Vec<(String, String)>, + keywords: Vec, + contextual_keywords: Vec, + literals: Vec, + tokens: Vec, + nodes: Vec, + ast: BTreeMap, +} + +#[derive(Deserialize, Debug)] +struct AstNode { + #[serde(default)] + #[serde(rename = "enum")] + variants: Vec, + + #[serde(default)] + traits: Vec, + #[serde(default)] + collections: Vec<(String, String)>, + #[serde(default)] + options: Vec, +} + +#[derive(Deserialize, Debug)] +#[serde(untagged)] +enum Attr { + Type(String), + NameType(String, String), +} diff --git a/crates/ra_tools/src/codegen.rs b/crates/ra_tools/src/codegen.rs deleted file mode 100644 index c7f37e20c..000000000 --- a/crates/ra_tools/src/codegen.rs +++ /dev/null @@ -1,336 +0,0 @@ -use std::{ - collections::BTreeMap, - fs, - io::Write, - path::Path, - process::{Command, Stdio}, -}; - -use heck::{ShoutySnakeCase, SnakeCase}; -use proc_macro2::{Punct, Spacing}; -use quote::{format_ident, quote}; -use ron; -use serde::Deserialize; - -use crate::{project_root, Mode, Result, AST, GRAMMAR, SYNTAX_KINDS}; - -pub fn generate(mode: Mode) -> Result<()> { - let grammar = project_root().join(GRAMMAR); - let grammar: Grammar = { - let text = fs::read_to_string(grammar)?; - ron::de::from_str(&text)? - }; - - let _syntax_kinds = project_root().join(SYNTAX_KINDS); - let _ast = project_root().join(AST); - - let ast = generate_syntax_kinds(&grammar)?; - println!("{}", ast); - Ok(()) -} - -fn generate_ast(grammar: &Grammar) -> Result { - let nodes = grammar.ast.iter().map(|(name, ast_node)| { - let variants = - ast_node.variants.iter().map(|var| format_ident!("{}", var)).collect::>(); - let name = format_ident!("{}", name); - - let kinds = if variants.is_empty() { vec![name.clone()] } else { variants.clone() } - .into_iter() - .map(|name| format_ident!("{}", name.to_string().to_shouty_snake_case())) - .collect::>(); - - let variants = if variants.is_empty() { - None - } else { - let kind_enum = format_ident!("{}Kind", name); - Some(quote!( - pub enum #kind_enum { - #(#variants(#variants),)* - } - - #( - impl From<#variants> for #name { - fn from(node: #variants) -> #name { - #name { syntax: node.syntax } - } - } - )* - - impl #name { - pub fn kind(&self) -> #kind_enum { - let syntax = self.syntax.clone(); - match syntax.kind() { - #( - #kinds => - #kind_enum::#variants(#variants { syntax }), - )* - _ => unreachable!(), - } - } - } - )) - }; - - let traits = ast_node.traits.iter().map(|trait_name| { - let trait_name = format_ident!("{}", trait_name); - quote!(impl ast::#trait_name for #name {}) - }); - - let collections = ast_node.collections.iter().map(|(name, kind)| { - let method_name = format_ident!("{}", name); - let kind = format_ident!("{}", kind); - quote! { - pub fn #method_name(&self) -> AstChildren<#kind> { - AstChildren::new(&self.syntax) - } - } - }); - - let options = ast_node.options.iter().map(|attr| { - let method_name = match attr { - Attr::Type(t) => format_ident!("{}", t.to_snake_case()), - Attr::NameType(n, _) => format_ident!("{}", n), - }; - let ty = match attr { - Attr::Type(t) | Attr::NameType(_, t) => format_ident!("{}", t), - }; - quote! { - pub fn #method_name(&self) -> Option<#ty> { - AstChildren::new(&self.syntax).next() - } - } - }); - - quote! { - #[derive(Debug, Clone, PartialEq, Eq, Hash)] - pub struct #name { - pub(crate) syntax: SyntaxNode, - } - - impl AstNode for #name { - fn can_cast(kind: SyntaxKind) -> bool { - match kind { - #(#kinds)|* => true, - _ => false, - } - } - fn cast(syntax: SyntaxNode) -> Option { - if Self::can_cast(syntax.kind()) { Some(Self { syntax }) } else { None } - } - fn syntax(&self) -> &SyntaxNode { &self.syntax } - } - - #variants - - #(#traits)* - - impl #name { - #(#collections)* - #(#options)* - } - } - }); - - let ast = quote! { - use crate::{ - SyntaxNode, SyntaxKind::{self, *}, - ast::{self, AstNode, AstChildren}, - }; - - #(#nodes)* - }; - - let pretty = reformat(ast)?; - Ok(pretty) -} - -fn generate_syntax_kinds(grammar: &Grammar) -> Result { - let single_byte_tokens_values = - grammar.single_byte_tokens.iter().map(|(token, _name)| token.chars().next().unwrap()); - let single_byte_tokens = grammar - .single_byte_tokens - .iter() - .map(|(_token, name)| format_ident!("{}", name)) - .collect::>(); - - let punctuation_values = - grammar.single_byte_tokens.iter().chain(grammar.multi_byte_tokens.iter()).map( - |(token, _name)| { - if "{}[]()".contains(token) { - let c = token.chars().next().unwrap(); - quote! { #c } - } else { - let cs = token.chars().map(|c| Punct::new(c, Spacing::Joint)); - quote! { #(#cs)* } - } - }, - ); - let punctuation = single_byte_tokens - .clone() - .into_iter() - .chain(grammar.multi_byte_tokens.iter().map(|(_token, name)| format_ident!("{}", name))) - .collect::>(); - - let 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 - .iter() - .map(|name| format_ident!("{}_KW", name.to_shouty_snake_case())) - .collect::>(); - - let literals = - grammar.literals.iter().map(|name| format_ident!("{}", name)).collect::>(); - - let tokens = grammar.tokens.iter().map(|name| format_ident!("{}", name)).collect::>(); - - let nodes = grammar.nodes.iter().map(|name| format_ident!("{}", name)).collect::>(); - - let ast = quote! { - #![allow(bad_style, missing_docs, unreachable_pub)] - use super::SyntaxInfo; - - /// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`. - #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] - #[repr(u16)] - pub enum SyntaxKind { - // Technical SyntaxKinds: they appear temporally during parsing, - // but never end up in the final tree - #[doc(hidden)] - TOMBSTONE, - #[doc(hidden)] - EOF, - #(#punctuation,)* - #(#keywords,)* - #(#literals,)* - #(#tokens,)* - #(#nodes,)* - - // Technical kind so that we can cast from u16 safely - #[doc(hidden)] - __LAST, - } - use self::SyntaxKind::*; - - impl From for SyntaxKind { - fn from(d: u16) -> SyntaxKind { - assert!(d <= (__LAST as u16)); - unsafe { std::mem::transmute::(d) } - } - } - - impl From for u16 { - fn from(k: SyntaxKind) -> u16 { - k as u16 - } - } - - impl SyntaxKind { - pub fn is_keyword(self) -> bool { - match self { - #(#keywords)|* => true, - _ => false, - } - } - - pub fn is_punct(self) -> bool { - match self { - #(#punctuation)|* => true, - _ => false, - } - } - - pub fn is_literal(self) -> bool { - match self { - #(#literals)|* => true, - _ => false, - } - } - - pub(crate) fn info(self) -> &'static SyntaxInfo { - match self { - #(#punctuation => &SyntaxInfo { name: stringify!(#punctuation) },)* - #(#keywords => &SyntaxInfo { name: stringify!(#keywords) },)* - #(#literals => &SyntaxInfo { name: stringify!(#literals) },)* - #(#tokens => &SyntaxInfo { name: stringify!(#tokens) },)* - #(#nodes => &SyntaxInfo { name: stringify!(#nodes) },)* - TOMBSTONE => &SyntaxInfo { name: "TOMBSTONE" }, - EOF => &SyntaxInfo { name: "EOF" }, - __LAST => &SyntaxInfo { name: "__LAST" }, - } - } - - pub fn from_keyword(ident: &str) -> Option { - let kw = match ident { - #(#keywords_values => #keywords,)* - _ => return None, - }; - Some(kw) - } - - pub fn from_char(c: char) -> Option { - let tok = match c { - #(#single_byte_tokens_values => #single_byte_tokens,)* - _ => return None, - }; - Some(tok) - } - } - - #[macro_export] - macro_rules! T { - #((#punctuation_values) => { $crate::SyntaxKind::#punctuation };)* - #((#keywords_idents) => { $crate::SyntaxKind::#keywords };)* - } - }; - - reformat(ast) -} - -fn reformat(text: impl std::fmt::Display) -> Result { - let mut rustfmt = Command::new("rustfmt") - .arg("--config-path") - .arg(project_root().join("rustfmt.toml")) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn()?; - write!(rustfmt.stdin.take().unwrap(), "{}", text)?; - let output = rustfmt.wait_with_output()?; - let stdout = String::from_utf8(output.stdout)?; - let preamble = "Generated file, do not edit by hand, see `crate/ra_tools/src/codegen`"; - Ok(format!("// {}\n\n{}", preamble, stdout)) -} - -#[derive(Deserialize, Debug)] -struct Grammar { - single_byte_tokens: Vec<(String, String)>, - multi_byte_tokens: Vec<(String, String)>, - keywords: Vec, - contextual_keywords: Vec, - literals: Vec, - tokens: Vec, - nodes: Vec, - ast: BTreeMap, -} - -#[derive(Deserialize, Debug)] -struct AstNode { - #[serde(default)] - #[serde(rename = "enum")] - variants: Vec, - - #[serde(default)] - traits: Vec, - #[serde(default)] - collections: Vec<(String, String)>, - #[serde(default)] - options: Vec, -} - -#[derive(Deserialize, Debug)] -#[serde(untagged)] -enum Attr { - Type(String), - NameType(String, String), -} diff --git a/crates/ra_tools/src/lib.rs b/crates/ra_tools/src/lib.rs index c02dee953..f1829d55a 100644 --- a/crates/ra_tools/src/lib.rs +++ b/crates/ra_tools/src/lib.rs @@ -1,4 +1,4 @@ -mod codegen; +mod boilerplate_gen; use std::{ collections::HashMap, @@ -11,7 +11,7 @@ use std::{ use itertools::Itertools; -pub use self::codegen::generate; +pub use self::boilerplate_gen::generate_boilerplate; pub type Result = std::result::Result>; -- cgit v1.2.3 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 +++--- crates/ra_tools/tests/cli.rs | 4 ++-- 4 files changed, 26 insertions(+), 20 deletions(-) (limited to 'crates/ra_tools') 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()?, diff --git a/crates/ra_tools/tests/cli.rs b/crates/ra_tools/tests/cli.rs index ae0eb337d..c672e5788 100644 --- a/crates/ra_tools/tests/cli.rs +++ b/crates/ra_tools/tests/cli.rs @@ -1,10 +1,10 @@ use walkdir::WalkDir; -use ra_tools::{gen_tests, generate, project_root, run_rustfmt, Verify}; +use ra_tools::{gen_tests, generate_boilerplate, project_root, run_rustfmt, Verify}; #[test] fn generated_grammar_is_fresh() { - if let Err(error) = generate(Verify) { + if let Err(error) = generate_boilerplate(Verify) { panic!("{}. Please update it by running `cargo gen-syntax`", error); } } -- cgit v1.2.3