Example #1
0
        /** Return a String containing a DOT description that, when displayed,
         *  will show the incoming state machine visually.  All nodes reachable
         *  from startState will be included.
         */
        public virtual string GenerateGraph(State startState)
        {
            if (startState == null)
            {
                return(null);
            }
            // The output DOT graph for visualization
            StringTemplate dot = null;

            markedStates = new HashSet <int>();
            if (startState is DFAState)
            {
                dot = GetTemplates().GetInstanceOf("dfa");
                dot.SetAttribute("startState",
                                 startState.StateNumber);
                dot.SetAttribute("useBox",
                                 AntlrTool.internalOption_ShowNFAConfigsInDFA);
                WalkCreatingDFADOT(dot, (DFAState)startState);
            }
            else
            {
                dot = GetTemplates().GetInstanceOf("nfa");
                dot.SetAttribute("startState",
                                 startState.StateNumber);
                WalkRuleNFACreatingDOT(dot, startState);
            }
            dot.SetAttribute("rankdir", rankdir);
            return(dot.Render());
        }
Example #2
0
            public override StringTemplate GenExpr(CodeGenerator generator, TemplateGroup templates, DFA dfa)
            {
                StringTemplate result =
                    Operands.Aggregate(default(StringTemplate),
                                       (template, operand) =>
                {
                    if (template == null)
                    {
                        return(operand.GenExpr(generator, templates, dfa));
                    }

                    StringTemplate eST = null;
                    if (templates != null)
                    {
                        eST = templates.GetInstanceOf("andPredicates");
                    }
                    else
                    {
                        eST = new StringTemplate("(<left>&&<right>)");
                    }

                    eST.SetAttribute("left", template);
                    eST.SetAttribute("right", operand.GenExpr(generator, templates, dfa));
                    return(eST);
                });

                return(result);
            }
Example #3
0
        public override string ToString()
        {
            GrammarAST decisionASTNode = probe.Dfa.DecisionASTNode;

            line = decisionASTNode.Line;
            charPositionInLine = decisionASTNode.CharPositionInLine;
            string fileName = probe.Dfa.Nfa.Grammar.FileName;

            if (fileName != null)
            {
                file = fileName;
            }

            StringTemplate st       = GetMessageTemplate();
            string         ruleName = probe.Dfa.NFADecisionStartState.enclosingRule.Name;

            st.SetAttribute("ruleName", ruleName);
            List <int> sortedAlts = new List <int>();

            sortedAlts.AddRange(altsWithRecursion);
            sortedAlts.Sort();
            st.SetAttribute("alts", sortedAlts);

            return(base.ToString(st));
        }
Example #4
0
        protected StringTemplate Template(string name)
        {
            StringTemplate st = generator.Templates.GetInstanceOf(name);

            chunks.Add(st);
            return(st);
        }
Example #5
0
        private void HandleTemplateExpression(string action)
        {
            StringTemplate st = Template("actionStringConstructor");

            action = action.Substring(1, action.Length - 2); // stuff inside {...}
            st.SetAttribute("stringExpr", TranslateAction(action));
        }
Example #6
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 override String ToString()
        {
            GrammarAST decisionASTNode = probe.Dfa.DecisionASTNode;

            line = decisionASTNode.Line;
            charPositionInLine = decisionASTNode.CharPositionInLine;
            String fileName = probe.Dfa.Nfa.Grammar.FileName;

            if (fileName != null)
            {
                file = fileName;
            }

            StringTemplate st = GetMessageTemplate();

            st.SetAttribute("targetRules", targetRules);
            st.SetAttribute("alt", alt);
            st.SetAttribute("callSiteStates", callSiteStates);

            var    labels = probe.GetSampleNonDeterministicInputSequence(sampleBadState);
            String input  = probe.GetInputSequenceDisplay(labels);

            st.SetAttribute("input", input);

            return(base.ToString(st));
        }
Example #8
0
        private void HandleIndirectTemplateInstance(string text)
        {
            string         action = text.Substring(1, text.Length - 1);
            StringTemplate st     = generator.TranslateTemplateConstructor(enclosingRule.Name, outerAltNum, actionToken, action);

            chunks.Add(st);
        }
