ParseException找不到符号getErrorOffset

时间:2017-04-12 12:43:54

标签: java javacc parseexception

使用JavaCC 6.0_1,将JDK_VERSION设置为(默认值)1.5, 我无法使用ParseException的getErrorOffset方法 - 尽管Java docs stating that it exists

有没有我错过的东西?

最小例子:

PARSER_BEGIN(B)
class B {
    public static void main(String[] args) {
        ParseException p = new ParseException("Test", 2);
        System.out.println(p.getErrorOffset());
    }
}
PARSER_END(B)

TOKEN: {<TRIVIAL: "foo">}

结果:

$ javacc B.jj
[...]
Parser generated successfully.
$ javac ./*.java
./B.java:5: error: no suitable constructor found for ParseException(String,int)
                ParseException p = new ParseException("Test", 2);
                                   ^
    constructor ParseException.ParseException(Token,int[][],String[]) is not applicable
      (actual and formal argument lists differ in length)
    constructor ParseException.ParseException() is not applicable
      (actual and formal argument lists differ in length)
    constructor ParseException.ParseException(String) is not applicable
      (actual and formal argument lists differ in length)
./B.java:6: error: cannot find symbol
                System.out.println(p.getErrorOffset());
                                    ^
  symbol:   method getErrorOffset()
  location: variable p of type ParseException
2 errors

(第二个错误同样表明&#34;错误偏移&#34; ParseException的一部分神秘地缺席......)

1 个答案:

答案 0 :(得分:0)

似乎我链接的类java.text.ParseException是而不是 JavaCC使用的类!

一些反思:

    System.out.println("getCanonicalName():\n"+ParseException.class.getCanonicalName()+"\n");
    System.out.println("getPackage():\n"+ParseException.class.getPackage()+"\n");
    System.out.println("getDeclaredMethods():");
    for (java.lang.reflect.Method m : ParseException.class.getDeclaredMethods()) {
        System.out.println(m);
    }
    System.out.println("\ngetDeclaredFields():");
    for (java.lang.reflect.Field m : ParseException.class.getFields()) {
        System.out.println(m);
    }

输出:

getCanonicalName():
ParseException

getPackage():
null

getDeclaredMethods():
private static java.lang.String ParseException.initialise(Token,int[][],java.lang.String[])
static java.lang.String ParseException.add_escapes(java.lang.String)

getDeclaredFields():
public Token ParseException.currentToken
public int[][] ParseException.expectedTokenSequences
public java.lang.String[] ParseException.tokenImage

Altogther,更多与解析器相关的功能,但缺少我所追求的看似非常有用的方法 - 尽管toString()包含该信息!