本文整理汇总了Java中org.antlr.v4.tool.Grammar类的典型用法代码示例。如果您正苦于以下问题:Java Grammar类的具体用法?Java Grammar怎么用?Java Grammar使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Grammar类属于org.antlr.v4.tool包,在下文中一共展示了Grammar类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handleOptionSetArg
import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
protected void handleOptionSetArg(String arg) {
int eq = arg.indexOf('=');
if ( eq>0 && arg.length()>3 ) {
String option = arg.substring("-D".length(), eq);
String value = arg.substring(eq+1);
if ( value.length()==0 ) {
errMgr.toolError(ErrorType.BAD_OPTION_SET_SYNTAX, arg);
return;
}
if ( Grammar.parserOptions.contains(option) ||
Grammar.lexerOptions.contains(option) )
{
if ( grammarOptions==null ) grammarOptions = new HashMap<String, String>();
grammarOptions.put(option, value);
}
else {
errMgr.grammarError(ErrorType.ILLEGAL_OPTION,
null,
null,
option);
}
}
else {
errMgr.toolError(ErrorType.BAD_OPTION_SET_SYNTAX, arg);
}
}
示例2: processGrammarsOnCommandLine
import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public void processGrammarsOnCommandLine() {
List<GrammarRootAST> sortedGrammars = sortGrammarByTokenVocab(grammarFiles);
for (GrammarRootAST t : sortedGrammars) {
final Grammar g = createGrammar(t);
g.fileName = t.fileName;
if ( gen_dependencies ) {
BuildDependencyGenerator dep =
new BuildDependencyGenerator(this, g);
/*
List outputFiles = dep.getGeneratedFileList();
List dependents = dep.getDependenciesFileList();
System.out.println("output: "+outputFiles);
System.out.println("dependents: "+dependents);
*/
System.out.println(dep.getDependencies().render());
}
else if (errMgr.getNumErrors() == 0) {
process(g, true);
}
}
}
示例3: generateATNs
import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public void generateATNs(Grammar g) {
DOTGenerator dotGenerator = new DOTGenerator(g);
List<Grammar> grammars = new ArrayList<Grammar>();
grammars.add(g);
List<Grammar> imported = g.getAllImportedGrammars();
if ( imported!=null ) grammars.addAll(imported);
for (Grammar ig : grammars) {
for (Rule r : ig.rules.values()) {
try {
String dot = dotGenerator.getDOT(g.atn.ruleToStartState[r.index], g.isLexer());
if (dot != null) {
writeDOTFile(g, r, dot);
}
}
catch (IOException ioe) {
errMgr.toolError(ErrorType.CANNOT_WRITE_FILE, ioe);
}
}
}
}
示例4: getOutputFileWriter
import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
/** This method is used by all code generators to create new output
* files. If the outputDir set by -o is not present it will be created.
* The final filename is sensitive to the output directory and
* the directory where the grammar file was found. If -o is /tmp
* and the original grammar file was foo/t.g4 then output files
* go in /tmp/foo.
*
* The output dir -o spec takes precedence if it's absolute.
* E.g., if the grammar file dir is absolute the output dir is given
* precendence. "-o /tmp /usr/lib/t.g4" results in "/tmp/T.java" as
* output (assuming t.g4 holds T.java).
*
* If no -o is specified, then just write to the directory where the
* grammar file was found.
*
* If outputDirectory==null then write a String.
*/
public Writer getOutputFileWriter(Grammar g, String fileName) throws IOException {
if (outputDirectory == null) {
return new StringWriter();
}
// output directory is a function of where the grammar file lives
// for subdir/T.g4, you get subdir here. Well, depends on -o etc...
File outputDir = getOutputDirectory(g.fileName);
File outputFile = new File(outputDir, fileName);
if (!outputDir.exists()) {
outputDir.mkdirs();
}
FileOutputStream fos = new FileOutputStream(outputFile);
OutputStreamWriter osw;
if ( grammarEncoding!=null ) {
osw = new OutputStreamWriter(fos, grammarEncoding);
}
else {
osw = new OutputStreamWriter(fos);
}
return new BufferedWriter(osw);
}
示例5: ParserFile
import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public ParserFile(OutputModelFactory factory, String fileName) {
super(factory, fileName);
Grammar g = factory.getGrammar();
namedActions = new HashMap<String, Action>();
for (String name : g.namedActions.keySet()) {
ActionAST ast = g.namedActions.get(name);
namedActions.put(name, new Action(factory, ast));
}
genPackage = g.tool.genPackage;
// need the below members in the ST for Python
genListener = g.tool.gen_listener;
genVisitor = g.tool.gen_visitor;
grammarName = g.name;
if (g.getOptionString("contextSuperClass") != null) {
contextSuperClass = new ActionText(null, g.getOptionString("contextSuperClass"));
}
}
示例6: ListenerFile
import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public ListenerFile(OutputModelFactory factory, String fileName) {
super(factory, fileName);
Grammar g = factory.getGrammar();
parserName = g.getRecognizerName();
grammarName = g.name;
for (Rule r : g.rules.values()) {
Map<String, List<Pair<Integer,AltAST>>> labels = r.getAltLabels();
if ( labels!=null ) {
for (Map.Entry<String, List<Pair<Integer, AltAST>>> pair : labels.entrySet()) {
listenerNames.add(pair.getKey());
listenerLabelRuleNames.put(pair.getKey(), r.name);
}
}
else {
// only add rule context if no labels
listenerNames.add(r.name);
}
}
ActionAST ast = g.namedActions.get("header");
if ( ast!=null ) header = new Action(factory, ast);
genPackage = factory.getGrammar().tool.genPackage;
}
示例7: VisitorFile
import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public VisitorFile(OutputModelFactory factory, String fileName) {
super(factory, fileName);
Grammar g = factory.getGrammar();
parserName = g.getRecognizerName();
grammarName = g.name;
for (Rule r : g.rules.values()) {
Map<String, List<Pair<Integer, AltAST>>> labels = r.getAltLabels();
if ( labels!=null ) {
for (Map.Entry<String, List<Pair<Integer, AltAST>>> pair : labels.entrySet()) {
visitorNames.add(pair.getKey());
visitorLabelRuleNames.put(pair.getKey(), r.name);
}
}
else {
// if labels, must label all. no need for generic rule visitor then
visitorNames.add(r.name);
}
}
ActionAST ast = g.namedActions.get("header");
if ( ast!=null ) header = new Action(factory, ast);
genPackage = factory.getGrammar().tool.genPackage;
}
示例8: getRuleDependencies
import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public static Map<Rule, Set<Rule>> getRuleDependencies(Grammar g, Collection<Rule> rules) {
Map<Rule, Set<Rule>> dependencies = new HashMap<Rule, Set<Rule>>();
for (Rule r : rules) {
List<GrammarAST> tokenRefs = r.ast.getNodesWithType(ANTLRParser.TOKEN_REF);
for (GrammarAST tref : tokenRefs) {
Set<Rule> calls = dependencies.get(r);
if ( calls==null ) {
calls = new HashSet<Rule>();
dependencies.put(r, calls);
}
calls.add(g.getRule(tref.getText()));
}
}
return dependencies;
}
示例9: checkGrammarName
import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
void checkGrammarName(Token nameToken) {
String fullyQualifiedName = nameToken.getInputStream().getSourceName();
if (fullyQualifiedName == null) {
// This wasn't read from a file.
return;
}
File f = new File(fullyQualifiedName);
String fileName = f.getName();
if ( g.originalGrammar!=null ) return; // don't warn about diff if this is implicit lexer
if ( !Utils.stripFileExtension(fileName).equals(nameToken.getText()) &&
!fileName.equals(Grammar.GRAMMAR_FROM_STRING_NAME)) {
g.tool.errMgr.grammarError(ErrorType.FILE_AND_GRAMMAR_NAME_DIFFER,
fileName, nameToken, nameToken.getText(), fileName);
}
}
示例10: checkInvalidRuleDef
import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
void checkInvalidRuleDef(Token ruleID) {
String fileName = null;
if ( ruleID.getInputStream()!=null ) {
fileName = ruleID.getInputStream().getSourceName();
}
if ( g.isLexer() && Character.isLowerCase(ruleID.getText().charAt(0)) ) {
g.tool.errMgr.grammarError(ErrorType.PARSER_RULES_NOT_ALLOWED,
fileName, ruleID, ruleID.getText());
}
if ( g.isParser() &&
Grammar.isTokenName(ruleID.getText()) )
{
g.tool.errMgr.grammarError(ErrorType.LEXER_RULES_NOT_ALLOWED,
fileName, ruleID, ruleID.getText());
}
}
示例11: checkImport
import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
void checkImport(Token importID) {
Grammar delegate = g.getImportedGrammar(importID.getText());
if ( delegate==null ) return;
List<Integer> validDelegators = validImportTypes.get(delegate.getType());
if ( validDelegators!=null && !validDelegators.contains(g.getType()) ) {
g.tool.errMgr.grammarError(ErrorType.INVALID_IMPORT,
g.fileName,
importID,
g, delegate);
}
if ( g.isCombined() &&
(delegate.name.equals(g.name+Grammar.getGrammarTypeToFileNameSuffix(ANTLRParser.LEXER))||
delegate.name.equals(g.name+Grammar.getGrammarTypeToFileNameSuffix(ANTLRParser.PARSER))) )
{
g.tool.errMgr.grammarError(ErrorType.IMPORT_NAME_CLASH,
g.fileName,
importID,
g, delegate);
}
}
示例12: SymbolChecks
import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public SymbolChecks(Grammar g, SymbolCollector collector) {
this.g = g;
this.collector = collector;
this.errMgr = g.tool.errMgr;
for (GrammarAST tokenId : collector.tokenIDRefs) {
tokenIDs.add(tokenId.getText());
}
/*
System.out.println("rules="+collector.rules);
System.out.println("rulerefs="+collector.rulerefs);
System.out.println("tokenIDRefs="+collector.tokenIDRefs);
System.out.println("terminals="+collector.terminals);
System.out.println("strings="+collector.strings);
System.out.println("tokensDef="+collector.tokensDefs);
System.out.println("actions="+collector.actions);
System.out.println("scopes="+collector.scopes);
*/
}
示例13: checkRuleArgs
import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public void checkRuleArgs(Grammar g, List<GrammarAST> rulerefs) {
if ( rulerefs==null ) return;
for (GrammarAST ref : rulerefs) {
String ruleName = ref.getText();
Rule r = g.getRule(ruleName);
GrammarAST arg = (GrammarAST)ref.getFirstChildWithType(ANTLRParser.ARG_ACTION);
if ( arg!=null && (r==null || r.args==null) ) {
errMgr.grammarError(ErrorType.RULE_HAS_NO_ARGS,
g.fileName, ref.token, ruleName);
}
else if ( arg==null && (r!=null&&r.args!=null) ) {
errMgr.grammarError(ErrorType.MISSING_RULE_ARGS,
g.fileName, ref.token, ruleName);
}
}
}
示例14: checkForQualifiedRuleIssues
import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public void checkForQualifiedRuleIssues(Grammar g, List<GrammarAST> qualifiedRuleRefs) {
for (GrammarAST dot : qualifiedRuleRefs) {
GrammarAST grammar = (GrammarAST)dot.getChild(0);
GrammarAST rule = (GrammarAST)dot.getChild(1);
g.tool.log("semantics", grammar.getText()+"."+rule.getText());
Grammar delegate = g.getImportedGrammar(grammar.getText());
if ( delegate==null ) {
errMgr.grammarError(ErrorType.NO_SUCH_GRAMMAR_SCOPE,
g.fileName, grammar.token, grammar.getText(),
rule.getText());
}
else {
if ( g.getRule(grammar.getText(), rule.getText())==null ) {
errMgr.grammarError(ErrorType.NO_SUCH_RULE_IN_SCOPE,
g.fileName, rule.token, grammar.getText(),
rule.getText());
}
}
}
}
示例15: isolatedRuleRef
import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public Rule isolatedRuleRef(String x) {
if ( node.resolver instanceof Grammar ) return null;
if ( x.equals(r.name) ) return r;
List<LabelElementPair> labels = null;
if ( node.resolver instanceof Rule ) {
labels = r.getElementLabelDefs().get(x);
}
else if ( node.resolver instanceof Alternative ) {
labels = ((Alternative)node.resolver).labelDefs.get(x);
}
if ( labels!=null ) { // it's a label ref. is it a rule label?
LabelElementPair anyLabelDef = labels.get(0);
if ( anyLabelDef.type==LabelType.RULE_LABEL ) {
return g.getRule(anyLabelDef.element.getText());
}
}
if ( node.resolver instanceof Alternative ) {
if ( ((Alternative)node.resolver).ruleRefs.get(x)!=null ) {
return g.getRule(x);
}
}
return null;
}