Java Code Examples for org.antlr.stringtemplate.language.AngleBracketTemplateLexer

The following examples show how to use org.antlr.stringtemplate.language.AngleBracketTemplateLexer. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public void compile() throws IOException{
	String junitFileName;
	if ( grammarInfo.getTreeGrammarName()!=null ) {
		junitFileName = "Test"+grammarInfo.getTreeGrammarName();
	}
	else {
		junitFileName = "Test"+grammarInfo.getGrammarName();
	}
	String lexerName = grammarInfo.getGrammarName()+"Lexer";
	String parserName = grammarInfo.getGrammarName()+"Parser";
	
	StringTemplateGroupLoader loader = new CommonGroupLoader("org/antlr/gunit", null);
	StringTemplateGroup.registerGroupLoader(loader);
	StringTemplateGroup.registerDefaultLexer(AngleBracketTemplateLexer.class);
	StringBuffer buf = compileToBuffer(junitFileName, lexerName, parserName);
	writeTestFile(".", junitFileName+".java", buf.toString());
}
 
Example 2
public void testEscapedLessThanInAction() throws Exception {
	Grammar g = new Grammar();
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	String action = "i<3; '<xmltag>'";
	ActionTranslator translator = new ActionTranslator(generator,"a",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),0);
	String expecting = action;
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, "<action>");
	actionST.setAttribute("action", rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);
}
 
Example 3
public void testEscaped$InAction() throws Exception {
	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" +
		"        ;");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	ActionTranslator translator =
		new ActionTranslator(generator,
								  "a",
								  new antlr.CommonToken(ANTLRParser.ACTION,action),0);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);
}
 
