Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/johnpatrickmorgan/sparse
Sparse is a simple parser-combinator library written in Swift.
https://github.com/johnpatrickmorgan/sparse
cocoapods ios macos parser spm swift
Last synced: about 1 month ago
JSON representation
Sparse is a simple parser-combinator library written in Swift.
- Host: GitHub
- URL: https://github.com/johnpatrickmorgan/sparse
- Owner: johnpatrickmorgan
- License: mit
- Created: 2017-01-12T22:04:06.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-11-07T17:07:40.000Z (about 5 years ago)
- Last Synced: 2024-09-29T11:25:40.918Z (about 1 month ago)
- Topics: cocoapods, ios, macos, parser, spm, swift
- Language: Swift
- Size: 64.5 KB
- Stars: 115
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ✂️ Sparse ✂️
[![Version](https://img.shields.io/cocoapods/v/Sparse.svg?style=flat)](http://cocoapods.org/pods/Sparse)
[![License](https://img.shields.io/cocoapods/l/Sparse.svg?style=flat)](http://cocoapods.org/pods/Sparse)
![Swift](https://img.shields.io/badge/Swift-5.1-orange.svg)Sparse is a simple parsing library, written in Swift. It is based on the parser-combinator approach used by Haskell's [Parsec](https://github.com/aslatter/parsec). Its focus is on natural language parser creation and descriptive error messages.
## Example
Here is a CSV parser:
```swift
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:
```swift
.package(url: "https://github.com/johnpatrickmorgan/sparse.git", from: "0.3.0"),
```### CocoaPods
Add the following line to your Podfile:
```ruby
pod "Sparse"
```## Credit
Sparse is based on Haskell's [Parsec](https://github.com/aslatter/parsec).
## License
Sparse is available under the MIT license. See the LICENSE file for more info.