urbit/gen/marc.hoon

230 lines
7.5 KiB
Plaintext
Raw Normal View History

2017-07-16 03:59:57 +03:00
::
:::: hoon/marc/gen
::
2017-07-18 04:07:47 +03:00
:- %say
|= *
:- %noun
2017-07-16 03:59:57 +03:00
=> |%
2017-07-18 04:07:47 +03:00
++ item (pair mite (list flow)) :: xml node generator
2017-07-16 03:59:57 +03:00
++ colm @ud :: column
2017-07-18 04:07:47 +03:00
++ flow (each item twig) :: node or generator
2017-07-16 03:59:57 +03:00
++ form (unit $?($emph $bold $code)) :: formatting
2017-07-18 04:07:47 +03:00
++ mite :: context
$? $down :: outer embed
$flow :: regular flow; div
$list :: unordered list
$lime :: list item
$lord :: ordered list
2017-07-19 06:19:27 +03:00
$poem :: verse
2017-07-18 04:07:47 +03:00
$bloc :: blockquote
$code :: preformatted code
2017-07-19 06:19:27 +03:00
$expr :: dynamic expression
2017-07-18 04:07:47 +03:00
==
2017-07-16 03:59:57 +03:00
++ trig :: line style
$: col/@ud :: start column
$= sty :: style
$? $done :: \/ terminator
$none :: end of input
$lint :: + line item
$lite :: - line item
$head :: # heading
$text :: anything else
== == ::
++ word @t :: source text
--
|%
++ parse
|= {naz/hair los/tape}
^- (like item)
::
2017-07-18 04:07:47 +03:00
:: err: error position
2017-07-16 03:59:57 +03:00
:: col: current control column
:: hac: stack of items under construction
:: cur: current item under construction
:: lub: current line stack
::
2017-07-18 04:07:47 +03:00
=| err/(unit hair)
=/ col q.naz
2017-07-16 03:59:57 +03:00
=| hac/(list item)
2017-07-18 04:07:47 +03:00
=/ cur/item [%flow ~]
2017-07-16 03:59:57 +03:00
=| lub/(unit (pair hair (list tape)))
2017-07-18 04:07:47 +03:00
=< $:line
2017-07-16 03:59:57 +03:00
|%
:: ::
++ $ :: complete parse
2017-07-18 04:07:47 +03:00
^- (like item)
?^ err [u.err ~]
2017-07-19 06:19:27 +03:00
=- [naz `[- [naz los]]]
2017-07-16 03:59:57 +03:00
=. hac [cur hac]
?~ hac !!
|- ^- item
:: complete assembly by inverting stack
::
=/ dis/item [p.i.hac (flop q.i.hac)]
::
:: add as last entry in parent, or return
::
?~ t.hac dis
2017-07-19 06:19:27 +03:00
$(i.hac `item`i.t.hac(q [[%& dis] q.i.t.hac]), t.hac `(list item)`t.t.hac)
2017-07-16 03:59:57 +03:00
:: ::
++ back :: column retreat
2017-07-18 04:07:47 +03:00
|= bac/@ud
^+ +>
2017-07-16 03:59:57 +03:00
!!
:: ::
2017-07-18 04:07:47 +03:00
++ snap :: capture line
2017-07-16 03:59:57 +03:00
=| nap/tape
2017-07-19 06:19:27 +03:00
^+ [nap +]
2017-07-16 03:59:57 +03:00
!!
:: ::
2017-07-18 04:07:47 +03:00
++ skip +:snap :: discard line
2017-07-19 06:19:27 +03:00
++ look :: inspect line
^- (unit trig)
!!
2017-07-16 03:59:57 +03:00
++ made :: finish paragraph
^+ .
!!
2017-07-18 04:07:47 +03:00
::
:: ++ expr
2017-07-16 03:59:57 +03:00
2017-07-18 04:07:47 +03:00
:: sab: rule for embedded twig
::
:: =* sab (ifix [gay ;~(plug (star ace) (just `@`10))] tall:vast)
:: vex: product of parsing following twig
::
:: =/ vex/(like twig) (sab naz los)
:: fail upward if parse failed
::
:: ?~ q.vex ..^$(err `p.vex)
:: otherwise, add item and continue
:: ::
:: %= $
:: naz p.q.u.vex
:: los q.q.u.vex
:: lap :_(lap [%| %marl p.u.vex])
:: ==
2017-07-16 03:59:57 +03:00
:: ::
2017-07-18 04:07:47 +03:00
++ line ^+ . :: body line loop
2017-07-16 03:59:57 +03:00
:: abort after first error
::
2017-07-19 06:19:27 +03:00
?: !=(~ err) .
2017-07-16 03:59:57 +03:00
:: pic: profile of this line
::
=/ pic look
:: if line is blank
::
2017-07-19 06:19:27 +03:00
?~ pic
2017-07-16 03:59:57 +03:00
:: break section
::
2017-07-19 06:19:27 +03:00
line:made:skip
2017-07-16 03:59:57 +03:00
:: line is not blank
::
=> .(pic u.pic)
2017-07-18 04:07:47 +03:00
:: if end of input, complete
2017-07-16 03:59:57 +03:00
::
2017-07-18 04:07:47 +03:00
?: ?=($none sty.pic)
..$(q.naz col.pic)
:: if end marker behind current column
2017-07-16 03:59:57 +03:00
::
2017-07-19 06:19:27 +03:00
?: &(?=($done sty.pic) (lth col.pic col))
2017-07-18 04:07:47 +03:00
:: retract and complete
2017-07-16 03:59:57 +03:00
::
2017-07-18 04:07:47 +03:00
(back(q.naz (add 2 col.pic)) col.pic)
2017-07-19 06:19:27 +03:00
:: bal: inspection copy of lub, current section
::
=/ bal lub
2017-07-18 04:07:47 +03:00
:: if within section
2017-07-16 03:59:57 +03:00
::
2017-07-19 06:19:27 +03:00
?^ bal
2017-07-18 04:07:47 +03:00
:: detect bad block structure
2017-07-16 03:59:57 +03:00
::
2017-07-18 04:07:47 +03:00
?: ?| ?=($head sty.pic)
?: ?=(?($code $poem $expr) p.cur)
(lth col.pic col)
2017-07-19 06:19:27 +03:00
|(!=(%text sty.pic) !=(col.pic col))
2017-07-18 04:07:47 +03:00
==
..$(err `[p.naz col.pic])
:: accept line and continue
::
2017-07-19 06:19:27 +03:00
=^ nap ..line snap
line(lub bal(q.u [nap q.u.bal]))
2017-07-18 04:07:47 +03:00
:: if column has retreated, adjust stack
2017-07-16 03:59:57 +03:00
::
2017-07-18 04:07:47 +03:00
=. ..$ ?: (lth col.pic col) ..$
(back col.pic)
:: dif: columns advanced
2017-07-19 06:19:27 +03:00
:: erp: error position
2017-07-16 03:59:57 +03:00
::
2017-07-18 04:07:47 +03:00
=/ dif (sub col.pic col)
2017-07-19 06:19:27 +03:00
=/ erp [p.naz col.pic]
2017-07-18 04:07:47 +03:00
=. col col.pic
:: nap: take first line
::
=^ nap ..$ snap
:: execute appropriate paragraph form
::
2017-07-19 19:53:34 +03:00
=< line:abet:apex
2017-07-18 04:07:47 +03:00
|%
:: ::
++ abet :: accept line
..$(lub `[naz nap ~])
:: ::
2017-07-19 06:19:27 +03:00
++ apex ^+ . :: by column offset
2017-07-18 04:07:47 +03:00
?+ dif fail
$0 apse
$2 expr
$4 code
$6 bloc
$8 poem
==
:: ::
2017-07-19 06:19:27 +03:00
++ apse ^+ . :: by prefix style
2017-07-18 04:07:47 +03:00
?- sty.pic
2017-07-19 06:19:27 +03:00
$done !!
2017-07-18 04:07:47 +03:00
$head head
$lite lite
$lint lint
$text text
==
:: ::
++ bloc apse:(push %bloc) :: blockquote line
2017-07-19 06:19:27 +03:00
++ fail .(err `erp) :: set error position
++ push |=(mite %_(+> hac [cur hac], cur [+< ~])) :: deeper stack
2017-07-18 04:07:47 +03:00
++ expr (push %expr) :: hoon expression
++ code (push %code) :: code literal
++ poem (push %poem) :: verse literal
++ head !!
++ lent :: list entry
|= ord/?
2017-07-19 06:19:27 +03:00
^+ +>
2017-07-18 04:07:47 +03:00
:: erase list marker
::
=. nap =+(+(col) (runt [- ' '] (slag - nap)))
:: indent by 2
::
=. col (add 2 col)
:: can't switch list types
::
?: =(?:(ord %list %lord) p.cur) fail
:: if not already in list, start list
::
=+ ?:(ord %lord %list)
2017-07-19 06:19:27 +03:00
?: =(- p.cur)
(push %lime)
(push:(push -) %lime)
2017-07-18 04:07:47 +03:00
::
++ lint (lent &) :: numbered list
++ lite (lent |) :: unnumbered list
++ text :: plain text
2017-07-19 06:19:27 +03:00
^+ .
2017-07-18 04:07:47 +03:00
:: except in lists, continue in current flow
::
?. ?=(?($list $lord) p.cur) .
:: in lists, finish current and switch to text
::
?> ?=(^ hac)
2017-07-19 06:19:27 +03:00
.(hac [[p.i.hac [[%& cur] q.i.hac]] t.hac], cur [%flow ~])
2017-07-18 04:07:47 +03:00
--
2017-07-16 03:59:57 +03:00
--
2017-07-18 04:07:47 +03:00
--