gpt4 book ai didi

Scala 解释器 : anyway to get the line of a compile error?

转载 作者:行者123 更新时间:2023-12-01 05:37:57 24 4
gpt4 key购买 nike

我正在使用 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
^

我希望所有错误消息都以“< console>:1”开头,因为错误位于要解释的代码的第一行...

使用 IMain 类,是否有另一种获取错误行号的方法? (除了不正确的结果,解析输出感觉有点像黑客......)

最佳答案

部分答案是您键入的内容并不是 Scala 解释器真正运行的内容。

你可以用 scala -Xprint:parser 看到这个:(我使用的是scala 2.8.1,因此可以解释一些差异)

scala> a.toString2
[[syntax trees at end of parser]]// Scala source: <console>
package <empty> {
object line2$object extends scala.ScalaObject {
def <init>() = {
super.<init>();
()
};
object $iw extends scala.ScalaObject {
def <init>() = {
super.<init>();
()
};
import line0$object.$iw.$iw.a;
object $iw extends scala.ScalaObject {
def <init>() = {
super.<init>();
()
};
val res0 = a.toString2
}
}
}
}

<console>:7: error: value toString2 is not a member of Int
a.toString2
^

相对:
scala> a2.toString
[[syntax trees at end of parser]]// Scala source: <console>
package <empty> {
object line3$object extends scala.ScalaObject {
def <init>() = {
super.<init>();
()
};
object $iw extends scala.ScalaObject {
def <init>() = {
super.<init>();
()
};
object $iw extends scala.ScalaObject {
def <init>() = {
super.<init>();
()
};
val res1 = a2.toString
}
}
}
}

<console>:6: error: not found: value a2
a2.toString
^

比较两行后的输出。请参阅输出的第一位中的额外行,该行表示:
 import line0$object.$iw.$iw.a;

?额外的导入行说明了您在行号中看到的差异。

所以现在您需要的是 IMain 类的某种方式来告诉您它在错误之前添加到代码顶部的内容。 (这仍然不能完全解决问题 - 请参阅您为 g} 获得的错误行)

关于Scala 解释器 : anyway to get the line of a compile error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7585959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com