예제 #1
0
        protected StringTemplate GetRuleElementST(string name,
                                                  string ruleTargetName,
                                                  GrammarAST elementAST,
                                                  GrammarAST ast_suffix,
                                                  string label)
        {
            string suffix = GetSTSuffix(elementAST, ast_suffix, label);

            name += suffix;
            // if we're building trees and there is no label, gen a label
            // unless we're in a synpred rule.
            Rule r = grammar.GetRule(currentRuleName);

            if ((grammar.BuildAST || suffix.Length > 0) && label == null &&
                (r == null || !r.isSynPred))
            {
                // we will need a label to do the AST or tracking, make one
                label = generator.CreateUniqueLabel(ruleTargetName);
                CommonToken labelTok = new CommonToken(ANTLRParser.ID, label);
                grammar.DefineRuleRefLabel(currentRuleName, labelTok, elementAST);
            }
            StringTemplate elementST = templates.GetInstanceOf(name);

            if (label != null)
            {
                elementST.SetAttribute("label", label);
            }
            return(elementST);
        }
예제 #2
0
        public virtual GrammarAST CreateSimpleRuleAST(string name, GrammarAST block, bool fragment)
        {
            GrammarAST modifier = null;

            if (fragment)
            {
                modifier = (GrammarAST)adaptor.Create(FRAGMENT, "fragment");
            }

            /*
             * EOBAST = block.getLastChild()
             * ^(RULE[block,"rule"] ID["name"] {modifier} ARG["ARG"] RET["RET"] SCOPE["scope"] {block} EOR[EOBAST,"<end-of-rule>"])
             */
            GrammarAST rule = (GrammarAST)adaptor.Create(RULE, block.Token, "rule");

            adaptor.AddChild(rule, adaptor.Create(ID, name));
            if (modifier != null)
            {
                adaptor.AddChild(rule, modifier);
            }
            adaptor.AddChild(rule, adaptor.Create(ARG, "ARG"));
            adaptor.AddChild(rule, adaptor.Create(RET, "RET"));
            adaptor.AddChild(rule, adaptor.Create(SCOPE, "scope"));
            adaptor.AddChild(rule, block);
            adaptor.AddChild(rule, adaptor.Create(EOR, block.LastChild.Token, "<end-of-rule>"));

            return(rule);
        }
예제 #3
0
 public Predicate(Predicate p)
 {
     this.predicateAST  = p.predicateAST;
     this._gated        = p._gated;
     this._synpred      = p._synpred;
     this.constantValue = p.constantValue;
 }
예제 #4
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);
            }
        }
예제 #5
0
 public Predicate(GrammarAST predicate)
 {
     this.predicateAST = predicate;
     this._gated       =
         predicate.Type == ANTLRParser.GATED_SEMPRED ||
         predicate.Type == ANTLRParser.SYN_SEMPRED;
     this._synpred =
         predicate.Type == ANTLRParser.SYN_SEMPRED ||
         predicate.Type == ANTLRParser.BACKTRACK_SEMPRED;
 }
예제 #6
0
        protected StringTemplate GetWildcardST(GrammarAST elementAST, GrammarAST ast_suffix, string label)
        {
            string name = "wildcard";

            if (grammar.type == GrammarType.Lexer)
            {
                name = "wildcardChar";
            }
            return(GetTokenElementST(name, name, elementAST, ast_suffix, label));
        }
예제 #7
0
            public override object Create(IToken payload)
            {
                GrammarAST t = new GrammarAST(payload);

                if (_outer != null)
                {
                    t.enclosingRuleName = _outer.currentRuleName;
                }
                return(t);
            }
예제 #8
0
 public ActionTranslator( CodeGenerator generator,
                              string ruleName,
                              GrammarAST actionAST )
     : this(new ANTLRStringStream( actionAST.Token.Text ))
 {
     this.generator = generator;
     this.grammar = generator.grammar;
     this.enclosingRule = grammar.GetLocallyDefinedRule( ruleName );
     this.actionToken = actionAST.Token;
     this.outerAltNum = actionAST.outerAltNum;
 }
예제 #9
0
 public ActionTranslator(CodeGenerator generator,
                         string ruleName,
                         GrammarAST actionAST)
     : this(new ANTLRStringStream(actionAST.Token.Text))
 {
     this.generator     = generator;
     this.grammar       = generator.Grammar;
     this.enclosingRule = grammar.GetLocallyDefinedRule(ruleName);
     this.actionToken   = actionAST.Token;
     this.outerAltNum   = actionAST.outerAltNum;
 }
