difftreelog
feat(lexer) explicit token names
in: master
7 files changed
Cargo.tomldiffbeforeafterboth--- a/Cargo.toml
+++ b/Cargo.toml
@@ -14,7 +14,7 @@
jrsonnet-evaluator = { path = "./crates/jrsonnet-evaluator", version = "0.5.0-pre97" }
jrsonnet-macros = { path = "./crates/jrsonnet-macros", version = "0.5.0-pre97" }
jrsonnet-ir = { path = "./crates/jrsonnet-ir", version = "0.5.0-pre97" }
-jrsonnet-ir-parser = { path = "./crates/jrsonnet-rowan-parser", version = "0.5.0-pre97" }
+jrsonnet-ir-parser = { path = "./crates/jrsonnet-ir-parser", version = "0.5.0-pre97" }
jrsonnet-peg-parser = { path = "./crates/jrsonnet-peg-parser", version = "0.5.0-pre97" }
jrsonnet-rowan-parser = { path = "./crates/jrsonnet-rowan-parser", version = "0.5.0-pre97" }
jrsonnet-interner = { path = "./crates/jrsonnet-interner", version = "0.5.0-pre97" }
crates/jrsonnet-lexer/src/generated/syntax_kinds.rsdiffbeforeafterboth--- a/crates/jrsonnet-lexer/src/generated/syntax_kinds.rs
+++ b/crates/jrsonnet-lexer/src/generated/syntax_kinds.rs
@@ -132,6 +132,10 @@
ERROR_COMMENT_TOO_SHORT,
#[regex("/\\*([^*/]|\\*[^/])+")]
ERROR_COMMENT_UNTERMINATED,
+ ERROR_NO_OPERATOR,
+ ERROR_MISSING_TOKEN,
+ ERROR_UNEXPECTED_TOKEN,
+ ERROR_CUSTOM,
#[token("tailstrict")]
TAILSTRICT_KW,
#[token("local")]
@@ -155,7 +159,6 @@
#[token("in")]
IN_KW,
META_OBJECT_APPLY,
- ERROR_NO_OPERATOR,
#[token("null")]
NULL_KW,
#[token("true")]
@@ -170,9 +173,6 @@
FOR_KW,
#[token("assert")]
ASSERT_KW,
- ERROR_MISSING_TOKEN,
- ERROR_UNEXPECTED_TOKEN,
- ERROR_CUSTOM,
LEXING_ERROR,
__LAST_TOKEN,
#[doc(hidden)]
crates/jrsonnet-lexer/src/string_block.rsdiffbeforeafterboth--- a/crates/jrsonnet-lexer/src/string_block.rs
+++ b/crates/jrsonnet-lexer/src/string_block.rs
@@ -211,6 +211,7 @@
// Process leading blank lines before calculating string block indent
while ctx.peek() == Some('\n') {
+ lex.mark_line("");
ctx.next();
}
crates/jrsonnet-rowan-parser/jsonnet.ungramdiffbeforeafterboth--- a/crates/jrsonnet-rowan-parser/jsonnet.ungram
+++ b/crates/jrsonnet-rowan-parser/jsonnet.ungram
@@ -209,7 +209,7 @@
| FieldNameDynamic
Visibility =
- ':' v1:':'? v2:':'?
+ ':' ':'? ':'?
Literal =
'null'
crates/jrsonnet-rowan-parser/src/generated/syntax_kinds.rsdiffbeforeafterboth--- a/crates/jrsonnet-rowan-parser/src/generated/syntax_kinds.rs
+++ b/crates/jrsonnet-rowan-parser/src/generated/syntax_kinds.rs
@@ -76,6 +76,10 @@
MULTI_LINE_COMMENT,
ERROR_COMMENT_TOO_SHORT,
ERROR_COMMENT_UNTERMINATED,
+ ERROR_NO_OPERATOR,
+ ERROR_MISSING_TOKEN,
+ ERROR_UNEXPECTED_TOKEN,
+ ERROR_CUSTOM,
TAILSTRICT_KW,
LOCAL_KW,
IMPORTSTR_KW,
@@ -88,7 +92,6 @@
ERROR_KW,
IN_KW,
META_OBJECT_APPLY,
- ERROR_NO_OPERATOR,
NULL_KW,
TRUE_KW,
FALSE_KW,
@@ -96,9 +99,6 @@
SUPER_KW,
FOR_KW,
ASSERT_KW,
- ERROR_MISSING_TOKEN,
- ERROR_UNEXPECTED_TOKEN,
- ERROR_CUSTOM,
LEXING_ERROR,
__LAST_TOKEN,
SOURCE_FILE,
@@ -199,6 +199,149 @@
_ => false,
}
}
+ pub fn error_description(self) -> Option<&'static str> {
+ match self {
+ ERROR_FLOAT_JUNK_AFTER_POINT => {
+ ::core::option::Option::Some("junk after decimal point in number literal")
+ }
+ ERROR_FLOAT_JUNK_AFTER_EXPONENT => {
+ ::core::option::Option::Some("junk after exponent in number literal")
+ }
+ ERROR_FLOAT_JUNK_AFTER_EXPONENT_SIGN => {
+ ::core::option::Option::Some("junk after exponent sign in number literal")
+ }
+ ERROR_STRING_DOUBLE_UNTERMINATED => {
+ ::core::option::Option::Some("unterminated double-quoted string")
+ }
+ ERROR_STRING_SINGLE_UNTERMINATED => {
+ ::core::option::Option::Some("unterminated single-quoted string")
+ }
+ ERROR_STRING_DOUBLE_VERBATIM_UNTERMINATED => {
+ ::core::option::Option::Some("unterminated verbatim double-quoted string")
+ }
+ ERROR_STRING_SINGLE_VERBATIM_UNTERMINATED => {
+ ::core::option::Option::Some("unterminated verbatim single-quoted string")
+ }
+ ERROR_STRING_VERBATIM_MISSING_QUOTES => {
+ ::core::option::Option::Some("verbatim string missing opening quotes")
+ }
+ ERROR_STRING_BLOCK_UNEXPECTED_END => {
+ ::core::option::Option::Some("unexpected end of text block")
+ }
+ ERROR_STRING_BLOCK_MISSING_NEW_LINE => {
+ ::core::option::Option::Some("text block requires new line after |||")
+ }
+ ERROR_STRING_BLOCK_MISSING_TERMINATION => {
+ ::core::option::Option::Some("unterminated text block")
+ }
+ ERROR_STRING_BLOCK_MISSING_INDENT => {
+ ::core::option::Option::Some("text block first line must be indented")
+ }
+ ERROR_COMMENT_TOO_SHORT => ::core::option::Option::Some("comment too short"),
+ ERROR_COMMENT_UNTERMINATED => {
+ ::core::option::Option::Some("unterminated multi-line comment")
+ }
+ ERROR_NO_OPERATOR => ::core::option::Option::Some("expected operator"),
+ ERROR_MISSING_TOKEN => ::core::option::Option::Some("missing token"),
+ ERROR_UNEXPECTED_TOKEN => ::core::option::Option::Some("unexpected token"),
+ ERROR_CUSTOM => ::core::option::Option::Some("error"),
+ LEXING_ERROR => ::core::option::Option::Some("unexpected character"),
+ _ => None,
+ }
+ }
+ pub fn display_name(self) -> &'static str {
+ match self {
+ OR => "'||'",
+ NULL_COAELSE => "'??'",
+ AND => "'&&'",
+ BIT_OR => "'|'",
+ BIT_XOR => "'^'",
+ BIT_AND => "'&'",
+ EQ => "'=='",
+ NE => "'!='",
+ LT => "'<'",
+ GT => "'>'",
+ LE => "'<='",
+ GE => "'>='",
+ LHS => "'<<'",
+ RHS => "'>>'",
+ PLUS => "'+'",
+ MINUS => "'-'",
+ MUL => "'*'",
+ DIV => "'/'",
+ MODULO => "'%'",
+ NOT => "'!'",
+ BIT_NOT => "'~'",
+ L_BRACK => "'['",
+ R_BRACK => "']'",
+ L_PAREN => "'('",
+ R_PAREN => "')'",
+ L_BRACE => "'{'",
+ R_BRACE => "'}'",
+ COLON => "':'",
+ SEMI => "';'",
+ DOT => "'.'",
+ DOTDOTDOT => "'...'",
+ COMMA => "','",
+ DOLLAR => "'$'",
+ ASSIGN => "'='",
+ QUESTION_MARK => "'?'",
+ FLOAT => "number",
+ ERROR_FLOAT_JUNK_AFTER_POINT => "junk after decimal point in number literal",
+ ERROR_FLOAT_JUNK_AFTER_EXPONENT => "junk after exponent in number literal",
+ ERROR_FLOAT_JUNK_AFTER_EXPONENT_SIGN => "junk after exponent sign in number literal",
+ STRING_DOUBLE => "string",
+ ERROR_STRING_DOUBLE_UNTERMINATED => "unterminated double-quoted string",
+ STRING_SINGLE => "string",
+ ERROR_STRING_SINGLE_UNTERMINATED => "unterminated single-quoted string",
+ STRING_DOUBLE_VERBATIM => "string",
+ ERROR_STRING_DOUBLE_VERBATIM_UNTERMINATED => {
+ "unterminated verbatim double-quoted string"
+ }
+ STRING_SINGLE_VERBATIM => "string",
+ ERROR_STRING_SINGLE_VERBATIM_UNTERMINATED => {
+ "unterminated verbatim single-quoted string"
+ }
+ ERROR_STRING_VERBATIM_MISSING_QUOTES => "verbatim string missing opening quotes",
+ STRING_BLOCK => "string",
+ ERROR_STRING_BLOCK_UNEXPECTED_END => "unexpected end of text block",
+ ERROR_STRING_BLOCK_MISSING_NEW_LINE => "text block requires new line after |||",
+ ERROR_STRING_BLOCK_MISSING_TERMINATION => "unterminated text block",
+ ERROR_STRING_BLOCK_MISSING_INDENT => "text block first line must be indented",
+ IDENT => "identifier",
+ WHITESPACE => "whitespace",
+ SINGLE_LINE_SLASH_COMMENT => "comment",
+ SINGLE_LINE_HASH_COMMENT => "comment",
+ MULTI_LINE_COMMENT => "comment",
+ ERROR_COMMENT_TOO_SHORT => "comment too short",
+ ERROR_COMMENT_UNTERMINATED => "unterminated multi-line comment",
+ ERROR_NO_OPERATOR => "expected operator",
+ ERROR_MISSING_TOKEN => "missing token",
+ ERROR_UNEXPECTED_TOKEN => "unexpected token",
+ ERROR_CUSTOM => "error",
+ TAILSTRICT_KW => "'tailstrict'",
+ LOCAL_KW => "'local'",
+ IMPORTSTR_KW => "'importstr'",
+ IMPORTBIN_KW => "'importbin'",
+ IMPORT_KW => "'import'",
+ IF_KW => "'if'",
+ THEN_KW => "'then'",
+ ELSE_KW => "'else'",
+ FUNCTION_KW => "'function'",
+ ERROR_KW => "'error'",
+ IN_KW => "'in'",
+ META_OBJECT_APPLY => "meta_object_apply",
+ NULL_KW => "'null'",
+ TRUE_KW => "'true'",
+ FALSE_KW => "'false'",
+ SELF_KW => "'self'",
+ SUPER_KW => "'super'",
+ FOR_KW => "'for'",
+ ASSERT_KW => "'assert'",
+ LEXING_ERROR => "unexpected character",
+ _ => "unknown",
+ }
+ }
pub fn from_raw(r: u16) -> Self {
assert!(r < Self::__LAST as u16);
unsafe { std::mem::transmute(r) }
xtask/src/sourcegen/kinds.rsdiffbeforeafterboth--- a/xtask/src/sourcegen/kinds.rs
+++ b/xtask/src/sourcegen/kinds.rs
@@ -19,6 +19,7 @@
is_lexer_error: bool,
regex: Option<String>,
priority: Option<u32>,
+ description: String,
},
/// Keyword - literal match of token
Keyword {
@@ -113,6 +114,24 @@
}
}
+ pub fn display_name(&self) -> String {
+ match self {
+ Self::Keyword { code, .. } => format!("'{code}'"),
+ Self::Literal { name, .. } => match name.as_str() {
+ "FLOAT" => "number".to_owned(),
+ "IDENT" => "identifier".to_owned(),
+ "STRING_DOUBLE" | "STRING_SINGLE" | "STRING_DOUBLE_VERBATIM"
+ | "STRING_SINGLE_VERBATIM" | "STRING_BLOCK" => "string".to_owned(),
+ "WHITESPACE" => "whitespace".to_owned(),
+ "SINGLE_LINE_SLASH_COMMENT" | "SINGLE_LINE_HASH_COMMENT"
+ | "MULTI_LINE_COMMENT" => "comment".to_owned(),
+ _ => name.to_lowercase(),
+ },
+ Self::Meta { name, .. } => name.to_lowercase(),
+ Self::Error { description, .. } => description.clone(),
+ }
+ }
+
pub fn method_name(&self) -> Ident {
match self {
Self::Keyword { name, .. } => {
@@ -138,7 +157,7 @@
});
$(define_kinds!($into = $($rest)*))?
}};
- ($into:ident = error($name:literal$(, priority = $priority:literal)? $(, lexer = $lexer:literal)?) $(=> $regex:literal)? $(; $($rest:tt)*)?) => {{
+ ($into:ident = error($name:literal, $desc:literal $(, priority = $priority:literal)? $(, lexer = $lexer:literal)?) $(=> $regex:literal)? $(; $($rest:tt)*)?) => {{
{
let regex = None$(.or(Some($regex.to_owned())))?;
let priority = None$(.or(Some($priority)))?;
@@ -148,6 +167,7 @@
is_lexer_error: false $(|| $lexer)? || regex.is_some() || priority.is_some(),
regex,
priority,
+ description: $desc.to_owned(),
});
}
$(define_kinds!($into = $($rest)*))?
@@ -248,31 +268,35 @@
"=" => "ASSIGN";
"?" => "QUESTION_MARK";
// Literals
- lit("FLOAT") => r"(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?";
- error("FLOAT_JUNK_AFTER_POINT") => r"(?:0|[1-9][0-9]*)\.[^0-9]";
- error("FLOAT_JUNK_AFTER_EXPONENT") => r"(?:0|[1-9][0-9]*)(?:\.[0-9]+)?[eE][^+\-0-9]";
- error("FLOAT_JUNK_AFTER_EXPONENT_SIGN") => r"(?:0|[1-9][0-9]*)(?:\.[0-9]+)?[eE][+-][^0-9]";
+ lit("FLOAT") => r"(?:0|[1-9][0-9]*(?:_[0-9]+)*)(?:\.[0-9]+(?:_[0-9]+)*)?(?:[eE][+-]?[0-9]+(?:_[0-9]+)*)?";
+ error("FLOAT_JUNK_AFTER_POINT", "junk after decimal point in number literal") => r"(?:0|[1-9][0-9]*(?:_[0-9]+)*)\.[^0-9]";
+ error("FLOAT_JUNK_AFTER_EXPONENT", "junk after exponent in number literal") => r"(?:0|[1-9][0-9]*(?:_[0-9]+)*)(?:\.[0-9]+(?:_[0-9]+)*)?[eE][^+\-0-9]";
+ error("FLOAT_JUNK_AFTER_EXPONENT_SIGN", "junk after exponent sign in number literal") => r"(?:0|[1-9][0-9]*(?:_[0-9]+)*)(?:\.[0-9]+(?:_[0-9]+)*)?[eE][+-][^0-9]";
lit("STRING_DOUBLE") => "\"(?s:[^\"\\\\]|\\\\.)*\"";
- error("STRING_DOUBLE_UNTERMINATED") => "\"(?s:[^\"\\\\]|\\\\.)*";
+ error("STRING_DOUBLE_UNTERMINATED", "unterminated double-quoted string") => "\"(?s:[^\"\\\\]|\\\\.)*";
lit("STRING_SINGLE") => "'(?s:[^'\\\\]|\\\\.)*'";
- error("STRING_SINGLE_UNTERMINATED") => "'(?s:[^'\\\\]|\\\\.)*";
+ error("STRING_SINGLE_UNTERMINATED", "unterminated single-quoted string") => "'(?s:[^'\\\\]|\\\\.)*";
lit("STRING_DOUBLE_VERBATIM") => "@\"(?:[^\"]|\"\")*\"";
- error("STRING_DOUBLE_VERBATIM_UNTERMINATED") => "@\"(?:[^\"]|\"\")*";
+ error("STRING_DOUBLE_VERBATIM_UNTERMINATED", "unterminated verbatim double-quoted string") => "@\"(?:[^\"]|\"\")*";
lit("STRING_SINGLE_VERBATIM") => "@'(?:[^']|'')*'";
- error("STRING_SINGLE_VERBATIM_UNTERMINATED") => "@'(?:[^']|'')*";
- error("STRING_VERBATIM_MISSING_QUOTES") => "@[^\"'\\s]\\S+";
+ error("STRING_SINGLE_VERBATIM_UNTERMINATED", "unterminated verbatim single-quoted string") => "@'(?:[^']|'')*";
+ error("STRING_VERBATIM_MISSING_QUOTES", "verbatim string missing opening quotes") => "@[^\"'\\s]\\S+";
lit("STRING_BLOCK") => r"\|\|\|", "crate::string_block::lex_str_block_test";
- error("STRING_BLOCK_UNEXPECTED_END", lexer = true);
- error("STRING_BLOCK_MISSING_NEW_LINE", lexer = true);
- error("STRING_BLOCK_MISSING_TERMINATION", lexer = true);
- error("STRING_BLOCK_MISSING_INDENT", lexer = true);
+ error("STRING_BLOCK_UNEXPECTED_END", "unexpected end of text block", lexer = true);
+ error("STRING_BLOCK_MISSING_NEW_LINE", "text block requires new line after |||", lexer = true);
+ error("STRING_BLOCK_MISSING_TERMINATION", "unterminated text block", lexer = true);
+ error("STRING_BLOCK_MISSING_INDENT", "text block first line must be indented", lexer = true);
lit("IDENT") => r"[_a-zA-Z][_a-zA-Z0-9]*";
lit("WHITESPACE") => r"[ \t\n\r]+";
lit("SINGLE_LINE_SLASH_COMMENT") => r"//[^\r\n]*?(\r\n|\n)?";
lit("SINGLE_LINE_HASH_COMMENT") => r"#[^\r\n]*?(\r\n|\n)?";
lit("MULTI_LINE_COMMENT") => r"/\*([^*]|\*[^/])*\*/";
- error("COMMENT_TOO_SHORT") => r"/\*/";
- error("COMMENT_UNTERMINATED") => r"/\*([^*/]|\*[^/])+";
+ error("COMMENT_TOO_SHORT", "comment too short") => r"/\*/";
+ error("COMMENT_UNTERMINATED", "unterminated multi-line comment") => r"/\*([^*/]|\*[^/])+";
+ error("NO_OPERATOR", "expected operator");
+ error("MISSING_TOKEN", "missing token");
+ error("UNEXPECTED_TOKEN", "unexpected token");
+ error("CUSTOM", "error");
];
kinds
}
xtask/src/sourcegen/mod.rsdiffbeforeafterboth1use std::{collections::HashMap, path::PathBuf};23use anyhow::Result;4use ast::{lower, AstSrc};5use itertools::Itertools;6use kinds::{KindsSrc, TokenKind};7use proc_macro2::{Ident, Punct, Spacing, Span, TokenStream};8use quote::{format_ident, quote};9use ungrammar::Grammar;10use util::{ensure_file_contents, reformat, to_pascal_case, to_upper_snake_case};1112mod ast;13mod kinds;14mod util;1516enum SpecialName {17 Literal,18 Meta,19 Error,20}21fn classify_special(name: &str) -> Option<(SpecialName, &str)> {22 let name = name.strip_suffix('!')?;23 Some(if let Some(name) = name.strip_prefix("LIT_") {24 (SpecialName::Literal, name)25 } else if let Some(name) = name.strip_prefix("META_") {26 (SpecialName::Meta, name)27 } else if let Some(name) = name.strip_prefix("ERROR_") {28 (SpecialName::Error, name)29 } else {30 return None;31 })32}3334pub fn generate_ungrammar() -> Result<()> {35 let grammar: Grammar = include_str!(concat!(36 env!("CARGO_MANIFEST_DIR"),37 "/../crates/jrsonnet-rowan-parser/jsonnet.ungram"38 ))39 .parse()?;4041 let mut kinds = kinds::jsonnet_kinds();42 let ast = lower(&kinds, &grammar);4344 for token in grammar.tokens() {45 let token = &grammar[token];46 let token = &token.name.clone();47 if !kinds.is_token(token) {48 if let Some((special, name)) = classify_special(token) {49 match special {50 SpecialName::Literal => panic!("literal is not defined: {name}"),51 SpecialName::Meta => {52 eprintln!("implicit meta: {name}");53 kinds.define_token(TokenKind::Meta {54 grammar_name: token.to_owned(),55 name: format!("META_{name}"),56 });57 }58 SpecialName::Error => {59 eprintln!("implicit error: {name}");60 kinds.define_token(TokenKind::Error {61 grammar_name: token.to_owned(),62 name: format!("ERROR_{name}"),63 regex: None,64 priority: None,65 is_lexer_error: true,66 });67 }68 }69 continue;70 }71 let name = to_upper_snake_case(token);72 eprintln!("implicit kw: {token}");73 kinds.define_token(TokenKind::Keyword {74 code: token.to_owned(),75 name: format!("{name}_KW"),76 });77 }78 }79 for node in &ast.nodes {80 let name = to_upper_snake_case(&node.name);81 kinds.define_node(&name);82 }83 for enum_ in &ast.enums {84 let name = to_upper_snake_case(&enum_.name);85 kinds.define_node(&name);86 }87 for token_enum in &ast.token_enums {88 let name = to_upper_snake_case(&token_enum.name);89 kinds.define_node(&name);90 }9192 let syntax_kinds = generate_syntax_kinds(&kinds, &ast, false)?;9394 let nodes = generate_nodes(&kinds, &ast)?;95 ensure_file_contents(96 &PathBuf::from(concat!(97 env!("CARGO_MANIFEST_DIR"),98 "/../crates/jrsonnet-rowan-parser/src/generated/syntax_kinds.rs",99 )),100 &syntax_kinds,101 );102 ensure_file_contents(103 &PathBuf::from(concat!(104 env!("CARGO_MANIFEST_DIR"),105 "/../crates/jrsonnet-rowan-parser/src/generated/nodes.rs",106 )),107 &nodes,108 );109110 let lexer_syntax_kinds = generate_syntax_kinds(&kinds, &ast, true)?;111 ensure_file_contents(112 &PathBuf::from(concat!(113 env!("CARGO_MANIFEST_DIR"),114 "/../crates/jrsonnet-lexer/src/generated/syntax_kinds.rs",115 )),116 &lexer_syntax_kinds,117 );118 Ok(())119}120121fn generate_syntax_kinds(kinds: &KindsSrc, grammar: &AstSrc, lexer: bool) -> Result<String> {122 let t_macros = kinds.tokens().filter_map(TokenKind::expand_t_macros);123 let token_kinds = kinds.tokens().map(|t| t.expand_kind(lexer));124125 let keywords = kinds126 .tokens()127 .filter(|k| matches!(k, TokenKind::Keyword { .. }))128 .map(TokenKind::name)129 .map(|n| format_ident!("{n}"));130131 let mut nodes = kinds132 .nodes133 .iter()134 .map(|name| format_ident!("{}", name))135 .collect::<Vec<_>>();136137 if lexer {138 nodes.clear();139 }140141 let enums = grammar142 .enums143 .iter()144 .map(|e| format_ident!("{}", to_upper_snake_case(&e.name)))145 .chain(146 grammar147 .token_enums148 .iter()149 .map(|e| format_ident!("{}", to_upper_snake_case(&e.name))),150 )151 .collect::<Vec<_>>();152 let is_enum = if lexer {153 quote! {}154 } else {155 quote! {156 pub fn is_enum(self) -> bool {157 match self {158 #(#enums)|* => true,159 _ => false,160 }161 }162 }163 };164165 let derive_logos = if lexer {166 quote! {167 , logos::Logos168 }169 } else {170 quote! {}171 };172173 let ast = quote! {174 #![allow(bad_style, missing_docs, unreachable_pub, clippy::manual_non_exhaustive, clippy::match_like_matches_macro)]175176 /// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT`.177 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug #derive_logos)]178 #[repr(u16)]179 pub enum SyntaxKind {180 #[doc(hidden)]181 TOMBSTONE,182 #[doc(hidden)]183 EOF,184 #(#token_kinds,)*185 LEXING_ERROR,186 __LAST_TOKEN,187 #(#nodes,)*188 #[doc(hidden)]189 __LAST,190 }191 use self::SyntaxKind::*;192193 impl SyntaxKind {194 pub fn is_keyword(self) -> bool {195 match self {196 #(#keywords)|* => true,197 _ => false,198 }199 }200201 #is_enum202203 pub fn from_raw(r: u16) -> Self {204 assert!(r < Self::__LAST as u16);205 unsafe { std::mem::transmute(r) }206 }207 pub fn into_raw(self) -> u16 {208 self as u16209 }210 }211212 #[macro_export]213 macro_rules! T {#(#t_macros);*}214 #[allow(unused_imports)]215 pub use T;216 };217218 reformat(&ast.to_string())219}220221#[allow(clippy::too_many_lines, clippy::cognitive_complexity)]222fn generate_nodes(kinds: &KindsSrc, grammar: &AstSrc) -> Result<String> {223 let (node_defs, node_boilerplate_impls): (Vec<_>, Vec<_>) = grammar224 .nodes225 .iter()226 .map(|node| {227 let name = format_ident!("{}", node.name);228 let kind = format_ident!("{}", to_upper_snake_case(&node.name));229 let traits = node.traits.iter().map(|trait_name| {230 let trait_name = format_ident!("{}", trait_name);231 quote!(impl ast::#trait_name for #name {})232 });233234 let mut type_positions: HashMap<String, usize> = HashMap::new();235 let field_positions: Vec<_> = node236 .fields237 .iter()238 .map(|field| {239 let ty_str = field.ty().to_string();240 let pos = *type_positions.get(&ty_str).unwrap_or(&0);241 type_positions.insert(ty_str, pos + 1);242 pos243 })244 .collect();245246 let methods = node247 .fields248 .iter()249 .zip(field_positions.iter())250 .map(|(field, &pos)| {251 let method_name = field.method_name(kinds);252 let ty = field.ty();253254 if field.is_many() {255 quote! {256 pub fn #method_name(&self) -> AstChildren<#ty> {257 support::children(&self.syntax)258 }259 }260 } else if let Some(token_kind) = field.token_kind(kinds) {261 quote! {262 pub fn #method_name(&self) -> Option<#ty> {263 support::token(&self.syntax, #token_kind)264 }265 }266 } else if field.is_token_enum(grammar) {267 quote! {268 pub fn #method_name(&self) -> Option<#ty> {269 support::token_child(&self.syntax)270 }271 }272 } else if pos == 0 {273 quote! {274 pub fn #method_name(&self) -> Option<#ty> {275 support::children(&self.syntax).next()276 }277 }278 } else {279 quote! {280 pub fn #method_name(&self) -> Option<#ty> {281 support::children(&self.syntax).nth(#pos)282 }283 }284 }285 });286 (287 quote! {288 #[pretty_doc_comment_placeholder_workaround]289 #[derive(Debug, Clone, PartialEq, Eq, Hash)]290 pub struct #name {291 pub(crate) syntax: SyntaxNode,292 }293294 #(#traits)*295296 impl #name {297 #(#methods)*298 }299 },300 quote! {301 impl AstNode for #name {302 fn can_cast(kind: SyntaxKind) -> bool {303 kind == #kind304 }305 fn cast(syntax: SyntaxNode) -> Option<Self> {306 if Self::can_cast(syntax.kind()) { Some(Self { syntax }) } else { None }307 }308 fn syntax(&self) -> &SyntaxNode { &self.syntax }309 }310 },311 )312 })313 .unzip();314315 let (enum_defs, enum_boilerplate_impls): (Vec<_>, Vec<_>) = grammar316 .enums317 .iter()318 .map(|en| {319 let variants: Vec<_> = en320 .variants321 .iter()322 .map(|var| format_ident!("{}", var))323 .collect();324 let name = format_ident!("{}", en.name);325 let kinds: Vec<_> = variants326 .iter()327 .map(|name| format_ident!("{}", to_upper_snake_case(&name.to_string())))328 .collect();329 let traits = en.traits.iter().map(|trait_name| {330 let trait_name = format_ident!("{}", trait_name);331 quote!(impl ast::#trait_name for #name {})332 });333334 let ast_node = quote! {335 impl AstNode for #name {336 fn can_cast(kind: SyntaxKind) -> bool {337 match kind {338 #(#kinds)|* => true,339 _ => false,340 }341 }342 fn cast(syntax: SyntaxNode) -> Option<Self> {343 let res = match syntax.kind() {344 #(345 #kinds => #name::#variants(#variants { syntax }),346 )*347 _ => return None,348 };349 Some(res)350 }351 fn syntax(&self) -> &SyntaxNode {352 match self {353 #(354 #name::#variants(it) => &it.syntax,355 )*356 }357 }358 }359 };360361 (362 quote! {363 #[pretty_doc_comment_placeholder_workaround]364 #[derive(Debug, Clone, PartialEq, Eq, Hash)]365 pub enum #name {366 #(#variants(#variants),)*367 }368369 #(#traits)*370 },371 quote! {372 #(373 impl From<#variants> for #name {374 fn from(node: #variants) -> #name {375 #name::#variants(node)376 }377 }378 )*379 #ast_node380 },381 )382 })383 .unzip();384385 let (token_enum_defs, token_enum_boilerplate_impls): (Vec<_>, Vec<_>) = grammar386 .token_enums387 .iter()388 .map(|en| {389 let variants: Vec<_> = en390 .variants391 .iter()392 .map(|token| {393 format_ident!(394 "{}",395 to_pascal_case(kinds.token(token).expect("token exists").name())396 )397 })398 .collect();399 let name = format_ident!("{}", en.name);400 let kind_name = format_ident!("{}Kind", en.name);401 let kinds: Vec<_> = variants402 .iter()403 .map(|name| format_ident!("{}", to_upper_snake_case(&name.to_string())))404 .collect();405406 let ast_node = quote! {407 impl AstToken for #name {408 fn can_cast(kind: SyntaxKind) -> bool {409 #kind_name::can_cast(kind)410 }411 fn cast(syntax: SyntaxToken) -> Option<Self> {412 let kind = #kind_name::cast(syntax.kind())?;413 Some(#name { syntax, kind })414 }415 fn syntax(&self) -> &SyntaxToken {416 &self.syntax417 }418 }419420 impl #kind_name {421 fn can_cast(kind: SyntaxKind) -> bool {422 match kind {423 #(#kinds)|* => true,424 _ => false,425 }426 }427 pub fn cast(kind: SyntaxKind) -> Option<Self> {428 let res = match kind {429 #(#kinds => Self::#variants,)*430 _ => return None,431 };432 Some(res)433 }434 }435 };436437 (438 quote! {439 #[pretty_doc_comment_placeholder_workaround]440 #[derive(Debug, Clone, PartialEq, Eq, Hash)]441 pub struct #name { syntax: SyntaxToken, kind: #kind_name }442443 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]444 pub enum #kind_name {445 #(#variants,)*446 }447 },448 quote! {449 #ast_node450451 impl #name {452 pub fn kind(&self) -> #kind_name {453 self.kind454 }455 }456457 impl std::fmt::Display for #name {458 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {459 std::fmt::Display::fmt(self.syntax(), f)460 }461 }462 },463 )464 })465 .unzip();466467 let (any_node_defs, any_node_boilerplate_impls): (Vec<_>, Vec<_>) = grammar468 .nodes469 .iter()470 .flat_map(|node| node.traits.iter().map(move |t| (t, node)))471 .into_group_map()472 .into_iter()473 .sorted_by_key(|(k, _)| *k)474 .map(|(trait_name, nodes)| {475 let name = format_ident!("Any{}", trait_name);476 let trait_name = format_ident!("{}", trait_name);477 let kinds: Vec<_> = nodes478 .iter()479 .map(|name| format_ident!("{}", to_upper_snake_case(&name.name)))480 .collect();481482 (483 quote! {484 #[pretty_doc_comment_placeholder_workaround]485 #[derive(Debug, Clone, PartialEq, Eq, Hash)]486 pub struct #name {487 pub(crate) syntax: SyntaxNode,488 }489 impl ast::#trait_name for #name {}490 },491 quote! {492 impl #name {493 #[inline]494 pub fn new<T: ast::#trait_name>(node: T) -> #name {495 #name {496 syntax: node.syntax().clone()497 }498 }499 }500 impl AstNode for #name {501 fn can_cast(kind: SyntaxKind) -> bool {502 match kind {503 #(#kinds)|* => true,504 _ => false,505 }506 }507 fn cast(syntax: SyntaxNode) -> Option<Self> {508 Self::can_cast(syntax.kind()).then(|| #name { syntax })509 }510 fn syntax(&self) -> &SyntaxNode {511 &self.syntax512 }513 }514 },515 )516 })517 .unzip();518519 let enum_names = grammar.enums.iter().map(|it| &it.name);520 let node_names = grammar.nodes.iter().map(|it| &it.name);521522 let display_impls = enum_names523 .chain(node_names.clone())524 .map(|it| format_ident!("{}", it))525 .map(|name| {526 quote! {527 impl std::fmt::Display for #name {528 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {529 std::fmt::Display::fmt(self.syntax(), f)530 }531 }532 }533 });534535 let ast = quote! {536 #![allow(non_snake_case, clippy::match_like_matches_macro)]537538 use crate::{539 SyntaxNode, SyntaxToken, SyntaxKind::{self, *},540 ast::{AstNode, AstToken, AstChildren, support},541 T,542 };543544 #(#node_defs)*545 #(#enum_defs)*546 #(#token_enum_defs)*547 #(#any_node_defs)*548 #(#node_boilerplate_impls)*549 #(#enum_boilerplate_impls)*550 #(#token_enum_boilerplate_impls)*551 #(#any_node_boilerplate_impls)*552 #(#display_impls)*553 };554555 let ast = ast.to_string().replace("T ! [", "T![");556557 let mut res = String::with_capacity(ast.len() * 2);558559 let mut docs = grammar560 .nodes561 .iter()562 .map(|it| &it.doc)563 .chain(grammar.enums.iter().map(|it| &it.doc));564565 for chunk in ast.split("# [pretty_doc_comment_placeholder_workaround] ") {566 res.push_str(chunk);567 if let Some(doc) = docs.next() {568 write_doc_comment(doc, &mut res);569 }570 }571572 let res = reformat(&res)?;573 Ok(res.replace("#[derive", "\n#[derive"))574}575576fn write_doc_comment(contents: &[String], dest: &mut String) {577 use std::fmt::Write;578 for line in contents {579 writeln!(dest, "///{line}").unwrap();580 }581}582583pub fn escape_token_macro(token: &str) -> TokenStream {584 if "{}[]()$".contains(token) {585 let c = token.chars().next().unwrap();586 quote! { #c }587 } else if token.contains('$') {588 quote! { #token }589 } else if token.chars().all(|v: char| v.is_ascii_lowercase()) {590 let i = Ident::new(token, Span::call_site());591 quote! { #i }592 } else {593 let cs = token.chars().map(|c| Punct::new(c, Spacing::Joint));594 quote! { #(#cs)* }595 }596}1use std::{collections::HashMap, path::PathBuf};23use anyhow::Result;4use ast::{lower, AstSrc};5use itertools::Itertools;6use kinds::{KindsSrc, TokenKind};7use proc_macro2::{Ident, Punct, Spacing, Span, TokenStream};8use quote::{format_ident, quote};9use ungrammar::Grammar;10use util::{ensure_file_contents, reformat, to_pascal_case, to_upper_snake_case};1112mod ast;13mod kinds;14mod util;1516enum SpecialName {17 Literal,18 Meta,19 Error,20}21fn classify_special(name: &str) -> Option<(SpecialName, &str)> {22 let name = name.strip_suffix('!')?;23 Some(if let Some(name) = name.strip_prefix("LIT_") {24 (SpecialName::Literal, name)25 } else if let Some(name) = name.strip_prefix("META_") {26 (SpecialName::Meta, name)27 } else if let Some(name) = name.strip_prefix("ERROR_") {28 (SpecialName::Error, name)29 } else {30 return None;31 })32}3334pub fn generate_ungrammar() -> Result<()> {35 let grammar: Grammar = include_str!(concat!(36 env!("CARGO_MANIFEST_DIR"),37 "/../crates/jrsonnet-rowan-parser/jsonnet.ungram"38 ))39 .parse()?;4041 let mut kinds = kinds::jsonnet_kinds();42 let ast = lower(&kinds, &grammar);4344 for token in grammar.tokens() {45 let token = &grammar[token];46 let token = &token.name.clone();47 if !kinds.is_token(token) {48 if let Some((special, name)) = classify_special(token) {49 match special {50 SpecialName::Literal => panic!("literal is not defined: {name}"),51 SpecialName::Meta => {52 eprintln!("implicit meta: {name}");53 kinds.define_token(TokenKind::Meta {54 grammar_name: token.to_owned(),55 name: format!("META_{name}"),56 });57 }58 SpecialName::Error => {59 panic!("error token ERROR_{name} must be explicitly defined in jsonnet_kinds()");60 }61 }62 continue;63 }64 let name = to_upper_snake_case(token);65 eprintln!("implicit kw: {token}");66 kinds.define_token(TokenKind::Keyword {67 code: token.to_owned(),68 name: format!("{name}_KW"),69 });70 }71 }72 for node in &ast.nodes {73 let name = to_upper_snake_case(&node.name);74 kinds.define_node(&name);75 }76 for enum_ in &ast.enums {77 let name = to_upper_snake_case(&enum_.name);78 kinds.define_node(&name);79 }80 for token_enum in &ast.token_enums {81 let name = to_upper_snake_case(&token_enum.name);82 kinds.define_node(&name);83 }8485 let syntax_kinds = generate_syntax_kinds(&kinds, &ast, false)?;8687 let nodes = generate_nodes(&kinds, &ast)?;88 ensure_file_contents(89 &PathBuf::from(concat!(90 env!("CARGO_MANIFEST_DIR"),91 "/../crates/jrsonnet-rowan-parser/src/generated/syntax_kinds.rs",92 )),93 &syntax_kinds,94 );95 ensure_file_contents(96 &PathBuf::from(concat!(97 env!("CARGO_MANIFEST_DIR"),98 "/../crates/jrsonnet-rowan-parser/src/generated/nodes.rs",99 )),100 &nodes,101 );102103 let lexer_syntax_kinds = generate_syntax_kinds(&kinds, &ast, true)?;104 ensure_file_contents(105 &PathBuf::from(concat!(106 env!("CARGO_MANIFEST_DIR"),107 "/../crates/jrsonnet-lexer/src/generated/syntax_kinds.rs",108 )),109 &lexer_syntax_kinds,110 );111 Ok(())112}113114fn generate_syntax_kinds(kinds: &KindsSrc, grammar: &AstSrc, lexer: bool) -> Result<String> {115 let t_macros = kinds.tokens().filter_map(TokenKind::expand_t_macros);116 let token_kinds = kinds.tokens().map(|t| t.expand_kind(lexer));117118 let keywords = kinds119 .tokens()120 .filter(|k| matches!(k, TokenKind::Keyword { .. }))121 .map(TokenKind::name)122 .map(|n| format_ident!("{n}"));123124 let mut nodes = kinds125 .nodes126 .iter()127 .map(|name| format_ident!("{}", name))128 .collect::<Vec<_>>();129130 if lexer {131 nodes.clear();132 }133134 let enums = grammar135 .enums136 .iter()137 .map(|e| format_ident!("{}", to_upper_snake_case(&e.name)))138 .chain(139 grammar140 .token_enums141 .iter()142 .map(|e| format_ident!("{}", to_upper_snake_case(&e.name))),143 )144 .collect::<Vec<_>>();145 let is_enum = if lexer {146 quote! {}147 } else {148 quote! {149 pub fn is_enum(self) -> bool {150 match self {151 #(#enums)|* => true,152 _ => false,153 }154 }155 }156 };157158 let derive_logos = if lexer {159 quote! {160 , logos::Logos161 }162 } else {163 quote! {}164 };165166 let error_desc_arms = kinds.tokens().filter_map(|t| {167 if let TokenKind::Error {168 name, description, ..169 } = t170 {171 let ident = format_ident!("{name}");172 Some(quote! { #ident => ::core::option::Option::Some(#description) })173 } else {174 None175 }176 });177178 let display_name_arms = kinds.tokens().map(|t| {179 let ident = format_ident!("{}", t.name());180 let display = t.display_name();181 quote! { #ident => #display }182 });183184 let ast = quote! {185 #![allow(bad_style, missing_docs, unreachable_pub, clippy::manual_non_exhaustive, clippy::match_like_matches_macro)]186187 /// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT`.188 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug #derive_logos)]189 #[repr(u16)]190 pub enum SyntaxKind {191 #[doc(hidden)]192 TOMBSTONE,193 #[doc(hidden)]194 EOF,195 #(#token_kinds,)*196 LEXING_ERROR,197 __LAST_TOKEN,198 #(#nodes,)*199 #[doc(hidden)]200 __LAST,201 }202 use self::SyntaxKind::*;203204 impl SyntaxKind {205 pub fn is_keyword(self) -> bool {206 match self {207 #(#keywords)|* => true,208 _ => false,209 }210 }211212 #is_enum213214 pub fn error_description(self) -> Option<&'static str> {215 match self {216 #(#error_desc_arms,)*217 LEXING_ERROR => ::core::option::Option::Some("unexpected character"),218 _ => None,219 }220 }221222 pub fn display_name(self) -> &'static str {223 match self {224 #(#display_name_arms,)*225 LEXING_ERROR => "unexpected character",226 _ => "unknown",227 }228 }229230 pub fn from_raw(r: u16) -> Self {231 assert!(r < Self::__LAST as u16);232 unsafe { std::mem::transmute(r) }233 }234 pub fn into_raw(self) -> u16 {235 self as u16236 }237 }238239 #[macro_export]240 macro_rules! T {#(#t_macros);*}241 #[allow(unused_imports)]242 pub use T;243 };244245 reformat(&ast.to_string())246}247248#[allow(clippy::too_many_lines, clippy::cognitive_complexity)]249fn generate_nodes(kinds: &KindsSrc, grammar: &AstSrc) -> Result<String> {250 let (node_defs, node_boilerplate_impls): (Vec<_>, Vec<_>) = grammar251 .nodes252 .iter()253 .map(|node| {254 let name = format_ident!("{}", node.name);255 let kind = format_ident!("{}", to_upper_snake_case(&node.name));256 let traits = node.traits.iter().map(|trait_name| {257 let trait_name = format_ident!("{}", trait_name);258 quote!(impl ast::#trait_name for #name {})259 });260261 let mut type_positions: HashMap<String, usize> = HashMap::new();262 let field_positions: Vec<_> = node263 .fields264 .iter()265 .map(|field| {266 let ty_str = field.ty().to_string();267 let pos = *type_positions.get(&ty_str).unwrap_or(&0);268 type_positions.insert(ty_str, pos + 1);269 pos270 })271 .collect();272273 let methods = node274 .fields275 .iter()276 .zip(field_positions.iter())277 .map(|(field, &pos)| {278 let method_name = field.method_name(kinds);279 let ty = field.ty();280281 if field.is_many() {282 quote! {283 pub fn #method_name(&self) -> AstChildren<#ty> {284 support::children(&self.syntax)285 }286 }287 } else if let Some(token_kind) = field.token_kind(kinds) {288 quote! {289 pub fn #method_name(&self) -> Option<#ty> {290 support::token(&self.syntax, #token_kind)291 }292 }293 } else if field.is_token_enum(grammar) {294 quote! {295 pub fn #method_name(&self) -> Option<#ty> {296 support::token_child(&self.syntax)297 }298 }299 } else if pos == 0 {300 quote! {301 pub fn #method_name(&self) -> Option<#ty> {302 support::children(&self.syntax).next()303 }304 }305 } else {306 quote! {307 pub fn #method_name(&self) -> Option<#ty> {308 support::children(&self.syntax).nth(#pos)309 }310 }311 }312 });313 (314 quote! {315 #[pretty_doc_comment_placeholder_workaround]316 #[derive(Debug, Clone, PartialEq, Eq, Hash)]317 pub struct #name {318 pub(crate) syntax: SyntaxNode,319 }320321 #(#traits)*322323 impl #name {324 #(#methods)*325 }326 },327 quote! {328 impl AstNode for #name {329 fn can_cast(kind: SyntaxKind) -> bool {330 kind == #kind331 }332 fn cast(syntax: SyntaxNode) -> Option<Self> {333 if Self::can_cast(syntax.kind()) { Some(Self { syntax }) } else { None }334 }335 fn syntax(&self) -> &SyntaxNode { &self.syntax }336 }337 },338 )339 })340 .unzip();341342 let (enum_defs, enum_boilerplate_impls): (Vec<_>, Vec<_>) = grammar343 .enums344 .iter()345 .map(|en| {346 let variants: Vec<_> = en347 .variants348 .iter()349 .map(|var| format_ident!("{}", var))350 .collect();351 let name = format_ident!("{}", en.name);352 let kinds: Vec<_> = variants353 .iter()354 .map(|name| format_ident!("{}", to_upper_snake_case(&name.to_string())))355 .collect();356 let traits = en.traits.iter().map(|trait_name| {357 let trait_name = format_ident!("{}", trait_name);358 quote!(impl ast::#trait_name for #name {})359 });360361 let ast_node = quote! {362 impl AstNode for #name {363 fn can_cast(kind: SyntaxKind) -> bool {364 match kind {365 #(#kinds)|* => true,366 _ => false,367 }368 }369 fn cast(syntax: SyntaxNode) -> Option<Self> {370 let res = match syntax.kind() {371 #(372 #kinds => #name::#variants(#variants { syntax }),373 )*374 _ => return None,375 };376 Some(res)377 }378 fn syntax(&self) -> &SyntaxNode {379 match self {380 #(381 #name::#variants(it) => &it.syntax,382 )*383 }384 }385 }386 };387388 (389 quote! {390 #[pretty_doc_comment_placeholder_workaround]391 #[derive(Debug, Clone, PartialEq, Eq, Hash)]392 pub enum #name {393 #(#variants(#variants),)*394 }395396 #(#traits)*397 },398 quote! {399 #(400 impl From<#variants> for #name {401 fn from(node: #variants) -> #name {402 #name::#variants(node)403 }404 }405 )*406 #ast_node407 },408 )409 })410 .unzip();411412 let (token_enum_defs, token_enum_boilerplate_impls): (Vec<_>, Vec<_>) = grammar413 .token_enums414 .iter()415 .map(|en| {416 let variants: Vec<_> = en417 .variants418 .iter()419 .map(|token| {420 format_ident!(421 "{}",422 to_pascal_case(kinds.token(token).expect("token exists").name())423 )424 })425 .collect();426 let name = format_ident!("{}", en.name);427 let kind_name = format_ident!("{}Kind", en.name);428 let kinds: Vec<_> = variants429 .iter()430 .map(|name| format_ident!("{}", to_upper_snake_case(&name.to_string())))431 .collect();432433 let ast_node = quote! {434 impl AstToken for #name {435 fn can_cast(kind: SyntaxKind) -> bool {436 #kind_name::can_cast(kind)437 }438 fn cast(syntax: SyntaxToken) -> Option<Self> {439 let kind = #kind_name::cast(syntax.kind())?;440 Some(#name { syntax, kind })441 }442 fn syntax(&self) -> &SyntaxToken {443 &self.syntax444 }445 }446447 impl #kind_name {448 fn can_cast(kind: SyntaxKind) -> bool {449 match kind {450 #(#kinds)|* => true,451 _ => false,452 }453 }454 pub fn cast(kind: SyntaxKind) -> Option<Self> {455 let res = match kind {456 #(#kinds => Self::#variants,)*457 _ => return None,458 };459 Some(res)460 }461 }462 };463464 (465 quote! {466 #[pretty_doc_comment_placeholder_workaround]467 #[derive(Debug, Clone, PartialEq, Eq, Hash)]468 pub struct #name { syntax: SyntaxToken, kind: #kind_name }469470 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]471 pub enum #kind_name {472 #(#variants,)*473 }474 },475 quote! {476 #ast_node477478 impl #name {479 pub fn kind(&self) -> #kind_name {480 self.kind481 }482 }483484 impl std::fmt::Display for #name {485 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {486 std::fmt::Display::fmt(self.syntax(), f)487 }488 }489 },490 )491 })492 .unzip();493494 let (any_node_defs, any_node_boilerplate_impls): (Vec<_>, Vec<_>) = grammar495 .nodes496 .iter()497 .flat_map(|node| node.traits.iter().map(move |t| (t, node)))498 .into_group_map()499 .into_iter()500 .sorted_by_key(|(k, _)| *k)501 .map(|(trait_name, nodes)| {502 let name = format_ident!("Any{}", trait_name);503 let trait_name = format_ident!("{}", trait_name);504 let kinds: Vec<_> = nodes505 .iter()506 .map(|name| format_ident!("{}", to_upper_snake_case(&name.name)))507 .collect();508509 (510 quote! {511 #[pretty_doc_comment_placeholder_workaround]512 #[derive(Debug, Clone, PartialEq, Eq, Hash)]513 pub struct #name {514 pub(crate) syntax: SyntaxNode,515 }516 impl ast::#trait_name for #name {}517 },518 quote! {519 impl #name {520 #[inline]521 pub fn new<T: ast::#trait_name>(node: T) -> #name {522 #name {523 syntax: node.syntax().clone()524 }525 }526 }527 impl AstNode for #name {528 fn can_cast(kind: SyntaxKind) -> bool {529 match kind {530 #(#kinds)|* => true,531 _ => false,532 }533 }534 fn cast(syntax: SyntaxNode) -> Option<Self> {535 Self::can_cast(syntax.kind()).then(|| #name { syntax })536 }537 fn syntax(&self) -> &SyntaxNode {538 &self.syntax539 }540 }541 },542 )543 })544 .unzip();545546 let enum_names = grammar.enums.iter().map(|it| &it.name);547 let node_names = grammar.nodes.iter().map(|it| &it.name);548549 let display_impls = enum_names550 .chain(node_names.clone())551 .map(|it| format_ident!("{}", it))552 .map(|name| {553 quote! {554 impl std::fmt::Display for #name {555 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {556 std::fmt::Display::fmt(self.syntax(), f)557 }558 }559 }560 });561562 let ast = quote! {563 #![allow(non_snake_case, clippy::match_like_matches_macro)]564565 use crate::{566 SyntaxNode, SyntaxToken, SyntaxKind::{self, *},567 ast::{AstNode, AstToken, AstChildren, support},568 T,569 };570571 #(#node_defs)*572 #(#enum_defs)*573 #(#token_enum_defs)*574 #(#any_node_defs)*575 #(#node_boilerplate_impls)*576 #(#enum_boilerplate_impls)*577 #(#token_enum_boilerplate_impls)*578 #(#any_node_boilerplate_impls)*579 #(#display_impls)*580 };581582 let ast = ast.to_string().replace("T ! [", "T![");583584 let mut res = String::with_capacity(ast.len() * 2);585586 let mut docs = grammar587 .nodes588 .iter()589 .map(|it| &it.doc)590 .chain(grammar.enums.iter().map(|it| &it.doc));591592 for chunk in ast.split("# [pretty_doc_comment_placeholder_workaround] ") {593 res.push_str(chunk);594 if let Some(doc) = docs.next() {595 write_doc_comment(doc, &mut res);596 }597 }598599 let res = reformat(&res)?;600 Ok(res.replace("#[derive", "\n#[derive"))601}602603fn write_doc_comment(contents: &[String], dest: &mut String) {604 use std::fmt::Write;605 for line in contents {606 writeln!(dest, "///{line}").unwrap();607 }608}609610pub fn escape_token_macro(token: &str) -> TokenStream {611 if "{}[]()$".contains(token) {612 let c = token.chars().next().unwrap();613 quote! { #c }614 } else if token.contains('$') {615 quote! { #token }616 } else if token.chars().all(|v: char| v.is_ascii_lowercase()) {617 let i = Ident::new(token, Span::call_site());618 quote! { #i }619 } else {620 let cs = token.chars().map(|c| Punct::new(c, Spacing::Joint));621 quote! { #(#cs)* }622 }623}