解析器组合
"你好,世界!" { greeting: "你好" , who: "世界" } 字符串 "你好,世界!" 解析为一个对象,具有问候 "你好" ,和 "世界" 。类型 Parser<T> = (input: string) => [ T , string ] [ "pInteger数字10,"字符串20 30" ] T 类型的解析器是一个从输入字符串到 T 的一对及字符串剩余部分的函数。例如,pInteger 在 "10 20 30" 后切割 "10",并返回数字 10 和剩余的字符串 "20 30"。 pHello [ "你好" , "100" ] pIgnoreWhitespace [ "null" , "100" ] pInteger [ "100" , "" ] "你好 100" 由 pHello 解析,然后 pIgnoreWhitespace,然后 pInteger,每个都获取一个前面的剩余部分:pHello 返回 "你好" 和 "100"。pIgnoreWhitespace 返回 " " 和 "100"。pInteger 返回 "100" 和 ""。pHello100 [ "你好 100 [\"你好\", null, 100] " , " " ] "你好 100" 由 pHello100 解析,每个都获取前面的剩余部分:pHello100 返回 "你好 100" 和 ""。pSeq(pHello , pIgnoreWhitespace , pInteger )解析器<[ string , null , number ]> pSeq 给定 pHello,pIgnoreWhitespace,pInteger 是一个元组 [string, null, number] 的解析器。const pHello100 = pSeq(...) const result = pHello100("你好 100") pHello100 [ "你好 100 [\"你好\", null, 100] " , " " ] "你好 100" 由 pHello100 解析,每个都获取前面的剩余部分:pHello100 返回 "你好 100" 和 ""。pGreeting [ "你好 100 [\"你好\", null, 100] { greeting: "你好", who: 100 } pMap " , " " ] "你好 100" 由 pGreeting 解析,每个都获取前面的剩余部分:pGreeting 返回 "你好 100" 和 ""。pSeq(pNumber, pSemicolon) [ "10; [10, ";"] " , "20;30;" ] pSeq(pNumber, pSemicolon) [ "20; [20, ";"] " , "30;" ] pSeq(pNumber, pSemicolon) [ "30; [30, ";"] " , "" ] "10;20;30;" 由 pSeq(pNumber, pSemicolon) 解析,然后 pSeq(pNumber, pSemicolon),然后 pSeq(pNumber, pSemicolon),每个都获取前面的剩余部分:pSeq(pNumber, pSemicolon) 返回 "10;" 和 "20;30;"。pSeq(pNumber, pSemicolon) 返回 "20;" 和 "30;"。pSeq(pNumber, pSemicolon) 返回 "30;" 和 ""。 PING 1.1.1.1 (1.1.1.1) 56(84) 字节的数据。 64 字节来自 1.1.1.1: icmp_seq=1 ttl=52 time=389 ms 64 字节来自 1.1.1.1: icmp_seq=2 ttl=52 time=68.7 ms 64 字节来自 1.1.1.1: icmp_seq=3 ttl=52 time=47.1 ms 64 字节来自 1.1.1.1: icmp_seq=4 ttl=52 time=48.6 ms 64 字节来自 1.1.1.1: icmp_seq=5 ttl=52 time=85.8 ms 64 字节来自 1.1.1.1: icmp_seq=6 ttl=52 time=84.8 ms 64 字节来自 1.1.1.1: icmp_seq=7 ttl=52 time=87.4 ms --- 1.1.1.1 ping 统计 --- 8 个数据包发送,7 个接收,丢包率 12.5%,时间 7008ms rtt 最小/平均/最大/mdev = 47.129/115.880/388.750/112.515 ms { header: { targetIp: "1.1.1.1", resolvedIp: "1.1.1.1", icmpPayloadSize: 56, totalPacketSize: 84 }, packets: [ { byteCount: 64, from: "1.1.1.1", seq: 1, ttl: 52, time: 389 }, { byteCount: 64, from: "1.1.1.1", seq: 2, ttl: 52, time: 68.7 }, { byteCount: 64, from: "1.1.1.1", seq: 3, ttl: 52, time: 47.1 }, { byteCount: 64, from: "1.1.1.1", seq: 4, ttl: 52, time: 48.6 }, { byteCount: 64, from: "1.1.1.1", seq: 5, ttl: 52, time: 85.8 }, { byteCount: 64, from: "1.1.1.1", seq: 6, ttl: 52, time: 84.8 }, { byteCount: 64, from: "1.1.1.1", seq: 7, ttl: 52, time: 87.4 }, ], statistics: { packetsTransmitted: 8, packetsReceived: 7, packetLoss: 12.5, time: 7008, rtt: { min: 47.129, avg: 115.88, max: 388.75, mdev: 112.515 }, }, } 折叠 const result = list.reduce((acc, n) => acc + n) ["你好", " ", 100] → "你好 100" 展开 const result = pSeq(...)("你好 100") "你好 100" → ["你好", " ", 100]
本站免费、广告极少。如果觉得有帮助,可以请我们喝杯咖啡 —— 任何金额都对持续运营有实际帮助。
☕请我喝杯咖啡