예제 #10
0
        protected virtual GrammarAST CreateSynSemPredFromBlock(GrammarAST synpredBlockAST, int synpredTokenType)
        {
            // add grammar fragment to a list so we can make fake rules for them later.
            string predName = Grammar.DefineSyntacticPredicate(synpredBlockAST, currentRuleName);
            // convert (alpha)=> into {synpredN}? where N is some pred count
            // during code gen we convert to function call with templates
            string     synpredinvoke = predName;
            GrammarAST p             = (GrammarAST)adaptor.Create(synpredTokenType, synpredinvoke);

            // track how many decisions have synpreds
            Grammar.blocksWithSynPreds.Add(currentBlockAST);
            return(p);
        }
예제 #11
0
 public virtual void Cleanup(GrammarAST root)
 {
     if (GrammarType == GrammarType.Lexer)
     {
         string     filter        = (string)Grammar.GetOption("filter");
         GrammarAST tokensRuleAST =
             Grammar.AddArtificialMatchTokensRule(
                 root,
                 Grammar.lexerRuleNamesInCombined,
                 Grammar.GetDelegateNames(),
                 filter != null && filter.Equals("true"));
     }
 }
예제 #12
0
        protected StringTemplate GetTokenElementST(string name,
                                                   string elementName,
                                                   GrammarAST elementAST,
                                                   GrammarAST ast_suffix,
                                                   string label)
        {
            bool tryUnchecked = false;

            if (name == "matchSet" && !string.IsNullOrEmpty(elementAST.enclosingRuleName) && Rule.GetRuleType(elementAST.enclosingRuleName) == RuleType.Lexer)
            {
                if ((elementAST.Parent.Type == ANTLRLexer.ALT && elementAST.Parent.Parent.Parent.Type == RULE && elementAST.Parent.Parent.ChildCount == 2) ||
                    (elementAST.Parent.Type == ANTLRLexer.NOT && elementAST.Parent.Parent.Parent.Parent.Type == RULE && elementAST.Parent.Parent.Parent.ChildCount == 2))
                {
                    // single alt at the start of the rule needs to be checked
                }
                else
                {
                    tryUnchecked = true;
                }
            }

            string suffix = GetSTSuffix(elementAST, ast_suffix, label);
            // if we're building trees and there is no label, gen a label
            // unless we're in a synpred rule.
            Rule r = grammar.GetRule(currentRuleName);

            if ((grammar.BuildAST || suffix.Length > 0) && label == null &&
                (r == null || !r.isSynPred))
            {
                label = generator.CreateUniqueLabel(elementName);
                CommonToken labelTok = new CommonToken(ANTLRParser.ID, label);
                grammar.DefineTokenRefLabel(currentRuleName, labelTok, elementAST);
            }

            StringTemplate elementST = null;

            if (tryUnchecked && templates.IsDefined(name + "Unchecked" + suffix))
            {
                elementST = templates.GetInstanceOf(name + "Unchecked" + suffix);
            }
            if (elementST == null)
            {
                elementST = templates.GetInstanceOf(name + suffix);
            }

            if (label != null)
            {
                elementST.SetAttribute("label", label);
            }
            return(elementST);
        }
예제 #13
0
        /** Create a copy of the alt and make it into a BLOCK; all actions,
         *  labels, tree operators, rewrites are removed.
         */
        protected virtual GrammarAST CreateBlockFromDupAlt(GrammarAST alt)
        {
            /*
             * ^(BLOCK["BLOCK"] {GrammarAST.dupTreeNoActions(alt)} EOB["<end-of-block>"])
             */
            GrammarAST nalt = GrammarAST.DupTreeNoActions(alt, null);

            GrammarAST block = (GrammarAST)adaptor.Create(BLOCK, alt.Token, "BLOCK");

            adaptor.AddChild(block, nalt);
            adaptor.AddChild(block, adaptor.Create(EOB, "<end-of-block>"));

            return(block);
        }
예제 #14
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]);
                    }
                }
            }
        }
