[stringtemplate-interest] Trying to output StringTemplate with values
Louis Burroughs
louis.burroughs at gmail.com
Wed Aug 31 16:42:13 PDT 2011
When I launch STViz I see a tree view with the template name (template())
and below it the attributes attr1=foo1, attr2=foo2, etc. What I want to do
is output this information in template form.
If I start with code:
SET currentYear = '2011';
I use an antlr grammar to parse this code and I would like to output to a
file something like:
<Set("currentYear", "2011")>
If the template is:
Set(var, val)::="SET <var> = <val>"
Thanks,
Louis
On Wed, Aug 31, 2011 at 2:41 PM, Terence Parr <parrt at cs.usfca.edu> wrote:
> ok,I'm still not sure what you're trying to achieve. can you use the STViz
> to see the parameters you are looking for?
> Ter
> On Aug 29, 2011, at 12:30 PM, Louis Burroughs wrote:
>
> > When I use STGroup.show() I get a dump of the original STGroup without
> the actual attribute values. ST.toString() gives me the template name and
> doesn't print the attributes or recurse . ST.render() is inserting the
> attributes into the template so I get a roundtrip of the code I parsed with
> Antlr. I am looking for an output of the template with attribute values as
> parameters.
> >
> > I wrote my own version of show, but it is rough and I don't want to do
> something that you guys are doing already.
> >
> > btw -> I am a huge fan of Antlr and StringTemplate!
> >
> >
> > public void writeTemplate(String template, String parseFile, String
> encoding) {
> >
> > try {
> > ANTLRFileStream input = new
> ANTLRFileStream(parseFile, encoding);
> > EsqlGrammarLexer lexer = new
> EsqlGrammarLexer(input);
> > TokenStream tokens = new CommonTokenStream(lexer);
> > EsqlGrammarParser parser = new
> EsqlGrammarParser(tokens);
> > EsqlGrammarParser.esqlContents_return esqlContents
> = parser.esqlContents();
> >
> > BufferedTreeNodeStream nodeStream = new
> BufferedTreeNodeStream(new CommonTreeAdaptor(),
> > esqlContents.getTree());
> >
> > EsqlTree ast = new EsqlTree(nodeStream);
> > ast.setTemplateLib(new STGroupFile(template));
> >
> > EsqlTree.evaluator_return r = ast.evaluator();
> >
> > System.out.println(ast.getTemplateLib().show());
> >
> > } catch (RecognitionException ex) {
> > LOGGER.log(Level.SEVERE, null, ex);
> >
> > } catch (IOException e) {
> > LOGGER.log(Level.SEVERE, null, e);
> > }
> > }
> >
> >
> >
> >
> > My version of show
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> >
> > private String show(String name, STGroup stg, ST template) throws
> IOException, JSONException {
> > StringBuilder buf = new StringBuilder();
> > CompiledST c = stg.rawGetTemplate(name);
> > if (c == null)
> > return "";
> > int slash = name.lastIndexOf(47);
> > name = name.substring(slash + 1, name.length());
> >
> > buf.append(name);
> > buf.append('(');
> > Map<String, Object> attributes = template.getAttributes();
> > if (c != null && c.formalArguments != null) {
> > for (FormalArgument fa :
> c.formalArguments.values()) {
> > buf.append(fa);
> > buf.append(" = ");
> > Object o = attributes.get(fa.name);
> > // System.out.println(o);
> > if (o instanceof List) {
> > List list = (List) o;
> > if (!list.isEmpty()) {
> > if (list.get(0) instanceof
> ST) {
> > for (Object o2 :
> list) {
> > if (o2 ==
> null)
> >
> continue;
> > for (int
> ndx = 0; ndx > indent; ndx++) {
> >
> buf.append(" ");
> > }
> > indent +=
> 4;
> > if
> (!isFirst) {
> >
> buf.append("<");
> > } else {
> >
> isFirst = false;
> > }
> >
> buf.append("{");
> > ST st =
> (ST) o2;
> >
> buf.append(show(st.getName(), stg, st));
> >
> buf.append(", ");
> > }
> > buf.append("}");
> > } else {
> > buf.append("[");
> >
> buf.append(Misc.join(list.iterator(), ","));
> > buf.append("], ");
> > }
> > }
> > } else {
> > buf.append("\"");
> > buf.append(o);
> > buf.append("\"");
> > buf.append(", ");
> > }
> > }
> >
> > }
> > buf.append(')');
> > buf.append(">");
> > buf.append(System.getProperty("line.separator"));
> > // buf.append(new StringBuilder().append(" ::=
> > // <<").append(Misc.newline).toString());
> > // buf.append(new
> > //
> StringBuilder().append(c.template).append(Misc.newline).toString());
> > // buf.append(new
> > //
> StringBuilder().append(">>").append(Misc.newline).toString());
> >
> > return buf.toString();
> > }
> >
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> >
> > On Mon, Aug 29, 2011 at 2:20 PM, Terence Parr <parrt at cs.usfca.edu>
> wrote:
> > > I just can't get it to output a String or File in the format I am
> looking for.
> >
> > What is the issue?
> >
> > Ter
> >
> >
> > _______________________________________________
> > stringtemplate-interest mailing list
> > stringtemplate-interest at antlr.org
> > http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110831/bf6207fd/attachment.html
More information about the stringtemplate-interest
mailing list