org.antlr.stringtemplate.language.AngleBracketTemplateLexer Java Examples

The following examples show how to use org.antlr.stringtemplate.language.AngleBracketTemplateLexer. 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
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
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 #2
Source File: JUnitCodeGen.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
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 #3
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
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 #4
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
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 #5
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
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 #6
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
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 #7
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
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 File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testReuseExistingLabelWithImplicitTokenLabel() throws Exception {
	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");
	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);

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example #9
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testIllegalAssignToLocalAttr() throws Exception {
	String action = "$tree = null; $st = null; $start = 0; $stop = 0; $text = 0;";
	String expecting = "retval.tree = null; retval.st = null;   ";
	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"grammar a;\n" +
		"rule\n" +
		"    : 'y' {" + action +"}\n" +
		"    ;");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	ActionTranslator translator = new ActionTranslator(generator,
																 "rule",
																 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	String rawTranslation =
		translator.translate();

	int expectedMsgID = ErrorManager.MSG_WRITE_TO_READONLY_ATTR;
	ArrayList expectedErrors = new ArrayList(3);
	GrammarSemanticsMessage expectedMessage =
		new GrammarSemanticsMessage(expectedMsgID, g, null, "start", "");
	expectedErrors.add(expectedMessage);
	GrammarSemanticsMessage expectedMessage2 =
		new GrammarSemanticsMessage(expectedMsgID, g, null, "stop", "");
	expectedErrors.add(expectedMessage2);
			GrammarSemanticsMessage expectedMessage3 =
		new GrammarSemanticsMessage(expectedMsgID, g, null, "text", "");
	expectedErrors.add(expectedMessage3);
	checkErrors(equeue, expectedErrors);

	StringTemplateGroup templates =
		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
	String found = actionST.toString();
	assertEquals(expecting, found);
}
 
Example #10
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testReuseExistingListLabelWithImplicitTokenLabel() throws Exception {
	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");
	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);

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example #11
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testLabelOnRuleRefInLexer() throws Exception {
	String action = "$i.text";
	String expecting = "(i!=null?i.getText():null)";
	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"R : 'z' i=ID {"+action+"};" +
		"fragment 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,
								  "R",
								  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
Source File: TestTemplates.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
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");
	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();

	assertNoErrors(equeue);

	assertEquals(expecting, found);
}
 
Example #13
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testAssignToTreeNodeAttribute() throws Exception {
	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" +
		";");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	ActionTranslator translator = new ActionTranslator(generator,
																 "rule",
																 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("unexpected errors: "+equeue, 0, equeue.errors.size());
	assertEquals(expecting, found);
}
 
Example #14
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testDoNotTranslateAttributeCompare() throws Exception {
	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';"
	);
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer();
	ActionTranslator translator = new ActionTranslator(generator,
																 "RULE",
																 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("unexpected errors: "+equeue, 0, equeue.errors.size());
	assertEquals(expecting, found);
}
 
Example #15
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testTypeOfGuardedAttributeRefIsCorrect() throws Exception {
	String action = "int x = $b::n;";
	String expecting = "int x = ((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");
	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 #16
Source File: TestTemplates.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
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");
	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();

	assertNoErrors(equeue);

	assertEquals(expecting, found);
}
 
Example #17
Source File: TestTemplates.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testStringConstructor() throws Exception {
	String action = "x = %{$ID.text};";
	String expecting = "x = new StringTemplate(templateLib,(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");
	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();

	assertNoErrors(equeue);

	assertEquals(expecting, found);
}
 
Example #18
Source File: TestTemplates.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
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");
	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();

	assertNoErrors(equeue);

	assertEquals(expecting, found);
}
 
Example #19
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testLexerRulePropertyRefs() throws Exception {
	String action = "$text $type $line $pos $channel $index $start $stop";
	String expecting = "getText() _type state.tokenStartLine state.tokenStartCharPositionInLine _channel -1 state.tokenStartCharIndex (getCharIndex()-1)";
	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"R : 'r' {"+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,
								  "R",
								  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 #20
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testImplicitTokenLabel() throws Exception {
	String action = "$ID; $ID.text; $ID.getText()";
	String expecting = "ID1; (ID1!=null?ID1.getText():null); ID1.getText()";

	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");

	ActionTranslator translator =
		new ActionTranslator(generator,
								  "a",
								  new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	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
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testPlusEqualWildcardLabel() throws Exception {
	String action = "$ids.size();"; // must be qualified
	String expecting = "list_ids.size();";

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"grammar t;\n"+
		"a : ids+=. ( ',' ids+=ID {"+action+"})* ;" +
		"ID : 'a';\n");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	ActionTranslator translator =
		new ActionTranslator(generator,
								  "a",
								  new antlr.CommonToken(ANTLRParser.ACTION,action),1);
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	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 #22
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testPlusEqualSetLabel() throws Exception {
	String action = "$ids.size();"; // must be qualified
	String expecting = "list_ids.size();";

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"grammar t;\n"+
		"a : ids+=('a'|'b') ( ',' ids+=ID {"+action+"})* ;" +
		"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 #23
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testPlusEqualStringLabel() throws Exception {
	String action = "$ids.size();"; // must be qualified
	String expecting = "list_ids.size();";

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"grammar t;\n"+
		"a : ids+='if' ( ',' ids+=ID {"+action+"})* ;" +
		"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 #24
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testSimplePlusEqualLabel() throws Exception {
	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");
	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 #25
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testUnqualifiedRuleScopeAttribute() throws Exception {
	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");
	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_SIMPLE_ATTRIBUTE;
	Object expectedArg = "n";
	Object expectedArg2 = null;
	GrammarSemanticsMessage expectedMessage =
		new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2);
	checkError(equeue, expectedMessage);
}
 
Example #26
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testUnknownGlobalDynamicAttribute() throws Exception {
	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");
	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);

	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);
}
 
Example #27
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testUnknownDynamicAttribute() throws Exception {
	String action = "$a::x";
	String expecting = action;

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"grammar t;\n"+
		"a\n" +
		"scope {\n" +
		"  int n;\n" +
		"} : {"+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);

	int expectedMsgID = ErrorManager.MSG_UNKNOWN_DYNAMIC_SCOPE_ATTRIBUTE;
	Object expectedArg = "a";
	Object expectedArg2 = "x";
	GrammarSemanticsMessage expectedMessage =
		new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2);
	checkError(equeue, expectedMessage);
}
 
Example #28
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testRefToTextAttributeForCurrentRule() throws Exception {
	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");
	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 #29
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testRefToTemplateAttributeForCurrentRule() throws Exception {
	String action = "$st=null;";
	String expecting = "retval.st =null;";

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"parser grammar t;\n" +
		"options {output=template;}\n"+
		"a : {"+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 #30
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testDynamicScopeRefOkEvenThoughRuleRefExists() throws Exception {
	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");
	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());
}