Skip to content

Commit 2d98af2

Browse files
feat: add initial ANTLR grammar for JPP
1 parent 92d66c1 commit 2d98af2

1 file changed

Lines changed: 320 additions & 0 deletions

File tree

Lines changed: 320 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,320 @@
1+
grammar JPPExpression;
2+
// A grammar for the JavaPreprocessor
3+
// https://www.slashdev.ca/javapp/
4+
5+
jppExpression
6+
: logicalOrExpression
7+
;
8+
9+
logicalOrExpression
10+
: logicalAndExpression (OR logicalAndExpression)*
11+
;
12+
13+
logicalAndExpression
14+
: expression (AND expression)*
15+
;
16+
17+
expression
18+
: definedExpression
19+
| undefinedExpression
20+
| comparisonExpression
21+
;
22+
23+
comparisonExpression
24+
: operand ((LT|GT|LEQ|GEQ|EQ|NEQ) operand)?
25+
;
26+
27+
operand
28+
: propertyExpression
29+
| Constant
30+
| StringLiteral+
31+
| unaryOperator Constant
32+
;
33+
34+
definedExpression
35+
: 'defined' '(' Identifier ')'
36+
;
37+
38+
undefinedExpression
39+
: NOT 'defined' '(' Identifier ')'
40+
;
41+
42+
propertyExpression
43+
: '${' Identifier '}'
44+
;
45+
46+
unaryOperator
47+
: U_PLUS
48+
| U_MINUS
49+
;
50+
51+
U_PLUS : '+';
52+
U_MINUS : '-';
53+
54+
AND : 'and';
55+
OR : 'or';
56+
NOT : '!';
57+
58+
LT : '<';
59+
LEQ : '<=';
60+
GT : '>';
61+
GEQ : '>=';
62+
63+
EQ : '==';
64+
NEQ : '!=';
65+
66+
DOT : '.';
67+
68+
Identifier
69+
: ('\\')? ( IdentifierNondigit
70+
| Digit
71+
)+
72+
;
73+
74+
fragment
75+
IdentifierNondigit
76+
: Nondigit
77+
| UniversalCharacterName
78+
//| // other implementation-defined characters...y
79+
;
80+
81+
fragment
82+
Nondigit
83+
: [a-zA-Z_]
84+
;
85+
86+
fragment
87+
Digit
88+
: [0-9]
89+
;
90+
91+
fragment
92+
UniversalCharacterName
93+
: '\\u' HexQuad
94+
| '\\U' HexQuad HexQuad
95+
;
96+
97+
fragment
98+
HexQuad
99+
: HexadecimalDigit HexadecimalDigit HexadecimalDigit HexadecimalDigit
100+
;
101+
102+
Constant
103+
: IntegerConstant
104+
| FloatingConstant
105+
;
106+
107+
fragment
108+
IntegerConstant
109+
: DecimalConstant IntegerSuffix?
110+
| OctalConstant IntegerSuffix?
111+
| HexadecimalConstant IntegerSuffix?
112+
| BinaryConstant
113+
;
114+
115+
fragment
116+
BinaryConstant
117+
: '0' [bB] [0-1]+
118+
;
119+
120+
fragment
121+
DecimalConstant
122+
: NonzeroDigit Digit*
123+
;
124+
125+
fragment
126+
OctalConstant
127+
: '0' OctalDigit*
128+
;
129+
130+
fragment
131+
HexadecimalConstant
132+
: HexadecimalPrefix HexadecimalDigit+
133+
;
134+
135+
fragment
136+
HexadecimalPrefix
137+
: '0' [xX]
138+
;
139+
140+
fragment
141+
NonzeroDigit
142+
: [1-9]
143+
;
144+
145+
fragment
146+
OctalDigit
147+
: [0-7]
148+
;
149+
150+
fragment
151+
HexadecimalDigit
152+
: [0-9a-fA-F]
153+
;
154+
155+
fragment
156+
IntegerSuffix
157+
: UnsignedSuffix LongSuffix?
158+
| UnsignedSuffix LongLongSuffix
159+
| LongSuffix UnsignedSuffix?
160+
| LongLongSuffix UnsignedSuffix?
161+
;
162+
163+
fragment
164+
UnsignedSuffix
165+
: [uU]
166+
;
167+
168+
fragment
169+
LongSuffix
170+
: [lL]
171+
;
172+
173+
fragment
174+
LongLongSuffix
175+
: 'll' | 'LL'
176+
;
177+
178+
fragment
179+
FloatingConstant
180+
: DecimalFloatingConstant
181+
| HexadecimalFloatingConstant
182+
;
183+
184+
fragment
185+
DecimalFloatingConstant
186+
: FractionalConstant ExponentPart? FloatingSuffix?
187+
| DigitSequence ExponentPart FloatingSuffix?
188+
;
189+
190+
fragment
191+
HexadecimalFloatingConstant
192+
: HexadecimalPrefix (HexadecimalFractionalConstant | HexadecimalDigitSequence) BinaryExponentPart FloatingSuffix?
193+
;
194+
195+
fragment
196+
FractionalConstant
197+
: DigitSequence? '.' DigitSequence
198+
| DigitSequence '.'
199+
;
200+
201+
fragment
202+
ExponentPart
203+
: [eE] Sign? DigitSequence
204+
;
205+
206+
fragment
207+
Sign
208+
: [+-]
209+
;
210+
211+
DigitSequence
212+
: Digit+
213+
;
214+
215+
fragment
216+
HexadecimalFractionalConstant
217+
: HexadecimalDigitSequence? '.' HexadecimalDigitSequence
218+
| HexadecimalDigitSequence '.'
219+
;
220+
221+
fragment
222+
BinaryExponentPart
223+
: [pP] Sign? DigitSequence
224+
;
225+
226+
fragment
227+
HexadecimalDigitSequence
228+
: HexadecimalDigit+
229+
;
230+
231+
fragment
232+
FloatingSuffix
233+
: [flFL]
234+
;
235+
236+
237+
fragment
238+
CCharSequence
239+
: CChar+
240+
;
241+
242+
fragment
243+
CChar
244+
: ~['\\\r\n]
245+
| EscapeSequence
246+
;
247+
248+
fragment
249+
EscapeSequence
250+
: SimpleEscapeSequence
251+
| OctalEscapeSequence
252+
| HexadecimalEscapeSequence
253+
| UniversalCharacterName
254+
;
255+
256+
fragment
257+
SimpleEscapeSequence
258+
: '\\' ['"?abfnrtv\\]
259+
;
260+
261+
fragment
262+
OctalEscapeSequence
263+
: '\\' OctalDigit OctalDigit? OctalDigit?
264+
;
265+
266+
fragment
267+
HexadecimalEscapeSequence
268+
: '\\x' HexadecimalDigit+
269+
;
270+
271+
StringLiteral
272+
: EncodingPrefix? '"' SCharSequence? '"'
273+
;
274+
275+
fragment
276+
EncodingPrefix
277+
: 'u8'
278+
| 'u'
279+
| 'U'
280+
| 'L'
281+
;
282+
283+
fragment
284+
SCharSequence
285+
: SChar+
286+
;
287+
288+
fragment
289+
SChar
290+
: ~["\\\r\n]
291+
| EscapeSequence
292+
| '\\\n' // Added line
293+
| '\\\r\n' // Added line
294+
;
295+
296+
Whitespace
297+
: [ \t]+ -> channel(HIDDEN)
298+
;
299+
300+
Newline
301+
: ( '\r' '\n'?
302+
| '\n'
303+
)
304+
-> channel(HIDDEN)
305+
;
306+
307+
BlockComment
308+
: '/*' .*? '*/'
309+
-> channel(HIDDEN)
310+
;
311+
312+
OpenBlockComment
313+
: '/*' ~[*/]*
314+
-> channel(HIDDEN)
315+
;
316+
317+
LineComment
318+
: '//' ~[\r\n]*
319+
-> channel(HIDDEN)
320+
;

0 commit comments

Comments
 (0)