agrestio / agrest

Server-side Java REST Framework for easy access to data graphs from various backends
https://agrest.io
Apache License 2.0
80 stars 34 forks source link

Exp.toString() - smarter parenthesis #644

Closed andrus closed 6 months ago

andrus commented 1 year ago

Currently, our expressions generate parenthesis defensively in their "toString()" method, to avoid context checking. While this generates syntactically correct expression strings, they are not very human-readable. E.g.:

Exp.parse("a = $1 and b = $2 or not d = 'a'").toString();

// produces the following String:
// (((a) = ($1)) and ((b) = ($2))) or (not ((d) = ('a')))

Ideally Exp.toString() should be aware of expression associativity (operator precedence) rules as defined in the grammar, and only add the needed parenthesis, but for now we can go after a low-hanging fruit - remove parenthethis around simple expressions like paths, parameters and scalars. So the expression above should produce smth like this, which is already way more readable:

// ((a = $1) and (b = $2)) or (not (d = 'a'))