对于不包含“&”的评论,我需要词法分析器规则在一条线的尽头。
'&安培;'是一个续行,但也是评论中间的有效字符。
这是我的语法:
grammar Test;
begin
: line+
;
line
: command? NEW_LINE #CommandLine
;
command
: PRINT STRINGLITERAL
;
STRINGLITERAL
: '"' .*? '"'
;
PRINT : 'print';
NEW_LINE
: ('\r'? '\n')
| '\\'
;
LINE_CONTINUE
: '&' [ \t]* NEW_LINE -> channel(HIDDEN)
;
COMMENT
: '!' ~[\r\n!&]* '!'? -> channel(HIDDEN)
;
WS
: [ \T]+ -> channel(HIDDEN)
;
上述内容有效,直到我收到如下评论:
! rock & roll
以下是我需要解析的一些有效文本:
print "hello world" ! rock & roll
print ! comment here &
"hello world"
这是输出:
line 1:30 token recognition error at: '& r'
line 1:33 token recognition error at: 'o'
line 1:34 token recognition error at: 'l'
line 1:35 token recognition error at: 'l'
[@0,3:7='print',<2>,1:3]
[@1,8:8=' ',<6>,channel=1,1:8]
[@2,9:21='"hello world"',<1>,1:9]
[@3,22:22=' ',<6>,channel=1,1:22]
[@4,23:29='! rock ',<5>,channel=1,1:23]
[@5,36:37='\r\n',<3>,1:36]
[@6,38:42='print',<2>,2:0]
[@7,43:43=' ',<6>,channel=1,2:5]
[@8,44:58='! comment here ',<5>,channel=1,2:6]
[@9,59:61='&\r\n',<4>,channel=1,2:21]
[@10,62:65=' ',<6>,channel=1,3:0]
[@11,66:78='"hello world"',<1>,3:4]
[@12,79:80='\r\n',<3>,3:17]
[@13,81:80='<EOF>',<-1>,4:19]
(begin (line (command print "hello world") \r\n) (line (command print "hello world") \r\n))
请注意@roll无法识别
答案 0 :(得分:0)
所以我认为解决方法是将COMMENT规则更改为:
COMMENT
: '!' ~[\r\n]* ('!'|LINE_CONTINUE)? -> channel(HIDDEN)
;
然后新输出:
[@0,3:7='print',<2>,1:3]
[@1,8:8=' ',<6>,channel=1,1:8]
[@2,9:21='"hello world"',<1>,1:9]
[@3,22:22=' ',<6>,channel=1,1:22]
[@4,23:35='! rock & roll',<5>,channel=1,1:23]
[@5,36:37='\r\n',<3>,1:36]
[@6,38:42='print',<2>,2:0]
[@7,43:43=' ',<6>,channel=1,2:5]
[@8,44:61='! comment here &\r\n',<5>,channel=1,2:6]
[@9,62:65=' ',<6>,channel=1,3:0]
[@10,66:78='"hello world"',<1>,3:4]
[@11,79:80='\r\n',<3>,3:17]
[@12,81:80='<EOF>',<-1>,4:19]
(begin (line (command print "hello world") \r\n) (line (command print "hello world") \r\n))
注意:
[@8,44:61='! comment here &\r\n',<5>,channel=1,2:6]
上面的评论现在结尾有&amp; \ r \ n,但没关系。