Failure to correctly differentiate between Int and Double parser #296
Replies: 5 comments
-
@rex-remind101 When the input is
What might be causing some confusions is that You can use a mutable value, eg: So for your failing examples: let parser = OneOf {
Int.parser().map(.case(Value.int))
Double.parser(of: Substring.self).map(.case(Value.float))
}
let input = "1.0"
let argument = try parser.parse(input)
XCTAssertEqual(argument, .float(1.0))
let parser = OneOf {
Double.parser(of: Substring.self).map(.case(Value.float))
Int.parser().map(.case(Value.int))
}
let input = "1.0"
let argument = try parser.parse(input)
XCTAssertEqual(argument, .int(1))
|
Beta Was this translation helpful? Give feedback.
-
I guess that's just not the semantics I want, a decimal point implies a float number for my use case and I believe that is typical. I ended up doing a copy-paste and slight refactor of Fwiw, I don't see how the current semantics could allow clear disambiguation between |
Beta Was this translation helpful? Give feedback.
-
@rex-remind101 You can probably implement the lookahead with a let parser = OneOf {
Parse(.case(Value.int)) {
Int.parser()
Not { "." }
}
Double.parser()
.map(.case(Value.float))
} |
Beta Was this translation helpful? Give feedback.
-
Amazing, I'll give that a shot later but makes sense. |
Beta Was this translation helpful? Give feedback.
-
May be useful to add this pattern to the Backtracking section https://pointfreeco.github.io/swift-parsing/0.9.0/documentation/parsing/backtracking |
Beta Was this translation helpful? Give feedback.
-
When using
OneOf
there is failure to differentiate between an Int and Double.Examples:
passes
fails:
passes
fails:
Beta Was this translation helpful? Give feedback.
All reactions