on 492ae2f help sign in
715 lines 11 kB history raw
1### `++poja`
3JSON parser core
5JSON parser core: parses a [`++cord`]() `a` to the hoon structure for JSON,
6a [`++json`]().
8Accepts
9-------
11`a` is [`++cord`]().
13Produces
14--------
17A `(like json)`.
19Source
20------
22 ++ poja :: JSON parser core
23 =< |=(a=cord (rush a apex))
24 |%
26Examples
27--------
29 ~zod/try=> (poja '[1,2,3]')
30 [~ [%a p=~[[%n p=~.1] [%n p=~.2] [%n p=~.3]]]]
31 ~zod/try=> (poja 'null')
32 [~ ~]
33 ~zod/try=> (poja 'invalid{json')
34 ~
36### `++tops`
38Parse object
40Top level parsing [`++rule`](). Parses either a single JSON object, or an array
41of JSON objects to a [`++json`](). See also: [`++abox`](), [`++obox`]().
43Accepts
44-------
46A [`++nail`]().
48Produces
49--------
51A `(like json)`.
53Source
54------
56 ++ apex ;~(pose abox obox) :: JSON object
58Examples
59--------
61 ~zod/try=> (rash '[1,2]' apex:poja)
62 [%a p=~[[%n p=~.1] [%n p=~.2]]]
63 ~zod/try=> (rash '{"sam": "kot"}' apex:poja)
64 [%o p={[p=~.sam q=[%s p=~.kot]]}]
65 ~zod/try=> (rash 'null' apex:poja)
66 ! {1 1}
67 ! exit
69### `++apex`
71Parse value
73Parsing [`++rule`](). Parses JSON values to [`++json`]().
75Accepts
76-------
78A [`++nail`]().
80Produces
81--------
83A `(like json)`.
85Source
86------
88 ++ valu :: JSON value
89 %+ knee *json |. ~+
90 ;~ pfix spac
91 ;~ pose
92 (cold ~ (jest 'null'))
93 (jify %b bool)
94 (jify %s stri)
95 (cook |=(s=tape [%n p=(rap 3 s)]) numb)
96 abox
97 obox
98 ==
99 ==
101Examples
102--------
104 ~zod/try=> (rash '[1,2]' valu:poja)
105 [%a p=~[[%n p=~.1] [%n p=~.2]]]
106 ~zod/try=> (rash '{"sam": "kot"}' valu:poja)
107 [%o p={[p='sam' q=[%s p=~.kot]]}]
108 ~zod/try=> (rash 'null' valu:poja)
109 ~
110 ~zod/try=> (rash '20' valu:poja)
111 [%n p=~.20]
112 ~zod/try=> (rash '"str"' valu:poja)
113 [%s p=~.str]
114 ~zod/try=> (rash 'true' valu:poja)
115 [%b p=%.y]
117### `++abox`
119Parse array
121Parsing rule. Parses a JSON array with values enclosed within `[]` and
122delimited by a `,`.
124Accepts
125-------
127A [`++nail`]().
129Produces
130--------
132A `(like json)`.
134Source
135------
137 ++ abox (stag %a (ifix [sel (ws ser)] (more (ws com) valu)))
139Examples
140--------
142 ~zod/try=> (rash '[1, 2,4]' abox:poja)
143 [[%n p=~.1] ~[[%n p=~.2] [%n p=~.4]]]
145JSON Objects
146------------
148### `++pair`
150Parse key value pair
152Parsing rule. Parses a [`++json`]() from a JSON key-value pair of a
153string and value delimited by `:`.
155Accepts
156-------
158A [`++nail`]().
160Produces
161--------
163A `(like json)`.
165Source
166------
168 ++ pair ;~(plug ;~(sfix (ws stri) (ws col)) valu)
170Examples
171--------
173 ~zod/try=> (rash '"ham": 2' pair:poja)
174 ['ham' [%n p=~.2]]
176### `++obje`
178Parse array of objects
180Parsing rule. Parses a [`++json`]() from an array of JSON object
181key-value pairs that are enclosed within `{}` and separated by `,`.
183Accepts
184-------
186A [`++nail`]().
188Produces
189--------
191A `(like ,[%o p=(map ,@t json)])`
193Source
194------
196 ++ obje (ifix [(ws kel) (ws ker)] (more (ws com) pair))
198Examples
199--------
201 ~zod/try=> (rash '{"ham": 2, "lam":true}' obje:poja)
202 [['ham' [%n p=~.2]] ~[['lam' [%b p=%.y]]]]
204### `++obox`
206Parse boxed object
208Parsing rule. Parses an array of JSON objects to an object of [`++json`](). See also: [`++json`]().
210Accepts
211-------
213A [`++nail`]().
215Produces
216--------
218A `(like ,[%o p=(map ,@t json)])`
220Source
221------
223 ++ obox (stag %o (cook mo obje))
225Examples
226--------
228 ~zod/try=> (rash '{"ham": 2, "lam":true}' obox:poja)
229 [%o {[p='lam' q=[%b p=%.y]] [p='ham' q=[%n p=~.2]]}]
231JSON Booleans
232-------------
234### `++bool`
236Parse boolean
238Parsing rule. Parses a string of either `true` or `false` to a
239[`++json`]() boolean.
242Accepts
243-------
245A [`++nail`]().
247Produces
248--------
251A `(like ,[%b p=?])`
253Source
254------
256 ++ bool ;~(pose (cold & (jest 'true')) (cold | (jest 'false')))
258Examples
259--------
261 ~zod/try=> (rash 'true' bool:poja)
262 %.y
263 ~zod/try=> (rash 'false' bool:poja)
264 %.n
265 ~zod/try=> (rash 'null' bool:poja)
266 ! {1 1}
267 ! exit
269JSON strings
270------------
272### `++stri`
274Parse string
276Parsing rule. Parse a string to a [`++cord`](). A JSON string is a list
277of characters enclosed in double quotes along with escaping `\`s, to a
278[`++cord`](). See also [`++jcha`]().
280Accepts
281-------
283A [`++nail`]().
285Produces
286--------
288A `(like ,[%s p=@t])`.
290Source
291------
293 ++ stri
294 (cook crip (ifix [doq doq] (star jcha)))
296Examples
297--------
299 ~zod/try=> (rash '"ham"' stri:poja)
300 'ham'
301 ~zod/try=> (rash '"h\\nam"' stri:poja)
302 'h
303 am'
304 ~zod/try=> (rash '"This be \\"quoted\\""' stri:poja)
305 'This be "quoted"'
307### `++jcha`
309Parse char from string
311Parsing rule. Parses either a literal or escaped character from a JSON
312string to a [`++cord`]().
315Accepts
316-------
318A [`++nail`]().
320Produces
321--------
323A `(like cord)`.
325Source
326------
328 ++ jcha ;~(pose ;~(less doq bas prn) esca) :: character in string
330Examples
331--------
333 ~zod/try=> (rash 'a' jcha:poja)
334 'a'.
335 ~zod/try=> (rash '!' jcha:poja)
336 '!'
337 ~zod/try=> (rash '\\"' jcha:poja)
338 '"'
339 ~zod/try=> (rash '\\u00a4' jcha:poja)
340 '¤'
341 ~zod/try=> (rash '\\n' jcha:poja)
342 '
343 '
345### `++esca`
347Parse escaped char
349Parsing rule. Parses a backslash-escaped special character, low ASCII,
350or UTF16 codepoint, to a [`++cord`]().
353Accepts
354-------
356A [`++nail`]().
358Produces
359--------
361A `(like cord)`.
363Source
364------
366 ++ esca :: Escaped character
367 ;~ pfix bas
368 ;~ pose
369 doq fas soq bas
370 (sear ~(get by `(map ,@t ,@)`(mo b/8 t/9 n/10 f/12 r/13 ~)) low)
371 ;~(pfix (just 'u') (cook tuft qix:ab)) :: 4-digit hex to UTF-8
372 ==
374Examples
375--------
377 ~zod/try=> (rash 'b' esca:poja)
378 ! {1 1}
379 ! exit
380 ~zod/try=> (rash '\n' esca:poja)
381 ~ <syntax error at [1 9]>
382 ~zod/try=> (rash '\\n' esca:poja)
383 '
384 '
385 ~zod/try=> `@`(rash '\\r' esca:poja)
386 13
387 ~zod/try=> (rash '\\u00c4' esca:poja)
388 'Ä'
389 ~zod/try=> (rash '\\u00df' esca:poja)
390 'ß'
392JSON numbers
393------------
395A JSON numbers are stored as cords internally in lieu of full float
396support, so ++numb and subarms are really more *validators* than parsers
397per se.
399### `++numb`
401Parse number
403Parsing rule. Parses decimal numbers with an optional `-`, fractional
404part, or exponent part, to a [`++tape`]().
406Accepts
407-------
409A [`++nail`]().
411Produces
412--------
414An [`++edge`]() of a [`++tape`]().
416Source
417------
419 ++ numb
420 ;~ (comp twel)
421 (mayb (piec hep))
422 ;~ pose
423 (piec (just '0'))
424 ;~(plug (shim '1' '9') digs)
425 ==
426 (mayb frac)
427 (mayb expo)
428 ==
430Examples
431--------
433 ~zod/try=> (rash '0' numb:poja)
434 ~[~~0]
435 ~zod/try=> (rash '1' numb:poja)
436 ~[~~1]
437 ~zod/try=> `tape`(rash '1' numb:poja)
438 "1"
439 ~zod/try=> `tape`(rash '12.6' numb:poja)
440 "12.6"
441 ~zod/try=> `tape`(rash '-2e20' numb:poja)
442 "-2e20"
443 ~zod/try=> `tape`(rash '00e20' numb:poja)
444 ! {1 2}
445 ! exit
447### `++digs`
449Parse 1-9
451Parsing rule. Parses digits `0` through `9` to a [`++tape`]().
453Accepts
454-------
456A [`++nail`]().
458Produces
459--------
461The [`++edge`]() of a `++tape`.
463Source
464------
466 ++ digs (star (shim '0' '9'))
468Examples
469--------
471 ~zod/try=> (rash '' digs:poja)
472 ""
473 ~zod/try=> (rash '25' digs:poja)
474 "25"
475 ~zod/try=> (rash '016' digs:poja)
476 "016"
477 ~zod/try=> (rash '7' digs:poja)
478 "7"
480### `++expo`
482Parse exponent part
484Parsing rule. Parses an exponent to a [`++tape`]() .An exponent is an
485`e` or 'E', followed by an optional `+` or `-`, and then by digits.
487Accepts
488-------
490A [`++nail`]().
492Produces
493--------
495The [`++edge`]() of a `++tape`.
497Source
498------
500 ++ expo :: Exponent part
501 ;~ (comp twel)
502 (piec (mask "eE"))
503 (mayb (piec (mask "+-")))
504 digs
505 ==
507Examples
508--------
510 ~zod/try=> `tape`(rash 'e7' expo:poja)
511 "e7"
512 ~zod/try=> `tape`(rash 'E17' expo:poja)
513 "E17"
514 ~zod/try=> `tape`(rash 'E-4' expo:poja)
515 "E-4"
517### `++frac`
519Fractional part
521Parsing rule. Parses a dot followed by digits to a [`++cord`]().
523Accepts
524-------
526A `++nail`.
528Produces
529--------
531The [`++edge`]() of a `++tape`.
533Source
534------
536 ++ frac ;~(plug dot digs) :: Fractional part
538Examples
539--------
541 ~zod/try=> (rash '.25' frac:poja)
542 [~~~. "25"]
543 ~zod/try=> (rash '.016' frac:poja)
544 [~~~. "016"]
545 ~zod/try=> (rash '.7' frac:poja)
546 [~~~. "7"]
548whitespace
549----------
551### `++spac`
553Parse whitespace
555Parsing rule. Parses a whitespace to a [`++tape`]().
557Accepts
558-------
560A [`++nail`]().
562Produces
563--------
565A `(like tape)`.
567Source
568------
570 ++ spac (star (mask [`@`9 `@`10 `@`13 ' ' ~]))
572Examples
573--------
575 ~zod/try=> (scan "" spac:poja)
576 ""
577 ~zod/try=> (scan " " spac:poja)
578 " "
579 ~zod/try=> `*`(scan `tape`~[' ' ' ' ' ' `@`9 ' ' ' ' `@`13] spac:poja)
580 [32 32 32 9 32 32 13 0]
581 ~zod/try=> (scan " m " spac:poja)
582 ! {1 4}
583 ! exit
585### `++ws`
587Allow prefix whitespace
589Parser modifier. Produces a rule that allows for a whitespace before
590applying `sef`.
592Accepts
593-------
595`sef` is a [`++rule`]().
597Produces
598--------
600A `++rule`.
602Source
603------
605 ++ ws |*(sef=_rule ;~(pfix spac sef))
607Examples
608--------
610 ~zod/try=> (rash ' 4' digs:poja)
611 ! {1 1}
612 ! exit
613 ~zod/try=> (rash ' 4' (ws digs):poja)
614 "4"
615 ~zod/try=> (rash '''
617 4
618 ''' (ws digs):poja)
619 "4"
621Plumbing
622--------
624### `++mayb`
626Maybe parse
628Parser modifier. Need to document, an example showing failure.
630Accepts
631-------
633XX
635Produces
636--------
638XX
640Source
641------
643 ++ mayb |*(bus=_rule ;~(pose bus (easy "")))
645Examples
646--------
648 ~zod/try=> (abox:poja 1^1 "not-an-array")
649 [p=[p=1 q=1] q=~]
650 ~zod/try=> ((mayb abox):poja 1^1 "not-an-array")
651 [p=[p=1 q=1] q=[~ [p="" q=[p=[p=1 q=1] q="not-an-array"]]]]
653### `++twel`
655Weld two tapes
657Concatenates two tapes, `a` and `b`, producing a [`++tape`]().
659Accepts
660-------
662`a` is a [`++tape`]().
664`b` is a [`++tape`]().
666Produces
667--------
669The [`++edge`]() of a `++tape`.
671Source
672------
674 ++ twel |=([a=tape b=tape] (weld a b))
676Examples
677--------
679 ~zod/try=> (twel "sam" "hok"):poja
680 ~[~~s ~~a ~~m ~~h ~~o ~~k]
681 ~zod/try=> (twel "kre" ""):poja
682 ~[~~k ~~r ~~e]
684### `++piec`
686Parse char to list
688Parser modifer. Parses an atom with `bus` and then wraps it in a
689[`++list`]().
691Accepts
692-------
694`bus` is a [`++rule`]().
696Produces
697--------
699A `++rule`.
701Source
702------
704 ++ piec
705 |* bus=_rule
706 (cook |=(a=@ [a ~]) bus)
707 ::
709Examples
710--------
712 d~zod/try=> (scan "4" (piec:poja dem:ag))
713 [4 ~]