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

Take and/or TakeUpTo parsers #341

Open
wants to merge 2 commits 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
80 changes: 80 additions & 0 deletions Sources/Parsing/ParserPrinters/Take.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/// A parser that returns input from the until the `terminator` `Parser`
/// matches. This provides a method of "lazy" (as opposed to "greedy") consumption of the input.
///
/// ```swift
/// enum ParkType {
/// case park
/// case world
/// }
///
/// let lineParser: some Parser<Substring, (String, ParkType)> = Take {
/// Prefix(0...).map(.string)
/// } upTo: {
/// OneOf {
/// "Park".map { ParkType.park }
/// "World".map { ParkType.world }
/// }
/// }
///
/// var input = "Jurrasic World"[...]
/// let parsed = try line.parse(&input) // ("Jurrasic ", .world)
/// ```
public struct Take<Input: Collection, Taken: Parser, Terminator: Parser>: Parser
where Input.SubSequence == Input, Terminator.Input == Input, Taken.Input == Input {
public let taken: Taken
public let terminator: Terminator

@inlinable
public init(
@ParserBuilder<Input> _ taken: () -> Taken,
@ParserBuilder<Input> upTo terminator: () -> Terminator
) {
self.taken = taken()
self.terminator = terminator()
}

@inlinable
@inline(__always)
public func parse(_ input: inout Input) throws -> (Taken.Output, Terminator.Output) {
let original = input

var currentIndex = input.startIndex
while currentIndex <= input.endIndex {
let terminatorOutput: Terminator.Output
var takenInput: Input

do {
var test = input[currentIndex...]
terminatorOutput = try terminator.parse(&test)
input = test
takenInput = original[..<currentIndex]
} catch {
guard currentIndex < input.endIndex else {
break
}
currentIndex = input.index(after: currentIndex)
continue
}

let takenOutput = try taken.parse(&takenInput)
guard takenInput.isEmpty else {
throw ParsingError.expectedInput("to match \(formatValue(terminator))", at: takenInput)
}
return (takenOutput, terminatorOutput)
}
throw ParsingError.expectedInput("take up to \(formatValue(terminator))", at: input)
}
}

extension Take: ParserPrinter where Input: PrependableCollection, Taken: ParserPrinter, Terminator: ParserPrinter {
@inlinable
public func print(_ output: Output, into input: inout Input) throws {
let (takenOutput, terminatorOutput) = output
try terminator.print(terminatorOutput, into: &input)
try taken.print(takenOutput, into: &input)
}
}

extension Parsers {
public typealias Take = Parsing.Take // NB: Convenience type alias for discovery
}
57 changes: 57 additions & 0 deletions Sources/Parsing/ParserPrinters/TakeUpTo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/// A parser that consumes a subsequence from the beginning of its input until the `terminator` `Parser`
/// matches. This provides a method of "lazy" (as opposed to "greedy") consumption of the input.
///
/// ```swift
/// let lineParser = TakeUpTo {
/// OneOf { "Park", "World" }
/// }
///
/// var input = "Jurrasic World"[...]
/// let parsed = try line.parse(&input) // ("Jurrasic ", "World")
/// ```
public struct TakeUpTo<Input: Collection, Upstream: Parser>: Parser
where Input.SubSequence == Input, Upstream.Input == Input
{
public let terminator: Upstream

@inlinable
public init(
_ terminator: Upstream
) {
self.terminator = terminator
}

@inlinable
public init(
@ParserBuilder<Input> _ terminator: () -> Upstream
) {
self.terminator = terminator()
}

@inlinable
@inline(__always)
public func parse(_ input: inout Input) throws -> Input {
let original = input

var currentIndex = input.startIndex
while currentIndex <= input.endIndex {
do {
var test = input[currentIndex...]
let _ = try terminator.parse(&test)
input = original[currentIndex...]
return original[..<currentIndex]
} catch {
// do nothing
}
guard currentIndex < input.endIndex else {
break
}
currentIndex = input.index(after: currentIndex)
}
throw ParsingError.expectedInput("take up to \(formatValue(self.terminator))", at: input)
}
}

extension Parsers {
public typealias TakeUpTo = Parsing.TakeUpTo // NB: Convenience type alias for discovery
}
94 changes: 94 additions & 0 deletions Tests/ParsingTests/TakeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import Parsing
import XCTest

final class TakeTests: XCTestCase {
func testIntAndVoid() throws {
let parser: some Parser<Substring, (Int, Void)> = Take {
Int.parser()
} upTo: {
"."
}

var input = "123.456"[...]
XCTAssertEqual(123, try parser.parse(&input).0)
}

func testUnterminated() throws {
let parser: some Parser<Substring, (Int, Void)> = Take {
Int.parser()
} upTo: {
"."
}

var input = "123456"[...]

XCTAssertThrowsError(try parser.parse(&input))
}

func testTakeStringAndInt() throws {
let parser: some Parser<Substring, (String, Int)> = Take {
Prefix(0...).map(.string)
} upTo: {
"456".map { 456 }
}

var input = "123456"[...]
let output = try parser.parse(&input)
XCTAssertEqual("123", output.0)
XCTAssertEqual(456, output.1)
}

func testTakeUpToEnd() throws {
let parser: some Parser<Substring, (Substring, ())> = Take {
Prefix(0...)
} upTo: {
End<Substring>()
}

var input = "123"[...]
XCTAssertEqual("123", try parser.parse(&input).0)
XCTAssertEqual("", input)
}

func testTakeCantParseUpTo() throws {
let parser: some Parser<Substring, (Int, ())> = Take {
Int.parser()
} upTo: {
End<Substring>()
}

var input = "123abc"[...]
XCTAssertThrowsError(try parser.parse(&input))
}

func testComplextInitMap() throws {
enum Example: Equatable {
case a
case b
}

struct ComplexType: Equatable {
let string: String
let number: Int
let example: Example?
}

let parser: some Parser<Substring, ComplexType> = Parse(ComplexType.init(string:number:example:)) {
Take {
Prefix(0...).map(.string)
} upTo: {
Int.parser()
}
Optionally {
OneOf {
"a".map { Example.a }
"b".map { Example.b }
}
}
}

var input = "Hello1b"[...]
XCTAssertEqual(ComplexType(string: "Hello", number: 1, example: .b), try parser.parse(&input))
XCTAssertTrue(input.isEmpty)
}
}
54 changes: 54 additions & 0 deletions Tests/ParsingTests/TakeUpToTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Parsing
import XCTest

final class TakeUpToTests: XCTestCase {
func testSimple() throws {
let parser = TakeUpTo {
"."
}

var input = "123.456"[...]
XCTAssertEqual("123", try parser.parse(&input))
XCTAssertEqual(".456", input)
}

func testUnterminated() throws {
let parser = TakeUpTo {
"."
}

var input = "123456"[...]

XCTAssertThrowsError(try parser.parse(&input))
}

func testTakeUpToString() throws {
let parser = TakeUpTo {
"456".map { 456 }
}
var input = "123456"[...]
let output = try parser.parse(&input)
XCTAssertEqual("123", output)
XCTAssertEqual("456", input)
}

func testTakeSubstring() throws {
let parser = TakeUpTo {
"456".map { 456 }
}
var input = "123456"[...]
let output = try parser.parse(&input)
XCTAssertEqual("123", output)
XCTAssertEqual("456", input)
}

func testTakeUpToEnd() throws {
let parser = TakeUpTo {
End<Substring>()
}

var input = "123"[...]
XCTAssertEqual("123", try parser.parse(&input))
XCTAssertTrue(input.isEmpty)
}
}