Skip to content

Commit 5b28af9

Browse files
authored
Update grammer for Node<'i, P> -> Node<'i, G>. (#135)
Update grammer for Node<'i, P> -> Node<'i, G>.
2 parents a94efa1 + 42960bb commit 5b28af9

File tree

4 files changed

+63
-30
lines changed

4 files changed

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

3434
[workspace]
3535
members = [

src/generate/rust.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,8 +1084,8 @@ where
10841084
#[allow(non_snake_case)]
10851085
fn #variants_from_forest_ident(
10861086
forest: &'a gll::grammer::forest::ParseForest<'i, _G, I>,
1087-
_node: Node<'i, _P>,
1088-
_r: traverse!(typeof(Node<'i, _P>) #variants_shape),
1087+
_node: Node<'i, _G>,
1088+
_r: traverse!(typeof(Node<'i, _G>) #variants_shape),
10891089
) -> Self {
10901090
#variants_body
10911091
}
@@ -1103,8 +1103,8 @@ where
11031103
quote!(
11041104
fn from_forest(
11051105
forest: &'a gll::grammer::forest::ParseForest<'i, _G, I>,
1106-
_node: Node<'i, _P>,
1107-
_r: traverse!(typeof(Node<'i, _P>) #shape),
1106+
_node: Node<'i, _G>,
1107+
_r: traverse!(typeof(Node<'i, _G>) #shape),
11081108
) -> Self {
11091109
#ident {
11101110
#(#fields_ident: #fields_expr),*
@@ -1538,7 +1538,6 @@ fn code_label_decl_and_impls<Pat>(
15381538
}
15391539
impl gll::runtime::CodeLabel for _C {
15401540
type GrammarReflector = _G;
1541-
type NodeKind = _P;
15421541

15431542
fn enclosing_fn(self) -> Self {
15441543
match self {

src/generate/templates/header.rs

Lines changed: 2 additions & 2 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, _P, I>,
7+
forest_and_node: gll::grammer::forest::OwnedParseForestAndNode<_G, I>,
88
_marker: PhantomData<T>,
99
}
1010

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

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

src/runtime.rs

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub struct Runtime<'a, 'i, C: CodeLabel, I: Input, Pat> {
1111
parser: Parser<'a, 'i, C::GrammarReflector, I, Pat>,
1212
state: &'a mut RuntimeState<'i, C>,
1313
current: C,
14-
saved: Option<Node<'i, C::NodeKind>>,
14+
saved: Option<Node<'i, C::GrammarReflector>>,
1515
}
1616

1717
struct RuntimeState<'i, C: CodeLabel> {
@@ -20,20 +20,18 @@ struct RuntimeState<'i, C: CodeLabel> {
2020
memoizer: Memoizer<'i, C>,
2121
}
2222

23-
impl<'i, P, G, C, I: Input, Pat: Ord> Runtime<'_, 'i, C, I, Pat>
23+
impl<'i, G, C, I: Input, Pat: Ord> Runtime<'_, 'i, C, I, Pat>
2424
where
25-
// FIXME(eddyb) these shouldn't be needed, as they are bounds on
26-
// `GrammarReflector::NodeKind`, but that's ignored currently.
27-
P: fmt::Debug + Ord + Hash + Copy,
28-
G: GrammarReflector<NodeKind = P>,
29-
C: CodeStep<I, Pat, GrammarReflector = G, NodeKind = P>,
25+
G: GrammarReflector,
26+
G::NodeKind: Ord,
27+
C: CodeStep<I, Pat, GrammarReflector = G>,
3028
{
3129
pub fn parse(
3230
grammar: G,
3331
input: I,
3432
callee: C,
35-
kind: P,
36-
) -> ParseResult<I::SourceInfoPoint, Pat, OwnedParseForestAndNode<G, P, I>> {
33+
kind: G::NodeKind,
34+
) -> ParseResult<I::SourceInfoPoint, Pat, OwnedParseForestAndNode<G, I>> {
3735
Parser::parse_with(grammar, input, |mut parser| {
3836
let call = Call {
3937
callee,
@@ -128,25 +126,25 @@ where
128126
}
129127

130128
// FIXME(eddyb) maybe specialize this further, for `forest_add_split`?
131-
pub fn save(&mut self, kind: P) {
129+
pub fn save(&mut self, kind: G::NodeKind) {
132130
let old_saved = self.saved.replace(Node {
133131
kind,
134132
range: self.parser.take_result(),
135133
});
136134
assert_eq!(old_saved, None);
137135
}
138136

139-
pub fn take_saved(&mut self) -> Node<'i, P> {
137+
pub fn take_saved(&mut self) -> Node<'i, G> {
140138
self.saved.take().unwrap()
141139
}
142140

143141
// FIXME(eddyb) safeguard this against misuse.
144-
pub fn forest_add_choice(&mut self, kind: P, choice: usize) {
142+
pub fn forest_add_choice(&mut self, kind: G::NodeKind, choice: usize) {
145143
self.parser.forest_add_choice(kind, choice);
146144
}
147145

148146
// FIXME(eddyb) safeguard this against misuse.
149-
pub fn forest_add_split(&mut self, kind: P, left: Node<'i, P>) {
147+
pub fn forest_add_split(&mut self, kind: G::NodeKind, left: Node<'i, G>) {
150148
self.parser.forest_add_split(kind, left);
151149
}
152150

@@ -234,7 +232,10 @@ struct Threads<'i, C: CodeLabel> {
234232
seen: BTreeSet<Call<'i, Continuation<'i, C>>>,
235233
}
236234

237-
impl<'i, C: CodeLabel> Threads<'i, C> {
235+
impl<'i, C: CodeLabel> Threads<'i, C>
236+
where
237+
<C::GrammarReflector as GrammarReflector>::NodeKind: Ord,
238+
{
238239
fn spawn(&mut self, next: Continuation<'i, C>, range: Range<'i>) {
239240
let t = Call {
240241
callee: next,
@@ -266,17 +267,47 @@ impl<'i, C: CodeLabel> Threads<'i, C> {
266267
}
267268
}
268269

269-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
270270
struct Continuation<'i, C: CodeLabel> {
271271
code: C,
272-
saved: Option<Node<'i, C::NodeKind>>,
272+
saved: Option<Node<'i, C::GrammarReflector>>,
273273
// FIXME(eddyb) for GC purposes, this would also need to be a `Node`,
274274
// except that's not always the case? But `Node | Range` seems likely
275275
// to be a deoptimization, especially if `Node` stops containing a
276276
// `Range` (e.g. if it's an index in a node array).
277277
result: Range<'i>,
278278
}
279279

280+
// FIXME(eddyb) can't derive these on `Continuation<C>` because that puts
281+
// bounds on `C` (and worse, `C::GrammarReflector`).
282+
impl<C: CodeLabel> Copy for Continuation<'_, C> {}
283+
impl<C: CodeLabel> Clone for Continuation<'_, C> {
284+
fn clone(&self) -> Self {
285+
*self
286+
}
287+
}
288+
impl<C: CodeLabel> PartialEq for Continuation<'_, C> {
289+
fn eq(&self, other: &Self) -> bool {
290+
(self.code, self.saved, self.result) == (other.code, other.saved, other.result)
291+
}
292+
}
293+
impl<C: CodeLabel> Eq for Continuation<'_, C> {}
294+
impl<C: CodeLabel> PartialOrd for Continuation<'_, C>
295+
where
296+
<C::GrammarReflector as GrammarReflector>::NodeKind: Ord,
297+
{
298+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
299+
(self.code, self.saved, self.result).partial_cmp(&(other.code, other.saved, other.result))
300+
}
301+
}
302+
impl<C: CodeLabel> Ord for Continuation<'_, C>
303+
where
304+
<C::GrammarReflector as GrammarReflector>::NodeKind: Ord,
305+
{
306+
fn cmp(&self, other: &Self) -> Ordering {
307+
(self.code, self.saved, self.result).cmp(&(other.code, other.saved, other.result))
308+
}
309+
}
310+
280311
// TODO(eddyb) figure out if `Call<Continuation<C>>` can be optimized,
281312
// based on the fact that `result.end == range.start` should always hold.
282313
// (Also, `range.end` is constant across a whole parse)
@@ -314,7 +345,10 @@ struct GraphStack<'i, C: CodeLabel> {
314345
returns: HashMap<Call<'i, C>, BTreeSet<Continuation<'i, C>>>,
315346
}
316347

317-
impl<C: CodeLabel> GraphStack<'_, C> {
348+
impl<C: CodeLabel> GraphStack<'_, C>
349+
where
350+
<C::GrammarReflector as GrammarReflector>::NodeKind: Ord,
351+
{
318352
// FIXME(eddyb) figure out what to do here, now that
319353
// the GSS is no longer exposed in the public API.
320354
#[allow(unused)]
@@ -343,7 +377,10 @@ struct Memoizer<'i, C: CodeLabel> {
343377
lengths: HashMap<Call<'i, C>, BTreeSet<usize>>,
344378
}
345379

346-
impl<'i, C: CodeLabel> Memoizer<'i, C> {
380+
impl<'i, C: CodeLabel> Memoizer<'i, C>
381+
where
382+
<C::GrammarReflector as GrammarReflector>::NodeKind: Ord,
383+
{
347384
fn results<'a>(&'a self, call: Call<'i, C>) -> impl DoubleEndedIterator<Item = Range<'i>> + 'a {
348385
self.lengths
349386
.get(&call)
@@ -360,10 +397,7 @@ impl<'i, C: CodeLabel> Memoizer<'i, C> {
360397
}
361398

362399
pub trait CodeLabel: fmt::Debug + Ord + Hash + Copy + 'static {
363-
// HACK(eddyb) this allows using `C::NodeKind` in structs without
364-
// autoderiving adding spurious bounds on `C::GrammarReflector`.
365-
type GrammarReflector: GrammarReflector<NodeKind = Self::NodeKind>;
366-
type NodeKind: fmt::Debug + Ord + Hash + Copy;
400+
type GrammarReflector: GrammarReflector;
367401

368402
fn enclosing_fn(self) -> Self;
369403
}

0 commit comments

Comments
 (0)