Пример #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 = stlib.GetInstanceOf(Path.Combine(dfaTemplateDirectoryName, "dfa"));
                dot.SetAttribute("startState",
                                 startState.stateNumber);
                dot.SetAttribute("useBox",
                                 AntlrTool.internalOption_ShowNFAConfigsInDFA);
                WalkCreatingDFADOT(dot, (DFAState)startState);
            }
            else
            {
                dot = stlib.GetInstanceOf(Path.Combine(dfaTemplateDirectoryName, "nfa"));
                dot.SetAttribute("startState",
                                 startState.stateNumber);
                WalkRuleNFACreatingDOT(dot, startState);
            }
            dot.SetAttribute("rankdir", rankdir);
            return(dot.ToString());
        }
Пример #2
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 = stlib.GetInstanceOf(Path.Combine(dfaTemplateDirectoryName, "nfa"));

            markedStates = new HashSet <object>();
            dot.SetAttribute("startState", startState.stateNumber);
            walkRuleNFACreatingDOT(dot, startState);
            return(dot.ToString());
        }
        public string Convert(Model.TemplateSource.PropertyModel source)
        {
            string result = "";
            string templateString = sourceStructure.GetPropertyTemplate(source.TypeName);

            Antlr3.ST.StringTemplate template = new Antlr3.ST.StringTemplate();
            template.Template = templateString;
            template.SetAttribute("property", source);

            result = template.ToString();
            return result;
        }
Пример #4
0
        protected FileResult FileExcel(Antlr3.ST.StringTemplate st, string fileName)
        {
            Response.AppendCookie(new HttpCookie("fileDownload", "true")
            {
                Path = "/"
            });
            st.SetAttribute("FileTitle", fileName);
            var str = st.ToString();

            byte[] fileContents = Encoding.UTF8.GetBytes(str);
            return(File(fileContents, "application/vnd.ms-excel", $"{fileName}.xls"));
        }
Пример #5
0
        public void TestTemplateConstructor() /*throws Exception*/
        {
            string action    = "x = %foo(name={$ID.text});";
            string expecting = "x = templateLib.getInstanceOf(\"foo\"," +
                               LINE_SEP + "  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(".", typeof(AngleBracketTemplateLexer));
            StringTemplate actionST = new StringTemplate(templates, rawTranslation);
            string         found    = actionST.ToString();

            assertNoErrors(equeue);

            assertEquals(expecting, found);
        }
Пример #6
0
        public void TestNewlineLiterals() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "lexer grammar T;\n" +
                "A : '\\n\\n' ;\n"  // ANTLR sees '\n\n'
                );
            string expecting = "match(\"\\n\\n\")";

            AntlrTool antlr = newTool();

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

            g.CodeGenerator = generator;
            generator.GenRecognizer(); // codegen phase sets some vars we need
            StringTemplate codeST = generator.RecognizerST;
            string         code   = codeST.ToString();
            int            m      = code.IndexOf("match(\"");
            string         found  = code.Substring(m, expecting.Length);

            assertEquals(expecting, found);
        }
Пример #7
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(".", typeof(AngleBracketTemplateLexer));
            StringTemplate actionST = new StringTemplate(templates, rawTranslation);
            string         found    = actionST.ToString();

            assertNoErrors(equeue);

            assertEquals(expecting, found);
        }
Пример #8
0
        public virtual string ToString(StringTemplate messageST)
        {
            // setup the location
            locationST      = ErrorManager.GetLocationFormat();
            reportST        = ErrorManager.GetReportFormat();
            messageFormatST = ErrorManager.GetMessageFormat();
            bool locationValid = false;

            if (line != -1)
            {
                locationST.SetAttribute("line", line);
                locationValid = true;
            }
            if (charPositionInLine != -1)
            {
                locationST.SetAttribute("column", charPositionInLine + 1);
                locationValid = true;
            }
            if (file != null)
            {
                locationST.SetAttribute("file", file);
                locationValid = true;
            }

            messageFormatST.SetAttribute("id", msgID);
            messageFormatST.SetAttribute("text", messageST);

            if (locationValid)
            {
                reportST.SetAttribute("location", locationST);
            }
            reportST.SetAttribute("message", messageFormatST);
            reportST.SetAttribute("type", ErrorManager.GetMessageType(msgID));

            return(reportST.ToString());
        }
