Exemple #1
0
        /** Get the last disabled alt number and check in the grammar to see
         *  if that alt is a simple wildcard.  If so, treat like an else clause
         *  and don't emit the error.  Strip out the last alt if it's wildcard.
         */
        protected virtual void StripWildCardAlts(ICollection <int> disabledAlts)
        {
            List <int> sortedDisableAlts = new List <int>(disabledAlts);

            sortedDisableAlts.Sort();
            //Collections.sort( sortedDisableAlts );
            int lastAlt =
                (int)sortedDisableAlts[sortedDisableAlts.Count - 1];
            GrammarAST blockAST =
                dfa.nfa.grammar.GetDecisionBlockAST(dfa.decisionNumber);
            //[email protected]("block with error = "+blockAST.toStringTree());
            GrammarAST lastAltAST = null;

            if (blockAST.GetChild(0).Type == ANTLRParser.OPTIONS)
            {
                // if options, skip first child: ( options { ( = greedy false ) )
                lastAltAST = (GrammarAST)blockAST.GetChild(lastAlt);
            }
            else
            {
                lastAltAST = (GrammarAST)blockAST.GetChild(lastAlt - 1);
            }
            //[email protected]("last alt is "+lastAltAST.toStringTree());
            // if last alt looks like ( ALT . <end-of-alt> ) then wildcard
            // Avoid looking at optional blocks etc... that have last alt
            // as the EOB:
            // ( BLOCK ( ALT 'else' statement <end-of-alt> ) <end-of-block> )
            if (lastAltAST.Type != ANTLRParser.EOB &&
                lastAltAST.GetChild(0).Type == ANTLRParser.WILDCARD &&
                lastAltAST.GetChild(1).Type == ANTLRParser.EOA)
            {
                //[email protected]("wildcard");
                disabledAlts.Remove(lastAlt);
            }
        }
Exemple #2
0
        /** Rewrite alt to have a synpred as first element;
         *  (xxx)=>xxx
         *  but only if they didn't specify one manually.
         */
        protected virtual void PrefixWithSynPred(GrammarAST alt)
        {
            // if they want backtracking and it's not a lexer rule in combined grammar
            string autoBacktrack = (string)Grammar.GetBlockOption(currentBlockAST, "backtrack");

            if (autoBacktrack == null)
            {
                autoBacktrack = (string)Grammar.GetOption("backtrack");
            }
            if (autoBacktrack != null && autoBacktrack.Equals("true") &&
                !(GrammarType == GrammarType.Combined &&
                  Rule.GetRuleType(currentRuleName) == RuleType.Lexer) &&
                alt.GetChild(0).Type != SYN_SEMPRED)
            {
                // duplicate alt and make a synpred block around that dup'd alt
                GrammarAST synpredBlockAST = CreateBlockFromDupAlt(alt);

                // Create a BACKTRACK_SEMPRED node as if user had typed this in
                // Effectively we replace (xxx)=>xxx with {synpredxxx}? xxx
                GrammarAST synpredAST = CreateSynSemPredFromBlock(synpredBlockAST,
                                                                  BACKTRACK_SEMPRED);

                // insert BACKTRACK_SEMPRED as first element of alt
                //synpredAST.getLastSibling().setNextSibling( alt.getFirstChild() );
                //synpredAST.addChild( alt.getFirstChild() );
                //alt.setFirstChild( synpredAST );
                GrammarAST[] children = alt.GetChildrenAsArray();
                adaptor.SetChild(alt, 0, synpredAST);
                for (int i = 0; i < children.Length; i++)
                {
                    if (i < children.Length - 1)
                    {
                        adaptor.SetChild(alt, i + 1, children[i]);
                    }
                    else
                    {
                        adaptor.AddChild(alt, children[i]);
                    }
                }
            }
        }
