Skip to content

Commit a94efa1

Browse files
authored
Update grammer for choice being encoded as an index. (#134)
Update grammer for choice being encoded as an index.
2 parents 2774766 + 3bbd085 commit a94efa1

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ doctest = false
2929
test = false
3030

3131
[patch.'crates-io']
32-
grammer = { git = "https://github.com/lykenware/grammer", rev = "7c360f270d6482d3bc499b05fe2a3f46eb68a2d7" }
32+
grammer = { git = "https://github.com/lykenware/grammer", rev = "c36836d09c2f8954b7535d38e9ca57c8a6f5041e" }
3333

3434
[workspace]
3535
members = [

src/generate/rust.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ impl<Pat: RustInputPat> NodeShapeMethods<Pat> for NodeShape<IRule> {
304304
let variant = match shape {
305305
NodeShape::Opaque => quote!(Opaque),
306306
NodeShape::Alias(inner) => quote!(Alias(#inner)),
307-
NodeShape::Choice => quote!(Choice),
307+
NodeShape::Choice(count) => quote!(Choice(#count)),
308308
NodeShape::Opt(inner) => quote!(Opt(#inner)),
309309
NodeShape::Split(left, right) => quote!(Split(#left, #right)),
310310
};
@@ -389,6 +389,13 @@ impl<Pat: MatchesEmpty + RustInputPat> GrammarGenerateMethods<Pat> for grammer::
389389
let rule = *rules.anon.get_index(i).unwrap();
390390
rule.node_shape(cx, Some(rules.named))
391391
.map(|rule| rule.node_kind(cx, &mut rules));
392+
// HACK(eddyb) this is needed because `NodeShape` doesn't
393+
// encode `Choice` cases directly, only their count.
394+
if let Rule::Or(cases) = &cx[rule] {
395+
for &rule in cases {
396+
rule.node_kind(cx, &mut rules);
397+
}
398+
}
392399
i += 1;
393400
}
394401

@@ -727,12 +734,11 @@ fn reify_as<Pat>(label: Rc<CodeLabel>) -> Thunk<impl ContFn<Pat>> {
727734
})
728735
}
729736

730-
fn forest_add_choice<Pat: RustInputPat>(rule: IRule, choice: IRule) -> Thunk<impl ContFn<Pat>> {
737+
fn forest_add_choice<Pat: RustInputPat>(rule: IRule, choice: usize) -> Thunk<impl ContFn<Pat>> {
731738
Thunk::new(move |mut cont| {
732739
if let Some(rules) = &mut cont.rules.as_mut() {
733740
let node_kind_src = rule.node_kind(cont.cx, rules).to_src();
734-
let choice_src = choice.node_kind(cont.cx, rules).to_src();
735-
cont = thunk!(rt.forest_add_choice(#node_kind_src, #choice_src);).apply(cont);
741+
cont = thunk!(rt.forest_add_choice(#node_kind_src, #choice);).apply(cont);
736742
}
737743
cont
738744
})
@@ -794,8 +800,8 @@ impl<Pat: RustInputPat> RuleGenerateMethods<Pat> for IRule {
794800
concat_and_forest_add(self, left, right.generate_parse()).apply(cont)
795801
}
796802
Rule::Or(ref cases) => {
797-
parallel(ThunkIter(cases.iter().map(|&rule| {
798-
rule.generate_parse() + forest_add_choice(self, rule)
803+
parallel(ThunkIter(cases.iter().enumerate().map(|(i, &rule)| {
804+
rule.generate_parse() + forest_add_choice(self, i)
799805
})))
800806
.apply(cont)
801807
}
@@ -1407,6 +1413,19 @@ fn declare_node_kind<Pat: RustInputPat>(
14071413
.iter()
14081414
.map(|&rule| rule.node_shape(cx, Some(rules.named)).to_src(cx, rules))
14091415
.collect::<Vec<_>>();
1416+
let nodes_shape_choices = all_rules
1417+
.iter()
1418+
.map(|&rule| {
1419+
let choices = match &cx[rule] {
1420+
Rule::Or(choices) => &choices[..],
1421+
_ => &[],
1422+
};
1423+
let choices = choices
1424+
.iter()
1425+
.map(|rule| rule.node_kind(cx, rules).to_src());
1426+
quote!([#(#choices,)*])
1427+
})
1428+
.collect::<Vec<_>>();
14101429

14111430
quote!(
14121431
pub struct _G;
@@ -1427,6 +1446,11 @@ fn declare_node_kind<Pat: RustInputPat>(
14271446
#(#nodes_kind_src => #nodes_shape_src),*
14281447
}
14291448
}
1449+
fn node_shape_choice_get(&self, kind: _P, i: usize) -> _P {
1450+
match kind {
1451+
#(#nodes_kind_src => #nodes_shape_choices[i]),*
1452+
}
1453+
}
14301454
fn node_desc(&self, kind: _P) -> String {
14311455
let s = match kind {
14321456
#(#nodes_kind_src => #nodes_desc),*

src/runtime.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ where
140140
self.saved.take().unwrap()
141141
}
142142

143-
pub fn forest_add_choice(&mut self, kind: P, choice: P) {
143+
// FIXME(eddyb) safeguard this against misuse.
144+
pub fn forest_add_choice(&mut self, kind: P, choice: usize) {
144145
self.parser.forest_add_choice(kind, choice);
145146
}
146147

0 commit comments

Comments
 (0)