Java Code Examples for org.stringtemplate.v4.STGroup#getInstanceOf()

The following examples show how to use org.stringtemplate.v4.STGroup#getInstanceOf() . 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: STViz.java    From codebuff with 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 2
Source File: STViz.java    From codebuff with 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 3
Source File: STViz.java    From codebuff with 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 4
Source File: STViz.java    From codebuff with 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 5
Source File: GenC.java    From cs652 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args) {
	STGroup templates = new STGroupFile("C.stg");
	ST fileST = templates.getInstanceOf("file");
	fileST.add("name", "Foo");
	fileST.add("fields", new Field("i", "int"));
	fileST.add("fields", new Field("j", "float"));
	System.out.println(fileST.render());
}
 
Example 6
Source File: STViz.java    From codebuff with 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 7
Source File: STViz.java    From codebuff with 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 8
Source File: STViz.java    From codebuff with 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 9
Source File: STViz.java    From codebuff with 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 10
Source File: STViz.java    From codebuff with 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 11
Source File: PySnippetExecutor.java    From bookish with 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 12
Source File: STViz.java    From codebuff with 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 13
Source File: STViz.java    From codebuff with 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 14
Source File: STViz.java    From codebuff with 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 15
Source File: STViz.java    From codebuff with 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 16
Source File: STViz.java    From codebuff with 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 17
Source File: STViz.java    From codebuff with 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 18
Source File: STViz.java    From codebuff with 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 19
Source File: STViz.java    From codebuff with 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 20
Source File: STViz.java    From codebuff with 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();
}