Reputation: 2013
I'm using Antlr4 to build a parser and I'm using Netbeans as an IDE and ant
as a build system. Ant runs Antlr which generates a parser.java
and lexer.java
in the project's src/
directory.
They both need a package directive, and so I have to add it by hand.
Is there a way to prepend a line of text to all source files in a directory with Ant? Or are there any other solutions to this? (Maybe with Antlr?)
Upvotes: 0
Views: 195
Reputation: 2013
There's an odd caveat where @header {...} in a grammar for a combined lexer+parser works. However for separate lexer and parser grammar files each has to have the header declared as
@lexer::header { ... }
// or
@parser::header { ... }
or else only the parser will have a header added (that is the caveat).
Also, as mentioned by Vincent there is a command line option, but that same caveat applies.
Upvotes: 0
Reputation: 1536
You can use a combinaison of the -o
option for antlr4 command line and @header{...}
in your grammar:
-o
allows you to specify the output directory where all output will be generated,
@header{...}
will insert the code you write into {..}
as your generated code header so you can put your package declaration into this block.
for example:
grammar myGrammar;
@header {
package my.package;
}
...
If you don't want to tie your grammar to a specific language, you can use the -package
option (allows you to specify a package or namespace for the generated files) and remove the package declaration from your @header{...}
code.
Upvotes: 1