[antlr-interest] passing an argument to a lexer
Gavin Lambert
antlr at mirality.co.nz
Sat Apr 26 03:33:04 PDT 2008
At 19:23 26/04/2008, siemsen at ucar.edu wrote:
>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
getSourceName isn't defined on CharStream, it's on
ANTLRFileStream. You can get to it with a cast, if you're
absolutely certain you're only going to be giving file streams to
the lexer:
... ((ANTLRFileStream)input).getSourceName() ...
(Actually, it appears to be *defined* on ANTLRStringStream, but I
can't see how this would give it a useful value.)
Another (possibly better) option is to use a @members (or
@lexer::members) section to create an additional field/property in
your lexer class where you can store the base directory
name. This is potentially more flexible, since you can extend it
later to search multiple folders (for example). It also doesn't
limit you to using file streams, which is useful when unit-testing
the grammar.
More information about the antlr-interest
mailing list