antlr4cs
antlr4cs copied to clipboard
Cannot include lexer grammar with custom constructor
I have created two grammars:
- filter.g4 that has some lexical rules and a custom constructor
- apply.g4 that includes token definitions from filter.g4
When I generate Parser.cs for the second grammar (ApplyParser.cs) it copies everything from @lexer::members { }
in filter.g4 including constructor FilterLexer() and put everything in the ApplyParser class. When I compile project, I'm getting error "method must have return type" on custom FilterLexer() constructor that is copied into ApplyParser class.
Not a big deal in my case, because I have refactored my grammar and placed token definitions in third grammar that is included both from filter.g4 and apply.g4, so it is not a bug but you might document this somewhere.
The approach I normally use when a grammar requires a custom constructor is marking it as Abstract. Marking a grammar FilterLexer as abstract causes it to produce an abstract class AbstractFilterLexer
instead of the normal class FilterLexer
. The rename allows you to create your own class FilterLexer : AbstractFilterLexer
in code.