Java Code Examples for org.stringtemplate.v4.STGroup

The following examples show how to use org.stringtemplate.v4.STGroup. 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
Source Project: bookish   Source File: Tool.java    License: MIT License 6 votes vote down vote up
public List<ST> getSnippetTemplates(STGroup pycodeTemplates, List<ExecutableCodeDef> defs) {
	List<ST> snippets = new ArrayList<>();
	for (ExecutableCodeDef def : defs) {
		String tname = def.isOutputVisible ? "pyeval" : "pyfig";
		ST snippet = pycodeTemplates.getInstanceOf(tname);
		snippet.add("def",def);
		// Don't allow "plt.show()" to execute, strip it
		String code = null;
		if ( def.code!=null ) {
			code = def.code.replace("plt.show()", "");
		}
		if ( code!=null && code.trim().length()==0 ) {
			code = null;
		}
		snippet.add("code", code);
		snippets.add(snippet);
	}
	return snippets;
}
 
Example 2
Source Project: protostuff-compiler   Source File: ExtensibleStCompiler.java    License: Apache License 2.0 6 votes vote down vote up
private void addPropertyExtensions(STGroup group, ExtensionProvider extensionProvider) {
    Map<Class<?>, PropertyProvider> extenderMap = extensionProvider.propertyProviders();
    for (Map.Entry<Class<?>, PropertyProvider> entry : extenderMap.entrySet()) {
        Class<?> objectClass = entry.getKey();
        PropertyProvider extender = entry.getValue();
        group.registerModelAdaptor(objectClass, new ObjectModelAdaptor() {
            @Override
            public synchronized Object getProperty(Interpreter interp, ST self, Object o, Object property, String propertyName) {
                if (extender.hasProperty(propertyName)) {
                    return extender.getProperty(o, propertyName);
                }
                return super.getProperty(interp, self, o, property, propertyName);
            }
        });
    }
}
 
Example 3
Source Project: codebuff   Source File: MapModelAdaptor.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Object getProperty(Interpreter interp, ST self, Object o, Object property, String propertyName) throws STNoSuchPropertyException {
    Object value;
    Map<?, ?> map = (Map<?, ?>)o;
    if ( property==null ) value = map.get(STGroup.DEFAULT_KEY);
    else if ( property.equals("keys") ) value = map.keySet();
    else if ( property.equals("values") ) value = map.values();
    else if ( map.containsKey(property) ) value = map.get(property);
    else if ( map.containsKey(propertyName) ) { // if can't find the key, try toString version
        value = map.get(propertyName);
         }
    else value = map.get(STGroup.DEFAULT_KEY); // not found, use default
    if ( value==STGroup.DICT_KEY ) {
        value = property;
    }
    return value;
}
 
Example 4
Source Project: codebuff   Source File: MapModelAdaptor.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Object getProperty(Interpreter interp, ST self, Object o, Object property, String propertyName) throws STNoSuchPropertyException {
    Object value;
    Map<?, ?> map = (Map<?, ?>)o;
    if ( property==null ) value = map.get(STGroup.DEFAULT_KEY);
    else if ( property.equals("keys") ) value = map.keySet();
    else if ( property.equals("values") ) value = map.values();
    else if ( map.containsKey(property) ) value = map.get(property);
    else if ( map.containsKey(propertyName) ) { // if can't find the key, try toString version
             value = map.get(propertyName);
    }
    else value = map.get(STGroup.DEFAULT_KEY); // not found, use default
    if ( value==STGroup.DICT_KEY ) {
        value = property;
    }
    return value;
}
 
Example 5
Source Project: codebuff   Source File: MapModelAdaptor.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Object getProperty(Interpreter interp, ST self, Object o, Object property, String propertyName) throws STNoSuchPropertyException {
    Object value;
    Map<?, ?> map = (Map<?, ?>)o;
    if ( property==null ) value = map.get(STGroup.DEFAULT_KEY);
    else if ( property.equals("keys") ) value = map.keySet();
    else if ( property.equals("values") ) value = map.values();
    else if ( map.containsKey(property) ) value = map.get(property);
    else if ( map.containsKey(propertyName) ) { // if can't find the key, try toString version
             value = map.get(propertyName);
    }
    else value = map.get(STGroup.DEFAULT_KEY); // not found, use default
    if ( value==STGroup.DICT_KEY ) {
        value = property;
    }
    return value;
}
 