Пример #9
0
        public void TestRefToRuleWithNoReturnValue() /*throws Exception*/
        {
            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);

            string grammarStr =
                "grammar P;\n" +
                "a : x=b ;\n" +
                "b : B ;\n" +
                "B : 'b' ;\n";
            Grammar g = new Grammar(grammarStr);

            AntlrTool     antlr     = newTool();
            CodeGenerator generator = new CodeGenerator(antlr, g, "Java");

            g.CodeGenerator = generator;
            StringTemplate recogST = generator.GenRecognizer();
            string         code    = recogST.ToString();

            assertTrue("not expecting label", code.IndexOf("x=b();") < 0);

            assertEquals("unexpected errors: " + equeue, 0, equeue.errors.Count);
        }
Пример #10
0
        public void TestArguments()
        {
            string action = "$i; $i.x; $u; $u.x";
            string expecting = "i; i.x; u; u.x";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "parser grammar t;\n" +
                "a[User u, int i]\n" +
                "        : {" + 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, "a",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
        }
Пример #11
0
 public void TestEscaped_InAction()
 {
     string action = "int \\$n; \"\\$in string\\$\"";
     string expecting = "int $n; \"$in string$\"";
     Grammar g = new Grammar(
         "parser grammar t;\n" +
         "@members {" + action + "}\n" +
         "a[User u, int i]\n" +
         "        : {" + 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,
                                   "a",
                                   new CommonToken( ANTLRParser.ACTION, action ), 0 );
     string rawTranslation =
         translator.Translate();
     StringTemplateGroup templates =
         new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) );
     StringTemplate actionST = new StringTemplate( templates, rawTranslation );
     string found = actionST.ToString();
     assertEquals( expecting, found );
 }
Пример #12
0
        public void TestRefToTextAttributeForCurrentRule()
        {
            string action = "$text";
            string expecting = "input.toString(retval.start,input.LT(-1))";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "parser grammar t;\n" +
                "options {output=template;}\n" +
                "a : {" + action + "}\n" +
                "  ;\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( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
        }
Пример #13
0
 public void TestDoNotTranslateAttributeCompare()
 {
     string action = "$a.line == $b.line";
     string expecting = "(a!=null?a.getLine():0) == (b!=null?b.getLine():0)";
     ErrorQueue equeue = new ErrorQueue();
     ErrorManager.SetErrorListener( equeue );
     Grammar g = new Grammar(
             "lexer grammar a;\n" +
             "RULE:\n" +
             "     a=ID b=ID {" + action + "}" +
             "    ;\n" +
             "ID : 'id';"
     );
     AntlrTool antlr = newTool();
     CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
     g.CodeGenerator = generator;
     generator.GenRecognizer();
     ActionTranslator translator = new ActionTranslator( generator,
                                                                  "RULE",
                                                                  new CommonToken( ANTLRParser.ACTION, action ), 1 );
     string rawTranslation =
         translator.Translate();
     StringTemplateGroup templates =
         new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) );
     StringTemplate actionST = new StringTemplate( templates, rawTranslation );
     string found = actionST.ToString();
     assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
     assertEquals( expecting, found );
 }
Пример #14
0
        public void TestDynamicScopeRefOkEvenThoughRuleRefExists()
        {
            string action = "$b::n;";
            string expecting = "((b_scope)b_stack.peek()).n;";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "s : b ;\n" +
                "b\n" +
                "scope {\n" +
                "  int n;\n" +
                "} : '(' b ')' {" + action + "}\n" + // refers to current invocation's n
                "  ;\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, "b",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
        }
Пример #15
0
 public void TestAssignToTreeNodeAttribute()
 {
     string action = "$tree.scope = localScope;";
     string expecting = "(()retval.tree).scope = localScope;";
     ErrorQueue equeue = new ErrorQueue();
     ErrorManager.SetErrorListener( equeue );
     Grammar g = new Grammar(
         "grammar a;\n" +
         "options { output=AST; }" +
         "rule\n" +
         "@init {\n" +
         "   Scope localScope=null;\n" +
         "}\n" +
         "@after {\n" +
         "   $tree.scope = localScope;\n" +
         "}\n" +
         "   : 'a' -> ^('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,
                                                                  "rule",
                                                                  new CommonToken( ANTLRParser.ACTION, action ), 1 );
     string rawTranslation =
         translator.Translate();
     StringTemplateGroup templates =
         new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) );
     StringTemplate actionST = new StringTemplate( templates, rawTranslation );
     string found = actionST.ToString();
     assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
     assertEquals( expecting, found );
 }
Пример #16
0
        public void TestBasicRuleScope()
        {
            string action = "$a::n;";
            string expecting = "((a_scope)a_stack.peek()).n;";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a\n" +
                "scope {\n" +
                "  int n;\n" +
                "} : {" + action + "}\n" +
                "  ;\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( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
        }
