Replies: 1 comment 5 replies
-
The patch in my head for Click to expanddiff --git a/Sources/Parsing/ParserPrinters/Prefix.swift b/Sources/Parsing/ParserPrinters/Prefix.swift
index ebc24ca2b..65a378c0d 100644
--- a/Sources/Parsing/ParserPrinters/Prefix.swift
+++ b/Sources/Parsing/ParserPrinters/Prefix.swift
@@ -101,18 +101,21 @@ public struct Prefix<Input: Collection>: Parser where Input.SubSequence == Input
@inlinable
@inline(__always)
public func parse(_ input: inout Input) throws -> Input {
+ let copied = input
var prefix = self.maximum.map(input.prefix) ?? input
prefix = self.predicate.map { prefix.prefix(while: $0) } ?? prefix
let count = prefix.count
input.removeFirst(count)
guard count >= self.minimum else {
+ let errorInput = input
+ input = copied
let atLeast = self.minimum - count
throw ParsingError.expectedInput(
"""
\(self.minimum - count) \(count == 0 ? "" : "more ")element\(atLeast == 1 ? "" : "s")\
\(self.predicate == nil ? "" : " satisfying predicate")
""",
- at: input
+ at: errorInput
)
}
return prefix
diff --git a/Sources/Parsing/Parsers/FlatMap.swift b/Sources/Parsing/Parsers/FlatMap.swift
index b41aacd0a..1abc807b1 100644
--- a/Sources/Parsing/Parsers/FlatMap.swift
+++ b/Sources/Parsing/Parsers/FlatMap.swift
@@ -45,11 +45,13 @@ extension Parsers {
do {
return try self.transform(self.upstream.parse(&input)).parse(&input)
} catch let ParsingError.failed(reason, context) {
+ let errorInput = input
+ input = original
throw ParsingError.failed(
reason,
.init(
originalInput: original,
- remainingInput: input,
+ remainingInput: errorInput,
debugDescription: context.debugDescription,
underlyingError: ParsingError.failed(reason, context)
)
diff --git a/Tests/ParsingTests/ConditionalTests.swift b/Tests/ParsingTests/ConditionalTests.swift
index b655c9856..72985b98e 100644
--- a/Tests/ParsingTests/ConditionalTests.swift
+++ b/Tests/ParsingTests/ConditionalTests.swift
@@ -30,7 +30,7 @@ final class ConditionalTests: XCTestCase {
"\(error)"
)
}
- XCTAssertEqual(" Hello, world!", input)
+ XCTAssertEqual("43 Hello, world!", input)
}
}
diff --git a/Tests/ParsingTests/FlatMapTests.swift b/Tests/ParsingTests/FlatMapTests.swift
index c7911d7b7..c42a4ada6 100644
--- a/Tests/ParsingTests/FlatMapTests.swift
+++ b/Tests/ParsingTests/FlatMapTests.swift
@@ -43,6 +43,6 @@ final class FlatMapTests: XCTestCase {
"\(error)"
)
}
- XCTAssertEqual("llo, world!", Substring(input))
+ XCTAssertEqual("Hello, world!", Substring(input))
}
}
diff --git a/Tests/ParsingTests/PrefixTests.swift b/Tests/ParsingTests/PrefixTests.swift
index 70fbace0a..9279be539 100644
--- a/Tests/ParsingTests/PrefixTests.swift
+++ b/Tests/ParsingTests/PrefixTests.swift
@@ -27,7 +27,7 @@ final class PrefixTests: XCTestCase {
"\(error)"
)
}
- XCTAssertEqual("", input)
+ XCTAssertEqual("42 Hi!", input)
}
func testPrefixWhile() {
@@ -61,7 +61,7 @@ final class PrefixTests: XCTestCase {
"\(error)"
)
}
- XCTAssertEqual("", input)
+ XCTAssertEqual("42 Hello, world!", input)
}
func testPrefixRangeFromWhileSuccess() { |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, Thanks for this great tool, I really enjoy developing with it.
I found a confusing case in
PrefixTests
swift-parsing/Tests/ParsingTests/PrefixTests.swift
Lines 11 to 15 in 4d80541
When the parse fails with an error, the input is gone. Would you happen to know if this is expected?
Unlike
BoolTests
, the input remains the same as the parse failed.swift-parsing/Tests/ParsingTests/BoolTests.swift
Lines 17 to 31 in 4d80541
The context I expect is to get
Int
first then collect the next N String. I think I can usePrefix(Int)
as follow, but maybe I approach the wrong way. 😞The test case of 4d80541
Maybe collecting N count have a better approach than using
Prefix
? 🤔Beta Was this translation helpful? Give feedback.
All reactions