예제 #15
0
        protected virtual GrammarAST SetToBlockWithSet(GrammarAST b)
        {
            /*
             * alt = ^(ALT["ALT"] {b} EOA["EOA"])
             * prefixWithSynpred( alt )
             * return ^(BLOCK["BLOCK"] {alt} EOB["<end-of-block>"])
             */
            GrammarAST alt = (GrammarAST)adaptor.Create(ALT, "ALT");

            adaptor.AddChild(alt, b);
            adaptor.AddChild(alt, adaptor.Create(EOA, "<end-of-alt>"));

            PrefixWithSynPred(alt);

            GrammarAST block = (GrammarAST)adaptor.Create(BLOCK, b.Token, "BLOCK");

            adaptor.AddChild(block, alt);
            adaptor.AddChild(alt, adaptor.Create(EOB, "<end-of-block>"));

            return(block);
        }
예제 #16
0
        /** Return a non-empty template name suffix if the token is to be
         *  tracked, added to a tree, or both.
         */
        protected string GetSTSuffix(GrammarAST elementAST, GrammarAST ast_suffix, string label)
        {
            if (grammar.type == GrammarType.Lexer)
            {
                return("");
            }
            // handle list label stuff; make element use "Track"

            string operatorPart  = "";
            string rewritePart   = "";
            string listLabelPart = "";
            Rule   ruleDescr     = grammar.GetRule(currentRuleName);

            if (ast_suffix != null && !ruleDescr.isSynPred)
            {
                if (ast_suffix.Type == ANTLRParser.ROOT)
                {
                    operatorPart = "RuleRoot";
                }
                else if (ast_suffix.Type == ANTLRParser.BANG)
                {
                    operatorPart = "Bang";
                }
            }
            if (currentAltHasASTRewrite && elementAST.Type != WILDCARD)
            {
                rewritePart = "Track";
            }
            if (IsListLabel(label))
            {
                listLabelPart = "AndListLabel";
            }
            string STsuffix = operatorPart + rewritePart + listLabelPart;

            //[email protected]("suffix = "+STsuffix);

            return(STsuffix);
        }
예제 #17
0
 private void HandleElementAction(out Template code, GrammarAST action, bool forced)
 {
     code = templates.GetInstanceOf(forced ? "execForcedAction" : "execAction");
     code.SetAttribute("action", generator.TranslateAction(currentRuleName, action));
 }
예제 #18
0
 public ActionLabel(GrammarAST actionAST)
     : base(ACTION)
 {
     _actionAST = actionAST;
 }
예제 #19
0
 private void HandleRewriteTemplateAction(out Template code, GrammarAST action)
 {
     // set alt num for same reason as ARGLIST above
     action.outerAltNum = this.outerAltNum;
     code = templates.GetInstanceOf("rewriteAction");
     code.SetAttribute("action", generator.TranslateAction(currentRuleName, action));
 }
예제 #20
0
 private void HandleRewriteTemplateAfterId(out Template code, GrammarAST id, GrammarAST ind)
 {
     if (id != null && id.Text.Equals("template"))
     {
         code = templates.GetInstanceOf("rewriteInlineTemplate");
     }
     else if (id != null)
     {
         code = templates.GetInstanceOf("rewriteExternalTemplate");
         code.SetAttribute("name", id.Text);
     }
     else if (ind != null)
     {
         // must be \%({expr})(args)
         code = templates.GetInstanceOf("rewriteIndirectTemplate");
         IList<object> chunks = generator.TranslateAction(currentRuleName, ind);
         code.SetAttribute("expr", chunks);
     }
     else
     {
         throw new ArgumentException();
     }
 }
예제 #21
0
 private void HandleAtomStringLiteral(GrammarAST label, GrammarAST astSuffix, out Template code, string labelText, GrammarAST s)
 {
     int i = s.Token.TokenIndex;
     if (grammar.type == GrammarType.Lexer)
     {
         code = templates.GetInstanceOf("lexerStringRef");
         code.SetAttribute("string",
             generator.Target.GetTargetStringLiteralFromANTLRStringLiteral(generator, s.Text));
         code.SetAttribute("elementIndex", i);
         if (label != null)
             code.SetAttribute("label", labelText);
     }
     else
     {
         // else it's a token type reference
         code = GetTokenElementST("tokenRef", "string_literal", s, astSuffix, labelText);
         string tokenLabel =
             generator.GetTokenTypeAsTargetLabel(grammar.GetTokenType(s.Text));
         code.SetAttribute("token", tokenLabel);
         if (s.terminalOptions != null)
         {
             code.SetAttribute("terminalOptions", s.terminalOptions);
         }
         code.SetAttribute("elementIndex", i);
         generator.GenerateLocalFollow(s, tokenLabel, currentRuleName, i);
     }
 }
