-
Notifications
You must be signed in to change notification settings - Fork 0
/
javacc-wrapper.xml
537 lines (425 loc) · 21.5 KB
/
javacc-wrapper.xml
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
<!--
~ BSD-style license; for more info see http://pmd.sourceforge.net/license.html
-->
<project name="pmd" default="alljavacc" basedir="/">
<!-- Wraps a JavaCC/JJTree task and adapts the generated sources to the PMD
codebase.
Input parameters:
- lang-name: Capitalized name of the language eg Jsp or Java.
By convention the grammar file must be named exactly
so with a .jj[t] extension.
- lang-name-camelcase: Name of the language in camelcase. Useful for languages
whose name is an acronym, eg PLSQL (in camelcase, "Plsql").
Defaults to lang-name.
- lang-terse-name: Terse name, used in the conventional package names
It also uses the following maven properties:
- javacc.outputDirectory: Directory in which to root the generated package tree
- javacc.jar: JAR of JavaCC in the local maven repository
- some properties of project.build
-->
<condition property="lang-name-camelcase" value="${lang-name}">
<!-- Eg for "PLSQL": "Plsql" -->
<not>
<isset property="lang-name-camelcase" />
</not>
</condition>
<property name="target-package-dir" value="${javacc.outputDirectory}/net/sourceforge/pmd/lang/${lang-terse-name}/ast" />
<property name="stamp-file" value="${project.build.directory}/last-generated-timestamp" />
<property name="javacc-home.path" value="${project.build.directory}/lib/javacc" />
<property name="lang-ast-package" value="net.sourceforge.pmd.lang.${lang-terse-name}.ast" />
<property name="ast-api-package" value="net.sourceforge.pmd.lang.ast" />
<property name="ast-impl-package" value="${ast-api-package}.impl.javacc" />
<property name="grammar-file" value="etc/grammar/${lang-name}.jjt" />
<property name="parser-name" value="${lang-name}ParserImpl" />
<property name="parser-file" value="${target-package-dir}/${parser-name}.java" />
<property name="ast-prefix" value="AST" />
<property name="constants-itf-name" value="${parser-name}Constants" />
<property name="constants-itf-file" value="${target-package-dir}/${constants-itf-name}.java" />
<property name="token-constants-name" value="${lang-name}TokenKinds" />
<property name="token-constants-file" value="${target-package-dir}/${token-constants-name}.java" />
<property name="node-name" value="${lang-name}Node" />
<property name="base-class-name" value="Abstract${lang-name}Node" />
<!-- This will be moved to impl package when all language modules have been ported -->
<property name="base-tokenmgr" value="${ast-impl-package}.AbstractTokenManager"/>
<property name="charstream-itf" value="${ast-impl-package}.CharStream"/>
<property name="tokenmgr-name" value="${parser-name}TokenManager" />
<property name="tokenmgr-file" value="${target-package-dir}/${tokenmgr-name}.java" />
<!-- Visitor names -->
<!-- This is the default name generated by JJTree -->
<property name="gen-visitor-name" value="${parser-name}Visitor" />
<!-- This is the actual name we use -->
<property name="generic-visitor-interface-name" value="${lang-name-camelcase}Visitor" />
<property name="generic-visitor-interface-file"
value="${target-package-dir}/${generic-visitor-interface-name}.java" />
<!-- TARGETS -->
<target name="alljavacc"
description="Generates JavaCC sources and cleans them up"
depends="checkUpToDate,init,jjtree,jjtree-ersatz,javacc,adapt-generated,default-visitor,cleanup" />
<target name="checkUpToDate"
description="Checks the input files are up to date">
<uptodate property="javaccBuildNotRequired" targetfile="${stamp-file}">
<srcfiles dir="etc/grammar" includes="${lang-name}.jj*" />
<srcfiles file="${ant.file}" />
</uptodate>
<echo message="Up-to-date check: javaccBuildNotRequired=${javaccBuildNotRequired}" />
<condition property="jjtreeBuildNotRequired">
<or>
<isset property="no-jjtree" />
<isset property="javaccBuildNotRequired" />
</or>
</condition>
</target>
<target name="init" unless="javaccBuildNotRequired" description="Initialize build">
<mkdir dir="${javacc-home.path}" />
<copy file="${javacc.jar}" tofile="${javacc-home.path}/javacc.jar" />
<mkdir dir="${javacc.outputDirectory}" />
<touch file="${stamp-file}" />
<delete dir="${target-package-dir}" />
<mkdir dir="${target-package-dir}" />
</target>
<target name="cleanup" unless="javaccBuildNotRequired">
<delete dir="${javacc-home.path}" />
</target>
<target name="jjtree" unless="jjtreeBuildNotRequired" description="Runs JJTree">
<!--
Token-tracking behavior is implemented in the JJTree builder,
so the grammar should have TRACK_TOKEN=false; (this is the default value)
-->
<java fork="true"
classname="jjtree"
classpath="${javacc-home.path}/javacc.jar">
<sysproperty key="file.encoding" value="UTF-8" />
<arg value="-OUTPUT_DIRECTORY:${target-package-dir}" />
<arg value="-NODE_USES_PARSER:false" />
<arg value="-NODE_PACKAGE:${lang-ast-package}" />
<arg value="etc/grammar/${lang-name}.jjt" />
</java>
</target>
<target name="jjtree-ersatz" if="no-jjtree" unless="javaccBuildNotRequired">
<!-- If no jjtree is run, then we look for a .jj file to place
in the target dir for the javacc target to pick up on it -->
<copy file="etc/grammar/${lang-name}.jj" todir="${target-package-dir}" />
</target>
<target name="javacc" depends="jjtree" unless="javaccBuildNotRequired">
<java fork="true"
classname="javacc"
classpath="${javacc-home.path}/javacc.jar">
<sysproperty key="file.encoding" value="UTF-8" />
<arg value="-STATIC:false" />
<arg value="-USER_CHAR_STREAM:true" />
<arg value="-UNICODE_INPUT:true" />
<arg value="-OUTPUT_DIRECTORY:${target-package-dir}" />
<arg value="${target-package-dir}/${lang-name}.jj" />
</java>
</target>
<target name="adapt-generated" unless="javaccBuildNotRequired">
<delete file="${target-package-dir}/Node.java" />
<delete file="${target-package-dir}/SimpleNode.java" />
<delete file="${target-package-dir}/JJT${parser-name}State.java" />
<delete file="${target-package-dir}/CharStream.java" />
<delete file="${target-package-dir}/ParseException.java" />
<delete file="${target-package-dir}/Token.java" />
<delete file="${target-package-dir}/TokenMgrError.java" />
<!-- Remove all duplicate files -->
<delete>
<fileset dir="${javacc.outputDirectory}">
<present present="both" targetdir="${project.build.sourceDirectory}" />
</fileset>
</delete>
<antcall target="cleanup-token-manager" />
<antcall target="cleanup-parser" />
<antcall target="cleanup-nodes" />
<!-- Map Javacc names to our names -->
<replaceregexp flags="g">
<regexp pattern="\bToken\b" />
<substitution expression="${ast-impl-package}.JavaccToken" />
<fileset dir="${target-package-dir}" />
</replaceregexp>
<replaceregexp flags="g">
<regexp pattern="(?<!\.)\bCharStream\b" />
<substitution expression="${charstream-itf}" />
<fileset dir="${target-package-dir}" />
</replaceregexp>
<antcall target="constants-files" />
<!-- For compatibility -->
<replaceregexp flags="g">
<regexp pattern="\.image\b(?! =)" />
<substitution expression=".getImage()" />
<fileset dir="${target-package-dir}" />
</replaceregexp>
<replace token=".beginLine" value=".getBeginLine()">
<fileset dir="${target-package-dir}" />
</replace>
<replace token=".beginColumn" value=".getBeginColumn()">
<fileset dir="${target-package-dir}" />
</replace>
<!-- Used by debug modes -->
<replace token="TokenMgrError.addEscapes" value="net.sourceforge.pmd.util.StringUtil.escapeJava">
<fileset dir="${target-package-dir}" />
</replace>
</target>
<target name="cleanup-parser">
<replace token="new Token()" value="token_source.input_stream.getTokenDocument().open()">
<fileset file="${parser-file}" />
</replace>
<replaceregexp flags="g">
<regexp pattern='jjtree.openNodeScope\((jjtn.*?)\);' />
<substitution expression='jjtree.openNodeScope(\1, getToken(1));' />
<file name="${parser-file}" />
</replaceregexp>
<replaceregexp flags="g">
<regexp pattern='jjtree.closeNodeScope\((jjtn.*?)\);' />
<substitution expression='jjtree.closeNodeScope(\1, getToken(0));' />
<file name="${parser-file}" />
</replaceregexp>
<!-- The tree builder is generic now -->
<replaceregexp flags="g">
<regexp pattern="\bJJT${parser-name}State\b" />
<substitution expression="${ast-impl-package}.JjtreeBuilder<${base-class-name}>" />
<fileset file="${parser-file}" />
</replaceregexp>
<replaceregexp flags="g">
<regexp pattern="new ParseException\(token, exptokseq, tokenImage\);" />
<substitution expression="new ParseException(token, exptokseq);" />
<fileset file="${parser-file}" />
</replaceregexp>
<replaceregexp flags="g">
<regexp pattern="throw new ParseException\(\);" />
<substitution expression='throw net.sourceforge.pmd.util.AssertionUtil.shouldNotReachHere("consumetoken(-1) should have thrown");' />
<fileset file="${parser-file}" />
</replaceregexp>
<replaceregexp>
<regexp pattern="public interface"/>
<substitution expression="interface"/>
<fileset file="${target-package-dir}/${parser-name}TreeConstants.java" />
</replaceregexp>
</target>
<target name="cleanup-token-manager">
<replaceregexp>
<regexp pattern='(public )?class ${tokenmgr-name}' />
<substitution expression='class ${tokenmgr-name} extends ${base-tokenmgr}' />
<file name="${tokenmgr-file}" />
</replaceregexp>
<!-- This is a programming error and should be caught in tests -->
<replaceregexp>
<regexp pattern='throw new TokenMgrError\("Error: Ignoring invalid lexical state.*?\);' />
<substitution expression='throw new IllegalArgumentException("Invalid lexical state " + lexState);' />
<file name="${tokenmgr-file}" />
</replaceregexp>
<!-- Useless argument, also replace lex state ID with its name -->
<replaceregexp>
<regexp pattern='curLexState, error_line, error_column, error_after, curChar, TokenMgrError.LEXICAL_ERROR\)' />
<substitution expression='lexStateNames[curLexState], error_line, error_column, error_after, curChar)' />
<file name="${tokenmgr-file}" />
</replaceregexp>
<!-- Patch token creation routine, delegates to the token document. -->
<replaceregexp flags="s">
<file name="${tokenmgr-file}" />
<regexp pattern="protected Token jjFillToken.*?(?=int curLexState = )" />
<substitution expression="protected Token jjFillToken() {return input_stream.getTokenDocument().createToken(jjmatchedKind, input_stream, jjstrLiteralImages[jjmatchedKind]);}" />
</replaceregexp>
<!-- Renamed CharStream methods -->
<replaceregexp flags="g">
<file name="${tokenmgr-file}" />
<regexp pattern="image.append\(input_stream.GetSuffix\(" />
<substitution expression="input_stream.appendSuffix(image,(" />
</replaceregexp>
<replaceregexp flags="g">
<file name="${tokenmgr-file}" />
<regexp pattern="input_stream.GetImage" />
<substitution expression="input_stream.getTokenImage" />
</replaceregexp>
<replaceregexp flags="g">
<file name="${tokenmgr-file}" />
<regexp pattern="input_stream.BeginToken" />
<substitution expression="input_stream.markTokenStart" />
</replaceregexp>
<!-- This is used to allow for tokens to be immutable. The lexical actions
return the new token instead of mutating it. -->
<replaceregexp flags="sg">
<file name="${tokenmgr-file}" />
<regexp pattern="void ((Token|Skip)LexicalActions\(Token matchedToken\))\R\{(.*?)}\R(?=(private )?void)" />
<substitution expression="Token \1 { \3
return matchedToken;
}" />
</replaceregexp>
<!-- Update call sites for the previous routines -->
<replaceregexp>
<file name="${tokenmgr-file}" />
<regexp pattern="(Token|Skip)LexicalActions\(matchedToken\);" />
<substitution expression="matchedToken = \0" />
</replaceregexp>
<!-- Fix end column being now exclusive -->
<!-- JavaCC assumes it's inclusive, uses it for character positions of errors -->
<!-- This is also used in debug mode -->
<replaceregexp flags="sg">
<file name="${tokenmgr-file}" />
<regexp pattern="input_stream.getEndColumn\(\)" />
<substitution expression="(input_stream.getEndColumn() - 1)" />
</replaceregexp>
</target>
<target name="constants-files">
<replaceregexp flags="g">
<regexp pattern="implements ${constants-itf-name}," />
<substitution expression="implements" />
<fileset dir="${target-package-dir}" />
</replaceregexp>
<replaceregexp flags="g">
<regexp pattern=", ${constants-itf-name}\b" />
<substitution expression="" />
<fileset dir="${target-package-dir}" />
</replaceregexp>
<replaceregexp flags="g">
<regexp pattern="implements ${constants-itf-name}\b" />
<substitution expression="" />
<fileset dir="${target-package-dir}" />
</replaceregexp>
<replaceregexp>
<regexp pattern="package ${lang-ast-package};" />
<substitution expression="\0
import static ${lang-ast-package}.${token-constants-name}.*;" />
<fileset file="${tokenmgr-file}" />
<fileset file="${parser-file}" />
</replaceregexp>
<!-- Create constants file -->
<move file="${constants-itf-file}" tofile="${token-constants-file}" />
<replaceregexp>
<regexp pattern="(public )?interface ${constants-itf-name} \{" />
<substitution expression="/** Token kinds ({@link ${ast-impl-package}.JavaccToken#kind}) for this language. */${line.separator}
@net.sourceforge.pmd.annotation.InternalApi${line.separator}
public final class ${token-constants-name} \{${line.separator}
private ${token-constants-name}() { /* Utility class */ }${line.separator}
"/>
<fileset file="${token-constants-file}" />
</replaceregexp>
<replaceregexp flags="g">
<regexp pattern="/\*\* RegularExpression Id. \*/\R" />
<substitution expression="public static final " />
<fileset file="${token-constants-file}" />
</replaceregexp>
<replaceregexp flags="s">
<regexp pattern="/\*\* End of File. \*/\R" />
<substitution expression="public static final " />
<fileset file="${token-constants-file}" />
</replaceregexp>
<!-- Lexical states are kept package-private -->
<replaceregexp flags="g">
<regexp pattern="/\*\* Lexical state. \*/\R" />
<substitution expression="static final " />
<fileset file="${token-constants-file}" />
</replaceregexp>
<replace>
<replacetoken>/** Literal token values. */</replacetoken>
<replacevalue><![CDATA[
/** Returns a string describing the given token kind.
* Returns null if the kind is unknown.
*
* @param kind Kind of token
*
* @return A string describing the given kind
*/
public static @org.checkerframework.checker.nullness.qual.Nullable
String describe(int kind) {
return kind < 0 || kind >= tokenImage.length
? null
: tokenImage[kind];
}
/** Returns a new token manager for this language. The CharStream
* should be configured with the correct language-specific escaping
* and token document. This is not a published API, it's intended to
* be used as a basis for a CPD Tokenizer.
*/
@net.sourceforge.pmd.annotation.InternalApi
public static net.sourceforge.pmd.lang.TokenManager<%%%API_PACK%%%.impl.javacc.JavaccToken> newTokenManager(%%%API_PACK%%%.impl.javacc.CharStream cs) {
return new %%%TOKEN_MGR_NAME%%%(cs);
}
private static final
]]> </replacevalue>
<fileset file="${token-constants-file}" />
</replace>
<replace>
<replacetoken>};</replacetoken>
<replacevalue><![CDATA[
};
/** Nams of the tokens, each index corresponds to a kind. See also {@link #describe(int)}. */
public static final java.util.List<String> TOKEN_NAMES = java.util.Collections.unmodifiableList(java.util.Arrays.asList(tokenImage));
]]> </replacevalue>
<fileset file="${token-constants-file}" />
</replace>
<replaceregexp>
<regexp pattern="%%%TOKEN_MGR_NAME%%%" />
<substitution expression="${tokenmgr-name}" />
<fileset file="${token-constants-file}" />
</replaceregexp>
<replaceregexp flags="g">
<regexp pattern="%%%API_PACK%%%" />
<substitution expression="${ast-api-package}" />
<fileset file="${token-constants-file}" />
</replaceregexp>
</target>
<target name="cleanup-nodes">
<replaceregexp flags="g">
<regexp pattern="\bParseException\b" />
<substitution expression="${ast-api-package}.ParseException" />
<fileset dir="${target-package-dir}" />
</replaceregexp>
<replaceregexp flags="g">
<regexp pattern="extends SimpleNode\b" />
<substitution expression="extends ${base-class-name}" />
<fileset dir="${target-package-dir}" />
</replaceregexp>
<!-- Remove constructor with parser -->
<replaceregexp flags="s">
<regexp pattern="public \w+\(${parser-name}.*?}" />
<substitution expression="" />
<fileset dir="${target-package-dir}">
<include name="${ast-prefix}*" />
</fileset>
</replaceregexp>
<!-- Make constructor package-private -->
<replaceregexp>
<regexp pattern="public (\w+\(int)" />
<substitution expression="\1" />
<fileset dir="${target-package-dir}">
<include name="${ast-prefix}*" />
</fileset>
</replaceregexp>
<!-- Make constructor package-private -->
<replaceregexp>
<regexp pattern="(?<!public(\R| ))class" />
<substitution expression="public class" />
<fileset dir="${target-package-dir}">
<include name="${ast-prefix}*" />
</fileset>
</replaceregexp>
<replaceregexp flags="g">
<regexp pattern="/\*\* Accept the visitor. \*\*/" />
<substitution expression="" />
<fileset dir="${target-package-dir}">
<include name="${ast-prefix}*" />
</fileset>
</replaceregexp>
</target>
<target name="default-visitor" depends="jjtree" unless="jjtreeBuildNotRequired">
<move file="${target-package-dir}/${gen-visitor-name}.java" tofile="${generic-visitor-interface-file}" />
<replace file="${generic-visitor-interface-file}">
<replacefilter token="${gen-visitor-name}"
value="${generic-visitor-interface-name}<P, R> extends ${ast-api-package}.AstVisitor<P, R>" />
<replacefilter token="SimpleNode" value="${node-name}" />
<!-- Root method, eg visitJavaNode -->
<replacefilter
token="Object visit(${node-name} node, Object data);"
value="default R visit${lang-name-camelcase}Node(${node-name} node, P data) { return visitNode(node, data); }">
</replacefilter>
<replacefilter token="public Object visit(" value="default R visit(" />
<replacefilter token="Object data);" value="P data) { return visit${lang-name-camelcase}Node(node, data); }" />
</replace>
<replace>
<replacefilter token="public Object jjtAccept(${gen-visitor-name} visitor, Object data)"
value="@Override protected <P, R> R accept${generic-visitor-interface-name}(${generic-visitor-interface-name}<? super P, ? extends R> visitor, P data)" />
<fileset dir="${target-package-dir}">
<include name="${ast-prefix}*" />
</fileset>
</replace>
</target>
</project>