org.antlr.stringtemplate.StringTemplateGroup Java Examples

The following examples show how to use org.antlr.stringtemplate.StringTemplateGroup. 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: 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 #3
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 #4
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 #5
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 #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: 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 #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: StringTemplateHelper.java    From attic-aurora with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new template helper.
 *
 * @param templateContextClass Classpath context for the location of the template file.
 * @param templateName Template file name (excluding .st suffix) relative to
 *     {@code templateContextClass}.
 * @param cacheTemplates Whether the template should be cached.
 */
public StringTemplateHelper(
    Class<?> templateContextClass,
    String templateName,
    boolean cacheTemplates) {

  MorePreconditions.checkNotBlank(templateName);
  String templatePath =
      templateContextClass.getPackage().getName().replace('.', '/') + "/" + templateName;
  StringTemplateGroup group = new StringTemplateGroup(templateName);
  Preconditions.checkNotNull(group.getInstanceOf(templatePath),
      "Failed to load template at: %s", templatePath);

  this.group = group;
  if (!cacheTemplates) {
    group.setRefreshInterval(0);
  }
  this.templatePath = templatePath;
}
 
Example #10
Source File: ProtoToJavaBeanPrimitiveCompiler.java    From protostuff with Apache License 2.0 6 votes vote down vote up
@Override
public void compile(ProtoModule module, Proto proto) throws IOException
{
    String javaPackageName = proto.getJavaPackageName();
    StringTemplateGroup group = getSTG("java_bean_primitives");

    // this compiler doesnt allow setters
    module.getOptions().setProperty("no_setters", "true");

    // TODO find a way to push this up to the parsing step
    if (module.getOption("ByteBuffer") != null)
    {
        for (Message m : proto.getMessages())
        {
            m.setByteBufferFieldPresent(true);

            setByteBuffer(m);
        }
    }

    writeEnums(module, proto, javaPackageName, group);
    writeMessages(module, proto, javaPackageName, group);
}
 
Example #11
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 #12
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 #13
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 #14
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testAssignToOwnRulenameAttr() throws Exception {
	String action = "$rule.tree = null;";
	String expecting = "retval.tree = 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();
	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
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testRefToRuleRefInLexer() throws Exception {
	String action = "$ID.text";
	String expecting = "(ID1!=null?ID1.getText():null)";
	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"R : 'z' 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,
								  "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
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testRefToRuleRefInLexerNoAttribute() throws Exception {
	String action = "$ID";
	String expecting = "ID1";
	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"R : 'z' 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,
								  "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 #17
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 #18
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 #19
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 #20
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testFullyQualifiedRefToTemplateAttributeInCurrentRule() throws Exception {
	String action = "$a.st;"; // can be qualified
	String expecting = "retval.st;";

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"parser grammar t;\n" +
		"options {output=template;}\n"+
		"a : (A->{$A.text}) {"+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 #21
Source File: PluginProtoCompiler.java    From protostuff with Apache License 2.0 5 votes vote down vote up
/**
 * Returns null if template is not found.
 */
public static StringTemplate getTemplateFrom(StringTemplateGroup group,
        String template)
{
    try
    {
        return group.lookupTemplate(template);
    }
    catch (IllegalArgumentException e)
    {
        return null;
    }
}
 
Example #22
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 #23
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testIsolatedGlobalScopeRef() throws Exception {
	String action = "$Symbols;";
	String expecting = "Symbols_stack;";

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

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"grammar t;\n"+
		"a\n" +
		"scope {\n" +
		"  float n;\n" +
		"} : b ;\n" +
		"b : {"+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, "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 #25
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testIsolatedDynamicRuleScopeRef() throws Exception {
	String action = "$a;"; // refers to stack not top of stack
	String expecting = "a_stack;";

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"grammar t;\n"+
		"a\n" +
		"scope {\n" +
		"  int n;\n" +
		"} : b ;\n" +
		"b : {"+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, "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 #26
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testBasicRuleScope() throws Exception {
	String action = "$a::n;";
	String expecting = "((a_scope)a_stack.peek()).n;";

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

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example #27
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testRuleScopeOutsideRule() throws Exception {
	String action = "public void foo() {$a::name;}";
	String expecting = "public void foo() {((a_scope)a_stack.peek()).name;}";

	ErrorQueue equeue = new ErrorQueue();
	ErrorManager.setErrorListener(equeue);
	Grammar g = new Grammar(
		"grammar t;\n"+
		"@members {"+action+"}\n" +
		"a\n" +
		"scope { String name; }\n" +
		"  : {foo();}\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,
																 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);

	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
}
 
Example #28
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 #29
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 #30
Source File: TestAttributes.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
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());
}