Example #9
0
        public override string ToString()
        {
            line = 0;
            charPositionInLine = 0;
            if (offendingToken != null)
            {
                line = offendingToken.Line;
                charPositionInLine = offendingToken.CharPositionInLine;
            }
            if (g != null)
            {
                file = g.FileName;
            }
            StringTemplate st = GetMessageTemplate();

            if (arg != null)
            {
                st.SetAttribute("arg", arg);
            }
            if (arg2 != null)
            {
                st.SetAttribute("arg2", arg2);
            }
            return(base.ToString(st));
        }
Example #10
0
        public override void PrefixAlt(GrammarAST altTree, GrammarAST rewriteTree, int alt)
        {
            altTree     = GrammarAST.DupTree(altTree);
            rewriteTree = GrammarAST.DupTree(rewriteTree);

            StripSynPred(altTree);

            int nextPrec = Precedence(alt);
            // rewrite e to be e_[rec_arg]
            StringTemplate refST = recRuleTemplates.GetInstanceOf("recRuleRef");

            refST.SetAttribute("ruleName", ruleName);
            refST.SetAttribute("arg", nextPrec);
            altTree = ReplaceRuleRefs(altTree, refST.Render());
            string altText = Text(altTree);

            altText  = altText.Trim();
            altText += "{}"; // add empty alt to prevent pred hoisting

            StringTemplate nameST = recRuleTemplates.GetInstanceOf("recRuleName");

            nameST.SetAttribute("ruleName", ruleName);
            rewriteTree = ReplaceRuleRefs(rewriteTree, nameST.Render());
            string rewriteText = Text(rewriteTree);

            prefixAlts.Add(altText + (rewriteText != null ? " " + rewriteText : ""));
            //System.out.println("prefixAlt " + alt + ": " + altText + ", rewrite=" + rewriteText);
        }
Example #11
0
        private void HandleSetAttribute(string x, string y, string expr)
        {
            StringTemplate st = Template("actionSetAttribute");

            st.SetAttribute("st", x);
            st.SetAttribute("attrName", y);
            st.SetAttribute("expr", TranslateAction(expr));
        }
Example #12
0
 protected override void GenRecognizerHeaderFile(AntlrTool tool,
                                                 CodeGenerator generator,
                                                 Grammar grammar,
                                                 StringTemplate headerFileST,
                                                 string extName)
 {
     generator.Write(headerFileST, grammar.name + Grammar.grammarTypeToFileNameSuffix[(int)grammar.type] + extName);
 }
Example #13
0
        private void HandleDynamicAbsoluteIndexedScopeAttribute(string x, string y, string expr)
        {
            StringTemplate st = Template("scopeAttributeRef");

            st.SetAttribute("scope", x);
            st.SetAttribute("attr", ResolveDynamicScope(x).GetAttribute(y));
            st.SetAttribute("index", expr);
        }
Example #14
0
 protected internal virtual void GenRecognizerHeaderFile(AntlrTool tool,
                                                         CodeGenerator generator,
                                                         Grammar grammar,
                                                         StringTemplate headerFileST,
                                                         string extName) // e.g., ".h"
 {
     // no header file by default
 }
Example #15
0
 protected StringTemplate chooseWhereCyclicDFAsGo(AntlrTool tool,
                                                  CodeGenerator generator,
                                                  Grammar grammar,
                                                  StringTemplate recognizerST,
                                                  StringTemplate cyclicDFAST)
 {
     return(recognizerST);
 }
Example #16
0
        /// <summary>
        /// initialize template
        /// </summary>
        private static Antlr4.StringTemplate.Template InitializeTemplate()
        {
            var templateName = TemplatePath + "XmlScript.stg";

            Antlr4.StringTemplate.TemplateGroup templateGroup = new Antlr4.StringTemplate.TemplateGroupFile(templateName);
            templateGroup.TrackCreationEvents = true;
            Antlr4.StringTemplate.Template template = templateGroup.GetInstanceOf("XmlScript");
            return(template);
        }
Example #17
0
        public string GetArtificialPrimaryRule()
        {
            StringTemplate ruleST = recRuleTemplates.GetInstanceOf("recPrimaryRule");

            ruleST.SetAttribute("ruleName", ruleName);
            ruleST.SetAttribute("alts", prefixAlts);
            ruleST.SetAttribute("alts", otherAlts);
            //Console.WriteLine(ruleST);
            return(ruleST.Render());
        }
