0
使用ANTLR4和Netbeans平臺我創建了一個語法,在該語法中我在Netbeans平臺應用程序中實現了語法高亮顯示。一切工作正常,當我試圖改變我的代碼。IlegalStateException當我將未完成的代碼鍵入支持語法高亮的JEditorPane中時
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:
java.lang.IllegalStateException: Lexer de.nor[email protected]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不會引發異常所以這似乎來自我鍵入如何解決此問題?
完美地工作,謝謝! – padde 2014-09-30 06:36:27