Example 6
Source Project: codebuff   Source File: MapModelAdaptor.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Object getProperty(Interpreter interp, ST self, Object o, Object property, String propertyName) throws STNoSuchPropertyException {
    Object value;
    Map<?, ?> map = (Map<?, ?>)o;
    if ( property==null ) value = map.get(STGroup.DEFAULT_KEY);
    else if ( property.equals("keys") ) value = map.keySet();
    else if ( property.equals("values") ) value = map.values();
    else if ( map.containsKey(property) ) value = map.get(property);
    else if ( map.containsKey(propertyName) ) { // if can't find the key, try toString version
             value = map.get(propertyName);
    }
    else value = map.get(STGroup.DEFAULT_KEY); // not found, use default
    if ( value==STGroup.DICT_KEY ) {
        value = property;
    }
    return value;
}
 
Example 7
Source Project: codebuff   Source File: MapModelAdaptor.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Object getProperty(Interpreter interp, ST self, Object o, Object property, String propertyName) throws STNoSuchPropertyException {
    Object value;
    Map<?, ?> map = (Map<?, ?>)o;
    if ( property==null ) value = map.get(STGroup.DEFAULT_KEY);
    else if ( property.equals("keys") ) value = map.keySet();
    else if ( property.equals("values") ) value = map.values();
    else if ( map.containsKey(property) ) value = map.get(property);
    else if ( map.containsKey(propertyName) ) { // if can't find the key, try toString version
             value = map.get(propertyName);
    }
    else value = map.get(STGroup.DEFAULT_KEY); // not found, use default
    if ( value==STGroup.DICT_KEY ) {
        value = property;
    }
    return value;
}
 
Example 8
Source Project: codebuff   Source File: MapModelAdaptor.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Object getProperty(Interpreter interp, ST self, Object o, Object property, String propertyName) throws STNoSuchPropertyException {
    Object value;
    Map<?, ?> map = (Map<?, ?>)o;
    if ( property==null ) value = map.get(STGroup.DEFAULT_KEY);
    else if ( property.equals("keys") ) value = map.keySet();
    else if ( property.equals("values") ) value = map.values();
    else if ( map.containsKey(property) ) value = map.get(property);
    else if ( map.containsKey(propertyName) ) { // if can't find the key, try toString version
             value = map.get(propertyName);
    }
    else value = map.get(STGroup.DEFAULT_KEY); // not found, use default
    if ( value==STGroup.DICT_KEY ) {
        value = property;
    }
    return value;
}
 
Example 9
Source Project: codebuff   Source File: MapModelAdaptor.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Object getProperty(Interpreter interp, ST self, Object o, Object property, String propertyName) throws STNoSuchPropertyException {
    Object value;
    Map<?, ?> map = (Map<?, ?>)o;
    if ( property==null ) value = map.get(STGroup.DEFAULT_KEY);
    else if ( property.equals("keys") ) value = map.keySet();
    else if ( property.equals("values") ) value = map.values();
    else if ( map.containsKey(property) ) value = map.get(property);
    else if ( map.containsKey(propertyName) ) { // if can't find the key, try toString version
        value = map.get(propertyName);
         }
    else value = map.get(STGroup.DEFAULT_KEY); // not found, use default
    if ( value==STGroup.DICT_KEY ) {
        value = property;
    }
    return value;
}
 
Example 10
Source Project: buck   Source File: BuckPyFunction.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns a python string containing all of the default rule parameters that should be made
 * available to user defined rules in the python build file parser
 */
