Esempio n. 1
0
 public override void AddTransition(Transition e)
 {
     if (e == null)
     {
         throw new ArgumentException("You can't add a null transition");
     }
     if (_numTransitions > transition.Length)
     {
         throw new ArgumentException("You can only have " + transition.Length + " transitions");
     }
     if (e != null)
     {
         transition[_numTransitions] = e;
         _numTransitions++;
         // Set the "back pointer" of the target state so that it
         // knows about the label of the incoming edge.
         Label label = e.Label;
         if (label.IsAtom || label.IsSet)
         {
             if (((NFAState)e.Target).incidentEdgeLabel != null)
             {
                 ErrorManager.InternalError("Clobbered incident edge");
             }
             ((NFAState)e.Target).incidentEdgeLabel = e.Label;
         }
     }
 }
Esempio n. 2
0
        private void HandleEnclosingRuleScopeAttribute(string x, string y)
        {
            if (IsRuleRefInAlt(x))
            {
                ErrorManager.GrammarError(ErrorManager.MSG_RULE_REF_AMBIG_WITH_RULE_IN_ALT, grammar, actionToken, x);
            }

            StringTemplate st    = null;
            AttributeScope scope = enclosingRule.GetLocalAttributeScope(y);

            if (scope.IsPredefinedRuleScope)
            {
                st = Template("rulePropertyRef_" + y);
                grammar.ReferenceRuleLabelPredefinedAttribute(x);
                st.SetAttribute("scope", x);
                st.SetAttribute("attr", y);
            }
            else if (scope.IsPredefinedLexerRuleScope)
            {
                // perhaps not the most precise error message to use, but...
                ErrorManager.GrammarError(ErrorManager.MSG_RULE_HAS_NO_ARGS, grammar, actionToken, x);
            }
            else if (scope.IsParameterScope)
            {
                st = Template("parameterAttributeRef");
                st.SetAttribute("attr", scope.GetAttribute(y));
            }
            else
            {
                // must be return value
                st = Template("returnAttributeRef");
                st.SetAttribute("ruleDescriptor", enclosingRule);
                st.SetAttribute("attr", scope.GetAttribute(y));
            }
        }
        public void TestNoWildcardAsRootError() /*throws Exception*/
        {
            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);

            string treeGrammar =
                "tree grammar TP;\n" +
                "options {output=AST;}\n" +
                "a : ^(. INT) \n" +
                "  ;\n";

            Grammar   g     = new Grammar(treeGrammar);
            AntlrTool antlr = newTool();

            antlr.SetOutputDirectory(null);   // write to /dev/null
            CodeGenerator generator = new CodeGenerator(antlr, g, "Java");

            g.CodeGenerator = generator;
            generator.GenRecognizer();

            Assert.AreEqual(1, equeue.errors.Count, "unexpected errors: " + equeue);

            int    expectedMsgID                 = ErrorManager.MSG_WILDCARD_AS_ROOT;
            object expectedArg                   = null;
            RecognitionException expectedExc     = null;
            GrammarSyntaxMessage expectedMessage =
                new GrammarSyntaxMessage(expectedMsgID, g, null, expectedArg, expectedExc);

            checkError(equeue, expectedMessage);
        }
Esempio n. 4
0
        public void TestRewriteRuleAndRewriteModeIgnoreActionsPredicates() /*throws Exception*/
        {
            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            Grammar g = new Grammar(
                "tree grammar TP;\n" +
                "options {ASTLabelType=CommonTree; output=template; rewrite=true;}\n" +
                "a: {action} {action2} x=A -> {ick}\n" +
                " | {pred1}? y+=B -> {ick}\n" +
                " | C {action} -> {ick}\n" +
                " | {pred2}?=> z+=D -> {ick}\n" +
                " | (E)=> ^(F G) -> {ick}\n" +
                " ;\n"
                );
            AntlrTool antlr = newTool();

            antlr.SetOutputDirectory(null);   // write to /dev/null
            CodeGenerator generator = new CodeGenerator(antlr, g, "Java");

            g.CodeGenerator = generator;
            generator.GenRecognizer();

            Assert.AreEqual(0, equeue.warnings.Count, "unexpected errors: " + equeue);
        }
