Java Code Examples for org.antlr.stringtemplate.StringTemplate#toString()

The following examples show how to use org.antlr.stringtemplate.StringTemplate#toString() . 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 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
public void testRefToTextAttributeForCurrentTreeRule() throws Exception {
	String action = "$text";
	String expecting = "input.getTokenStream().toString(\n" +
					   "              input.getTreeAdaptor().getTokenStartIndex(retval.start),\n" +
					   "              input.getTreeAdaptor().getTokenStopIndex(retval.start))";

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"tree grammar t;\n" +
		"options {ASTLabelType=CommonTree;}\n" +
		"a : {###"+action+"!!!}\n" +
		"  ;\n");

	Tool antlr = newTool();
	antlr.setOutputDirectory(null); // write to /dev/null
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // codegen phase sets some vars we need
	StringTemplate codeST = generator.getRecognizerST();
	String code = codeST.toString();
	String found = code.substring(code.indexOf("###")+3,code.indexOf("!!!"));
	assertEquals(expecting, found);

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example 3
public void testForwardRefRuleLabels() throws Exception {
	String action = "$r.x; $r.start; $r.stop; $r.tree; $a.x; $a.tree;";
	String expecting = "(r!=null?r.x:0); (r!=null?((Token)r.start):null); (r!=null?((Token)r.stop):null); (r!=null?((Object)r.tree):null); (r!=null?r.x:0); (r!=null?((Object)r.tree):null);";

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"parser grammar t;\n"+
		"b : r=a {###"+action+"!!!}\n" +
		"  ;\n" +
		"a returns [int x]\n" +
		"  : ;\n");
	Tool antlr = newTool();
	antlr.setOutputDirectory(null); // write to /dev/null
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // codegen phase sets some vars we need

	StringTemplate codeST = generator.getRecognizerST();
	String code = codeST.toString();
	String found = code.substring(code.indexOf("###")+3,code.indexOf("!!!"));
	assertEquals(expecting, found);

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example 4
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 5
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 6
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 7
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 8
Source Project: quetzal   File: Expression.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String visit(Store store, FilterContext context,
		Expression exp) {
	Iterator<Expression> iter = ((BuiltinFunctionExpression) exp)
			.getArguments().iterator();
	Expression e = iter.next();
	StringTemplate t = store.getInstanceOf("RDF_LANGMATCHES");

	if (e instanceof BuiltinFunctionExpression) {
		String fTerm = e.visit(context, store);
		t.setAttribute("ftype", fTerm);
	}
	e = iter.next();
	if (e instanceof ConstantExpression) {
		String constantData = ((ConstantExpression) e)
				.getConstant().toDataString();

		StringBuffer inClause = new StringBuffer();
		inClause.append("SELECT LOWER(");
		inClause.append(Constants.NAME_COLUMN_DATATYPE_NAME);
		inClause.append(") FROM ");
		inClause.append(store.getDataTypeTable());
		inClause.append(" WHERE ");
		inClause.append(Constants.NAME_COLUMN_DATATYPE_ID);
		inClause.append(" BETWEEN ");
		inClause.append(TypeMap.LANG_IDS_START);
		inClause.append(" AND ");
		inClause.append(TypeMap.LANG_IDS_END);
		if (!constantData.equals("*")) {
			inClause.append(" AND ");
			inClause.append(Constants.NAME_COLUMN_DATATYPE_NAME);
			inClause.append(" LIKE '");
			inClause.append(constantData.toUpperCase());
			inClause.append("%'");
		}
		t.setAttribute("rstart", inClause);
	}
	return t.toString();
}
 
Example 9
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());
}
 
Example 10
public void testTreeRuleStopAttributeIsInvalid() throws Exception {
	String action = "$r.x; $r.start; $r.stop";
	String expecting = "(r!=null?r.x:0); (r!=null?((CommonTree)r.start):null); $r.stop";

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"tree grammar t;\n" +
		"options {ASTLabelType=CommonTree;}\n"+
		"a returns [int x]\n" +
		"  :\n" +
		"  ;\n"+
		"b : r=a {###"+action+"!!!}\n" +
		"  ;");
	Tool antlr = newTool();
	antlr.setOutputDirectory(null); // write to /dev/null
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // codegen phase sets some vars we need
	StringTemplate codeST = generator.getRecognizerST();
	String code = codeST.toString();
	String found = code.substring(code.indexOf("###")+3,code.indexOf("!!!"));
	assertEquals(expecting, found);

	int expectedMsgID = ErrorManager.MSG_UNKNOWN_RULE_ATTRIBUTE;
	Object expectedArg = "a";
	Object expectedArg2 = "stop";
	GrammarSemanticsMessage expectedMessage =
		new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2);
	System.out.println("equeue:"+equeue);
	checkError(equeue, expectedMessage);
}
 
