Swiftpack.co - nearfri/Strix as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by nearfri.
nearfri/Strix 2.4.5
A parser combinator library written in Swift.
⭐️ 8
🕓 10 weeks ago
.package(url: "https://github.com/nearfri/Strix.git", from: "2.4.5")

Strix 🦉

Swift Swift Compatibility Platform Compatibility codecov

Strix is a parser combinator library written in Swift.

Installation

Swift Package Manager

dependencies: [.package(url: "https://github.com/nearfri/Strix.git", from: "2.0.0")],
targets: [.target(name: "<Your Target Name>", dependencies: ["Strix"])]

Example

CSV Parsing

Year Make Model Description Price
1997 Ford E350 ac, abs, moon 3000.00
1999 Chevy Venture "Extended Edition" 4900.00
1999 Chevy Venture "Extended Edition, Very Large" 5000.00
1996 Jeep Grand Cherokee MUST SELL!
air, moon roof, loaded
4799.00

The above table of data may be represented in CSV format as follows:

Year,Make,Model,Description,Price
1997,Ford,E350,"ac, abs, moon",3000.00
1999,Chevy,"Venture ""Extended Edition""","",4900.00
1999,Chevy,"Venture ""Extended Edition, Very Large""",,5000.00
1996,Jeep,Grand Cherokee,"MUST SELL!
air, moon roof, loaded",4799.00

It consists of data records represented by lines which are made of one or more fields separated by commas. Sophisticated CSV implementations permit special characters such as newline, comma and double quotes. They are allowed by requiring " (double quote) characters around the fields containing them. Embedded double quote are represented by a pair of consecutive double quotes.

Following is an example of a CSV parser:

import Strix

let doubleQuote: Parser<Character> = .character("\"")
let twoDoubleQuote: Parser<Character> = Parser.string("\"\"") *> .just("\"")
let escapedText: Parser<String> = Parser.many((.none(of: "\"") <|> twoDoubleQuote))
    .map({ String($0) })
let escapedField: Parser<String> = doubleQuote *> escapedText <* doubleQuote

let nonSeparator: Parser<Character> = .satisfy("non-separator", { $0 != "," && !$0.isNewline })
let nonEscapedField: Parser<String> = .skipped(by: .many(nonSeparator))

let field: Parser<String> = escapedField <|> nonEscapedField
let record: Parser<[String]> = .many(field, separatedBy: .character(","))

let csvParser: Parser<[[String]]> = .many(record, separatedBy: .newline)

Passing the above CSV as csvString to try csvParser.run(csvString) will return:

[
    ["Year", "Make", "Model", "Description", "Price"],
    ["1997", "Ford", "E350", "ac, abs, moon", "3000.00"],
    ["1999", "Chevy", "Venture \"Extended Edition\"", "", "4900.00"],
    ["1999", "Chevy", "Venture \"Extended Edition, Very Large\"", "", "5000.00"],
    ["1996", "Jeep", "Grand Cherokee", "MUST SELL!\nair, moon roof, loaded", "4799.00"]
]

If you want more examples, see StrixParsers.

License

Strix is released under the MIT license. See LICENSE for more information.

GitHub

link
Stars: 8
Last commit: 4 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

2.4.5
11 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics