You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by bayareagreg <ba...@gmail.com> on 2017/11/26 10:32:37 UTC

How to find out the names of variables used in a groovy expression

Hello,

I need to programmatically find out the names of variables used in a
"simple" groovy expression that reads a value from one or more variables.
One can assume the expression is read-only, that is no modifications of any
state will be used. E.g. in an expression like this

"${foo}", it should return "foo"
also same in "${foo.bar.zot}"

is what I am asking possible? If yes please point me in the direction of
which API to use for this.
Thank you in advance




--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html

Re: How to find out the names of variables used in a groovy expression

Posted by Jesper Steen Møller <je...@selskabet.org>.
HI Greg,

Note: This list is primarily for developers and maintainers of the Groovy project, not for how to use Groovy. You could try users@groovy.apache.org instead. As for your question, which is admittedly borderline:
1) At compile time (i.e. in a macro or AST transformer), this should be doable, but it's not a slam-dunk oneliner (you need to drill down into each GString-expression in the abstract syntax tree and find out how each expression is constructed, and which variable/field names are in use)
2) At runtime, string interpolation has already expanded into a construction of a GString by the compiler, and by that time, the expressions are no logner recognizable (generally speaking).

I can't quite guess what you're trying to accomplish, but perhaps you should consider a templating engine instead, for the ability to parse and work with GString-like templates. Have a look at http://docs.groovy-lang.org/latest/html/documentation/template-engines.html <http://docs.groovy-lang.org/latest/html/documentation/template-engines.html>

-Jesper

> On 26 Nov 2017, at 11.32, bayareagreg <ba...@gmail.com> wrote:
> 
> Hello,
> 
> I need to programmatically find out the names of variables used in a
> "simple" groovy expression that reads a value from one or more variables.
> One can assume the expression is read-only, that is no modifications of any
> state will be used. E.g. in an expression like this
> 
> "${foo}", it should return "foo"
> also same in "${foo.bar.zot}"
> 
> is what I am asking possible? If yes please point me in the direction of
> which API to use for this.
> Thank you in advance
> 
> 
> 
> 
> --
> Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html


Re: How to find out the names of variables used in a groovy expression

Posted by MG <mg...@arscreat.com>.
After the clarification I would also recommend and approach along the 
line of waiting until the binding actually tries to access an unbound 
variable and evaluate its value then (since the name of the variables is 
not actually needed for anything else)...

On 26.11.2017 19:47, Marcin Erdmann wrote:
> Wouldn't it be easier for you then to implement a class which extends 
> groovy.lang.Binding which lazy evaluates your default variables and 
> use that as your script binding instead?
>
> On Sun, Nov 26, 2017 at 6:26 PM, bayareagreg <bayareagreg@gmail.com 
> <ma...@gmail.com>> wrote:
>
>     All right, let me explain why I need this.
>     In my product, we let our users evaluate "custom" groovy
>     expressions they
>     construct. This is done in Java via groovy Script object. There
>     are half a
>     dozen "standard" product variables users can refer to in those
>     expressions.
>     The values of these variables are bound into Bindings object
>     before the
>     script is run. The values may be a String, a GPathResult, a
>     java.util.Map,
>     etc. The problem is that some of the variables are quite expensive to
>     compute and it is a waste of time to do so if the expression does not
>     reference these variables.
>     For example, if the expression happens to be "${x}", I would be
>     nice to only
>     compute the value of x and bind it before invoking the script, not
>     y, z, w,
>     etc.
>     That is why I was wondering if there was a simple way to get the
>     list of all
>     variables used in an expression. Another way to solve the problem
>     would be
>     some kind of "lazy evaluation" approach where we would bind all
>     variables to
>     some type of "proxy" object, such that the real values are only
>     computed on
>     "as needed" basis. I could not find an easy way to do that either.
>     Some
>     pointers would be very much appreciated
>     Thanks in advance
>     Greg
>
>
>
>     --
>     Sent from:
>     http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html
>     <http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html>
>
>


