-
Notifications
You must be signed in to change notification settings - Fork 10
/
tavor.go
1941 lines (1533 loc) · 45.6 KB
/
tavor.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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package parser
import (
"bytes"
"fmt"
"io"
"os"
"reflect"
"strconv"
"text/scanner"
"github.com/zimmski/container/list/linkedlist"
"github.com/zimmski/tavor"
"github.com/zimmski/tavor/log"
"github.com/zimmski/tavor/token"
"github.com/zimmski/tavor/token/aggregates"
"github.com/zimmski/tavor/token/conditions"
"github.com/zimmski/tavor/token/constraints"
"github.com/zimmski/tavor/token/expressions"
"github.com/zimmski/tavor/token/lists"
"github.com/zimmski/tavor/token/primitives"
"github.com/zimmski/tavor/token/sequences"
"github.com/zimmski/tavor/token/variables"
)
const zeroRune = 0
type tokenUsage struct {
token token.Token
position scanner.Position
variableScope *token.VariableScope
definitionName string
}
type attributeForwardUsage struct {
definitionName string
tokenName string
tokenPosition scanner.Position
attribute string
attributePosition scanner.Position
operator string
operatorToken token.Token
pointer *primitives.Pointer
variableScope *token.VariableScope
}
type call struct {
from string
variableScope *token.VariableScope
}
type tavorParser struct {
scan scanner.Scanner
err string
earlyUse map[string][]tokenUsage
lookup map[string]tokenUsage
lookupUsage map[token.Token]struct{}
used map[string][]tokenUsage
variableUsages []token.Token
called map[string][]call
forwardAttributeUsage []attributeForwardUsage
}
func (p *tavorParser) expectRune(expect rune, got rune) (rune, error) {
if got != expect {
return got, &token.ParserError{
Message: fmt.Sprintf("expected %s but got %s", scanner.TokenString(expect), scanner.TokenString(got)),
Type: token.ParseErrorExpectRune,
Position: p.scan.Pos(),
}
}
return got, nil
}
func (p *tavorParser) expectScanRune(expect rune) (rune, error) {
got := p.scan.Scan()
log.Debugf("%d:%v -> %v", p.scan.Line, scanner.TokenString(got), p.scan.TokenText())
return p.expectRune(expect, got)
}
func (p *tavorParser) expectText(expect string, got rune) (rune, error) {
got, err := p.expectRune(scanner.Ident, got)
if err != nil {
return zeroRune, err
}
if g := p.scan.TokenText(); g != expect {
return zeroRune, &token.ParserError{
Message: fmt.Sprintf("expected %q got %q", expect, g),
Type: token.ParseErrorExpectOperator,
Position: p.scan.Pos(),
}
}
return got, nil
}
func (p *tavorParser) expectScanText(expect string) (rune, error) {
got, err := p.expectScanRune(scanner.Ident)
if err != nil {
return zeroRune, err
}
if g := p.scan.TokenText(); g != expect {
return zeroRune, &token.ParserError{
Message: fmt.Sprintf("expected %q got %q", expect, g),
Type: token.ParseErrorExpectOperator,
Position: p.scan.Pos(),
}
}
return got, nil
}
func (p *tavorParser) parseGlobalScope(variableScope *token.VariableScope) error {
var err error
c := p.scan.Scan()
log.Debugf("%d:%v -> %v", p.scan.Line, scanner.TokenString(c), p.scan.TokenText())
for c != scanner.EOF {
switch c {
case '\n':
// ignore new lines in the global scope
case scanner.Ident:
c, err = p.parseTokenDefinition(variableScope)
if err != nil {
return err
}
continue
case '$':
c, err = p.parseTypedTokenDefinition(variableScope)
if err != nil {
return err
}
continue
default:
return &token.ParserError{
Message: fmt.Sprintf("token names have to start with a letter and not with %s", scanner.TokenString(c)),
Type: token.ParseErrorInvalidTokenName,
Position: p.scan.Pos(),
}
}
c = p.scan.Scan()
log.Debugf("%d:%v -> %v", p.scan.Line, scanner.TokenString(c), p.scan.TokenText())
}
return nil
}
func (p *tavorParser) getToken(definitionName string, name string, variableScope *token.VariableScope) token.Token {
if tok := variableScope.Get(name); tok != nil {
if v, ok := tok.(token.VariableToken); ok {
tok = variables.NewVariableValue(v)
p.variableUsages = append(p.variableUsages, v)
log.Debugf("use variable value (%p)%#v", tok, tok)
} else {
log.Debugf("use token (%p)%#v", tok, tok)
}
return tok
}
_, ok := p.lookup[name]
if !ok {
log.Debugf("getToken use empty pointer for %s", name)
var tokenInterface *token.Token
b := primitives.NewEmptyPointer(tokenInterface)
n := primitives.NewPointer(b)
p.lookup[name] = tokenUsage{
token: n,
position: p.scan.Position,
}
p.earlyUse[name] = append(p.earlyUse[name], tokenUsage{
token: b,
position: p.scan.Position,
variableScope: variableScope,
definitionName: definitionName,
})
}
p.used[name] = append(p.used[name], tokenUsage{
token: nil,
position: p.scan.Position,
variableScope: variableScope,
})
/*
THIS IS A TERRIBLE HACK THIS SHOULD BE -REMOVED- fixed ASAP
but let me explain
B = 1 | 2
A = B B
This should result in 4 permutations 11 21 12 and 22
but without this condition this would result in only
11s and 22s. The clone alone would be fine but this
leads to more problems if the clone is not saved back
into the lookup.
For example
Bs = +(1)
A = $Bs.Count Bs
would not work without saving back.
But what if somebody writes
Cs = +(1)
B = $Cs.Count Cs
A = +(B)
or even
Cs = +(1)
B = $Cs.Count Cs
A = $Cs.Count +(B)
So TODO and FIXME all over this
*/
tok := p.lookup[name].token
if _, ok := p.lookupUsage[tok]; ok {
if t, ok := tok.(*primitives.Pointer); ok && t.Get() == nil {
// FIXME if tok is directly given to NewPointer we get a panic: reflect: non-interface type passed to Type.Implements
var tokInterface *token.Token
ntok := primitives.NewEmptyPointer(tokInterface)
_ = ntok.Set(tok)
log.Debugf("token %s (%p)%#v is an empty pointer, better just forward to it (%p)%#v", name, tok, tok, ntok, ntok)
tok = ntok
} else {
ntok := tok.Clone()
log.Debugf("token %s (%p)%#v was already used once. Cloned as (%p)%#v", name, tok, tok, ntok, ntok)
p.lookup[name] = tokenUsage{
token: ntok,
position: p.scan.Position,
}
if t, ok := tok.(*primitives.Pointer); ok && t.Resolve() == nil {
p.earlyUse[name] = append(p.earlyUse[name], tokenUsage{
token: ntok,
position: p.scan.Position,
variableScope: variableScope,
definitionName: definitionName,
})
}
tok = ntok
}
} else {
log.Debugf("use token (%p)%#v", tok, tok)
}
p.lookupUsage[tok] = struct{}{}
p.addCall(definitionName, variableScope, name)
return tok
}
func (p *tavorParser) addCall(definitionName string, variableScope *token.VariableScope, name string) {
if _, ok := p.called[name]; !ok {
p.called[name] = make([]call, 0, 1)
}
c := call{
from: definitionName,
variableScope: variableScope,
}
p.called[name] = append(p.called[name], c)
}
func (p *tavorParser) parseTerm(definitionName string, c rune, variableScope *token.VariableScope) (rune, []token.Token, error) {
var err error
var tokens []token.Token
addToken := func(tok token.Token) {
tokens = append(tokens, tok)
}
OUT:
for {
switch c {
case scanner.Ident:
name := p.scan.TokenText()
variableScope = variableScope.Push()
tok := p.getToken(definitionName, name, variableScope)
addToken(tok)
case scanner.Int:
v, _ := strconv.Atoi(p.scan.TokenText())
addToken(primitives.NewConstantInt(v))
case scanner.String:
s := p.scan.TokenText()
if s[len(s)-1] != '"' {
return zeroRune, nil, &token.ParserError{
Message: "string is not terminated",
Type: token.ParseErrorNonTerminatedString,
Position: p.scan.Pos(),
}
}
s, _ = strconv.Unquote(s)
if len(s) == 0 {
return zeroRune, nil, &token.ParserError{
Message: "empty strings are not allowed",
Type: token.ParseErrorEmptyString,
Position: p.scan.Pos(),
}
}
addToken(primitives.NewConstantString(s))
case '(':
log.Debug("Group:")
log.IncreaseIndentation()
c = p.scan.Scan()
log.Debugf("parseTerm Group %d:%v -> %v", p.scan.Line, scanner.TokenString(c), p.scan.TokenText())
c, toks, err := p.parseScope(definitionName, c, variableScope)
if err != nil {
return zeroRune, nil, err
}
_, err = p.expectRune(')', c)
if err != nil {
return zeroRune, nil, err
}
switch len(toks) {
case 0:
// ignore
case 1:
addToken(toks[0])
default:
addToken(lists.NewConcatenation(toks...))
}
log.DecreaseIndentation()
case '?':
log.Debug("Optional:")
log.IncreaseIndentation()
_, err = p.expectScanRune('(')
if err != nil {
return zeroRune, nil, err
}
c = p.scan.Scan()
log.Debugf("parseTerm optional after ( %d:%v -> %v", p.scan.Line, scanner.TokenString(c), p.scan.TokenText())
c, toks, err := p.parseScope(definitionName, c, variableScope)
if err != nil {
return zeroRune, nil, err
}
_, err = p.expectRune(')', c)
if err != nil {
return zeroRune, nil, err
}
switch len(toks) {
case 0:
// ignore
case 1:
addToken(constraints.NewOptional(toks[0]))
default:
addToken(constraints.NewOptional(lists.NewConcatenation(toks...)))
}
log.DecreaseIndentation()
case '+', '*':
log.Debug("Repeat:")
log.IncreaseIndentation()
sym := c
c = p.scan.Scan()
log.Debugf("parseTerm repeat before ( %d:%v -> %v", p.scan.Line, scanner.TokenString(c), p.scan.TokenText())
var from, to token.Token
if sym == '*' {
from, to = primitives.NewConstantInt(0), primitives.NewConstantInt(tavor.MaxRepeat)
} else {
if c == scanner.Int {
iFrom, _ := strconv.Atoi(p.scan.TokenText())
from = primitives.NewConstantInt(iFrom)
c = p.scan.Scan()
log.Debugf("parseTerm repeat after from ( %d:%v -> %v", p.scan.Line, scanner.TokenString(c), p.scan.TokenText())
// until there is an explicit "to" we can assume to==from
to = from // do not clone here! since really to==from
} else if c == '$' {
c = p.scan.Scan()
c, from, err = p.parseTokenAttribute(definitionName, c, variableScope)
if err != nil {
return zeroRune, nil, err
}
// until there is an explicit "to" we can assume to==from
to = from // do not clone here! since really to==from
} else {
from, to = primitives.NewConstantInt(1), primitives.NewConstantInt(tavor.MaxRepeat)
}
if c == ',' {
c = p.scan.Scan()
log.Debugf("parseTerm repeat after , ( %d:%v -> %v", p.scan.Line, scanner.TokenString(c), p.scan.TokenText())
if c == scanner.Int {
iTo, _ := strconv.Atoi(p.scan.TokenText())
to = primitives.NewConstantInt(iTo)
c = p.scan.Scan()
log.Debugf("parseTerm repeat after to ( %d:%v -> %v", p.scan.Line, scanner.TokenString(c), p.scan.TokenText())
} else if c == '$' {
c = p.scan.Scan()
c, to, err = p.parseTokenAttribute(definitionName, c, variableScope)
if err != nil {
return zeroRune, nil, err
}
} else {
to = primitives.NewConstantInt(tavor.MaxRepeat)
}
}
}
_, err = p.expectRune('(', c)
if err != nil {
return zeroRune, nil, err
}
log.Debugf("repeat from %v to %v", from, to)
c = p.scan.Scan()
log.Debugf("parseTerm repeat after ( %d:%v -> %v", p.scan.Line, scanner.TokenString(c), p.scan.TokenText())
c, toks, err := p.parseScope(definitionName, c, variableScope)
if err != nil {
return zeroRune, nil, err
}
_, err = p.expectRune(')', c)
if err != nil {
return zeroRune, nil, err
}
switch len(toks) {
case 0:
// ignore
case 1:
switch t := toks[0].(type) {
case *constraints.Optional:
return zeroRune, nil, &token.ParserError{
Message: "repeats with an optional are not allowed",
Type: token.ParseErrorRepeatWithOptionalTerm,
Position: p.scan.Pos(),
}
case *lists.One:
for i := t.InternalLen() - 1; i >= 0; i-- {
v, _ := t.InternalGet(i)
if _, ok := v.(*constraints.Optional); ok {
return zeroRune, nil, &token.ParserError{
Message: "repeats with an optional are not allowed",
Type: token.ParseErrorRepeatWithOptionalTerm,
Position: p.scan.Pos(),
}
}
}
}
addToken(lists.NewRepeatWithTokens(toks[0], from, to))
default:
addToken(lists.NewRepeatWithTokens(lists.NewConcatenation(toks...), from, to))
}
log.DecreaseIndentation()
case '@':
log.Debug("Once")
log.IncreaseIndentation()
_, err = p.expectScanRune('(')
if err != nil {
return zeroRune, nil, err
}
c = p.scan.Scan()
log.Debugf("parseTerm once after ( %d:%v -> %v", p.scan.Line, scanner.TokenString(c), p.scan.TokenText())
c, toks, err := p.parseScope(definitionName, c, variableScope)
if err != nil {
return zeroRune, nil, err
}
_, err = p.expectRune(')', c)
if err != nil {
return zeroRune, nil, err
}
switch len(toks) {
case 1:
if t, ok := toks[0].(*lists.One); ok {
le := t.InternalLen()
tl := make([]token.Token, le)
for i := 0; i < le; i++ {
tl[i], _ = t.InternalGet(i)
}
addToken(lists.NewOnce(tl...))
} else {
addToken(lists.NewOnce(toks[0]))
}
default:
addToken(lists.NewOnce(toks...))
}
log.DecreaseIndentation()
case '$':
c = p.scan.Scan()
log.Debugf("parseTerm after $ ( %d:%v -> %v", p.scan.Line, scanner.TokenString(c), p.scan.TokenText())
var tok token.Token
if c == '{' {
c, tok, err = p.parseExpression(definitionName, variableScope)
if err != nil {
return zeroRune, nil, err
}
_, err = p.expectRune('}', c)
if err != nil {
return zeroRune, nil, err
}
c = p.scan.Scan()
} else {
c, tok, err = p.parseTokenAttribute(definitionName, c, variableScope)
}
if err != nil {
return zeroRune, nil, err
}
addToken(tok)
continue
case '[':
log.Debug("Character class:")
log.IncreaseIndentation()
var pattern bytes.Buffer
p.scan.Whitespace ^= 1 << ' '
c = p.scan.Scan()
for c != ']' && c != '\n' && c != scanner.EOF {
if _, err := pattern.WriteString(p.scan.TokenText()); err != nil {
panic(err)
}
c = p.scan.Scan()
}
p.scan.Whitespace |= 1 << ' '
if c != ']' {
_, err := p.expectRune(']', c)
return zeroRune, nil, err
}
addToken(primitives.NewCharacterClass(pattern.String()))
log.DecreaseIndentation()
case '<':
log.Debug("Variable:")
log.IncreaseIndentation()
c = p.scan.Scan()
justSave := false
if c == '=' {
justSave = true
c = p.scan.Scan()
}
_, err = p.expectRune(scanner.Ident, c)
if err != nil {
return zeroRune, nil, err
}
if len(tokens) == 0 {
return zeroRune, nil, &token.ParserError{
Message: "Variable has to be assigned to a token",
Type: token.ParseErrorNoTokenForVariable,
Position: p.scan.Pos(),
}
}
variableName := p.scan.TokenText()
_, err = p.expectScanRune('>')
if err != nil {
return zeroRune, nil, err
}
// TODO do not overwrite Token names... this sould lead to an already defined error, only variables can overwrite each other
var variable token.Token
if justSave {
variable = variables.NewVariableSave(variableName, tokens[len(tokens)-1])
log.Debugf("just-save variable %q as (%p)%#v", variableName, variable, variable)
} else {
variable = variables.NewVariable(variableName, tokens[len(tokens)-1])
log.Debugf("variable %q as (%p)%#v", variableName, variable, variable)
}
tokens[len(tokens)-1] = variable
variableScope.Set(variableName, variable)
p.variableUsages = append(p.variableUsages, variable)
log.DecreaseIndentation()
case ',': // multi line token
if _, err := p.expectScanRune('\n'); err != nil {
return zeroRune, nil, err
}
c = p.scan.Scan()
log.Debugf("parseTerm multiline %d:%v -> %v", p.scan.Line, scanner.TokenString(c), p.scan.TokenText())
if c == '\n' {
return zeroRune, nil, &token.ParserError{
Message: "multi line token definition unexpectedly terminated",
Type: token.ParseErrorUnexpectedTokenDefinitionTermination,
Position: p.scan.Pos(),
}
}
continue
default:
break OUT
}
c = p.scan.Scan()
log.Debugf("parseTerm %d:%v -> %v", p.scan.Line, scanner.TokenString(c), p.scan.TokenText())
}
return c, tokens, nil
}
func (p *tavorParser) parseExpression(definitionName string, variableScope *token.VariableScope) (rune, token.Token, error) {
log.Debug("Expression")
log.IncreaseIndentation()
c := p.scan.Scan()
log.Debugf("parseExpression %d:%v -> %v", p.scan.Line, scanner.TokenString(c), p.scan.TokenText())
c, tok, err := p.parseExpressionTerm(definitionName, c, variableScope)
if err != nil {
return zeroRune, nil, err
} else if tok == nil {
return zeroRune, nil, &token.ParserError{
Message: "empty expressions are not allowed",
Type: token.ParseErrorEmptyExpressionIsInvalid,
Position: p.scan.Pos(), // TODO correct position
}
}
log.DecreaseIndentation()
return c, tok, nil
}
func (p *tavorParser) parseExpressionTerm(definitionName string, c rune, variableScope *token.VariableScope) (rune, token.Token, error) {
var tok token.Token
var err error
// single term
switch c {
case scanner.Ident:
attribute := p.scan.TokenText()
switch attribute {
case "defined":
_, err := p.expectScanRune(scanner.Ident)
if err != nil {
return zeroRune, nil, err
}
name := p.scan.TokenText()
c = p.scan.Scan()
return c, conditions.NewVariableDefined(name, variableScope), nil
case "include":
c, tok, err = p.parseExpressionOperatorInclude()
if err != nil {
return zeroRune, nil, err
}
default:
if p.scan.Peek() == '.' {
c, tok, err = p.parseTokenAttribute(definitionName, c, variableScope)
if err != nil {
return zeroRune, nil, err
}
} else {
name := p.scan.TokenText()
variableScope = variableScope.Push()
tok = p.getToken(definitionName, name, variableScope)
c = p.scan.Scan()
}
}
case scanner.Int:
v, _ := strconv.Atoi(p.scan.TokenText())
tok = primitives.NewConstantInt(v)
c = p.scan.Scan()
}
if tok == nil {
return zeroRune, nil, nil
}
log.Debugf("parseExpressionTerm %d:%v -> %v", p.scan.Line, scanner.TokenString(c), p.scan.TokenText())
// operators
switch c {
case '+', '-', '*', '/':
sym := c
c = p.scan.Scan()
log.Debugf("parseExpressionTerm operator %d:%v -> %v", p.scan.Line, scanner.TokenString(c), p.scan.TokenText())
var t token.Token
c, t, err = p.parseExpressionTerm(definitionName, c, variableScope)
if err != nil {
return zeroRune, nil, err
} else if t == nil {
return zeroRune, nil, &token.ParserError{
Message: "expected another expression term after operator",
Type: token.ParseErrorExpectedExpressionTerm,
Position: p.scan.Pos(),
}
}
switch sym {
case '+':
tok = expressions.NewAddArithmetic(tok, t)
case '-':
tok = expressions.NewSubArithmetic(tok, t)
case '*':
tok = expressions.NewMulArithmetic(tok, t)
case '/':
tok = expressions.NewDivArithmetic(tok, t)
}
case scanner.Ident:
switch op := p.scan.TokenText(); op {
case "path":
c, tok, err = p.parseExpressionOperatorPath(tok, definitionName, variableScope)
if err != nil {
return zeroRune, nil, err
}
default:
return zeroRune, nil, &token.ParserError{
Message: fmt.Sprintf("Operator %q is unknown", op),
Type: token.ParseErrorUnkownOperator,
Position: p.scan.Pos(),
}
}
}
return c, tok, nil
}
func (p *tavorParser) parseExpressionGroup(definitionName string, variableScope *token.VariableScope, max int) ([]token.Token, error) {
_, err := p.expectScanRune('(')
if err != nil {
return nil, err
}
c := p.scan.Scan()
var tok token.Token
var toks []token.Token
for {
c, tok, err = p.parseExpressionTerm(definitionName, c, variableScope) // TODO
if err != nil {
return nil, err
} else if tok == nil {
return nil, &token.ParserError{
Message: "expected a expression",
Type: token.ParseErrorExpectedExpressionTerm,
Position: p.scan.Pos(),
}
}
toks = append(toks, tok)
if (max != -1 && len(toks) >= max) || c != ',' {
break
}
c = p.scan.Scan()
}
_, err = p.expectRune(')', c)
if err != nil {
return nil, err
}
return toks, err
}
func (p *tavorParser) parseExpressionOperatorNotIn(tok *sequences.Sequence, definitionName string, c rune, variableScope *token.VariableScope) (rune, token.Token, error) {
log.Debug("Not in operator:")
log.IncreaseIndentation()
defer log.DecreaseIndentation()
_, err := p.expectText("not", c)
if err != nil {
return zeroRune, nil, err
}
_, err = p.expectScanText("in")
if err != nil {
return zeroRune, nil, err
}
expectToks, err := p.parseExpressionGroup(definitionName, variableScope, -1)
if err != nil {
return zeroRune, nil, err
}
c = p.scan.Scan()
return c, tok.ExistingItem(expectToks), nil
}
var includeCache = map[string]token.Token{}
func (p *tavorParser) parseExpressionOperatorInclude() (c rune, tok token.Token, err error) {
log.Debug("Include operator:")
log.IncreaseIndentation()
defer log.DecreaseIndentation()
_, err = p.expectScanRune(scanner.String)
if err != nil {
return zeroRune, nil, err
}
filepath, err := strconv.Unquote(p.scan.TokenText())
if err != nil {
return zeroRune, nil, err
}
if tok, ok := includeCache[filepath]; ok {
tok = tok.Clone()
c = p.scan.Scan()
return c, tok, nil
}
f, err := os.Open(filepath)
if err != nil {
return zeroRune, nil, fmt.Errorf("cannot open tavor file %q: %v", filepath, err)
}
defer func() {
if e := f.Close(); err == nil {
err = e
}
}()
tok, err = ParseTavor(f)
if err != nil {
return zeroRune, nil, fmt.Errorf("%s: %#v", filepath, err)
}
includeCache[filepath] = tok
c = p.scan.Scan()
return c, tok, nil
}
func (p *tavorParser) parseExpressionOperatorPath(tok token.Token, definitionName string, variableScope *token.VariableScope) (rune, token.Token, error) {
log.Debug("Path operator:")
log.IncreaseIndentation()
defer log.DecreaseIndentation()
listPosition := p.scan.Pos()
/*l, ok := tok.(token.ListToken)
if !ok {
return zeroRune, nil, &token.ParserError{
Message: "expected list token",
Type: token.ParseErrorInvalidTokenType,
Position: p.scan.Pos(),
}
}*/
_, err := p.expectScanText("from")
if err != nil {
return zeroRune, nil, err
}
ts, err := p.parseExpressionGroup(definitionName, variableScope, 1)
if err != nil {
return zeroRune, nil, err
}
from := ts[0]
log.Debugf("path operator from %p(%#v)", from, from)
nVariableScope := variableScope.Push()
nVariableScope.Set("e", variables.NewVariable("e", nil))
_, err = p.expectScanText("over")
if err != nil {
return zeroRune, nil, err
}
ts, err = p.parseExpressionGroup(definitionName, nVariableScope, 1)
if err != nil {
return zeroRune, nil, err
}
over := ts[0]
_, err = p.expectScanText("connect")
if err != nil {
return zeroRune, nil, err
}
_, err = p.expectScanText("by")
if err != nil {
return zeroRune, nil, err
}
connects, err := p.parseExpressionGroup(definitionName, nVariableScope, -1)
if err != nil {
return zeroRune, nil, err
}
_, err = p.expectScanText("without")
if err != nil {
return zeroRune, nil, err
}
withouts, err := p.parseExpressionGroup(definitionName, nVariableScope, -1)
if err != nil {
return zeroRune, nil, err
}
c := p.scan.Scan()