[antlr-interest] TestRig: use with package-ized lexer/parser?

Graham Wideman gwlist at grahamwideman.com
Mon Nov 12 03:43:46 PST 2012


Hi Terr and all,

Update:

I've solved half of the puzzle of TestRig's dynamic class loading, but I'm still interested in whether there's already an intended way to use TestRig with a grammar that specifies a package.

The part I solved is how TestRig manages to find default-package lexer and parser class files under normal conditions. The short version is that TestRig relies on there being a "dot" (current dir) in the CLASSPATH environment variable. But there are a number of correlated details/pitfalls.

The keys are:

1) The user must invoke TestRig (runs grun shell/batch file) from the directory containing the lexer and parser class files, in such a way that TestRig (or my version TestRig2) sees that dir as the working dir. The subtle issue here is...

2) TestRig's own jar file must be in the CLASSPATH, so that it can be invoked with a reference to the class to run:
   java org.antlr.v4.runtime.misc.TestRig %*
and NOT with the -jar option like this (with runnable class in the manifest):
   java -jar "path-to-jarfile\TestRig2.jar"   %*
... as this latter apparently changes the working directory.

3) The current working dir (".") must be included in the CLASSPATH (environment variable) that TestRig sees. (Note: the class loader for files does not treat the current working directory as an assumed place to look for classes. It only looks in cwd if there's a dot in CLASSPATH).

The appropriate CLASSPATH (including TestRig and dot can be set up in grun shell/batch file). However...

4) When debugging in NetBeans, I didn't find a way to set the debug/run CLASSPATH to include the current working dir (though there is a setting for current working dir itself).  One work-around is to add the desired directory (the one containing the lexer and parser class) as a "Library" in Netbeans.  Unfortunately there doesn't seem to be a way to set "." as a Library.

5) It's perhaps obvious, but the package name for a custom version of TestRig is not salient to finding the lexer/parser classes (though of course invocation of custom TestRig does have to refer to the custom package name).

(And by way of leaving clues: I wanted to look at the ClassLoader search algorithm, but the JRE 7 source from Oracle manages to omit crucial sun.misc.URLClassPath. However, more-comprehensive source is available here: http://jdk7src.sourceforge.net/)

------

OK, so now I can build and debug my own version of TestRig, and unless someone has an alternative bright idea, I hope to explore adding a command-line option to specify a package name for the grammar.

-- Graham 



More information about the antlr-interest mailing list