forked from processing/processing4
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProcessing.g4
More file actions
144 lines (120 loc) · 3.42 KB
/
Processing.g4
File metadata and controls
144 lines (120 loc) · 3.42 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
/**
* Based on Java 1.7 grammar for ANTLR 4, see Java.g4
*
* - changes main entry point to reflect sketch types 'static' | 'active'
* - adds support for type converter functions like "int()"
* - adds pseudo primitive type "color"
* - adds HTML hex notation with hash symbol: #ff5522
* - allow color to appear as part of qualified names (like in imports)
*/
grammar Processing;
@lexer::members {
public static final int WHITESPACE = 1;
public static final int COMMENTS = 2;
}
// import Java grammar
import JavaParser;
// main entry point, select sketch type
processingSketch
: staticProcessingSketch
| javaProcessingSketch
| activeProcessingSketch
// | warnMixedModes
;
// java mode, is a compilation unit
javaProcessingSketch
: packageDeclaration? importDeclaration* typeDeclaration+ EOF
;
// No method declarations, just statements
staticProcessingSketch
: (importDeclaration | blockStatement | typeDeclaration)* EOF
;
// active mode, has function definitions
activeProcessingSketch
: (importDeclaration | classBodyDeclaration)* EOF
;
// User incorrectly mixing modes. Included to allow for kind error message.
warnMixedModes
: (importDeclaration | classBodyDeclaration | blockStatement)* blockStatement classBodyDeclaration (importDeclaration | classBodyDeclaration | blockStatement)*
| (importDeclaration | classBodyDeclaration | blockStatement)* classBodyDeclaration blockStatement (importDeclaration | classBodyDeclaration | blockStatement)*
;
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);
}
;
// catch special API function calls that we are interested in
methodCall
: functionWithPrimitiveTypeName
| IDENTIFIER '(' expressionList? ')'
| THIS '(' expressionList? ')'
| SUPER '(' expressionList? ')'
;
// 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
: BOOLEAN
| CHAR
| BYTE
| SHORT
| INT
| LONG
| FLOAT
| DOUBLE
| colorPrimitiveType
;
colorPrimitiveType
: 'color'
;
qualifiedName
: (IDENTIFIER | colorPrimitiveType) ('.' (IDENTIFIER | colorPrimitiveType))*
;
// added HexColorLiteral
literal
: integerLiteral
| floatLiteral
| CHAR_LITERAL
| stringLiteral
| BOOL_LITERAL
| NULL_LITERAL
| hexColorLiteral
;
// As parser rule so this produces a separate listener
// for us to alter its value.
hexColorLiteral
: HexColorLiteral
;
// add color literal notations for
// #ff5522
HexColorLiteral
: '#' (HexDigit HexDigit)? HexDigit HexDigit HexDigit HexDigit HexDigit HexDigit
;
// hide but do not remove whitespace and comments
WS : [ \t\r\n\u000C]+ -> channel(1)
;
COMMENT
: '/*' .*? '*/' -> channel(2)
;
LINE_COMMENT
: '//' ~[\r\n]* -> channel(2)
;
CHAR_LITERAL
: '\'' (~['\\\r\n] | EscapeSequence)* '\'' // A bit nasty but let JDT tackle invalid chars
;