-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Capture slice and value #283
Comments
This would be pretty easy to add and would be generally useful. I'm trying to avoid adding more symbols to make grammars easier to read for new users, but For your use case though, there's an existing feature might be better than repurposing ParseSlice: the undocumented You could use this to make a customized replacement for
|
For the case where someone needs a value
This passed all my tests in a moderately complex grammar. |
As it has taken me quite some time to figure out how rule my_rule() -> MyType<'input>
= sliced:with_slice(<blank()* data:content() blank()* { data }>)
{ MyType { slice: sliced.0, data: sliced.1 } } |
I'd like a built-in expression to capture both the result of a parsing expression and the input slice that spans it, like
$(e)
but produces a tuple of(slice spanning e, return value of e)
. (This is basically theconsumed
combinator in nom;$$(e)
might be a good syntax.)My motivation is that I'm working on a programming-language parser where each AST node has a
location
field that's basically a three-tuple: (start index, length, identifier for source file). I can't get the file identifier from theposition!()
expression, but I've set up theParseSlice
implementation for my file type to insert it. However, since there's no direct access to the input object within the available expressions inpeg
, the slice operator seems to be the only way to get this value, and if I use slice I don't get the return value of the expression. I think I can work around it for the use-cases I have by pulling the location fields from some of the subexpressions, but the only fully-general solution I can think of would be something like this which parses the expression twice (I haven't tried it, I'm not sure about the return value of&e
):The text was updated successfully, but these errors were encountered: