String template = "foo(x,y) ::= << <x; format=\"%,d\"> <y; format=\"%,2.3f\"> >>\n"; STGroup g = new STGroupString(template); g.registerRenderer(Number.class, new NumberRenderer());
default String transform(Map<?, ?> map){ String collG = "map(tree) ::= <<\n <tree.keys:{k | - <k> ==> [<tree.(k).(\"type\")> <tree.(k).(\"val\")>]}; separator=\"\n\">\n>>"; STGroup stg = new STGroupString(collG); ST ret = stg.getInstanceOf("map");
String template = "foo(right_now) ::= << <right_now; format=\"full\"> >>\n"; STGroup g = new STGroupString(template); g.registerRenderer(Date.class, new DateRenderer()); ST st = group.getInstanceOf("foo"); st.add("right_now", new Date()); String result = st.render();
@Override default String transform(Iterator<T> it){ IsTransformer.super.transform(it); STGroup stg = new STGroupString(Iterable_To_Text.TO_STRING_ST); ST ret = stg.getInstanceOf("toText"); while(it.hasNext()){ ret.add("entries", it.next()); } return ret.render(); }
STGroup groupS = new STGroupString("some group", "val(value) ::= \"<span>Value is {value; format=\\\"%1.5f\\\"}</span>\"", '{', '}'); groupS.registerRenderer(Number.class, new NumberRenderer()); ST valTpl = groupS.getInstanceOf("val"); valTpl.add("value", 3.14159265358979353); System.out.println(valTpl.render());
@Override default String transform(Iterable<T> it){ IsTransformer.super.transform(it); STGroup stg = new STGroupString(TO_STRING_ST); ST ret = stg.getInstanceOf("toText"); for(Object obj : it){ ret.add("entries", obj); } return ret.render(); }
@Override default String transform(T[] ar){ IsTransformer.super.transform(ar); STGroup stg = new STGroupString(Iterable_To_Text.TO_STRING_ST); ST ret = stg.getInstanceOf("toText"); for(Object obj : ar){ ret.add("entries", obj); } return ret.render(); }
import org.stringtemplate.v4.*; import net.sf.json.*; class STExampleApp { public static void main(String args[]) { String template = "decl(type, name, value) ::= \"<type> <name><init(value)>;\"\n" + "init(v) ::= \"<if(v)> = <v><endif>\""; STGroup views = new STGroupString(template); ST decl = views.getInstanceOf("decl"); decl.add("type", "int"); decl.add("name", "x"); decl.add("value", 12); System.out.println(decl.render()); } }
public Grammar(Tool tool) { setTool(tool); builtFromString = true; composite = new CompositeGrammar(this); STGroup lexerGrammarSTG = new STGroupString(lexerGrammarTemplate); lexerGrammarST = lexerGrammarSTG.getInstanceOf("grammar"); target = CodeGenerator.loadLanguageTarget((String)getOption("language")); }
public Grammar(Tool tool) { setTool(tool); builtFromString = true; composite = new CompositeGrammar(this); STGroup lexerGrammarSTG = new STGroupString(lexerGrammarTemplate); lexerGrammarST = lexerGrammarSTG.getInstanceOf("grammar"); target = CodeGenerator.loadLanguageTarget((String)getOption("language")); }
/** Create a grammar from file name. */ public Grammar(Tool tool, String fileName, CompositeGrammar composite) { this.composite = composite; setTool(tool); setFileName(fileName); // ensure we have the composite set to something if ( composite.delegateGrammarTreeRoot==null ) { composite.setDelegationRoot(this); } STGroup lexerGrammarSTG = new STGroupString(lexerGrammarTemplate); lexerGrammarST = lexerGrammarSTG.getInstanceOf("grammar"); target = CodeGenerator.loadLanguageTarget((String) getOption("language")); }
/** Create a grammar from file name. */ public Grammar(Tool tool, String fileName, CompositeGrammar composite) { this.composite = composite; setTool(tool); setFileName(fileName); // ensure we have the composite set to something if ( composite.delegateGrammarTreeRoot==null ) { composite.setDelegationRoot(this); } STGroup lexerGrammarSTG = new STGroupString(lexerGrammarTemplate); lexerGrammarST = lexerGrammarSTG.getInstanceOf("grammar"); target = CodeGenerator.loadLanguageTarget((String) getOption("language")); }
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(); }
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(); }
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(); }
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(); }
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(); }
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(); }
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(); }
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(); }