scan_string functionality?
#1292
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello
I have been using
pyparsingfor parsing but a grammar I had to deal with recently is getting increasingly more complex and Lark's definition and "automatic" AST transformation look more attractive.However, there is one thing I have not been able to replicate with Lark and that is the
scan_string()functionality.scan_stringtakes the grammar and a string to parse and returns the fragments (asbegin, endpairs) of the original string that matched the grammar as well as the parsed results of the matched fragments.What I am working on is similar to a template language: Text that contains markup that delineates expressions of a DSL.
With
pyparsingI simply scan the text, get the fragments and then substitute the paragraph span with the result from the evaluation of the DSL.But, with Lark, I have to somehow express the text surrounding the markup so that the whole text gets parsed. Simple rules such as:
Do not work, as the parser stops at
PAR_OPEN.I have also tried ugly constructions such as trying to parse a string from location
konwards and upon success, advancingkto the end of the parsed segment, upon failure advancingkto the next character....But I don't like where this is going. I have also tried capturing the generic text via anon_errorfunction (a variant of the above solution) but it results in equally ugly code.Is there a better practice to deal with this problem using Lark?
EDIT:
OR split the string first on the markup marker and then evaluate the even index strings ... If there is a way of handling everything via Lark though it would be nice to learn about it.
Beta Was this translation helpful? Give feedback.
All reactions