Example #18
0
        public string GetArtificialPrecStartRule()
        {
            StringTemplate ruleST = recRuleTemplates.GetInstanceOf("recRuleStart");

            ruleST.SetAttribute("ruleName", ruleName);
            ruleST.SetAttribute("maxPrec", 0);

            //Console.WriteLine("start: " + ruleST);
            return(ruleST.Render());
        }
Example #19
0
        protected internal virtual void GenRecognizerFile(AntlrTool tool,
                                                          CodeGenerator generator,
                                                          Grammar grammar,
                                                          StringTemplate outputFileST)
        {
            string fileName =
                generator.GetRecognizerFileName(grammar.name, grammar.type);

            generator.Write(outputFileST, fileName);
        }
Example #20
0
        private void HandleSetExpressionAttribute(string a, string id, string expr)
        {
            StringTemplate st     = Template("actionSetAttribute");
            string         action = a;

            action = action.Substring(1, action.Length - 2); // stuff inside {...}
            st.SetAttribute("st", TranslateAction(action));
            st.SetAttribute("attrName", id);
            st.SetAttribute("expr", TranslateAction(expr));
        }
        public virtual StringTemplate GetDependencies()
        {
            LoadDependencyTemplates();
            StringTemplate dependenciesST = templates.GetInstanceOf("dependencies");

            dependenciesST.SetAttribute("in", GetDependenciesFileList());
            dependenciesST.SetAttribute("out", GetGeneratedFileList());
            dependenciesST.SetAttribute("grammarFileName", grammar.fileName);
            return(dependenciesST);
        }
Example #22
0
        /** Return a String containing a DOT description that, when displayed,
         *  will show the incoming state machine visually.  All nodes reachable
         *  from startState will be included.
         */
        public string GetRuleNFADOT(State startState)
        {
            // The output DOT graph for visualization
            StringTemplate dot = GetTemplates().GetInstanceOf("nfa");

            markedStates = new HashSet <object>();
            dot.SetAttribute("startState", startState.stateNumber);
            walkRuleNFACreatingDOT(dot, startState);
            return(dot.Render());
        }
Example #23
0
        protected override void GenRecognizerFile(AntlrTool tool,
                                                  CodeGenerator generator,
                                                  Grammar grammar,
                                                  StringTemplate outputFileST)
        {
            // Before we write this, and cause it to generate its string,
            // we need to add all the string literals that we are going to match
            //
            outputFileST.SetAttribute("literals", _strings);
            string fileName = generator.GetRecognizerFileName(grammar.name, grammar.type);

            generator.Write(outputFileST, fileName);
        }
Example #24
0
        public static string Render(string template, object model)
        {
            //var tpl = new Antlr4.StringTemplate.Template(template, '$', '$');
            //tpl.Add("Model", model);
            //return tpl.Render();
            var group = new Antlr4.StringTemplate.TemplateGroup('$', '$');
            var tpl   = new Antlr4.StringTemplate.Template(group, template);

            //register renderer for additional formatting from st file.
            group.RegisterRenderer(typeof(string), new StringAttributeRenderer());

            tpl.Add("Model", model);
            return(tpl.Render());
        }
Example #25
0
        private void HandleDynamicScopeAttribute(string x, string y)
        {
            AttributeScope scope = ResolveDynamicScope(x);

            if (scope != null)
            {
                StringTemplate st = Template("scopeAttributeRef");
                st.SetAttribute("scope", x);
                st.SetAttribute("attr", scope.GetAttribute(y));
            }
            else
            {
                // error: invalid dynamic attribute
            }
        }
Example #26
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);
            }
        }
Example #27
0
        protected override void GenRecognizerHeaderFile(AntlrTool tool,
                                                        CodeGenerator generator,
                                                        Grammar grammar,
                                                        StringTemplate headerFileST,
                                                        string extName)
        {
            // Pick up the file name we are generating. This method will return a
            // a file suffixed with .c, so we must substring and add the extName
            // to it as we cannot assign into strings in Java.
            //
            string fileName = generator.GetRecognizerFileName(grammar.name, grammar.type);

            fileName = fileName.Substring(0, fileName.Length - 2) + extName;

            generator.Write(headerFileST, fileName);
        }