Esempio n. 5
0
        private void HandleSetLocalAttribute(string attributeName, string expr)
        {
            StringTemplate st;
            AttributeScope scope = enclosingRule.GetLocalAttributeScope(attributeName);

            if (scope.IsPredefinedRuleScope)
            {
                if (attributeName.Equals("tree") || attributeName.Equals("st"))
                {
                    st = Template("ruleSetPropertyRef_" + attributeName);
                    grammar.ReferenceRuleLabelPredefinedAttribute(enclosingRule.Name);
                    st.SetAttribute("scope", enclosingRule.Name);
                    st.SetAttribute("attr", attributeName);
                    st.SetAttribute("expr", TranslateAction(expr));
                }
                else
                {
                    ErrorManager.GrammarError(ErrorManager.MSG_WRITE_TO_READONLY_ATTR, grammar, actionToken, attributeName, "");
                }
            }
            else if (scope.IsParameterScope)
            {
                st = Template("parameterSetAttributeRef");
                st.SetAttribute("attr", scope.GetAttribute(attributeName));
                st.SetAttribute("expr", TranslateAction(expr));
            }
            else
            {
                st = Template("returnSetAttributeRef");
                st.SetAttribute("ruleDescriptor", enclosingRule);
                st.SetAttribute("attr", scope.GetAttribute(attributeName));
                st.SetAttribute("expr", TranslateAction(expr));
            }
        }
Esempio n. 6
0
        private void HandleIsolatedLexerRuleReference(string name)
        {
            string label = enclosingRule.GetElementLabel(name, outerAltNum, generator);

            CheckElementRefUniqueness(name, false);
            if (label == null)
            {
                ErrorManager.GrammarError(ErrorManager.MSG_FORWARD_ELEMENT_REF, grammar, actionToken, name);
            }
            else
            {
                StringTemplate st = Template("lexerRuleLabel");
                st.SetAttribute("label", label);
            }
        }
Esempio n. 7
0
        private void HandleRuleScopeAttribute(string x, string y)
        {
            Grammar.LabelElementPair pair = enclosingRule.GetRuleLabel(x);
            string refdRuleName           = pair != null ? pair.referencedRuleName : x;
            string label = x;

            if (pair == null)
            {
                // $ruleref.attr  gotta get old label or compute new one
                CheckElementRefUniqueness(x, false);
                label = enclosingRule.GetElementLabel(x, outerAltNum, generator);
                if (label == null)
                {
                    ErrorManager.GrammarError(ErrorManager.MSG_FORWARD_ELEMENT_REF, grammar, actionToken, "$" + x + "." + y);
                    label = x;
                }
            }

            StringTemplate st;
            Rule           refdRule = grammar.GetRule(refdRuleName);
            AttributeScope scope    = refdRule.GetLocalAttributeScope(y);

            if (scope.IsPredefinedRuleScope)
            {
                st = Template("ruleLabelPropertyRef_" + y);
                grammar.ReferenceRuleLabelPredefinedAttribute(refdRuleName);
                st.SetAttribute("scope", label);
                st.SetAttribute("attr", y);
            }
            else if (scope.IsPredefinedLexerRuleScope)
            {
                st = Template("lexerRuleLabelPropertyRef_" + y);
                grammar.ReferenceRuleLabelPredefinedAttribute(refdRuleName);
                st.SetAttribute("scope", label);
                st.SetAttribute("attr", y);
            }
            else if (scope.IsParameterScope)
            {
                // TODO: error!
            }
            else
            {
                st = Template("ruleLabelRef");
                st.SetAttribute("referencedRule", refdRule);
                st.SetAttribute("scope", label);
                st.SetAttribute("attr", scope.GetAttribute(y));
            }
        }
Esempio n. 8
0
        public void TestRewriteRuleAndRewriteModeNotSimple() /*throws Exception*/ {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "tree grammar TP;\n" +
                "options {ASTLabelType=CommonTree; output=template; rewrite=true;}\n" +
                "a  : ID+ -> {ick}\n" +
                "   | INT INT -> {ick}\n" +
                "   ;\n"
            );
            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer();

            Assert.AreEqual(0, equeue.warnings.Count, "unexpected errors: " + equeue);
        }
Esempio n. 9
0
        public override IToken NextToken()
        {
            IToken token = base.NextToken();

            while (token.Type == STRAY_BRACKET)
            {
                ErrorManager.SyntaxError(
                    ErrorManager.MSG_SYNTAX_ERROR,
                    null,
                    token,
                    "antlr: dangling ']'? make sure to escape with \\]",
                    null);

                // skip this token
                token = base.NextToken();
            }

            return(token);
        }
