[antlr-interest] passing an argument to a lexer
siemsen at ucar.edu
siemsen at ucar.edu
Sat Apr 26 00:23:00 PDT 2008
I have a working translator that implements include files using the
scheme described in the ANTLR Wiki page titled "How do I implement
include files?" at http://www.antlr.org/wiki/pages/viewpage.action?pageId=557057
It works, but only if the include files are located in the current
directory. To run the translator, I have to "cd" to the right
directory first. Not good.
I want the translator to look for include files relative to the
current input file. It's easy to get the base path of the current
input file, but I don't know how to get the path into the lexer. The
relevant fragment of my "rig" looks like this
<etc.>
File inputFile = new File(inputFileName);
String inputSubDirectory = inputFile.getParent();
// Open an input file stream from the given file name
CharStream input = new ANTLRFileStream(inputFileName);
// Create a lexer that feeds from the input file stream
TranslateCIMLexer lexer = new TranslateCIMLexer(input);
// Create a stream of tokens fed by the lexer
CommonTokenStream tokens = new CommonTokenStream(lexer);
<etc.>
How can I pass inputSubDirectory into TranslateCIMLexer so that it's
available in lexer rule actions?
I hoped that calling input.getSourceName() from the relevant lexer
rule action might give the full path to the file, but the compiler
gave me
/Users/siemsen/TranslateCIM/target/generated-sources/antlr/
TranslateCIMLexer.java:[1449,82] cannot find symbol
symbol : method getSourceName()
location: interface org.antlr.runtime.CharStream
-- Pete
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080426/f8b51285/attachment.html
More information about the antlr-interest
mailing list