Пример #17
0
        public void TestSetFullyQualifiedRefToCurrentRuleRetVal()
        {
            string action = "$a.i = 1;";
            string expecting = "retval.i = 1;";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a returns [int i, int j]: {" + action + "}\n" +
                "  ;\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( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
        }
Пример #18
0
        public void TestSettingLexerRulePropertyRefs()
        {
            string action = "$text $type=1 $line=1 $pos=1 $channel=1 $index";
            string expecting = "getText() _type=1 state.tokenStartLine=1 state.tokenStartCharPositionInLine=1 _channel=1 -1";
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "lexer grammar t;\n" +
                "R : 'r' {" + 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,
                                          "R",
                                          new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();

            assertEquals( expecting, found );

            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
        }
Пример #19
0
        public void TestRuleLabelBeforeRefToPredefinedAttr()
        {
            // As of Mar 2007, I'm removing unused labels.  Unfortunately,
            // the action is not seen until code gen.  Can't see $x.text
            // before stripping unused labels.  We really need to translate
            // actions first so code gen logic can use info.
            string action = "$x.text";
            string expecting = "(x!=null?input.toString(x.start,x.stop):null)";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "parser grammar t;\n" +
                "a : x=b {" + action + "} ;\n" +
                "b : B ;\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( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
        }
Пример #20
0
        public void TestRuleRefWithDynamicScope()
        {
            string action = "$field::x = $field.st;";
            string expecting = "((field_scope)field_stack.peek()).x = retval.st;";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar a;\n" +
                "field\n" +
                "scope { StringTemplate x; }\n" +
                "    :   'y' {" + action + "}\n" +
                "    ;\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,
                                                                         "field",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
        }
Пример #21
0
        public void TestReuseExistingListLabelWithImplicitTokenLabel()
        {
            string action = "$ID.text;";
            string expecting = "(x!=null?x.getText():null);";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a : x+=ID {" + action + "} ;" +
                "ID : 'a';\n" );
            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer();

            ActionTranslator translator = new ActionTranslator( generator, "a",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
        }
Пример #22
0
        public void TestReturnWithMultipleRuleRefs()
        {
            string action1 = "$obj = $rule2.obj;";
            string action2 = "$obj = $rule3.obj;";
            string expecting1 = "obj = rule21;";
            string expecting2 = "obj = rule32;";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "rule1 returns [ Object obj ]\n" +
                ":	rule2 { " + action1 + " }\n" +
                "|	rule3 { " + action2 + " }\n" +
                ";\n" +
                "rule2 returns [ Object obj ]\n" +
                ":	foo='foo' { $obj = $foo.text; }\n" +
                ";\n" +
                "rule3 returns [ Object obj ]\n" +
                ":	bar='bar' { $obj = $bar.text; }\n" +
                ";" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            int i = 0;
            string action = action1;
            string expecting = expecting1;
            do
            {
                ActionTranslator translator = new ActionTranslator( generator, "rule1",
                                                                             new CommonToken( ANTLRParser.ACTION, action ), i + 1 );
                string rawTranslation =
                        translator.Translate();
                StringTemplateGroup templates =
                        new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) );
                StringTemplate actionST = new StringTemplate( templates, rawTranslation );
                string found = actionST.ToString();
                assertEquals( expecting, found );
                action = action2;
                expecting = expecting2;
            } while ( i++ < 1 );
            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
        }
Пример #23
0
        public void TestReturnValueWithNumber()
        {
            string action = "$x.i1";
            string expecting = "x";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a returns [int i1]\n" +
                "        : 'a'\n" +
                "        ;\n" +
                "b : x=a {" + 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,
                                          "b",
                                          new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
        }
