-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFoxySheep.g4
executable file
·218 lines (189 loc) · 8.52 KB
/
FoxySheep.g4
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
grammar FoxySheep;
import FoxySheepLexerRules;
// PARSER RULES
prog
: expr (NEWLINE+ expr?)*
;
expr
: numberLiteral #Number
| StringLiteral #StringLiteral
| LPAREN expr RPAREN #Parentheses //Grouping with parentheses
| LBRACE expressionList RBRACE #List //List expression
| LANGLE expressionList RANGLE #AngleBracket
| LFLOOR expr RFLOOR #Floor
| LCEILING expr RCEILING #Ceiling
| LBRACKETINGBAR expressionList RBRACKETINGBAR #BracketingBar
| LDOUBLEBRACKETINGBAR expressionList RDOUBLEBRACKETINGBAR #DoubleBracketingBar
| expr DOUBLECOLON StringLiteral (DOUBLECOLON StringLiteral)? #Message // MessageName[]
| slotExpression #Slot //"forms containing #"
| outExpression #Out //"forms containing %"
// | pattern #PatternForm // "forms containing _"
| symbol? (TRIPPLEBLANK | DOUBLEBLANK | BLANK) expr? #PatternBlanks
| symbol? BLANKDOT #PatternBlankDot
| symbol #SymbolLiteral
| DOUBLELESS StringLiteral #Get
//TODO: Finish and test box related grammar.
// | BoxLeftBoxParenthesis box* BoxRightBoxParenthesis #BoxParen
| expr QUESTIONMARK expr #PatternTest
| expr LBRACKET expressionList RBRACKET #HeadExpression
| expr accessExpression #Accessor //accessor, i.e. a[[1]]
| BoxConstructor expr #BoxConstructor
| expr (DOUBLEPLUS | DOUBLEMINUS) #Increment //Increment or Decrement
| (DOUBLEPLUS | DOUBLEMINUS) expr #PreIncrement //PreIncrement or PreDecrement
| expr ATASTERISK expr #Composition
| expr SLASHASTERISK expr #RightComposition
| <assoc=right> expr AT expr #Prefix //Application: f@x === f[x]
| expr TILDE expr TILDE expr #Infix //application: x~f~y === f[x, y]
| <assoc=right> expr (MAP | MAPALL | DOUBLEAT | TRIPPLEAT) expr #MapApply //Map[], MapAll[], Apply[e, e], Apply[e, e, {1}]
| expr (BANG | DOUBLEBANG) #Factorial //Factorial (!) and Double Factorial (!!)
| expr (CONJUGATE | TRANSPOSE | CONJUGATETRANSPOSE | HERMITIANCONJUGATE) #Conjugate
| expr SINGLEQUOTE+ #Derivative //Derivative[n][expr] where n counts the quotes.
| expr LESSGREATER expr #StringJoin
| <assoc=right> expr CARET expr #Power
| INTEGRAL expr DIFFERENTIALD expr #Integrate
| <assoc=right> DEL expr #Del
| <assoc=right> SQUARE expr #Square
| expr SMALLCIRCLE expr #SmallCircle
| expr CIRCLEDOT expr #CircleDot
| expr DOUBLEASTERISK expr #NonCommutativeMultiply
| expr CROSS expr #Cross
| expr DOT expr #Dot
| (MINUS | PLUS | PLUSMINUS | MINUSPLUS) expr #UnaryPlusMinus
| expr (SLASH | DIVIDE) expr #Divide
| expr RAWBACKSLASH expr #Backslash
| expr DIAMOND expr #Diamond
| expr WEDGE expr #Wedge
| expr VEE expr #Vee
| expr CIRCLETIMES expr #CircleTimes
| expr CENTERDOT expr #CenterDot
//Implicit Multiplication
// | expr expr #MultiplyImplicit
| expr MultiplicationSymbol? expr #Times
| expr STAR expr #Star
| expr VERTICALTILDE expr #VerticalTilde
| expr COPRODUCT expr #Coproduct
| expr CAP expr #Cap
| expr CUP expr #Cup
| expr CIRCLEPLUS expr #CirclePlus
| expr CIRCLEMINUS expr #CircleMinus
| expr (BINARYPLUS | BINARYMINUS | BINARYPLUSMINUS | BINARYMINUSPLUS) expr #PlusOp
| expr INTERSECTION expr #Intersection
| expr UNION expr #Union
//Span expressions.
/*
* There is a tricky context sensitivity with the following two rules. Consider the
* expression "2 ;; 10 ;; 3". Since implicit multiplication has higher precedence
* than span, this expression wants to parse as Times[2, Span[All, 10, 3]]. We
* solve this problem in the lexer by detecting when the ";;" follows a complete
* expression (or another ";;"), and if it doesn't, sending a different token to
* the parser. Hence the two tokens SPANSEMICOLONS and DOUBLESEMICOLON both
* representing ";;".
*
* Note that the following rules do not accurately represent permissible Span
* expressions in Wolfram Language (though they accept and reject the right stuff).
* The parse tree is rewritten post-parse to obtain the correct tree for Wolfram
* Language Span expressions, because the correct parse trees are difficult to get
* using automated parser generators.
*
*/
| expr DOUBLESEMICOLON expr? #SpanA
| SPANSEMICOLONS expr? (DOUBLESEMICOLON expr?)* #SpanB
//Comparison
| expr (EqualSymbol | NotEqualSymbol | GreaterEqualSymbol | LessEqualSymbol | GREATER | LESS) expr #Comparison
| expr (VERTICALBAR | NOTVERTICALBAR | DOUBLEVERTICALBAR | NOTDOUBLEVERTICALBAR) expr #VerticalBar
| expr (TRIPPLEEQUAL | EQUALBANGEQUAL) expr #Same //SameQ, UnsameQ
| expr (ELEMENT | NOTELEMENT | SUBSET | SUPERSET) expr #SetContainment //Set membership and containment
// ForAll and friends require subscripting, i.e. they are not text-based commands.
// | expr (FORALL | EXISTS | NOTEXISTS) expr //Logical quantifiers
| (BANG | NOT) expr #Not //Logical not
| expr (DOUBLEAMP | AND | NAND) expr #And //Logical And/Nand
| expr (XOR | XNOR) expr #Xor //Logical Xor/XNor
| expr (OR | NOR | DOUBLEBAR) expr #Or //Logical Or/Nor
| expr LRDOUBLEARROW expr #Equivalent //Logical equivalent
| <assoc=right> expr (RDOUBLEARROW | LCONTAINS) expr #Implies //Logical implies
| <assoc=right> expr (RIGHTTEE | DOUBLERIGHTTEE) expr #RightTee //RightTee, DoubleRightTee
| expr (LEFTTEE | DOUBLELEFTTEE | UPTEE | DOWNTEE) expr #Tee //LeftTee, etc.
| <assoc=right> expr SUCHTHAT expr #SuchThat
| expr (DOUBLEDOT | TRIPPLEDOT) #Repeated //Repeated[] and RepeatedNull[]
| expr BAR expr #Alternatives
| symbol RAWCOLON expr #PatternExp //Pattern[] and Optional[]
| expr RAWCOLON expr #Optional //Pattern[] and Optional[]
| expr DOUBLETILDE expr #StringExpression
| expr SLASHSEMI expr #Condition
| <assoc=right> expr (MINUSGREATER | RARROW | COLONGREATER | COLONARROW) expr #Rule //Rule and RuleDelayed
| expr (SLASHDOT | DOUBLESLASHDOT) expr #ReplaceAll //ReplaceAll[]/ReplaceRepeated[]
| <assoc=right> expr (PLUSEQUAL | MINUSEQUAL | ASTERISKEQUAL | SLASHEQUAL) expr #OpEquals //var += 1
| expr AMP #Function //Function[expr]
| expr COLON expr #Colon //Undefined flat infix, not to be confused with pattern which uses RAWCOLON.
| expr DOUBLESLASH expr #Postfix //Postfix "headed" exression, i.e. expr//InputForm
| expr VERTICALSEPARATOR expr #VerticalSeparator
| <assoc=right> expr THEREFORE expr #Therefore
| expr BECAUSE expr #Because
| expr EQUAL DOT #Unset
| <assoc=right> expr (EQUAL | COLONEQUAL | CARETEQUAL | CARETCOLONEQUAL | FUNCTIONARROW) expr #Set //Set[], SetDelayed[], etc.
| <assoc=right> symbol SLASHCOLON expr (EQUAL | COLONEQUAL) expr #TagSet //TagSetDelayed
| symbol SLASHCOLON expr EQUAL DOT #TagUnset
| expr (DOUBLEGREATER | TRIPPLEGREATER) StringLiteral #Put //Put/PutAppend
| expr SEMICOLON expr? #CompoundExpression
;
//Symbols and Contexts
symbol
: context? Name #ContextName
;
context
: BACKQUOTE? Name BACKQUOTE #SimpleContext
| Name BACKQUOTE Name BACKQUOTE #CompoundContext
;
//Numbers
numberLiteral
: DIGITS NumberInBase numberLiteralPrecision? numberLiteralExponent? #NumberBaseN // Number in any base.
| (DIGITS | DecimalNumber) numberLiteralPrecision? numberLiteralExponent? #NumberBaseTen // Number in base ten.
;
numberLiteralPrecision
: DOUBLEBACKQUOTE (DecimalNumber | DIGITS)
| BACKQUOTE (DecimalNumber | DIGITS)?
;
numberLiteralExponent
: (ASTERISKCARET (PLUS|MINUS)? DIGITS)
;
//Slot[] and Out[]
outExpression
: PERCENTDIGITS #OutNumbered
| PERCENTS #OutUnnumbered
;
slotExpression
: HASHDIGITS #SlotDigits
| HASHStringLiteral #SlotNamed
| DOUBLEHASHDIGITS #SlotSequenceDigits
| DOUBLEHASH #SlotSequence
| HASH #SlotUnnamed
;
// List-like expressions
expressionList // Can be empty.
: expr? (COMMA expr?)* #ExpressionListed
;
// Accessor is used to access parts of an expression, i.e. a[[1]]
accessExpression
: LBRACKET LBRACKET expressionList RBRACKET RBRACKET #AccessExpressionA
| LBARBRACKET expressionList RBARBRACKET #AccessExpressionB
;
//box
// : expr
// | BoxLeftBoxParenthesis box* BoxRightBoxParenthesis
//
// | box BoxUnderscript box BoxOtherscript box // Underoverscript[]
// | box BoxOverscript box BoxOtherscript box // Underoverscript[]
// | <assoc=right> box (BoxOverscript | BoxUnderscript) box // Overscript[]/Underscript
//
// | box BoxSubscript box BoxOtherscript box // Power[Subscript[]]
// | box BoxSubscript box //Subscript
// | InterpretedBox BoxLeftBoxParenthesis box+ BoxRightBoxParenthesis
//
// | box BoxOverscript box
// | box BoxUnderscript box BoxOtherscript box
// | box BoxUnderscript box
// | box BoxFraction box
// | BoxSqrt box BoxOtherscript box
// | BoxSqrt box
// | box FormBox box
// ;