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);
            }
Beispiel #2
0
        public TemplateGroup GetTemplates()
        {
            if (_stlib == null)
            {
                _stlib = new TemplateGroupFile(Path.Combine(dfaTemplateDirectoryName, "dot.stg"));
            }

            return(_stlib);
        }
            public override StringTemplate GenExpr(CodeGenerator generator, TemplateGroup templates, DFA dfa)
            {
                if (templates != null)
                {
                    return(templates.GetInstanceOf("false_value"));
                }

                return(new StringTemplate("false"));
            }
Beispiel #4
0
 public LeftRecursiveRuleAnalyzer(ITreeNodeStream input, Grammar g, string ruleName)
     : base(input)
 {
     this.g        = g;
     this.ruleName = ruleName;
     language      = (string)g.GetOption("language");
     generator     = new CodeGenerator(g.Tool, g, language);
     generator.LoadTemplates(language);
     recRuleTemplates = LoadPrecRuleTemplates(g.Tool);
 }
Beispiel #5
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());
        }
Beispiel #6
0
        /** The format gets reset either from the Tool if the user supplied a command line option to that effect
         *  Otherwise we just use the default "antlr".
         */
        public static void SetFormat(String formatName)
        {
            if (ErrorManager.formatName == formatName)
            {
                return;
            }

            ErrorManager.formatName = formatName;
            string fileName = Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(AntlrTool.ToolPathRoot, "Tool"), "Templates"), "messages"), "formats"), formatName + ".stg");

            format = new TemplateGroupFile(fileName);
            if (!File.Exists(fileName) && formatName != "antlr")
            {
                SetFormat("antlr");
                return;
            }

            format.EnableCache = AntlrTool.EnableTemplateCache;
            format.Listener    = initSTListener;
            if (!format.IsDefined("message"))
            {
                // pick random msg to load
                if (formatName.Equals("antlr"))
                {
                    RawError("no such message format file " + fileName + " retrying with default ANTLR format");
                    // recurse on this rule, trying the default message format
                    SetFormat("antlr");
                    return;
                }
                else
                {
                    // recurse on this rule, trying the default message format
                    SetFormat("antlr");
                }
            }

            format.Listener = blankSTListener;
            bool formatOK = VerifyFormat();

            if (!formatOK && formatName.Equals("antlr"))
            {
                RawError("ANTLR installation corrupted; ANTLR messages format file " + formatName + ".stg incomplete");
                Panic();
            }
            else if (!formatOK)
            {
                SetFormat("antlr");   // recurse on this rule, trying the default message format
            }
        }
