David Stafford created GROOVY-9894:
--------------------------------------
Summary: Dollar slashy string ending in escaped dollar parses in 2.0 fails in 3.0
Key: GROOVY-9894
URL:
https://issues.apache.org/jira/browse/GROOVY-9894 Project: Groovy
Issue Type: Bug
Components: lexer
Affects Versions: 3.0.7, 3.0.5
Environment: Windows 10
Reporter: David Stafford
This program fails to compile in 3.0.7 (and 3.0.5) but parses in 2.5:
{code:java}
public class TestClass {
static final String uriRegex = $/^/([a-zA-Z0-9]+)/$$/$
public TestClass(
) {
// String uuid = UUID.randomUUID().toString();
// "/${evenme}/${uuid}.xml"
}
}
{code}
error is:
{noformat}
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
src\main\groovy\com\broadridge\datafabric\publisher\enrichment\formatters\TestClass.groovy: 7: Unexpected input: '$/$\r\n\r\n\tpublic TestClass(\r\n\t) {\r\n\t\t//\t\tString uuid = UUID.randomUUID().toString();\r\n // "/${evenme}/${uuid}.xml"' @ line 7, column 35.
// "/${evenme}/${uuid}.xml"
^
1 error
{noformat}
In my actual code, only the comment is pointed out as incorrect, which was very disconcerting. The issue is the $/$ in uriRegex is taking precedence over the $$ (possibly because its a longer but later match?). This means the lexer thinks we're still in a dollar-slashy string. What it should do is consume the $$, turn it into a $ (escaped dollar), then hit the /$, and end the string.
My workaround is to convert to a slashy string, but that means I need to escape all the forward slashes in the regex, and that's what dollar slashy strings are good for (avoiding extra escapes). As it stands, in 3.0 a dollar-slashy can't end with a dollar, which is a pretty common character for a regex to end with.
This came up when converting a large codebase to groovy 3.0, getting bizarre error messages pointing far away from the actual issue.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)