The ? operator from regex #41
-
I'm trying to parse a fragment of Dotty syntax from Graphviz Discarding the boilerplate
two nodes look like this
A node has an optional attribute list, like this
In a regex, I would use the What's the equivalent of I can see a workaround:
But I'd prefer something like
which avoids parsing node twice. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Experimenting, I find the answer is: Use the Here's a worked example of a parser that can accept typealias Input = ArraySlice<Int>
let n = { (v: Int) in
Prefix<Input>(minLength: 1) {
$0 == v
}.map { $0.first! }
}
let data: Input = [1,3]
let parser = n(1)
.take(Optional.parser(of: n(2)))
.take(n(3))
let parsed = parser.parse(data)
XCTAssertNotNil(parsed.output)
print(parsed) // output: Optional((1, nil, 3)), rest: ArraySlice([])) output: does not fail, feel free to copy this code. |
Beta Was this translation helpful? Give feedback.
Experimenting, I find the answer is: Use the
Optional.parser
Here's a worked example of a parser that can accept
[1,2,3]
or[1,3]
:output: does not fail,
Optional((1, nil, 3)), rest: ArraySlice([])
feel free to copy this code.