Re: How to find out the names of variables used in a groovy expression

Posted by bayareagreg <ba...@gmail.com>.
Thank you. I will try this 



--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html

Re: How to find out the names of variables used in a groovy expression

Posted by Marcin Erdmann <ma...@proxerd.pl>.
Wouldn't it be easier for you then to implement a class which extends
groovy.lang.Binding which lazy evaluates your default variables and use
that as your script binding instead?

On Sun, Nov 26, 2017 at 6:26 PM, bayareagreg <ba...@gmail.com> wrote:

> All right, let me explain why I need this.
> In my product, we let our users evaluate "custom" groovy expressions they
> construct. This is done in Java via groovy Script object. There are half a
> dozen "standard" product variables users can refer to in those expressions.
> The values of these variables are bound into Bindings object before the
> script is run. The values may be a String, a GPathResult, a java.util.Map,
> etc. The problem is that some of the variables are quite expensive to
> compute and it is a waste of time to do so if the expression does not
> reference these variables.
> For example, if the expression happens to be "${x}", I would be nice to
> only
> compute the value of x and bind it before invoking the script, not y, z, w,
> etc.
> That is why I was wondering if there was a simple way to get the list of
> all
> variables used in an expression. Another way to solve the problem would be
> some kind of "lazy evaluation" approach where we would bind all variables
> to
> some type of "proxy" object, such that the real values are only computed on
> "as needed" basis. I could not find an easy way to do that either. Some
> pointers would be very much appreciated
> Thanks in advance
> Greg
>
>
>
> --
> Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html
>

Re: How to find out the names of variables used in a groovy expression

Posted by MG <mg...@arscreat.com>.
I use Groovy as a programming, not as a script langage, so I have not 
done something like this myself, but something along the line of
http://groovyconsole.appspot.com/script/112001
should work.

On 26.11.2017 19:26, bayareagreg wrote:
> All right, let me explain why I need this.
> In my product, we let our users evaluate "custom" groovy expressions they
> construct. This is done in Java via groovy Script object. There are half a
> dozen "standard" product variables users can refer to in those expressions.
> The values of these variables are bound into Bindings object before the
> script is run. The values may be a String, a GPathResult, a java.util.Map,
> etc. The problem is that some of the variables are quite expensive to
> compute and it is a waste of time to do so if the expression does not
> reference these variables.
> For example, if the expression happens to be "${x}", I would be nice to only
> compute the value of x and bind it before invoking the script, not y, z, w,
> etc.
> That is why I was wondering if there was a simple way to get the list of all
> variables used in an expression. Another way to solve the problem would be
> some kind of "lazy evaluation" approach where we would bind all variables to
> some type of "proxy" object, such that the real values are only computed on
> "as needed" basis. I could not find an easy way to do that either. Some
> pointers would be very much appreciated
> Thanks in advance
> Greg
>
>
>
> --
> Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html
>


Re: How to find out the names of variables used in a groovy expression

Posted by bayareagreg <ba...@gmail.com>.
All right, let me explain why I need this.
In my product, we let our users evaluate "custom" groovy expressions they
construct. This is done in Java via groovy Script object. There are half a
dozen "standard" product variables users can refer to in those expressions.
The values of these variables are bound into Bindings object before the
script is run. The values may be a String, a GPathResult, a java.util.Map,
etc. The problem is that some of the variables are quite expensive to
compute and it is a waste of time to do so if the expression does not
reference these variables. 
For example, if the expression happens to be "${x}", I would be nice to only
compute the value of x and bind it before invoking the script, not y, z, w,
etc.
That is why I was wondering if there was a simple way to get the list of all
variables used in an expression. Another way to solve the problem would be
some kind of "lazy evaluation" approach where we would bind all variables to
some type of "proxy" object, such that the real values are only computed on
"as needed" basis. I could not find an easy way to do that either. Some
pointers would be very much appreciated
Thanks in advance
Greg



--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html