forked from antlr/antlr4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Java.test.stg
301 lines (221 loc) · 7.03 KB
/
Java.test.stg
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
writeln(s) ::= <<outStream.println(<s>);>>
write(s) ::= <<outStream.print(<s>);>>
writeList(s) ::= <<outStream.println(<s; separator="+">);>>
False() ::= "false"
True() ::= "true"
Not(v) ::= "!<v>"
Assert(s) ::= <<assert(<s>);>>
Cast(t,v) ::= "((<t>)<v>)"
Append(a,b) ::= "<a> + <b>"
AppendStr(a,b) ::= <%<Append(a,b)>%>
Concat(a,b) ::= "<a><b>"
AssertIsList(v) ::= "List\<?> __ttt__ = <v>;" // just use static type system
AssignLocal(s,v) ::= "<s> = <v>;"
InitIntMember(n,v) ::= <%int <n> = <v>;%>
InitBooleanMember(n,v) ::= <%boolean <n> = <v>;%>
InitIntVar(n,v) ::= <%<InitIntMember(n,v)>%>
IntArg(n) ::= "int <n>"
VarRef(n) ::= "<n>"
GetMember(n) ::= <%this.<n>%>
SetMember(n,v) ::= <%this.<n> = <v>;%>
AddMember(n,v) ::= <%this.<n> += <v>;%>
MemberEquals(n,v) ::= <%this.<n> == <v>%>
ModMemberEquals(n,m,v) ::= <%this.<n> % <m> == <v>%>
ModMemberNotEquals(n,m,v) ::= <%this.<n> % <m> != <v>%>
DumpDFA() ::= "this.dumpDFA(outStream);"
Pass() ::= ""
StringList() ::= "List\<String>"
BuildParseTrees() ::= "setBuildParseTree(true);"
BailErrorStrategy() ::= <%setErrorHandler(new BailErrorStrategy());%>
ToStringTree(s) ::= <%<s>.toStringTree(this)%>
Column() ::= "this.getCharPositionInLine()"
Text() ::= "this.getText()"
ValEquals(a,b) ::= <%<a>==<b>%>
TextEquals(a) ::= <%this.getText().equals("<a>")%>
PlusText(a) ::= <%"<a>" + this.getText()%>
InputText() ::= "this._input.getText()"
LTEquals(i, v) ::= <%this._input.LT(<i>).getText().equals(<v>)%>
LANotEquals(i, v) ::= <%this._input.LA(<i>)!=<v>%>
TokenStartColumnEquals(i) ::= <%this._tokenStartCharPositionInLine==<i>%>
ImportListener(X) ::= ""
GetExpectedTokenNames() ::= "this.getExpectedTokens().toString(this.tokenNames)"
ImportRuleInvocationStack() ::= ""
RuleInvocationStack() ::= "getRuleInvocationStack()"
LL_EXACT_AMBIG_DETECTION() ::= <<_interp.setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);>>
ParserToken(parser, token) ::= <%<parser>.<token>%>
Production(p) ::= <%<p>%>
Result(r) ::= <%<r>%>
ParserPropertyMember() ::= <<
@members {
boolean Property() {
return true;
}
}
>>
ParserPropertyCall(p, call) ::= "<p>.<call>"
PositionAdjustingLexerDef() ::= ""
PositionAdjustingLexer() ::= <<
@Override
public Token nextToken() {
if (!(_interp instanceof PositionAdjustingLexerATNSimulator)) {
_interp = new PositionAdjustingLexerATNSimulator(this, _ATN, _decisionToDFA, _sharedContextCache);
}
return super.nextToken();
}
@Override
public Token emit() {
switch (_type) {
case TOKENS:
handleAcceptPositionForKeyword("tokens");
break;
case LABEL:
handleAcceptPositionForIdentifier();
break;
default:
break;
}
return super.emit();
}
private boolean handleAcceptPositionForIdentifier() {
String tokenText = getText();
int identifierLength = 0;
while (identifierLength \< tokenText.length() && isIdentifierChar(tokenText.charAt(identifierLength))) {
identifierLength++;
}
if (getInputStream().index() > _tokenStartCharIndex + identifierLength) {
int offset = identifierLength - 1;
getInterpreter().resetAcceptPosition(getInputStream(), _tokenStartCharIndex + offset, _tokenStartLine, _tokenStartCharPositionInLine + offset);
return true;
}
return false;
}
private boolean handleAcceptPositionForKeyword(String keyword) {
if (getInputStream().index() > _tokenStartCharIndex + keyword.length()) {
int offset = keyword.length() - 1;
getInterpreter().resetAcceptPosition(getInputStream(), _tokenStartCharIndex + offset, _tokenStartLine, _tokenStartCharPositionInLine + offset);
return true;
}
return false;
}
@Override
public PositionAdjustingLexerATNSimulator getInterpreter() {
return (PositionAdjustingLexerATNSimulator)super.getInterpreter();
}
private static boolean isIdentifierChar(char c) {
return Character.isLetterOrDigit(c) || c == '_';
}
protected static class PositionAdjustingLexerATNSimulator extends LexerATNSimulator {
public PositionAdjustingLexerATNSimulator(Lexer recog, ATN atn,
DFA[] decisionToDFA,
PredictionContextCache sharedContextCache)
{
super(recog, atn, decisionToDFA, sharedContextCache);
}
protected void resetAcceptPosition(CharStream input, int index, int line, int charPositionInLine) {
input.seek(index);
this.line = line;
this.charPositionInLine = charPositionInLine;
consume(input);
}
}
>>
BasicListener(X) ::= <<
@parser::members {
public class LeafListener extends TBaseListener {
public void visitTerminal(TerminalNode node) {
outStream.println(node.getSymbol().getText());
}
}
}
>>
WalkListener(s) ::= <<
ParseTreeWalker walker = new ParseTreeWalker();
walker.walk(new LeafListener(), <s>);
>>
TreeNodeWithAltNumField(X) ::= <<
@parser::members {
public static class MyRuleNode extends ParserRuleContext {
public int altNum;
public MyRuleNode(ParserRuleContext parent, int invokingStateNumber) {
super(parent, invokingStateNumber);
}
@Override public int getAltNumber() { return altNum; }
@Override public void setAltNumber(int altNum) { this.altNum = altNum; }
}
}
>>
TokenGetterListener(X) ::= <<
@parser::members {
public class LeafListener extends TBaseListener {
public void exitA(TParser.AContext ctx) {
if (ctx.getChildCount()==2)
outStream.printf("%s %s %s\n",ctx.INT(0).getSymbol().getText(),
ctx.INT(1).getSymbol().getText(),ctx.INT());
else
outStream.println(ctx.ID().getSymbol());
}
}
}
>>
RuleGetterListener(X) ::= <<
@parser::members {
public class LeafListener extends TBaseListener {
public void exitA(TParser.AContext ctx) {
if (ctx.getChildCount()==2) {
outStream.printf("%s %s %s\n",ctx.b(0).start.getText(),
ctx.b(1).start.getText(),ctx.b().get(0).start.getText());
} else
outStream.println(ctx.b(0).start.getText());
}
}
}
>>
LRListener(X) ::= <<
@parser::members {
public class LeafListener extends TBaseListener {
public void exitE(TParser.EContext ctx) {
if (ctx.getChildCount()==3) {
outStream.printf("%s %s %s\n",ctx.e(0).start.getText(),
ctx.e(1).start.getText(), ctx.e().get(0).start.getText());
} else
outStream.println(ctx.INT().getSymbol().getText());
}
}
}
>>
LRWithLabelsListener(X) ::= <<
@parser::members {
public class LeafListener extends TBaseListener {
public void exitCall(TParser.CallContext ctx) {
outStream.printf("%s %s\n",ctx.e().start.getText(),ctx.eList());
}
public void exitInt(TParser.IntContext ctx) {
outStream.println(ctx.INT().getSymbol().getText());
}
}
}
>>
DeclareContextListGettersFunction() ::= <<
void foo() {
SContext s = null;
List\<? extends AContext> a = s.a();
List\<? extends BContext> b = s.b();
}
>>
Declare_foo() ::= <<
public void foo() {outStream.println("foo");}
>>
Invoke_foo() ::= "foo();"
Declare_pred() ::= <<boolean pred(boolean v) {
outStream.println("eval="+v);
return v;
}
>>
Invoke_pred(v) ::= <<this.pred(<v>)>>
ParserTokenType(t) ::= "Parser.<t>"
ContextRuleFunction(ctx, rule) ::= "<ctx>.<rule>"
ContextListFunction(ctx, rule) ::= "<ctx>.<rule>()"
StringType() ::= "String"
ContextMember(ctx, member) ::= "<ctx>.<member>"
SubContextLocal(ctx, subctx, local) ::= "<ctx>.<subctx>.<local>"
SubContextMember(ctx, subctx, member) ::= "<ctx>.<subctx>.<member>"