[antlr-interest] ANTLR + StringTemplates = formattingTool?

birmanstefan birmanstefan at yahoo.com
Fri May 14 11:03:10 PDT 2004


Hello!!!

     I'm a newbie which dares to combine antlr and string templates to 
get a formatting tool :). 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?

     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?
     
     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)?

     Can you please tell me your opinion about this solution? 
Are there any fake issues? Is there a way to do it in a more 
elegant/simpler way?

   Any suggestion/link is welcomed!

   Thanks, 
          Stefan.

PS: hope examples were clear enough.






 
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