use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.
the class SemanticPipeline method hasTypeOrMoreCommand.
boolean hasTypeOrMoreCommand(@NotNull Rule r) {
GrammarAST ast = r.ast;
if (ast == null) {
return false;
}
GrammarAST altActionAst = (GrammarAST) ast.getFirstDescendantWithType(ANTLRParser.LEXER_ALT_ACTION);
if (altActionAst == null) {
// the rule isn't followed by any commands
return false;
}
// first child is the alt itself, subsequent are the actions
for (int i = 1; i < altActionAst.getChildCount(); i++) {
GrammarAST node = (GrammarAST) altActionAst.getChild(i);
if (node.getType() == ANTLRParser.LEXER_ACTION_CALL) {
if ("type".equals(node.getChild(0).getText())) {
return true;
}
} else if ("more".equals(node.getText())) {
return true;
}
}
return false;
}
use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.
the class SymbolChecks method checkForModeConflicts.
public void checkForModeConflicts(Grammar g) {
if (g.isLexer()) {
LexerGrammar lexerGrammar = (LexerGrammar) g;
for (String modeName : lexerGrammar.modes.keySet()) {
if (!modeName.equals("DEFAULT_MODE") && reservedNames.contains(modeName)) {
Rule rule = lexerGrammar.modes.get(modeName).iterator().next();
g.tool.errMgr.grammarError(ErrorType.MODE_CONFLICTS_WITH_COMMON_CONSTANTS, g.fileName, rule.ast.parent.getToken(), modeName);
}
if (g.getTokenType(modeName) != Token.INVALID_TYPE) {
Rule rule = lexerGrammar.modes.get(modeName).iterator().next();
g.tool.errMgr.grammarError(ErrorType.MODE_CONFLICTS_WITH_TOKEN, g.fileName, rule.ast.parent.getToken(), modeName);
}
}
}
}
use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.
the class SymbolChecks method checkRuleArgs.
// CAN ONLY CALL THE TWO NEXT METHODS AFTER GRAMMAR HAS RULE DEFS (see semanticpipeline)
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);
}
}
}
use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.
the class UseDefAnalyzer method trackTokenRuleRefsInActions.
// side-effect: updates Alternative with refs in actions
public static void trackTokenRuleRefsInActions(Grammar g) {
for (Rule r : g.rules.values()) {
for (int i = 1; i <= r.numberOfAlts; i++) {
Alternative alt = r.alt[i];
for (ActionAST a : alt.actions) {
ActionSniffer sniffer = new ActionSniffer(g, r, alt, a, a.token);
sniffer.examineAction();
}
}
}
}
use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.
the class RuleFunction method addContextGetters.
public void addContextGetters(OutputModelFactory factory, Collection<RuleAST> contextASTs) {
List<AltAST> unlabeledAlternatives = new ArrayList<AltAST>();
Map<String, List<AltAST>> labeledAlternatives = new LinkedHashMap<String, List<AltAST>>();
for (RuleAST ast : contextASTs) {
try {
unlabeledAlternatives.addAll(rule.g.getUnlabeledAlternatives(ast));
for (Map.Entry<String, List<Tuple2<Integer, AltAST>>> entry : rule.g.getLabeledAlternatives(ast).entrySet()) {
List<AltAST> list = labeledAlternatives.get(entry.getKey());
if (list == null) {
list = new ArrayList<AltAST>();
labeledAlternatives.put(entry.getKey(), list);
}
for (Tuple2<Integer, AltAST> tuple : entry.getValue()) {
list.add(tuple.getItem2());
}
}
} catch (RecognitionException ex) {
}
}
// Add ctx labels for elements in alts with no '#' label
if (!unlabeledAlternatives.isEmpty()) {
Set<Decl> decls = getDeclsForAllElements(unlabeledAlternatives);
// put directly in base context
for (Decl decl : decls) {
ruleCtx.addDecl(decl);
}
}
// make structs for '#' labeled alts, define ctx labels for elements
altLabelCtxs = new LinkedHashMap<String, AltLabelStructDecl>();
if (!labeledAlternatives.isEmpty()) {
for (Map.Entry<String, List<AltAST>> entry : labeledAlternatives.entrySet()) {
AltLabelStructDecl labelDecl = new AltLabelStructDecl(factory, rule, entry.getKey());
altLabelCtxs.put(entry.getKey(), labelDecl);
Set<Decl> decls = getDeclsForAllElements(entry.getValue());
for (Decl decl : decls) {
labelDecl.addDecl(decl);
}
}
}
}
Aggregations