Beispiel #7
0
        /** We really only need a single locale for entire running ANTLR code
         *  in a single VM.  Only pay attention to the language, not the country
         *  so that French Canadians and French Frenchies all get the same
         *  template file, fr.stg.  Just easier this way.
         */
        public static void SetLocale(CultureInfo locale)
        {
            if (ErrorManager.locale == locale)
            {
                return;
            }

            ErrorManager.locale = locale;
            string language = locale.TwoLetterISOLanguageName;
            string fileName = Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(AntlrTool.ToolPathRoot, "Tool"), "Templates"), "messages"), "languages"), language + ".stg");

            if (!File.Exists(fileName) && locale.TwoLetterISOLanguageName != CultureInfo.GetCultureInfo("en-us").TwoLetterISOLanguageName)
            {
                SetLocale(CultureInfo.GetCultureInfo("en-us"));
                return;
            }

            messages             = new TemplateGroupFile(fileName);
            messages.EnableCache = AntlrTool.EnableTemplateCache;
            messages.Listener    = initSTListener;
            if (!messages.IsDefined("INTERNAL_ERROR"))
            {
                // pick random msg to load
                if (language.Equals(CultureInfo.GetCultureInfo("en-us").TwoLetterISOLanguageName))
                {
                    RawError("ANTLR installation corrupted; cannot find English messages file " + fileName);
                    Panic();
                }
                else
                {
                    // recurse on this rule, trying the US locale
                    SetLocale(CultureInfo.GetCultureInfo("en-us"));
                }
            }

            messages.Listener = blankSTListener;
            bool messagesOK = VerifyMessages();

            if (!messagesOK && language.Equals(CultureInfo.GetCultureInfo("en-us").TwoLetterISOLanguageName))
            {
                RawError("ANTLR installation corrupted; English messages file " + language + ".stg incomplete");
                Panic();
            }
            else if (!messagesOK)
            {
                SetLocale(CultureInfo.GetCultureInfo("en-us")); // try US to see if that will work
            }
        }
            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);
            }
            public override StringTemplate GenExpr(CodeGenerator generator, TemplateGroup templates, DFA dfa)
            {
                StringTemplate eST = null;

                if (templates != null)
                {
                    if (_synpred)
                    {
                        eST = templates.GetInstanceOf("evalSynPredicate");
                    }
                    else
                    {
                        eST = templates.GetInstanceOf("evalPredicate");
                        generator.Grammar.decisionsWhoseDFAsUsesSemPreds.Add(dfa);
                    }

                    string predEnclosingRuleName = _predicateAST.enclosingRuleName;

                    /*
                     * String decisionEnclosingRuleName =
                     *  dfa.getNFADecisionStartState().getEnclosingRule();
                     * // if these rulenames are diff, then pred was hoisted out of rule
                     * // Currently I don't warn you about this as it could be annoying.
                     * // I do the translation anyway.
                     */
                    //eST.setAttribute("pred", this.toString());
                    if (generator != null)
                    {
                        eST.SetAttribute("pred", generator.TranslateAction(predEnclosingRuleName, _predicateAST));
                    }
                }
                else
                {
                    eST = new StringTemplate("<pred>");
                    eST.SetAttribute("pred", this.ToString());
                    return(eST);
                }

                if (generator != null)
                {
                    string description = generator.Target.GetTargetStringLiteralFromString(this.ToString());
                    eST.SetAttribute("description", description);
                }

                return(eST);
            }
Beispiel #10
0
            public override StringTemplate GenExpr(CodeGenerator generator, TemplateGroup templates, DFA dfa)
            {
                StringTemplate eST = null;

                if (templates != null)
                {
                    eST = templates.GetInstanceOf("orPredicates");
                }
                else
                {
                    eST = new StringTemplate("(<operands; separator=\"||\">)");
                }

                foreach (SemanticContext semctx in Operands)
                {
                    eST.SetAttribute("operands", semctx.GenExpr(generator, templates, dfa));
                }

                return(eST);
            }
Beispiel #11
0
        public void TestTemplateConstructor() /*throws Exception*/
        {
            string action    = "x = %foo(name={$ID.text});";
            string expecting = "x = templateLib.getInstanceOf(\"foo\"," +
                               "new STAttrMap().put(\"name\", (ID1!=null?ID1.getText():null)));";

            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);
        }
Beispiel #12
0
        public void TestSetAttr() /*throws Exception*/
        {
            string action    = "%x.y = z;";
            string expecting = "(x).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);
        }
Beispiel #13
0
 /** Generate an expression that will evaluate the semantic context,
  *  given a set of output templates.
  */
 public abstract StringTemplate GenExpr(CodeGenerator generator, TemplateGroup templates, DFA dfa);
Beispiel #14
0
        public TemplateGroup GetTemplates()
        {
            if (_stlib == null)
                _stlib = new TemplateGroupFile(Path.Combine(dfaTemplateDirectoryName, "dot.stg"));

            return _stlib;
        }
Beispiel #15
0
 public virtual StringTemplate GenFixedLookaheadDecision(TemplateGroup templates,
                                                         DFA dfa)
 {
     return(WalkFixedDFAGeneratingStateMachine(templates, dfa, dfa.StartState, 1));
 }
