rxwei / Parsey

Swift Parser Combinators
MIT License
58 stars 6 forks source link

error: 'Parse<Target>' initializer is inaccessible due to 'internal' protection level #2

Closed jerbs closed 7 years ago

jerbs commented 7 years ago

I am trying to write a parser for IP numbers, e.g. "192.168.140.1". The parser should fail, if one of the numbers is invalid. I thought I could write a combinator to define the constraints. Then it would be possible to write: Lexer.unsignedInteger.validRange(0,255)

What I am getting is: error: 'Parse' initializer is inaccessible due to 'internal' protection level

rxwei commented 7 years ago

About the error: Parse is the result of parsing, which should not be created by the user.

Thanks for bringing this up. I added two functions:

  1. A variant of flatMap taking a closure of type (Target) -> MapTarget?, which can be a failable initializer such as Int.init.
  2. A combinator satisfying(_ predicate: @escaping (Target) -> Bool)

Now you can write it this way:

static let number = Lexer.unsignedInteger.flatMap{Int($0)}.satisfying((0...255).contains)
static let address = number.many(separatedBy: Lexer.character("."))
rxwei commented 7 years ago

@jerbs Is this resolved?

jerbs commented 7 years ago

Thanks for the quick fix. Problem is resolved. Having the satisfying combinator as part of the library is of course better than implementing an own one.

What's the reason for having the Parse properties declared public? I thought your intension was making the library open for extensions.

Last but not least many thanks for this great library. I am just learning about functional programming with Swift. I would say that your library is a quite good place to learn how to do it right.

rxwei commented 7 years ago

I intended to let Parse provide the user with access to more parsing context through mapParse, applyParse and flatMapParse. These three functions take a closure with Parse as argument. I haven't personally used it, though. Operators like ^^, ^^^, ** are powerful enough for most use cases, including source range tracking.

Glad to hear that my library helps :) Feel free to let me know if you have any questions!