public String addDefaultAttributes() {
  STGroup group = buckPyFunctionTemplate.get();

  ST st;
  // STGroup#getInstanceOf may not be thread safe.
  // See discussion in: https://github.com/antlr/stringtemplate4/issues/61
  synchronized (group) {
    st = group.getInstanceOf("buck_py_attrs");
  }

  st.add("implicit_required_attrs", UDR_IMPLICIT_REQUIRED_ATTRIBUTES);
  st.add("implicit_optional_attrs", UDR_IMPLICIT_OPTIONAL_ATTRIBUTES);
  st.add("implicit_required_test_attrs", UDR_IMPLICIT_REQUIRED_TEST_ATTRIBUTES);
  st.add("implicit_optional_test_attrs", UDR_IMPLICIT_OPTIONAL_TEST_ATTRIBUTES);

  try {
    StringWriter stringWriter = new StringWriter();
    st.write(new AutoIndentWriter(stringWriter, "\n"));
    return stringWriter.toString();
  } catch (IOException e) {
    throw new IllegalStateException("ST writer should not throw with StringWriter", e);
  }
}
 
Example 11
Source Project: codebuff   Source File: MapModelAdaptor.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Object getProperty(Interpreter interp, ST self, Object o, Object property, String propertyName) throws STNoSuchPropertyException {
    Object value;
    Map<?, ?> map = (Map<?, ?>)o;
    if ( property==null ) value = map.get(STGroup.DEFAULT_KEY);
    else if ( property.equals("keys") ) value = map.keySet();
    else if ( property.equals("values") ) value = map.values();
    else if ( map.containsKey(property) ) value = map.get(property);
    else if ( map.containsKey(propertyName) ) { // if can't find the key, try toString version
             value = map.get(propertyName);
    }
    else value = map.get(STGroup.DEFAULT_KEY); // not found, use default
    if ( value==STGroup.DICT_KEY ) {
        value = property;
    }
    return value;
}
 
Example 12
Source Project: codebuff   Source File: MapModelAdaptor.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Object getProperty(Interpreter interp, ST self, Object o, Object property, String propertyName) throws STNoSuchPropertyException {
    Object value;
    Map<?, ?> map = (Map<?, ?>)o;
    if ( property==null ) value = map.get(STGroup.DEFAULT_KEY);
    else if ( property.equals("keys") ) value = map.keySet();
    else if ( property.equals("values") ) value = map.values();
    else if ( map.containsKey(property) ) value = map.get(property);
    else if ( map.containsKey(propertyName) ) { // if can't find the key, try toString version
        value = map.get(propertyName);
         }
    else value = map.get(STGroup.DEFAULT_KEY); // not found, use default
    if ( value==STGroup.DICT_KEY ) {
        value = property;
    }
    return value;
}
 
Example 13
Source Project: bookish   Source File: Article.java    License: MIT License 5 votes vote down vote up
public STGroup loadTemplates() {
	String templateFileName = "templates/"+tool.target+"-article.stg";
	STGroup templates = new STGroupFile(templateFileName);
	if ( tool.isLatexTarget() ) {
		templates.registerRenderer(String.class, new LatexEscaper());
	}
	else {
		templates.registerRenderer(String.class, new HTMLEscaper());
	}
	return templates;
}
 
Example 14
Source Project: bookish   Source File: PySnippetExecutor.java    License: MIT License 5 votes vote down vote up
public List<ST> getSnippetTemplates(ChapDocInfo doc,
                                    String label,
                                    STGroup pycodeTemplates,
                                    List<ExecutableCodeDef> defs)
{
	List<ST> snippets = new ArrayList<>();
	for (ExecutableCodeDef def : defs) {
		// avoid generating output for disable=true snippets if output available
		String basename = doc.getSourceBaseName();
		String snippetsDir = tool.getBuildDir()+"/snippets";
		String chapterSnippetsDir = snippetsDir+"/"+basename;
		String errFilename = chapterSnippetsDir+"/"+basename+"_"+label+"_"+def.index+".err";
		String outFilename = chapterSnippetsDir+"/"+basename+"_"+label+"_"+def.index+".out";
		if ( ((new File(errFilename)).exists()&&(new File(outFilename)).exists()) &&
			 !def.isEnabled ) {
			continue;
		}

		String tname = def.isOutputVisible ? "pyeval" : "pyfig";
		ST snippet = pycodeTemplates.getInstanceOf(tname);
		snippet.add("def",def);
		// Don't allow "plt.show()" to execute, strip it
		String code = null;
		if ( def.code!=null ) {
			code = def.code.replace("plt.show()", "");
		}
		if ( code!=null && code.trim().length()==0 ) {
			code = null;
		}
		code = HTMLEscaper.stripBangs(code);
		snippet.add("code", code);
		snippets.add(snippet);
	}
	return snippets;
}
 