예제 #22
0
 private void HandleNotElementBlock(out IIntSet elements, GrammarAST assign_st)
 {
     elements = assign_st.SetValue;
     elements = grammar.Complement(elements);
 }
예제 #23
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);
                    }
                }
            }
        }
예제 #24
0
        private void HandleAtomTokenReference(GrammarAST scope, GrammarAST label, GrammarAST astSuffix, out Template code, string labelText, GrammarAST t, GrammarAST targ)
        {
            if (currentAltHasASTRewrite && t.terminalOptions != null && t.terminalOptions[Grammar.defaultTokenOption] != null)
            {
                ErrorManager.GrammarError(ErrorManager.MSG_HETERO_ILLEGAL_IN_REWRITE_ALT, grammar, t.Token, t.Text);
            }

            grammar.CheckRuleReference(scope, t, targ, currentRuleName);
            if (grammar.type == GrammarType.Lexer)
            {
                if (grammar.GetTokenType(t.Text) == Label.EOF)
                {
                    code = templates.GetInstanceOf("lexerMatchEOF");
                }
                else
                {
                    code = templates.GetInstanceOf("lexerRuleRef");
                    if (IsListLabel(labelText))
                        code = templates.GetInstanceOf("lexerRuleRefAndListLabel");

                    string scopeName = null;
                    if (scope != null)
                        scopeName = scope.Text;

                    Rule rdef2 = grammar.GetRule(scopeName, t.Text);
                    code.SetAttribute("rule", rdef2);
                    if (scope != null)
                    {
                        // scoped rule ref
                        Grammar scopeG = grammar.composite.GetGrammar(scope.Text);
                        code.SetAttribute("scope", scopeG);
                    }
                    else if (rdef2.Grammar != this.grammar)
                    {
                        // nonlocal
                        // if rule definition is not in this grammar, it's nonlocal
                        code.SetAttribute("scope", rdef2.Grammar);
                    }

                    if (targ != null)
                    {
                        IList<object> args = generator.TranslateAction(currentRuleName, targ);
                        code.SetAttribute("args", args);
                    }
                }

                int i = t.Token.TokenIndex;
                code.SetAttribute("elementIndex", i);
                if (label != null)
                    code.SetAttribute("label", labelText);
            }
            else
            {
                code = GetTokenElementST("tokenRef", t.Text, t, astSuffix, labelText);
                string tokenLabel = generator.GetTokenTypeAsTargetLabel(grammar.GetTokenType(t.Text));
                code.SetAttribute("token", tokenLabel);
                if (!currentAltHasASTRewrite && t.terminalOptions != null)
                    code.SetAttribute("terminalOptions", t.terminalOptions);

                int i = t.Token.TokenIndex;
                code.SetAttribute("elementIndex", i);
                generator.GenerateLocalFollow(t, tokenLabel, currentRuleName, i);
            }

            t.code = code;
        }
예제 #25
0
 private void HandleExceptionHandler(Template ruleTemplate, GrammarAST argAction, GrammarAST action)
 {
     IList<object> chunks = generator.TranslateAction(currentRuleName, action);
     ruleTemplate.SetAttribute("exceptions.{decl,action}", argAction.Text, chunks);
 }
예제 #26
0
 public Predicate(int constantValue)
 {
     this._predicateAST  = new GrammarAST();
     this._constantValue = constantValue;
 }
예제 #27
0
 private void HandleFinallyClause(Template ruleTemplate, GrammarAST action)
 {
     IList<object> chunks = generator.TranslateAction(currentRuleName, action);
     ruleTemplate.SetAttribute("finally", chunks);
 }
예제 #28
0
 private void HandleNotElementTokenReference(out IIntSet elements, GrammarAST assign_t)
 {
     int ttype = grammar.GetTokenType(assign_t.Text);
     elements = grammar.Complement(ttype);
 }
예제 #29
0
        private void HandleNotElementStringLiteral(out IIntSet elements, GrammarAST assign_s)
        {
            int ttype = 0;
            if (grammar.type == GrammarType.Lexer)
            {
                // TODO: error!
            }
            else
            {
                ttype = grammar.GetTokenType(assign_s.Text);
            }

            elements = grammar.Complement(ttype);
        }
예제 #30
0
        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);
        }
