public void TestBracketArgParsing() { ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); // now check in actual grammar. Grammar g = new Grammar( "parser grammar t;\n" + "a[String[\\] ick, int i]\n" + " : A \n" + " ;" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); // forces load of templates Rule r = g.GetRule( "a" ); AttributeScope parameters = r.ParameterScope; var attrs = parameters.Attributes; Assert.AreEqual("String[] ick", attrs.ElementAt(0).Decl.ToString(), "attribute mismatch"); Assert.AreEqual("ick", attrs.ElementAt(0).Name, "parameter name mismatch"); Assert.AreEqual("String[]", attrs.ElementAt(0).Type, "declarator mismatch"); Assert.AreEqual("int i", attrs.ElementAt(1).Decl.ToString(), "attribute mismatch"); Assert.AreEqual("i", attrs.ElementAt(1).Name, "parameter name mismatch"); Assert.AreEqual("int", attrs.ElementAt(1).Type, "declarator mismatch"); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
public void TestCStyleReturnInitValue() { ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar t;\n" + "a : r ;\n" + "r returns [int (*x)()=NULL] : 'a' ;\n" ); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); Rule r = g.GetRule( "r" ); AttributeScope retScope = r.ReturnScope; var parameters = retScope.Attributes; Assert.IsNotNull(parameters, "missing return action"); Assert.AreEqual( 1, parameters.Count ); string found = parameters.ElementAt( 0 ).ToString(); string expecting = "int (*)() x=NULL"; Assert.AreEqual( expecting, found ); }
public void TestGenericsAsReturnValue() { ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); string grammar = "parser grammar T;\n" + "a returns [HashMap<String,String> foo] : ;\n"; Grammar g = new Grammar( grammar ); Rule ra = g.GetRule( "a" ); var attrs = ra.ReturnScope.Attributes; Assert.AreEqual("HashMap<String,String> foo", attrs.ElementAt(0).Decl.ToString(), "attribute mismatch"); Assert.AreEqual("foo", attrs.ElementAt(0).Name, "parameter name mismatch"); Assert.AreEqual("HashMap<String,String>", attrs.ElementAt(0).Type, "declarator mismatch"); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); }
public void TestMultipleReturnInitValue() { ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar t;\n" + "a : r ;\n" + "r returns [int x=0, int y, String s=new String(\"foo\")] : 'a' {$x = 4;} ;\n" ); Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue); Rule r = g.GetRule( "r" ); AttributeScope retScope = r.ReturnScope; var parameters = retScope.Attributes; Assert.IsNotNull(parameters, "missing return action"); Assert.AreEqual( 3, parameters.Count ); Assert.AreEqual( "int x=0", parameters.ElementAt( 0 ).ToString() ); Assert.AreEqual( "int y", parameters.ElementAt( 1 ).ToString() ); Assert.AreEqual( "String s=new String(\"foo\")", parameters.ElementAt( 2 ).ToString() ); }
public void TestGenericsAsArgumentDefinition2() { string action = "$foo.get(\"ick\"); x=3;"; string expecting = "foo.get(\"ick\"); x=3;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); string grammar = "parser grammar T;\n" + "a[HashMap<String,String> foo, int x, List<String> duh]\n" + " : {" + action + "}\n" + " ;"; Grammar g = new Grammar( grammar ); Rule ra = g.GetRule( "a" ); var attrs = ra.ParameterScope.Attributes; Assert.AreEqual("HashMap<String,String> foo", attrs.ElementAt(0).Decl.ToString().Trim(), "attribute mismatch"); Assert.AreEqual("foo", attrs.ElementAt(0).Name, "parameter name mismatch"); Assert.AreEqual("HashMap<String,String>", attrs.ElementAt(0).Type, "declarator mismatch"); Assert.AreEqual("int x", attrs.ElementAt(1).Decl.ToString().Trim(), "attribute mismatch"); Assert.AreEqual("x", attrs.ElementAt(1).Name, "parameter name mismatch"); Assert.AreEqual("int", attrs.ElementAt(1).Type, "declarator mismatch"); Assert.AreEqual("List<String> duh", attrs.ElementAt(2).Decl.ToString().Trim(), "attribute mismatch"); Assert.AreEqual("duh", attrs.ElementAt(2).Name, "parameter name mismatch"); Assert.AreEqual("List<String>", attrs.ElementAt(2).Type, "declarator mismatch"); 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 TestReturnInitValue() { ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar t;\n" + "a : r ;\n" + "r returns [int x=0] : 'a' {$x = 4;} ;\n" ); assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count ); Rule r = g.GetRule( "r" ); AttributeScope retScope = r.returnScope; var parameters = retScope.Attributes; assertNotNull( "missing return action", parameters ); assertEquals( 1, parameters.Count ); string found = parameters.ElementAt( 0 ).ToString(); string expecting = "int x=0"; assertEquals( expecting, found ); }
public void TestGenericsAsArgumentDefinition() { string action = "$foo.get(\"ick\");"; string expecting = "foo.get(\"ick\");"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); string grammar = "parser grammar T;\n" + "a[HashMap<String,String> foo]\n" + " : {" + action + "}\n" + " ;"; Grammar g = new Grammar( grammar ); Rule ra = g.GetRule( "a" ); var attrs = ra.parameterScope.Attributes; assertEquals( "attribute mismatch", "HashMap<String,String> foo", attrs.ElementAt( 0 ).Decl.ToString() ); assertEquals( "parameter name mismatch", "foo", attrs.ElementAt( 0 ).Name ); assertEquals( "declarator mismatch", "HashMap<String,String>", attrs.ElementAt( 0 ).Type ); 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 ); }
//throws Exception protected void checkSymbols( Grammar g, string rulesStr, string tokensStr ) { var tokens = g.GetTokenDisplayNames(); // make sure expected tokens are there //StringTokenizer st = new StringTokenizer( tokensStr, ", " ); //while ( st.hasMoreTokens() ) foreach ( string tokenName in tokensStr.Split( new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries ) ) { //String tokenName = st.nextToken(); Assert.IsTrue(g.GetTokenType(tokenName) != Label.INVALID, "token " + tokenName + " expected"); tokens.Remove( tokenName ); } // make sure there are not any others (other than <EOF> etc...) foreach ( string tokenName in tokens ) { Assert.IsTrue( g.GetTokenType( tokenName ) < Label.MIN_TOKEN_TYPE, "unexpected token name " + tokenName ); } // make sure all expected rules are there //st = new StringTokenizer( rulesStr, ", " ); int n = 0; //while ( st.hasMoreTokens() ) foreach ( string ruleName in rulesStr.Split( new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries ) ) { //String ruleName = st.nextToken(); Assert.IsNotNull(g.GetRule(ruleName), "rule " + ruleName + " expected"); n++; } var rules = g.Rules; //System.out.println("rules="+rules); // make sure there are no extra rules Assert.AreEqual(n, rules.Count, "number of rules mismatch; expecting " + n + "; found " + rules.Count); }
//throws Exception protected void checkPlusEqualsLabels( Grammar g, string ruleName, string tokenLabelsStr, string ruleLabelsStr ) { // make sure expected += labels are there Rule r = g.GetRule( ruleName ); StringTokenizer st = new StringTokenizer( tokenLabelsStr, ", " ); ICollection<string> tokenLabels = null; while ( st.hasMoreTokens() ) { if ( tokenLabels == null ) { tokenLabels = new List<string>(); } string labelName = st.nextToken(); tokenLabels.Add( labelName ); } ICollection<string> ruleLabels = null; if ( ruleLabelsStr != null ) { st = new StringTokenizer( ruleLabelsStr, ", " ); ruleLabels = new List<string>(); while ( st.hasMoreTokens() ) { string labelName = st.nextToken(); ruleLabels.Add( labelName ); } } Assert.IsTrue((tokenLabels != null && r.TokenListLabels != null) || (tokenLabels == null && r.TokenListLabels == null), "token += labels mismatch; " + tokenLabels + "!=" + r.TokenListLabels); Assert.IsTrue((ruleLabels != null && r.RuleListLabels != null) || (ruleLabels == null && r.RuleListLabels == null), "rule += labels mismatch; " + ruleLabels + "!=" + r.RuleListLabels); if ( tokenLabels != null ) { Assert.IsTrue( tokenLabels.SequenceEqual( r.TokenListLabels.Keys ) ); } if ( ruleLabels != null ) { Assert.IsTrue( ruleLabels.SequenceEqual( r.RuleListLabels.Keys ) ); } }