CocoaPods trunk is moving to be read-only. Read more on the blog, there are 19 months to go.

Sparse 0.4.0

Sparse 0.4.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Nov 2019
SPMSupports SPM

Maintained by John Morgan.



Sparse 0.4.0

✂️ Sparse ✂️

Version License Swift

Sparse is a simple parsing library, written in Swift. It is based on the parser-combinator approach used by Haskell's Parsec. Its focus is on natural language parser creation and descriptive error messages.

Example

Here is a CSV parser:

let quote = character("\"")

let illegalCellCharacters = CharacterSet.newlines.union(CharacterSet(charactersIn: ","))
let unquotedCellCharacter = characterNot(in: illegalCellCharacters)
    .named("cell character")
let unquotedCell = many(unquotedCellCharacter).asString()
    .map { $0.trimmingCharacters(in: .whitespaces) }

let escapedQuote = quote.skipThen(quote)
    .named("escaped quote")
let quotedCellCharacter = characterNot("\"")
    .named("quoted cell character")
    .otherwise(escapedQuote)

let quotedCell = many(quotedCellCharacter, boundedBy: quote).asString()

let cell = quotedCell.otherwise(unquotedCell)

let cellSeparator = whitespaces().then(character(",")).then(whitespaces())
    .named("cell separator")
let line = many(cell, separator: cellSeparator)

let ending = optional(newline()).then(end())
let csv = many(line, separator: newline()).thenSkip(ending)

public func parseCSV(input: String) throws -> [[String]] {
    return try csv.parse(input)
}

Naming the component parsers allows for more descriptive error messages, e.g.:

Line 8, Column 12
r8c1 , "r8"c2" , r8c3 ,r8c4,r8c5,r8c6,r8c7 , r8c8
~~~~~~~~~~~^
Expected: '"' in escaped quote
Expected: whitespace in cell separator
Expected: ',' in cell separator
Expected: newline
Expected: EOF: file

Installation

Swift Package Manager

Add Sparse as a dependency in Package.swift:

.package(url: "https://github.com/johnpatrickmorgan/sparse.git", from: "0.3.0"),

CocoaPods

Add the following line to your Podfile:

pod "Sparse"

Credit

Sparse is based on Haskell's Parsec.

License

Sparse is available under the MIT license. See the LICENSE file for more info.