Example #1
0
        public static IDictionary<int, Interval> GetStateToGrammarRegionMap(GrammarRootAST ast, IntervalSet grammarTokenTypes)
        {
            IDictionary<int, Interval> stateToGrammarRegionMap = new Dictionary<int, Interval>();
            if (ast == null)
                return stateToGrammarRegionMap;

            IList<GrammarAST> nodes = ast.GetNodesWithType(grammarTokenTypes);
            foreach (GrammarAST n in nodes)
            {
                if (n.atnState != null)
                {
                    Interval tokenRegion = Interval.Of(n.TokenStartIndex, n.TokenStopIndex);
                    Antlr.Runtime.Tree.ITree ruleNode = null;
                    // RULEs, BLOCKs of transformed recursive rules point to original token interval
                    switch (n.Type)
                    {
                    case ANTLRParser.RULE:
                        ruleNode = n;
                        break;
                    case ANTLRParser.BLOCK:
                    case ANTLRParser.CLOSURE:
                        ruleNode = n.GetAncestor(ANTLRParser.RULE);
                        break;
                    }
                    if (ruleNode is RuleAST)
                    {
                        string ruleName = ((RuleAST)ruleNode).GetRuleName();
                        Rule r = ast.g.GetRule(ruleName);
                        if (r is LeftRecursiveRule)
                        {
                            RuleAST originalAST = ((LeftRecursiveRule)r).GetOriginalAST();
                            tokenRegion = Interval.Of(originalAST.TokenStartIndex, originalAST.TokenStopIndex);
                        }
                    }
                    stateToGrammarRegionMap[n.atnState.stateNumber] = tokenRegion;
                }
            }
            return stateToGrammarRegionMap;
        }
Example #2
0
        /** Return list of (TOKEN_NAME node, 'literal' node) pairs */
        public static IList<System.Tuple<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 . .) .)))",
			// TODO: allow doc comment in there
		};
            GrammarASTAdaptor adaptor = new GrammarASTAdaptor(ast.Token.InputStream);
            Antlr.Runtime.Tree.TreeWizard wiz = new Antlr.Runtime.Tree.TreeWizard(adaptor, ANTLRParser.tokenNames);
            IList<System.Tuple<GrammarAST, GrammarAST>> lexerRuleToStringLiteral =
                new List<System.Tuple<GrammarAST, GrammarAST>>();

            IList<GrammarAST> ruleNodes = ast.GetNodesWithType(ANTLRParser.RULE);
            if (ruleNodes == null || ruleNodes.Count == 0)
                return null;

            foreach (GrammarAST r in ruleNodes)
            {
                //tool.log("grammar", r.toStringTree());
                //			System.out.println("chk: "+r.toStringTree());
                Antlr.Runtime.Tree.ITree name = r.GetChild(0);
                if (name.Type == ANTLRParser.TOKEN_REF)
                {
                    // check rule against patterns
                    bool isLitRule;
                    foreach (string pattern in patterns)
                    {
                        isLitRule =
                            DefAlias(r, pattern, wiz, lexerRuleToStringLiteral);
                        if (isLitRule)
                            break;
                    }
                    //				if ( !isLitRule ) System.out.println("no pattern matched");
                }
            }
            return lexerRuleToStringLiteral;
        }