Example 15
Source Project: cs652   Source File: GenDOT.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String gen(LinkViz graph) {
    STGroup templates = new STGroupFile("DOT.stg");
    ST fileST = templates.getInstanceOf("file");
    fileST.add("gname", "testgraph");
    for (LinkViz.Link x : graph.links) {
        ST edgeST = templates.getInstanceOf("edge");
        edgeST.add("from", x.from);
        edgeST.add("to", x.to);
        fileST.add("edges", edgeST);
    }
    return fileST.render(); // render (eval) template to text
}
 
Example 16
Source Project: codebuff   Source File: STViz.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void test1() throws IOException
{ // test rig
    String templates = "method(type,name,locals,args,stats) ::= <<\n"+"public <type> <name>(<args:{a| int <a>}; separator=\", \">) {\n"+"    <if(locals)>int locals[<locals>];<endif>\n"+"    <stats;separator=\"\\n\">\n"+"}\n"+">>\n"+"assign(a,b) ::= \"<a> = <b>;\"\n"+"return(x) ::= <<return <x>;>>\n"+"paren(x) ::= \"(<x>)\"\n";
    String tmpdir = System.getProperty("java.io.tmpdir");
    writeFile(tmpdir, "t.stg", templates);
    STGroup group = new STGroupFile(tmpdir+"/"+"t.stg");
    ST st = group.getInstanceOf("method");
    st.impl.dump();
    st.add("type", "float");
    st.add("name", "foo");
    st.add("locals", 3);
    st.add("args", new String[] {"x",
                                 "y",
                                 "z"});
    ST s1 = group.getInstanceOf("assign");
    ST paren = group.getInstanceOf("paren");
    paren.add("x", "x");
    s1.add("a", paren);
    s1.add("b", "y");
    ST s2 = group.getInstanceOf("assign");
    s2.add("a", "y");
    s2.add("b", "z");
    ST s3 = group.getInstanceOf("return");
    s3.add("x", "3.14159");
    st.add("stats", s1);
    st.add("stats", s2);
    st.add("stats", s3);
    STViz viz = st.inspect();
    System.out.println(st.render()); // should not mess up ST event lists
}
 
Example 17
Source Project: codebuff   Source File: STViz.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void test4() throws IOException
{
    String templates = "main(t) ::= <<\n"+"hi: <t>\n"+">>\n"+"foo(x,y={hi}) ::= \"<bar(x,y)>\"\n"+"bar(x,y) ::= << <y> >>\n"+"ignore(m) ::= \"<m>\"\n";
    STGroup group = new STGroupString(templates);
    ST st = group.getInstanceOf("main");
    ST foo = group.getInstanceOf("foo");
    st.add("t", foo);
    ST ignore = group.getInstanceOf("ignore");
    ignore.add("m", foo); // embed foo twice!
    st.inspect();
    st.render();
}
 
Example 18
Source Project: codebuff   Source File: Compiler.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static CompiledST defineBlankRegion(CompiledST outermostImpl, Token nameToken) {
    String outermostTemplateName = outermostImpl.name;
    String mangled = STGroup.getMangledRegionName(outermostTemplateName, nameToken.getText());
    CompiledST blank = new CompiledST();
    blank.isRegion = true;
    blank.templateDefStartToken = nameToken;
    blank.regionDefType = ST.RegionType.IMPLICIT;
    blank.name = mangled;
    outermostImpl.addImplicitlyDefinedTemplate(blank);
    return blank;
}
 
Example 19
Source Project: codebuff   Source File: STViz.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void test3() throws IOException
{
    String templates = "main() ::= <<\n"+
                       "Foo: <{bar};format=\"lower\">\n" +">>\n";
    String tmpdir = System.getProperty("java.io.tmpdir");
    writeFile(tmpdir, "t.stg", templates);
    STGroup group = new STGroupFile(tmpdir+"/"+"t.stg");
    ST st = group.getInstanceOf("main");
    st.inspect();
}
 