Exemple #3
0
        public virtual void IssueWarnings()
        {
            // NONREGULAR DUE TO RECURSION > 1 ALTS
            // Issue this before aborted analysis, which might also occur
            // if we take too long to terminate
            if (_nonLLStarDecision && !dfa.AutoBacktrackMode)
            {
                ErrorManager.NonLLStarDecision(this);
            }

            IssueRecursionWarnings();

            // generate a separate message for each problem state in DFA
            ICollection <DFAState> resolvedStates = NondeterministicStatesResolvedWithSemanticPredicate;
            ICollection <DFAState> problemStates  = DFAStatesWithSyntacticallyAmbiguousAlts;

            if (problemStates.Count > 0)
            {
                foreach (DFAState d in problemStates)
                {
                    if (dfa.nfa.grammar.NFAToDFAConversionExternallyAborted())
                    {
                        break;
                    }

                    IDictionary <int, ICollection <IToken> > insufficientAltToLocations = GetIncompletelyCoveredAlts(d);
                    if (insufficientAltToLocations != null && insufficientAltToLocations.Count > 0)
                    {
                        ErrorManager.InsufficientPredicates(this, d, insufficientAltToLocations);
                    }
                    // don't report problem if resolved
                    if (resolvedStates == null || !resolvedStates.Contains(d))
                    {
                        // first strip last alt from disableAlts if it's wildcard
                        // then don't print error if no more disable alts
                        ICollection <int> disabledAlts = GetDisabledAlternatives(d);
                        StripWildCardAlts(disabledAlts);
                        if (disabledAlts.Count > 0)
                        {
                            // nondeterminism; same input predicts multiple alts.
                            // but don't emit error if greedy=true explicitly set
                            bool       explicitlyGreedy = false;
                            GrammarAST blockAST         = d.dfa.nfa.grammar.GetDecisionBlockAST(d.dfa.decisionNumber);
                            if (blockAST != null)
                            {
                                String greedyS = (String)blockAST.GetBlockOption("greedy");
                                if (greedyS != null && greedyS.Equals("true"))
                                {
                                    explicitlyGreedy = true;
                                }
                            }

                            if (!explicitlyGreedy)
                            {
                                ErrorManager.Nondeterminism(this, d);
                            }
                        }
                    }
                }
            }

            ICollection <DFAState> danglingStates = DanglingStates;

            if (danglingStates.Count > 0)
            {
                //Console.Error.WriteLine( "no emanating edges for states: " + danglingStates );
                foreach (DFAState d in danglingStates)
                {
                    ErrorManager.DanglingState(this, d);
                }
            }

            if (!_nonLLStarDecision)
            {
                var unreachableAlts = dfa.UnreachableAlts;
                if (unreachableAlts != null && unreachableAlts.Count > 0)
                {
                    // give different msg if it's an empty Tokens rule from delegate
                    bool isInheritedTokensRule = false;
                    if (dfa.IsTokensRuleDecision)
                    {
                        foreach (int altI in unreachableAlts)
                        {
                            GrammarAST decAST             = dfa.DecisionASTNode;
                            GrammarAST altAST             = (GrammarAST)decAST.GetChild(altI - 1);
                            GrammarAST delegatedTokensAlt =
                                (GrammarAST)altAST.GetFirstChildWithType(ANTLRParser.DOT);
                            if (delegatedTokensAlt != null)
                            {
                                isInheritedTokensRule = true;
                                ErrorManager.GrammarWarning(ErrorManager.MSG_IMPORTED_TOKENS_RULE_EMPTY,
                                                            dfa.nfa.grammar,
                                                            null,
                                                            dfa.nfa.grammar.name,
                                                            delegatedTokensAlt.GetChild(0).Text);
                            }
                        }
                    }
                    if (isInheritedTokensRule)
                    {
                    }
                    else
                    {
                        ErrorManager.UnreachableAlts(this, unreachableAlts);
                    }
                }
            }
        }
        private void HandleNotElementEnd(GrammarAST n, GrammarAST label, GrammarAST astSuffix, out Template code, IIntSet elements, GrammarAST start)
        {
            if (n.GetChild(0) != start)
                throw new System.InvalidOperationException();

            string labelText = null;
            if (label != null)
                labelText = label.Text;

            code = GetTokenElementST("matchSet", "set", (GrammarAST)n.GetChild(0), astSuffix, labelText);
            code.SetAttribute("s", generator.GenSetExpr(templates, elements, 1, false));
            int i = n.Token.TokenIndex;
            code.SetAttribute("elementIndex", i);
            if (grammar.type != GrammarType.Lexer)
                generator.GenerateLocalFollow(n, "set", currentRuleName, i);
        }
 private void HandleRulesInit(GrammarAST start, out string ruleName, out bool generated)
 {
     ruleName = start.GetChild(0).Text;
     generated = grammar.GenerateMethodForRule(ruleName);
 }
 private void HandleRuleInit(GrammarAST start, out string initAction, out GrammarAST block2, out Antlr3.Analysis.DFA dfa, out Rule ruleDescr, out string description)
 {
     initAction = null;
     // get the dfa for the BLOCK
     block2 = (GrammarAST)start.GetFirstChildWithType(BLOCK);
     dfa = block2.LookaheadDFA;
     // init blockNestingLevel so it's block level RULE_BLOCK_NESTING_LEVEL
     // for alts of rule
     blockNestingLevel = RULE_BLOCK_NESTING_LEVEL - 1;
     ruleDescr = grammar.GetRule(start.GetChild(0).Text);
     currentRuleName = start.GetChild(0).Text;
     description = string.Empty;
 }
        /** Rewrite alt to have a synpred as first element;
         *  (xxx)=>xxx
         *  but only if they didn't specify one manually.
         */
        protected virtual void PrefixWithSynPred( GrammarAST alt )
        {
            // if they want backtracking and it's not a lexer rule in combined grammar
            string autoBacktrack = (string)Grammar.GetBlockOption( currentBlockAST, "backtrack" );
            if ( autoBacktrack == null )
            {
                autoBacktrack = (string)Grammar.GetOption( "backtrack" );
            }
            if ( autoBacktrack != null && autoBacktrack.Equals( "true" ) &&
                 !( GrammarType == GrammarType.Combined &&
                 Rule.GetRuleType(currentRuleName) == RuleType.Lexer) &&
                 alt.GetChild( 0 ).Type != SYN_SEMPRED )
            {
                // duplicate alt and make a synpred block around that dup'd alt
                GrammarAST synpredBlockAST = CreateBlockFromDupAlt( alt );

                // Create a BACKTRACK_SEMPRED node as if user had typed this in
                // Effectively we replace (xxx)=>xxx with {synpredxxx}? xxx
                GrammarAST synpredAST = CreateSynSemPredFromBlock( synpredBlockAST,
                                                                  BACKTRACK_SEMPRED );

                // insert BACKTRACK_SEMPRED as first element of alt
                //synpredAST.getLastSibling().setNextSibling( alt.getFirstChild() );
                //synpredAST.addChild( alt.getFirstChild() );
                //alt.setFirstChild( synpredAST );
                GrammarAST[] children = alt.GetChildrenAsArray();
                adaptor.SetChild( alt, 0, synpredAST );
                for ( int i = 0; i < children.Length; i++ )
                {
                    if ( i < children.Length - 1 )
                        adaptor.SetChild( alt, i + 1, children[i] );
                    else
                        adaptor.AddChild( alt, children[i] );
                }
            }
        }