TestsTested | β |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Mar 2016 |
SPMSupports SPM | β |
Maintained by Yasuhiro Inami.
Monadic Parser Combinator for try! Swift.
try
, but please try :)NOTE: This library is still in early development. Please see TODO & FIXME.
Open Examples/TryParsecPlayground.playground
.
$ sudo gem install fastlane
$ fastlane play # prepares Xcode Playground
// Simple Arithmetic
let ans = parseArithmetic(" ( 12 + 3 ) * 4+5").value
expect(ans) == 65
// CSV
let csv = parseCSV("foo,bar,baz\r\n1,22,333\r\n").value
expect(csv) == [["foo", "bar", "baz"], ["1", "22", "333"]]
// XML
let xmlString = "<p class=\"welcome\"><a href=\"underground.html\" target=\"_blank\">Hello</a><?php echo ' Cruel'; ?> World<!-- π --><![CDATA[π£->π]]></p>"
let xml = parseXML(xmlString).value
expect(xml) == [.Element("p", [XML.Attribute("class", "welcome")], [.Element("a", [XML.Attribute("href", "underground.html"), XML.Attribute("target", "_blank")], [.Text("Hello")]), .ProcessingInstruction("php echo ' Cruel'; "), .Text(" World"), .Comment(" π "), .Text("π£->π")])]
// JSON
let jsonString = "{ \"string\" : \"hello\", \"array\" : [1, \"two\", [true, null]] }"
let json = parseJSON(jsonString).value
expect(json) == JSON.Object([
"string" : .String("hello"),
"array" : .Array([.Number(1), .String("two"), .Array([.Bool(true), .Null])])
])
import Curry
struct Model: FromJSON, ToJSON
{
let string: String
let array: [Any]?
static func fromJSON(json: JSON) -> Result<Model, JSON.ParseError>
{
return fromJSONObject(json) {
curry(self.init)
<^> $0 !! "string"
<*> $0 !? "array"
}
}
static func toJSON(model: Model) -> JSON
{
return toJSONObject([
"string" ~ model.string,
"array" ~ model.array
])
}
}
let jsonString = "{ \"string\" : \"hello\", \"array\" : [1, \"two\", [true, null]] }"
// JSON String -> Model
let decoded: Result<Model, JSON.ParseError> = decode(jsonString)
// Model -> JSON String
let encoded: String = encode(decoded.value!)
For curry
ing, see thoughtbot/Curry for more information.
>>-
, <^>
, <*>
, *>
, <*
, <|>
, <?>
zeroOrOne
, many
, many1
, manyTill
, skipMany
, skipMany1
, sepBy
, sepBy1
, sepEndBy
, sepEndBy1
, count
, chainl
, chainl1
, chainr
, chainr1
peek
, endOfInput
, satisfy
, skip
, skipWhile
, take
, takeWhile
, any
, char
, not
, string
, asciiCI
, oneOf
, noneOf
, digit
, hexDigit
, lowerAlphabet
, upperAlphabet
, alphabet
, alphaNum
, space
, skipSpaces
, endOfLine
, number
NSJSONSerialization
(even with whole-module-optimization)This library is heavily inspired by following developers & libraries: