-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaScanner.g4
109 lines (95 loc) · 2.1 KB
/
JavaScanner.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
lexer grammar JavaScanner;
options
{
language = 'Java';
superClass = 'Lexer'; //classes DecafScanner extends Lexer;
}
@lexer::header {
package compiler.grammar;
}
// Selectively turns on debug tracing mode.
// You can insert arbitrary Java code into your parser/lexer this way.
@lexer::memebers
{
/** Whether to display debug information. */
private boolean trace = false;
public void setTrace(boolean shouldTrace) {
trace = shouldTrace;
}
@Override
public void traceIn(String rname) throws CharStreamException {
if (trace) {
super.traceIn(rname);
}
}
@Override
public void traceOut(String rname) throws CharStreamException {
if (trace) {
super.traceOut(rname);
}
}
}
// Comments and White Space
WS_ : (' ' | '\t' | '\n' ) -> skip;
BLOCK_COMMENT : '/*' .*? '*/' -> skip ;
LINE_COMMENT : '//' .*? '\n' -> skip ;
// Miscillaneous characters
L_SQUARE : '[';
R_SQUARE : ']';
L_PAREN : '(';
R_PAREN : ')';
L_CURL : '{';
R_CURL : '}';
COMMA : ',';
SEMI_COL : ';';
DOT : '.';
// boolean ops
LT_OP : '<';
GT_OP : '>';
LEQ_OP : '<=';
GEQ_OP : '>=';
EQ_OP : '==';
NEQ_OP : '!=';
AND_OP : '&&';
OR_OP : '||';
NOT_OP : '!';
// arithmatic ops
ADD_OP : '+';
SUB_OP : '-';
MUL_OP : '*';
DIV_OP : '/';
MOD_OP : '%';
// assignment ops
AS_OP : '=';
// reserved words
RES_VOID : 'void';
RES_STRING : 'String';
RES_THIS : 'this';
RES_WHILE : 'while';
RES_BREAK : 'break';
RES_READ_INT : 'readInt';
RES_INT : 'int';
RES_CLASS : 'class';
RES_EXTENDS : 'extends';
RES_IF : 'if';
RES_NEW : 'new';
RES_READ_LINE : 'readLine';
RES_INTERFACE : 'interface';
RES_IMPLEMENTS : 'implements';
RES_ELSE : 'else';
RES_BOOLEAN : 'boolean';
RES_NULL : 'null';
RES_FOR : 'for';
RES_RETURN : 'return';
RES_PRINT : 'print';
RES_CONTINUE : 'continue';
// main tokens
BOOL : 'true' | 'false';
STRING : '"' (ESC | ALLOWED_CHARS )* '"';
INT : DIGIT+;
ID : (ALL_LETTERS | '_')(ALL_LETTERS | DIGIT | '_')*;
// fragments
fragment ESC : ('\\n' | '\\t' | '\\\\' | '\\"' | '\\\'');
fragment ALLOWED_CHARS : ' '..'!' | '#'..'&' | '('..'[' | ']'..'~' ;
fragment DIGIT : '0'..'9';
fragment ALL_LETTERS : 'a'..'z' | 'A'..'Z';