Example 20
Source Project: codebuff   Source File: STViz.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void test2() throws IOException
{ // test rig
    String templates = "t1(q1=\"Some\\nText\") ::= <<\n"+"<q1>\n"+">>\n"+"\n"+"t2(p1) ::= <<\n"+
                       "<p1>\n"+
                       ">>\n"+
                       "\n" +"main() ::= <<\n"+"START-<t1()>-END\n"+"\n"+"START-<t2(p1=\"Some\\nText\")>-END\n"+">>\n";
    String tmpdir = System.getProperty("java.io.tmpdir");
    writeFile(tmpdir, "t.stg", templates);
    STGroup group = new STGroupFile(tmpdir+"/"+"t.stg");
    ST st = group.getInstanceOf("main");
    STViz viz = st.inspect();
}
 
Example 21
Source Project: codebuff   Source File: STViz.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void test1() throws IOException
{ // test rig
    String templates = "method(type,name,locals,args,stats) ::= <<\n"+"public <type> <name>(<args:{a| int <a>}; separator=\", \">) {\n"+"    <if(locals)>int locals[<locals>];<endif>\n"+"    <stats;separator=\"\\n\">\n"+"}\n"+">>\n"+"assign(a,b) ::= \"<a> = <b>;\"\n"+"return(x) ::= <<return <x>;>>\n"+"paren(x) ::= \"(<x>)\"\n";
    String tmpdir = System.getProperty("java.io.tmpdir");
    writeFile(tmpdir, "t.stg", templates);
    STGroup group = new STGroupFile(tmpdir+"/"+"t.stg");
    ST st = group.getInstanceOf("method");
    st.impl.dump();
    st.add("type", "float");
    st.add("name", "foo");
    st.add("locals", 3);
    st.add("args", new String[] {"x",
                                 "y",
                                 "z"});
    ST s1 = group.getInstanceOf("assign");
    ST paren = group.getInstanceOf("paren");
    paren.add("x", "x");
    s1.add("a", paren);
    s1.add("b", "y");
    ST s2 = group.getInstanceOf("assign");
    s2.add("a", "y");
    s2.add("b", "z");
    ST s3 = group.getInstanceOf("return");
    s3.add("x", "3.14159");
    st.add("stats", s1);
    st.add("stats", s2);
    st.add("stats", s3);
    STViz viz = st.inspect();
    System.out.println(st.render()); // should not mess up ST event lists
}
 
Example 22
Source Project: codebuff   Source File: STViz.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void test2() throws IOException
{ // test rig
    String templates = "t1(q1=\"Some\\nText\") ::= <<\n"+"<q1>\n"+">>\n"+"\n"+"t2(p1) ::= <<\n"+"<p1>\n"+">>\n"+"\n"+"main() ::= <<\n"+"START-<t1()>-END\n"+"\n"+"START-<t2(p1=\"Some\\nText\")>-END\n"+">>\n";
    String tmpdir = System.getProperty("java.io.tmpdir");
    writeFile(tmpdir, "t.stg", templates);
    STGroup group = new STGroupFile(tmpdir+"/"+"t.stg");
    ST st = group.getInstanceOf("main");
    STViz viz = st.inspect();
}
 
Example 23
Source Project: codebuff   Source File: STViz.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void test1() throws IOException
{ // test rig
    String templates = "method(type,name,locals,args,stats) ::= <<\n"+"public <type> <name>(<args:{a| int <a>}; separator=\", \">) {\n"+"    <if(locals)>int locals[<locals>];<endif>\n"+"    <stats;separator=\"\\n\">\n"+"}\n"+">>\n"+"assign(a,b) ::= \"<a> = <b>;\"\n"+"return(x) ::= <<return <x>;>>\n"+"paren(x) ::= \"(<x>)\"\n";
    String tmpdir = System.getProperty("java.io.tmpdir");
    writeFile(tmpdir, "t.stg", templates);
    STGroup group = new STGroupFile(tmpdir+"/"+"t.stg");
    ST st = group.getInstanceOf("method");
    st.impl.dump();
    st.add("type", "float");
    st.add("name", "foo");
    st.add("locals", 3);
    st.add("args", new String[] {"x",
                                 "y",
                                 "z"});
    ST s1 = group.getInstanceOf("assign");
    ST paren = group.getInstanceOf("paren");
    paren.add("x", "x");
    s1.add("a", paren);
    s1.add("b", "y");
    ST s2 = group.getInstanceOf("assign");
    s2.add("a", "y");
    s2.add("b", "z");
    ST s3 = group.getInstanceOf("return");
    s3.add("x", "3.14159");
    st.add("stats", s1);
    st.add("stats", s2);
    st.add("stats", s3);
    STViz viz = st.inspect();
    System.out.println(st.render()); // should not mess up ST event lists
}
 
