Details
-
Improvement
-
Status: Closed
-
Major
-
Resolution: Fixed
-
1.6-beta-1
-
None
-
Patch
Description
Currently, user-defined occurrences of variables this, super and literals null, true, false are represented as singletons in the AST (VariableExpression.THIS_EXPRESSION, ConstantExpression.NULL, etc.). This means that their line/column information is lost, causing troubles for tools relying on that information (IDEs, code analyzers, transformations, etc.). It isn't even easy to detect that line/column information is missing for these expressions, as they will return "arbitrary" values.
Attached is a patch that changes AntlrParserPlugin so that it creates a new instance of VariableExpression/ConstantExpression for every user-defined occurrence of the variables/literals mentioned above, thus preserving line/column information. The singletons can still be used for internal purposes (like representing an implicit this in a method call); however, in order to test for the presence of one of the expressions, it is now necessary to call one of the new methods isXXXExpression (e.g. myVar.isThisExpression()) instead of comparing against the singleton (e.g. myVar == VariableExpression.THIS_EXPRESSION). This keeps changes to the code base to a minimum while avoiding the problem of defining equality of ASTNode's by way of overriding equals(). All affected code in groovy-core has been adapted accordingly.
Groovy-core builds successfully (ant install) after applying the patch on my local machine. Initial manual tests indicate that the patch works as intended. Of course an automated test would be preferable - is there already an easy way to get to an expression's AST node from a unit test? If not, I might write a transformation that allows to capture a declaration's AST node in a certain compiler phase.
Please have a look at the patch and give me some feedback. Thanks!