forked from simoneguidi94/gopapageno
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcopp.go
573 lines (449 loc) · 16.3 KB
/
copp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
package gopapageno
import (
"context"
"fmt"
)
// COPParser implements parsing using a simplified approach to C-OPGs.
type COPParser struct {
g *Grammar
concurrency int
reductionStrategy ReductionStrategy
// Pools
pools struct {
stacks []*Pool[stack[*Token]]
nonterminals []*Pool[Token]
stateStacks []*Pool[stack[CyclicAutomataState]]
// These are only used when reducing using a single sweep.
sweepInput *Pool[stack[Token]]
sweepStack *Pool[stack[*Token]]
sweepStateStack *Pool[stack[CyclicAutomataState]]
producedTokensMap []map[*Token]*Token
}
workers []*coppWorker
results []*COPPStack
}
// NewCOPParser allocates all required resources for a COPParser to be usable.
func NewCOPParser(g *Grammar, src []byte, opts *RunOptions) *COPParser {
p := &COPParser{
g: g,
concurrency: opts.Concurrency,
reductionStrategy: opts.ReductionStrategy,
workers: make([]*coppWorker, opts.Concurrency),
results: make([]*COPPStack, opts.Concurrency),
}
srcLen := len(src)
stackPoolBaseSize := stacksCountFactored[*Token](src, opts)
ntPoolBaseSize := srcLen / opts.AvgTokenLength / p.concurrency
// Initialize memory pools for stacks.
p.pools.stacks = make([]*Pool[stack[*Token]], p.concurrency)
// Initialize pools to hold pointers to tokens generated by the reduction steps.
p.pools.nonterminals = make([]*Pool[Token], p.concurrency)
// Initialize memory pools for cyclic states.
p.pools.stateStacks = make([]*Pool[stack[CyclicAutomataState]], p.concurrency)
p.pools.producedTokensMap = make([]map[*Token]*Token, p.concurrency)
stackMult := 1.0
if stackPoolBaseSize == 0 {
stackMult = 1.0 - (0.999 * opts.ParallelFactor)
}
ntMultiplier := 1.0 - (0.6 * opts.ParallelFactor)
stackLen := stackLengthFor[*Token](stackMult)
for thread := 0; thread < p.concurrency; thread++ {
p.pools.stacks[thread] = NewPool(stackPoolBaseSize+1, WithConstructor(newStackFactory[*Token](stackLen)))
p.pools.nonterminals[thread] = NewPool[Token](int(float64(ntPoolBaseSize) * ntMultiplier))
p.pools.stateStacks[thread] = NewPool(stackPoolBaseSize+1, WithConstructor(newStackFactory[CyclicAutomataState](stackLen)))
p.pools.producedTokensMap[thread] = make(map[*Token]*Token, int(float64(ntPoolBaseSize)*ntMultiplier))
}
// If reduction is sweep or mixed, we create another stack and input for the final pass.
if p.concurrency > 1 && (p.reductionStrategy == ReductionSweep || p.reductionStrategy == ReductionMixed) {
inputPoolBaseSize := stacksCount[Token](src, p.concurrency, opts.AvgTokenLength)
p.pools.sweepInput = NewPool(inputPoolBaseSize, WithConstructor(newStack[Token]))
p.pools.sweepStack = NewPool(stackPoolBaseSize+1, WithConstructor(newStackFactory[*Token](stackLen)))
p.pools.sweepStateStack = NewPool(stackPoolBaseSize+1, WithConstructor(newStackFactory[CyclicAutomataState](stackLen)))
}
for thread := 0; thread < p.concurrency; thread++ {
p.workers[thread] = &coppWorker{
parser: p,
id: thread,
ntPool: p.pools.nonterminals[thread],
}
}
return p
}
// Parse performs C-OPG parsing of the provided tokensLists, returning the root of the resulting parse tree.
func (p *COPParser) Parse(ctx context.Context, tokensLists []*LOS[Token]) (*Token, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
p.concurrency = len(tokensLists)
resultCh := make(chan parseResult[COPPStack])
errCh := make(chan error, 1)
// First parallel pass of the algorithm.
for thread := 0; thread < p.concurrency; thread++ {
var nextToken *Token
// If the thread is not the last, take the first token of the next stack as lookahead.
if thread < p.concurrency-1 {
nextInputListIter := tokensLists[thread+1].HeadIterator()
nextToken = nextInputListIter.Next()
}
s := NewCOPPStack(p.pools.stacks[thread], p.pools.stateStacks[thread], p.pools.producedTokensMap[thread])
go p.workers[thread].parse(ctx, s, tokensLists[thread], nextToken, false, resultCh, errCh)
}
if err := collectResults[COPPStack](p.results, resultCh, errCh, p.concurrency); err != nil {
return nil, err
}
//If the number of threads is greater than one, results must be combined and work should continue.
reductionPasses := 0
// Reduction phase
for p.concurrency--; p.concurrency >= 1; p.concurrency-- {
// This branch performs a final sweep, it's taken either if ReductionSweep has been selected as a strategy
// and if ReductionMixed has already performed the maximum number of parallel passes.
if p.reductionStrategy == ReductionSweep || (p.reductionStrategy == ReductionMixed && reductionPasses >= 2) {
// Nullifies the previous p.Concurrency-- (Concurrency is used by CombineSweepLOS)
p.concurrency++
// Create the final input by joining together the stacks from the previous step.
stack := p.results[0].Combine()
input, producedTokens := p.CombineSweepLOS(p.pools.sweepInput, p.results[1:])
// Merge produced tokens maps
// TODO: Find a better place to handle this.
for k, v := range producedTokens {
stack.ProducedTokens[k] = v
}
// Sets correct Concurrency level for final sweep.
p.concurrency = 1
go p.workers[0].parse(ctx, stack, input, nil, true, resultCh, errCh)
if err := collectResults[COPPStack](p.results, resultCh, errCh, 1); err != nil {
cancel()
return nil, err
}
} else {
// This branch performs parallel reductions.
for i := 0; i < p.concurrency; i++ {
stackLeft := p.results[i]
stackRight := p.results[i+1]
stack := stackLeft.Combine()
// TODO: I should find a way to make this work without creating a new LOS for the inputs.
// Unfortunately the new stack depends on the content of tokensLists[i] since its elements are stored there.
// We can't erase the old input easily to reuse its storage.
input, producedTokens := stackRight.CombineLOS(tokensLists[i].pool)
// Merge produced tokens maps
// TODO: Find a better place to handle this.
for k, v := range producedTokens {
stack.ProducedTokens[k] = v
}
go p.workers[i].parse(ctx, stack, input, nil, true, resultCh, errCh)
}
if err := collectResults[COPPStack](p.results, resultCh, errCh, p.concurrency); err != nil {
cancel()
return nil, err
}
reductionPasses++
}
}
root, err := p.results[0].LastNonterminal()
if err != nil {
return nil, err
}
return root, nil
}
type coppWorker struct {
parser *COPParser
id int
ntPool *Pool[Token]
}
// parseCyclic implements COPP.
func (w *coppWorker) parse(ctx context.Context, stack *COPPStack, tokens *LOS[Token], nextToken *Token, finalPass bool, resultCh chan<- parseResult[COPPStack], errCh chan<- error) {
tokensIt := tokens.HeadIterator()
rhs := make([]TokenType, 0, w.parser.g.MaxPrefixLength)
rhsTokens := make([]*Token, 0, w.parser.g.MaxPrefixLength)
// If the thread is the first, push a # onto the stack
// Otherwise, push the first inputToken onto the stack
if !finalPass {
if w.id == 0 {
stack.Push(&Token{
Type: TokenTerm,
Precedence: PrecEmpty,
})
} else {
t := tokensIt.Next()
t.Precedence = PrecEmpty
stack.Push(t)
}
// If the thread is the last, push a # onto the tokensList.
// Otherwise, push the lookahead token.
if w.id == w.parser.concurrency-1 {
tokens.Push(Token{
Type: TokenTerm,
Precedence: PrecEmpty,
})
} else if nextToken != nil {
tokens.Push(*nextToken)
}
}
var prec Precedence
// prefixCount is used to identify where to cut double occurrences of repeated prefixes.
var prefixCount int
for inputToken := tokensIt.Next(); inputToken != nil; {
// Find the first terminal on the stack and get the precedence between it and the current token
firstTerminal := stack.FirstTerminal()
if !inputToken.Type.IsTerminal() {
prec = PrecYields
} else {
// TODO: Consider removing this check, it is probably unnecessary.
if firstTerminal == nil {
prec = w.parser.g.precedence(TokenTerm, inputToken.Type)
} else {
prec = w.parser.g.precedence(firstTerminal.Type, inputToken.Type)
if prec == PrecEquals && firstTerminal.Precedence == PrecTakes {
prec = PrecYields
}
// This is required to put aside tokens that require the previous one to be matched within a rhs.
// Since the previous one has Empty precedence, it means that it's unusable.
if prec == PrecEquals && firstTerminal.Precedence == PrecEmpty {
prec = PrecTakes
}
}
}
// If it yields precedence, PUSH the inputToken onto the stack.
if prec == PrecYields {
inputToken.Precedence = prec
if inputToken.Type.IsTerminal() {
stack.Push(inputToken)
}
// If the current construction is a single nonterminal, append the token to it.
// Otherwise, swap.
if stack.IsCurrentSingleNonterminal() {
stack.AppendStateToken(inputToken)
} else {
stack.SwapState()
stack.AppendStateToken(inputToken)
}
rhsTokens = rhsTokens[:0]
rhs = rhs[:0]
prefixCount = 0
inputToken = tokensIt.Next()
} else if prec == PrecEquals {
inputToken.Precedence = prec
// If the current construction is a single nonterminal, prepend the previous construction to it.
if stack.IsCurrentSingleNonterminal() {
stack.State.CurrentIndex = stack.State.PreviousIndex
stack.State.CurrentLen += stack.State.PreviousLen
}
if len(rhsTokens) == 0 {
rhsTokens = append(rhsTokens, stack.Current()...)
for i := range stack.State.CurrentLen {
rhs = append(rhs, rhsTokens[i].Type)
}
}
rhsTokens = append(rhsTokens, inputToken)
rhs = append(rhs, inputToken.Type)
// Try to identify if the current construction matches a prefix.
lhs, ruleNum := w.parser.g.findPrefixMatch(rhs)
if lhs == TokenEmpty {
prefixCount++
stack.AppendStateToken(inputToken)
// Replace the topmost token on the stack, keeping its state unchanged.
_, s := stack.Pop2()
stack.PushWithState(inputToken, *s)
inputToken = tokensIt.Next()
continue
}
lhsToken, err := w.matchPrefix(lhs, ruleNum, rhsTokens[:len(rhsTokens)-prefixCount-1], stack)
if err != nil {
errCh <- fmt.Errorf("worker %d could not match: %v", w.id, err)
return
}
// Reset state
stack.StateTokenStack.Tos = stack.State.CurrentIndex
stack.State.CurrentLen = 0
stack.AppendStateToken(lhsToken)
for i := len(rhsTokens) - prefixCount - 1; i < len(rhsTokens); i++ {
stack.AppendStateToken(rhsTokens[i])
}
prefixCount = 0
// Replace the topmost token on the stack, keeping its state unchanged.
// TODO: Consider adding a Shift method to stack.
_, s := stack.Pop2()
stack.PushWithState(inputToken, *s)
inputToken = tokensIt.Next()
} else if prec == PrecTakes {
// If there are no tokens yielding precedence on the stack, push inputToken onto the stack.
// Otherwise, perform a reduction.
if stack.YieldingPrecedence() == 0 {
if firstTerminal != nil && firstTerminal.Precedence != PrecEmpty {
firstTerminal.Precedence = PrecTakes
}
inputToken.Precedence = prec
stack.Push(inputToken)
if inputToken.Type != TokenTerm {
stack.SwapState()
}
inputToken = tokensIt.Next()
} else {
var i int
rhsTokens = rhsTokens[:0]
rhs = rhs[:0]
if stack.IsCurrentSingleNonterminal() {
rhsTokens = append(rhsTokens, stack.Previous()...)
for i = 0; i < stack.State.PreviousLen; i++ {
rhs = append(rhs, rhsTokens[i].Type)
}
}
rhsTokens = append(rhsTokens, stack.Current()...)
for j := 0; j < stack.State.CurrentLen; j++ {
rhs = append(rhs, rhsTokens[i].Type)
i++
}
_, st := stack.Pop2()
stack.UpdateFirstTerminal()
// Prefix is made of a single nonterminal
if st.CurrentLen == 1 && !stack.StateTokenStack.Data[st.CurrentIndex].IsTerminal() {
stack.State.PreviousIndex = st.PreviousIndex
stack.State.PreviousLen = st.PreviousLen
} else {
stack.State.PreviousIndex = st.CurrentIndex
stack.State.PreviousLen = st.CurrentLen
}
lhsToken, err := w.match(rhs, rhsTokens, stack)
if err != nil {
errCh <- fmt.Errorf("worker %d could not match: %v", w.id, err)
return
}
// Reset state
stack.StateTokenStack.Tos = stack.State.PreviousIndex + stack.State.PreviousLen + 1
stack.State.CurrentIndex = stack.StateTokenStack.Tos - 1
stack.State.CurrentLen = 1
stack.StateTokenStack.Replace(lhsToken)
prefixCount = 0
}
rhsTokens = rhsTokens[:0]
rhs = rhs[:0]
} else {
//If there's no precedence relation, abort the parsing
errCh <- fmt.Errorf("no precedence relation found")
return
}
}
resultCh <- parseResult[COPPStack]{w.id, stack}
}
func (w *coppWorker) matchPrefix(lhs TokenType, ruleNum uint16, rhsTokens []*Token, s *COPPStack) (*Token, error) {
var lhsToken *Token
var rf RuleFlags
firstToken := rhsTokens[0]
parentToken, ok := s.ProducedTokens[firstToken]
if !ok {
lhsToken = w.ntPool.Get()
lhsToken.Type = lhs
rf = rf.Set(RuleCyclic)
} else {
lhsToken = parentToken
rf = rf.Set(RuleAppend)
}
rightParent, ok := s.ProducedTokens[rhsTokens[len(rhsTokens)-1]]
if ok {
rhsTokens[len(rhsTokens)-1] = rightParent
rf = rf.Set(RuleCombine)
}
//Execute the semantic action
w.parser.g.Func(ruleNum, rf, lhsToken, rhsTokens, w.id)
s.ProducedTokens[rhsTokens[0]] = lhsToken
return firstToken, nil
}
func (w *coppWorker) match(rhs []TokenType, rhsTokens []*Token, s *COPPStack) (*Token, error) {
lhs, ruleNum := w.parser.g.findRuleMatch(rhs)
if lhs == TokenEmpty {
return nil, fmt.Errorf("could not find match for rhs %v", rhs)
}
var lhsToken *Token
rt := RuleCyclic
parentToken, ok := s.ProducedTokens[rhsTokens[0]]
if !ok {
lhsToken = w.ntPool.Get()
lhsToken.Type = lhs
} else {
lhsToken = parentToken
rt = RuleAppend
}
rightParent, ok := s.ProducedTokens[rhsTokens[len(rhsTokens)-1]]
if ok {
rhsTokens[len(rhsTokens)-1] = rightParent
rt = RuleCombine
}
//Execute the semantic action
w.parser.g.Func(ruleNum, rt, lhsToken, rhsTokens, w.id)
return lhsToken, nil
}
func (p *COPParser) CombineSweepLOS(pool *Pool[stack[Token]], stacks []*COPPStack) (*LOS[Token], map[*Token]*Token) {
input := NewLOS[Token](pool)
newProducedTokens := make(map[*Token]*Token)
tokenSet := make(map[*Token]struct{}, stacks[0].Length())
for i := 0; i < p.concurrency-1; i++ {
it := stacks[i].Iterator()
//Ignore the first token.
t, st := it.Next()
tokenSet[t] = struct{}{}
for _, t := range stacks[i].StateTokenStack.Slice(st.CurrentIndex, st.CurrentLen) {
tokenSet[t] = struct{}{}
}
for t, st = it.Next(); t != nil; t, st = it.Next() {
if t.Precedence == PrecEquals {
if !it.IsLast() {
continue
}
for _, stateToken := range stacks[i].StateTokenStack.Slice(st.CurrentIndex, st.CurrentLen) {
if _, ok := tokenSet[stateToken]; !ok {
stateToken.Precedence = PrecEmpty
newToken := input.Push(*stateToken)
parentToken, ok := stacks[i].ProducedTokens[stateToken]
if ok {
newProducedTokens[newToken] = parentToken
}
tokenSet[stateToken] = struct{}{}
}
}
for _, stateToken := range stacks[i].Previous() {
if _, ok := tokenSet[stateToken]; !ok {
stateToken.Precedence = PrecEmpty
newToken := input.Push(*stateToken)
parentToken, ok := stacks[i].ProducedTokens[stateToken]
if ok {
newProducedTokens[newToken] = parentToken
}
tokenSet[stateToken] = struct{}{}
}
}
for _, stateToken := range stacks[i].Current() {
if _, ok := tokenSet[stateToken]; !ok {
stateToken.Precedence = PrecEmpty
newToken := input.Push(*stateToken)
parentToken, ok := stacks[i].ProducedTokens[stateToken]
if ok {
newProducedTokens[newToken] = parentToken
}
tokenSet[stateToken] = struct{}{}
}
}
continue
}
for _, stateToken := range stacks[i].StateTokenStack.Slice(st.CurrentIndex, st.CurrentLen) {
if _, ok := tokenSet[stateToken]; !ok {
stateToken.Precedence = PrecEmpty
newToken := input.Push(*stateToken)
parentToken, ok := stacks[i].ProducedTokens[stateToken]
if ok {
newProducedTokens[newToken] = parentToken
}
tokenSet[stateToken] = struct{}{}
}
}
if _, ok := tokenSet[t]; !ok {
t.Precedence = PrecEmpty
newToken := input.Push(*t)
parentToken, ok := stacks[i].ProducedTokens[t]
if ok {
newProducedTokens[newToken] = parentToken
}
tokenSet[t] = struct{}{}
}
}
}
return input, newProducedTokens
}