-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessingParser.g4
More file actions
211 lines (181 loc) · 4.75 KB
/
ProcessingParser.g4
File metadata and controls
211 lines (181 loc) · 4.75 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
/*
Derived from Processing preprocessor code by Jakub Valtar and Florian Jenett.
Copyright (c) 2021 Gagik Amaryan
*/
parser grammar ProcessingParser;
import JavaParser;
options { tokenVocab=ProcessingLexer; }
// main entry point, select sketch type
sketch
: staticMode
| activeMode
;
blockStatement
: localVariableDefinition
| localVariableDeclaration ';'
| statement
| localTypeDeclaration
;
// simplified variable definition for the purpose
// of float declarations only for now
localVariableDefinition
: typeType IDENTIFIER '=' expression ';'
;
// static mode, will run once (no function definitions)
staticMode
: importDeclaration* blockStatement* importDeclaration* blockStatement* EOF
;
// active mode, has function definitions
activeMode
: importDeclaration* classBodyDeclaration* importDeclaration* classBodyDeclaration* EOF
;
variableDeclaratorId
: warnTypeAsVariableName
| Identifier ('[' ']')*
;
// bug #93
// https://github.com/processing/processing/issues/93
// prevent from types being used as variable names
warnTypeAsVariableName
: primitiveType ('[' ']')* {
notifyErrorListeners("Type names are not allowed as variable names: "+$primitiveType.text);
}
;
expression
: FRAMECOUNT
// | additionOperation
| primary
| expression (ADD|SUBTRACT|MUL|DIV) expression
| expression '.' Identifier
| expression '.' 'this'
| expression '.' 'new' nonWildcardTypeArguments? innerCreator
| expression '.' 'super' superSuffix
| expression '.' explicitGenericInvocation
| expression '[' expression ']'
| mathFunction
| apiFunction
| expression '(' expressionList? ')'
| 'new' creator
| functionWithPrimitiveTypeName
| '(' typeType ')' expression
| expression ('++' | '--')
| ('+'|SUBTRACT|'++'|'--') expression
| ('~'|'!') expression
| expression ('*'|'/'|'%') expression
| expression (ADD|SUBTRACT) expression
| expression ('<' '<' | '>' '>' '>' | '>' '>') expression
| expression ('<=' | '>=' | '>' | '<') expression
| expression 'instanceof' typeType
| expression ('==' | '!=') expression
| expression '&' expression
| expression '^' expression
| expression '|' expression
| expression '&&' expression
| expression '||' expression
| expression '?' expression ':' expression
| warnTypeAsVariableName
| <assoc=right> expression
( '='
| '+='
| '-='
| '*='
| '/='
| '&='
| '|='
| '^='
| '>>='
| '>>>='
| '<<='
| '%='
)
expression
;
// additionOperation:
// expression (ADD | SUBTRACT) expression;
mathFunction
: mathSinCos
| ABS_FUNC '(' expression ')'
;
mathSinCos: (SIN | COS) '(' expression ')';
// catch special API function calls that we are interessted in
apiFunction
: apiDraw
| apiColor
;
apiDraw
: circleFunction
| drawFourDecimal
;
apiColor
: colorFunction '(' colorLiteral ')'
;
colorFunction: (FILL | BACKGROUND | STROKE);
circleFunction : CIRCLE '(' expression ',' expression ',' expression ')';
drawFourDecimal : drawFourDecimalShape '(' expression ',' expression ',' expression ',' expression ')';
// draw functions which take 4 decimal arguments
drawFourDecimalShape: RECT
| ELLIPSE
| LINE;
position: DECIMAL_LITERAL
| FRAMECOUNT;
drawShape
: CIRCLE
| RECT
| ELLIPSE
;
apiSize
: 'size' '(' expression ',' expression ( ',' expression )? ')'
;
// these are primitive type names plus "()"
// "color" is a special Processing primitive (== int)
functionWithPrimitiveTypeName
: ( 'boolean'
| 'byte'
| 'char'
| 'float'
| 'int'
| COLOR
) '(' expressionList ')'
;
// adding support for "color" primitive
primitiveType: javaPrimitiveType
;
// original Java.g4 primitiveType
javaPrimitiveType
: 'boolean'
| 'char'
| 'byte'
| 'short'
| 'int'
| 'long'
| 'float'
| 'double'
;
// added HexColorLiteral
literal
: colorLiteral
| integerLiteral
| floatLiteral
| CHAR_LITERAL
| STRING_LITERAL
| BOOL_LITERAL
| 'null'
;
colorLiteral
: hexColorLiteral
| singleColorLiteral
| rgbColorLiteral
;
hexColorLiteral : '#' hexColorValue;
hexColorValue: HEX_COLOR_VALUE;
singleColorLiteral
: integerLiteral
;
rgbColorLiteral
: integerLiteral ',' integerLiteral ',' integerLiteral
;
// As parser rule so this produces a separate listener
// for us to alter its value.
// hexColorLiteral
// : HEX_COLOR_LITERAL
// ;