示例#1
0
        public void EnterEveryRule(ParserRuleContext ctx)
        {
            string ruleName             = ruleNames[ctx.RuleIndex];
            IList <TerminalNode> tnodes = getDirectTerminalChildren(ctx);
            // Find all ordered unique pairs of literals;
            // no (a,a) pairs and only literals like '{', 'begin', '}', ...
            // Add a for (a,a) into ruleToRepeatedTokensSet for later filtering
            RuleAltKey ruleAltKey = new RuleAltKey(ruleName, ctx.getAltNumber());

            for (int i = 0; i < tnodes.Count; i++)
            {
                for (int j = i + 1; j < tnodes.Count; j++)
                {
                    TerminalNode a     = tnodes[i];
                    TerminalNode b     = tnodes[j];
                    int          atype = a.Symbol.Type;
                    int          btype = b.Symbol.Type;

                    // KED: THIS CODE DOES NOT WORK WITH GRAMMARS CONTAINING FRAGMENTS.
                    // IN ANTLRV4LEXER.G4 THAT IS IN THIS DIRECTORY, THE GRAMMAR DOES NOT USE
                    // FRAGMENT FOR COLON. BUT THE G4 GRAMMAR IN THE ANTLR GRAMMARS-G4 EXAMPLES,
                    // IT DOES. CONSEQUENTLY, GETLITERALNAME RETURNS NULL!
                    // FRAGMENTS AREN'T PART OF THE VOCABULARY, SO THIS CODE DOES NOT WORK!!

                    // only include literals like '{' and ':' not IDENTIFIER etc...
                    if (vocab.GetLiteralName(atype) == null || vocab.GetLiteralName(btype) == null)
                    {
                        continue;
                    }

                    if (atype == btype)
                    {
                        ISet <int> repeatedTokensSet = null;
                        if (!ruleToRepeatedTokensSet.TryGetValue(ruleAltKey, out repeatedTokensSet))
                        {
                            repeatedTokensSet = new HashSet <int>();
                            ruleToRepeatedTokensSet[ruleAltKey] = repeatedTokensSet;
                        }
                        repeatedTokensSet.Add(atype);
                    }
                    else
                    {
                        org.antlr.codebuff.misc.Pair <int, int>         pair     = new org.antlr.codebuff.misc.Pair <int, int>(atype, btype);
                        ISet <org.antlr.codebuff.misc.Pair <int, int> > pairsBag = null;
                        if (!ruleToPairsBag.TryGetValue(ruleAltKey, out pairsBag))
                        {
                            pairsBag = new HashSet <org.antlr.codebuff.misc.Pair <int, int> >();
                            ruleToPairsBag[ruleAltKey] = pairsBag;
                        }
                        pairsBag.Add(pair);
                    }
                }
            }
        }