예제 #31
0
        private void HandleNotElementCharLiteral(out IIntSet elements, GrammarAST assign_c)
        {
            int ttype = 0;
            if (grammar.type == GrammarType.Lexer)
                ttype = Grammar.GetCharValueFromGrammarCharLiteral(assign_c.Text);
            else
                ttype = grammar.GetTokenType(assign_c.Text);

            elements = grammar.Complement(ttype);
        }
예제 #32
0
        private void HandleBlockAlternative(Template code, ref int altNum, Template altCode, GrammarAST rewStart, Template rewCode)
        {
            if (this.blockNestingLevel == RULE_BLOCK_NESTING_LEVEL)
            {
                this.outerAltNum++;
            }

            // add the rewrite code as just another element in the alt :)
            // (unless it's a " -> ..." rewrite
            // ( -> ... )
            GrammarAST firstRewriteAST = (GrammarAST)rewStart.FindFirstType(REWRITE);
            bool etc =
                rewStart.Type == REWRITES &&
                firstRewriteAST.GetChild(0) != null &&
                firstRewriteAST.GetChild(0).Type == ETC;

            if (rewCode != null && !etc)
            {
                altCode.SetAttribute("rew", rewCode);
            }

            // add this alt to the list of alts for this block
            code.SetAttribute("alts", altCode);
            altCode.SetAttribute("altNum", altNum);
            altCode.SetAttribute("outerAlt", blockNestingLevel == RULE_BLOCK_NESTING_LEVEL);
            altNum++;
        }
예제 #33
0
 /** Make a semantic predicate label */
 public PredicateLabel(GrammarAST predicateASTNode)
     : base(SEMPRED)
 {
     this._semanticContext = new SemanticContext.Predicate(predicateASTNode);
 }
예제 #34
0
 private void HandleAtomWildcard(GrammarAST astSuffix, out Template code, string labelText, GrammarAST w)
 {
     code = GetWildcardST(w, astSuffix, labelText);
     code.SetAttribute("elementIndex", w.Token.TokenIndex);
 }
예제 #35
0
 private void HandleElementSemanticPredicate(out Template code, GrammarAST sp)
 {
     code = templates.GetInstanceOf("validateSemanticPredicate");
     code.SetAttribute("pred", generator.TranslateAction(currentRuleName, sp));
     string description = generator.Target.GetTargetStringLiteralFromString(sp.Text);
     code.SetAttribute("description", description);
 }
예제 #36
0
 private void HandleElementCharRange(GrammarAST label, out Template code, GrammarAST a, GrammarAST b)
 {
     code = templates.GetInstanceOf("charRangeRef");
     string low = generator.Target.GetTargetCharLiteralFromANTLRCharLiteral(generator, a.Text);
     string high = generator.Target.GetTargetCharLiteralFromANTLRCharLiteral(generator, b.Text);
     code.SetAttribute("a", low);
     code.SetAttribute("b", high);
     if (label != null)
         code.SetAttribute("label", label.Text);
 }
예제 #37
0
        private void HandleRewriteInit(out Template code, GrammarAST start)
        {
            code = null;

            if (state.backtracking == 0)
            {
                if (start.Type == REWRITES)
                {
                    if (generator.Grammar.BuildTemplate)
                    {
                        code = templates.GetInstanceOf("rewriteTemplate");
                    }
                    else
                    {
                        code = templates.GetInstanceOf("rewriteCode");
                        code.SetAttribute("treeLevel", OUTER_REWRITE_NESTING_LEVEL);
                        code.SetAttribute("rewriteBlockLevel", OUTER_REWRITE_NESTING_LEVEL);
                        code.SetAttribute("referencedElementsDeep", GetTokenTypesAsTargetLabels(start.rewriteRefsDeep));
                        HashSet<string> tokenLabels = grammar.GetLabels(start.rewriteRefsDeep, LabelType.Token);
                        HashSet<string> tokenListLabels = grammar.GetLabels(start.rewriteRefsDeep, LabelType.TokenList);
                        HashSet<string> ruleLabels = grammar.GetLabels(start.rewriteRefsDeep, LabelType.Rule);
                        HashSet<string> ruleListLabels = grammar.GetLabels(start.rewriteRefsDeep, LabelType.RuleList);
                        HashSet<string> wildcardLabels = grammar.GetLabels(start.rewriteRefsDeep, LabelType.WildcardTree);
                        HashSet<string> wildcardListLabels = grammar.GetLabels(start.rewriteRefsDeep, LabelType.WildcardTreeList);
                        // just in case they ref $r for "previous value", make a stream
                        // from retval.tree
                        Template retvalST = templates.GetInstanceOf("prevRuleRootRef");
                        ruleLabels.Add(retvalST.Render());
                        code.SetAttribute("referencedTokenLabels", tokenLabels);
                        code.SetAttribute("referencedTokenListLabels", tokenListLabels);
                        code.SetAttribute("referencedRuleLabels", ruleLabels);
                        code.SetAttribute("referencedRuleListLabels", ruleListLabels);
                        code.SetAttribute("referencedWildcardLabels", wildcardLabels);
                        code.SetAttribute("referencedWildcardListLabels", wildcardListLabels);
                    }
                }
                else
                {
                    code = templates.GetInstanceOf("noRewrite");
                    code.SetAttribute("treeLevel", OUTER_REWRITE_NESTING_LEVEL);
                    code.SetAttribute("rewriteBlockLevel", OUTER_REWRITE_NESTING_LEVEL);
                }
            }
        }
