[antlr-interest] ANTLR + StringTemplates = formattingTool?
Terence Parr
parrt at cs.usfca.edu
Fri May 14 12:14:29 PDT 2004
On May 14, 2004, at 11:03 AM, birmanstefan wrote:
> I'm a newbie which dares to combine antlr and string templates to
> get a formatting tool :).
Sounds great. My new version of StringTemplate is about ready (just
have to do the doc). It has autoindentation based upon the indent
level of attribute references themselves and has group file format that
is like an output grammar containing template definitions. Saves
having to do multiple files.
First, you may find the following cabal (mainly the "cabal notes")
interesting:
http://www.antlr.org/workshop/cabal-2003/index.html
Our idea related to your below grammar is that you could associate a
template inline, or somehow related to the rulename that would generate
output for that rule. Mostly the template would generate a result that
is returned etc...
Also of note is our dynamically scoped attributes (as suggested by
Monty) where you can set attributes defined in any invoking rule.
> My basic idea is to have two kinds of AST
> nodes : templatized (make use of a StringTemplate to format code) and
> not templatized.
> Let's assume we have next g :
>
> rule_a :
> // templatized node
> #(RULE_A rule_b rule_c)
> {
> #rule_a.setTemplate("rule_a");
> #rule_a.setAttribute(rule_a_attrs, rule_a_values);
> System.out.println(#rule_a.getFormattedCode());
> }
> ;
> rule_b :
> // templatized node
> #(RULE_B .....)
> {
> #rule_b.setTemplate("rule_b");
> computeSomeMoreCodeForRuleBAttributes();
> #rule_b.setAttribute(rule_b_attrs, rule_b_values);
> sendToRuleA(#rule_b.getFormattedCode());
> }
> ;
> rule_c :
> // templatized node
> #(RULE_C ....)
> {
> computeCode();
> sendToRuleA(computedCode);
> }
> ;
>
> Methods setTemplate() and setAttribute() are just some API to work
> with StringTemplates. The node in this case behaves as adapter to
> StringTemplate. How I compute code or send code between rules is also
> not important right now.
>
> There are few points to discuss. First of all is the number of
> templates : at worst is equal to number of rules; this may imply a lot
> of maintainance effort.
>
> A second one is about automation. It would be nice to say
> something like :
>
> rule_a:
> { #rule_a.activateTemplate = true; }
> #(RULE_A.....)
>
> and the node automatically associate a template called "rule_a"; is
> there a way to access rule name (because it's unique) from an AST
> node?
Nothing exists currently...this will all be defined after I get basic
parsing working for ANTLR 3.0 :)
> A third issue is related to context. Let's say I have an
> enumeration "[a, b, c]"; if it's in a method result context I want to
> use a "," as separator, while if it's in a different context use a
> "\n ," separator. Is there a way to define rule aspects?
You should return the list of elements (which of course could be
templates themselves as for statements etc...) and then whoever decides
to embed that list can specify the separator. Keep everything as lists
of items as long as possible and only render to string (using
separators) when necessary.
> The n-th issue :D is related to indentation. I thought of
> including in each node an indentation level (an int). The problem is
> that for some nodes I would like to increment it while for others to
> keep it constant. Is there a way to pass the indentation level down
> the AST, during AST construction? or to choose constructor on the fly
> (based on rule context or other parameters)?
Could do during AST construction or choose on the fly during the tree
walk. The current default behavior for StringTemplate 2.0b1 is to
indent based upon the indentation of the attribute. FOr example, (from
a group file format):
method(name,type,stats) : <<
public $type$ $name$() {
$stats; separator=";\n"$
}
>>
which would automatically indent anything generated from stats with 4
spaces.
Ter
--
Professor Comp. Sci., University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org
Cofounder, http://www.jguru.com
Cofounder, http://www.knowspam.net enjoy email again!
Cofounder, http://www.peerscope.com pure link sharing
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/antlr-interest/
<*> To unsubscribe from this group, send an email to:
antlr-interest-unsubscribe at yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list