org.antlr.stringtemplate.StringTemplate Java Examples

The following examples show how to use org.antlr.stringtemplate.StringTemplate. 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: ProtoToJavaBeanCompiler.java    From protostuff with Apache License 2.0 7 votes vote down vote up
protected void writeMessages(ProtoModule module, Proto proto, String javaPackageName, StringTemplateGroup group)
        throws IOException
{
    for (Message m : proto.getMessages())
    {
        Writer writer = CompilerUtil.newWriter(module,
                javaPackageName, m.getName() + ".java");
        AutoIndentWriter out = new AutoIndentWriter(writer);

        StringTemplate messageBlock = group.getInstanceOf("message_block");
        messageBlock.setAttribute("message", m);
        messageBlock.setAttribute("module", module);
        messageBlock.setAttribute("options", module.getOptions());

        messageBlock.write(out);
        writer.close();
    }
}
 
Example #2
Source File: IForeach.java    From swift-k with Apache License 2.0 6 votes vote down vote up
@Override
protected void setTemplateAttributes(OutputContext oc, StringTemplate st) {
    Collection<String> cleanups = getScope().getCleanups();
    if (cleanups != null) {
        cleanups.remove(varName);
        if (indexVarName != null) {
            cleanups.remove(indexVarName);
        }
    }
    super.setTemplateAttributes(oc, st);
    st.setAttribute("var", varName);
    st.setAttribute("in", in.getTemplate(oc));
    if (indexVarName != null) {
        st.setAttribute("indexVar", indexVarName);
        st.setAttribute("indexVarField", indexVarFieldName);
    }
    if (selfClose) {
        st.setAttribute("selfClose", "true");
    }
    setPartials(st, wrefs, "wrefs");
    mergeRefs(rrefs, wrefs);
    setPartials(st, rrefs, "rrefs");
}
 
