当我将未完成的代码输入到具有语法高亮支持的 JEditorPane 中时出现 IlegalStateException [英] IlegalStateException when I type unfinished code into a JEditorPane with syntax highlighting support

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

问题描述

使用 ANTLR4 和 Netbeans 平台我已经创建了一个语法,我已经在我的 Netbeans 平台应用程序中实现了语法突出显示.在我尝试更改代码之前,一切正常.

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天全站免登陆