Esempio n. 10
0
        public override void ReportError(RecognitionException ex)
        {
            IToken token = null;

            if (ex is MismatchedTokenException)
            {
                token = ((MismatchedTokenException)ex).Token;
            }
            else if (ex is NoViableAltException)
            {
                token = ((NoViableAltException)ex).Token;
            }
            ErrorManager.SyntaxError(
                ErrorManager.MSG_SYNTAX_ERROR,
                grammar,
                token,
                "codegen: " + ex.ToString(),
                ex);
        }
Esempio n. 11
0
        private void HandleTokenScopeAttribute(string x, string y)
        {
            string label = x;

            if (enclosingRule.GetTokenLabel(x) == null)
            {
                // \tokenref.attr  gotta get old label or compute new one
                CheckElementRefUniqueness(x, true);
                label = enclosingRule.GetElementLabel(x, outerAltNum, generator);
                if (label == null)
                {
                    ErrorManager.GrammarError(ErrorManager.MSG_FORWARD_ELEMENT_REF, grammar, actionToken, "$" + x + "." + y);
                    label = x;
                }
            }
            StringTemplate st = Template("tokenLabelPropertyRef_" + y);

            st.SetAttribute("scope", label);
            st.SetAttribute("attr", AttributeScope.tokenScope.GetAttribute(y));
        }
Esempio n. 12
0
        public void CheckElementRefUniqueness(string @ref, bool isToken)
        {
            IList <GrammarAST> refs = null;

            if (isToken)
            {
                refs = enclosingRule.GetTokenRefsInAlt(@ref, outerAltNum);
            }
            else
            {
                refs = enclosingRule.GetRuleRefsInAlt(@ref, outerAltNum);
            }
            if (refs != null && refs.Count > 1)
            {
                ErrorManager.GrammarError(ErrorManager.MSG_NONUNIQUE_REF,
                                          grammar,
                                          actionToken,
                                          @ref);
            }
        }
Esempio n. 13
0
 public virtual void Run()
 {
     for (int decision = i; decision <= j; decision++)
     {
         NFAState decisionStartState = grammar.GetDecisionNFAStartState(decision);
         if (decisionStartState.NumberOfTransitions > 1)
         {
             grammar.CreateLookaheadDFA(decision, true);
         }
     }
     // now wait for others to finish
     try
     {
         barrier.WaitForRelease();
     }
     catch (OperationCanceledException e)
     {
         ErrorManager.InternalError("what the hell? DFA interruptus", e);
     }
 }
Esempio n. 14
0
        public override void ReportError(RecognitionException ex)
        {
            //Token token = null;
            //try
            //{
            //    token = LT( 1 );
            //}
            //catch ( TokenStreamException tse )
            //{
            //    ErrorManager.internalError( "can't get token???", tse );
            //}
            IToken token = ex.Token;

            ErrorManager.SyntaxError(
                ErrorManager.MSG_SYNTAX_ERROR,
                Grammar,
                token,
                "antlr: " + ex.ToString(),
                ex);
        }
Esempio n. 15
0
        protected virtual void IssueRecursionWarnings()
        {
            // RECURSION OVERFLOW
            ICollection <int> dfaStatesWithRecursionProblems =
                _stateToRecursionOverflowConfigurationsMap.Keys;
            // now walk truly unique (unaliased) list of dfa states with inf recur
            // Goal: create a map from alt to map<target,IList<callsites>>
            // Map<Map<String target, IList<NFAState call sites>>
            IDictionary <int, IDictionary <string, ICollection <NFAState> > > altToTargetToCallSitesMap =
                new Dictionary <int, IDictionary <string, ICollection <NFAState> > >();
            // track a single problem DFA state for each alt
            var altToDFAState = new Dictionary <int, DFAState>();

            ComputeAltToProblemMaps(dfaStatesWithRecursionProblems,
                                    _stateToRecursionOverflowConfigurationsMap,
                                    altToTargetToCallSitesMap, // output param
                                    altToDFAState);            // output param

            // walk each alt with recursion overflow problems and generate error
            ICollection <int> alts       = altToTargetToCallSitesMap.Keys;
            List <int>        sortedAlts = new List <int>(alts);

            sortedAlts.Sort();
            //Collections.sort( sortedAlts );
            foreach (int altI in sortedAlts)
            {
                IDictionary <string, ICollection <NFAState> > targetToCallSiteMap;
                altToTargetToCallSitesMap.TryGetValue(altI, out targetToCallSiteMap);
                var      targetRules    = targetToCallSiteMap.Keys;
                var      callSiteStates = targetToCallSiteMap.Values;
                DFAState sampleBadState;
                altToDFAState.TryGetValue(altI, out sampleBadState);
                ErrorManager.RecursionOverflow(this,
                                               sampleBadState,
                                               altI,
                                               targetRules,
                                               callSiteStates);
            }
        }
