use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.
the class Grammar method defAlias.
protected static boolean defAlias(GrammarAST r, String pattern, org.antlr.runtime.tree.TreeWizard wiz, List<Pair<GrammarAST, GrammarAST>> lexerRuleToStringLiteral) {
HashMap<String, Object> nodes = new HashMap<String, Object>();
if (wiz.parse(r, pattern, nodes)) {
GrammarAST litNode = (GrammarAST) nodes.get("lit");
GrammarAST nameNode = (GrammarAST) nodes.get("name");
Pair<GrammarAST, GrammarAST> pair = new Pair<GrammarAST, GrammarAST>(nameNode, litNode);
lexerRuleToStringLiteral.add(pair);
return true;
}
return false;
}
use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.
the class Grammar method getStringLiteralAliasesFromLexerRules.
/** Return list of (TOKEN_NAME node, 'literal' node) pairs */
public static List<Pair<GrammarAST, GrammarAST>> getStringLiteralAliasesFromLexerRules(GrammarRootAST ast) {
String[] patterns = { "(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL)))", "(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL ACTION)))", "(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL SEMPRED)))", "(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) .)))", "(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) . .)))", "(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) (LEXER_ACTION_CALL . .))))", "(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) . (LEXER_ACTION_CALL . .))))", "(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) (LEXER_ACTION_CALL . .) .)))" };
GrammarASTAdaptor adaptor = new GrammarASTAdaptor(ast.token.getInputStream());
org.antlr.runtime.tree.TreeWizard wiz = new org.antlr.runtime.tree.TreeWizard(adaptor, ANTLRParser.tokenNames);
List<Pair<GrammarAST, GrammarAST>> lexerRuleToStringLiteral = new ArrayList<Pair<GrammarAST, GrammarAST>>();
List<GrammarAST> ruleNodes = ast.getNodesWithType(ANTLRParser.RULE);
if (ruleNodes == null || ruleNodes.isEmpty())
return null;
for (GrammarAST r : ruleNodes) {
//tool.log("grammar", r.toStringTree());
// System.out.println("chk: "+r.toStringTree());
org.antlr.runtime.tree.Tree name = r.getChild(0);
if (name.getType() == ANTLRParser.TOKEN_REF) {
// check rule against patterns
boolean isLitRule;
for (String pattern : patterns) {
isLitRule = defAlias(r, pattern, wiz, lexerRuleToStringLiteral);
if (isLitRule)
break;
}
// if ( !isLitRule ) System.out.println("no pattern matched");
}
}
return lexerRuleToStringLiteral;
}
use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.
the class BasicSemanticChecks method checkNumRules.
void checkNumRules(GrammarAST rulesNode) {
if (rulesNode.getChildCount() == 0) {
GrammarAST root = (GrammarAST) rulesNode.getParent();
GrammarAST IDNode = (GrammarAST) root.getChild(0);
g.tool.errMgr.grammarError(ErrorType.NO_RULES, g.fileName, null, IDNode.getText(), g);
}
}
use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.
the class BasicSemanticChecks method finishPrequels.
@Override
public void finishPrequels(GrammarAST firstPrequel) {
if (firstPrequel == null)
return;
GrammarAST parent = (GrammarAST) firstPrequel.parent;
List<GrammarAST> options = parent.getAllChildrenWithType(OPTIONS);
List<GrammarAST> imports = parent.getAllChildrenWithType(IMPORT);
List<GrammarAST> tokens = parent.getAllChildrenWithType(TOKENS_SPEC);
checkNumPrequels(options, imports, tokens);
}
use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.
the class RuleCollector method discoverRule.
@Override
public void discoverRule(RuleAST rule, GrammarAST ID, List<GrammarAST> modifiers, ActionAST arg, ActionAST returns, GrammarAST thrws, GrammarAST options, ActionAST locals, List<GrammarAST> actions, GrammarAST block) {
int numAlts = block.getChildCount();
Rule r;
if (LeftRecursiveRuleAnalyzer.hasImmediateRecursiveRuleRefs(rule, ID.getText())) {
r = new LeftRecursiveRule(g, ID.getText(), rule);
} else {
r = new Rule(g, ID.getText(), rule, numAlts);
}
rules.put(r.name, r);
if (arg != null) {
r.args = ScopeParser.parseTypedArgList(arg, arg.getText(), g);
r.args.type = AttributeDict.DictType.ARG;
r.args.ast = arg;
arg.resolver = r.alt[currentOuterAltNumber];
}
if (returns != null) {
r.retvals = ScopeParser.parseTypedArgList(returns, returns.getText(), g);
r.retvals.type = AttributeDict.DictType.RET;
r.retvals.ast = returns;
}
if (locals != null) {
r.locals = ScopeParser.parseTypedArgList(locals, locals.getText(), g);
r.locals.type = AttributeDict.DictType.LOCAL;
r.locals.ast = locals;
}
for (GrammarAST a : actions) {
// a = ^(AT ID ACTION)
ActionAST action = (ActionAST) a.getChild(1);
r.namedActions.put(a.getChild(0).getText(), action);
action.resolver = r;
}
}
Aggregations