Example #28
0
            public override StringTemplate GenExpr(CodeGenerator generator, TemplateGroup templates, DFA dfa)
            {
                StringTemplate eST = null;

                if (templates != null)
                {
                    eST = templates.GetInstanceOf("notPredicate");
                }
                else
                {
                    eST = new StringTemplate("!(<pred>)");
                }

                eST.SetAttribute("pred", ctx.GenExpr(generator, templates, dfa));
                return(eST);
            }
Example #29
0
        /** Do a depth-first walk of the state machine graph and
         *  fill a DOT description template.  Keep filling the
         *  states and edges attributes.
         */
        protected virtual void WalkCreatingDFADOT(StringTemplate dot,
                                                  DFAState s)
        {
            if (markedStates.Contains(s.StateNumber))
            {
                return; // already visited this node
            }

            markedStates.Add(s.StateNumber);   // mark this node as completed.

            // first add this node
            StringTemplate st;

            if (s.IsAcceptState)
            {
                st = GetTemplates().GetInstanceOf("stopstate");
            }
            else
            {
                st = GetTemplates().GetInstanceOf("state");
            }
            st.SetAttribute("name", GetStateLabel(s));
            dot.SetAttribute("states", st);

            // make a DOT edge for each transition
            for (int i = 0; i < s.NumberOfTransitions; i++)
            {
                Transition edge = (Transition)s.GetTransition(i);
                //Console.Out.WriteLine( "dfa " + s.dfa.decisionNumber + " edge from s"
                //    + s.stateNumber + " [" + i + "] of " + s.NumberOfTransitions );
                if (StripNonreducedStates)
                {
                    if (edge.Target is DFAState &&
                        ((DFAState)edge.Target).AcceptStateReachable != Reachable.Yes)
                    {
                        continue; // don't generate nodes for terminal states
                    }
                }
                st = GetTemplates().GetInstanceOf("edge");
                st.SetAttribute("label", GetEdgeLabel(edge));
                st.SetAttribute("src", GetStateLabel(s));
                st.SetAttribute("target", GetStateLabel(edge.Target));
                st.SetAttribute("arrowhead", arrowhead);
                dot.SetAttribute("edges", st);
                WalkCreatingDFADOT(dot, (DFAState)edge.Target);   // keep walkin'
            }
        }
        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);
            }
        }
Example #31
0
        /** Do a depth-first walk of the state machine graph and
         *  fill a DOT description template.  Keep filling the
         *  states and edges attributes.  We know this is an NFA
         *  for a rule so don't traverse edges to other rules and
         *  don't go past rule end state.
         */
        protected virtual void WalkRuleNFACreatingDOT( StringTemplate dot,
                                              State s )
        {
            if ( markedStates.Contains( s.StateNumber ) )
            {
                return; // already visited this node
            }

            markedStates.Add( s.StateNumber ); // mark this node as completed.

            // first add this node
            StringTemplate stateST;
            if ( s.IsAcceptState )
            {
                stateST = GetTemplates().GetInstanceOf( "stopstate" );
            }
            else
            {
                stateST = GetTemplates().GetInstanceOf( "state" );
            }
            stateST.SetAttribute( "name", GetStateLabel( s ) );
            dot.SetAttribute( "states", stateST );

            if ( s.IsAcceptState )
            {
                return; // don't go past end of rule node to the follow states
            }

            // special case: if decision point, then line up the alt start states
            // unless it's an end of block
            if ( ( (NFAState)s ).IsDecisionState )
            {
                GrammarAST n = ( (NFAState)s ).associatedASTNode;
                if ( n != null && n.Type != ANTLRParser.EOB )
                {
                    StringTemplate rankST = GetTemplates().GetInstanceOf( "decision-rank" );
                    NFAState alt = (NFAState)s;
                    while ( alt != null )
                    {
                        rankST.SetAttribute( "states", GetStateLabel( alt ) );
                        if ( alt.transition[1] != null )
                        {
                            alt = (NFAState)alt.transition[1].Target;
                        }
                        else
                        {
                            alt = null;
                        }
                    }
                    dot.SetAttribute( "decisionRanks", rankST );
                }
            }

            // make a DOT edge for each transition
            StringTemplate edgeST = null;
            for ( int i = 0; i < s.NumberOfTransitions; i++ )
            {
                Transition edge = (Transition)s.GetTransition( i );
                if ( edge is RuleClosureTransition )
                {
                    RuleClosureTransition rr = ( (RuleClosureTransition)edge );
                    // don't jump to other rules, but display edge to follow node
                    edgeST = GetTemplates().GetInstanceOf( "edge" );
                    if ( rr.Rule.Grammar != grammar )
                    {
                        edgeST.SetAttribute( "label", "<" + rr.Rule.Grammar.name + "." + rr.Rule.Name + ">" );
                    }
                    else
                    {
                        edgeST.SetAttribute( "label", "<" + rr.Rule.Name + ">" );
                    }
                    edgeST.SetAttribute( "src", GetStateLabel( s ) );
                    edgeST.SetAttribute( "target", GetStateLabel( rr.FollowState ) );
                    edgeST.SetAttribute( "arrowhead", arrowhead );
                    dot.SetAttribute( "edges", edgeST );
                    WalkRuleNFACreatingDOT( dot, rr.FollowState );
                    continue;
                }
                if ( edge.IsAction )
                {
                    edgeST = GetTemplates().GetInstanceOf( "action-edge" );
                }
                else if ( edge.IsEpsilon )
                {
                    edgeST = GetTemplates().GetInstanceOf( "epsilon-edge" );
                }
                else
                {
                    edgeST = GetTemplates().GetInstanceOf( "edge" );
                }
                edgeST.SetAttribute( "label", GetEdgeLabel( edge ) );
                edgeST.SetAttribute( "src", GetStateLabel( s ) );
                edgeST.SetAttribute( "target", GetStateLabel( edge.Target ) );
                edgeST.SetAttribute( "arrowhead", arrowhead );
                dot.SetAttribute( "edges", edgeST );
                WalkRuleNFACreatingDOT( dot, edge.Target ); // keep walkin'
            }
        }
