nerdsupremacist / Syntax

Write value-driven parsers quickly in Swift with an intuitive SwiftUI-like DSL
https://quickbirdstudios.com/blog/swift-string-parse/
MIT License
147 stars 10 forks source link

With Xcode 14.1 beta 1 there is a memory crash #9

Open doozMen opened 1 year ago

doozMen commented 1 year ago

I guess because `free()1Password nulls out freed memory there is a memory problem as when I use the following code


import Foundation
import Syntax

public enum JSON {
  case object([String : JSON])
  case array([JSON])
  case int(Int)
  case double(Double)
  case bool(Bool)
  case string(String)
  case null
}

public struct TokenParser: RecursiveParser {
  public init() {}

  public var body: any Parser<JSON> {
    Either {
      JSONDictionaryParser().map(JSON.object)
      JSONArrayParser().map(JSON.array)

      StringLiteral().map(JSON.string)
      IntLiteral().map(JSON.int)
      DoubleLiteral().map(JSON.double)
      BooleanLiteral().map(JSON.bool)

      Word("null").map(to: JSON.null)
    }
  }
}

public struct JSONArrayParser: Parser {
  // reference to the JSON parser

  public var body: any Parser<[JSON]> {
    "["

    TokenParser().separated(by: ",")

    "]"
  }
}

public struct JSONDictionaryParser: Parser {  
  public var body: any Parser<[String : JSON]> {
    "{"

    // Group acts kinda like parenthesis here.
    // It groups the key-value pair into one parser
    Group {
      StringLiteral()

      ":"

      TokenParser()
    }
    .separated(by: ",")
    .map { values in
      // put the pairs in a dictionary
      return Dictionary(values) { $1 }
    }

    "}"
  }
}

To run a simple test


final class SyntaxTokenParsing: XCTestCase {
  func testSyntax() throws {

    let test = """
    {
    "key1" : { "value": 1 },
    "key2" : { "value": 2 }
    }
    """
    let jsonParsed = try TokenParser().parse(test)

    XCTAssertEqual("\(jsonParsed)", "")
  }
}

This works on Xcode 14.0 but not on 14.1

Screenshot 2022-09-23 at 11 16 09
andrews05 commented 1 year ago

I'm getting the same error in Xcode 14.2. Trivial example

struct TestParser: Parser {
    var body: AnyParser<(String, Int)> {
        Word("D")
        IntLiteral()
    }
}

try? TestParser().parse("D 1")