diff --git a/docs/manual/OptionalTasks/antlr.html b/docs/manual/OptionalTasks/antlr.html index da2400d45..7beac307c 100644 --- a/docs/manual/OptionalTasks/antlr.html +++ b/docs/manual/OptionalTasks/antlr.html @@ -54,6 +54,71 @@ No + + glib + + An optional super grammar file that the target grammar overrides. This + feature is only needed for advanced vocabularies. + + No + + + debug + + When set to "yes", this flag adds code to the generated parser that will + launch the ParseView debugger upon invocation. The default is "no". +
+ Note: ParseView is a separate component that needs to be installed or your + grammar will have compilation errors. + + No + + + html + + Emit an html version of the grammar with hyperlinked actions. + + No + + + diagnostic + + Generates a text file with debugging infomation based on the target grammar. + + No + + + trace + + Forces all rules to call traceIn/traceOut if set to "yes". + The default is "no". + + No + + + traceParser + + Only forces parser rules to call traceIn/traceOut if set to "yes". + The default is "no". + + No + + + traceLexer + + Only forces lexer rules to call traceIn/traceOut if set to "yes". + The default is "no". + + No + + + traceTreeWalker + + Only forces tree walker rules to call traceIn/traceOut if set to + "yes". The default is "no". + + No + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - \ No newline at end of file + diff --git a/src/etc/testcases/taskdefs/optional/antlr/extended.calc.g b/src/etc/testcases/taskdefs/optional/antlr/extended.calc.g new file mode 100644 index 000000000..adf83297e --- /dev/null +++ b/src/etc/testcases/taskdefs/optional/antlr/extended.calc.g @@ -0,0 +1,7 @@ +// Not really a great extension, but it is only a test after all! + +class ExtendedCalcParser extends CalcParser; + +exprList + : LPAREN (expr)* RPAREN + ; diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ANTLR.java b/src/main/org/apache/tools/ant/taskdefs/optional/ANTLR.java index 8f4577877..9f0ae8298 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/ANTLR.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/ANTLR.java @@ -74,6 +74,7 @@ import org.apache.tools.ant.types.Path; * * @author Erik Meade * @author Stephane Bailliez + * @author Stephen Chin */ public class ANTLR extends Task { @@ -85,6 +86,30 @@ public class ANTLR extends Task { /** where to output the result */ private File outputDirectory; + /** an optional super grammar file */ + private String superGrammar; + + /** optional flag to enable parseView debugging */ + private boolean debug; + + /** optional flag to enable html output */ + private boolean html; + + /** optional flag to print out a diagnostic file */ + private boolean diagnostic; + + /** optional flag to add trace methods */ + private boolean trace; + + /** optional flag to add trace methods to the parser only */ + private boolean traceParser; + + /** optional flag to add trace methods to the lexer only */ + private boolean traceLexer; + + /** optional flag to add trace methods to the tree walker only */ + private boolean traceTreeWalker; + /** should fork ? */ private final boolean fork = true; @@ -106,15 +131,70 @@ public class ANTLR extends Task { this.outputDirectory = outputDirectory; } + /** + * Sets an optional super grammar file + */ + public void setGlib(String superGrammar) { + this.superGrammar = superGrammar; + } + + /** + * Sets a flag to enable ParseView debugging + */ + public void setDebug(boolean enable) { + debug = enable; + } + + /** + * Sets a flag to emit html + */ + public void setHtml(boolean enable) { + html = enable; + } + + /** + * Sets a flag to emit diagnostic text + */ + public void setDiagnostic(boolean enable) { + diagnostic = enable; + } + + /** + * Sets a flag to enable all tracing + */ + public void setTrace(boolean enable) { + trace = enable; + } + + /** + * Sets a flag to enable parser tracing + */ + public void setTraceParser(boolean enable) { + traceParser = enable; + } + + /** + * Sets a flag to allow the user to enable lexer tracing + */ + public void setTraceLexer(boolean enable) { + traceLexer = enable; + } + + /** + * Sets a flag to allow the user to enable tree walker tracing + */ + public void setTraceTreeWalker(boolean enable) { + traceTreeWalker = enable; + } + // we are forced to fork ANTLR since there is a call // to System.exit() and there is nothing we can do // right now to avoid this. :-( (SBa) // I'm not removing this method to keep backward compatibility - // and public void setFork(boolean s) { //this.fork = s; } - + /** * The working directory of the process */ @@ -184,10 +264,9 @@ public class ANTLR extends Task { validateAttributes(); //TODO: use ANTLR to parse the grammer file to do this. if (target.lastModified() > getGeneratedFile().lastModified()) { - commandline.createArgument().setValue("-o"); - commandline.createArgument().setValue(outputDirectory.toString()); + populateAttributes(); commandline.createArgument().setValue(target.toString()); - + log(commandline.describeCommand(), Project.MSG_VERBOSE); int err = run(commandline.getCommandline()); if (err == 1) { @@ -198,11 +277,47 @@ public class ANTLR extends Task { } } + /** + * A refactored method for populating all the command line arguments based + * on the user-specified attributes. + */ + private void populateAttributes() { + commandline.createArgument().setValue("-o"); + commandline.createArgument().setValue(outputDirectory.toString()); + if (superGrammar != null) { + commandline.createArgument().setValue("-glib"); + commandline.createArgument().setValue(superGrammar); + } + if (html) { + commandline.createArgument().setValue("-html"); + } + if (diagnostic) { + commandline.createArgument().setValue("-diagnostic"); + } + if (trace) { + commandline.createArgument().setValue("-trace"); + } + if (traceParser) { + commandline.createArgument().setValue("-traceParser"); + } + if (traceLexer) { + commandline.createArgument().setValue("-traceLexer"); + } + if (traceTreeWalker) { + commandline.createArgument().setValue("-traceTreeWalker"); + } + } + private void validateAttributes() throws BuildException { if (target == null || !target.isFile()) { throw new BuildException("Invalid target: " + target); } - + + // validate the superGrammar file + if (superGrammar != null && !new File(superGrammar).isFile()) { + throw new BuildException("Invalid super grammar file: " + superGrammar); + } + // if no output directory is specified, used the target's directory if (outputDirectory == null) { String fileName = target.toString(); diff --git a/src/testcases/org/apache/tools/ant/taskdefs/optional/ANTLRTest.java b/src/testcases/org/apache/tools/ant/taskdefs/optional/ANTLRTest.java index b2531882a..668afabb4 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/optional/ANTLRTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/optional/ANTLRTest.java @@ -68,6 +68,7 @@ import org.apache.tools.ant.BuildFileTest; * system classpath. (see ANTLR install.html) * * @author Erik Meade + * @author Stephen Chin */ public class ANTLRTest extends BuildFileTest { @@ -117,6 +118,57 @@ public class ANTLRTest extends BuildFileTest { public void test7() { expectBuildException("test7", "Unable to determine generated class"); } + + /** + * This is a negative test for the super grammar (glib) option. + */ + public void test8() { + expectBuildException("test8", "Invalid super grammar file"); + } + + /** + * This is a positive test for the super grammar (glib) option. ANTLR + * will throw an error if everything is not correct. + */ + public void test9() { + executeTarget("test9"); + } + + /** + * This test creates an html-ized version of the calculator grammar. + * The sanity check is simply whether or not an html file was generated. + */ + public void test10() { + executeTarget("test10"); + File outputDirectory = new File(TASKDEFS_DIR + "antlr.tmp"); + String[] calcFiles = outputDirectory.list(new HTMLFilter()); + assertEquals(1, calcFiles.length); + } + + /** + * This is just a quick sanity check to run the diagnostic option and + * make sure that it doesn't throw any funny exceptions. + */ + public void test11() { + executeTarget("test11"); + } + + /** + * This is just a quick sanity check to run the trace option and + * make sure that it doesn't throw any funny exceptions. + */ + public void test12() { + executeTarget("test12"); + } + + /** + * This is just a quick sanity check to run all the rest of the + * trace options (traceLexer, traceParser, and traceTreeWalker) to + * make sure that they don't throw any funny exceptions. + */ + public void test13() { + executeTarget("test13"); + } } class CalcFileFilter implements FilenameFilter { @@ -124,3 +176,9 @@ class CalcFileFilter implements FilenameFilter { return name.startsWith("Calc"); } } + +class HTMLFilter implements FilenameFilter { + public boolean accept(File dir, String name) { + return name.endsWith("html"); + } +}