Skip to content

Commit 8e7eedc

Browse files
committed
Update codegen/grammar
1 parent 3928efe commit 8e7eedc

File tree

4 files changed

+71
-29
lines changed

4 files changed

+71
-29
lines changed

Cargo.lock

Lines changed: 3 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ast-generator/Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ edition = "2021"
66
# When updating these dependencies, run `rust/update_cargo_deps.sh`
77
[dependencies]
88
ungrammar = "1.16.1"
9-
proc-macro2 = "1.0.33"
10-
quote = "1.0.12"
11-
itertools = "0.10.1"
9+
proc-macro2 = "1.0.47"
10+
quote = "1.0.20"
11+
either = "1.9.0"
12+
ra_ap_stdx = "0.0.248"
13+
itertools = "0.12.0"

rust/ast-generator/src/codegen/grammar.rs

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ use std::{
1111
fs,
1212
};
1313

14+
use either::Either;
1415
use itertools::Itertools;
1516
use proc_macro2::{Punct, Spacing};
1617
use quote::{format_ident, quote};
18+
use ra_ap_stdx::panic_context;
1719
use ungrammar::{Grammar, Rule};
1820

1921
use crate::{
@@ -495,6 +497,7 @@ fn generate_syntax_kinds(grammar: KindsSrc) -> String {
495497
.map(|name| format_ident!("{}", name))
496498
.collect::<Vec<_>>();
497499

500+
// FIXME: This generates enum kinds?
498501
let nodes = grammar
499502
.nodes
500503
.iter()
@@ -757,6 +760,7 @@ pub(crate) fn lower(grammar: &Grammar) -> AstSrc {
757760
for &node in &nodes {
758761
let name = grammar[node].name.clone();
759762
let rule = &grammar[node].rule;
763+
let _g = panic_context::enter(name.clone());
760764
match lower_enum(grammar, rule) {
761765
Some(variants) => {
762766
let enum_src = AstEnumSrc {
@@ -904,11 +908,16 @@ fn lower_separated_list(
904908
Rule::Seq(it) => it,
905909
_ => return false,
906910
};
907-
let (node, repeat, trailing_sep) = match rule.as_slice() {
911+
912+
let (nt, repeat, trailing_sep) = match rule.as_slice() {
908913
[Rule::Node(node), Rule::Rep(repeat), Rule::Opt(trailing_sep)] => {
909-
(node, repeat, Some(trailing_sep))
914+
(Either::Left(node), repeat, Some(trailing_sep))
915+
}
916+
[Rule::Node(node), Rule::Rep(repeat)] => (Either::Left(node), repeat, None),
917+
[Rule::Token(token), Rule::Rep(repeat), Rule::Opt(trailing_sep)] => {
918+
(Either::Right(token), repeat, Some(trailing_sep))
910919
}
911-
[Rule::Node(node), Rule::Rep(repeat)] => (node, repeat, None),
920+
[Rule::Token(token), Rule::Rep(repeat)] => (Either::Right(token), repeat, None),
912921
_ => return false,
913922
};
914923
let repeat = match &**repeat {
@@ -917,21 +926,34 @@ fn lower_separated_list(
917926
};
918927
if !matches!(
919928
repeat.as_slice(),
920-
[comma, Rule::Node(n)]
921-
if trailing_sep.map_or(true, |it| comma == &**it) && n == node
929+
[comma, nt_]
930+
if trailing_sep.map_or(true, |it| comma == &**it) && match (nt, nt_) {
931+
(Either::Left(node), Rule::Node(nt_)) => node == nt_,
932+
(Either::Right(token), Rule::Token(nt_)) => token == nt_,
933+
_ => false,
934+
}
922935
) {
923936
return false;
924937
}
925-
let ty = grammar[*node].name.clone();
926-
let name = label
927-
.cloned()
928-
.unwrap_or_else(|| pluralize(&to_lower_snake_case(&ty)));
929-
let field = Field::Node {
930-
name,
931-
ty,
932-
cardinality: Cardinality::Many,
933-
};
934-
acc.push(field);
938+
match nt {
939+
Either::Right(token) => {
940+
let name = clean_token_name(&grammar[*token].name);
941+
let field = Field::Token(name);
942+
acc.push(field);
943+
}
944+
Either::Left(node) => {
945+
let ty = grammar[*node].name.clone();
946+
let name = label
947+
.cloned()
948+
.unwrap_or_else(|| pluralize(&to_lower_snake_case(&ty)));
949+
let field = Field::Node {
950+
name,
951+
ty,
952+
cardinality: Cardinality::Many,
953+
};
954+
acc.push(field);
955+
}
956+
}
935957
true
936958
}
937959

rust/ast-generator/src/codegen/grammar/ast_src.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,34 @@ const CONTEXTUAL_KEYWORDS: &[&str] = &[
118118
"dyn",
119119
"auto",
120120
"yeet",
121+
"safe",
121122
];
122123
// keywords we use for special macro expansions
123-
const CONTEXTUAL_BUILTIN_KEYWORDS: &[&str] = &["builtin", "offset_of", "format_args", "asm"];
124+
const CONTEXTUAL_BUILTIN_KEYWORDS: &[&str] = &[
125+
"asm",
126+
"att_syntax",
127+
"builtin",
128+
"clobber_abi",
129+
"format_args",
130+
// "in",
131+
"inlateout",
132+
"inout",
133+
"label",
134+
"lateout",
135+
"may_unwind",
136+
"nomem",
137+
"noreturn",
138+
"nostack",
139+
"offset_of",
140+
"options",
141+
"out",
142+
"preserves_flags",
143+
"pure",
144+
// "raw",
145+
"readonly",
146+
"sym",
147+
];
148+
124149
// keywords that are keywords depending on the edition
125150
const EDITION_DEPENDENT_KEYWORDS: &[(&str, Edition)] = &[
126151
("try", Edition::Edition2018),

0 commit comments

Comments
 (0)