[stringtemplate-interest] Wrapping long arithmetic expressions
Mike Graham
mikegraham_uk at yahoo.co.uk
Tue Apr 21 16:31:33 PDT 2009
Hi Ter,
Yes, they are long strings composed from templates like:
plus(a,b) ::= "$a$ + $b$"
which are invoked from an Antlr walker. The a and b here obviously can
be other arithmetic expressions that have already been turned into
strings. I also track significant bracketing to keep the expression
correct.
Auto-indent is working to good effect for conditional expression
nesting but some of the arithmetic expressions I end up with are long than this message. I don't fully understand your comment about leaving these as
in template form - I assume you mean that would make it easier for me
to write a formatter that justified exactly as I want it because I still
have the structure explicitly available. At the topmost level, I'd then
use a special purpose renderer to walk though that structure to turn it
into a string.
The solution I generated is not quite a clean at that. I solved the
problem for current purposes with a formatter that uses the Python
textwrap library class (I'm using the Python Antlr runtime). This
class provides for subsequent line indents, and I know exactly how many
spaces I'll need to similate an anchor/wrap kind of justification on the
= sign. So now I have a couple of templates that look something like this:
expr(e) ::= <<
theResult = $e;format="textwrap12"$;
>>
The result isn't strictly correct because the subsequent lines are short
on the right-hand side, but the result is readable which is what I was
after. It is horribly inelegant so pass arguments into the formatter
by tacking them onto the end of the format argument. Sorry about that. I
have wondered if the renderer interface might allow arguments to be
passed in to new formatters, like the character position of an anchor if
one were present in the above template. But I haven't spent enough time
looking at the standard code to understand how that piece works, so
maybe a misguided idea.
When I get time to revisit this, I'll try your suggestion. Thank you
very much for replying to my message.
Best regards,
-- Mike.
--- On Tue, 21/4/09, Terence Parr <parrt at cs.usfca.edu> wrote:
> From: Terence Parr <parrt at cs.usfca.edu>
> Subject: Re: [stringtemplate-interest] Wrapping long arithmetic expressions
> To: "Mike Graham" <mikegraham_uk at yahoo.co.uk>
> Cc: stringtemplate-interest at antlr.org
> Date: Tuesday, 21 April, 2009, 8:48 PM
> Hi Mike,
>
> Hmm....yeah, ST tries not to split individual
> elements. That said, it should handle
> properly so that they get auto indented. So your expr
> values are just long strings? If they are developed from
> string templates, you can leave them as templates rather
> than rendering them to string and passing them in as an
> attribute. would that help? Knowing where to split something
> might be difficult even in your own writer unless you parse
> the output.
> Ter
>
> On Apr 9, 2009, at 4:04 PM, Mike Graham wrote:
>
> >
> > I've hunted though the archives and document pages,
> but haven't worked out how to do the following.
> >
> > I'm writing a utility to document and translate often
> very long conditional and arithmetic expressions. I'm
> parsing them with Antlr and rewriting them using
> StringTemplate in a Antlr tree walker. The results need to
> be readable and valid C# code. I have a top level template
> in a group that looks like this:
> >
> > result(e) ::= <<
> > TheResult = $expr$;
> >>>
> >
> > However, since $expr$ can get very long (may hundreds
> of characters). I'd really like to be able to do something
> like this:
> >
> > result(e) ::= <<
> > TheResult = <$expr$; anchor, wrap> ;
> >>>
> >
> > so that I can wrap the expression at whitespace neatly
> aligned to the =. But the wrap works only for lists with
> separators and I end up with an arithmetic expression with
> infix operators.
> >
> > I guess the correct solution is to write a new
> StringTemplateWriter, but I'm not sure if the anchor/wrap
> would be recognised in this context by StringTemplate. Could
> anyone say of this is a viable solution? Is there an easier
> way of doing this that I've overlooked? Grateful for any
> suggestions.
> >
> > Best regards,
> >
> > -- Mike.
> >
> >
> >
> >
> >
> >
> > _______________________________________________
> > stringtemplate-interest mailing list
> > stringtemplate-interest at antlr.org
> > http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>
>
More information about the stringtemplate-interest
mailing list