首页 > 解决方案 > 在没有前导零的情况下对十进制值评估关系运算符时,Groovy 抱怨出现意外标记

问题描述

我在执行以下 Groovy 脚本片段时遇到问题。

GroovyShell sh = new GroovyShell();
sh.evaluate("\"abcd\".length() >= .34");

我收到以下异常。下面提到了整个堆栈跟踪。

Exception in thread "main" org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 1: unexpected token: >= @ line 1, column 17.
    "abcd".length() >= .34d

如果我更改.340.34,它可以工作。但是,由于某些限制,我将无法更改脚本内容。任何克服的帮助将不胜感激。

我收到以下异常

Exception in thread "main" org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 1: unexpected token: >= @ line 1, column 17.
    "abcd".length() >= .34d
                       ^
1 error

at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:150)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:120)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:132)
at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:350)
at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:144)
at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:110)
at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:234)
at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:168)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:943)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:584)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:623)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:594)
at groovytest.Testtest.main(Testtest.java:18)

标签: groovy

解决方案


您的 Groovy 代码段不正确 - 在十进制数小于1.0. 如果您尝试直接使用以下表达式编译groovyc

"abcd".length() >= .34

编译将失败,错误如下:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
test.groovy: 2: Unexpected input: '.' @ line 2, column 20.
   "abcd".length() >= .34
                      ^

1 error

Java 支持这种表示法,但是 Groovy2.x最高3.0.0-alpha-3版本不支持它。

实在的方法

修复输入 Groovy 代码片段,使其仅包含有效且可编译的代码。任何无效的 Groovy 语句或表达式都会导致失败和编译错误。

replaceAll()解决方法:使用方法添加前导零

编译此类错误代码段的唯一方法是将所有 .\d+ (点后跟至少一个空格并以数字结尾)替换为 0.$1. 考虑以下示例:

def snippet = "\"abcd\".length() >= .34; \"efgh\".length() >= .22; \"xyz\".length() >= 0.11;"

println snippet.replaceAll(' \\.(\\d+)', ' 0.$1')

它添加0到所有缺少前导零的十进制数字。运行此示例将以下输出打印到控制台:

"abcd".length() >= 0.34; "efgh".length() >= 0.22; "xyz".length() >= 0.11;

如果您将此类修改后的代码段传递给GroovyShell.evaluate()方法,它将无错误地运行。

当然,这不是一个坚如磐石的解决方案,它只是一种自动修复代码片段中引入的一些语法错误的方法。在某些极端情况下,此解决方法可能会导致一些副作用,您必须注意这一点。


推荐阅读