Пример #24
0
        public void TestUnqualifiedRuleScopeAttribute()
        {
            string action = "$n;"; // must be qualified
            string expecting = "$n;";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a\n" +
                "scope {\n" +
                "  int n;\n" +
                "} : b\n" +
                "  ;\n" +
                "b : {'+action+'}\n" +
                "  ;\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            ActionTranslator translator =
                new ActionTranslator( generator,
                                          "b",
                                          new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            int expectedMsgID = ErrorManager.MSG_UNKNOWN_SIMPLE_ATTRIBUTE;
            object expectedArg = "n";
            object expectedArg2 = null;
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
            checkError( equeue, expectedMessage );
        }
Пример #25
0
        public void TestSharedGlobalScope()
        {
            string action = "$Symbols::x;";
            string expecting = "((Symbols_scope)Symbols_stack.peek()).x;";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "scope Symbols {\n" +
                "  String x;\n" +
                "}\n" +
                "a\n" +
                "scope { int y; }\n" +
                "scope Symbols;\n" +
                " : b {" + action + "}\n" +
                " ;\n" +
                "b : ID {$Symbols::x=$ID.text} ;\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( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
        }
Пример #26
0
        public void TestAssignToOwnRulenameAttr()
        {
            string action = "$rule.tree = null;";
            string expecting = "retval.tree = 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();
            StringTemplateGroup templates =
                new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
        }
Пример #27
0
        public void TestRefToReturnValueBeforeRefToPredefinedAttr()
        {
            string action = "$x.foo";
            string expecting = "(x!=null?x.foo:0)";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "parser grammar t;\n" +
                "a : x=b {" + action + "} ;\n" +
                "b returns [int foo] : B {$b.start} ;\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( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
        }
Пример #28
0
        public void TestBasicGlobalScope()
        {
            string action = "$Symbols::names.add($id.text);";
            string expecting = "((Symbols_scope)Symbols_stack.peek()).names.add((id!=null?id.getText():null));";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "scope Symbols {\n" +
                "  int n;\n" +
                "  List names;\n" +
                "}\n" +
                "a scope Symbols; : (id=ID ';' {" + action + "} )+\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( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
        }
Пример #29
0
        public void TestNonDynamicAttributeOutsideRule2()
        {
            string action = "[TestMethod] public void foo() { $x.y; }";
            string expecting = action;

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "parser grammar t;\n" +
                "@members {'+action+'}\n" +
                "a : ;\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            ActionTranslator translator = new ActionTranslator( generator,
                                                                         null,
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 0 );
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            int expectedMsgID = ErrorManager.MSG_ATTRIBUTE_REF_NOT_IN_RULE;
            object expectedArg = "x";
            object expectedArg2 = "y";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
            checkError( equeue, expectedMessage );
        }
Пример #30
0
        public void TestComplicatedArgParsingWithTranslation()
        {
            string action = "x, $A.text+\"3242\", (*$A).foo(21,33), 3.2+1, '\\n', " +
                            "\"a,oo\\nick\", {bl, \"fdkj\"eck}";
            string expecting = "x, (A1!=null?A1.getText():null)+\"3242\", (*A1).foo(21,33), 3.2+1, '\\n', \"a,oo\\nick\", {bl, \"fdkj\"eck}";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );

            // now check in actual grammar.
            Grammar g = new Grammar(
                "parser grammar t;\n" +
                "a[User u, int i]\n" +
                "        : A a[" + action + "] B\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( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
        }
Пример #31
0
        /** From T.g return a list of File objects that
         *  name files ANTLR will emit from T.g.
         */
        public virtual IList <string> GetGeneratedFileList()
        {
            List <FileInfo> files = new List <FileInfo>();

            System.IO.DirectoryInfo outputDir = tool.GetOutputDirectory(grammarFileName);
            if (outputDir.Name.Equals("."))
            {
                outputDir = null;
            }
            else if (outputDir.Name.IndexOf(' ') >= 0)
            { // has spaces?
                string escSpaces = outputDir.ToString().Replace(
                    " ",
                    "\\ ");
                outputDir = new System.IO.DirectoryInfo(escSpaces);
            }
            // add generated recognizer; e.g., TParser.java
            string recognizer =
                generator.GetRecognizerFileName(grammar.name, grammar.type);

            files.Add(new FileInfo(System.IO.Path.Combine(outputDir.FullName, recognizer)));
            // add output vocab file; e.g., T.tokens. This is always generated to
            // the base output directory, which will be just . if there is no -o option
            //
            files.Add(new FileInfo(System.IO.Path.Combine(tool.OutputDirectory, generator.VocabFileName)));
            // are we generating a .h file?
            StringTemplate headerExtST = null;
            StringTemplate extST       = generator.Templates.GetInstanceOf("codeFileExtension");

            if (generator.Templates.IsDefined("headerFile"))
            {
                headerExtST = generator.Templates.GetInstanceOf("headerFileExtension");
                string suffix   = Grammar.grammarTypeToFileNameSuffix[(int)grammar.type];
                string fileName = grammar.name + suffix + headerExtST.ToString();
                files.Add(new FileInfo(System.IO.Path.Combine(outputDir.FullName, fileName)));
            }
            if (grammar.type == GrammarType.Combined)
            {
                // add autogenerated lexer; e.g., TLexer.java TLexer.h TLexer.tokens
                // don't add T__.g (just a temp file)
                string suffix = Grammar.grammarTypeToFileNameSuffix[(int)GrammarType.Lexer];
                string lexer  = grammar.name + suffix + extST.ToString();
                files.Add(new FileInfo(System.IO.Path.Combine(outputDir.FullName, lexer)));

                // TLexer.h
                if (headerExtST != null)
                {
                    string header = grammar.name + suffix + headerExtST.ToString();
                    files.Add(new FileInfo(System.IO.Path.Combine(outputDir.FullName, header)));
                }
                // for combined, don't generate TLexer.tokens
            }

            // handle generated files for imported grammars
            IList <Grammar> imports =
                grammar.composite.GetDelegates(grammar.composite.RootGrammar);

            foreach (Grammar g in imports)
            {
                outputDir = tool.GetOutputDirectory(g.FileName);
                string fname = GroomQualifiedFileName(outputDir.ToString(), g.GetRecognizerName() + extST.ToString());
                files.Add(new FileInfo(fname));
            }

            if (files.Count == 0)
            {
                return(null);
            }

            return(files.Select(info => info.FullName).ToArray());
        }
