This is a follow-up question to my previous: Why do we need to specify the standard Lark lexer to be able to catch comment terminals?
I need to "catch" and save comments in the DSL parsed by the Lark-based parser. It seems to work well when using the 'standard'
lexer, but then the grammar can't parse the rest of the DSL.
Instead the 'dynamic'
or 'dynamic_complete'
needs to be used, but then the comments can't seem to be "caught".
I have been using a variant of the second example from Larks own recipes for testing:
import lark
comments = []
grammar = r'''
start: INT*
COMMENT: "//" /[^\n]*/
%import common (INT, WS)
%ignore COMMENT
%ignore WS
'''
parser = lark.Lark(grammar, lexer='dynamic', lexer_callbacks={'COMMENT': comments.append})
source = r'''
1 2 3 // hello
// world
4 5 6
'''
parser.parse(source)
print(comments)
This program will print the comments as an empty list ([]
), but will otherwise ignore them.
Are there other ways to "catch" and save terminals which otherwise needs to be ignored?