on cf70879 help sign in
83 lines 3.0 kB history raw
1### `++scanf` XX
3Formatted scan
5 ++ scanf :: formatted scan
6 |* [tape (pole ,_:/(*$&(_rule tape)))]
7 => .(+< [a b]=+<)
8 (scan a (parsf b))
10Scan with `;"`-interpolated parsers.
12A- here there be monsters, monsters of my making. But the basic idea is
13you use `;"` (which currently is parsed by sail but shouldn't be) to mix
14literal text and [++rule]s, and apply this to text which is a
15correspending mixture of aforementioned literals and sections parsable
16by the relevant rules. ++parsf is the parser form that combines a
17tape-rule mix into one big ++rule, ++norm being a parsf internal that
18winnows the `;"` result into a list of discriminate literals and rules,
19and ++bill doing the actual composing: ++\$:parsf just adds a layer that
20collapses the result list to a tuple, such that (scanf "foo 1 2 bar"
21;"foo {dem} {dem} bar") parses [1 2] and not [1 2 \~].
23 ~zod/try=> `[p=@ud q=@ud]`(scanf "Score is 5 to 2" [;"Score is {n} to {n}"]:n=dim:ag)
24 [p=5 q=2]
26 ~zod/try=> =n ;~(pfix (star (just '0')) (cook |=(@ud +<) dim:ag))
27 ~zod/try=> (scanf "2014-08-12T23:10:58.931Z" ;"{n}\-{n}\-{n}T{n}:{n}:{n}.{n}Z")
28 [2.014 8 12 23 10 58 931]
29 ~zod/try=> =dat (scanf "2014-08-12T23:10:58.931Z" ;"{n}\-{n}\-{n}T{n}:{n}:{n}.{n}Z")
30 ~zod/try=> `@da`(year `date`dat(- [%& -.dat], |6 ~[(div (mul |6.dat (bex 16)) 1.000)]))
31 ~2014.8.12..23.10.58..ee56
33### `++parsf` XX
35 ++ parsf :: make parser from:
36 |^ |* a=(pole ,_:/(*$&(_rule tape))) :: ;"chars{rule}chars"
37 %- cook :_ (bill (norm a))
38 |* (list)
39 ?~ +< ~
40 ?~ t i
41 [i $(+< t)]
42 ::
44`parsf` generates a `_rule` from a tape with rules embedded in it,
45literal sections being matched verbatim. The parsed type is a tuple of
46the embedded rules' results.
48Two intermediate arms are used:
50#### ++norm XX
52 :: .= (norm [;"{n}, {n}"]:n=dim:ag) ~[[& dim] [| ", "] [& dim]]:ag
53 ++ norm
54 |* (pole ,_:/(*$&(_rule tape)))
55 ?~ +< ~
56 => .(+< [i=+<- t=+<+])
57 :_ t=$(+< t)
58 =+ rul=->->.i
59 ^= i
60 ?~ rul [%| p=rul]
61 ?~ +.rul [%| p=rul]
62 ?@ &2.rul [%| p=;;(tape rul)]
63 [%& p=rul]
64 ::
66`norm` converts a `;"` pole of `[[%~. [%~. ?(tape _rule)] ~] ~]` into a
67more convenient list of discriminated tapes and rules.
69#### ++bill XX
71 :: .= (bill ~[[& dim] [| ", "] [& dim]]:ag)
72 :: ;~(plug dim ;~(pfix com ace ;~(plug dim (easy)))):ag
73 ++ bill
74 |* (list (each ,_rule tape))
75 ?~ +< (easy ~)
76 ?: ?=(| -.i) ;~(pfix (jest (crip p.i)) $(+< t))
77 %+ cook |*([* *] [i t]=+<)
78 ;~(plug p.i $(+< t))
79 --
80 ::
82`bill` builds a parser out of rules and tapes, ignoring the literal
83sections and producing a list of the rules' results.