[antlr-interest] passing an argument to a lexer
siemsen at ucar.edu
siemsen at ucar.edu
Sat Apr 26 23:00:54 PDT 2008
Thanks, Gavin and Johannes. The cast helped. My lexer error messages
now contain the current file name. Very nice!
-- Pete
On Apr 26, 2008, at 4:33 AM, Gavin Lambert wrote:
> 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