예제 #38
0
        /** Return a non-empty template name suffix if the token is to be
         *  tracked, added to a tree, or both.
         */
        protected string GetSTSuffix( GrammarAST elementAST, GrammarAST ast_suffix, string label )
        {
            if ( grammar.type == GrammarType.Lexer )
            {
                return "";
            }
            // handle list label stuff; make element use "Track"

            string operatorPart = "";
            string rewritePart = "";
            string listLabelPart = "";
            Rule ruleDescr = grammar.GetRule( currentRuleName );
            if ( ast_suffix != null && !ruleDescr.isSynPred )
            {
                if ( ast_suffix.Type == ANTLRParser.ROOT )
                {
                    operatorPart = "RuleRoot";
                }
                else if ( ast_suffix.Type == ANTLRParser.BANG )
                {
                    operatorPart = "Bang";
                }
            }
            if ( currentAltHasASTRewrite && elementAST.Type != WILDCARD )
            {
                rewritePart = "Track";
            }
            if ( IsListLabel( label ) )
            {
                listLabelPart = "AndListLabel";
            }
            string STsuffix = operatorPart + rewritePart + listLabelPart;
            //[email protected]("suffix = "+STsuffix);

            return STsuffix;
        }
예제 #39
0
        private void HandleGrammarSpecAfterName(GrammarAST comment)
        {
            if (comment != null)
            {
                outputFileST.SetAttribute("docComment", comment.Text);
                headerFileST.SetAttribute("docComment", comment.Text);
            }

            recognizerST.SetAttribute("name", grammar.GetRecognizerName());
            outputFileST.SetAttribute("name", grammar.GetRecognizerName());
            headerFileST.SetAttribute("name", grammar.GetRecognizerName());
            recognizerST.SetAttribute("scopes", grammar.GlobalScopes);
            headerFileST.SetAttribute("scopes", grammar.GlobalScopes);
        }
예제 #40
0
        private void HandleRewriteAfterRewrite(Template code, GrammarAST r, ref GrammarAST pred, Template alt)
        {
            rewriteBlockNestingLevel = OUTER_REWRITE_NESTING_LEVEL;
            IList<object> predChunks = null;
            if (pred != null)
                predChunks = generator.TranslateAction(currentRuleName, pred);

            string description = grammar.GrammarTreeToString(r, false);
            description = generator.Target.GetTargetStringLiteralFromString(description);
            code.SetAttribute("alts.{pred,alt,description}", predChunks, alt, description);
            pred = null;
        }
예제 #41
0
 protected StringTemplate GetWildcardST( GrammarAST elementAST, GrammarAST ast_suffix, string label )
 {
     string name = "wildcard";
     if ( grammar.type == GrammarType.Lexer )
     {
         name = "wildcardChar";
     }
     return GetTokenElementST( name, name, elementAST, ast_suffix, label );
 }
