Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed non throwing parsers crash #347

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/Parsing/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ extension Parser {
/// - Parameter input: A nebulous collection of data to be parsed.
/// - Returns: A more well-structured value parsed from the given input.
@inlinable
public func parse<C: Collection>(_ input: C) rethrows -> Output
public func parse<C: Collection>(_ input: C) throws -> Output
where Input == C.SubSequence {
var input = input[...]
return try Parse {
Expand Down Expand Up @@ -238,7 +238,7 @@ extension Parser {
/// - Returns: A more well-structured value parsed from the given input.
@_disfavoredOverload
@inlinable
public func parse<S: StringProtocol>(_ input: S) rethrows -> Output
public func parse<S: StringProtocol>(_ input: S) throws -> Output
where Input == S.SubSequence.UTF8View {
var input = input[...].utf8
return try Parse {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Parsing/ParserPrinters/Consumed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ where

extension Consumed: ParserPrinter where Upstream.Input: PrependableCollection {
@inlinable
public func print(_ output: Upstream.Input, into input: inout Upstream.Input) rethrows {
public func print(_ output: Upstream.Input, into input: inout Upstream.Input) throws {
do {
_ = try self.upstream.parse(output)
input.prepend(contentsOf: output)
Expand Down
52 changes: 52 additions & 0 deletions Tests/ParsingTests/ParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,56 @@ final class ParserTests: XCTestCase {
)
}
}

func testNonThrowingParserParseCollectionSuccess() throws {
let p = Parse { CharacterSet(charactersIn: "abc") }
XCTAssertEqual("abacab", try p.parse("abacab"))
}

func testNonThrowingParserParseCollectionFailure() {
let p = Parse { CharacterSet(charactersIn: "abc") }
XCTAssertThrowsError(try p.parse("abacad")) { error in
XCTAssertEqual(
"""
error: unexpected input
--> input:1:6
1 | abacad
| ^ expected end of input
""",
"\(error)"
)
}
}

func testNonThrowingParserParseStringToUTF8ViewSuccess() {
enum Currency { case eur, gbp, usd, unknown }
let p = OneOf {
"€".utf8.map { Currency.eur }
"£".utf8.map { Currency.gbp }
"$".utf8.map { Currency.usd }
}
.replaceError(with: Currency.unknown)
XCTAssertEqual(Currency.usd, try p.parse("$"))
}

func testNonThrowingParserParseStringToUTF8ViewFailure() {
enum Currency { case eur, gbp, usd, unknown }
let p = OneOf {
"€".utf8.map { Currency.eur }
"£".utf8.map { Currency.gbp }
"$".utf8.map { Currency.usd }
}
.replaceError(with: Currency.unknown)
XCTAssertThrowsError(try p.parse("฿")) { error in
XCTAssertEqual(
"""
error: unexpected input
--> input:1:1
1 | ฿
| ^ expected end of input
""",
"\(error)"
)
}
}
}