Replies: 4 comments 8 replies
-
I agree that is cumbersome, but |
Beta Was this translation helpful? Give feedback.
-
I see, in that case I'll use a simple custom error type like this until the struct ParsingError: Error {
let expectedInput: String
let remainingInput: Substring
init(expected expectedInput: String, at remainingInput: Substring) {
self.expectedInput = expectedInput
self.remainingInput = remainingInput
}
} The above custom parser example looks like this with my custom error type: /// A parser that consumes all whitespace characters from the beginning of the input. No output.
struct Whitespaces: Parser {
let minCharacters: Int
init(
minCharacters: Int = 0
) {
self.minCharacters = minCharacters
}
func parse(_ input: inout Substring) throws {
let foundWhitespaces = input.prefix { char in
char == " " || char == "\n" || char == "\r" || char == "\t"
}
guard foundWhitespaces.count >= minCharacters else {
throw ParsingError(expected: "\(minCharacters) whitespaces", at: input)
}
input.removeFirst(foundWhitespaces.count)
}
} |
Beta Was this translation helpful? Give feedback.
-
I think this would also be beneficial for testing, since we could write an assert that expects a specific error to be thrown. |
Beta Was this translation helpful? Give feedback.
-
So... ParserError hasn't materially changed since April 2022. Would you accept a PR that flipped this to public? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I just started migrating over to the latest version of swift-parsing. While doing that, I had some custom parsers that I wanted to update to the new throwing style. One of my parsers has a
minCharacters
variable and when I can't find that chars, I'd like to throw aParsingError
. But when I do, I get an error becauseParsingError
seems to be internal to the library. Is there any reason we shouldn't have access to theParsingError
type for our custom parsers? While I could write my own error type, wouldn't it be more convenient if all parsers would throw the same errors?Here's my custom parser:
Beta Was this translation helpful? Give feedback.
All reactions