use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.
the class BasicSemanticChecks method checkElementIsOuterMostInSingleAlt.
/**
Make sure that action is last element in outer alt; here action,
a2, z, and zz are bad, but a3 is ok:
(RULE A (BLOCK (ALT {action} 'a')))
(RULE B (BLOCK (ALT (BLOCK (ALT {a2} 'x') (ALT 'y')) {a3})))
(RULE C (BLOCK (ALT 'd' {z}) (ALT 'e' {zz})))
*/
protected void checkElementIsOuterMostInSingleAlt(GrammarAST tree) {
CommonTree alt = tree.parent;
CommonTree blk = alt.parent;
boolean outerMostAlt = blk.parent.getType() == RULE;
Tree rule = tree.getAncestor(RULE);
String fileName = tree.getToken().getInputStream().getSourceName();
if (!outerMostAlt || blk.getChildCount() > 1) {
ErrorType e = ErrorType.LEXER_COMMAND_PLACEMENT_ISSUE;
g.tool.errMgr.grammarError(e, fileName, tree.getToken(), rule.getChild(0).getText());
}
}
use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.
the class BasicSemanticChecks method finishRule.
@Override
public void finishRule(RuleAST rule, GrammarAST ID, GrammarAST block) {
if (rule.isLexerRule())
return;
BlockAST blk = (BlockAST) rule.getFirstChildWithType(BLOCK);
int nalts = blk.getChildCount();
GrammarAST idAST = (GrammarAST) rule.getChild(0);
for (int i = 0; i < nalts; i++) {
AltAST altAST = (AltAST) blk.getChild(i);
if (altAST.altLabel != null) {
String altLabel = altAST.altLabel.getText();
// first check that label doesn't conflict with a rule
// label X or x can't be rule x.
Rule r = ruleCollector.rules.get(Utils.decapitalize(altLabel));
if (r != null) {
g.tool.errMgr.grammarError(ErrorType.ALT_LABEL_CONFLICTS_WITH_RULE, g.fileName, altAST.altLabel.token, altLabel, r.name);
}
// Now verify that label X or x doesn't conflict with label
// in another rule. altLabelToRuleName has both X and x mapped.
String prevRuleForLabel = ruleCollector.altLabelToRuleName.get(altLabel);
if (prevRuleForLabel != null && !prevRuleForLabel.equals(rule.getRuleName())) {
g.tool.errMgr.grammarError(ErrorType.ALT_LABEL_REDEF, g.fileName, altAST.altLabel.token, altLabel, rule.getRuleName(), prevRuleForLabel);
}
}
}
List<GrammarAST> altLabels = ruleCollector.ruleToAltLabels.get(rule.getRuleName());
int numAltLabels = 0;
if (altLabels != null)
numAltLabels = altLabels.size();
if (numAltLabels > 0 && nalts != numAltLabels) {
g.tool.errMgr.grammarError(ErrorType.RULE_WITH_TOO_FEW_ALT_LABELS, g.fileName, idAST.token, rule.getRuleName());
}
}
use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.
the class Tool method sortGrammarByTokenVocab.
public List<GrammarRootAST> sortGrammarByTokenVocab(List<String> fileNames) {
// System.out.println(fileNames);
Graph<String> g = new Graph<String>();
List<GrammarRootAST> roots = new ArrayList<GrammarRootAST>();
for (String fileName : fileNames) {
GrammarAST t = parseGrammar(fileName);
// came back as error node
if (t == null || t instanceof GrammarASTErrorNode)
continue;
if (((GrammarRootAST) t).hasErrors)
continue;
GrammarRootAST root = (GrammarRootAST) t;
roots.add(root);
root.fileName = fileName;
String grammarName = root.getChild(0).getText();
GrammarAST tokenVocabNode = findOptionValueAST(root, "tokenVocab");
// Make grammars depend on any tokenVocab options
if (tokenVocabNode != null) {
String vocabName = tokenVocabNode.getText();
// Strip quote characters if any
int len = vocabName.length();
int firstChar = vocabName.charAt(0);
int lastChar = vocabName.charAt(len - 1);
if (len >= 2 && firstChar == '\'' && lastChar == '\'') {
vocabName = vocabName.substring(1, len - 1);
}
// If the name contains a path delimited by forward slashes,
// use only the part after the last slash as the name
int lastSlash = vocabName.lastIndexOf('/');
if (lastSlash >= 0) {
vocabName = vocabName.substring(lastSlash + 1);
}
g.addEdge(grammarName, vocabName);
}
// add cycle to graph so we always process a grammar if no error
// even if no dependency
g.addEdge(grammarName, grammarName);
}
List<String> sortedGrammarNames = g.sort();
// System.out.println("sortedGrammarNames="+sortedGrammarNames);
List<GrammarRootAST> sortedRoots = new ArrayList<GrammarRootAST>();
for (String grammarName : sortedGrammarNames) {
for (GrammarRootAST root : roots) {
if (root.getGrammarName().equals(grammarName)) {
sortedRoots.add(root);
break;
}
}
}
return sortedRoots;
}
use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.
the class ParserATNFactory method newState.
public <T extends ATNState> T newState(Class<T> nodeType, GrammarAST node) {
Exception cause;
try {
Constructor<T> ctor = nodeType.getConstructor();
T s = ctor.newInstance();
if (currentRule == null)
s.setRuleIndex(-1);
else
s.setRuleIndex(currentRule.index);
atn.addState(s);
return s;
} catch (InstantiationException ex) {
cause = ex;
} catch (IllegalAccessException ex) {
cause = ex;
} catch (IllegalArgumentException ex) {
cause = ex;
} catch (InvocationTargetException ex) {
cause = ex;
} catch (NoSuchMethodException ex) {
cause = ex;
} catch (SecurityException ex) {
cause = ex;
}
String message = String.format("Could not create %s of type %s.", ATNState.class.getName(), nodeType.getName());
throw new UnsupportedOperationException(message, cause);
}
use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.
the class ParserATNFactory method rule.
/* start->ruleblock->end */
@Override
public Handle rule(GrammarAST ruleAST, String name, Handle blk) {
Rule r = g.getRule(name);
RuleStartState start = atn.ruleToStartState[r.index];
epsilon(start, blk.left);
RuleStopState stop = atn.ruleToStopState[r.index];
epsilon(blk.right, stop);
Handle h = new Handle(start, stop);
// ATNPrinter ser = new ATNPrinter(g, h.left);
// System.out.println(ruleAST.toStringTree()+":\n"+ser.asString());
ruleAST.atnState = start;
return h;
}
Aggregations