1 package org.codehaus.mojo.javacc;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.File;
23
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugin.MojoFailureException;
26
27 /**
28 * Parses a JJTree grammar file (<code>*.jjt</code>) and transforms it to Java source files and a JavaCC grammar
29 * file. Please see the <a href="https://javacc.dev.java.net/doc/JJTree.html">JJTree Reference Documentation</a> for
30 * more information.
31 *
32 * @goal jjtree
33 * @phase generate-sources
34 * @since 2.0
35 * @deprecated As of version 2.4, use the <code>jjtree-javacc</code> goal instead.
36 * @author jesse <jesse.mcconnell@gmail.com>
37 * @version $Id: JJTreeMojo.java 7847 2008-10-05 17:52:15Z bentmann $
38 */
39 public class JJTreeMojo
40 extends AbstractPreprocessorMojo
41 {
42
43 /**
44 * The Java version for which to generate source code. Default value is <code>1.4</code>.
45 *
46 * @parameter expression="${jdkVersion}"
47 * @since 2.4
48 */
49 private String jdkVersion;
50
51 /**
52 * A flag whether to generate sample implementations for <code>SimpleNode</code> and any other nodes used in the
53 * grammar. Default value is <code>true</code>.
54 *
55 * @parameter expression="${buildNodeFiles}"
56 */
57 private Boolean buildNodeFiles;
58
59 /**
60 * A flag whether to generate a multi mode parse tree or a single mode parse tree. Default value is
61 * <code>false</code>.
62 *
63 * @parameter expression="${multi}"
64 */
65 private Boolean multi;
66
67 /**
68 * A flag whether to make each non-decorated production void instead of an indefinite node. Default value is
69 * <code>false</code>.
70 *
71 * @parameter expression="${nodeDefaultVoid}"
72 */
73 private Boolean nodeDefaultVoid;
74
75 /**
76 * The name of a custom class that extends <code>SimpleNode</code> and will be used as the super class for the
77 * generated tree node classes. By default, the tree node classes will directly extend the class
78 * <code>SimpleNode</code>.
79 *
80 * @parameter expression="${nodeClass}"
81 * @since 2.5
82 */
83 private String nodeClass;
84
85 /**
86 * The name of a custom factory class used to create <code>Node</code> objects. This class must have a method with
87 * the signature <code>public static Node jjtCreate(int id)</code>. By default, the class <code>SimpleNode</code>
88 * will be used as the factory class.
89 *
90 * @parameter expression="${nodeFactory}"
91 */
92 private String nodeFactory;
93
94 /**
95 * The package to generate the AST node classes into. This value may use a leading asterisk to reference the package
96 * of the corresponding parser. For example, if the parser package is <code>org.apache</code> and this parameter
97 * is set to <code>*.demo</code>, the tree node classes will be located in the package
98 * <code>org.apache.demo</code>. By default, the package of the corresponding parser is used.
99 *
100 * @parameter expression="${nodePackage}"
101 */
102 private String nodePackage;
103
104 /**
105 * The prefix used to construct node class names from node identifiers in multi mode. Default value is
106 * <code>AST</code>.
107 *
108 * @parameter expression="${nodePrefix}"
109 */
110 private String nodePrefix;
111
112 /**
113 * A flag whether user-defined parser methods should be called on entry and exit of every node scope. Default value
114 * is <code>false</code>.
115 *
116 * @parameter expression="${nodeScopeHook}"
117 */
118 private Boolean nodeScopeHook;
119
120 /**
121 * A flag whether the node construction routines need an additional method parameter to receive the parser object.
122 * Default value is <code>false</code>.
123 *
124 * @parameter expression="${nodeUsesParser}"
125 */
126 private Boolean nodeUsesParser;
127
128 /**
129 * A flag whether to generate code for a static parser. Note that this setting must match the corresponding option
130 * for the <code>javacc</code> mojo. Default value is <code>true</code>.
131 *
132 * @parameter expression="${isStatic}" alias="staticOption"
133 */
134 private Boolean isStatic;
135
136 /**
137 * A flag whether to insert the methods <code>jjtGetFirstToken()</code>, <code>jjtSetFirstToken()</code>,
138 * <code>getLastToken()</code> and <code>jjtSetLastToken()</code> into the class <code>SimpleNode</code>. Default
139 * value is <code>false</code>.
140 *
141 * @parameter expression="${trackTokens}"
142 * @since 2.5
143 */
144 private Boolean trackTokens;
145
146 /**
147 * A flag whether to insert a <code>jjtAccept()</code> method in the node classes and to generate a visitor
148 * implementation with an entry for every node type used in the grammar. Default value is <code>false</code>.
149 *
150 * @parameter expression="${visitor}"
151 */
152 private Boolean visitor;
153
154 /**
155 * The name of a class to use for the data argument of the <code>jjtAccept()</code> and <code>visit()</code>
156 * methods. Default value is <code>java.lang.Object</code>.
157 *
158 * @parameter expression="${visitorDataType}"
159 * @since 2.5
160 */
161 private String visitorDataType;
162
163 /**
164 * The name of a class to use as the return type of the <code>jjtAccept()</code> and <code>visit()</code> methods.
165 * Default value is <code>java.lang.Object</code>.
166 *
167 * @parameter expression="${visitorReturnType}"
168 * @since 2.5
169 */
170 private String visitorReturnType;
171
172 /**
173 * The name of an exception class to include in the signature of the generated <code>jjtAccept()</code>
174 * and <code>visit()</code> methods. By default, the <code>throws</code> clause of the generated methods is
175 * empty such that only unchecked exceptions can be thrown.
176 *
177 * @parameter expression="${visitorException}"
178 */
179 private String visitorException;
180
181 /**
182 * Directory where the input JJTree files (<code>*.jjt</code>) are located.
183 *
184 * @parameter expression="${sourceDirectory}" default-value="${basedir}/src/main/jjtree"
185 */
186 private File sourceDirectory;
187
188 /**
189 * Directory where the output Java files for the node classes and the JavaCC grammar file will be located.
190 *
191 * @parameter expression="${outputDirectory}" default-value="${project.build.directory}/generated-sources/jjtree"
192 */
193 private File outputDirectory;
194
195 /**
196 * The directory to store the processed input files for later detection of stale sources.
197 *
198 * @parameter expression="${timestampDirectory}"
199 * default-value="${project.build.directory}/generated-sources/jjtree-timestamp"
200 */
201 private File timestampDirectory;
202
203 /**
204 * The granularity in milliseconds of the last modification date for testing whether a source needs recompilation.
205 *
206 * @parameter expression="${lastModGranularityMs}" default-value="0"
207 */
208 private int staleMillis;
209
210 /**
211 * A set of Ant-like inclusion patterns used to select files from the source directory for processing. By default,
212 * the patterns <code>**/*.jjt</code> and <code>**/*.JJT</code> are used to select grammar files.
213 *
214 * @parameter
215 */
216 private String[] includes;
217
218 /**
219 * A set of Ant-like exclusion patterns used to prevent certain files from being processed. By default, this set is
220 * empty such that no files are excluded.
221 *
222 * @parameter
223 */
224 private String[] excludes;
225
226 /**
227 * {@inheritDoc}
228 */
229 protected File getSourceDirectory()
230 {
231 return this.sourceDirectory;
232 }
233
234 /**
235 * {@inheritDoc}
236 */
237 protected String[] getIncludes()
238 {
239 if ( this.includes != null )
240 {
241 return this.includes;
242 }
243 else
244 {
245 return new String[] { "**/*.jjt", "**/*.JJT" };
246 }
247 }
248
249 /**
250 * {@inheritDoc}
251 */
252 protected String[] getExcludes()
253 {
254 return this.excludes;
255 }
256
257 /**
258 * {@inheritDoc}
259 */
260 protected File getOutputDirectory()
261 {
262 return this.outputDirectory;
263 }
264
265 /**
266 * {@inheritDoc}
267 */
268 protected File getTimestampDirectory()
269 {
270 return this.timestampDirectory;
271 }
272
273 /**
274 * {@inheritDoc}
275 */
276 protected int getStaleMillis()
277 {
278 return this.staleMillis;
279 }
280
281 /**
282 * {@inheritDoc}
283 */
284 protected void processGrammar( GrammarInfo grammarInfo )
285 throws MojoExecutionException, MojoFailureException
286 {
287 File jjtFile = grammarInfo.getGrammarFile();
288
289 // determine target directory for tree node files
290 String nodePackageName = grammarInfo.resolvePackageName( this.nodePackage );
291 File nodeDirectory;
292 if ( nodePackageName != null )
293 {
294 nodeDirectory = new File( nodePackageName.replace( '.', File.separatorChar ) );
295 }
296 else
297 {
298 nodeDirectory = new File( grammarInfo.getParserDirectory() );
299 }
300 nodeDirectory = new File( getOutputDirectory(), nodeDirectory.getPath() );
301
302 // generate final grammar file and node files
303 JJTree jjtree = newJJTree();
304 jjtree.setInputFile( jjtFile );
305 jjtree.setOutputDirectory( nodeDirectory );
306 jjtree.setNodePackage( nodePackageName );
307 jjtree.run();
308
309 // create timestamp file
310 createTimestamp( grammarInfo );
311 }
312
313 /**
314 * Creates a new facade to invoke JJTree. Most options for the invocation are derived from the current values of the
315 * corresponding mojo parameters. The caller is responsible to set the input file, output directory and package on
316 * the returned facade.
317 *
318 * @return The facade for the tool invocation, never <code>null</code>.
319 */
320 protected JJTree newJJTree()
321 {
322 JJTree jjtree = new JJTree();
323 jjtree.setLog( getLog() );
324 jjtree.setJdkVersion( this.jdkVersion );
325 jjtree.setStatic( this.isStatic );
326 jjtree.setBuildNodeFiles( this.buildNodeFiles );
327 jjtree.setMulti( this.multi );
328 jjtree.setNodeDefaultVoid( this.nodeDefaultVoid );
329 jjtree.setNodeClass( this.nodeClass );
330 jjtree.setNodeFactory( this.nodeFactory );
331 jjtree.setNodePrefix( this.nodePrefix );
332 jjtree.setNodeScopeHook( this.nodeScopeHook );
333 jjtree.setNodeUsesParser( this.nodeUsesParser );
334 jjtree.setTrackTokens( this.trackTokens );
335 jjtree.setVisitor( this.visitor );
336 jjtree.setVisitorDataType( this.visitorDataType );
337 jjtree.setVisitorReturnType( this.visitorReturnType );
338 jjtree.setVisitorException( this.visitorException );
339 return jjtree;
340 }
341
342 /**
343 * Prevents registration of our output or a following invocation of the javacc mojo will cause duplicate sources
344 * which in turn will make compilation fail.
345 */
346 protected void addCompileSourceRoot()
347 {
348 // do nothing
349 }
350
351 }