Example #3
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testRefToStartAttributeForCurrentRule() throws Exception {
	String action = "$start;";
	String expecting = "((Token)retval.start);";

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"parser grammar t;\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);
	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
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testRuleRefWhenRuleHasScope() throws Exception {
	String action = "$b.start;";
	String expecting = "(b1!=null?((Token)b1.start):null);";

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"grammar t;\n" +
		"a : b {###"+action+"!!!} ;\n" +
		"b\n" +
		"scope {\n" +
		"  int n;\n" +
		"} : 'b' \n" +
		"  ;\n");
	Tool antlr = newTool();
	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
	g.setCodeGenerator(generator);
	generator.genRecognizer(); // forces load of templates

	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 #5
Source File: DOTGenerator.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
/** Return a String containing a DOT description that, when displayed,
   *  will show the incoming state machine visually.  All nodes reachable
   *  from startState will be included.
   */
  public String getDOT(State startState) {
if ( startState==null ) {
	return null;
}
// The output DOT graph for visualization
StringTemplate dot = null;
markedStates = new HashSet();
      if ( startState instanceof DFAState ) {
          dot = stlib.getInstanceOf("org/antlr/tool/templates/dot/dfa");
	dot.setAttribute("startState",
			Utils.integer(startState.stateNumber));
	dot.setAttribute("useBox",
					 Boolean.valueOf(Tool.internalOption_ShowNFAConfigsInDFA));
	walkCreatingDFADOT(dot, (DFAState)startState);
      }
      else {
          dot = stlib.getInstanceOf("org/antlr/tool/templates/dot/nfa");
	dot.setAttribute("startState",
			Utils.integer(startState.stateNumber));
	walkRuleNFACreatingDOT(dot, startState);
      }
dot.setAttribute("rankdir", rankdir);
      return dot.toString();
  }
 
Example #6
Source File: CTarget.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected void genRecognizerHeaderFile(Tool tool,
									   CodeGenerator generator,
									   Grammar grammar,
									   StringTemplate headerFileST,
									   String extName)
	throws IOException
{
           // Pick up the file name we are generating. This method will return a 
           // a file suffixed with .c, so we must substring and add the extName
           // to it as we cannot assign into strings in Java.
           ///
           String fileName = generator.getRecognizerFileName(grammar.name, grammar.type);
           fileName = fileName.substring(0, fileName.length()-2) + extName;
           
           System.out.println("Generating " + fileName);
           generator.write(headerFileST, fileName);
}
 
Example #7
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
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 #8
Source File: MutationHtmlReportListener.java    From pitest with Apache License 2.0 6 votes vote down vote up
private void createPackageIndexPage(final PackageSummaryData psData) {
  final StringTemplateGroup group = new StringTemplateGroup("mutation_test");
  final StringTemplate st = group
      .getInstanceOf("templates/mutation/package_index");

  final Writer writer = this.outputStrategy.createWriterForFile(psData
      .getPackageDirectory() + File.separator + "index.html");
  st.setAttribute("packageData", psData);
  try {
    writer.write(st.toString());
    writer.close();
  } catch (final IOException e) {
    e.printStackTrace();
  }

}
 
Example #9
Source File: NonRegularDecisionMessage.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public String toString() {
	GrammarAST decisionASTNode = probe.dfa.getDecisionASTNode();
	line = decisionASTNode.getLine();
	column = decisionASTNode.getColumn();
	String fileName = probe.dfa.nfa.grammar.getFileName();
	if ( fileName!=null ) {
		file = fileName;
	}

	StringTemplate st = getMessageTemplate();
	String ruleName = probe.dfa.getNFADecisionStartState().enclosingRule.name;
	st.setAttribute("ruleName", ruleName);
	List sortedAlts = new ArrayList();
	sortedAlts.addAll(altsWithRecursion);
	Collections.sort(sortedAlts); // make sure it's 1, 2, ...
	st.setAttribute("alts", sortedAlts);

	return super.toString(st);
}
 
Example #10
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 #11
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 #12
Source File: GrammarSyntaxMessage.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public String toString() {
	line = 0;
	column = 0;
	if ( offendingToken!=null ) {
		line = offendingToken.getLine();
		column = offendingToken.getColumn();
	}
	// TODO: actually set the right Grammar instance to get the filename
	// TODO: have to update all v2 grammar files for this. or use errormanager and tool to get the current grammar
	if (g != null) {
		file = g.getFileName();
	}
	StringTemplate st = getMessageTemplate();
	if ( arg!=null ) {
		st.setAttribute("arg", arg);
	}
	return super.toString(st);
}
 
Example #13
Source File: ProtoToJavaV2ProtocSchemaCompiler.java    From protostuff with Apache License 2.0 6 votes vote down vote up
@Override
protected void compile(ProtoModule module, Proto proto) throws IOException
{
    String javaPackageName = proto.getJavaPackageName();
    StringTemplateGroup group = getSTG(getOutputId());

    String fileName = resolveFileName(proto);
    Writer writer = CompilerUtil.newWriter(module, javaPackageName,
            "Schema" + fileName + ".java");

    AutoIndentWriter out = new AutoIndentWriter(writer);
    StringTemplate protoOuterBlock = group.getInstanceOf("proto_block");

    protoOuterBlock.setAttribute("proto", proto);
    protoOuterBlock.setAttribute("module", module);
    protoOuterBlock.setAttribute("options", module.getOptions());

    protoOuterBlock.write(out);
    writer.close();
}
 
Example #14
Source File: IConditionBranch.java    From swift-k with Apache License 2.0 6 votes vote down vote up
private void addPre(OutputContext oc, StringTemplate st, Map<String, Integer> counts, String templateName) {
    if (counts != null) {
        boolean any = false;
        StringTemplate pre = oc.template(templateName);
        for (Map.Entry<String, Integer> e : counts.entrySet()) {
            if (e.getValue() > 0) {
                pre.setAttribute("items", e.getKey());
                pre.setAttribute("items", e.getValue());
                any = true;
            }
        }
        if (any) {
            st.setAttribute("statements", pre);
        }
    }
}
 
Example #15
Source File: ProtoToJavaBeanCompiler.java    From protostuff with Apache License 2.0 6 votes vote down vote up
protected void writeEnums(ProtoModule module, Proto proto, String javaPackageName, StringTemplateGroup group)
        throws IOException
{
    for (EnumGroup eg : proto.getEnumGroups())
    {
        Writer writer = CompilerUtil.newWriter(module,
                javaPackageName, eg.getName() + ".java");
        AutoIndentWriter out = new AutoIndentWriter(writer);

        StringTemplate enumBlock = group.getInstanceOf("enum_block");
        enumBlock.setAttribute("eg", eg);
        enumBlock.setAttribute("module", module);
        enumBlock.setAttribute("options", module.getOptions());

        enumBlock.write(out);
        writer.close();
    }
}
 
Example #16
Source File: SPARQLToSQLExpression.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
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
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testTokenLabelTreeProperty() throws Exception {
	String action = "$id.tree;";
	String expecting = "id_tree;";

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"grammar t;\n"+
		"a : id=ID {"+action+"} ;\n" +
		"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 #18
Source File: ProtoToProtoCompiler.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public static String extendBy(StringTemplateGroup group, Message extend, Message by) throws IOException
{
    StringWriter stringer = new StringWriter(16);
    NoIndentWriter out = new NoIndentWriter(stringer);

    StringTemplate messageBlock = group.getInstanceOf("extend_by");
    messageBlock.setAttribute("message", extend);
    messageBlock.setAttribute("by", by);
    messageBlock.write(out);

    return stringer.toString();
}
 
Example #19
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 #20
Source File: AbstractINode.java    From swift-k with Apache License 2.0 5 votes vote down vote up
protected void setAllStr(OutputContext oc, StringTemplate st, List<String> l, String attrName) {
    if (l != null) {
        for (String s : l) {
            st.setAttribute(attrName, s);
        }
    }
}
 
Example #21
Source File: AbstractINode.java    From swift-k with Apache License 2.0 5 votes vote down vote up
protected void setAll(OutputContext oc, StringTemplate st, List<? extends INode> l, String attrName) {
    if (l != null) {
        for (INode n : l) {
            st.setAttribute(attrName, n.getTemplate(oc));
        }
    }
}
 
Example #22
Source File: PluginProtoCompiler.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public static void compileMessageBlockTo(Writer writer,
        ProtoModule module, Message message,
        StringTemplate messageBlockTemplate) throws IOException
{
    AutoIndentWriter out = new AutoIndentWriter(writer);

    StringTemplate messageBlock = messageBlockTemplate.getInstanceOf();
    messageBlock.setAttribute("message", message);
    messageBlock.setAttribute("module", module);
    messageBlock.setAttribute("options", module.getOptions());

    messageBlock.write(out);
}
 
Example #23
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 #24
Source File: WikiResultRenderer.java    From yatspec with Apache License 2.0 5 votes vote down vote up
public String render(Result result) throws Exception {
    final EnhancedStringTemplateGroup group = new EnhancedStringTemplateGroup(getClass());
    sequence(customRenderers).fold(group, registerRenderer());
    group.registerRenderer(instanceOf(JavaSource.class), callable(new JavaSourceRenderer()));
    group.registerRenderer(instanceOf(Notes.class), callable(new NotesRenderer()));
    final StringTemplate template = group.getInstanceOf("wiki");
    template.setAttribute("testResult", result);
    StringWriter writer = new StringWriter();
    template.write(new NoIndentWriter(writer));
    return writer.toString();
}
 
Example #25
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
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
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
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
Source File: IFormalParameter.java    From swift-k with Apache License 2.0 5 votes vote down vote up
@Override
protected void setTemplateAttributes(OutputContext oc, StringTemplate st) {
    super.setTemplateAttributes(oc, st);
    st.setAttribute("name", name);
    //st.setAttribute("cleanCount", 0);
    StringTemplate typest = new StringTemplate("type");
    typest.setAttribute("name", type.getType().toString());
    st.setAttribute("type", typest);
    if (defaultValue != null) {
        st.setAttribute("default", defaultValue.getTemplate(oc));
    }
}
 
Example #28
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testLexerLabelRefs() throws Exception {
	String action = "$a $b.text $c $d.text";
	String expecting = "a (b!=null?b.getText():null) c (d!=null?d.getText():null)";
	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"R : a='c' b='hi' c=. d=DUH {"+action+"};\n" +
		"DUH : 'd' ;\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 #29
Source File: Expression.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public String visit(Store store, FilterContext context, Expression exp,
		String type) {
	Iterator<Expression> iter = ((BuiltinFunctionExpression) exp)
			.getArguments().iterator();
	List<String> args = new LinkedList<String>();
	while (iter.hasNext()) {
		String s = iter.next().visit(context, store);
		if (!s.equals("")) {
			args.add(s);
		}
	}
	StringTemplate t = store.getInstanceOf(type);
	t.setAttribute("args", args);
	return t.toString();
}
 
Example #30
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
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);
}