Scala 解释器:无论如何要获取编译错误行?

发布于 2024-12-07 04:20:14 字数 999 浏览 0 评论 0原文

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

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

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携君以终年 2024-12-14 04:20:14

部分答案是您输入的内容并不是 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} 得到的错误行)

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)

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
         ^

versus:

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
       ^

Compare the output after both lines. See the extra line in the first bit of output that says:

 import line0$object.$iw.$iw.a;

? 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})

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文