diff --git a/Sources/Parsing/ParserPrinters/StartsWith.swift b/Sources/Parsing/ParserPrinters/StartsWith.swift index d21568f005..22e24816d1 100644 --- a/Sources/Parsing/ParserPrinters/StartsWith.swift +++ b/Sources/Parsing/ParserPrinters/StartsWith.swift @@ -39,6 +39,7 @@ public struct StartsWith: Parser where Input.SubSequence == Input { public let count: Int public let possiblePrefix: AnyCollection + public let originalPossiblePrefix: Any public let startsWith: (Input) -> Bool /// Initializes a parser that successfully returns `Void` when the initial elements of its input @@ -57,13 +58,14 @@ public struct StartsWith: Parser where Input.SubSequence == I where PossiblePrefix.Element == Input.Element { self.count = possiblePrefix.count self.possiblePrefix = AnyCollection(possiblePrefix) + self.originalPossiblePrefix = possiblePrefix self.startsWith = { input in input.starts(with: possiblePrefix, by: areEquivalent) } } @inlinable public func parse(_ input: inout Input) throws { guard self.startsWith(input) else { - throw ParsingError.expectedInput(formatValue(self.possiblePrefix), at: input) + throw ParsingError.expectedInput(formatValue(self.originalPossiblePrefix), at: input) } input.removeFirst(self.count) } diff --git a/Tests/ParsingTests/StartsWithTests.swift b/Tests/ParsingTests/StartsWithTests.swift index 00310b564a..1eff9a6092 100644 --- a/Tests/ParsingTests/StartsWithTests.swift +++ b/Tests/ParsingTests/StartsWithTests.swift @@ -7,4 +7,19 @@ final class StartsWithTests: XCTestCase { XCTAssertNoThrow(try StartsWith("Hello".utf8).parse(&str)) XCTAssertEqual(", world!", Substring(str)) } + + func testParseFailure() { + var input = "Goodnight, Blob!"[...].utf8 + XCTAssertThrowsError(try StartsWith("Hello, ".utf8).parse(&input)) { error in + XCTAssertEqual( + """ + error: unexpected input + --> input:1:1 + 1 | Goodnight, Blob! + | ^ expected "Hello, " + """, + "\(error)" + ) + } + } }