Scala Interpreter:无论如何得到编译错误的行?
发布时间:2020-12-16 19:24:22 所属栏目:安全 来源:网络整理
导读:我正在使用 scala解释器来运行一些用户定义的脚本.为此,我使用“IMain”类.除了报告发生编译错误的行之外,它就像魅力一样. 为了获得错误行号,我只解析解释器输出消息,它的形式为 console:lineNumber:错误:… 问题是,行号似乎根据错误的性质和封闭范围(在d
我正在使用
scala解释器来运行一些用户定义的脚本.为此,我使用“IMain”类.除了报告发生编译错误的行之外,它就像魅力一样.
为了获得错误行号,我只解析解释器输出消息,它的形式为< console>:lineNumber:错误:… 问题是,行号似乎根据错误的性质和封闭范围(在def内或不在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}获得的错误行) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |