added -package option.

This commit is contained in:
Terence Parr 2012-09-22 17:36:14 -07:00
parent 1909fb3f85
commit 913bb717bd
5 changed files with 15 additions and 1 deletions

View File

@ -1,6 +1,6 @@
grammar A;
s : INT { System.out.println($ctx.getStart());} ;
s : INT { System.out.println($start.getText());} ;
INT : [0-9]+ ;
WS : [ \t\n]+ -> skip ;

View File

@ -14,7 +14,11 @@ javaTypeInitMap ::= [
ParserFile(file, parser, namedActions) ::= <<
// $ANTLR ANTLRVersion> <file.fileName> generatedTimestamp>
<if(file.genPackage)>
package <file.genPackage>;
<else>
<namedActions.header>
<endif>
import org.antlr.v4.runtime.atn.*;
import org.antlr.v4.runtime.dfa.DFA;
import org.antlr.v4.runtime.*;
@ -676,7 +680,11 @@ _prevctx.stop = _input.LT(-1);
LexerFile(lexerFile, lexer, namedActions) ::= <<
// $ANTLR ANTLRVersion> <lexerFile.fileName> generatedTimestamp>
<if(lexerFile.genPackage)>
package <lexerFile.genPackage>;
<else>
<namedActions.header>
<endif>
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.Token;

View File

@ -126,6 +126,7 @@ public class Tool {
public boolean verbose_dfa = false;
public boolean gen_listener = true;
public boolean gen_visitor = false;
public String genPackage = null;
public Map<String, String> grammarOptions = null;
public static Option[] optionDefs = {
@ -144,6 +145,7 @@ public class Tool {
new Option("gen_listener", "-no-listener", "don't generate parse tree listener"),
new Option("gen_visitor", "-visitor", "generate parse tree visitor"),
new Option("gen_visitor", "-no-visitor", "don't generate parse tree visitor (default)"),
new Option("genPackage", "-package", OptionArgType.STRING, "specify a package/namespace for the generated code"),
new Option("", "-D<option>=value", "set/override a grammar-level option"),

View File

@ -37,6 +37,7 @@ import java.util.HashMap;
import java.util.Map;
public class LexerFile extends OutputFile {
public String genPackage; // from -package cmd-line
@ModelElement public Lexer lexer;
@ModelElement public Map<String, Action> namedActions;
@ -48,5 +49,6 @@ public class LexerFile extends OutputFile {
ActionAST ast = g.namedActions.get(name);
namedActions.put(name, new Action(factory, ast));
}
genPackage = factory.getGrammar().tool.genPackage;
}
}

View File

@ -38,6 +38,7 @@ import java.util.Map;
/** */
public class ParserFile extends OutputFile {
public String genPackage; // from -package cmd-line
@ModelElement public Parser parser;
@ModelElement public Map<String, Action> namedActions;
@ -49,5 +50,6 @@ public class ParserFile extends OutputFile {
ActionAST ast = g.namedActions.get(name);
namedActions.put(name, new Action(factory, ast));
}
genPackage = factory.getGrammar().tool.genPackage;
}
}