Latest 0.3.0
Homepage https://github.com/johnpatrickmorgan/Sparse
License MIT
Platforms ios 8.0, osx 10.10
Authors

✂️ 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 cellCharacter = characterNot(in: illegalCellCharacters).named("cell character")
let escapedQuote = quote.skipThen(quote)
let quotedCellCharacter = characterNot(""").otherwise(escapedQuote).named("quoted cell character")

let quotedCell = many(quotedCellCharacter, boundedBy: quote).asString()
let unquotedCell = many(cellCharacter).asString().map { $0.trimmingCharacters(in: .whitespaces) }

let cell = quotedCell.otherwise(unquotedCell)
let cellSeparator = whitespaces().then(character(",")).then(whitespaces())

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

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

Line 5, Column 11
r5c1, "r5"c2" ,r5c3,r5c4 ,r5c5,r5c6,r5c7,r5c8
~~~~~~~~~~^
Expected: '"' in quoted cell character
Expected: whitespace in cell separator
Expected: ',' in cell separator
Expected: line separator
Expected: newline
Expected: EOF

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.

Latest podspec

{
    "name": "Sparse",
    "version": "0.3.0",
    "summary": "Sparse is a simple parsing library written in Swift.",
    "description": "Sparse is a simple parsing library, written in Swift. It is basednon the parser-combinator approach as used by Haskell's Parsec.nIts focus is on natural language parser creation and descriptivenerror messages.",
    "homepage": "https://github.com/johnpatrickmorgan/Sparse",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "johnpatrickmorgan": "[email protected]"
    },
    "source": {
        "git": "https://github.com/johnpatrickmorgan/Sparse.git",
        "tag": "0.3.0"
    },
    "social_media_url": "https://twitter.com/jpmmusic",
    "platforms": {
        "ios": "8.0",
        "osx": "10.10"
    },
    "source_files": "Sources/**/*"
}

Pin It on Pinterest

Share This