Beispiel #16
0
        /** We really only need a single locale for entire running ANTLR code
         *  in a single VM.  Only pay attention to the language, not the country
         *  so that French Canadians and French Frenchies all get the same
         *  template file, fr.stg.  Just easier this way.
         */
        public static void SetLocale( CultureInfo locale )
        {
            if (ErrorManager.locale == locale)
                return;

            ErrorManager.locale = locale;
            string language = locale.TwoLetterISOLanguageName;
            string fileName = Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(AntlrTool.ToolPathRoot, "Tool"), "Templates"), "messages"), "languages"), language + ".stg");
            if (!File.Exists(fileName) && locale.TwoLetterISOLanguageName != CultureInfo.GetCultureInfo("en-us").TwoLetterISOLanguageName)
            {
                SetLocale(CultureInfo.GetCultureInfo("en-us"));
                return;
            }

            messages = new TemplateGroupFile(fileName);
            messages.EnableCache = AntlrTool.EnableTemplateCache;
            messages.Listener = initSTListener;
            if (!messages.IsDefined("INTERNAL_ERROR"))
            {
                // pick random msg to load
                if (language.Equals(CultureInfo.GetCultureInfo("en-us").TwoLetterISOLanguageName))
                {
                    RawError("ANTLR installation corrupted; cannot find English messages file " + fileName);
                    Panic();
                }
                else
                {
                    // recurse on this rule, trying the US locale
                    SetLocale(CultureInfo.GetCultureInfo("en-us"));
                }
            }

            messages.Listener = blankSTListener;
            bool messagesOK = VerifyMessages();
            if ( !messagesOK && language.Equals(CultureInfo.GetCultureInfo("en-us").TwoLetterISOLanguageName) ) {
                RawError("ANTLR installation corrupted; English messages file "+language+".stg incomplete");
                Panic();
            }
            else if ( !messagesOK ) {
                SetLocale(CultureInfo.GetCultureInfo("en-us")); // try US to see if that will work
            }
        }
Beispiel #17
0
        /** The format gets reset either from the Tool if the user supplied a command line option to that effect
         *  Otherwise we just use the default "antlr".
         */
        public static void SetFormat( String formatName )
        {
            if (ErrorManager.formatName == formatName)
                return;

            ErrorManager.formatName = formatName;
            string fileName = Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(AntlrTool.ToolPathRoot, "Tool"), "Templates"), "messages"), "formats"), formatName + ".stg");
            format = new TemplateGroupFile(fileName);
            if (!File.Exists(fileName) && formatName != "antlr")
            {
                SetFormat("antlr");
                return;
            }

            format.EnableCache = AntlrTool.EnableTemplateCache;
            format.Listener = initSTListener;
            if (!format.IsDefined("message"))
            {
                // pick random msg to load
                if (formatName.Equals("antlr"))
                {
                    RawError("no such message format file " + fileName + " retrying with default ANTLR format");
                    // recurse on this rule, trying the default message format
                    SetFormat("antlr");
                    return;
                }
                else
                {
                    // recurse on this rule, trying the default message format
                    SetFormat("antlr");
                }
            }

            format.Listener = blankSTListener;
            bool formatOK = VerifyFormat();
            if ( !formatOK && formatName.Equals( "antlr" ) )
            {
                RawError( "ANTLR installation corrupted; ANTLR messages format file " + formatName + ".stg incomplete" );
                Panic();
            }
            else if ( !formatOK )
            {
                SetFormat( "antlr" ); // recurse on this rule, trying the default message format
            }
        }
Beispiel #18
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 );
        }
Beispiel #19
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 );
        }
