Template recursion for code generation

15 views
Skip to first unread message

Andy

unread,
Dec 31, 2009, 2:33:08 AM12/31/09
to JSON Template
This is a note about a new feature I put in the Python version. I
need to flesh it out and document it, but any feedback is welcome.

JSON Template can currently format JSON structures of *fixed depth*.
That is, the depth of the structure is known at template-writing-time.

This fits a lot of JSON structures, but parse trees can be represented
as JSON trees, and their depth is variable. So with a very small
amount of code, I've added *template recursion* to JSON Template.

This follows the templates-as-formatters concept described in one of
my articles: http://json-template.googlecode.com/svn/trunk/doc/On-Design-Minimalism.html

So there are no new concepts in the language, but there is some API
support for recursion.

So basically you can do

{subtree|template foo}

And the template 'foo' is used to format the var 'subtree'. Note that
in JSON Template variables can be JSON objects themselves -- they
don't have to be just strings and integers.

Templates can be mutually recursive and self-recursive by using {s|
template SELF} (SELF is a magic string, you don't need to give it a
name)

Here is an example of formatting parse trees I generated from
Narcissus:

http://github.com/andychu/narcissus/blob/master/codegen/sexp.py

With this feature, I also noticed that JSON Template becomes closer to
StringTemplate, which is used for the ANTLR parser generator.

http://stringtemplate.org/

So I think it's pretty nice that the language is flexible enough to
use for code generation now, without any language changes! It needs
to be polished a bit, and I probably will want auto-indenting for
generating code. But that will also be useful for generating web
content (HTML/XML/JS/CSS all have indentation).

Andy

Andy

unread,
Dec 31, 2009, 2:36:21 AM12/31/09
to JSON Template
Also, this brings JSON Template to parity with the "include" feature
of other template languages.

That article described how people could implement "includes" on top of
the language, but I don't think many people actually did that. So
having a built-in formatter "template" will make things work out of
the box a bit better.

That helps with: http://code.google.com/p/json-template/issues/detail?id=24

Andy

On Dec 30, 11:33 pm, Andy <a...@chubot.org> wrote:
> This is a note about a new feature I put in the Python version.  I
> need to flesh it out and document it, but any feedback is welcome.
>
> JSON Template can currently format JSON structures of *fixed depth*.
> That is, the depth of the structure is known at template-writing-time.
>
> This fits a lot of JSON structures, but parse trees can be represented
> as JSON trees, and their depth is variable.  So with a very small
> amount of code, I've added *template recursion* to JSON Template.
>
> This follows the templates-as-formatters concept described in one of

> my articles:http://json-template.googlecode.com/svn/trunk/doc/On-Design-Minimalis...

Steven Roussey

unread,
Jan 25, 2010, 10:06:40 PM1/25/10
to JSON Template
Other than SELF, this is something I made for the JS and PHP versions
do. Where are you with this and other things?

Place holders would come in real handy right now, and I'm not sure if
you were for doing that or not.

At any rate, I'll be taking a fresh look at json-template's js and php
versions soon. In particular, I'm going to try and model a new PHP
version on the changes in the JS version from last fall.

-steve--

On Dec 30 2009, 11:33 pm, Andy <a...@chubot.org> wrote:
> This is a note about a new feature I put in the Python version.  I
> need to flesh it out and document it, but any feedback is welcome.
>
> JSON Template can currently format JSON structures of *fixed depth*.
> That is, the depth of the structure is known at template-writing-time.
>
> This fits a lot of JSON structures, but parse trees can be represented
> as JSON trees, and their depth is variable.  So with a very small
> amount of code, I've added *template recursion* to JSON Template.
>
> This follows the templates-as-formatters concept described in one of

> my articles:http://json-template.googlecode.com/svn/trunk/doc/On-Design-Minimalis...

Andy Chu

unread,
Jan 27, 2010, 4:39:27 AM1/27/10
to json-t...@googlegroups.com
On Mon, Jan 25, 2010 at 7:06 PM, Steven Roussey <srou...@gmail.com> wrote:
> Other than SELF, this is something I made for the JS and PHP versions
> do. Where are you with this and other things?

I only touched the Python implementation I believe... I was pretty
satisfied with it.

> Place holders would come in real handy right now, and I'm not sure if
> you were for doing that or not.

What do you mean by placeholders?

> At any rate, I'll be taking a fresh look at json-template's js and php
> versions soon. In particular, I'm going to try and model a new PHP
> version on the changes in the JS version from last fall.

OK sounds good. Let me know if you have questions.

Andy

Reply all
Reply to author
Forward
0 new messages