Example #32
0
        /** Create a grammar from file name.  */
        public Grammar( Tool tool, string fileName, CompositeGrammar composite )
        {
            nameSpaceChecker = new NameSpaceChecker( this );
            ll1Analyzer = new LL1Analyzer( this );
            sanity = new GrammarSanity( this );

            this.composite = composite;
            Tool = tool;
            FileName = fileName;
            // ensure we have the composite set to something
            if ( composite.DelegateGrammarTreeRoot == null )
            {
                composite.SetDelegationRoot( this );
            }
            else
            {
                defaultRuleModifier = composite.DelegateGrammarTreeRoot.Grammar.DefaultRuleModifier;
            }

            TemplateGroup lexerGrammarTemplateGroup = new TemplateGroupString(lexerGrammarTemplateText);
            _lexerGrammarTemplate = lexerGrammarTemplateGroup.GetInstanceOf("grammar");
            target = CodeGenerator.LoadLanguageTarget((string)GetOption("language"), tool.TargetsDirectory);
        }
Example #33
0
        public void TestSetAttrOfExpr()
        {
            string action = "%{foo($ID.text).getST()}.y = z;";
            string expecting = "(foo((ID1!=null?ID1.getText():null)).getST()).setAttribute(\"y\", z);";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {\n" +
                "    output=template;\n" +
                "}\n" +
                "\n" +
                "a : ID {" + action + "}\n" +
                "  ;\n" +
                "\n" +
                "ID : 'a';\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator( generator,
                                                                         "a",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup();
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.Render();

            assertNoErrors( equeue );

            Assert.AreEqual( expecting, found );
        }
Example #34
0
        /** Parse a rule we add artificially that is a list of the other lexer
         *  rules like this: "Tokens : ID | INT | SEMI ;"  nextToken() will invoke
         *  this to set the current token.  Add char literals before
         *  the rule references.
         *
         *  If in filter mode, we want every alt to backtrack and we need to
         *  do k=1 to force the "first token def wins" rule.  Otherwise, the
         *  longest-match rule comes into play with LL(*).
         *
         *  The ANTLRParser antlr.g file now invokes this when parsing a lexer
         *  grammar, which I think is proper even though it peeks at the info
         *  that later phases will (re)compute.  It gets a list of lexer rules
         *  and builds a string representing the rule; then it creates a parser
         *  and adds the resulting tree to the grammar's tree.
         */
        public GrammarAST AddArtificialMatchTokensRule( GrammarAST grammarAST,
                                                       IList<string> ruleNames,
                                                       IList<string> delegateNames,
                                                       bool filterMode )
        {
            StringTemplate matchTokenRuleST = null;
            if ( filterMode )
            {
                matchTokenRuleST = new StringTemplate(
                        ArtificialTokensRuleName +
                        " options {k=1; backtrack=true;} : <rules; separator=\"|\">;");
            }
            else
            {
                matchTokenRuleST = new StringTemplate(
                        ArtificialTokensRuleName + " : <rules; separator=\"|\">;");
            }

            // Now add token rule references
            for ( int i = 0; i < ruleNames.Count; i++ )
            {
                string rname = (string)ruleNames[i];
                matchTokenRuleST.SetAttribute( "rules", rname );
            }
            for ( int i = 0; i < delegateNames.Count; i++ )
            {
                string dname = (string)delegateNames[i];
                matchTokenRuleST.SetAttribute( "rules", dname + ".Tokens" );
            }

            GrammarAST r = ParseArtificialRule(matchTokenRuleST.Render());
            AddRule(grammarAST, r);
            //addRule((GrammarAST)parser.getAST());
            //return (GrammarAST)parser.getAST();
            return r;
        }
        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 HandleFinallyClause(Template ruleTemplate, GrammarAST action)
 {
     IList<object> chunks = generator.TranslateAction(currentRuleName, action);
     ruleTemplate.SetAttribute("finally", chunks);
 }
Example #37
0
        protected override void GenRecognizerFile(AntlrTool tool, CodeGenerator generator, Grammar grammar, Template outputFileST)
        {
            if (!grammar.IsRoot)
            {
                Grammar rootGrammar = grammar.composite.RootGrammar;
                string actionScope = grammar.GetDefaultActionScope(grammar.type);
                IDictionary<string, object> actions;
                object rootNamespace;
                if (rootGrammar.Actions.TryGetValue(actionScope, out actions) && actions.TryGetValue("namespace", out rootNamespace))
                {
                    if (!grammar.Actions.TryGetValue(actionScope, out actions))
                    {
                        actions = new Dictionary<string, object>();
                        grammar.Actions[actionScope] = actions;
                    }

                    actions["namespace"] = rootNamespace;
                }
            }

            generator.Templates.RegisterRenderer(typeof(string), new StringRenderer(generator, this));
            base.GenRecognizerFile(tool, generator, grammar, outputFileST);
        }
 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();
     }
 }