Example 4
public void testFullyQualifiedRefToCurrentRuleParameter() throws Exception {
	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");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	ActionTranslator translator = new ActionTranslator(generator,"a",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example 5
public void testFullyQualifiedRefToCurrentRuleRetVal() throws Exception {
	String action = "$a.i;";
	String expecting = "retval.i;";

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"grammar t;\n"+
		"a returns [int i, int j]: {"+action+"}\n" +
		"  ;\n");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	ActionTranslator translator = new ActionTranslator(generator,"a",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example 6
public void testSetFullyQualifiedRefToCurrentRuleRetVal() throws Exception {
	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");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	ActionTranslator translator = new ActionTranslator(generator,"a",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example 7
public void testTokenRefTreeProperty() throws Exception {
	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");
	Tool antlr = newTool();
	antlr.setOutputDirectory(null); // write to /dev/null
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer();

	ActionTranslator translator = new ActionTranslator(generator,"a",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);
}
 
Example 8
Source Project: quetzal   Source File: StringTemplateTester.java    License: Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) {
       StringTemplate query =
               new StringTemplate("SELECT $column$ FROM $table$;");
       //StringTemplate query2 = new StringTemplate("QS<sql_id> AS ( SELECT <project; separator=\",\"> FROM (VALUES <values:{<it:{(<it; separator=\",\">})}>)");
	String str = "QS<sql_id> AS ( SELECT <project; separator=\",\"> FROM (VALUES <values:{(<it; separator=\",\">)}>))";

       StringTemplate query2 = new StringTemplate(str, AngleBracketTemplateLexer.class);
       query.setAttribute("column", "subject");
       query.setAttribute("table", "emails");
       System.out.println("QUERY: "+query.toString());
       List<List<String>> l = new LinkedList<List<String>>();
       List<String> l1 = new LinkedList<String>();
       List<String> l2 = new LinkedList<String>();
       l1.add("foo");
       l1.add("bar");
       l2.add("foo1");
       l2.add("bar2");
       l.add(l1);
       l.add(l2);
       
       List<String> numLists = new LinkedList<String>();
       numLists.add("x");
       numLists.add("y");
       query2.setAttribute("sql_id", "1");
       query2.setAttribute("values", l);
       query2.setAttribute("project", numLists);
       System.out.println("QUERY: "+query2.toString());
       
       
}
 
Example 9
private StringTemplateGroup getTemplateGroup() {
	StringTemplateGroupLoader loader = new CommonGroupLoader("org/antlr/gunit", null);
	StringTemplateGroup.registerGroupLoader(loader);
	StringTemplateGroup.registerDefaultLexer(AngleBracketTemplateLexer.class);
	StringTemplateGroup group = StringTemplateGroup.loadGroup("gUnitTestResult");
	return group;
}
 
Example 10
public void testArguments() throws Exception {
	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" +
		"        ;");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	ActionTranslator translator = new ActionTranslator(generator,"a",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example 11
public void testGenericsAsArgumentDefinition() throws Exception {
	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");
	List<Attribute> attrs = ra.parameterScope.getAttributes();
	assertEquals("attribute mismatch","HashMap<String,String> foo",attrs.get(0).decl.toString());
	assertEquals("parameter name mismatch","foo",attrs.get(0).name);
	assertEquals("declarator mismatch", "HashMap<String,String>", attrs.get(0).type);

	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	ActionTranslator translator = new ActionTranslator(generator,"a",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example 12
public void testComplicatedArgParsingWithTranslation() throws Exception {
	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" +
		"        ;");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	ActionTranslator translator = new ActionTranslator(generator,"a",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example 13
/** $x.start refs are checked during translation not before so ANTLR misses
 the fact that rule r has refs to predefined attributes if the ref is after
 the def of the method or self-referential.  Actually would be ok if I didn't
 convert actions to strings; keep as templates.
 June 9, 2006: made action translation leave templates not strings
 */
public void testRefToReturnValueBeforeRefToPredefinedAttr() throws Exception {
	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");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	ActionTranslator translator = new ActionTranslator(generator,"a",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example 14
public void testRuleLabelBeforeRefToPredefinedAttr() throws Exception {
	// 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");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	ActionTranslator translator = new ActionTranslator(generator,"a",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example 15
public void testInvalidArguments() throws Exception {
	String action = "$x";
	String expecting = action;

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"parser grammar t;\n"+
		"a[User u, int i]\n" +
		"        : {"+action+"}\n" +
		"        ;");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	ActionTranslator translator = new ActionTranslator(generator,
																 "a",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);

	int expectedMsgID = ErrorManager.MSG_UNKNOWN_SIMPLE_ATTRIBUTE;
	Object expectedArg = "x";
	GrammarSemanticsMessage expectedMessage =
		new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg);
	checkError(equeue, expectedMessage);
}
 
Example 16
public void testReturnValue() throws Exception {
	String action = "$x.i";
	String expecting = "x";

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"grammar t;\n"+
		"a returns [int i]\n" +
		"        : 'a'\n" +
		"        ;\n" +
		"b : x=a {"+action+"} ;\n");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	ActionTranslator translator =
		new ActionTranslator(generator,
								  "b",
								  new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example 17
public void testReturnValueWithNumber() throws Exception {
	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");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	ActionTranslator translator =
		new ActionTranslator(generator,
								  "b",
								  new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example 18
public void testReturnValues() throws Exception {
	String action = "$i; $i.x; $u; $u.x";
	String expecting = "retval.i; retval.i.x; retval.u; retval.u.x";

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"parser grammar t;\n"+
		"a returns [User u, int i]\n" +
		"        : {"+action+"}\n" +
		"        ;");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	ActionTranslator translator = new ActionTranslator(generator,"a",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example 19
public void testInvalidReturnValues() throws Exception {
	String action = "$x";
	String expecting = action;

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"parser grammar t;\n"+
		"a returns [User u, int i]\n" +
		"        : {"+action+"}\n" +
		"        ;");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	ActionTranslator translator = new ActionTranslator(generator,"a",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);

	int expectedMsgID = ErrorManager.MSG_UNKNOWN_SIMPLE_ATTRIBUTE;
	Object expectedArg = "x";
	GrammarSemanticsMessage expectedMessage =
		new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg);
	checkError(equeue, expectedMessage);
}
 
Example 20
public void testTokenLabels() throws Exception {
	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" +
		"  ;");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	ActionTranslator translator = new ActionTranslator(generator,"a",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example 21
public void testInvalidRuleLabelAccessesParameter() throws Exception {
	String action = "$r.z";
	String expecting = action;

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"parser grammar t;\n"+
		"a[int z] returns [int x]\n" +
		"  :\n" +
		"  ;\n"+
		"b : r=a[3] {"+action+"}\n" +
		"  ;");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	ActionTranslator translator = new ActionTranslator(generator, "b",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);

	int expectedMsgID = ErrorManager.MSG_INVALID_RULE_PARAMETER_REF;
	Object expectedArg = "a";
	Object expectedArg2 = "z";
	GrammarSemanticsMessage expectedMessage =
		new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2);
	checkError(equeue, expectedMessage);
}
 
Example 22
public void testInvalidRuleLabelAccessesScopeAttribute() throws Exception {
	String action = "$r.n";
	String expecting = action;

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"parser grammar t;\n"+
		"a\n" +
		"scope { int n; }\n" +
		"  :\n" +
		"  ;\n"+
		"b : r=a[3] {"+action+"}\n" +
		"  ;");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	ActionTranslator translator = new ActionTranslator(generator, "b",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);

	int expectedMsgID = ErrorManager.MSG_INVALID_RULE_SCOPE_ATTRIBUTE_REF;
	Object expectedArg = "a";
	Object expectedArg2 = "n";
	GrammarSemanticsMessage expectedMessage =
		new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2);
	checkError(equeue, expectedMessage);
}
 
Example 23
public void testInvalidRuleAttribute() throws Exception {
	String action = "$r.blort";
	String expecting = action;

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"parser grammar t;\n"+
		"a[int z] returns [int x]\n" +
		"  :\n" +
		"  ;\n"+
		"b : r=a[3] {"+action+"}\n" +
		"  ;");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	ActionTranslator translator = new ActionTranslator(generator, "b",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);

	int expectedMsgID = ErrorManager.MSG_UNKNOWN_RULE_ATTRIBUTE;
	Object expectedArg = "a";
	Object expectedArg2 = "blort";
	GrammarSemanticsMessage expectedMessage =
		new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2);
	checkError(equeue, expectedMessage);
}
 
Example 24
public void testNonDynamicAttributeOutsideRule() throws Exception {
	String action = "public void foo() { $x; }";
	String expecting = action;

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"parser grammar t;\n"+
		"@members {'+action+'}\n" +
		"a : ;\n");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	ActionTranslator translator = new ActionTranslator(generator,
																 null,
																 new antlr.CommonToken(ANTLRParser.ACTION,action),0);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	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";
	GrammarSemanticsMessage expectedMessage =
		new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg);
	checkError(equeue, expectedMessage);
}
 
Example 25
public void testNonDynamicAttributeOutsideRule2() throws Exception {
	String action = "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");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	ActionTranslator translator = new ActionTranslator(generator,
																 null,
																 new antlr.CommonToken(ANTLRParser.ACTION,action),0);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	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);
}
 
Example 26
public void testBasicGlobalScope() throws Exception {
	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");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	ActionTranslator translator = new ActionTranslator(generator,"a",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example 27
public void testIndexedGlobalScope() throws Exception {
	String action = "$Symbols[-1]::names.add($id.text);";
	String expecting =
		"((Symbols_scope)Symbols_stack.elementAt(Symbols_stack.size()-1-1)).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");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	ActionTranslator translator = new ActionTranslator(generator,"a",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example 28
public void test0IndexedGlobalScope() throws Exception {
	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");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	ActionTranslator translator = new ActionTranslator(generator,"a",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	assertEquals(expecting, rawTranslation);

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example 29
public void testAbsoluteIndexedGlobalScope() throws Exception {
	String action = "$Symbols[3]::names.add($id.text);";
	String expecting =
		"((Symbols_scope)Symbols_stack.elementAt(3)).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");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	ActionTranslator translator = new ActionTranslator(generator,"a",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	assertEquals(expecting, rawTranslation);

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example 30
public void testScopeAndAttributeWithUnderscore() throws Exception {
	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");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	ActionTranslator translator = new ActionTranslator(generator,"a",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();
	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();

	assertEquals(expecting, found);

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