on 4fdc19d help sign in
618 lines 9.3 kB history raw
1+++
2title = "4l: Atom Parsing"
3weight = 39
4template = "doc.html"
5+++
7## `++so`
9Coin parser engine
11Core containing arms that parse atoms encoded in strings.
13#### Source
15```hoon
16++ so
17 ~% %so + ~
18 |%
19```
21---
23### `++bisk:so`
25Parse aura-atom pair
27Parsing `rule`. Parses an `@u` of any permitted base,
28producing a `dime`.
30#### Source
32```hoon
33++ bisk
34 ~+
35 ;~ pose
36 ;~ pfix (just '0')
37 ;~ pose
38 (stag %ub ;~(pfix (just 'b') bay:ag))
39 (stag %uc ;~(pfix (just 'c') fim:ag))
40 (stag %ui ;~(pfix (just 'i') dim:ag))
41 (stag %ux ;~(pfix (just 'x') hex:ag))
42 (stag %uv ;~(pfix (just 'v') viz:ag))
43 (stag %uw ;~(pfix (just 'w') wiz:ag))
44 ==
45 ==
46 (stag %ud dem:ag)
47 ==
48```
50#### Examples
52```
53> (scan "25" bisk:so)
54[%ud 25]
56> (scan "0x12.6401" bisk:so)
57[%ux 1.205.249]
58```
60---
62### `++crub:so`
64Parse `@da`, `@dr`, `@p`, `@t`
66Parsing `rule`. Parses any atom of any of the following auras after a leading
67sig: `@da`, `@dr`, `@p`, and `@t`. Produces a `dime`.
69#### Source
71```hoon
72++ crub
73 ~+
74 ;~ pose
75 (cook |=(det=date `dime`[%da (year det)]) when)
76 ::
77 %+ cook
78 |= [a=(list [p=?(%d %h %m %s) q=@]) b=(list @)]
79 =+ rop=`tarp`[0 0 0 0 b]
80 |- ^- dime
81 ?~ a
82 [%dr (yule rop)]
83 ?- p.i.a
84 %d $(a t.a, d.rop (add q.i.a d.rop))
85 %h $(a t.a, h.rop (add q.i.a h.rop))
86 %m $(a t.a, m.rop (add q.i.a m.rop))
87 %s $(a t.a, s.rop (add q.i.a s.rop))
88 ==
89 ;~ plug
90 %+ most
91 dot
92 ;~ pose
93 ;~(pfix (just 'd') (stag %d dim:ag))
94 ;~(pfix (just 'h') (stag %h dim:ag))
95 ;~(pfix (just 'm') (stag %m dim:ag))
96 ;~(pfix (just 's') (stag %s dim:ag))
97 ==
98 ;~(pose ;~(pfix ;~(plug dot dot) (most dot qix:ab)) (easy ~))
99 ==
100 ::
101 (stag %p fed:ag)
102 ;~(pfix dot (stag %ta urs:ab))
103 ;~(pfix sig (stag %t urx:ab))
104 ;~(pfix hep (stag %c (cook taft urx:ab)))
105 ==
106```
108#### Examples
110```
111> (scan "1926.5.12" crub:so)
112[p=~.da q=170.141.184.449.747.016.871.285.095.307.149.312.000]
114> ;;([%da @da] (scan "1926.5.12" crub:so))
115[%da ~1926.5.12]
117> (scan "s10" crub:so)
118[p=~.dr q=184.467.440.737.095.516.160]
120> ;;([%dr @dr] (scan "s10" crub:so))
121[%dr ~s10]
123> (scan "sampel" crub:so)
124[%p 1.135]
126> (scan ".mas" crub:so)
127[%ta 7.561.581]
128```
130---
132### `++nuck:so`
134Top-level coin parser
136Parsing `rule`. Switches on the first character and applies the
137corresponding `coin` parser.
139#### Source
141```hoon
142++ nuck
143 ~/ %nuck |= a=nail %. a
144 %+ knee *coin |. ~+
145 %- stew
146 ^. stet ^. limo
147 :~ :- ['a' 'z'] (cook |=(a=@ta [%$ %tas a]) sym)
148 :- ['0' '9'] (stag %$ bisk)
149 :- '-' (stag %$ tash)
150 :- '.' ;~(pfix dot perd)
151 :- '~' ;~(pfix sig ;~(pose twid (easy [%$ %n 0])))
152 ==
153```
155#### Examples
157```
158> (scan "~pillyt" nuck:so)
159[%$ p=[p=~.p q=13.184]]
161> (scan "0x12" nuck:so)
162[%$ p=[p=~.ux q=18]]
164> (scan ".127.0.0.1" nuck:so)
165[%$ p=[p=~.if q=2.130.706.433]]
167> (scan "._20_0w25_sam__" nuck:so)
168[ %many
169 p
170 ~[
171 [%$ p=[p=~.ud q=20]]
172 [%$ p=[p=~.uw q=133]]
173 [%$ p=[p=~.tas q=7.168.371]]
174 ]
176```
178---
180### `++nusk:so`
182Parse coin literal with escapes
184Parsing `rule`. Parses a coin literal with escapes.
186#### Source
188```hoon
189++ nusk
190 ~+
191 :(sear |=(a=@ta (rush a nuck)) wick urt:ab)
192```
194#### Examples
196```
197> ~.asd_a
198~.asd_a
200> ._1_~~.asd~-a__
201[1 ~.asd_a]
203> (scan "~~.asd~-a" nusk:so)
204[%$ p=[p=~.ta q=418.212.246.369]]
205```
207---
209### `++perd:so`
211Parsing coin literal without prefixes
213Parsing `rule`. Parses a dime or tuple without their respective standard
214prefixes.
216#### Source
218```hoon
219++ perd
220 ~+
221 ;~ pose
222 (stag %$ zust)
223 (stag %many (ifix [cab ;~(plug cab cab)] (more cab nusk)))
224 ==
225```
227#### Examples
229```
230> (scan "y" perd:so)
231[%$ [%f %.y]]
233> (scan "n" perd:so)
234[%$ [%f %.n]]
236> (scan "_20_x__" perd:so)
237[%many [[%$ p=[p=~.ud q=20]] [i=[%$ p=[p=~.tas q=120]] t=~]]]
238```
240---
242### `++royl:so`
244Parse dime float
246Parsing `rule`. Parses a number into a `dime` float.
248#### Source
250```hoon
251++ royl
252 ~+
253 ;~ pose
254 (stag %rh royl-rh)
255 (stag %rq royl-rq)
256 (stag %rd royl-rd)
257 (stag %rs royl-rs)
258 ==
259```
261#### Examples
263```
264> (scan "~3.14" royl:so)
265[%rd .~3.14]
267> (scan "3.14" royl:so)
268[%rs .3.14]
269```
271---
273### `++royl-rh:so`
275Parse half-precision float
277Parsing `rule`. Parses a `@rh`.
279#### Source
281```hoon
282++ royl-rh (cook rylh ;~(pfix ;~(plug sig sig) (cook royl-cell royl-rn)))
283```
285#### Examples
287```
288> (scan "~~3.14" royl-rh:so)
289.~~3.14
290```
292---
294### `++royl-rq:so`
296Parse quad-precision float
298Parsing `rule`. Parses a `@rq`.
300#### Source
302```hoon
303++ royl-rq (cook rylq ;~(pfix ;~(plug sig sig sig) (cook royl-cell royl-rn)))
304```
306#### Examples
308```
309> (scan "~~~3.14" royl-rq:so)
310.~~~3.14
311```
313---
315### `++royl-rd:so`
317Parse double-precision float
319Parsing `rule`. Parses a `@rd`.
321#### Source
323```hoon
324++ royl-rd (cook ryld ;~(pfix sig (cook royl-cell royl-rn)))
325```
327#### Examples
329```
330> (scan "~3.14" royl-rd:so)
331.~3.14
332```
334---
336### `++royl-rs:so`
338Parse single-precision float
340Parsing `rule`. Parses a `@rs`.
342#### Source
344```hoon
345++ royl-rs (cook ryls (cook royl-cell royl-rn))
346```
348#### Examples
350```
351> (scan "3.14" royl-rs:so)
352.3.14
353```
355---
357### `++royl-rn:so`
359Parse real number
361Parsing `rule`. Parses a real number to a [`++rn`](/docs/hoon/reference/stdlib/3b#rn).
363#### Source
365```hoon
366++ royl-rn
367 =/ moo
368 |= a=tape
369 :- (lent a)
370 (scan a (bass 10 (plus sid:ab)))
371 ;~ pose
372 ;~ plug
373 (easy %d)
374 ;~(pose (cold | hep) (easy &))
375 ;~ plug dim:ag
376 ;~ pose
377 ;~(pfix dot (cook moo (plus (shim '0' '9'))))
378 (easy [0 0])
379 ==
380 ;~ pose
381 ;~ pfix
382 (just 'e')
383 ;~(plug ;~(pose (cold | hep) (easy &)) dim:ag)
384 ==
385 (easy [& 0])
386 ==
387 ==
388 ==
389 ::
390 ;~ plug
391 (easy %i)
392 ;~ sfix
393 ;~(pose (cold | hep) (easy &))
394 (jest 'inf')
395 ==
396 ==
397 ::
398 ;~ plug
399 (easy %n)
400 (cold ~ (jest 'nan'))
401 ==
402 ==
403```
405#### Examples
407```
408> (scan "3.14" royl-rn:so)
409[%d %.y 3 [2 14] [%.y 0]]
411> (scan "-3.14e-39" royl-rn:so)
412[%d %.n 3 [2 14] [%.n 39]]
414> (scan "3" royl-rn:so)
415[%d %.y 3 [0 0] [%.y 0]]
416```
418---
420### `++royl-cell:so`
422Convert rn to dn
424Intermediate parsed float converter. Convert a
425[`++rn`](/docs/hoon/reference/stdlib/3b#rn) to
426[`++dn`](/docs/hoon/reference/stdlib/3b#dn).
428#### Accepts
430A `++rn`.
432#### Produces
434A `++dn`.
436#### Source
438```hoon
439++ royl-cell
440 |= rn
441 ^- dn
442 ?. ?=([%d *] +<) +<
443 =+ ^= h
444 (dif:si (new:si f.b i.b) (sun:si d.b))
445 [%d a h (add (mul c.b (pow 10 d.b)) e.b)]
446```
448#### Examples
450```
451> (royl-cell:so (scan "3.14" royl-rn:so))
452[%d s=%.y e=-2 a=314]
454> (ryls (royl-cell:so (scan "3.14" royl-rn:so)))
455.3.14
456```
458---
460### `++tash:so`
462Parse signed dime
464Parsing `rule`. Parse a `@s` to a `dime`.
466#### Source
468```hoon
469++ tash
470 ~+
471 =+ ^= neg
472 |= [syn=? mol=dime] ^- dime
473 ?> =('u' (end 3 p.mol))
474 [(cat 3 's' (rsh 3 p.mol)) (new:si syn q.mol)]
475 ;~ pfix hep
476 ;~ pose
477 (cook |=(a=dime (neg | a)) bisk)
478 ;~(pfix hep (cook |=(a=dime (neg & a)) bisk))
479 ==
480 ==
481```
483#### Examples
485```
486> (scan "-20" tash:so)
487[p=~.sd q=39]
489> ;;([%sd @sd] (scan "-20" tash:so))
490[%sd -20]
492> ;;([%sd @sd] (scan "--20" tash:so))
493[%sd --20]
495> ;;([%sx @sx] (scan "--0x2e" tash:so))
496[%sx --0x2e]
497```
499---
501### `++twid:so`
503Parse coins without `~` prefix
505Parsing rule. Parses coins after a leading sig, `~`.
507#### Source
509```hoon
510++ twid
511 ~+
512 ;~ pose
513 %+ stag %blob
514 %+ sear |=(a=@ (mole |.((cue a))))
515 ;~(pfix (just '0') vum:ag)
516 ::
517 (stag %$ crub)
518 ==
519```
521#### Examples
523```
524> (scan "zod" twid:so)
525[%$ [%p 0]]
527> (scan ".sam" twid:so)
528[%$ [%ta 7.168.371]]
530> (scan "0ph" twid:so)
531[%blob [1 1]]
532```
534---
536### `++when:so`
538Parse date
540Parsing `rule`. Parse a `@da`-formatted date string (sans the leading `~`) to a
541`date`.
543#### Source
545```hoon
546++ when
547 ~+
548 ;~ plug
549 %+ cook
550 |=([a=@ b=?] [b a])
551 ;~(plug dim:ag ;~(pose (cold | hep) (easy &)))
552 ;~(pfix dot mot:ag) :: month
553 ;~(pfix dot dip:ag) :: day
554 ;~ pose
555 ;~ pfix
556 ;~(plug dot dot)
557 ;~ plug
558 dum:ag
559 ;~(pfix dot dum:ag)
560 ;~(pfix dot dum:ag)
561 ;~(pose ;~(pfix ;~(plug dot dot) (most dot qix:ab)) (easy ~))
562 ==
563 ==
564 (easy [0 0 0 ~])
565 ==
566 ==
567```
569#### Examples
571```
572> `date`(scan "2000.1.1..12.00.00..ffff" when:so)
573[[a=%.y y=2.000] m=1 t=[d=1 h=12 m=0 s=0 f=~[0xffff]]]
574```
576---
578### `++zust:so`
580Parse dimes from `@i`, `@f`, `@r` or `@q`
582Parsing rule. Parses an atom of either `@if` (IP address), `@f` (loobean), `@r`
583(floating point) into a `dime`. The `@q` alone requires a leading `~`.
585#### Source
587```hoon
588++ zust
589 ~+
590 ;~ pose
591 (stag %is bip:ag)
592 (stag %if lip:ag)
593 royl
594 (stag %f ;~(pose (cold & (just 'y')) (cold | (just 'n'))))
595 (stag %q ;~(pfix sig feq:ag))
596 ==
597```
599#### Examples
601```
602> (scan "~sampel" zust:so)
603[%q 1.135]
605> (scan "y" zust:so)
606[%f %.y]
608> (scan "127.0.0.1" zust:so)
609[%if 2.130.706.433]
611> (scan "af.0.0.0.0.e7a5.30d2.7" zust:so)
612[%is 908.651.950.243.594.834.993.091.554.288.205.831]
614> (scan "12.09" zust:so)
615[%rs .12.09]
616```
618---