Example #39
0
 public void TestEscapedLessThanInAction()
 {
     Grammar g = new Grammar();
     AntlrTool antlr = newTool();
     CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
     string action = "i<3; '<xmltag>'";
     ActionTranslator translator = new ActionTranslator( generator, "a",
                                                                  new CommonToken( ANTLRParser.ACTION, action ), 0 );
     string expecting = action;
     string rawTranslation =
         translator.Translate();
     StringTemplateGroup templates =
         new StringTemplateGroup();
     StringTemplate actionST = new StringTemplate( templates, "<action>" );
     actionST.SetAttribute( "action", rawTranslation );
     string found = actionST.Render();
     Assert.AreEqual( expecting, found );
 }
 private void HandleRewriteTemplateAlt(out Template code)
 {
     code = templates.GetInstanceOf("rewriteEmptyTemplate");
 }
Example #41
0
 public void TestDoNotTranslateScopeAttributeCompare()
 {
     string action = "if ($rule::foo == \"foo\" || 1) { System.out.println(\"ouch\"); }";
     string expecting = "if (((rule_scope)rule_stack.peek()).foo == \"foo\" || 1) { System.out.println(\"ouch\"); }";
     ErrorQueue equeue = new ErrorQueue();
     ErrorManager.SetErrorListener( equeue );
     Grammar g = new Grammar(
             "grammar a;\n" +
             "rule\n" +
             "scope {\n" +
             "   String foo;" +
             "} :\n" +
             "     twoIDs" +
             "    ;\n" +
             "twoIDs:\n" +
             "    ID ID {" + action + "}\n" +
             "    ;\n" +
             "ID : 'id';"
     );
     AntlrTool antlr = newTool();
     CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
     g.CodeGenerator = generator;
     generator.GenRecognizer();
     ActionTranslator translator = new ActionTranslator( generator,
                                                                  "twoIDs",
                                                                  new CommonToken( ANTLRParser.ACTION, action ), 1 );
     string rawTranslation =
         translator.Translate();
     // check that we didn't use scopeSetAttributeRef int translation!
     bool foundScopeSetAttributeRef = false;
     for ( int i = 0; i < translator.chunks.Count; i++ )
     {
         object chunk = translator.chunks[i];
         if ( chunk is StringTemplate )
         {
             if ( ( (StringTemplate)chunk ).Name.Equals( "/scopeSetAttributeRef" ) )
             {
                 foundScopeSetAttributeRef = true;
             }
         }
     }
     Assert.IsFalse(foundScopeSetAttributeRef, "action translator used scopeSetAttributeRef template in comparison!");
     StringTemplateGroup templates =
         new StringTemplateGroup();
     StringTemplate actionST = new StringTemplate( templates, rawTranslation );
     string found = actionST.Render();
     Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
     Assert.AreEqual( expecting, found );
 }