예제 #42
0
 protected StringTemplate GetRuleElementST( string name,
                                           string ruleTargetName,
                                           GrammarAST elementAST,
                                           GrammarAST ast_suffix,
                                           string label )
 {
     string suffix = GetSTSuffix( elementAST, ast_suffix, label );
     name += suffix;
     // if we're building trees and there is no label, gen a label
     // unless we're in a synpred rule.
     Rule r = grammar.GetRule( currentRuleName );
     if ( ( grammar.BuildAST || suffix.Length > 0 ) && label == null &&
          ( r == null || !r.isSynPred ) )
     {
         // we will need a label to do the AST or tracking, make one
         label = generator.CreateUniqueLabel( ruleTargetName );
         CommonToken labelTok = new CommonToken( ANTLRParser.ID, label );
         grammar.DefineRuleRefLabel( currentRuleName, labelTok, elementAST );
     }
     StringTemplate elementST = templates.GetInstanceOf( name );
     if ( label != null )
     {
         elementST.SetAttribute( "label", label );
     }
     return elementST;
 }
예제 #43
0
 private void HandleRewriteAtomAction(bool isRoot, out Template code, GrammarAST action)
 {
     // actions in rewrite rules yield a tree object
     string actText = action.Text;
     IList<object> chunks = generator.TranslateAction(currentRuleName, action);
     code = templates.GetInstanceOf("rewriteNodeAction" + (isRoot ? "Root" : ""));
     code.SetAttribute("action", chunks);
 }
예제 #44
0
        protected StringTemplate GetTokenElementST( string name,
                                                   string elementName,
                                                   GrammarAST elementAST,
                                                   GrammarAST ast_suffix,
                                                   string label )
        {
            bool tryUnchecked = false;
            if (name == "matchSet" && !string.IsNullOrEmpty(elementAST.enclosingRuleName) && Rule.GetRuleType(elementAST.enclosingRuleName) == RuleType.Lexer)
            {
                if ( ( elementAST.Parent.Type == ANTLRLexer.ALT && elementAST.Parent.Parent.Parent.Type == RULE && elementAST.Parent.Parent.ChildCount == 2 )
                    || ( elementAST.Parent.Type == ANTLRLexer.NOT && elementAST.Parent.Parent.Parent.Parent.Type == RULE && elementAST.Parent.Parent.Parent.ChildCount == 2 ) )
                {
                    // single alt at the start of the rule needs to be checked
                }
                else
                {
                    tryUnchecked = true;
                }
            }

            string suffix = GetSTSuffix( elementAST, ast_suffix, label );
            // if we're building trees and there is no label, gen a label
            // unless we're in a synpred rule.
            Rule r = grammar.GetRule( currentRuleName );
            if ( ( grammar.BuildAST || suffix.Length > 0 ) && label == null &&
                 ( r == null || !r.isSynPred ) )
            {
                label = generator.CreateUniqueLabel( elementName );
                CommonToken labelTok = new CommonToken( ANTLRParser.ID, label );
                grammar.DefineTokenRefLabel( currentRuleName, labelTok, elementAST );
            }

            StringTemplate elementST = null;
            if ( tryUnchecked && templates.IsDefined( name + "Unchecked" + suffix ) )
                elementST = templates.GetInstanceOf( name + "Unchecked" + suffix );
            if ( elementST == null )
                elementST = templates.GetInstanceOf( name + suffix );

            if ( label != null )
            {
                elementST.SetAttribute( "label", label );
            }
            return elementST;
        }
예제 #45
0
        private void HandleRewriteAtomLabel(bool isRoot, out Template code, GrammarAST label)
        {
            string labelName = label.Text;
            Rule rule = grammar.GetRule(currentRuleName);
            Grammar.LabelElementPair pair = rule.GetLabel(labelName);
            if (labelName.Equals(currentRuleName))
            {
                // special case; ref to old value via $ rule
                if (rule.HasRewrite(outerAltNum) && rule.GetRuleRefsInAlt(outerAltNum).Contains(labelName))
                    ErrorManager.GrammarError(ErrorManager.MSG_RULE_REF_AMBIG_WITH_RULE_IN_ALT, grammar, label.Token, labelName);

                Template labelST = templates.GetInstanceOf("prevRuleRootRef");
                code = templates.GetInstanceOf("rewriteRuleLabelRef" + (isRoot ? "Root" : ""));
                code.SetAttribute("label", labelST);
            }
            else if (pair == null)
            {
                ErrorManager.GrammarError(ErrorManager.MSG_UNDEFINED_LABEL_REF_IN_REWRITE, grammar, label.Token, labelName);
                code = new Template(string.Empty);
            }
            else
            {
                string stName = null;
                switch (pair.type)
                {
                case LabelType.Token:
                    stName = "rewriteTokenLabelRef";
                    break;

                case LabelType.WildcardTree:
                    stName = "rewriteWildcardLabelRef";
                    break;

                case LabelType.WildcardTreeList:
                    stName = "rewriteRuleListLabelRef"; // acts like rule ref list for ref
                    break;

                case LabelType.Rule:
                    stName = "rewriteRuleLabelRef";
                    break;

                case LabelType.TokenList:
                    stName = "rewriteTokenListLabelRef";
                    break;

                case LabelType.RuleList:
                    stName = "rewriteRuleListLabelRef";
                    break;
                }

                if (isRoot)
                    stName += "Root";

                code = templates.GetInstanceOf(stName);
                code.SetAttribute("label", labelName);
            }
        }