Example 24
Source Project: codebuff   Source File: STViz.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void test1() throws IOException
{ // test rig
    String templates = "method(type,name,locals,args,stats) ::= <<\n"+"public <type> <name>(<args:{a| int <a>}; separator=\", \">) {\n"+"    <if(locals)>int locals[<locals>];<endif>\n"+"    <stats;separator=\"\\n\">\n"+"}\n"+">>\n"+"assign(a,b) ::= \"<a> = <b>;\"\n"+"return(x) ::= <<return <x>;>>\n"+"paren(x) ::= \"(<x>)\"\n";
    String tmpdir = System.getProperty("java.io.tmpdir");
    writeFile(tmpdir, "t.stg", templates);
    STGroup group = new STGroupFile(tmpdir+"/"+"t.stg");
    ST st = group.getInstanceOf("method");
    st.impl.dump();
    st.add("type", "float");
    st.add("name", "foo");
    st.add("locals", 3);
    st.add("args", new String[] {"x",
                                 "y",
                                 "z"});
    ST s1 = group.getInstanceOf("assign");
    ST paren = group.getInstanceOf("paren");
    paren.add("x", "x");
    s1.add("a", paren);
    s1.add("b", "y");
    ST s2 = group.getInstanceOf("assign");
    s2.add("a", "y");
    s2.add("b", "z");
    ST s3 = group.getInstanceOf("return");
    s3.add("x", "3.14159");
    st.add("stats", s1);
    st.add("stats", s2);
    st.add("stats", s3);
    STViz viz = st.inspect();
    System.out.println(st.render()); // should not mess up ST event lists
}
 
Example 25
Source Project: codebuff   Source File: Compiler.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static CompiledST defineBlankRegion(CompiledST outermostImpl, Token nameToken) {
    String outermostTemplateName = outermostImpl.name;
    String mangled = STGroup.getMangledRegionName(outermostTemplateName, nameToken.getText());
    CompiledST blank = new CompiledST();
    blank.isRegion = true;
    blank.templateDefStartToken = nameToken;
    blank.regionDefType = ST.RegionType.IMPLICIT;
    blank.name = mangled;
    outermostImpl.addImplicitlyDefinedTemplate(blank);
    return blank;
}
 
Example 26
Source Project: codebuff   Source File: STViz.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void test4() throws IOException
{
    String templates = "main(t) ::= <<\n"+"hi: <t>\n"+">>\n"+"foo(x,y={hi}) ::= \"<bar(x,y)>\"\n" +
                       "bar(x,y) ::= << <y> >>\n" +"ignore(m) ::= \"<m>\"\n";
    STGroup group = new STGroupString(templates);
    ST st = group.getInstanceOf("main");
    ST foo = group.getInstanceOf("foo");
    st.add("t", foo);
    ST ignore = group.getInstanceOf("ignore");
    ignore.add("m", foo); // embed foo twice!
    st.inspect();
    st.render();
}
 
