TryParsec 是一款組合式解析工具庫(支持 CSV, XML, JSON,以及簡單算術(shù)表達(dá)式),用 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[文本內(nèi)容]]></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("文本內(nèi)容")])] // 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])]) ])