groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paolo Di Tommaso <paolo.ditomm...@gmail.com>
Subject Re: How create a custom string literal type
Date Sun, 03 May 2015 19:09:50 GMT
Almost, I'm think to use an AST to convert a GStringExpression to
ConstantExpression, in which all $ placeholders are escaped. Then use a
custom TemplateEngine to interpolate variables prefixed with a % character.


p


On Sun, May 3, 2015 at 8:11 PM, Dinko Srkoč <dinko.srkoc@gmail.com> wrote:

> On 3 May 2015 at 19:53, Paolo Di Tommaso <paolo.ditommaso@gmail.com>
> wrote:
> > I think that AST cannot work here because the string has been already
> > parsed.
>
> Perhaps, but here's the idea - find all ConstantExpressions with
> String values and parse them again. If you find '%DONE' inside, try
> all VariableExpressions in scope with name 'DONE'. If found, replace
> the ConstantExpression with another one, having a GString that you
> assembled as a value.
>
> My AST skills are a bit rusty, does this seem feasible?
>
> Cheers,
> Dinko
>
> >
> >
> > p
> >
> >
> > On Sun, May 3, 2015 at 7:30 PM, Dinko Srkoč <dinko.srkoc@gmail.com>
> wrote:
> >>
> >> On 3 May 2015 at 18:01, Paolo Di Tommaso <paolo.ditommaso@gmail.com>
> >> wrote:
> >> > Hi,
> >> >
> >> > I would need to implement a string literal in which the dollar char is
> >> > not
> >> > interpreted as variable placeholder, indeed to use a custom character
> to
> >> > interpolate variables.
> >> >
> >> > For example I would like define a string like this:
> >> >
> >> >
> >> > def DONE = 'Task completed'
> >> >
> >> >
> >> > def script = /%
> >> >
> >> > mv file_name $HOME
> >> > echo %DONE
> >> > /%
> >> >
> >> >
> >> >
> >> > In which the $HOME string is ignored while  %DONE is interpolated as a
> >> > groovy variable.
> >> >
> >> > Is there any way to do that ?
> >>
> >> Probably not ... although, I imagine it might be possible using AST
> >> transformations.
> >>
> >> On a related note, it would be interesting to have the ability to use
> >> a different GString implementation, instead of GStringImpl.
> >>
> >> Cheers,
> >> Dinko
> >>
> >> >
> >> >
> >> > Cheers,
> >> > Paolo
> >> >
> >> >
> >> >
> >
> >
>

Mime
View raw message