Example 27
Source Project: codebuff   Source File: STViz.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void test1() throws IOException { // test rig
      String templates =
	"method(type,name,locals,args,stats) ::= <<\n" +
	"public <type> <name>(<args:{a| int <a>}; separator=\", \">) {\n" +
	"    <if(locals)>int locals[<locals>];<endif>\n"+
	"    <stats;separator=\"\\n\">\n" +
	"}\n" +
	">>\n"+
	"assign(a,b) ::= \"<a> = <b>;\"\n"+
	"return(x) ::= <<return <x>;>>\n" +
	"paren(x) ::= \"(<x>)\"\n";

      String tmpdir = System.getProperty("java.io.tmpdir");
      writeFile(tmpdir, "t.stg", templates);
      STGroup group = new STGroupFile(tmpdir+"/"+"t.stg");
      ST st = group.getInstanceOf("method");
      st.impl.dump();
      st.add("type", "float");
      st.add("name", "foo");
      st.add("locals", 3);
      st.add("args", new String[] {"x", "y", "z"});
      ST s1 = group.getInstanceOf("assign");
      ST paren = group.getInstanceOf("paren");
      paren.add("x", "x");
      s1.add("a", paren);
      s1.add("b", "y");
      ST s2 = group.getInstanceOf("assign");
      s2.add("a", "y");
      s2.add("b", "z");
      ST s3 = group.getInstanceOf("return");
      s3.add("x", "3.14159");
      st.add("stats", s1);
      st.add("stats", s2);
      st.add("stats", s3);

STViz viz = st.inspect();
System.out.println(st.render()); // should not mess up ST event lists
  }
 
Example 28
Source Project: codebuff   Source File: STViz.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void test4() throws IOException
{
    String templates = "main(t) ::= <<\n"+"hi: <t>\n"+">>\n"+"foo(x,y={hi}) ::= \"<bar(x,y)>\"\n"+"bar(x,y) ::= << <y> >>\n"+"ignore(m) ::= \"<m>\"\n";
    STGroup group = new STGroupString(templates);
    ST st = group.getInstanceOf("main");
    ST foo = group.getInstanceOf("foo");
    st.add("t", foo);
    ST ignore = group.getInstanceOf("ignore");
    ignore.add("m", foo); // embed foo twice!
    st.inspect();
    st.render();
}
 
Example 29
Source Project: codebuff   Source File: Compiler.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static CompiledST defineBlankRegion(CompiledST outermostImpl, Token nameToken) {
    String outermostTemplateName = outermostImpl.name;
    String mangled = STGroup.getMangledRegionName(outermostTemplateName, nameToken.getText());
    CompiledST blank = new CompiledST();
    blank.isRegion = true;
    blank.templateDefStartToken = nameToken;
    blank.regionDefType = ST.RegionType.IMPLICIT;
    blank.name = mangled;
    outermostImpl.addImplicitlyDefinedTemplate(blank);
    return blank;
}
 
Example 30
Source Project: codebuff   Source File: STViz.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void test1() throws IOException
{ // test rig
    String templates = "method(type,name,locals,args,stats) ::= <<\n"+"public <type> <name>(<args:{a| int <a>}; separator=\", \">) {\n"+"    <if(locals)>int locals[<locals>];<endif>\n"+"    <stats;separator=\"\\n\">\n"+"}\n"+">>\n"+"assign(a,b) ::= \"<a> = <b>;\"\n"+"return(x) ::= <<return <x>;>>\n"+"paren(x) ::= \"(<x>)\"\n";
    String tmpdir = System.getProperty("java.io.tmpdir");
    writeFile(tmpdir, "t.stg", templates);
    STGroup group = new STGroupFile(tmpdir+"/"+"t.stg");
    ST st = group.getInstanceOf("method");
    st.impl.dump();
    st.add("type", "float");
    st.add("name", "foo");
    st.add("locals", 3);
    st.add("args", new String[] {"x",
                                 "y",
                                 "z"});
    ST s1 = group.getInstanceOf("assign");
    ST paren = group.getInstanceOf("paren");
    paren.add("x", "x");
    s1.add("a", paren);
    s1.add("b", "y");
    ST s2 = group.getInstanceOf("assign");
    s2.add("a", "y");
    s2.add("b", "z");
    ST s3 = group.getInstanceOf("return");
    s3.add("x", "3.14159");
    st.add("stats", s1);
    st.add("stats", s2);
    st.add("stats", s3);
    STViz viz = st.inspect();
    System.out.println(st.render()); // should not mess up ST event lists
}