Beispiel #20
0
        protected virtual StringTemplate WalkFixedDFAGeneratingStateMachine(
            TemplateGroup templates,
            DFA dfa,
            DFAState s,
            int k)
        {
            //System.Console.Out.WriteLine( "walk " + s.stateNumber + " in dfa for decision " + dfa.decisionNumber );
            if (s.IsAcceptState)
            {
                StringTemplate dfaST2 = templates.GetInstanceOf("dfaAcceptState");
                dfaST2.SetAttribute("alt", s.GetUniquelyPredictedAlt());
                return(dfaST2);
            }

            // the default templates for generating a state and its edges
            // can be an if-then-else structure or a switch
            string dfaStateName              = "dfaState";
            string dfaLoopbackStateName      = "dfaLoopbackState";
            string dfaOptionalBlockStateName = "dfaOptionalBlockState";
            string dfaEdgeName = "dfaEdge";

            if (_parentGenerator.CanGenerateSwitch(s))
            {
                dfaStateName              = "dfaStateSwitch";
                dfaLoopbackStateName      = "dfaLoopbackStateSwitch";
                dfaOptionalBlockStateName = "dfaOptionalBlockStateSwitch";
                dfaEdgeName = "dfaEdgeSwitch";
            }

            StringTemplate dfaST = templates.GetInstanceOf(dfaStateName);

            if (dfa.NFADecisionStartState.decisionStateType == NFAState.LOOPBACK)
            {
                dfaST = templates.GetInstanceOf(dfaLoopbackStateName);
            }
            else if (dfa.NFADecisionStartState.decisionStateType == NFAState.OPTIONAL_BLOCK_START)
            {
                dfaST = templates.GetInstanceOf(dfaOptionalBlockStateName);
            }
            dfaST.SetAttribute("k", k);
            dfaST.SetAttribute("stateNumber", s.StateNumber);
            dfaST.SetAttribute("semPredState",
                               s.IsResolvedWithPredicates);

            /*
             * string description = dfa.getNFADecisionStartState().Description;
             * description = parentGenerator.target.getTargetStringLiteralFromString( description );
             * //System.Console.Out.WriteLine( "DFA: " + description + " associated with AST " + dfa.getNFADecisionStartState() );
             * if ( description != null )
             * {
             *  dfaST.SetAttribute( "description", description );
             * }
             */
            int      EOTPredicts = NFA.INVALID_ALT_NUMBER;
            DFAState EOTTarget   = null;

            //System.Console.Out.WriteLine( "DFA state " + s.stateNumber );
            for (int i = 0; i < s.NumberOfTransitions; i++)
            {
                Transition edge = (Transition)s.GetTransition(i);
                //System.Console.Out.WriteLine( "edge " + s.stateNumber + "-" + edge.label.ToString() + "->" + edge.target.stateNumber );
                if (edge.Label.Atom == Label.EOT)
                {
                    // don't generate a real edge for EOT; track alt EOT predicts
                    // generate that prediction in the else clause as default case
                    EOTTarget   = (DFAState)edge.Target;
                    EOTPredicts = EOTTarget.GetUniquelyPredictedAlt();

                    /*
                     * System.Console.Out.WriteLine("DFA s"+s.stateNumber+" EOT goes to s"+
                     *                 edge.target.stateNumber+" predicates alt "+
                     *                 EOTPredicts);
                     */
                    continue;
                }
                StringTemplate edgeST = templates.GetInstanceOf(dfaEdgeName);
                // If the template wants all the label values delineated, do that
                if (edgeST.impl.TryGetFormalArgument("labels") != null)
                {
                    List <string> labels = edge.Label.Set.Select(value => _parentGenerator.GetTokenTypeAsTargetLabel(value)).ToList();
                    edgeST.SetAttribute("labels", labels);
                }
                else
                { // else create an expression to evaluate (the general case)
                    edgeST.SetAttribute("labelExpr",
                                        _parentGenerator.GenLabelExpr(templates, edge, k));
                }

                // stick in any gated predicates for any edge if not already a pred
                if (!edge.Label.IsSemanticPredicate)
                {
                    DFAState        target = (DFAState)edge.Target;
                    SemanticContext preds  =
                        target.GetGatedPredicatesInNFAConfigurations();
                    if (preds != null)
                    {
                        //System.Console.Out.WriteLine( "preds=" + target.getGatedPredicatesInNFAConfigurations() );
                        StringTemplate predST = preds.GenExpr(_parentGenerator,
                                                              _parentGenerator.Templates,
                                                              dfa);
                        edgeST.SetAttribute("predicates", predST);
                    }
                }

                StringTemplate targetST =
                    WalkFixedDFAGeneratingStateMachine(templates,
                                                       dfa,
                                                       (DFAState)edge.Target,
                                                       k + 1);
                edgeST.SetAttribute("targetState", targetST);
                dfaST.SetAttribute("edges", edgeST);
                //System.Console.Out.WriteLine( "back to DFA " + dfa.decisionNumber + "." + s.stateNumber );
            }

            // HANDLE EOT EDGE
            if (EOTPredicts != NFA.INVALID_ALT_NUMBER)
            {
                // EOT unique predicts an alt
                dfaST.SetAttribute("eotPredictsAlt", EOTPredicts);
            }
            else if (EOTTarget != null && EOTTarget.NumberOfTransitions > 0)
            {
                // EOT state has transitions so must split on predicates.
                // Generate predicate else-if clauses and then generate
                // NoViableAlt exception as else clause.
                // Note: these predicates emanate from the EOT target state
                // rather than the current DFAState s so the error message
                // might be slightly misleading if you are looking at the
                // state number.  Predicates emanating from EOT targets are
                // hoisted up to the state that has the EOT edge.
                for (int i = 0; i < EOTTarget.NumberOfTransitions; i++)
                {
                    Transition     predEdge = (Transition)EOTTarget.GetTransition(i);
                    StringTemplate edgeST   = templates.GetInstanceOf(dfaEdgeName);
                    edgeST.SetAttribute("labelExpr",
                                        _parentGenerator.GenSemanticPredicateExpr(templates, predEdge));
                    // the target must be an accept state
                    //System.Console.Out.WriteLine( "EOT edge" );
                    StringTemplate targetST =
                        WalkFixedDFAGeneratingStateMachine(templates,
                                                           dfa,
                                                           (DFAState)predEdge.Target,
                                                           k + 1);
                    edgeST.SetAttribute("targetState", targetST);
                    dfaST.SetAttribute("edges", edgeST);
                }
            }
            return(dfaST);
        }
        protected virtual StringTemplate WalkFixedDFAGeneratingStateMachine(
                TemplateGroup templates,
                DFA dfa,
                DFAState s,
                int k )
        {
            //System.Console.Out.WriteLine( "walk " + s.stateNumber + " in dfa for decision " + dfa.decisionNumber );
            if ( s.IsAcceptState )
            {
                StringTemplate dfaST2 = templates.GetInstanceOf( "dfaAcceptState" );
                dfaST2.SetAttribute( "alt", s.GetUniquelyPredictedAlt() );
                return dfaST2;
            }

            // the default templates for generating a state and its edges
            // can be an if-then-else structure or a switch
            string dfaStateName = "dfaState";
            string dfaLoopbackStateName = "dfaLoopbackState";
            string dfaOptionalBlockStateName = "dfaOptionalBlockState";
            string dfaEdgeName = "dfaEdge";
            if ( _parentGenerator.CanGenerateSwitch( s ) )
            {
                dfaStateName = "dfaStateSwitch";
                dfaLoopbackStateName = "dfaLoopbackStateSwitch";
                dfaOptionalBlockStateName = "dfaOptionalBlockStateSwitch";
                dfaEdgeName = "dfaEdgeSwitch";
            }

            StringTemplate dfaST = templates.GetInstanceOf( dfaStateName );
            if ( dfa.NFADecisionStartState.decisionStateType == NFAState.LOOPBACK )
            {
                dfaST = templates.GetInstanceOf( dfaLoopbackStateName );
            }
            else if ( dfa.NFADecisionStartState.decisionStateType == NFAState.OPTIONAL_BLOCK_START )
            {
                dfaST = templates.GetInstanceOf( dfaOptionalBlockStateName );
            }
            dfaST.SetAttribute( "k", k );
            dfaST.SetAttribute( "stateNumber", s.StateNumber );
            dfaST.SetAttribute( "semPredState",
                               s.IsResolvedWithPredicates );
            /*
            string description = dfa.getNFADecisionStartState().Description;
            description = parentGenerator.target.getTargetStringLiteralFromString( description );
            //System.Console.Out.WriteLine( "DFA: " + description + " associated with AST " + dfa.getNFADecisionStartState() );
            if ( description != null )
            {
                dfaST.SetAttribute( "description", description );
            }
            */
            int EOTPredicts = NFA.INVALID_ALT_NUMBER;
            DFAState EOTTarget = null;
            //System.Console.Out.WriteLine( "DFA state " + s.stateNumber );
            for ( int i = 0; i < s.NumberOfTransitions; i++ )
            {
                Transition edge = (Transition)s.GetTransition( i );
                //System.Console.Out.WriteLine( "edge " + s.stateNumber + "-" + edge.label.ToString() + "->" + edge.target.stateNumber );
                if ( edge.Label.Atom == Label.EOT )
                {
                    // don't generate a real edge for EOT; track alt EOT predicts
                    // generate that prediction in the else clause as default case
                    EOTTarget = (DFAState)edge.Target;
                    EOTPredicts = EOTTarget.GetUniquelyPredictedAlt();
                    /*
                    System.Console.Out.WriteLine("DFA s"+s.stateNumber+" EOT goes to s"+
                                       edge.target.stateNumber+" predicates alt "+
                                       EOTPredicts);
                    */
                    continue;
                }
                StringTemplate edgeST = templates.GetInstanceOf( dfaEdgeName );
                // If the template wants all the label values delineated, do that
                if ( edgeST.impl.TryGetFormalArgument( "labels" ) != null )
                {
                    List<string> labels = edge.Label.Set.Select( value => _parentGenerator.GetTokenTypeAsTargetLabel( value ) ).ToList();
                    edgeST.SetAttribute( "labels", labels );
                }
                else
                { // else create an expression to evaluate (the general case)
                    edgeST.SetAttribute( "labelExpr",
                                        _parentGenerator.GenLabelExpr( templates, edge, k ) );
                }

                // stick in any gated predicates for any edge if not already a pred
                if ( !edge.Label.IsSemanticPredicate )
                {
                    DFAState target = (DFAState)edge.Target;
                    SemanticContext preds =
                        target.GetGatedPredicatesInNFAConfigurations();
                    if ( preds != null )
                    {
                        //System.Console.Out.WriteLine( "preds=" + target.getGatedPredicatesInNFAConfigurations() );
                        StringTemplate predST = preds.GenExpr( _parentGenerator,
                                                              _parentGenerator.Templates,
                                                              dfa );
                        edgeST.SetAttribute( "predicates", predST );
                    }
                }

                StringTemplate targetST =
                    WalkFixedDFAGeneratingStateMachine( templates,
                                                       dfa,
                                                       (DFAState)edge.Target,
                                                       k + 1 );
                edgeST.SetAttribute( "targetState", targetST );
                dfaST.SetAttribute( "edges", edgeST );
                //System.Console.Out.WriteLine( "back to DFA " + dfa.decisionNumber + "." + s.stateNumber );
            }

            // HANDLE EOT EDGE
            if ( EOTPredicts != NFA.INVALID_ALT_NUMBER )
            {
                // EOT unique predicts an alt
                dfaST.SetAttribute( "eotPredictsAlt", EOTPredicts );
            }
            else if ( EOTTarget != null && EOTTarget.NumberOfTransitions > 0 )
            {
                // EOT state has transitions so must split on predicates.
                // Generate predicate else-if clauses and then generate
                // NoViableAlt exception as else clause.
                // Note: these predicates emanate from the EOT target state
                // rather than the current DFAState s so the error message
                // might be slightly misleading if you are looking at the
                // state number.  Predicates emanating from EOT targets are
                // hoisted up to the state that has the EOT edge.
                for ( int i = 0; i < EOTTarget.NumberOfTransitions; i++ )
                {
                    Transition predEdge = (Transition)EOTTarget.GetTransition( i );
                    StringTemplate edgeST = templates.GetInstanceOf( dfaEdgeName );
                    edgeST.SetAttribute( "labelExpr",
                                        _parentGenerator.GenSemanticPredicateExpr( templates, predEdge ) );
                    // the target must be an accept state
                    //System.Console.Out.WriteLine( "EOT edge" );
                    StringTemplate targetST =
                        WalkFixedDFAGeneratingStateMachine( templates,
                                                           dfa,
                                                           (DFAState)predEdge.Target,
                                                           k + 1 );
                    edgeST.SetAttribute( "targetState", targetST );
                    dfaST.SetAttribute( "edges", edgeST );
                }
            }
            return dfaST;
        }
 public void Init( Grammar g )
 {
     this.grammar = g;
     this.generator = grammar.CodeGenerator;
     this.templates = generator.Templates;
 }
Beispiel #23
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 );
        }
Beispiel #24
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 );
 }
Beispiel #25
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 );
 }
 public LeftRecursiveRuleAnalyzer(ITreeNodeStream input, Grammar g, string ruleName)
     : base(input)
 {
     this.g = g;
     this.ruleName = ruleName;
     language = (string)g.GetOption("language");
     generator = new CodeGenerator(g.Tool, g, language);
     generator.LoadTemplates(language);
     recRuleTemplates = LoadPrecRuleTemplates(g.Tool);
 }
 public virtual StringTemplate GenFixedLookaheadDecision( TemplateGroup templates,
                                                 DFA dfa )
 {
     return WalkFixedDFAGeneratingStateMachine( templates, dfa, dfa.StartState, 1 );
 }