Scala 解释器:无论如何要获取编译错误行?
我正在使用 scala 解释器来运行一些用户定义的脚本。为此,我使用“IMain”类。除了报告发生编译错误的行之外,它的工作方式就像一个魅力。 为了获取错误行号,我只需解析解释器输出消息,它的形式为 < console>:lineNumber: error: ...
问题在于行号似乎会根据错误的性质和封闭范围(在 def 或不是)。
REPL 也会发生这种情况,例如:
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) Server VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val a=7
a: Int = 7
scala> a.toString2
<console>:9: error: value toString2 is not a member of Int
a.toString2
^
scala> a2.toString
<console>:8: error: not found: value a2
a2.toString
^
scala> a.toString.length3
<console>:9: error: value length3 is not a member of java.lang.String
a.toString.length3
^
我希望所有错误消息都以“
使用 IMain 类,还有其他方法获取错误行号吗? (除了不正确的结果之外,解析输出感觉有点像黑客......)
I am using the scala interpreter to run some user defined script. for this I use the "IMain" class. Works like a charm except when it comes to report the line where a compile error happen.
To get the error line number, I just parse the interpreter output message, it is in the form < console>:lineNumber: error: ...
The problem is that the line number seems to change depending on the nature of the error and the enclosing scope (within a def or not).
This happens also with the REPL, for example:
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) Server VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val a=7
a: Int = 7
scala> a.toString2
<console>:9: error: value toString2 is not a member of Int
a.toString2
^
scala> a2.toString
<console>:8: error: not found: value a2
a2.toString
^
scala> a.toString.length3
<console>:9: error: value length3 is not a member of java.lang.String
a.toString.length3
^
I would expect all error messages to start with "< console>:1" since the error is in the first line of the code to interpret...
Using the IMain class, is there another way to get the error line number ? (in addition to incorrect results, parsing the output feels kind of a hack...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
部分答案是您输入的内容并不是 scala 解释器真正运行的内容。
您可以使用 scala -Xprint:parser 来查看这一点:(我使用的是 scala 2.8.1,因此这说明了一些差异)
与:
比较两行之后的输出。请参阅输出第一位中的额外行:
?该额外的导入行说明了您在行号中看到的差异。
因此,现在您需要的是 IMain 类以某种方式告诉您在发生错误之前它在代码顶部添加了多少内容。 (这仍然不能完全解决问题 - 请参阅
g}
得到的错误行)Part of the answer is that what you type isn't what the scala interpreter is really running.
You can see this with
scala -Xprint:parser
: (I'm using scala 2.8.1, so that accounts for some of the difference)versus:
Compare the output after both lines. See the extra line in the first bit of output that says:
? That extra import line accounts for the difference you're seeing in line numbers.
So now what you need is some way for the IMain class to tell you how much stuff it's added to the top of the code before the error. (That still won't completely fix things - see the error line you get for
g}
)