Example #42
0
        public void TestIllegalAssignToLocalAttr()
        {
            string action = "$tree = null; $st = null; $start = 0; $stop = 0; $text = 0;";
            string expecting = "retval.tree = null; retval.st = null;   ";
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar a;\n" +
                "rule\n" +
                "    : 'y' {" + action + "}\n" +
                "    ;" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator( generator,
                                                                         "rule",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();

            int expectedMsgID = ErrorManager.MSG_WRITE_TO_READONLY_ATTR;
            var expectedErrors = new List<object>( 3 );
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, "start", "" );
            expectedErrors.Add( expectedMessage );
            GrammarSemanticsMessage expectedMessage2 =
                new GrammarSemanticsMessage( expectedMsgID, g, null, "stop", "" );
            expectedErrors.Add( expectedMessage2 );
            GrammarSemanticsMessage expectedMessage3 =
            new GrammarSemanticsMessage( expectedMsgID, g, null, "text", "" );
            expectedErrors.Add( expectedMessage3 );
            checkErrors( equeue, expectedErrors );

            StringTemplateGroup templates =
                new StringTemplateGroup();
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.Render();
            Assert.AreEqual( expecting, found );
        }
Example #43
0
 protected override void GenRecognizerHeaderFile( AntlrTool tool,
                                        CodeGenerator generator,
                                        Grammar grammar,
                                        StringTemplate headerFileST,
                                        string extName )
 {
     generator.Write( headerFileST, grammar.name + Grammar.grammarTypeToFileNameSuffix[(int)grammar.type] + extName );
 }
 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);
 }
 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));
 }
        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;
        }
        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);
                }
            }
        }
        private void HandleGrammarInit(Grammar g, Template recognizerST, Template outputFileST, Template headerFileST)
        {
            if (state.backtracking == 0)
            {
                Init(g);
                this.recognizerST = recognizerST;
                this.outputFileST = outputFileST;
                this.headerFileST = headerFileST;
                string superClass = (string)g.GetOption("superClass");
                outputOption = (string)g.GetOption("output");
                if (superClass != null)
                    recognizerST.SetAttribute("superClass", superClass);

                if (g.type != GrammarType.Lexer)
                {
                    object labelType = g.GetOption("ASTLabelType");
                    if (labelType != null)
                        recognizerST.SetAttribute("ASTLabelType", labelType);
                }

                if (g.type == GrammarType.TreeParser && g.GetOption("ASTLabelType") == null)
                    ErrorManager.GrammarWarning(ErrorManager.MSG_MISSING_AST_TYPE_IN_TREE_GRAMMAR, g, null, g.name);

                if (g.type != GrammarType.TreeParser)
                {
                    object labelType = g.GetOption("TokenLabelType");
                    if (labelType != null)
                        recognizerST.SetAttribute("labelType", labelType);
                }

                recognizerST.SetAttribute("numRules", grammar.Rules.Count);
                outputFileST.SetAttribute("numRules", grammar.Rules.Count);
                headerFileST.SetAttribute("numRules", grammar.Rules.Count);
            }
        }
 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);
 }
 private void HandleExceptionHandler(Template ruleTemplate, GrammarAST argAction, GrammarAST action)
 {
     IList<object> chunks = generator.TranslateAction(currentRuleName, action);
     ruleTemplate.SetAttribute("exceptions.{decl,action}", argAction.Text, chunks);
 }
 private void HandleElementAction(out Template code, GrammarAST action, bool forced)
 {
     code = templates.GetInstanceOf(forced ? "execForcedAction" : "execAction");
     code.SetAttribute("action", generator.TranslateAction(currentRuleName, action));
 }
