what is the correct antlr comment lexer rule to exlude ampersand but only if it is at the end? -
what is the correct antlr comment lexer rule to exlude ampersand but only if it is at the end? -
i need lexer rule comment not include '&' @ end of line.
'&' line continuation valid character in middle of comment.
here grammar:
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) ;
the above works until have comment like:
! stone & roll
here valid text need parse:
print "hello world" ! stone & roll print ! comment here & "hello world"
here output:
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='! stone ',<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))
note @roll not recognized
so think solution alter comment rule to:
comment : '!' ~[\r\n]* ('!'|line_continue)? -> channel(hidden) ;
the new output then:
[@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='! stone & 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))
note:
[@8,44:61='! comment here &\r\n',<5>,channel=1,2:6]
the above comment has &\r\n on end ok.
antlr
Comments
Post a Comment