Example #1
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]);
                    }
                }
            }
        }
Example #2
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);
        }