[stringtemplate-interest] Functional List Formatting

Fred phreed at gmail.com
Mon Mar 3 06:51:25 PST 2008


On Sun, Mar 2, 2008 at 3:43 PM, Terence Parr <parrt at cs.usfca.edu> wrote:
> Hi Fred, There is no reason that front in reverse can't be provided in
>  that they do not violate any of the rules.  You can probably fake it
>  with a format option for a moment and a renderer.

If I could do that it would be perfect.
I was under the impression that creating a custom renderer and using format
could only effect a change on the elements of list.
e.g. I could create a custom renderer to do things like...

import org.antlr.stringtemplate.AttributeRenderer;

public class javaRenderer implements AttributeRenderer {

   public String toString(Object obj) {
       return obj.toString();
   }
   public String toString(Object obj, String formatName) {
       if (formatName.equals("toUpper")) {
           return obj.toString().toUpperCase();
       } else if (formatName.equals("toLower")) {
           return obj.toString().toLowerCase();
       } else if (formatName.equals("escape")) {
           return obj.toString().replaceAll("\"","\\\"");
       } else if (formatName.equals("trim")) {
           return obj.toString().trim();
       } else {
           throw new IllegalArgumentException("Unsupported format:
"+formatName);
       }
   }
}

Which, given a list L = ["abc","xyz","123"], a template of the form <L,
format="toUpper", separator=":"> would produce "ABC:XYZ:123".
But given that the toString method takes a single Object as an argument I
don't see how to use the format capability to reorder a list.
If there were a way to do this it would be exactly what I am looking for.

>  Ter
>
>
> On Feb 25, 2008, at 10:48 AM, Fred wrote:
>
>  > I have doing some things related to lists in string templates that tax
>  > the current set of functions.
>  > Specifically, 'first', 'last', 'rest', 'tail' aren't quite do it for
>  > me.
>  > In particular, I need to do things like...
>  >  'front'  : kind of like 'rest' but basically everything but 'last'
>  >  'reverse' : reverse the order of the list (useful for things like
>  >  building java package names)
>  >  'sublist(list,0,-2)+sublist(list,-1)' : all but the second to the
>  > last item.
>  >
>  > ok, that last one is garbage but you get the idea.
>  > I know this is verging on something not unlike a functional language
>  > and you may have good reasons for not going there.
>  > _______________________________________________
>  > stringtemplate-interest mailing list
>  > stringtemplate-interest at antlr.org
>  > http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20080303/d3504a89/attachment-0001.html 


More information about the stringtemplate-interest mailing list