-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathparser.sk
More file actions
1149 lines (1008 loc) · 37.4 KB
/
parser.sk
File metadata and controls
1149 lines (1008 loc) · 37.4 KB
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
namespace GLSLX.Parser {
var pratt Pratt = null
def typeParselet(type Type) fn(ParserContext, Token) Node {
return (context, token) => Node.createType(type).withRange(token.range)
}
def unaryPrefix(kind NodeKind) fn(ParserContext, Token, Node) Node {
assert(kind.isUnaryPrefix)
return (context, token, value) => Node.createUnary(kind, value).withRange(Range.span(token.range, value.range)).withInternalRange(token.range)
}
def unaryPostfix(kind NodeKind) fn(ParserContext, Node, Token) Node {
assert(kind.isUnaryPostfix)
return (context, value, token) => Node.createUnary(kind, value).withRange(Range.span(value.range, token.range)).withInternalRange(token.range)
}
def binaryParselet(kind NodeKind) fn(ParserContext, Node, Token, Node) Node {
assert(kind.isBinary)
return (context, left, token, right) => Node.createBinary(kind, left, right).withRange(Range.span(left.range, right.range)).withInternalRange(token.range)
}
def parseInt(text string) int {
if text.count > 1 && text[0] == '0' && (text[1] != 'x' && text[1] != 'X') {
return dynamic.parseInt(text, 8)
}
return (text as dynamic) | 0
}
def parseFloat(text string) double {
return +(text as dynamic)
}
def createExpressionParser Pratt {
var pratt = Pratt.new
var invalidUnaryOperator = (context ParserContext, token Token, value Node) Node => {
context.log.syntaxErrorInvalidOperator(token.range)
return Node.createUnknownConstant(.ERROR).withRange(Range.span(token.range, value.range))
}
var invalidBinaryOperator = (context ParserContext, left Node, token Token, right Node) Node => {
context.log.syntaxErrorInvalidOperator(token.range)
return Node.createUnknownConstant(.ERROR).withRange(Range.span(left.range, right.range))
}
pratt.literal(.TRUE, (context, token) => Node.createBool(true).withRange(token.range))
pratt.literal(.FALSE, (context, token) => Node.createBool(false).withRange(token.range))
pratt.literal(.INT_LITERAL, (context, token) => Node.createInt(parseInt(token.range.toString)).withRange(token.range))
pratt.literal(.FLOAT_LITERAL, (context, token) => Node.createFloat(parseFloat(token.range.toString)).withRange(token.range))
pratt.literal(.BOOL, typeParselet(.BOOL))
pratt.literal(.BVEC2, typeParselet(.BVEC2))
pratt.literal(.BVEC3, typeParselet(.BVEC3))
pratt.literal(.BVEC4, typeParselet(.BVEC4))
pratt.literal(.FLOAT, typeParselet(.FLOAT))
pratt.literal(.INT, typeParselet(.INT))
pratt.literal(.IVEC2, typeParselet(.IVEC2))
pratt.literal(.IVEC3, typeParselet(.IVEC3))
pratt.literal(.IVEC4, typeParselet(.IVEC4))
pratt.literal(.MAT2, typeParselet(.MAT2))
pratt.literal(.MAT3, typeParselet(.MAT3))
pratt.literal(.MAT4, typeParselet(.MAT4))
pratt.literal(.VEC2, typeParselet(.VEC2))
pratt.literal(.VEC3, typeParselet(.VEC3))
pratt.literal(.VEC4, typeParselet(.VEC4))
pratt.literal(.VOID, typeParselet(.VOID))
pratt.prefix(.COMPLEMENT, .UNARY_PREFIX, invalidUnaryOperator)
pratt.prefix(.DECREMENT, .UNARY_PREFIX, unaryPrefix(.PREFIX_DECREMENT))
pratt.prefix(.INCREMENT, .UNARY_PREFIX, unaryPrefix(.PREFIX_INCREMENT))
pratt.prefix(.MINUS, .UNARY_PREFIX, unaryPrefix(.NEGATIVE))
pratt.prefix(.NOT, .UNARY_PREFIX, unaryPrefix(.NOT))
pratt.prefix(.PLUS, .UNARY_PREFIX, unaryPrefix(.POSITIVE))
pratt.postfix(.DECREMENT, .UNARY_POSTFIX, unaryPostfix(.POSTFIX_DECREMENT))
pratt.postfix(.INCREMENT, .UNARY_POSTFIX, unaryPostfix(.POSTFIX_INCREMENT))
pratt.infix(.DIVIDE, .MULTIPLY, binaryParselet(.DIVIDE))
pratt.infix(.EQUAL, .COMPARE, binaryParselet(.EQUAL))
pratt.infix(.GREATER_THAN, .COMPARE, binaryParselet(.GREATER_THAN))
pratt.infix(.GREATER_THAN_OR_EQUAL, .COMPARE, binaryParselet(.GREATER_THAN_OR_EQUAL))
pratt.infix(.LESS_THAN, .COMPARE, binaryParselet(.LESS_THAN))
pratt.infix(.LESS_THAN_OR_EQUAL, .COMPARE, binaryParselet(.LESS_THAN_OR_EQUAL))
pratt.infix(.MINUS, .ADD, binaryParselet(.SUBTRACT))
pratt.infix(.MULTIPLY, .MULTIPLY, binaryParselet(.MULTIPLY))
pratt.infix(.NOT_EQUAL, .COMPARE, binaryParselet(.NOT_EQUAL))
pratt.infix(.PLUS, .ADD, binaryParselet(.ADD))
pratt.infix(.REMAINDER, .MULTIPLY, invalidBinaryOperator)
pratt.infix(.SHIFT_LEFT, .SHIFT, invalidBinaryOperator)
pratt.infix(.SHIFT_RIGHT, .SHIFT, invalidBinaryOperator)
pratt.infix(.LOGICAL_OR, .LOGICAL_OR, binaryParselet(.LOGICAL_OR))
pratt.infix(.LOGICAL_XOR, .LOGICAL_XOR, binaryParselet(.LOGICAL_XOR))
pratt.infix(.LOGICAL_AND, .LOGICAL_AND, binaryParselet(.LOGICAL_AND))
pratt.infix(.BITWISE_AND, .BITWISE_AND, invalidBinaryOperator)
pratt.infix(.BITWISE_OR, .BITWISE_OR, invalidBinaryOperator)
pratt.infix(.BITWISE_XOR, .BITWISE_XOR, invalidBinaryOperator)
pratt.infixRight(.ASSIGN, .ASSIGN, binaryParselet(.ASSIGN))
pratt.infixRight(.ASSIGN_ADD, .ASSIGN, binaryParselet(.ASSIGN_ADD))
pratt.infixRight(.ASSIGN_BITWISE_AND, .ASSIGN, invalidBinaryOperator)
pratt.infixRight(.ASSIGN_BITWISE_OR, .ASSIGN, invalidBinaryOperator)
pratt.infixRight(.ASSIGN_BITWISE_XOR, .ASSIGN, invalidBinaryOperator)
pratt.infixRight(.ASSIGN_DIVIDE, .ASSIGN, binaryParselet(.ASSIGN_DIVIDE))
pratt.infixRight(.ASSIGN_MULTIPLY, .ASSIGN, binaryParselet(.ASSIGN_MULTIPLY))
pratt.infixRight(.ASSIGN_REMAINDER, .ASSIGN, invalidBinaryOperator)
pratt.infixRight(.ASSIGN_SHIFT_LEFT, .ASSIGN, invalidBinaryOperator)
pratt.infixRight(.ASSIGN_SHIFT_RIGHT, .ASSIGN, invalidBinaryOperator)
pratt.infixRight(.ASSIGN_SUBTRACT, .ASSIGN, binaryParselet(.ASSIGN_SUBTRACT))
# Name
pratt.literal(.IDENTIFIER, (context, token) => {
var name = token.range.toString
var symbol = context.scope.find(name)
if symbol == null {
context.log.syntaxErrorBadSymbolReference(token.range)
return Node.createParseError.withRange(token.range)
}
# Check extension usage
if symbol.requiredExtension != null && context.compilationData.extensionBehavior(symbol.requiredExtension) == .DISABLE {
context.log.syntaxErrorDisabledExtension(token.range, name, symbol.requiredExtension)
}
symbol.useCount++
return (symbol.isStruct ? Node.createType(symbol.resolvedType) : Node.createName(symbol)).withRange(token.range)
})
# Sequence
pratt.infix(.COMMA, .COMMA, (context, left, token, right) => {
if left.kind != .SEQUENCE {
left = Node.createSequence.appendChild(left).withRange(left.range)
}
left.appendChild(right)
return left.withRange(context.spanSince(left.range))
})
# Dot
pratt.parselet(.DOT, .MEMBER).infix = (context, left) => {
var dot = context.current.range
context.next
var name = context.current.range
if !context.expect(.IDENTIFIER) {
return Node.createDot(left, "").withRange(context.spanSince(left.range)).withInternalRange(dot.rangeAtEnd)
}
return Node.createDot(left, name.toString).withRange(context.spanSince(left.range)).withInternalRange(name)
}
# Group
pratt.parselet(.LEFT_PARENTHESIS, .LOWEST).prefix = (context) => {
var token = context.next
var value = pratt.parse(context, .LOWEST)
if value == null || !context.expect(.RIGHT_PARENTHESIS) {
return Node.createParseError.withRange(context.spanSince(token.range))
}
return value.withRange(context.spanSince(token.range))
}
# Call
pratt.parselet(.LEFT_PARENTHESIS, .UNARY_POSTFIX).infix = (context, left) => {
var token = context.next
var node = Node.createCall(left)
if !parseCommaSeparatedList(context, node, .RIGHT_PARENTHESIS) {
return Node.createParseError.withRange(context.spanSince(token.range))
}
return node.withRange(context.spanSince(left.range)).withInternalRange(context.spanSince(token.range))
}
# Index
pratt.parselet(.LEFT_BRACKET, .MEMBER).infix = (context, left) => {
var token = context.next
# The "[]" syntax isn't valid but skip over it and recover
if context.peek(.RIGHT_BRACKET) {
context.unexpectedToken
context.next
return Node.createParseError.withRange(context.spanSince(token.range))
}
var value = pratt.parse(context, .LOWEST)
if value == null || !context.expect(.RIGHT_BRACKET) {
return Node.createParseError.withRange(context.spanSince(token.range))
}
return Node.createBinary(.INDEX, left, value).withRange(context.spanSince(left.range)).withInternalRange(context.spanSince(token.range))
}
# Hook
pratt.parselet(.QUESTION, .ASSIGN).infix = (context, left) => {
var token = context.next
var middle = pratt.parse(context, .COMMA)
if middle == null || !context.expect(.COLON) {
return Node.createParseError.withRange(context.spanSince(token.range))
}
var right = pratt.parse(context, .COMMA)
if right == null {
return Node.createParseError.withRange(context.spanSince(token.range))
}
return Node.createHook(left, middle, right).withRange(context.spanSince(left.range))
}
return pratt
}
def parseCommaSeparatedList(context ParserContext, parent Node, stop TokenKind) bool {
var isFirst = true
while !context.eat(stop) {
if !isFirst {
context.expect(.COMMA)
}
var firstToken = context.current
var value = pratt.parse(context, .COMMA)
if value != null {
parent.appendChild(value)
} else {
# Recover from errors due to partially-typed calls
parent.appendChild(Node.createParseError.withRange(context.spanSince(firstToken.range)))
if context.current.kind != .COMMA && context.current.kind != stop {
return false
}
}
isFirst = false
}
return true
}
def parseDoWhile(context ParserContext) Node {
var token = context.next
context.pushScope(Scope.new(.LOOP, context.scope))
var body = parseStatement(context, .LOCAL)
if body == null || !context.expect(.WHILE) || !context.expect(.LEFT_PARENTHESIS) {
return null
}
var test = pratt.parse(context, .LOWEST)
if test == null {
return null
}
if !context.expect(.RIGHT_PARENTHESIS) {
return null
}
context.popScope
return checkForSemicolon(context, token.range, Node.createDoWhile(body, test))
}
def parseExportOrImport(context ParserContext) Node {
var token = context.next
var old = context.flags
context.flags |= token.kind == .EXPORT ? .EXPORTED : .IMPORTED
# Parse a modifier block
if context.eat(.LEFT_BRACE) {
var node = Node.createModifierBlock
if !parseStatements(context, node, .GLOBAL) || !context.expect(.RIGHT_BRACE) {
return null
}
context.flags = old
return node.withRange(context.spanSince(token.range))
}
# Just parse a single statement
var statement = parseStatement(context, .GLOBAL)
if statement == null {
return null
}
context.flags = old
return statement
}
const _extensionBehaviors StringMap<ExtensionBehavior> = {
"disable": .DISABLE,
"enable": .ENABLE,
"require": .REQUIRE,
"warn": .WARN,
}
# From https://www.khronos.org/registry/webgl/extensions/
const _knownWebGLExtensions StringMap<int> = {
"GL_OES_standard_derivatives": 0,
"GL_EXT_frag_depth": 0,
"GL_EXT_draw_buffers": 0,
"GL_EXT_shader_texture_lod": 0,
}
def parseExtension(context ParserContext) Node {
var token = context.next
var range = context.current.range
if !context.expect(.IDENTIFIER) {
return null
}
var name = range.toString
# Parse an extension block (a non-standard addition)
if context.eat(.LEFT_BRACE) {
if !(name in context.compilationData.currentExtensions) {
context.compilationData.currentExtensions[name] = .DEFAULT # Silence warnings about this name
}
var block = Node.createModifierBlock
if !parseStatements(context, block, .GLOBAL) || !context.expect(.RIGHT_BRACE) {
return null
}
for child = block.firstChild; child != null; child = child.nextSibling {
if child.kind == .VARIABLES {
for variable = child.variablesType.nextSibling; variable != null; variable = variable.nextSibling {
variable.symbol.requiredExtension = name
}
} else if child.symbol != null {
child.symbol.requiredExtension = name
}
}
return block.withRange(context.spanSince(token.range))
}
# Warn about typos
if !(name in _knownWebGLExtensions) && !(name in context.compilationData.currentExtensions) {
context.log.syntaxWarningUnknownExtension(range, name)
}
# Parse a regular extension pragma
if !context.expect(.COLON) {
return null
}
var text = context.current.range.toString
if !(text in _extensionBehaviors) {
context.unexpectedToken
return null
}
context.next
# Activate or deactivate the extension
var behavior = _extensionBehaviors[text]
context.compilationData.currentExtensions[name] = behavior
return Node.createExtension(name, behavior).withRange(context.spanSince(token.range)).withInternalRange(range)
}
def parseFor(context ParserContext) Node {
var token = context.next
context.pushScope(Scope.new(.LOOP, context.scope))
if !context.expect(.LEFT_PARENTHESIS) {
return null
}
# Setup
var setup Node = null
if !context.eat(.SEMICOLON) {
# Check for a type
var comments = parseLeadingComments(context)
var flags = parseFlags(context, .LOCAL)
var type Node = null
if flags != 0 {
type = parseType(context, .REPORT_ERRORS)
if type == null {
return null
}
} else {
type = parseType(context, .IGNORE_ERRORS)
}
# Try to parse a variable
if type != null {
setup = parseAfterType(context, token.range, flags, type, .AVOID_FUNCTIONS, comments)
if setup == null {
return null
}
} else {
setup = pratt.parse(context, .LOWEST)
if setup == null {
return null
}
if !context.expect(.SEMICOLON) {
return null
}
}
}
# Test
var test Node = null
if !context.eat(.SEMICOLON) {
test = pratt.parse(context, .LOWEST)
if test == null {
return null
}
if !context.expect(.SEMICOLON) {
return null
}
}
# Update
var update Node = null
if !context.eat(.RIGHT_PARENTHESIS) {
update = pratt.parse(context, .LOWEST)
if update == null {
return null
}
if !context.expect(.RIGHT_PARENTHESIS) {
return null
}
}
# Body
var body = parseStatement(context, .LOCAL)
if body == null {
return null
}
context.popScope
return Node.createFor(setup, test, update, body).withRange(context.spanSince(token.range))
}
def parseIf(context ParserContext) Node {
var token = context.next
if !context.expect(.LEFT_PARENTHESIS) {
return null
}
var firstToken = context.current
var test = pratt.parse(context, .LOWEST)
if test == null {
test = Node.createParseError.withRange(context.spanSince(firstToken.range))
}
if !context.expect(.RIGHT_PARENTHESIS) {
return null
}
var yes = parseStatement(context, .LOCAL)
if yes == null {
return null
}
var no Node = null
if context.eat(.ELSE) {
no = parseStatement(context, .LOCAL)
if no == null {
return null
}
}
return Node.createIf(test, yes, no).withRange(context.spanSince(token.range))
}
def parseVersion(context ParserContext) Node {
var token = context.next
var range = context.current.range
if !context.expect(.INT_LITERAL) {
return null
}
return Node.createVersion((range.toString as dynamic) | 0).withRange(context.spanSince(token.range))
}
def parseWhile(context ParserContext) Node {
var token = context.next
context.pushScope(Scope.new(.LOOP, context.scope))
if !context.expect(.LEFT_PARENTHESIS) {
return null
}
var firstToken = context.current
var test = pratt.parse(context, .LOWEST)
if test == null {
test = Node.createParseError.withRange(context.spanSince(firstToken.range))
}
if !context.expect(.RIGHT_PARENTHESIS) {
return null
}
var body = parseStatement(context, .LOCAL)
if body == null {
return null
}
context.popScope
return Node.createWhile(test, body).withRange(context.spanSince(token.range))
}
def parseReturn(context ParserContext) Node {
var token = context.next
var value Node = null
if !context.eat(.SEMICOLON) {
var firstToken = context.current
value = pratt.parse(context, .LOWEST)
if value == null {
value = Node.createParseError.withRange(context.spanSince(firstToken.range))
}
context.expect(.SEMICOLON)
}
return Node.createReturn(value).withRange(context.spanSince(token.range))
}
def parsePrecision(context ParserContext) Node {
var token = context.next
var flag SymbolFlags = 0
switch context.current.kind {
case .LOWP { flag = .LOWP }
case .MEDIUMP { flag = .MEDIUMP }
case .HIGHP { flag = .HIGHP }
default {
context.unexpectedToken
return null
}
}
context.next
var type = parseType(context, .REPORT_ERRORS)
if type == null {
return null
}
return checkForSemicolon(context, token.range, Node.createPrecision(flag, type))
}
def parseStruct(context ParserContext, flags int, comments List<string>) Node {
var name = context.current.range
if !context.expect(.IDENTIFIER) {
return null
}
var symbol = StructSymbol.new(context.compilationData.nextSymbolID, name, name.toString, Scope.new(.STRUCT, context.scope))
symbol.flags |= context.flags | flags
symbol.comments = comments
if !tryToDefineUniquelyInScope(context, symbol) {
return null
}
var range = context.current.range
var block = Node.createStructBlock
var variables Node = null
if !context.expect(.LEFT_BRACE) {
return null
}
context.pushScope(symbol.scope)
while !context.peek(.RIGHT_BRACE) && !context.peek(.END_OF_FILE) {
var statement = parseStatement(context, .STRUCT)
if statement == null {
return null
}
if statement.kind != .VARIABLES {
context.log.syntaxErrorInsideStruct(statement.range)
continue
}
block.appendChild(statement)
for child = statement.variablesType.nextSibling; child != null; child = child.nextSibling {
var variable = child.symbol.asVariable
symbol.variables.append(variable)
if variable.value != null {
context.log.syntaxErrorStructVariableInitializer(variable.value.range)
}
}
}
context.popScope
if !context.expect(.RIGHT_BRACE) {
return null
}
block.withRange(context.spanSince(range))
# Parse weird struct-variable hybrid things
#
# struct S { int x; } y, z[2];
#
if context.peek(.IDENTIFIER) {
variables = parseVariables(0, Node.createType(symbol.resolvedType), context.next.range, context, comments)
if variables == null {
return null
}
}
else {
context.expect(.SEMICOLON)
}
return Node.createStruct(symbol, block, variables)
}
def checkForLoopAndSemicolon(context ParserContext, range Range, node Node) Node {
var found = false
for scope = context.scope; scope != null; scope = scope.parent {
if scope.kind == .LOOP {
found = true
break
}
}
if !found {
context.log.syntaxErrorOutsideLoop(range)
}
return checkForSemicolon(context, range, node)
}
def checkForSemicolon(context ParserContext, range Range, node Node) Node {
context.expect(.SEMICOLON)
return node.withRange(context.spanSince(range))
}
enum Allow {
AVOID_FUNCTIONS
ALLOW_FUNCTIONS
}
def parseAfterType(context ParserContext, range Range, flags SymbolFlags, type Node, allow Allow, comments List<string>) Node {
var name = context.current.range
if flags == 0 && !context.peek(.IDENTIFIER) {
var value = pratt.resume(context, .LOWEST, type)
if value == null {
return null
}
return checkForSemicolon(context, range, Node.createExpression(value))
}
if !context.expect(.IDENTIFIER) {
return null
}
if context.eat(.LEFT_PARENTHESIS) {
return parseFunction(flags, type, name, context, comments)
}
var variables = parseVariables(flags, type, name, context, comments)
if variables == null {
return null
}
return variables.withRange(context.spanSince(range))
}
def parseLeadingComments(context ParserContext) List<string> {
var firstToken = context.current
var comments = firstToken.comments
if comments == null {
return null
}
var nextRangeStart = firstToken.range.start
var leadingComments List<string> = null
# Scan the comments backwards
for i = comments.count - 1; i >= 0; i-- {
var comment = comments[i]
# Count the newlines in between this token and the next token
var whitespace = comment.source.contents.slice(comment.end, nextRangeStart)
var newlineCount = 0
for j = 0; j < whitespace.count; j++ {
var c = whitespace[j]
if c == '\r' || c == '\n' {
newlineCount++
if c == '\r' && j + 1 < whitespace.count && whitespace[j + 1] == '\n' {
j++
}
}
}
# Don't count comments if there's a blank line in between the comment and the statement
if newlineCount > 1 {
break
}
# Otherwise, count this comment
(leadingComments ?= []).append(comment.toString)
nextRangeStart = comment.start
}
if leadingComments != null {
leadingComments.reverse
}
return leadingComments
}
def parseStatement(context ParserContext, mode VariableKind) Node {
var token = context.current
switch token.kind {
case .BREAK { return checkForLoopAndSemicolon(context, context.next.range, Node.createBreak) }
case .CONTINUE { return checkForLoopAndSemicolon(context, context.next.range, Node.createContinue) }
case .DISCARD { return checkForSemicolon(context, context.next.range, Node.createDiscard) }
case .DO { return parseDoWhile(context) }
case .EXPORT, .IMPORT { return parseExportOrImport(context) }
case .EXTENSION { return parseExtension(context) }
case .FOR { return parseFor(context) }
case .IF { return parseIf(context) }
case .LEFT_BRACE { return parseBlock(context) }
case .PRECISION { return parsePrecision(context) }
case .RETURN { return parseReturn(context) }
case .SEMICOLON { return Node.createBlock.withRange(context.next.range) }
case .VERSION { return parseVersion(context) }
case .WHILE { return parseWhile(context) }
}
# Try to parse a variable or function
var comments = parseLeadingComments(context)
var flags = parseFlags(context, mode)
var type Node = null
if context.eat(.STRUCT) {
var struct = parseStruct(context, flags, comments)
if struct == null {
return null
}
return struct.withRange(context.spanSince(token.range))
}
if flags != 0 {
type = parseType(context, .REPORT_ERRORS)
if type == null {
return null
}
} else {
type = parseType(context, .IGNORE_ERRORS)
}
if type != null {
return parseAfterType(context, token.range, flags, type, .ALLOW_FUNCTIONS, comments)
}
# Parse an expression
var value = pratt.parse(context, .LOWEST)
if value == null {
return null
}
return checkForSemicolon(context, token.range, Node.createExpression(value))
}
def checkStatementLocation(context ParserContext, node Node) {
if node.kind == .VARIABLES || node.kind == .STRUCT {
return
}
var isOutsideFunction =
context.scope.kind == .GLOBAL ||
context.scope.kind == .STRUCT
var shouldBeOutsideFunction =
node.kind == .EXTENSION ||
node.kind == .FUNCTION ||
node.kind == .PRECISION ||
node.kind == .VERSION
if shouldBeOutsideFunction && !isOutsideFunction {
context.log.syntaxErrorInsideFunction(node.range)
} else if !shouldBeOutsideFunction && isOutsideFunction {
context.log.syntaxErrorOutsideFunction(node.range)
}
}
def parseInclude(context ParserContext, parent Node) bool {
# See if there is a string literal
var range = context.current.range
if !context.expect(.STRING_LITERAL) {
return false
}
# Decode the escapes
var path string
try {
path = dynamic.JSON.parse(range.toString)
} catch {
context.log.syntaxErrorInvalidString(range)
return false
}
# Must have access to the file system
var fileAccess = context.compilationData.fileAccess
if fileAccess == null {
context.log.semanticErrorIncludeWithoutFileAccess(range)
return false
}
# Must be able to read the file
var source = fileAccess(path, range.source.name)
if source == null {
context.log.semanticErrorIncludeBadPath(range, path)
return false
}
if source.name in context.processedIncludes {
# We've already processed this include; no need to do it again
return true
}
context.processedIncludes[source.name] = true
# Track the included file for jump-to-file in the IDE
context.includes.append(Include.new(range, source.entireRange))
# Parse the file and insert it into the parent
var tokens = Tokenizer.tokenize(context.log, source, .COMPILE)
var nestedContext = ParserContext.new(context.log, tokens, context.compilationData, context.resolver, context.processedIncludes)
nestedContext.pushScope(context.scope)
if !parseStatements(nestedContext, parent, .GLOBAL) || !nestedContext.expect(.END_OF_FILE) {
return false
}
return true
}
def parseBlock(context ParserContext) Node {
var token = context.current
var block = Node.createBlock
context.pushScope(Scope.new(.LOCAL, context.scope))
if !context.expect(.LEFT_BRACE) || !parseStatements(context, block, .LOCAL) || !context.expect(.RIGHT_BRACE) {
return null
}
context.popScope
return block.withRange(context.spanSince(token.range))
}
def parseFlags(context ParserContext, mode VariableKind) SymbolFlags {
var flags SymbolFlags = 0
while true {
var kind = context.current.kind
switch kind {
case .ATTRIBUTE { flags |= .ATTRIBUTE }
case .CONST { flags |= .CONST }
case .HIGHP { flags |= .HIGHP }
case .IN { flags |= .IN }
case .INOUT { flags |= .INOUT }
case .LOWP { flags |= .LOWP }
case .MEDIUMP { flags |= .MEDIUMP }
case .OUT { flags |= .OUT }
case .UNIFORM { flags |= .UNIFORM }
case .VARYING { flags |= .VARYING }
default { return flags }
}
if mode == .ARGUMENT && (kind == .ATTRIBUTE || kind == .UNIFORM || kind == .VARYING) ||
mode == .STRUCT && kind != .LOWP && kind != .MEDIUMP && kind != .HIGHP ||
mode != .ARGUMENT && (kind == .IN || kind == .OUT || kind == .INOUT) {
context.log.syntaxErrorBadQualifier(context.current.range)
}
context.next
}
}
enum ParseTypeMode {
IGNORE_ERRORS
REPORT_ERRORS
}
def parseType(context ParserContext, mode ParseTypeMode) Node {
var token = context.current
var type Type = null
switch token.kind {
case .BOOL { type = .BOOL }
case .BVEC2 { type = .BVEC2 }
case .BVEC3 { type = .BVEC3 }
case .BVEC4 { type = .BVEC4 }
case .FLOAT { type = .FLOAT }
case .INT { type = .INT }
case .IVEC2 { type = .IVEC2 }
case .IVEC3 { type = .IVEC3 }
case .IVEC4 { type = .IVEC4 }
case .MAT2 { type = .MAT2 }
case .MAT3 { type = .MAT3 }
case .MAT4 { type = .MAT4 }
case .SAMPLER2D { type = .SAMPLER2D }
case .SAMPLERCUBE { type = .SAMPLERCUBE }
case .VEC2 { type = .VEC2 }
case .VEC3 { type = .VEC3 }
case .VEC4 { type = .VEC4 }
case .VOID { type = .VOID }
case .IDENTIFIER {
var symbol = context.scope.find(token.range.toString)
if symbol == null || !symbol.isStruct {
if mode == .REPORT_ERRORS {
context.unexpectedToken
}
return null
}
type = symbol.resolvedType
}
default {
if mode == .REPORT_ERRORS {
context.unexpectedToken
}
return null
}
}
context.next
return Node.createType(type).withRange(context.spanSince(token.range))
}
def parseFunction(flags SymbolFlags, type Node, name Range, context ParserContext, comments List<string>) Node {
var originalScope = context.scope
var function = FunctionSymbol.new(context.compilationData.nextSymbolID, name, name.toString, Scope.new(.FUNCTION, originalScope))
function.flags |= context.flags | flags | (function.name == "main" ? .EXPORTED : 0)
function.comments = comments
function.returnType = type
context.pushScope(function.scope)
# Takes no arguments
if context.eat(.VOID) {
if !context.expect(.RIGHT_PARENTHESIS) {
return null
}
}
# Takes arguments
else if !context.eat(.RIGHT_PARENTHESIS) {
while true {
# Parse leading flags
var argumentFlags = parseFlags(context, .ARGUMENT)
# Parse the type
var argumentType = parseType(context, .REPORT_ERRORS)
if argumentType == null {
return null
}
# Parse the identifier
var argumentName = context.current.range
if !context.expect(.IDENTIFIER) {
return null
}
# Create the argument
var argument = VariableSymbol.new(context.compilationData.nextSymbolID, argumentName, argumentName.toString, context.scope, .ARGUMENT)
argument.flags |= argumentFlags
argument.type = argumentType
function.arguments.append(argument)
tryToDefineUniquelyInScope(context, argument)
# Array size
if !parseArraySize(context, argument) {
return null
}
# Parse another argument?
if !context.eat(.COMMA) {
break
}
}
if !context.expect(.RIGHT_PARENTHESIS) {
return null
}
}
var previous = originalScope.symbols.get(name.toString, null)
var hasBlock = !context.eat(.SEMICOLON)
# Merge adjacent function symbols to support overloading
if previous == null {
originalScope.define(function)
} else if previous.isFunction {
for link = previous.asFunction; link != null; link = link.previousOverload {
if !link.hasSameArgumentTypesAs(function) {
continue
}
# Overloading by return type is not allowed
if link.returnType.resolvedType != function.returnType.resolvedType {
context.log.syntaxErrorDifferentReturnType(function.returnType.range, function.name,
function.returnType.resolvedType, link.returnType.resolvedType, link.returnType.range)
}
# Defining a function more than once is not allowed
else if link.block != null || !hasBlock {
context.log.syntaxErrorDuplicateSymbolDefinition(function.range, link.range)
}
# Merge the function with its forward declaration
else {
assert(link.sibling == null)
assert(function.sibling == null)
link.sibling = function
function.sibling = link
function.flags |= link.flags
link.flags = function.flags
}
break
}
# Use a singly-linked list to store the function overloads
function.previousOverload = previous.asFunction
originalScope.redefine(function)
} else {
context.log.syntaxErrorDuplicateSymbolDefinition(name, previous.range)
return null
}
if hasBlock {
var old = context.flags
context.flags &= ~(.EXPORTED | .IMPORTED)
function.block = parseBlock(context)
context.flags &= old
if function.block == null {
return null
}
}
context.popScope
return Node.createFunction(function).withRange(context.spanSince(type.range))
}
def parseArraySize(context ParserContext, variable VariableSymbol) bool {
var token = context.current
if context.eat(.LEFT_BRACKET) {
# The "[]" syntax isn't valid but skip over it and recover
if context.eat(.RIGHT_BRACKET) {
context.log.syntaxErrorMissingArraySize(context.spanSince(token.range))
return true
}
variable.arrayCount = pratt.parse(context, .LOWEST)
if variable.arrayCount == null || !context.expect(.RIGHT_BRACKET) {
return false
}