【发布时间】:2011-09-28 15:52:31
【问题描述】:
我正在使用 scala 解释器来运行一些用户定义的脚本。为此,我使用“IMain”类。除了报告编译错误发生的行之外,它就像一个魅力。
为了得到错误行号,我只是解析解释器输出消息,它的形式是
问题在于行号似乎会根据错误的性质和封闭范围(是否在 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 类,是否有另一种方法来获取错误行号? (除了不正确的结果之外,解析输出感觉有点像黑客......)
【问题讨论】:
-
你得到的数字代表错误在行中的位置,而不是行本身的位置。
-
你使用的是IMain的compile()方法,还是IMain的interpret()方法?
-
到目前为止,我使用的是 IMain 解释器,它在 Scala 2.9.0.1 上运行良好
-
在写这个问题之前,我只是切换到 Scala 2.9.1.final。我第一次尝试时,行为与 2.9.0.1 相同(如您在原始问题中所见)。现在,由于某种原因,我得到了相同的行号:两种情况下都是 8(但在“g}”的情况下是 1)
标签: scala error-handling interpreter read-eval-print-loop