Update codegen/grammar

This commit is contained in:
Arthur Baars
2024-12-16 13:51:34 +01:00
parent 3928efe05f
commit 8e7eedc172
4 changed files with 71 additions and 29 deletions

13
Cargo.lock generated
View File

@@ -116,9 +116,11 @@ checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
name = "ast-generator" name = "ast-generator"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"itertools 0.10.5", "either",
"itertools 0.12.1",
"proc-macro2", "proc-macro2",
"quote", "quote",
"ra_ap_stdx",
"ungrammar", "ungrammar",
] ]
@@ -902,15 +904,6 @@ version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itertools" name = "itertools"
version = "0.12.1" version = "0.12.1"

View File

@@ -6,6 +6,8 @@ edition = "2021"
# When updating these dependencies, run `rust/update_cargo_deps.sh` # When updating these dependencies, run `rust/update_cargo_deps.sh`
[dependencies] [dependencies]
ungrammar = "1.16.1" ungrammar = "1.16.1"
proc-macro2 = "1.0.33" proc-macro2 = "1.0.47"
quote = "1.0.12" quote = "1.0.20"
itertools = "0.10.1" either = "1.9.0"
ra_ap_stdx = "0.0.248"
itertools = "0.12.0"

View File

@@ -11,9 +11,11 @@ use std::{
fs, fs,
}; };
use either::Either;
use itertools::Itertools; use itertools::Itertools;
use proc_macro2::{Punct, Spacing}; use proc_macro2::{Punct, Spacing};
use quote::{format_ident, quote}; use quote::{format_ident, quote};
use ra_ap_stdx::panic_context;
use ungrammar::{Grammar, Rule}; use ungrammar::{Grammar, Rule};
use crate::{ use crate::{
@@ -495,6 +497,7 @@ fn generate_syntax_kinds(grammar: KindsSrc) -> String {
.map(|name| format_ident!("{}", name)) .map(|name| format_ident!("{}", name))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// FIXME: This generates enum kinds?
let nodes = grammar let nodes = grammar
.nodes .nodes
.iter() .iter()
@@ -757,6 +760,7 @@ pub(crate) fn lower(grammar: &Grammar) -> AstSrc {
for &node in &nodes { for &node in &nodes {
let name = grammar[node].name.clone(); let name = grammar[node].name.clone();
let rule = &grammar[node].rule; let rule = &grammar[node].rule;
let _g = panic_context::enter(name.clone());
match lower_enum(grammar, rule) { match lower_enum(grammar, rule) {
Some(variants) => { Some(variants) => {
let enum_src = AstEnumSrc { let enum_src = AstEnumSrc {
@@ -904,11 +908,16 @@ fn lower_separated_list(
Rule::Seq(it) => it, Rule::Seq(it) => it,
_ => return false, _ => return false,
}; };
let (node, repeat, trailing_sep) = match rule.as_slice() {
let (nt, repeat, trailing_sep) = match rule.as_slice() {
[Rule::Node(node), Rule::Rep(repeat), Rule::Opt(trailing_sep)] => { [Rule::Node(node), Rule::Rep(repeat), Rule::Opt(trailing_sep)] => {
(node, repeat, Some(trailing_sep)) (Either::Left(node), repeat, Some(trailing_sep))
} }
[Rule::Node(node), Rule::Rep(repeat)] => (node, repeat, None), [Rule::Node(node), Rule::Rep(repeat)] => (Either::Left(node), repeat, None),
[Rule::Token(token), Rule::Rep(repeat), Rule::Opt(trailing_sep)] => {
(Either::Right(token), repeat, Some(trailing_sep))
}
[Rule::Token(token), Rule::Rep(repeat)] => (Either::Right(token), repeat, None),
_ => return false, _ => return false,
}; };
let repeat = match &**repeat { let repeat = match &**repeat {
@@ -917,21 +926,34 @@ fn lower_separated_list(
}; };
if !matches!( if !matches!(
repeat.as_slice(), repeat.as_slice(),
[comma, Rule::Node(n)] [comma, nt_]
if trailing_sep.map_or(true, |it| comma == &**it) && n == node if trailing_sep.map_or(true, |it| comma == &**it) && match (nt, nt_) {
(Either::Left(node), Rule::Node(nt_)) => node == nt_,
(Either::Right(token), Rule::Token(nt_)) => token == nt_,
_ => false,
}
) { ) {
return false; return false;
} }
let ty = grammar[*node].name.clone(); match nt {
let name = label Either::Right(token) => {
.cloned() let name = clean_token_name(&grammar[*token].name);
.unwrap_or_else(|| pluralize(&to_lower_snake_case(&ty))); let field = Field::Token(name);
let field = Field::Node { acc.push(field);
name, }
ty, Either::Left(node) => {
cardinality: Cardinality::Many, let ty = grammar[*node].name.clone();
}; let name = label
acc.push(field); .cloned()
.unwrap_or_else(|| pluralize(&to_lower_snake_case(&ty)));
let field = Field::Node {
name,
ty,
cardinality: Cardinality::Many,
};
acc.push(field);
}
}
true true
} }

View File

@@ -118,9 +118,34 @@ const CONTEXTUAL_KEYWORDS: &[&str] = &[
"dyn", "dyn",
"auto", "auto",
"yeet", "yeet",
"safe",
]; ];
// keywords we use for special macro expansions // keywords we use for special macro expansions
const CONTEXTUAL_BUILTIN_KEYWORDS: &[&str] = &["builtin", "offset_of", "format_args", "asm"]; const CONTEXTUAL_BUILTIN_KEYWORDS: &[&str] = &[
"asm",
"att_syntax",
"builtin",
"clobber_abi",
"format_args",
// "in",
"inlateout",
"inout",
"label",
"lateout",
"may_unwind",
"nomem",
"noreturn",
"nostack",
"offset_of",
"options",
"out",
"preserves_flags",
"pure",
// "raw",
"readonly",
"sym",
];
// keywords that are keywords depending on the edition // keywords that are keywords depending on the edition
const EDITION_DEPENDENT_KEYWORDS: &[(&str, Edition)] = &[ const EDITION_DEPENDENT_KEYWORDS: &[(&str, Edition)] = &[
("try", Edition::Edition2018), ("try", Edition::Edition2018),