[Work in Progress] Alternative Compiler (written in Javascript and PEG, runs in browser, node.js, and rhino)

150 views
Skip to first unread message

Jamon

unread,
Dec 8, 2011, 12:02:36 AM12/8/11
to closure-temp...@googlegroups.com
I'm working on an alternative compiler for Google Closure templates.  I'm curious about whether others would be interested, and I'm also seeking feedback on my approach.

The compiler is two-part. 

The first part uses PEG (Parsing Expression Grammar) and PEGJS to parse the template code and turn it into a Javascript/JSON object for later use in the compiler (or for providing immediate feedback/syntax highlighting/code assist).

The second part is a pure javascript implementation that outputs the code.  I have a lot of work still, as it's basically a proof of concept at this point.. I've only implemented a tiny subset of the features of closure templates, but it's enough to get feedback on the approach.  I plan to work around the inefficiencies of the output code by using an output buffer to combine multiple "print" commands, and some sort of ruleset for deciding whether to inline ifs as ternaries.

I'm undecided about using soyutils.js vs writing my own sanitation.

The perceived advantages of this approach are as follows:
  • No reliance on Java for compiling
  • Portable compiler (server, browser)
  • Real-time feedback on parsing results, errors, warnings when writing soy templates
  • Easier to extend the language (easy modular support for packaging and module definitions--requirejs, AMD, goog, YUI loaders, etc)
  • Simpler, more modular, maintainable compiler (by separating and simplifying parsing and codebuilding)
Thanks in advance for any feedback.  All feedback is welcome.

Jamon

Jamon

unread,
Dec 8, 2011, 9:31:06 PM12/8/11
to closure-temp...@googlegroups.com
Woops, I meant to link to the work in progress.  Sorry about that:

Michael Bolin

unread,
Dec 9, 2011, 11:26:32 AM12/9/11
to closure-temp...@googlegroups.com
Hi Jamon, I'm curious if you expect any runtime benefits in the JS that is ultimately generated, given that:

(1) You can already use the Closure Compiler to turns ifs into ternaries, where appropriate, and many other things.
(2) There is an output option for Closure Templates to use a JS string builder rather than concatenation (though on modern browsers, I believe raw concatenation is generally more efficient -- see High Performance JavaScript by Nicholas Zakas).

The biggest feature request I've heard is to have a Python backend for Soy.

But this seems like a neat way to learn about PEGs!
--Michael

Evgeny Fadeev

unread,
Dec 9, 2011, 11:43:45 AM12/9/11
to closure-temp...@googlegroups.com
+1 to Python backend to Soy.
--
Askbot
Santiago, Chile
skype: evgeny-fadeev

Jamon

unread,
Dec 13, 2011, 5:25:02 PM12/13/11
to closure-temp...@googlegroups.com
I do not expect a runtime performance benefit, I hope to achieve performance parity (my generated code is currently terribly inefficient).

I do expect this to give us a browser based IDE for building and testing your soy templates on the fly though (with syntax highlighting and as-you-type error reporting).

Raul Rangel

unread,
Dec 14, 2011, 10:06:05 AM12/14/11
to Closure Templates Discuss
That sounds like an awesome project!! Keep us posted :)

Jamon

unread,
Dec 15, 2011, 12:03:03 PM12/15/11
to closure-temp...@googlegroups.com
Thanks.  In the interim, I've just run into this project from the linkedin folks which looks pretty promising.


It's the same techniques as closure templates, but done in javascript... supporting client/server compiling, some additional loading techniques, etc.  I don't really care for the syntax, compared to the soy syntax, but I figured if you're looking for something similar to me, that this might be worth looking at, at least.
Reply all
Reply to author
Forward
0 new messages