How should nested parsing work? #282
-
Imagine a "simple" example where you're parsing anything between I have had just a hell of a time doing this, I've tried a few things but this is probably the closest I got. func parenParser() -> AnyParser<Substring, Substring> {
Parse {
"("
Optionally { Lazy { self.parenParser() } }
Prefix { $0 != ")" }
")"
}
.map { "\($0.0 ?? $0.1)" }
.eraseToAnyParser()
} It works on use cases like I feel like maybe I'm missing something simple, but after struggling for a bit I figured maybe I'd just ask here. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Figured this out myself by looking at how string parsing worked in the JSON example. Ended up using func parenParser() -> AnyParser<Substring, Substring> {
Parse {
"("
Many(into: Substring("")) { string, fragment in
string += fragment
} element: {
OneOf {
Prefix(1...) { $0 != "(" && $0 != ")" }
Lazy { parenParser() }
}
} terminator: {
")"
}
}
.eraseToAnyParser()
} |
Beta Was this translation helpful? Give feedback.
Figured this out myself by looking at how string parsing worked in the JSON example. Ended up using
Many
for character-by-character iteration (not optimized at all):