Search in sources :

Example 6 with GrammarASTWithOptions

use of org.antlr.v4.tool.ast.GrammarASTWithOptions 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

GrammarASTWithOptions (org.antlr.v4.tool.ast.GrammarASTWithOptions)5 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)4 ArrayList (java.util.ArrayList)2 CommonToken (org.antlr.runtime.CommonToken)2 Token (org.antlr.runtime.Token)2 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)2 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)2 Rule (org.antlr.v4.tool.Rule)2 TerminalAST (org.antlr.v4.tool.ast.TerminalAST)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 CommonTree (org.antlr.runtime.tree.CommonTree)1 GrammarToken (org.antlr.v4.parse.GrammarToken)1 ATNState (org.antlr.v4.runtime.atn.ATNState)1 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)1 RuleTransition (org.antlr.v4.runtime.atn.RuleTransition)1 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)1 Pair (org.antlr.v4.runtime.misc.Pair)1 AltAST (org.antlr.v4.tool.ast.AltAST)1