当我将未完成的代码键入具有语法突出显示支持的JEditorPane中时,会出现IlegalStateException [英] IlegalStateException when I type unfinished code into a JEditorPane with syntax highlighting support

查看:110
本文介绍了当我将未完成的代码键入具有语法突出显示支持的JEditorPane中时,会出现IlegalStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ANTLR4和Netbeans平台,我创建了一个语法,为此我在Netbeans Platform应用程序中实现了语法高亮显示.一切正常,直到尝试更改代码为止.

Using ANTLR4 and the Netbeans Platform I have created a grammar for which I have implemented syntax highlighting in my Netbeans Platform application. Everything works fine up to the point when I try to alter my code.

grammar MyRule;

my_rule : '(' my_rule ')'
             | binary
             | binary_hyst
             | my_rule (AND  my_rule)+
             | my_rule (OR  my_rule)+
             ;

binary : '(' binary ')'
       | unary (EQ | NE) unary 
       ;

binary_hyst : '(' binary_hyst ')'
            | unary hyst? ( GT | LT | GTE | LTE ) unary hyst?
            ;

hyst : (HYST '(' unary ')');

unary : '(' unary ')'
      | NUMBER
      | STRING
      | (MOV | DEC | INC) '(' unary ',' NUMBER ')'
      | unary '*' unary 
      | unary '/' unary
      | unary '+' unary
      | unary '-' unary
      ;


// TOKENS 
HYST    : ('hyst');
MOV     : ('mov');
INC     : ('inc');
DEC     : ('dec');

AND : ('&&');
OR  : ('||');

EQ : ('==');
NE : ('!=');

GT  : ('>');
LT  : ('<');
GTE : ('>=');
LTE : ('<=');

NUMBER
    :   '-'? INT '.' [0-9]+  // 1.35, 0.3, -4.5
    |   '-'? INT             // -3, 45
    ;


fragment INT :   '0' | [1-9] [0-9]* ; // no leading zeros
STRING : '"' ( ESC | . )*? '"' ;
ID : [a-zA-Z_]+;
fragment ESC: '\\'  [btnr"\\] ;

WS : [ \t\n\r]+ -> channel(HIDDEN) ;

此语法允许使用以下代码:("some_string" >= 20).但是当我尝试更改我的代码(即("some_id" >= 20) && ("some_other_id < 10)时,我收到一个IllegalStateException:

This grammar allows code like this: ("some_string" >= 20). But when I try to alter my code (i.e. ("some_id" >= 20) && ("some_other_id < 10) I receive an IllegalStateException:

java.lang.IllegalStateException: Lexer     de.nordsys.catelogconfig.rulelanguagesupport.lexer.TriggerRuleNBLexer@181848
      returned null token but lexerInput.readLength()=2
      lexer-state: null
      tokenStartOffset=20, readOffset=22, lookaheadOffset=23
   Chars: "&\n" - these characters need to be tokenized.
    Fix the lexer to not return null token in this state.
        at         org.netbeans.lib.lexer.LexerInputOperation.checkLexerInputFinished(LexerInputOperation.java:457)
        at org.netbeans.lib.lexer.LexerInputOperation.nextToken(LexerInputOperation.java:217)
        at org.netbeans.lib.lexer.inc.TokenListUpdater.relex(TokenListUpdater.java:627)
        at 

...

    line 1:1 token recognition error at: '&\n'

当我输入第一个'&'时会发生这种情况为&&"令牌.看来Netbeans想要解析我的代码,而词法分析器意识到这不是有效的令牌,这是正确的.如果我只是复制并粘贴&&"它工作正常,并且Netbeans不会抛出异常.因此,这似乎是由于我输入了不完整的令牌而引起的.我该如何解决?

This happens right when I type the first '&' for the '&&' token. It seems that Netbeans wants to parse my code and the lexer recognizes that this is not a valid token, which is correct. If I simply copy and paste a '&&' it works fine and Netbeans doesn't throw an exception. So this seems to be coming from me typing in incomplete tokens. How can I resolve this?

推荐答案

您需要确保为所有可能的输入序列创建令牌.最简单的方法是在词法分析器的末尾添加ERR_CHAR规则.

You need to ensure that tokens are created for all possible input sequences. The most straightforward way to do this is add an ERR_CHAR rule to the end of your lexer.

ERR_CHAR
  : .
  ;

这可以使语法突出显示器起作用,并将错误报告推迟到解析阶段.

This allows the syntax highlighter to work, and defers reporting of the error to the parsing phase.

这篇关于当我将未完成的代码键入具有语法突出显示支持的JEditorPane中时,会出现IlegalStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