欢迎加入QQ讨论群258996829

Swift 组合式解析工具库 TryParsec

发布时间:2016-05-09 00:16  回复:0  查看:3477  感兴趣:4  赞:0   最后回复:2016-05-09 00:16  

TryParsec 是一款组合式解析工具库(支持 CSV, XML, JSON,以及简单算术表达式),用 Swift 编写。

示例代码:

// 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])])
])

相关开源代码

您还未登录,请先登录

热门帖子

最新帖子