Example #52
0
        public void TestTemplateConstructorNoArgs()
        {
            string action = "x = %foo();";
            string expecting = "x = templateLib.getInstanceOf(\"foo\");";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {\n" +
                "    output=template;\n" +
                "}\n" +
                "\n" +
                "a : ID {" + action + "}\n" +
                "  ;\n" +
                "\n" +
                "ID : 'a';\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator =
                new ActionTranslator( generator,
                                            "a",
                                            new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup();
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.Render();

            assertNoErrors( equeue );

            Assert.AreEqual( expecting, found );
        }
Example #53
0
 public virtual void SetSynPredGateIfNotAlready( StringTemplate gateST )
 {
     string scope = GetDefaultActionScope( type );
     IDictionary<string, object> actionsForGrammarScope;
     Actions.TryGetValue(scope, out actionsForGrammarScope);
     // if no synpredgate action set by user then set
     if ( actionsForGrammarScope == null || !actionsForGrammarScope.ContainsKey( Grammar.SynpredGateActionName ) )
     {
         if ( actionsForGrammarScope == null )
         {
             actionsForGrammarScope = new Dictionary<string, object>();
             Actions[scope] = actionsForGrammarScope;
         }
         actionsForGrammarScope[Grammar.SynpredGateActionName] = gateST;
     }
 }
 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);
 }
 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);
 }
Example #56
0
        /** Do a depth-first walk of the state machine graph and
         *  fill a DOT description template.  Keep filling the
         *  states and edges attributes.
         */
        protected virtual void WalkCreatingDFADOT( StringTemplate dot,
                                          DFAState s )
        {
            if ( markedStates.Contains( s.StateNumber ) )
            {
                return; // already visited this node
            }

            markedStates.Add( s.StateNumber ); // mark this node as completed.

            // first add this node
            StringTemplate st;
            if ( s.IsAcceptState )
            {
                st = GetTemplates().GetInstanceOf( "stopstate" );
            }
            else
            {
                st = GetTemplates().GetInstanceOf( "state" );
            }
            st.SetAttribute( "name", GetStateLabel( s ) );
            dot.SetAttribute( "states", st );

            // make a DOT edge for each transition
            for ( int i = 0; i < s.NumberOfTransitions; i++ )
            {
                Transition edge = (Transition)s.GetTransition( i );
                //Console.Out.WriteLine( "dfa " + s.dfa.decisionNumber + " edge from s"
                //    + s.stateNumber + " [" + i + "] of " + s.NumberOfTransitions );
                if ( StripNonreducedStates )
                {
                    if ( edge.Target is DFAState &&
                        ( (DFAState)edge.Target ).AcceptStateReachable != Reachable.Yes )
                    {
                        continue; // don't generate nodes for terminal states
                    }
                }
                st = GetTemplates().GetInstanceOf( "edge" );
                st.SetAttribute( "label", GetEdgeLabel( edge ) );
                st.SetAttribute( "src", GetStateLabel( s ) );
                st.SetAttribute( "target", GetStateLabel( edge.Target ) );
                st.SetAttribute( "arrowhead", arrowhead );
                dot.SetAttribute( "edges", st );
                WalkCreatingDFADOT( dot, (DFAState)edge.Target ); // keep walkin'
            }
        }
        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);
            }
        }
        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);
            }
        }
Example #59
0
        /** Useful for when you are sure that you are not part of a composite
         *  already.  Used in Interp/RandomPhrase and testing.
         */
        public Grammar(Tool tool)
        {
            nameSpaceChecker = new NameSpaceChecker( this );
            ll1Analyzer = new LL1Analyzer( this );
            sanity = new GrammarSanity( this );

            builtFromString = true;
            composite = new CompositeGrammar( this );
            Tool = tool;

            TemplateGroup lexerGrammarTemplateGroup = new TemplateGroupString(lexerGrammarTemplateText);
            _lexerGrammarTemplate = lexerGrammarTemplateGroup.GetInstanceOf("grammar");
            string targetsDirectory = Path.Combine(AntlrTool.ToolPathRoot, "Targets");
            target = CodeGenerator.LoadLanguageTarget((string)GetOption("language"), targetsDirectory);
        }
        public override String ToString()
        {
            StringTemplate st = GetMessageTemplate();

            st.SetAttribute("listOfCycles", cycles);
            return(base.ToString(st));
        }