예제 #46
0
 /** Make a semantic predicate label */
 public PredicateLabel( GrammarAST predicateASTNode )
     : base(SEMPRED)
 {
     this._semanticContext = new SemanticContext.Predicate( predicateASTNode );
 }
예제 #47
0
        private void HandleRewriteAtomRuleReference(bool isRoot, out Template code, GrammarAST r)
        {
            string ruleRefName = r.Text;
            string stName = "rewriteRuleRef";
            if (isRoot)
                stName += "Root";

            code = templates.GetInstanceOf(stName);
            code.SetAttribute("rule", ruleRefName);
            if (grammar.GetRule(ruleRefName) == null)
            {
                ErrorManager.GrammarError(ErrorManager.MSG_UNDEFINED_RULE_REF, grammar, r.Token, ruleRefName);
                // blank; no code gen
                code = new Template(string.Empty);
            }
            else if (grammar.GetRule(currentRuleName).GetRuleRefsInAlt(ruleRefName, outerAltNum) == null)
            {
                ErrorManager.GrammarError(ErrorManager.MSG_REWRITE_ELEMENT_NOT_PRESENT_ON_LHS, grammar, r.Token, ruleRefName);
                // blank; no code gen
                code = new Template(string.Empty);
            }
            else
            {
                // track all rule refs as we must copy 2nd ref to rule and beyond
                if (!rewriteRuleRefs.Contains(ruleRefName))
                    rewriteRuleRefs.Add(ruleRefName);
            }
        }
예제 #48
0
 public Predicate()
 {
     predicateAST = new GrammarAST();
     this._gated  = false;
 }
예제 #49
0
        private void HandleRewriteAtomTokenReference(bool isRoot, out Template code, GrammarAST start, GrammarAST term, GrammarAST arg)
        {
            string tokenName = start.Token.Text;
            string stName = "rewriteTokenRef";
            Rule rule = grammar.GetRule(currentRuleName);
            ICollection<string> tokenRefsInAlt = rule.GetTokenRefsInAlt(outerAltNum);
            bool createNewNode = !tokenRefsInAlt.Contains(tokenName) || arg != null;
            if (createNewNode)
                stName = "rewriteImaginaryTokenRef";

            if (isRoot)
                stName += "Root";

            code = templates.GetInstanceOf(stName);
            code.SetAttribute("terminalOptions", term.terminalOptions);
            if (arg != null)
            {
                IList<object> args = generator.TranslateAction(currentRuleName, arg);
                code.SetAttribute("args", args);
            }

            code.SetAttribute("elementIndex", start.Token.TokenIndex);
            int ttype = grammar.GetTokenType(tokenName);
            string tok = generator.GetTokenTypeAsTargetLabel(ttype);
            code.SetAttribute("token", tok);
            if (grammar.GetTokenType(tokenName) == Label.INVALID)
            {
                ErrorManager.GrammarError(ErrorManager.MSG_UNDEFINED_TOKEN_REF_IN_REWRITE, grammar, start.Token, tokenName);
                // blank; no code gen
                code = new Template(string.Empty);
            }
        }
예제 #50
0
 private void HandleRewriteEbnf(out Template code, GrammarAST start, Template rewriteBlockCode)
 {
     code = rewriteBlockCode;
     string description = grammar.GrammarTreeToString(start, false);
     description = generator.Target.GetTargetStringLiteralFromString(description);
     code.SetAttribute("description", description);
 }
예제 #51
0
        // Setter/Getters

        /** What AST node is associated with this NFAState?  When you
         *  set the AST node, I set the node to point back to this NFA state.
         */
        public void SetDecisionASTNode(GrammarAST decisionASTNode)
        {
            decisionASTNode.NFAStartState = this;
            this.associatedASTNode        = decisionASTNode;
        }