Skip to content

0.7.0

Compare
Choose a tag to compare
@stephencelis stephencelis released this 14 Feb 16:15
· 133 commits to main since this release
  • Breaking change: Parser.parse's method signature has changed from optional-returning to throwing:

    -func parse(_ input: inout Input) -> Output?
    +func parse(_ input: inout Input) throws -> Output

    All of the parsers the library ships with now include error messages and context alongside failure, which can be printed:

    do {
      let output = users.parse(&input)
    } catch {
      print(error)
    }
    // error: multiple failures occurred
    // 
    // error: unexpected input
    //  --> input:3:11
    // 3 | 3,Blob Jr,tru
    //   |           ^ expected "true" or "false"
    // 
    // error: unexpected input
    //  --> input:2:16
    // 2 | 2,Blob Sr,false
    //   |                ^ expected end of input"

    This is unfortunately a breaking change. It does not seem possible to support both throwing and optional-returning at the same time without requiring alternate breaking changes, so we opted for a clean break that requires upgrading existing parsers to throwing parsers. For most of our users, it will hopefully be a matter of prepending try? to lines that call parse to get things building again, though we hope folks will take full advantage of the new error messages. For users with custom parser conformances, they will need to do a little more work to make these parsers throwing.

    For more information about the release and migration strategies, see our announcement.

  • Added: Parser.replaceError(with:), a parser modifier that transforms a throwing parser into a non-throwing parser by providing a default output.

    let sign = OneOf {
      "+".map { 1 }
      "-".map { -1 }
    }
    .replaceError(with: 1)
    
    var input = "-123"[...]
    
    // No `try` required:
    sign.parse(&input)  // -1
    input               // "123"
    
    // Simply returns the default when parsing fails:
    sign.parse(&input)  // 1
  • Added: the Fail parser has a new .init(throwing:) initializer that takes an Error for custom error messaging.

  • Deprecated: Conditional as a top-level parser has been deprecated. Use if-else builder syntax instead, which uses Conditional parsers under the hood, or reference the nested Parsers.Conditional type.

  • Infrastructure: documentation has been rewritten and expanded to reflect throwing parsers.

  • Infrastructure: Swift Package Index configuration was fixed so that our supported platform are better communicated (thanks @finestructure).