How can dsl::list and expression_production be combined and still handle trailing newlines? #123
-
I am currently struggling to parse a sequence of Expressions separated by newlines and am hoping to get some help. The basic premise is to parse a 'list' of expressions, separated by newlines, into a vector. I can parse a single line without issue, but when I try to add multiple lines I run into problems with a trailing newline at the end of the file. There are multiple ways to approach the problem, but the few I have tried have failed. I have lost track of what all I have tried, but out of everything I have found the following two are the most puzzling
Here are two playgrounds that exhibit this behavior. a playground with a Honorable mentions: I tried using Any suggestions on where to go from here? I'm sure I'm missing something simple. Note that in an ideal world I could parse either a single line or multiple lines with the same setup, but I'd settle for two separate structs if I must. I have tried to distill this problem down as much as possible to make it easier to debug, but I'm happy to give some more details on my particular application if it's relevant/helpful to finding a solution. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The issue here is that
However:
No, this is the exact correct tool for the job. It can use the existence of a terminator to determine whether it needs to continue parsing the list. Fundamentally, it makes sense to rephrase the question: You're not attempting to parse a list of expressions separated by newlines, you're parsing a list of lines, where each line is (Alternatively, you could also use If in the real example, you have a different terminator, you need to adapt it accordingly. |
Beta Was this translation helpful? Give feedback.
The issue here is that
dsl::list
withdsl::sep
uses the existence of the separator to determine whether it should parse anotherExpr
. So you can't have a newline after the expression.dsl::list
withdsl::trailing_sep
can't use the existence of the separator to check whether it can parse anotherExpr
(since it could be the trailing separator), soExpr
needs a branch condition to determine whether or not one matches, as the error message told you. Depending on whatExpr
parses, something likedsl::peek(foo) >> dsl::p<Expr>
as list item might do the tricker.