[stringtemplate-interest] Trying to output StringTemplate with values
Terence Parr
parrt at cs.usfca.edu
Wed Aug 31 11:41:48 PDT 2011
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
More information about the stringtemplate-interest
mailing list