-
Notifications
You must be signed in to change notification settings - Fork 78
/
Rascal.rsc
887 lines (748 loc) · 29.9 KB
/
Rascal.rsc
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
@license{
Copyright (c) 2009-2015 CWI
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html
}
@contributor{Jurgen J. Vinju - Jurgen.Vinju@cwi.nl - CWI}
@contributor{Tijs van der Storm - Tijs.van.der.Storm@cwi.nl}
@contributor{Paul Klint - Paul.Klint@cwi.nl - CWI}
@contributor{Arnold Lankamp - Arnold.Lankamp@cwi.nl}
@contributor{Michael Steindorfer - Michael.Steindorfer@cwi.nl - CWI}
@synopsis{The syntax definition of Rascal, excluding concrete syntax fragments}
@bootstrapParser
module lang::rascal::\syntax::Rascal
lexical BooleanLiteral
= "true"
| "false" ;
syntax Literal
= integer: IntegerLiteral integerLiteral
| regExp: RegExpLiteral regExpLiteral
| \real: RealLiteral realLiteral
| boolean: BooleanLiteral booleanLiteral
| string: StringLiteral stringLiteral
| dateTime: DateTimeLiteral dateTimeLiteral
| location: LocationLiteral locationLiteral
| rational: RationalLiteral rationalLiteral
;
syntax Expression = concrete: Concrete concrete;
syntax Pattern = concrete: Concrete concrete;
lexical Concrete
= typed: "(" LAYOUTLIST l1 Sym symbol LAYOUTLIST l2 ")" LAYOUTLIST l3 "`" ConcretePart* parts "`";
lexical ConcretePart
= @category="MetaSkipped" text : ![`\<\>\\\n]+ !>> ![`\<\>\\\n]
| newline: "\n" [\ \t \u00A0 \u1680 \u2000-\u200A \u202F \u205F \u3000]* "\'"
| @category="MetaVariable" hole : ConcreteHole hole
| @category="MetaSkipped" lt: "\\\<"
| @category="MetaSkipped" gt: "\\\>"
| @category="MetaSkipped" bq: "\\`"
| @category="MetaSkipped" bs: "\\\\"
;
syntax ConcreteHole
= \one: "\<" Sym symbol Name name "\>"
;
start syntax Module
= \default: Header header Body body ;
syntax ModuleParameters
= \default: "[" {TypeVar ","}+ parameters "]" ;
lexical DateAndTime
= "$" DatePart "T" TimePartNoTZ !>> [+\-] "$"
| "$" DatePart "T" TimePartNoTZ TimeZonePart "$";
syntax Strategy
= topDownBreak: "top-down-break"
| topDown: "top-down"
| bottomUp: "bottom-up"
| bottomUpBreak: "bottom-up-break"
| outermost: "outermost"
| innermost: "innermost" ;
lexical UnicodeEscape
= utf16: "\\" [u] [0-9 A-F a-f] [0-9 A-F a-f] [0-9 A-F a-f] [0-9 A-F a-f]
| utf32: "\\" [U] (("0" [0-9 A-F a-f]) | "10") [0-9 A-F a-f] [0-9 A-F a-f] [0-9 A-F a-f] [0-9 A-F a-f] // 24 bits
| ascii: "\\" [a] [0-7] [0-9A-Fa-f]
;
syntax Variable
= initialized: Name name "=" Expression initial
| unInitialized: Name name ;
lexical OctalIntegerLiteral
= [0] [0-7]+ !>> [0-9 A-Z _ a-z] ;
syntax TypeArg
= \default: Type type
| named: Type type Name name ;
syntax Renaming
= \default: Name from "=\>" Name to ;
syntax Catch
= \default: "catch" ":" Statement body
| binding: "catch" Pattern pattern ":" Statement body ;
lexical PathChars
= URLChars [|] ;
syntax Signature
= withThrows: FunctionModifiers modifiers Type type Name name Parameters parameters "throws" {Type ","}+ exceptions
| noThrows: FunctionModifiers modifiers Type type Name name Parameters parameters ;
syntax Sym
// named non-terminals
= nonterminal: Nonterminal nonterminal !>> "["
| parameter: "&" Nonterminal nonterminal
| parametrized: Nonterminal nonterminal >> "[" "[" {Sym ","}+ parameters "]"
| \start: "start" "[" Nonterminal nonterminal "]"
| labeled: Sym symbol NonterminalLabel label
// literals
| characterClass: Class charClass
| literal: StringConstant string
| caseInsensitiveLiteral: CaseInsensitiveStringConstant cistring
// regular expressions
| iter: Sym symbol "+"
| iterStar: Sym symbol "*"
| iterSep: "{" Sym symbol Sym sep "}" "+"
| iterStarSep: "{" Sym symbol Sym sep "}" "*"
| optional: Sym symbol "?"
| alternative: "(" Sym first "|" {Sym "|"}+ alternatives ")"
| sequence: "(" Sym first Sym+ sequence ")"
// TODO: MinimalIter: Sym symbol IntegerConstant minimal "+"
// TODO: MinimalIterSep: "{" Sym symbol Symbol sep "}" IntegerConstant minimal "+"
// TODO | Permutation: "(" Sym first "~" {Sym "~"}+ participants ")"
// TODO | Combination: "(" Sym first "#" {Sym "#"}+ elements ")"
| empty: "(" ")"
// conditionals
| column: Sym symbol "@" IntegerLiteral column
| endOfLine: Sym symbol "$"
| startOfLine: "^" Sym symbol
| except: Sym symbol "!" NonterminalLabel label
>
assoc (
left ( follow: Sym symbol "\>\>" Sym match
| notFollow: Sym symbol "!\>\>" Sym match
)
|
right ( precede: Sym match "\<\<" Sym symbol
| notPrecede: Sym match "!\<\<" Sym symbol
)
)
>
left unequal: Sym symbol "\\" Sym match
;
lexical TimePartNoTZ
= [0-2] [0-9] [0-5] [0-9] [0-5] [0-9] ([, .] [0-9] ([0-9] [0-9]?)?)?
| [0-2] [0-9] ":" [0-5] [0-9] ":" [0-5] [0-9] ([, .] [0-9] ([0-9] [0-9]?)?)?
;
syntax Header
= parameters: Tags tags "module" QualifiedName name ModuleParameters params Import* imports
| \default: Tags tags "module" QualifiedName name Import* imports ;
lexical Name
// Names are surrounded by non-alphabetical characters, i.e. we want longest match.
= ([A-Z a-z _] !<< [A-Z _ a-z] [0-9 A-Z _ a-z]* !>> [0-9 A-Z _ a-z]) \ RascalKeywords
| [\\] [A-Z _ a-z] [\- 0-9 A-Z _ a-z]* !>> [\- 0-9 A-Z _ a-z]
;
syntax SyntaxDefinition
= @Foldable \layout : Visibility vis "layout" Sym defined "=" Prod production ";"
| @Foldable \lexical : "lexical" Sym defined "=" Prod production ";"
| @Foldable \keyword : "keyword" Sym defined "=" Prod production ";"
| @Foldable language: Start start "syntax" Sym defined "=" Prod production ";" ;
syntax Kind
= function: "function"
| variable: "variable"
| \all: "all"
| \anno: "anno"
| \data: "data"
| view: "view"
| \alias: "alias"
| \module: "module"
| \tag: "tag" ;
syntax ImportedModule
= \default: QualifiedName name
| actualsRenaming: QualifiedName name ModuleActuals actuals Renamings renamings
| renamings: QualifiedName name Renamings renamings
| actuals: QualifiedName name ModuleActuals actuals
;
syntax Target
= empty:
| labeled: Name name ;
syntax IntegerLiteral
= /*prefer()*/ decimalIntegerLiteral: DecimalIntegerLiteral decimal
| /*prefer()*/ hexIntegerLiteral: HexIntegerLiteral hex
| /*prefer()*/ octalIntegerLiteral: OctalIntegerLiteral octal ;
syntax FunctionBody
= \default: "{" Statement* statements "}" ;
syntax Expression
= nonEmptyBlock : "{" Statement+ statements "}"
| bracket \bracket: "(" Expression expression ")"
| closure : Type type Parameters parameters "{" Statement+ statements "}"
| stepRange : "[" Expression first "," Expression second ".." Expression last "]"
| voidClosure : Parameters parameters "{" Statement* statements0 "}"
| \visit : Label label Visit visit
| reducer : "(" Expression init "|" Expression result "|" {Expression ","}+ generators ")"
| reifiedType : "type" "(" Expression symbol "," Expression definitions ")"
| callOrTree : Expression!transitiveClosure!transitiveReflexiveClosure!isDefined expression "(" {Expression ","}* arguments KeywordArguments[Expression] keywordArguments ")"
| literal : Literal literal
| \any : "any" "(" {Expression ","}+ generators ")"
| \all : "all" "(" {Expression ","}+ generators ")"
| comprehension : Comprehension comprehension
| \set : "{" {Expression ","}* elements0 "}"
| \list : "[" {Expression ","}* elements0 "]"
| reifyType : "#" Type type !>> "[" !selector
| range : "[" Expression first ".." Expression last "]"
| \tuple : "\<" {Expression ","}+ elements "\>"
| \map : "(" {Mapping[Expression] ","}* mappings ")"
| \it : [A-Z a-z _] !<< "it" !>> [A-Z a-z _]
| qualifiedName : QualifiedName qualifiedName
| subscript : Expression expression!transitiveClosure!transitiveReflexiveClosure!isDefined "[" {Expression ","}+ subscripts "]"
| slice : Expression expression!transitiveClosure!transitiveReflexiveClosure!isDefined "[" OptionalExpression optFirst ".." OptionalExpression optLast "]"
| sliceStep : Expression expression!transitiveClosure!transitiveReflexiveClosure!isDefined "[" OptionalExpression optFirst "," Expression second ".." OptionalExpression optLast "]"
| fieldAccess : Expression expression "." Name field
| fieldUpdate : Expression expression "[" Name key "=" Expression replacement "]"
| fieldProject : Expression expression!transitiveClosure!transitiveReflexiveClosure!isDefined "\<" {Field ","}+ fields "\>"
| setAnnotation: Expression expression "[" "@" Name name "=" Expression value "]"
| getAnnotation: Expression expression >> "@" "@" Name name
| is : Expression expression "is" Name name
| has : Expression expression "has" Name name
| transitiveClosure: Expression argument "+" !>> "="
| transitiveReflexiveClosure: Expression argument "*" !>> "="
> isDefined : Expression argument "?"
> negation : "!" Expression!match!noMatch argument
| negative : "-" Expression argument
| non-assoc splice : "*" Expression argument
| asType : "[" Type type "]" Expression!match!noMatch argument
> left composition: Expression lhs "o" Expression rhs
> left ( product: Expression lhs "*" () !>> "*" Expression!noMatch!match rhs
| \join : Expression lhs "join" Expression rhs
| remainder: Expression lhs "%" Expression rhs
| division: Expression lhs "/" Expression rhs
)
> left intersection: Expression lhs "&" !>> "&" Expression rhs
> left ( addition : Expression lhs "+" Expression!noMatch!match rhs
| subtraction: Expression!transitiveClosure!transitiveReflexiveClosure lhs "-" Expression rhs
| appendAfter: Expression lhs "\<\<" !>> "=" Expression rhs
| insertBefore: Expression lhs "\>\>" Expression rhs
)
> left modulo: Expression lhs "mod" Expression rhs
> non-assoc ( notIn: Expression lhs "notin" Expression rhs
| \in: Expression lhs "in" Expression rhs
)
> non-assoc ( greaterThanOrEq: Expression lhs "\>=" Expression rhs
| lessThanOrEq : Expression lhs "\<=" Expression rhs
| lessThan : Expression lhs "\<" !>> "-" Expression rhs
| greaterThan : Expression lhs "\>" Expression rhs
)
> non-assoc ( equals : Expression lhs "==" Expression rhs
| nonEquals : Expression lhs "!=" Expression rhs
)
> non-assoc ifDefinedOtherwise: Expression lhs "?" Expression rhs
> non-assoc ( noMatch: Pattern pattern "!:=" Expression expression
| match: Pattern pattern ":=" Expression expression
| enumerator: Pattern pattern "\<-" Expression expression
)
> non-assoc ( implication: Expression lhs "==\>" Expression rhs
| equivalence: Expression lhs "\<==\>" Expression rhs
)
> left and: Expression lhs "&&" Expression rhs
> left or: Expression lhs "||" Expression rhs
> right ifThenElse: Expression condition "?" Expression thenExp ":" Expression elseExp
;
syntax OptionalExpression
= expression: Expression expression
| noExpression: ()
;
syntax UserType
= name: QualifiedName name
| parametric: QualifiedName name >> "[" "[" {Type ","}+ parameters "]" ;
syntax Import
= \extend: "extend" ImportedModule module ";"
| \default: "import" ImportedModule module ";"
| \external: "import" QualifiedName name "=" LocationLiteral at ";"
| \syntax: SyntaxDefinition syntax ;
syntax Body
= toplevels: Toplevel* toplevels ;
lexical URLChars
= ![\t-\n \r \ \< |]* ;
lexical TimeZonePart
= [+ \-] [0-1] [0-9] ":" [0-5] [0-9]
| "Z"
| [+ \-] [0-1] [0-9]
| [+ \-] [0-1] [0-9] [0-5] [0-9]
;
syntax ProtocolPart
= nonInterpolated: ProtocolChars protocolChars
| interpolated: PreProtocolChars pre Expression expression ProtocolTail tail ;
syntax StringTemplate
= ifThen : "if" "(" {Expression ","}+ conditions ")" "{" Statement* preStats StringMiddle body Statement* postStats "}"
| ifThenElse: "if" "(" {Expression ","}+ conditions ")" "{" Statement* preStatsThen StringMiddle thenString Statement* postStatsThen "}" "else" "{" Statement* preStatsElse StringMiddle elseString Statement* postStatsElse "}"
| \for : "for" "(" {Expression ","}+ generators ")" "{" Statement* preStats StringMiddle body Statement* postStats "}"
| doWhile : "do" "{" Statement* preStats StringMiddle body Statement* postStats "}" "while" "(" Expression condition ")"
| \while : "while" "(" Expression condition ")" "{" Statement* preStats StringMiddle body Statement* postStats "}" ;
lexical PreStringChars
= @category="Constant" [\"] StringCharacter* [\<] ;
lexical CaseInsensitiveStringConstant
= @category="Constant" "\'" StringCharacter* chars "\'" ;
lexical Backslash
= [\\] !>> [/ \< \> \\] ;
syntax Label
= \default: Name name ":"
| empty: ;
lexical MidProtocolChars
= "\>" URLChars "\<" ;
lexical NamedBackslash
= [\\] !>> [\< \> \\] ;
syntax Field
= index: IntegerLiteral fieldIndex
| name: Name fieldName ;
lexical JustDate
= "$" DatePart "$";
lexical PostPathChars
= "\>" URLChars "|" ;
syntax PathPart
= nonInterpolated: PathChars pathChars
| interpolated: PrePathChars pre Expression expression PathTail tail ;
lexical DatePart
= [0-9] [0-9] [0-9] [0-9] "-" [0-1] [0-9] "-" [0-3] [0-9]
| [0-9] [0-9] [0-9] [0-9] [0-1] [0-9] [0-3] [0-9] ;
syntax FunctionModifier
= java: "java"
| \test: "test"
| \default: "default";
syntax Assignment
= ifDefined: "?="
| division: "/="
| product: "*="
| intersection: "&="
| subtraction: "-="
| \default: "="
| addition: "+="
| \append: "\<\<="
;
syntax Assignable
= bracket \bracket : "(" Assignable arg ")"
| variable : QualifiedName qualifiedName
| subscript : Assignable receiver "[" Expression subscript "]"
| slice : Assignable receiver "[" OptionalExpression optFirst ".." OptionalExpression optLast "]"
| sliceStep : Assignable receiver "[" OptionalExpression optFirst "," Expression second ".." OptionalExpression optLast "]"
| fieldAccess : Assignable receiver "." Name field
| ifDefinedOrDefault: Assignable receiver "?" Expression defaultExpression
| constructor : Name name "(" {Assignable ","}+ arguments ")"
| \tuple : "\<" {Assignable ","}+ elements "\>"
| annotation : Assignable receiver "@" Name annotation ;
lexical StringConstant
= @category="Constant" "\"" StringCharacter* chars "\"" ;
syntax Assoc
= associative: "assoc"
| left: "left"
| nonAssociative: "non-assoc"
| right: "right" ;
syntax Replacement
= unconditional: Expression replacementExpression
| conditional: Expression replacementExpression "when" {Expression ","}+ conditions ;
syntax DataTarget
= empty:
| labeled: Name label ":" ;
lexical StringCharacter
= "\\" [\" \' \< \> \\ b f n r t]
| UnicodeEscape
| ![\" \' \< \> \\]
| [\n][\ \t \u00A0 \u1680 \u2000-\u200A \u202F \u205F \u3000]* [\'] // margin
;
lexical JustTime
= "$T" TimePartNoTZ !>> [+\-] "$"
| "$T" TimePartNoTZ TimeZonePart "$"
;
lexical MidStringChars
= @category="Constant" [\>] StringCharacter* [\<] ;
lexical ProtocolChars
= [|] URLChars "://" !>> [\t-\n \r \ \u00A0 \u1680 \u2000-\u200A \u202F \u205F \u3000];
lexical RegExpModifier
= [d i m s]* ;
syntax CommonKeywordParameters
= absent: ()
| present: "(" {KeywordFormal ","}+ keywordFormalList ")"
;
syntax Parameters
= \default: "(" Formals formals KeywordFormals keywordFormals ")"
| varArgs: "(" Formals formals "..." KeywordFormals keywordFormals ")" ;
lexical OptionalComma = \default: ","? ;
syntax KeywordFormals
= \default: OptionalComma optionalComma [,\ (\t\n] << {KeywordFormal ","}+ keywordFormalList
| none: ()
;
syntax KeywordFormal
= \default: Type type Name name "=" Expression expression
;
syntax KeywordArguments[&T]
= \default: OptionalComma optionalComma [,\ (\t\n] << {KeywordArgument[&T] ","}+ keywordArgumentList
| none: ()
;
syntax KeywordArgument[&T] = \default: Name name "=" &T expression ;
lexical RegExp
= ![/ \< \> \\]
| "\<" Name "\>"
| [\\] [/ \< \> \\]
| "\<" Name ":" NamedRegExp* "\>"
| Backslash
// | @category="MetaVariable" [\<] Expression expression [\>] TODO: find out why this production existed
;
layout LAYOUTLIST
= LAYOUT* !>> [\u0009-\u000D \u0020 \u0085 \u00A0 \u1680 \u180E \u2000-\u200A \u2028 \u2029 \u202F \u205F \u3000] !>> "//" !>> "/*";
syntax LocalVariableDeclaration
= \default: Declarator declarator
| \dynamic: "dynamic" Declarator declarator ;
lexical RealLiteral
= [0-9]+ [D F d f]
| [0-9]+ [E e] [+ \-]? [0-9]+ [D F d f]?
| [0-9]+ "." !>> "." [0-9]* [D F d f]?
| [0-9]+ "." [0-9]* [E e] [+ \-]? [0-9]+ [D F d f]?
| [.] !<< "." [0-9]+ [D F d f]?
| [.] !<< "." [0-9]+ [E e] [+ \-]? [0-9]+ [D F d f]?
;
syntax Range
= fromTo: Char start "-" Char end
| character: Char character ;
syntax LocationLiteral
= \default: ProtocolPart protocolPart PathPart pathPart ;
syntax ShellCommand
= setOption: "set" QualifiedName name Expression expression
| undeclare: "undeclare" QualifiedName name
| help: "help"
| edit: "edit" QualifiedName name
| unimport: "unimport" QualifiedName name
| listDeclarations: "declarations"
| quit: "quit"
| history: "history"
| \test: "test"
| listModules: "modules"
| clear: "clear";
syntax StringMiddle
= mid: MidStringChars mid
| template: MidStringChars mid StringTemplate template StringMiddle tail
| interpolated: MidStringChars mid Expression expression StringMiddle tail ;
syntax QualifiedName
= \default: {Name "::"}+ names !>> "::" ;
lexical RationalLiteral
= [0-9][0-9]* [r]
| [1-9][0-9]* [r] [0-9][0-9]* !>> [0-9 A-Z _ a-z]
;
lexical DecimalIntegerLiteral
= "0" !>> [0-9 A-Z _ a-z]
| [1-9] [0-9]* !>> [0-9 A-Z _ a-z] ;
syntax DataTypeSelector
= selector: QualifiedName sort "." Name production ;
syntax StringTail
= midInterpolated: MidStringChars mid Expression expression StringTail tail
| post: PostStringChars post
| midTemplate: MidStringChars mid StringTemplate template StringTail tail ;
syntax PatternWithAction
= replacing: Pattern pattern "=\>" Replacement replacement
| arbitrary: Pattern pattern ":" Statement statement ;
lexical LAYOUT
= Comment
// all the white space chars defined in Unicode 6.0
| [\u0009-\u000D \u0020 \u0085 \u00A0 \u1680 \u180E \u2000-\u200A \u2028 \u2029 \u202F \u205F \u3000]
;
syntax Visit
= givenStrategy: Strategy strategy "visit" "(" Expression subject ")" "{" Case+ cases "}"
| defaultStrategy: "visit" "(" Expression subject ")" "{" Case+ cases "}" ;
start syntax Commands
= \commandlist: EvalCommand+ commands
;
start syntax EvalCommand
= declaration: Declaration declaration
| statement: Statement!variableDeclaration!functionDeclaration!visit statement
| \import: Import imported
| output: Output
;
lexical Output
= @category="Result" resultOutput: "⇨" ![\n\r]* [\n]
| @category="StdOut" stdoutOutput: ^ "≫" ![\n\r]* [\n]
| @category="StdErr" stderrOutput: ^ "⚠" ![\n\r]* [\n]
;
start syntax Command
= expression: Expression!nonEmptyBlock expression
| declaration: Declaration declaration
| shell: ":" ShellCommand command
| statement: Statement!variableDeclaration!functionDeclaration!visit statement
| \import: Import imported ;
lexical TagString
= "\\" !<< "{" ( ![{}] | ("\\" [{}]) | TagString)* contents "\\" !<< "}";
syntax ProtocolTail
= mid: MidProtocolChars mid Expression expression ProtocolTail tail
| post: PostProtocolChars post ;
lexical Nonterminal
= ([A-Z] !<< [A-Z] [0-9 A-Z _ a-z]* !>> [0-9 A-Z _ a-z]) \ RascalKeywords;
syntax PathTail
= mid: MidPathChars mid Expression expression PathTail tail
| post: PostPathChars post ;
syntax Visibility
= \private: "private"
| \default:
| \public: "public" ;
syntax StringLiteral
= template: PreStringChars pre StringTemplate template StringTail tail
| interpolated: PreStringChars pre Expression expression StringTail tail
| nonInterpolated: StringConstant constant ;
lexical Comment
= @category="Comment" "/*" (![*] | [*] !>> [/])* "*/"
| @category="Comment" "//" ![\n]* !>> [\ \t\r \u00A0 \u1680 \u2000-\u200A \u202F \u205F \u3000] $ // the restriction helps with parsing speed
;
syntax Renamings
= \default: "renaming" {Renaming ","}+ renamings ;
syntax Tags
= \default: Tag* tags ;
syntax Formals
= \default: {Pattern ","}* formals ;
lexical PostProtocolChars
= "\>" URLChars "://" ;
syntax Start
= absent:
| present: "start" ;
syntax Statement
= @breakable \assert: "assert" Expression expression ";"
| @breakable assertWithMessage: "assert" Expression expression ":" Expression message ";"
| @breakable expression: Expression!visit!nonEmptyBlock expression ";"
| @breakable \visit: Label label Visit visit
| @breakable \while: Label label "while" "(" {Expression ","}+ conditions ")" Statement!variableDeclaration!functionDeclaration body
| @breakable doWhile: Label label "do" Statement body "while" "(" Expression condition ")" ";"
| @breakable @breakable{generators} \for: Label label "for" "(" {Expression ","}+ generators ")" Statement body
| @breakable ifThen: Label label "if" "(" {Expression ","}+ conditions ")" Statement!variableDeclaration!functionDeclaration thenStatement () !>> "else"
| @breakable ifThenElse: Label label "if" "(" {Expression ","}+ conditions ")" Statement thenStatement "else" Statement!variableDeclaration!functionDeclaration elseStatement
| @breakable \switch: Label label "switch" "(" Expression expression ")" "{" Case+ cases "}"
| @breakable \fail: "fail" Target target ";"
| @breakable \break: "break" Target target ";"
| @breakable \continue: "continue" Target target ";"
| @breakable \filter: "filter" ";"
| @breakable \solve: "solve" "(" {QualifiedName ","}+ variables Bound bound ")" Statement!variableDeclaration!functionDeclaration body
| @breakable non-assoc \try: "try" Statement body Catch+ handlers
| @breakable tryFinally: "try" Statement body Catch+ handlers "finally" Statement!variableDeclaration!functionDeclaration finallyBody
| nonEmptyBlock: Label label "{" Statement+ statements "}"
| emptyStatement: ";"
| @breakable globalDirective: "global" Type type {QualifiedName ","}+ names ";"
| @breakable assignment: Assignable assignable Assignment operator Statement!functionDeclaration!variableDeclaration statement
| non-assoc (
@breakable \return : "return" Statement!functionDeclaration!variableDeclaration statement
| @breakable \throw : "throw" Statement!functionDeclaration!variableDeclaration statement
| @breakable \insert : "insert" DataTarget dataTarget Statement!functionDeclaration!variableDeclaration statement
| @breakable \append : "append" DataTarget dataTarget Statement!functionDeclaration!variableDeclaration statement
)
| @breakable functionDeclaration: FunctionDeclaration functionDeclaration
| @breakable variableDeclaration: LocalVariableDeclaration declaration ";"
;
syntax StructuredType
= \default: BasicType basicType "[" {TypeArg ","}+ arguments "]" ;
lexical NonterminalLabel
= [a-z] [0-9 A-Z _ a-z]* !>> [0-9 A-Z _ a-z] ;
syntax FunctionType
= typeArguments: Type type "(" {TypeArg ","}* arguments ")" ;
syntax Case
= @Foldable patternWithAction: "case" PatternWithAction patternWithAction
| @Foldable \default: "default" ":" Statement statement ;
syntax Declarator
= \default: Type type {Variable ","}+ variables ;
syntax Bound
= \default: ";" Expression expression
| empty: ;
keyword RascalKeywords
= "o"
| "syntax"
| "keyword"
| "lexical"
| "int"
| "break"
| "continue"
| "rat"
| "true"
| "bag"
| "num"
| "node"
| "finally"
| "private"
| "real"
| "list"
| "fail"
| "filter"
| "if"
| "tag"
| BasicType
| "extend"
| "append"
| "rel"
| "lrel"
| "void"
| "non-assoc"
| "assoc"
| "test"
| "anno"
| "layout"
| "data"
| "join"
| "it"
| "bracket"
| "in"
| "import"
| "false"
| "all"
| "dynamic"
| "solve"
| "type"
| "try"
| "catch"
| "notin"
| "else"
| "insert"
| "switch"
| "return"
| "case"
| "while"
| "str"
| "throws"
| "visit"
| "tuple"
| "for"
| "assert"
| "loc"
| "default"
| "map"
| "alias"
| "any"
| "module"
| "mod"
| "bool"
| "public"
| "one"
| "throw"
| "set"
| "start"
| "datetime"
| "value"
;
syntax Type
= bracket \bracket: "(" Type type ")"
| user: UserType user
| function: FunctionType function
| structured: StructuredType structured
| basic: BasicType basic
| selector: DataTypeSelector selector
| variable: TypeVar typeVar
| symbol: Sym!nonterminal!labeled!parametrized!parameter symbol
;
syntax Declaration
= variable : Tags tags Visibility visibility Type type {Variable ","}+ variables ";"
| annotation : Tags tags Visibility visibility "anno" Type annoType Type onType "@" Name name ";"
| \alias : Tags tags Visibility visibility "alias" UserType user "=" Type base ";"
| \tag : Tags tags Visibility visibility "tag" Kind kind Name name "on" {Type ","}+ types ";"
| dataAbstract: Tags tags Visibility visibility "data" UserType user CommonKeywordParameters commonKeywordParameters ";"
| @Foldable \data : Tags tags Visibility visibility "data" UserType user CommonKeywordParameters commonKeywordParameters"=" {Variant "|"}+ variants ";"
| function : FunctionDeclaration functionDeclaration
;
syntax Class
= simpleCharclass: "[" Range* ranges "]"
| complement: "!" Class charClass
> left difference: Class lhs "-" Class rhs
> left intersection: Class lhs "&&" Class rhs
> left union: Class lhs "||" Class rhs
| bracket \bracket: "(" Class charClass ")" ;
lexical RegExpLiteral
= "/" RegExp* "/" RegExpModifier ;
syntax FunctionModifiers
= \modifierlist: FunctionModifier* modifiers ;
syntax Comprehension
= @breakable{results,generators} \set: "{" {Expression ","}+ results "|" {Expression ","}+ generators "}"
| @breakable{from,to,generators} \map: "(" Expression from ":" Expression to "|" {Expression ","}+ generators ")"
| @breakable{results,generators} \list: "[" {Expression ","}+ results "|" {Expression ","}+ generators "]" ;
syntax Variant
= nAryConstructor: Name name "(" {TypeArg ","}* arguments KeywordFormals keywordArguments ")" ;
syntax FunctionDeclaration
= abstract: Tags tags Visibility visibility Signature signature ";"
| @Foldable @breakable{expression} expression: Tags tags Visibility visibility Signature signature "=" Expression expression ";"
| @Foldable @breakable{expression,conditions} conditional: Tags tags Visibility visibility Signature signature "=" Expression expression "when" {Expression ","}+ conditions ";"
| @Foldable \default: Tags tags Visibility visibility Signature signature FunctionBody body ;
lexical PreProtocolChars
= "|" URLChars "\<" ;
lexical NamedRegExp
= "\<" Name "\>"
| [\\] [/ \< \> \\]
| NamedBackslash
| ![/ \< \> \\] ;
syntax ProdModifier
= associativity: Assoc associativity
| \bracket: "bracket"
| \tag: Tag tag;
syntax Toplevel
= givenVisibility: Declaration declaration ;
lexical PostStringChars
= @category="Constant" [\>] StringCharacter* [\"] ;
lexical HexIntegerLiteral
= [0] [X x] [0-9 A-F a-f]+ !>> [0-9 A-Z _ a-z] ;
syntax TypeVar
= free: "&" Name name
| bounded: "&" Name name "\<:" Type bound ;
syntax BasicType
= \value: "value"
| \loc: "loc"
| \node: "node"
| \num: "num"
| \type: "type"
| \bag: "bag"
| \int: "int"
| rational: "rat"
| relation: "rel"
| listRelation: "lrel"
| \real: "real"
| \tuple: "tuple"
| string: "str"
| \bool: "bool"
| \void: "void"
| dateTime: "datetime"
| \set: "set"
| \map: "map"
| \list: "list"
;
lexical Char
= @category="Constant" "\\" [\ \" \' \- \< \> \[ \\ \] b f n r t]
| @category="Constant" ![\ \" \' \- \< \> \[ \\ \]]
| @category="Constant" UnicodeEscape
;
syntax Prod
= reference: ":" Name referenced
| labeled: ProdModifier* modifiers Name name ":" Sym* syms
| unlabeled: ProdModifier* modifiers Sym* syms
| @Foldable associativityGroup: Assoc associativity "(" Prod group ")"
// | TODO add bracket rule for easy readability
> left \all : Prod lhs "|" Prod rhs
> left first : Prod lhs "\>" !>> "\>" Prod rhs
;
syntax DateTimeLiteral
= /*prefer()*/ dateLiteral: JustDate date
| /*prefer()*/ timeLiteral: JustTime time
| /*prefer()*/ dateAndTimeLiteral: DateAndTime dateAndTime ;
lexical PrePathChars
= URLChars "\<" ;
syntax Mapping[&T]
= \default: &T!ifDefinedOtherwise from ":" &T to
;
lexical MidPathChars
= "\>" URLChars "\<" ;
/*
Note that Pattern must closely follow the definitions of Expression because eventually
these two non-terminals will be fused just before AST generation.
*/
syntax Pattern
= \set : "{" {Pattern ","}* elements0 "}"
| \list : "[" {Pattern ","}* elements0 "]"
| qualifiedName : QualifiedName qualifiedName
| multiVariable : QualifiedName qualifiedName "*"
| splice : "*" Pattern argument
| splicePlus : "+" Pattern argument
| negative : "-" Pattern argument
| literal : Literal literal
| \tuple : "\<" {Pattern ","}+ elements "\>"
| typedVariable : Type type Name name
| \map : "(" {Mapping[Pattern] ","}* mappings ")"
| reifiedType : "type" "(" Pattern symbol "," Pattern definitions ")"
| callOrTree : Pattern expression "(" {Pattern ","}* arguments KeywordArguments[Pattern] keywordArguments ")"
> variableBecomes : Name name ":" Pattern pattern
| asType : "[" Type type "]" Pattern argument
| descendant : "/" Pattern pattern
| anti : "!" Pattern pattern
| typedVariableBecomes: Type type Name name ":" Pattern pattern
;
syntax Tag
= @Folded @category="Comment" \default : "@" Name name TagString contents
| @Folded @category="Comment" empty : "@" Name name
| @Folded @category="Comment" expression: "@" Name name "=" Expression expression !>> "@";
syntax ModuleActuals
= \default: "[" {Type ","}+ types "]" ;