Search in sources :

Example 66 with GrammarAST

use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.

the class RuleCollector method discoverLexerRule.

@Override
public void discoverLexerRule(RuleAST rule, GrammarAST ID, List<GrammarAST> modifiers, GrammarAST block) {
    int numAlts = block.getChildCount();
    Rule r = new Rule(g, ID.getText(), rule, numAlts);
    r.mode = currentModeName;
    if (!modifiers.isEmpty())
        r.modifiers = modifiers;
    rules.put(r.name, r);
}
Also used : Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule)

Example 67 with GrammarAST

use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.

the class SemanticPipeline method process.

public void process() {
    if (g.ast == null)
        return;
    // COLLECT RULE OBJECTS
    RuleCollector ruleCollector = new RuleCollector(g);
    ruleCollector.process(g.ast);
    // DO BASIC / EASY SEMANTIC CHECKS
    int prevErrors = g.tool.errMgr.getNumErrors();
    BasicSemanticChecks basics = new BasicSemanticChecks(g, ruleCollector);
    basics.process();
    if (g.tool.errMgr.getNumErrors() > prevErrors)
        return;
    // TRANSFORM LEFT-RECURSIVE RULES
    prevErrors = g.tool.errMgr.getNumErrors();
    LeftRecursiveRuleTransformer lrtrans = new LeftRecursiveRuleTransformer(g.ast, ruleCollector.rules.values(), g);
    lrtrans.translateLeftRecursiveRules();
    // don't continue if we got errors during left-recursion elimination
    if (g.tool.errMgr.getNumErrors() > prevErrors)
        return;
    // STORE RULES IN GRAMMAR
    for (Rule r : ruleCollector.rules.values()) {
        g.defineRule(r);
    }
    // COLLECT SYMBOLS: RULES, ACTIONS, TERMINALS, ...
    SymbolCollector collector = new SymbolCollector(g);
    collector.process(g.ast);
    // CHECK FOR SYMBOL COLLISIONS
    SymbolChecks symcheck = new SymbolChecks(g, collector);
    // side-effect: strip away redef'd rules.
    symcheck.process();
    for (GrammarAST a : collector.namedActions) {
        g.defineAction(a);
    }
    // LINK (outermost) ALT NODES WITH Alternatives
    for (Rule r : g.rules.values()) {
        for (int i = 1; i <= r.numberOfAlts; i++) {
            r.alt[i].ast.alt = r.alt[i];
        }
    }
    // ASSIGN TOKEN TYPES
    g.importTokensFromTokensFile();
    if (g.isLexer()) {
        assignLexerTokenTypes(g, collector.tokensDefs);
    } else {
        assignTokenTypes(g, collector.tokensDefs, collector.tokenIDRefs, collector.terminals);
    }
    symcheck.checkForModeConflicts(g);
    assignChannelTypes(g, collector.channelDefs);
    // CHECK RULE REFS NOW (that we've defined rules in grammar)
    symcheck.checkRuleArgs(g, collector.rulerefs);
    identifyStartRules(collector);
    symcheck.checkForQualifiedRuleIssues(g, collector.qualifiedRulerefs);
    // don't continue if we got symbol errors
    if (g.tool.getNumErrors() > 0)
        return;
    // CHECK ATTRIBUTE EXPRESSIONS FOR SEMANTIC VALIDITY
    AttributeChecks.checkAllAttributeExpressions(g);
    UseDefAnalyzer.trackTokenRuleRefsInActions(g);
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) LeftRecursiveRuleTransformer(org.antlr.v4.analysis.LeftRecursiveRuleTransformer) Rule(org.antlr.v4.tool.Rule)

Example 68 with GrammarAST

use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.

the class SemanticPipeline method assignTokenTypes.

void assignTokenTypes(Grammar g, List<GrammarAST> tokensDefs, List<GrammarAST> tokenIDs, List<GrammarAST> terminals) {
    // create token types for tokens { A, B, C } ALIASES
    for (GrammarAST alias : tokensDefs) {
        if (g.getTokenType(alias.getText()) != Token.INVALID_TYPE) {
            g.tool.errMgr.grammarError(ErrorType.TOKEN_NAME_REASSIGNMENT, g.fileName, alias.token, alias.getText());
        }
        g.defineTokenName(alias.getText());
    }
    // DEFINE TOKEN TYPES FOR TOKEN REFS LIKE ID, INT
    for (GrammarAST idAST : tokenIDs) {
        if (g.getTokenType(idAST.getText()) == Token.INVALID_TYPE) {
            g.tool.errMgr.grammarError(ErrorType.IMPLICIT_TOKEN_DEFINITION, g.fileName, idAST.token, idAST.getText());
        }
        g.defineTokenName(idAST.getText());
    }
    // VERIFY TOKEN TYPES FOR STRING LITERAL REFS LIKE 'while', ';'
    for (GrammarAST termAST : terminals) {
        if (termAST.getType() != ANTLRParser.STRING_LITERAL) {
            continue;
        }
        if (g.getTokenType(termAST.getText()) == Token.INVALID_TYPE) {
            g.tool.errMgr.grammarError(ErrorType.IMPLICIT_STRING_DEFINITION, g.fileName, termAST.token, termAST.getText());
        }
    }
    g.tool.log("semantics", "tokens=" + g.tokenNameToTypeMap);
    g.tool.log("semantics", "strings=" + g.stringLiteralToTypeMap);
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST)