Пример #32
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;
             }
         }
     }
     assertFalse( "action translator used scopeSetAttributeRef template in comparison!", foundScopeSetAttributeRef );
     StringTemplateGroup templates =
         new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) );
     StringTemplate actionST = new StringTemplate( templates, rawTranslation );
     string found = actionST.ToString();
     assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
     assertEquals( expecting, found );
 }
Пример #33
0
        public void TestTokenRefTreeProperty()
        {
            string action = "$ID.tree;";
            string expecting = "ID1_tree;";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a : ID {" + action + "} ;" +
                "ID : 'a';\n" );
            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer();

            ActionTranslator translator = new ActionTranslator( generator, "a",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );
        }
Пример #34
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( ".", typeof( AngleBracketTemplateLexer ) );
     StringTemplate actionST = new StringTemplate( templates, "<action>" );
     actionST.SetAttribute( "action", rawTranslation );
     string found = actionST.ToString();
     assertEquals( expecting, found );
 }
Пример #35
0
        public void TestUnknownGlobalDynamicAttribute()
        {
            string action = "$Symbols::x";
            string expecting = action;

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "scope Symbols {\n" +
                "  int n;\n" +
                "}\n" +
                "a : {'+action+'}\n" +
                "  ;\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( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            int expectedMsgID = ErrorManager.MSG_UNKNOWN_DYNAMIC_SCOPE_ATTRIBUTE;
            object expectedArg = "Symbols";
            object expectedArg2 = "x";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
            checkError( equeue, expectedMessage );
        }
Пример #36
0
        public void TestTokenLabelFromMultipleAlts()
        {
            string action = "$ID.text;"; // must be qualified
            string action2 = "$INT.text;"; // must be qualified
            string expecting = "(ID1!=null?ID1.getText():null);";
            string expecting2 = "(INT2!=null?INT2.getText():null);";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a : ID {" + action + "}\n" +
                "  | INT {" + action2 + "}\n" +
                "  ;\n" +
                "ID : 'a';\n" +
                "INT : '0';\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( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
            translator = new ActionTranslator( generator,
                                                   "a",
                                                   new CommonToken( ANTLRParser.ACTION, action2 ), 2 );
            rawTranslation =
                translator.Translate();
            templates =
                new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) );
            actionST = new StringTemplate( templates, rawTranslation );
            found = actionST.ToString();

            assertEquals( expecting2, found );

            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
        }
Пример #37
0
        public void TestRefToRuleRefInLexerNoAttribute()
        {
            string action = "$ID";
            string expecting = "ID1";
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "lexer grammar t;\n" +
                "R : 'z' ID {" + action + "};" +
                "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,
                                          "R",
                                          new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();

            assertEquals( expecting, found );

            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
        }
Пример #38
0
        public void TestSimplePlusEqualLabel()
        {
            string action = "$ids.size();"; // must be qualified
            string expecting = "list_ids.size();";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "parser grammar t;\n" +
                "a : ids+=ID ( COMMA ids+=ID {" + 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,
                                          "a",
                                          new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
        }
Пример #39
0
        public void TestTokenLabels()
        {
            string action = "$id; $f; $id.text; $id.getText(); $id.dork " +
                            "$id.type; $id.line; $id.pos; " +
                            "$id.channel; $id.index;";
            string expecting = "id; f; (id!=null?id.getText():null); id.getText(); id.dork (id!=null?id.getType():0); (id!=null?id.getLine():0); (id!=null?id.getCharPositionInLine():0); (id!=null?id.getChannel():0); (id!=null?id.getTokenIndex():0);";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "parser grammar t;\n" +
                "a : id=ID f=FLOAT {" + 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, "a",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
        }