@@ -11,7 +11,7 @@ pub struct Runtime<'a, 'i, C: CodeLabel, I: Input, Pat> {
11
11
parser : Parser < ' a , ' i , C :: GrammarReflector , I , Pat > ,
12
12
state : & ' a mut RuntimeState < ' i , C > ,
13
13
current : C ,
14
- saved : Option < Node < ' i , C :: NodeKind > > ,
14
+ saved : Option < Node < ' i , C :: GrammarReflector > > ,
15
15
}
16
16
17
17
struct RuntimeState < ' i , C : CodeLabel > {
@@ -20,20 +20,18 @@ struct RuntimeState<'i, C: CodeLabel> {
20
20
memoizer : Memoizer < ' i , C > ,
21
21
}
22
22
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 >
24
24
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 > ,
30
28
{
31
29
pub fn parse (
32
30
grammar : G ,
33
31
input : I ,
34
32
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 > > {
37
35
Parser :: parse_with ( grammar, input, |mut parser| {
38
36
let call = Call {
39
37
callee,
@@ -128,25 +126,25 @@ where
128
126
}
129
127
130
128
// 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 ) {
132
130
let old_saved = self . saved . replace ( Node {
133
131
kind,
134
132
range : self . parser . take_result ( ) ,
135
133
} ) ;
136
134
assert_eq ! ( old_saved, None ) ;
137
135
}
138
136
139
- pub fn take_saved ( & mut self ) -> Node < ' i , P > {
137
+ pub fn take_saved ( & mut self ) -> Node < ' i , G > {
140
138
self . saved . take ( ) . unwrap ( )
141
139
}
142
140
143
141
// 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 ) {
145
143
self . parser . forest_add_choice ( kind, choice) ;
146
144
}
147
145
148
146
// 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 > ) {
150
148
self . parser . forest_add_split ( kind, left) ;
151
149
}
152
150
@@ -234,7 +232,10 @@ struct Threads<'i, C: CodeLabel> {
234
232
seen : BTreeSet < Call < ' i , Continuation < ' i , C > > > ,
235
233
}
236
234
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
+ {
238
239
fn spawn ( & mut self , next : Continuation < ' i , C > , range : Range < ' i > ) {
239
240
let t = Call {
240
241
callee : next,
@@ -266,17 +267,47 @@ impl<'i, C: CodeLabel> Threads<'i, C> {
266
267
}
267
268
}
268
269
269
- #[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord ) ]
270
270
struct Continuation < ' i , C : CodeLabel > {
271
271
code : C ,
272
- saved : Option < Node < ' i , C :: NodeKind > > ,
272
+ saved : Option < Node < ' i , C :: GrammarReflector > > ,
273
273
// FIXME(eddyb) for GC purposes, this would also need to be a `Node`,
274
274
// except that's not always the case? But `Node | Range` seems likely
275
275
// to be a deoptimization, especially if `Node` stops containing a
276
276
// `Range` (e.g. if it's an index in a node array).
277
277
result : Range < ' i > ,
278
278
}
279
279
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
+
280
311
// TODO(eddyb) figure out if `Call<Continuation<C>>` can be optimized,
281
312
// based on the fact that `result.end == range.start` should always hold.
282
313
// (Also, `range.end` is constant across a whole parse)
@@ -314,7 +345,10 @@ struct GraphStack<'i, C: CodeLabel> {
314
345
returns : HashMap < Call < ' i , C > , BTreeSet < Continuation < ' i , C > > > ,
315
346
}
316
347
317
- impl < C : CodeLabel > GraphStack < ' _ , C > {
348
+ impl < C : CodeLabel > GraphStack < ' _ , C >
349
+ where
350
+ <C :: GrammarReflector as GrammarReflector >:: NodeKind : Ord ,
351
+ {
318
352
// FIXME(eddyb) figure out what to do here, now that
319
353
// the GSS is no longer exposed in the public API.
320
354
#[ allow( unused) ]
@@ -343,7 +377,10 @@ struct Memoizer<'i, C: CodeLabel> {
343
377
lengths : HashMap < Call < ' i , C > , BTreeSet < usize > > ,
344
378
}
345
379
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
+ {
347
384
fn results < ' a > ( & ' a self , call : Call < ' i , C > ) -> impl DoubleEndedIterator < Item = Range < ' i > > + ' a {
348
385
self . lengths
349
386
. get ( & call)
@@ -360,10 +397,7 @@ impl<'i, C: CodeLabel> Memoizer<'i, C> {
360
397
}
361
398
362
399
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 ;
367
401
368
402
fn enclosing_fn ( self ) -> Self ;
369
403
}
0 commit comments