How can I write a rule to parse C++ comments either on a line alone or after other code?
I've tried lots of combinations, the latest one being:
?comment: "//" /[^\n]*/ NEWLINE
How can I write a rule to parse C++ comments either on a line alone or after other code?
I've tried lots of combinations, the latest one being:
?comment: "//" /[^\n]*/ NEWLINE
On
You had the right idea, but you should define comments as a single terminal (i.e. not a structure), for performance, and also so you can ignore them.
COMMENT: "//" /[^\n]*/ NEWLINE
%ignore COMMENT
Example grammar:
from lark import Lark
g = r"""
!start: "hello"
COMMENT: "//" /[^\n]*/ _NEWLINE
_NEWLINE: "\n"
%ignore COMMENT
%ignore " "
"""
parser = Lark(g)
print(parser.parse("hello // World \n"))
Using:
?comment: /\/\/[^\n]*/Then I had to handle the comment as a lark.lexer.Token.