Example 69 with GrammarAST

use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.

the class GrammarTransformPipeline method reduceBlocksToSets.

public void reduceBlocksToSets(GrammarAST root) {
    CommonTreeNodeStream nodes = new CommonTreeNodeStream(new GrammarASTAdaptor(), root);
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor();
    BlockSetTransformer transformer = new BlockSetTransformer(nodes, g);
    transformer.setTreeAdaptor(adaptor);
    transformer.downup(root);
}
Also used : BlockSetTransformer(org.antlr.v4.parse.BlockSetTransformer) GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

Example 70 with GrammarAST

use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.

the class GrammarTransformPipeline method augmentTokensWithOriginalPosition.

public static void augmentTokensWithOriginalPosition(final Grammar g, GrammarAST tree) {
    if (tree == null)
        return;
    List<GrammarAST> optionsSubTrees = tree.getNodesWithType(ANTLRParser.ELEMENT_OPTIONS);
    for (int i = 0; i < optionsSubTrees.size(); i++) {
        GrammarAST t = optionsSubTrees.get(i);
        CommonTree elWithOpt = t.parent;
        if (elWithOpt instanceof GrammarASTWithOptions) {
            Map<String, GrammarAST> options = ((GrammarASTWithOptions) elWithOpt).getOptions();
            if (options.containsKey(LeftRecursiveRuleTransformer.TOKENINDEX_OPTION_NAME)) {
                GrammarToken newTok = new GrammarToken(g, elWithOpt.getToken());
                newTok.originalTokenIndex = Integer.valueOf(options.get(LeftRecursiveRuleTransformer.TOKENINDEX_OPTION_NAME).getText());
                elWithOpt.token = newTok;
                GrammarAST originalNode = g.ast.getNodeWithTokenIndex(newTok.getTokenIndex());
                if (originalNode != null) {
                    // update the AST node start/stop index to match the values
                    // of the corresponding node in the original parse tree.
                    elWithOpt.setTokenStartIndex(originalNode.getTokenStartIndex());
                    elWithOpt.setTokenStopIndex(originalNode.getTokenStopIndex());
                } else {
                    // the original AST node could not be located by index;
                    // make sure to assign valid values for the start/stop
                    // index so toTokenString will not throw exceptions.
                    elWithOpt.setTokenStartIndex(newTok.getTokenIndex());
                    elWithOpt.setTokenStopIndex(newTok.getTokenIndex());
                }
            }
        }
    }
}
Also used : CommonTree(org.antlr.runtime.tree.CommonTree) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) GrammarASTWithOptions(org.antlr.v4.tool.ast.GrammarASTWithOptions) GrammarToken(org.antlr.v4.parse.GrammarToken)

Aggregations

GrammarAST (org.antlr.v4.tool.ast.GrammarAST)55 Rule (org.antlr.v4.tool.Rule)20 ATNState (org.antlr.v4.runtime.atn.ATNState)15 ArrayList (java.util.ArrayList)12 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)12 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)12 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)12 Grammar (org.antlr.v4.tool.Grammar)8 Decl (org.antlr.v4.codegen.model.decl.Decl)7 ActionAST (org.antlr.v4.tool.ast.ActionAST)7 AltAST (org.antlr.v4.tool.ast.AltAST)7 TerminalAST (org.antlr.v4.tool.ast.TerminalAST)7 LinkedHashMap (java.util.LinkedHashMap)6 Token (org.antlr.runtime.Token)6 RuleAST (org.antlr.v4.tool.ast.RuleAST)6 HashMap (java.util.HashMap)5 AddToLabelList (org.antlr.v4.codegen.model.AddToLabelList)5 Pair (org.antlr.v4.runtime.misc.Pair)5 LexerGrammar (org.antlr.v4.tool.LexerGrammar)5 GrammarASTWithOptions (org.antlr.v4.tool.ast.GrammarASTWithOptions)5