Skip to content

Commit 9f2a212

Browse files
committed
Move runtime::traverse back into grammer.
1 parent a0a7528 commit 9f2a212

File tree

5 files changed

+79
-543
lines changed

5 files changed

+79
-543
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 = "3c8cef4f42ac8cb2483fc14e55dfdd180d74a96a" }
32+
grammer = { git = "https://github.com/lykenware/grammer", rev = "5bb946aaa198b9c16d1a3a00545c643dcc04bed6" }
3333

3434
[workspace]
3535
members = [

src/generate/rust.rs

Lines changed: 61 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ enum RustAdt {
108108
trait RuleWithFieldsMethods<Pat> {
109109
fn rust_fields(self, cx: &Context<Pat>) -> RustFields;
110110
fn rust_adt(self, cx: &Context<Pat>) -> RustAdt;
111+
fn traverse_shape(self, cx: &Context<Pat>, rust_fields: &RustFields) -> Src;
111112
}
112113

113114
impl<Pat: RustInputPat> RuleWithFieldsMethods<Pat> for RuleWithFields {
@@ -224,6 +225,48 @@ impl<Pat: RustInputPat> RuleWithFieldsMethods<Pat> for RuleWithFields {
224225

225226
RustAdt::Struct(self.rust_fields(cx))
226227
}
228+
229+
fn traverse_shape(self, cx: &Context<Pat>, rust_fields: &RustFields) -> Src {
230+
let children = match &cx[self.fields] {
231+
Fields::Leaf(None) => return quote!(_),
232+
Fields::Leaf(Some(field)) => {
233+
let (i, _, _) = rust_fields.get_full(&field.name).unwrap();
234+
return quote!(#i);
235+
}
236+
Fields::Aggregate(children) => children,
237+
};
238+
let child = |rule, i| RuleWithFields {
239+
rule,
240+
fields: children
241+
.get(i)
242+
.cloned()
243+
.unwrap_or_else(|| cx.intern(Fields::Leaf(None))),
244+
};
245+
246+
match cx[self.rule] {
247+
Rule::Empty
248+
| Rule::Eat(_)
249+
| Rule::Call(_)
250+
| Rule::RepeatMany(..)
251+
| Rule::RepeatMore(..) => unreachable!(),
252+
Rule::Concat([left, right]) => {
253+
let left = child(left, 0).traverse_shape(cx, rust_fields);
254+
let right = child(right, 1).traverse_shape(cx, rust_fields);
255+
quote!((#left #right))
256+
}
257+
Rule::Or(ref cases) => {
258+
let cases_shape = cases
259+
.iter()
260+
.enumerate()
261+
.map(|(i, &rule)| child(rule, i).traverse_shape(cx, rust_fields));
262+
quote!({ _ @ #(#cases_shape)* })
263+
}
264+
Rule::Opt(rule) => {
265+
let shape = child(rule, 0).traverse_shape(cx, rust_fields);
266+
quote!([#shape])
267+
}
268+
}
269+
}
227270
}
228271

229272
trait RuleMethods<Pat>: Sized {
@@ -848,70 +891,6 @@ impl<Pat: RustInputPat> RuleGenerateMethods<Pat> for IRule {
848891
}
849892
}
850893

851-
trait RuleWithFieldsGenerateMethods<Pat> {
852-
fn generate_traverse_shape(
853-
self,
854-
cx: &Context<Pat>,
855-
rules: &mut RuleMap<'_>,
856-
rust_fields: &RustFields,
857-
) -> Src;
858-
}
859-
860-
impl<Pat: RustInputPat> RuleWithFieldsGenerateMethods<Pat> for RuleWithFields {
861-
fn generate_traverse_shape(
862-
self,
863-
cx: &Context<Pat>,
864-
rules: &mut RuleMap<'_>,
865-
rust_fields: &RustFields,
866-
) -> Src {
867-
let children = match &cx[self.fields] {
868-
Fields::Leaf(None) => return quote!(_),
869-
Fields::Leaf(Some(field)) => {
870-
let (i, _, _) = rust_fields.get_full(&field.name).unwrap();
871-
return quote!(#i);
872-
}
873-
Fields::Aggregate(children) => children,
874-
};
875-
let child = |rule, i| RuleWithFields {
876-
rule,
877-
fields: children
878-
.get(i)
879-
.cloned()
880-
.unwrap_or_else(|| cx.intern(Fields::Leaf(None))),
881-
};
882-
883-
match cx[self.rule] {
884-
Rule::Empty
885-
| Rule::Eat(_)
886-
| Rule::Call(_)
887-
| Rule::RepeatMany(..)
888-
| Rule::RepeatMore(..) => unreachable!(),
889-
Rule::Concat([left, right]) => {
890-
let left = child(left, 0).generate_traverse_shape(cx, rules, rust_fields);
891-
let right = child(right, 1).generate_traverse_shape(cx, rules, rust_fields);
892-
quote!((#left, #right))
893-
}
894-
Rule::Or(ref cases) => {
895-
// HACK(eddyb) only collected to a `Vec` to avoid `rules` borrow conflicts.
896-
let cases_shape = cases
897-
.iter()
898-
.enumerate()
899-
.map(|(i, &rule)| {
900-
child(rule, i).generate_traverse_shape(cx, rules, rust_fields)
901-
})
902-
.collect::<Vec<_>>();
903-
let cases_node_kind = cases.iter().map(|rule| rule.node_kind(cx, rules));
904-
let cases_node_kind_src = cases_node_kind.map(|kind| kind.to_src());
905-
quote!({ _P; _ @ #(#cases_node_kind_src => #cases_shape,)* })
906-
}
907-
Rule::Opt(rule) => {
908-
let shape = child(rule, 0).generate_traverse_shape(cx, rules, rust_fields);
909-
quote!([#shape])
910-
}
911-
}
912-
}
913-
}
914-
915894
fn impl_parse_with<Pat>(cx: &Context<Pat>, name: IStr) -> Src
916895
where
917896
Pat: RustInputPat,
@@ -1088,9 +1067,7 @@ where
10881067
.values()
10891068
.map(|(v_rule, variant)| match variant {
10901069
RustVariant::Newtype(_) => quote!(_),
1091-
RustVariant::StructLike(v_fields) => {
1092-
v_rule.generate_traverse_shape(cx, rules, v_fields)
1093-
}
1070+
RustVariant::StructLike(v_fields) => v_rule.traverse_shape(cx, v_fields),
10941071
})
10951072
.collect::<Vec<_>>();
10961073
let variants_expr = variants.iter().map(|(&v_name, (_, variant))| {
@@ -1113,7 +1090,7 @@ where
11131090

11141091
(
11151092
max_fields_len + 1,
1116-
quote!({ _P; #max_fields_len @ #(#variants_kind_src => #variants_shape,)* }),
1093+
quote!({ #max_fields_len @ #(#variants_shape)* }),
11171094
quote!(
11181095
match _r[#max_fields_len].unwrap().kind {
11191096
#(#variants_kind_src => #variants_expr,)*
@@ -1133,7 +1110,7 @@ where
11331110

11341111
(
11351112
fields.len(),
1136-
rule.generate_traverse_shape(cx, rules, fields),
1113+
rule.traverse_shape(cx, fields),
11371114
quote!(
11381115
#ident {
11391116
#(#fields_ident: #fields_expr),*
@@ -1144,23 +1121,29 @@ where
11441121
}
11451122
};
11461123

1147-
quote!(impl<'a, 'i, I>
1148-
traverse::FromShape<&'a gll::grammer::forest::ParseForest<'i, _G, I>, Node<'i, _G>>
1124+
quote!(
1125+
impl<'i, I> _forest::typed::Shaped for #ident<'_, 'i, I>
1126+
where I: gll::grammer::input::Input,
1127+
{
1128+
type Shape = _forest::typed::shape!(#shape);
1129+
type State = [usize; <_forest::typed::shape!(#shape) as _forest::typed::ShapeStateLen>::STATE_LEN];
1130+
}
1131+
impl<'a, 'i, I>
1132+
_forest::typed::FromShapeFields<'a, _forest::ParseForest<'i, _G, I>, Node<'i, _G>>
11491133
for #ident<'a, 'i, I>
11501134
where I: gll::grammer::input::Input,
11511135
{
1152-
type Shape = traverse::ty!(#shape);
1136+
type Output = Self;
11531137
type Fields = [Option<Node<'i, _G>>; #total_fields];
11541138

1155-
const SHAPE: Self::Shape = traverse::new!(#shape);
1156-
1157-
fn from_shape(
1158-
forest: &'a gll::grammer::forest::ParseForest<'i, _G, I>,
1139+
fn from_shape_fields(
1140+
forest: &'a _forest::ParseForest<'i, _G, I>,
11591141
_r: Self::Fields,
11601142
) -> Self {
11611143
#from_shape
11621144
}
1163-
})
1145+
}
1146+
)
11641147
}
11651148

11661149
fn rule_debug_impl<Pat>(cx: &Context<Pat>, name: IStr, rust_adt: &RustAdt) -> Src {
@@ -1333,7 +1316,7 @@ fn declare_node_kind<Pat: RustInputPat>(
13331316
)*
13341317
}
13351318

1336-
impl gll::grammer::forest::GrammarReflector for _G {
1319+
impl _forest::GrammarReflector for _G {
13371320
type NodeKind = _P;
13381321

13391322
fn node_shape(&self, kind: _P) -> NodeShape<_P> {

src/generate/templates/header.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub type Any = dyn any::Any;
44
pub struct Ambiguity<T>(T);
55

66
pub struct OwnedHandle<I: gll::grammer::input::Input, T: ?Sized> {
7-
forest_and_node: gll::grammer::forest::OwnedParseForestAndNode<_G, I>,
7+
forest_and_node: _forest::OwnedParseForestAndNode<_G, I>,
88
_marker: PhantomData<T>,
99
}
1010

@@ -19,7 +19,7 @@ impl<I: gll::grammer::input::Input, T: ?Sized> OwnedHandle<I, T> {
1919

2020
pub struct Handle<'a, 'i, I: gll::grammer::input::Input, T: ?Sized> {
2121
pub node: Node<'i, _G>,
22-
pub forest: &'a gll::grammer::forest::ParseForest<'i, _G, I>,
22+
pub forest: &'a _forest::ParseForest<'i, _G, I>,
2323
_marker: PhantomData<T>,
2424
}
2525

@@ -68,8 +68,10 @@ impl<'a, 'i, I: gll::grammer::input::Input, T> fmt::Debug for Handle<'a, 'i, I,
6868
where
6969
// FIXME(eddyb) this should be `Handle<'a, 'i, I, T>: fmt::Debug` but that
7070
// runs into overflows looking for `Handle<I, [[[...[[[_]]]...]]]>`.
71-
T: traverse::FromShape<&'a gll::grammer::forest::ParseForest<'i, _G, I>, Node<'i, _G>>,
72-
T: fmt::Debug,
71+
T: _forest::typed::Shaped
72+
+ _forest::typed::FromShapeFields<'a, _forest::ParseForest<'i, _G, I>, Node<'i, _G>>,
73+
T::Shape: _forest::typed::Shape<_forest::ParseForest<'i, _G, I>, Node<'i, _G>>,
74+
T::Output: fmt::Debug,
7375
{
7476
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7577
write!(f, "{:?} => ", self.source_info())?;
@@ -227,8 +229,10 @@ impl<'a, 'i, I: gll::grammer::input::Input, T> Handle<'a, 'i, I, [T]> {
227229
impl<'a, 'i, I, T> fmt::Debug for Handle<'a, 'i, I, T>
228230
where
229231
I: gll::grammer::input::Input,
230-
T: traverse::FromShape<&'a gll::grammer::forest::ParseForest<'i, _G, I>, Node<'i, _G>>,
231-
T: fmt::Debug,
232+
T: _forest::typed::Shaped
233+
+ _forest::typed::FromShapeFields<'a, _forest::ParseForest<'i, _G, I>, Node<'i, _G>>,
234+
T::Shape: _forest::typed::Shape<_forest::ParseForest<'i, _G, I>, Node<'i, _G>>,
235+
T::Output: fmt::Debug,
232236
{
233237
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
234238
write!(f, "{:?}", self.source_info())?;
@@ -251,17 +255,18 @@ where
251255
impl<'a, 'i, I, T> Handle<'a, 'i, I, T>
252256
where
253257
I: gll::grammer::input::Input,
254-
T: traverse::FromShape<&'a gll::grammer::forest::ParseForest<'i, _G, I>, Node<'i, _G>>,
258+
T: _forest::typed::Shaped
259+
+ _forest::typed::FromShapeFields<'a, _forest::ParseForest<'i, _G, I>, Node<'i, _G>>,
260+
T::Shape: _forest::typed::Shape<_forest::ParseForest<'i, _G, I>, Node<'i, _G>>,
255261
{
256-
pub fn one(self) -> Result<T, Ambiguity<Self>> {
262+
pub fn one(self) -> Result<T::Output, Ambiguity<Self>> {
257263
T::one(self.forest, self.forest.unpack_alias(self.node))
258-
.map_err(|gll::grammer::forest::MoreThanOne| Ambiguity(self))
264+
.map_err(|_forest::MoreThanOne| Ambiguity(self))
259265
}
260266

261267
pub fn all(
262268
self,
263-
) -> traverse::FromShapeAll<T, &'a gll::grammer::forest::ParseForest<'i, _G, I>, Node<'i, _G>>
264-
{
269+
) -> _forest::typed::ShapedAllIter<'a, T, _forest::ParseForest<'i, _G, I>, Node<'i, _G>> {
265270
T::all(self.forest, self.forest.unpack_alias(self.node))
266271
}
267272
}

src/generate/templates/imports.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
use gll::grammer::forest::{GrammarReflector as _, Node, NodeShape};
2-
use gll::runtime::{
3-
cursor::{self, Cursor as _},
4-
traverse::{self, Shape as _},
5-
};
1+
use gll::grammer::forest::{self as _forest, GrammarReflector as _, Node, NodeShape};
62
use std::any;
73
use std::fmt;
84
use std::marker::PhantomData;

0 commit comments

Comments
 (0)