Esempio n. 16
0
        private void HandleSetEnclosingRuleScopeAttribute(string x, string y, string expr)
        {
            StringTemplate st    = null;
            AttributeScope scope = enclosingRule.GetLocalAttributeScope(y);

            if (scope.IsPredefinedRuleScope)
            {
                if (y.Equals("st") || y.Equals("tree"))
                {
                    st = Template("ruleSetPropertyRef_" + y);
                    grammar.ReferenceRuleLabelPredefinedAttribute(x);
                    st.SetAttribute("scope", x);
                    st.SetAttribute("attr", y);
                    st.SetAttribute("expr", TranslateAction(expr));
                }
                else
                {
                    ErrorManager.GrammarError(ErrorManager.MSG_WRITE_TO_READONLY_ATTR, grammar, actionToken, x, y);
                }
            }
            else if (scope.IsPredefinedLexerRuleScope)
            {
                // this is a better message to emit than the previous one...
                ErrorManager.GrammarError(ErrorManager.MSG_WRITE_TO_READONLY_ATTR, grammar, actionToken, x, y);
            }
            else if (scope.IsParameterScope)
            {
                st = Template("parameterSetAttributeRef");
                st.SetAttribute("attr", scope.GetAttribute(y));
                st.SetAttribute("expr", TranslateAction(expr));
            }
            else
            { // must be return value
                st = Template("returnSetAttributeRef");
                st.SetAttribute("ruleDescriptor", enclosingRule);
                st.SetAttribute("attr", scope.GetAttribute(y));
                st.SetAttribute("expr", TranslateAction(expr));
            }
        }
Esempio n. 17
0
        public void TestStrayBracketRecovery()
        {
            string grammar =
                "grammar T;\n" +
                "options {output = AST;}\n" +
                "tokens{NODE;}\n" +
                "s : a=ID INT -> ^(NODE[$a]] INT);\n" +
                "ID: 'a'..'z'+;\n" +
                "INT: '0'..'9'+;\n";

            ErrorQueue errorQueue = new ErrorQueue();

            ErrorManager.SetErrorListener(errorQueue);

            bool found =
                rawGenerateAndBuildRecognizer(
                    "T.g", grammar, "TParser", "TLexer", false);

            Assert.IsFalse(found);
            Assert.AreEqual(
                "[error(100): :4:27: syntax error: antlr: dangling ']'? make sure to escape with \\]]",
                '[' + string.Join(", ", errorQueue.errors) + ']');
        }
Esempio n. 18
0
        public void TestMissingAttributeAccessPreventsCodeGeneration()
        {
            string grammar =
                "grammar T;\n" +
                "options {\n" +
                "    backtrack = true; \n" +
                "}\n" +
                "// if b is rule ref, gens bad void x=null code\n" +
                "a : x=b {Object o = $x; System.out.println(\"alt1\");}\n" +
                "  | y=b\n" +
                "  ;\n" +
                "\n" +
                "b : 'a' ;\n";

            ErrorQueue errorQueue = new ErrorQueue();

            ErrorManager.SetErrorListener(errorQueue);
            bool success = rawGenerateAndBuildRecognizer("T.g", grammar, "TParser", "TLexer", false);

            Assert.IsFalse(success);
            Assert.AreEqual(
                "[error(117): " + tmpdir.ToString() + Path.DirectorySeparatorChar + "T.g:6:9: missing attribute access on rule scope: x]",
                '[' + string.Join(", ", errorQueue.errors) + ']');
        }
Esempio n. 19
0
 private void HandleSetRuleScopeAttribute(string x, string y)
 {
     ErrorManager.GrammarError(ErrorManager.MSG_WRITE_TO_READONLY_ATTR, grammar, actionToken, x, y);
 }
Esempio n. 20
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);
                    }
                }
            }
        }
Esempio n. 21
0
 private void HandleUnknownTemplateSyntax()
 {
     chunks.Add(Text);
     ErrorManager.GrammarError(ErrorManager.MSG_INVALID_TEMPLATE_ACTION, grammar, actionToken, Text);
 }