public void TestMessageStringificationIsConsistent() { string action = "$other.tree = null;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar a;\n" + "options { output = AST;}" + "otherrule\n" + " : 'y' ;" + "rule\n" + " : other=otherrule {" + 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; object expectedArg = "other"; object expectedArg2 = "tree"; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 ); string expectedMessageString = expectedMessage.ToString(); Assert.AreEqual( expectedMessageString, expectedMessage.ToString() ); }
public void TestMessageStringificationIsConsistent() /*throws Exception*/ { string action = "$other.tree = null;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener(equeue); Grammar g = new Grammar( "grammar a;\n" + "options { output = AST;}" + "otherrule\n" + " : 'y' ;" + "rule\n" + " : other=otherrule {" + 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; object expectedArg = "other"; object expectedArg2 = "tree"; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2); string expectedMessageString = expectedMessage.ToString(); Assert.AreEqual(expectedMessageString, expectedMessage.ToString()); }
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); }
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); }
public void TestTemplateConstructorNoArgs() /*throws Exception*/ { 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); }
public void TestSetAttrOfExpr() /*throws Exception*/ { 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); }
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 found = translator.Translate(); Assert.AreEqual(expecting, found); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
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 found = translator.Translate(); Assert.AreEqual(expecting, found); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
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 ); }
public void TestFullyQualifiedRefToCurrentRuleParameter() { string action = "$a.i;"; string expecting = "i;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar t;\n" + "a[int i]: {" + 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 found = translator.Translate(); Assert.AreEqual(expecting, found); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
public void Test0IndexedGlobalScope() { string action = "$Symbols[0]::names.add($id.text);"; string expecting = "((Symbols_scope)Symbols_stack.elementAt(0)).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 found = translator.Translate(); Assert.AreEqual(expecting, found); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
public void TestDynamicRuleScopeRefInSubrule() { 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" + " float n;\n" + "} : b ;\n" + "b : {" + 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, "b", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string found = translator.Translate(); Assert.AreEqual(expecting, found); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
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 found = translator.Translate(); Assert.AreEqual(expecting, found); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
public void TestComplicatedSingleArgParsing() { string action = "(*a).foo(21,33,\",\")"; string expecting = "(*a).foo(21,33,\",\")"; 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(); Assert.AreEqual( expecting, rawTranslation ); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
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 found = translator.Translate(); Assert.AreEqual(expecting, found); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
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 found = translator.Translate(); Assert.AreEqual(expecting, found); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
public void TestScopeAndAttributeWithUnderscore() { string action = "$foo_bar::a_b;"; string expecting = "((foo_bar_scope)foo_bar_stack.peek()).a_b;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar t;\n" + "scope foo_bar {\n" + " int a_b;\n" + "}\n" + "a scope foo_bar; : (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 found = translator.Translate(); Assert.AreEqual( expecting, found ); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
public void TestRuleScopeOutsideRule() { string action = "public void foo() {$a::name;}"; string expecting = "public void foo() {((a_scope)a_stack.peek()).name;}"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar t;\n" + "@members {" + action + "}\n" + "a\n" + "scope { String name; }\n" + " : {foo();}\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, null, new CommonToken( ANTLRParser.ACTION, action ), 0 ); string found = translator.Translate(); Assert.AreEqual(expecting, found); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
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 found = translator.Translate(); Assert.AreEqual(expecting, found); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
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 found = translator.Translate(); Assert.AreEqual(expecting, found); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
public void TestStringArgParsing() { string action = "34, '{', \"it's<\", '\"', \"\\\"\", 19"; string expecting = "34, '{', \"it's<\", '\"', \"\\\"\", 19"; 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(); Assert.AreEqual( expecting, rawTranslation ); //IList<String> expectArgs = new List<String>() { // {add("34");} // {add("'{'");} // {add("\"it's<\"");} // {add("'\"'");} // {add("\"\\\"\"");} // that's "\"" // {add("19");} //}; IList<string> expectArgs = new List<string>( new string[] { "34", "'{'", "\"it's<\"", "'\"'", "\"\\\"\"", // that's "\"" "19" } ); List<string> actualArgs = CodeGenerator.GetListOfArgumentsFromAction( action, ',' ); //Assert.AreEqual( "args mismatch", expectArgs, actualArgs ); Assert.IsTrue(expectArgs.SequenceEqual(actualArgs), "args mismatch"); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
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 found = translator.Translate(); Assert.AreEqual(expecting, found); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
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 found = translator.Translate(); Assert.AreEqual(expecting, found); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); translator = new ActionTranslator( generator, "a", new CommonToken( ANTLRParser.ACTION, action2 ), 2 ); found = translator.Translate(); Assert.AreEqual( expecting2, found ); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
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 found = translator.Translate(); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); Assert.AreEqual( expecting, found ); }
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 found = translator.Translate(); Assert.AreEqual(expecting, found); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
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 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 ); }
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 found = translator.Translate(); Assert.AreEqual(expecting, found); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
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 found = translator.Translate(); Assert.AreEqual(expecting, found); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
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 found = translator.Translate(); Assert.AreEqual( expecting, found ); }
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 found = translator.Translate(); Assert.AreEqual(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 ); }
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 ); }
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 found = translator.Translate(); Assert.AreEqual(expecting, found); action = action2; expecting = expecting2; } while ( i++ < 1 ); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
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 found = translator.Translate(); Assert.AreEqual(expecting, found); }
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 found = translator.Translate(); Assert.AreEqual(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 ); }