Example 11
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 12
public void testSharedGlobalScope() throws Exception {
	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");
	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
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 14
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 15
public void testSettingLexerRulePropertyRefs() throws Exception {
	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");
	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 16
public String wrapNumericsInCase(Expression expression, String condition, FilterContext context) {
	Map<String, Pair<String, String>> varMap = context.getVarMap();
	
	List<String> types = getNumericTypeCheckSQL(expression, varMap);
	StringTemplate t = getInstanceOf(NUMERICS_CASE);
	t.setAttribute("types", types);
	t.setAttribute("condition", condition);
	return t.toString();
}
 
Example 17
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 18
Source Project: quetzal   File: UnaryExpression.java    License: Eclipse Public License 2.0 4 votes vote down vote up
@Override
public String visit(FilterContext context, Store store) {
	String s = getExpression().visit(context, store);
	if (s.equals(""))
		return s;

	StringTemplate t = null;
	if (getOperator() == EUnaryOp.MINUS) {
		t = store.getInstanceOf(UNARY_MINUS);
	} else if ((getExpression() instanceof VariableExpression && ((VariableExpression) getExpression())
			.getExpression() == null)) {
		t = store.getInstanceOf(EBV);
		String fType = null;
		String fTerm = null;
		for (Variable v : getExpression().gatherVariables()) {
			fType = context.getVarMap().get(v.getName()).snd;
			fTerm = context.getVarMap().get(v.getName()).fst;
		}
		if (fType == null)
			fType = TypeMap.IRI_ID + "";
		t.setAttribute("fterm", fTerm);
		t.setAttribute("ftype", fType);
		t.setAttribute("nrstart", TypeMap.DATATYPE_NUMERICS_IDS_START);
		t.setAttribute("nrend", TypeMap.DATATYPE_NUMERICS_IDS_END);
		t.setAttribute("tstring", TypeMap.STRING_ID);
		t.setAttribute("pstring", TypeMap.SIMPLE_LITERAL_ID);
		t.setAttribute("tboolean", TypeMap.BOOLEAN_ID);
		s = t.toString();

		if (getOperator() == EUnaryOp.NOT) {
			t = store.getInstanceOf(NOT_EBV);
			t.setAttribute("ebv", s);
			t.setAttribute("type", fType);
			t.setAttribute("unknownTypesStart", TypeMap.USER_ID_START);
			t.setAttribute("unknownTypesEnd", TypeMap.NONE_ID);
			return t.toString();
		}
	}

	if (getOperator() == EUnaryOp.NOT) {
		t = store.getInstanceOf(UNARY_NOT);
	}

	t.setAttribute("expression", s);

	return t.toString();
}
 
Example 19
public void testReturnWithMultipleRuleRefs() throws Exception {
	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"+
		";");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates
	int i = 0;
	String action = action1;
	String expecting = expecting1;
	do {
		ActionTranslator translator = new ActionTranslator(generator,"rule1",
																	 new antlr.CommonToken(ANTLRParser.ACTION,action),i+1);
		String rawTranslation =
				translator.translate();
		StringTemplateGroup templates =
				new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
		StringTemplate actionST = new StringTemplate(templates, rawTranslation);
		String found = actionST.toString();
		assertEquals(expecting, found);
		action = action2;
		expecting = expecting2;
	} while (i++ < 1);
	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example 20
@Override
public String visit(FilterContext context, Store store) {
	StringTemplate t;
	String s;
	List<String> l = new ArrayList<String>();

	for (Expression e : getComponents()) {
		s = e.visit(context, store);
		if (s.equals(""))
			continue;
		// Mihaela: ToAsk why only var exp where the exp is null become EBV?
		if ((e instanceof ConstantExpression)
				|| (e instanceof VariableExpression && ((VariableExpression) e)
						.getExpression() == null)) {
			t = store.getInstanceOf("RDF_EBV");

			String fTerm = null;
			String fType = null;

			s = e.visit(context, store);
			fTerm = s;
			Set<Variable> variables = e.gatherVariables();
			assert (variables.size() < 2);
			if (variables.size() == 0) {
				short constType = e.getReturnType();
				String constValue = ((ConstantExpression) e).getConstant()
						.toDataString();
				fTerm = "'" + getSID(constValue, store.getMaxStringLen())
						+ "'";
				if (constType >= TypeMap.DATATYPE_NUMERICS_IDS_START
						&& constType <= TypeMap.DATATYPE_NUMERICS_IDS_END) {
					// cast((value as decfloat) <> 0)
					fTerm = "CAST('" + constValue + "' AS "
							+ store.getDatatype("NUMERIC") + ")";
					t = store.getInstanceOf("RDF_NE");
					t.setAttribute("left", fTerm);
					t.setAttribute("right", 0);
				} else if (constType == TypeMap.BOOLEAN_ID) {
					// value=true
					t = store.getInstanceOf("RDF_EQ");
					t.setAttribute("left", fTerm);
					t.setAttribute("right", "'true'");
				} else {
					// value <> "";
					t = store.getInstanceOf("RDF_NE");
					t.setAttribute("left", fTerm);
					t.setAttribute("right", "''");
				}
			} else {
				for (Variable v : variables) {
					fType = context.getVarMap().get(v.getName()).snd;
					fTerm = context.getVarMap().get(v.getName()).fst;
				}
				if (fType == null)
					fType = TypeMap.IRI_ID + "";
				t.setAttribute("fterm", fTerm);
				t.setAttribute("ftype", fType);
				t.setAttribute("nrstart",
						TypeMap.DATATYPE_NUMERICS_IDS_START);
				t.setAttribute("nrend", TypeMap.DATATYPE_NUMERICS_IDS_END);
				t.setAttribute("tstring", TypeMap.STRING_ID);
				t.setAttribute("pstring", TypeMap.SIMPLE_LITERAL_ID);
				t.setAttribute("tboolean", TypeMap.BOOLEAN_ID);
			}

			s = t.toString();
		}
		l.add(s);
	}

	if (getType() == Expression.EExpressionType.OR) {
		t = store.getInstanceOf("logical_or_expressions");
	} else {
		t = store.getInstanceOf("logical_or_expressions");
	}
	t.setAttribute("expressions", l);

	return t.toString();
}