-
Notifications
You must be signed in to change notification settings - Fork 1
/
ANTLR2Peg.java
431 lines (358 loc) · 12.1 KB
/
ANTLR2Peg.java
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
import java.util.*;
public class ANTLR2Peg extends ANTLRv4ParserBaseListener {
ParseTreeProperty<String> prog = new ParseTreeProperty<String>();
String fileExt;
String pre;
String pos;
String ruleId;
ANTLR2Peg (String fileExt, String ruleId) {
super();
this.fileExt = fileExt;
this.ruleId = ruleId;
setPre();
setPos();
}
void setPre () {
pre = "local Parser = require 'pegparser.parser'\n"
+ "local Pretty = require 'pegparser.pretty'\n"
+ "local Util = require'pegparser.util'\n"
+ "local Cfg2Peg = require'pegparser.cfg2peg'\n"
+ "local Coder = require'pegparser.coder'\n\n"
+ "local s = [===[\n";
}
void setPos () {
pos = "]===]\n\n"
+ "local g = Parser.match(s)\n"
+ "assert(g)\n"
+ "pretty = Pretty.new()\n"
+ "print(pretty:printg(g, nil, true))\n"
+ "local c2p = Cfg2Peg.new(g)\n"
+ "c2p:setUseUnique(false)\n"
+ "c2p:setUsePrefix(true)\n"
+ "c2p:convert('" + this.ruleId + "', true)\n"
+ "local peg = c2p.peg\n"
+ "print(pretty:printg(peg, nil, true))\n\n"
+ "local p = Coder.makeg(peg)\n"
+ "local dir = Util.getPath(arg[0])\n"
+ "Util.testYes(dir .. '/examples/', '" + this.fileExt + "', p)\n"
+ "Util.testYes(dir .. '/grammarinator/tests_01/', '" + this.fileExt + "', p)\n"
+ "Util.testYes(dir .. '/grammarinator/tests_02/', '" + this.fileExt + "', p)\n"
+ "Util.testYes(dir .. '/grammarinator/tests_03/', '" + this.fileExt + "', p)\n"
+ "Util.testYes(dir .. '/grammarinator/tests_04/', '" + this.fileExt + "', p)\n"
+ "Util.testYes(dir .. '/outputNoTk/yes/', '" + this.fileExt + "', p)\n"
+ "Util.testNo(dir .. '/outputNoTk/no/', '" + this.fileExt + "', p)\n";
}
final String assertMsg = "Opção não suportada pelo pegparser";
public void setCode(ParseTree node, String code) {
prog.put(node, code);
}
public String getCode(ParseTree node) {
return prog.get(node);
}
public void copyCode (ParseTree node1, ParseTree node2) {
setCode(node1, getCode(node2));
}
public void copyText (ParseTree node1, ParseTree node2) {
setCode(node1, node2.getText());
}
public String unquoteString (String code) {
return code.substring(1, code.length() - 1);
}
public void assertFeature (boolean exp, ParserRuleContext ctx) {
assert exp : "Linha " + ctx.start.getLine() + ": " + assertMsg;
}
@Override
public void exitGrammarSpec(ANTLRv4Parser.GrammarSpecContext ctx) {
//grammarDecl prequelConstruct* rules modeSpec* EOF
String code = pre;
pre += getCode(ctx.grammarDecl());
code += getCode(ctx.rules());
code += pos;
setCode(ctx, code);
}
@Override
public void exitGrammarDecl(ANTLRv4Parser.GrammarDeclContext ctx) {
//grammarType identifier SEMI
copyText(ctx, ctx.identifier());
}
// grammarType, prequelConstruct, optionsSpec -> do nothing
// option, optionValue, delegateGrammars -> do nothing
// delegateGrammar -> do nothing
@Override public void exitTokensSpec(ANTLRv4Parser.TokensSpecContext ctx) {
assertFeature(false, ctx);
}
@Override public void exitChannelsSpec(ANTLRv4Parser.ChannelsSpecContext ctx) {
assertFeature(false, ctx);
}
//idList -> referenced only by tokensSpec and channelsSpec
@Override
public void exitAction_(ANTLRv4Parser.Action_Context ctx) {
assertFeature(false, ctx);
}
//actionScopeName, actionBlock -> referenced only by action_
@Override
public void exitArgActionBlock(ANTLRv4Parser.ArgActionBlockContext ctx) {
assertFeature(false, ctx);
}
@Override public void exitModeSpec(ANTLRv4Parser.ModeSpecContext ctx) {
assertFeature(false, ctx);
}
@Override
public void exitRules(ANTLRv4Parser.RulesContext ctx) {
StringBuilder buf = new StringBuilder();
for (ANTLRv4Parser.RuleSpecContext rsctx : ctx.ruleSpec()) {
if (rsctx.parserRuleSpec() != null) {
//System.out.println("syn " + getCode(rsctx.parserRuleSpec()));
buf.append(getCode(rsctx.parserRuleSpec()) + "\n");
} else {
//System.out.println("lex: " + rsctx.lexerRuleSpec().getText());
buf.append(getCode(rsctx.lexerRuleSpec()) + "\n");
}
//buf.append(getCode(rsctx) + "\n");
//buf.append(getCode(rsctx.getChild(0)) + "\n");
}
setCode(ctx, buf.toString());
}
//ruleSpec -> handled by exitRules
@Override
public void exitParserRuleSpec(ANTLRv4Parser.ParserRuleSpecContext ctx) {
String code = ctx.RULE_REF().getText() + " <- ";
code += getCode(ctx.ruleBlock());
setCode(ctx, code);
}
@Override
public void exitExceptionGroup(ANTLRv4Parser.ExceptionGroupContext ctx) {
assertFeature(ctx.getChildCount() == 0, ctx);
}
//exceptionHandler, finallyClause -> referenced only by exceptionGroup
@Override
public void exitRulePrequel(ANTLRv4Parser.RulePrequelContext ctx) {
assertFeature(false, ctx);
}
@Override
public void exitRuleReturns(ANTLRv4Parser.RuleReturnsContext ctx) {
assertFeature(false, ctx);
}
@Override
public void exitThrowsSpec(ANTLRv4Parser.ThrowsSpecContext ctx) {
assertFeature(false, ctx);
}
@Override
public void exitLocalsSpec(ANTLRv4Parser.LocalsSpecContext ctx) {
assertFeature(false, ctx);
}
@Override
public void exitRuleAction(ANTLRv4Parser.RuleActionContext ctx) {
assertFeature(false, ctx);
}
//ruleModifiers, ruleModifier
@Override
public void exitRuleBlock(ANTLRv4Parser.RuleBlockContext ctx) {
setCode(ctx, getCode(ctx.ruleAltList()));
}
@Override
public void exitRuleAltList(ANTLRv4Parser.RuleAltListContext ctx) {
int n = ctx.getChildCount();
String code = getCode(ctx.getChild(0));
for (int i = 2; i < n; i += 2) {
code += " / " + getCode(ctx.getChild(i));
}
setCode(ctx, code);
}
@Override
public void exitLabeledAlt(ANTLRv4Parser.LabeledAltContext ctx) {
setCode(ctx, getCode(ctx.alternative()));
}
@Override
public void exitLexerRuleSpec(ANTLRv4Parser.LexerRuleSpecContext ctx) {
String code = "";
if (ctx.FRAGMENT() != null) {
code = ctx.FRAGMENT().getText() + " ";
}
code += ctx.TOKEN_REF().getText() + " <- ";
code += getCode(ctx.lexerRuleBlock());
setCode(ctx, code);
}
@Override
public void exitLexerRuleBlock(ANTLRv4Parser.LexerRuleBlockContext ctx) {
copyCode(ctx, ctx.lexerAltList());
}
@Override
public void exitLexerAltList(ANTLRv4Parser.LexerAltListContext ctx) {
int n = ctx.getChildCount();
String code = getCode(ctx.getChild(0));
for (int i = 2; i < n; i += 2) {
code += " / " + getCode(ctx.getChild(i));
}
setCode(ctx, code);
}
@Override
public void exitLexerAlt(ANTLRv4Parser.LexerAltContext ctx) {
if (ctx.lexerElements() == null) {
setCode(ctx, "''");
return;
}
copyCode(ctx, ctx.lexerElements());
}
@Override
public void exitLexerElements(ANTLRv4Parser.LexerElementsContext ctx) {
int n = ctx.getChildCount();
if (n == 0) {
setCode(ctx, "''");
return;
}
String code = getCode(ctx.getChild(0));
for (int i = 1; i < n; i ++) {
code += " " + getCode(ctx.getChild(i));
}
setCode(ctx, code);
}
@Override
public void exitLexerElement(ANTLRv4Parser.LexerElementContext ctx) {
String code = getCode(ctx.getChild(0));
if (ctx.getChildCount() > 1) { //copy the text of ebnfsuffix or QUESTION
code += ctx.getChild(1).getText();
}
setCode(ctx, code);
}
@Override
public void exitLabeledLexerElement(ANTLRv4Parser.LabeledLexerElementContext ctx) {
//identifier (ASSIGN | PLUS_ASSIGN) (lexerAtom | lexerBlock)
assertFeature(false, ctx);
}
@Override
public void exitLexerBlock(ANTLRv4Parser.LexerBlockContext ctx) {
setCode(ctx, "(" + getCode(ctx.lexerAltList()) + ")");
}
@Override public void exitLexerCommands(ANTLRv4Parser.LexerCommandsContext ctx) {
int n = ctx.getChildCount();
String cmd = ctx.getChild(1).getText();
//System.out.println("n = " + n + " cmd = " + cmd + (cmd.equals("skip")));
assertFeature(n == 2 && ctx.getChild(1).getText().equals("skip"), ctx);
}
//lexerCommand, lexerCommandName, lexerCommandExpr -> referenced only through lexerCommands
@Override
public void exitAltList(ANTLRv4Parser.AltListContext ctx) {
int n = ctx.getChildCount();
String code = getCode(ctx.getChild(0));
for (int i = 2; i < n; i += 2) {
code += " / " + getCode(ctx.getChild(i));
}
setCode(ctx, code);
}
@Override
public void exitAlternative(ANTLRv4Parser.AlternativeContext ctx) {
if (ctx.getChildCount() == 0) { //matched the empty alternative
setCode(ctx, "''");
return;
}
StringBuilder buf = new StringBuilder();
for (ANTLRv4Parser.ElementContext ectx : ctx.element()) {
buf.append(getCode(ectx) + " ");
}
setCode(ctx, buf.toString());
}
@Override
public void exitElement(ANTLRv4Parser.ElementContext ctx) {
String code = "";
if (ctx.labeledElement() != null) {
assertFeature(false, ctx.labeledElement());
} else if (ctx.atom() != null) {
code = getCode(ctx.atom());
if (ctx.ebnfSuffix() != null) {
code += ctx.ebnfSuffix().getText();
}
} else if (ctx.ebnf() != null) {
code = getCode(ctx.ebnf());
} else { //action block
assertFeature(false, ctx.actionBlock());
}
setCode(ctx, code);
}
//labeledElement -> referenced only by element
@Override
public void exitEbnf(ANTLRv4Parser.EbnfContext ctx) {
String code = getCode(ctx.block());
if (ctx.blockSuffix() != null) {
code += ctx.blockSuffix().getText();
}
setCode(ctx, code);
}
//blockSuffix, ebnfSuffix -> getText
@Override
public void exitLexerAtom(ANTLRv4Parser.LexerAtomContext ctx) {
//characterRange | terminal | notSet | LEXER_CHAR_SET | DOT elementOptions?
if (ctx.DOT() != null) { //DOT elementOptions?
assertFeature(ctx.elementOptions() == null, ctx);
copyText(ctx, ctx.DOT());
} else if (ctx.LEXER_CHAR_SET() != null) {
copyText(ctx, ctx.LEXER_CHAR_SET());
} else {
copyCode(ctx, ctx.getChild(0));
}
}
@Override
public void exitAtom(ANTLRv4Parser.AtomContext ctx) {
if (ctx.DOT() != null) { //DOT elementOptions?
assertFeature(ctx.elementOptions() == null, ctx.elementOptions());
copyText(ctx, ctx.DOT());
} else {
copyCode(ctx, ctx.getChild(0));
}
}
@Override
public void exitNotSet(ANTLRv4Parser.NotSetContext ctx) {
String code = "(!" + getCode(ctx.getChild(1)) + " .)";
setCode(ctx, code);
}
@Override
public void exitBlockSet(ANTLRv4Parser.BlockSetContext ctx) {
int n = ctx.getChildCount();
String code = "(" + getCode(ctx.getChild(1));
for (int i = 3; i < n; i += 2) {
code += " / " + getCode(ctx.getChild(i));
}
code += ")";
setCode(ctx, code);
}
@Override
public void exitSetElement(ANTLRv4Parser.SetElementContext ctx) {
//setElement <- TOKEN_REF elementOptions? | STRING_LITERAL elementOptions?
// | characterRange | LEXER_CHAR_SET
assertFeature(ctx.getChildCount() == 1, ctx);
//assertFeature(ctx.TOKEN_REF() != null, ctx);
if (ctx.characterRange() != null) {
copyCode(ctx, ctx.getChild(0));
} else {
copyText(ctx, ctx.getChild(0));
}
}
@Override public void exitBlock(ANTLRv4Parser.BlockContext ctx) {
setCode(ctx, "(" + getCode(ctx.altList()) + ")");
}
@Override public void exitRuleref(ANTLRv4Parser.RulerefContext ctx) {
//ruleref <- RULE_REF argActionBlock? elementOptions?
assertFeature(ctx.getChildCount() == 1, ctx);
copyText(ctx, ctx.RULE_REF());
}
@Override
public void exitCharacterRange(ANTLRv4Parser.CharacterRangeContext ctx) {
//STRING_LITERAL RANGE STRING_LITERAL
String range1 = unquoteString(ctx.STRING_LITERAL(0).getText());
String range2 = unquoteString(ctx.STRING_LITERAL(1).getText());
setCode(ctx, "[" + range1 + "-" + range2 + "]");
}
@Override public void exitTerminal(ANTLRv4Parser.TerminalContext ctx) {
//TOKEN_REF elementOptions? | STRING_LITERAL elementOptions?
assertFeature(ctx.getChildCount() == 1, ctx);
copyText(ctx, ctx.getChild(0));
}
@Override
public void exitElementOptions(ANTLRv4Parser.ElementOptionsContext ctx) {
assertFeature(false, ctx);
}
//getElementOption -> only callable through elementOptions
//identifier -> getText
}