urbit/gen/marc.hoon

229 lines
7.7 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
$bloc :: blockquote
$code :: preformatted code
==
2017-07-16 03:59:57 +03:00
++ mode :: inclusion mode
$? $cord :: ++cord (hard string)
$manx :: ++manx (XML node)
$marl :: ++marl (XML nodes)
$tape :: ++tape (soft string)
$text :: (list cord)
$wall :: (list tape)
== ::
++ 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-16 03:59:57 +03:00
:- naz
:- ~
=. 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
$(i.hac i.t.hac(q [dis q.i.t.hac]), t.hac t.t.hac)
:: ::
++ 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-18 04:07:47 +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-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-18 04:07:47 +03:00
?^ err .
2017-07-16 03:59:57 +03:00
:: pic: profile of this line
::
=/ pic look
:: if line is blank
::
?~ u.pic
:: break section
::
2017-07-18 04:07:47 +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-18 04:07:47 +03:00
?: &(?=($done col.pic) (lth col.pic col))
:: 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)
:: if within section
2017-07-16 03:59:57 +03:00
::
2017-07-18 04:07:47 +03:00
?^ lub
:: 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)
|(!=($text sty.pic) !=(col.pic col))
==
..$(err `[p.naz col.pic])
:: accept line and continue
::
=^ nap ..$ snap
line(lub lub(q.u [nap q.u.lub]))
:: 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
:: erz: error position
2017-07-16 03:59:57 +03:00
::
2017-07-18 04:07:47 +03:00
=/ dif (sub col.pic col)
=/ erz [p.naz col.pic]
=. col col.pic
:: nap: take first line
::
=^ nap ..$ snap
:: execute appropriate paragraph form
::
=< line(..$ abet:apex)
|%
:: ::
++ abet :: accept line
..$(lub `[naz nap ~])
:: ::
++ apex :: by column offset
?+ dif fail
$0 apse
$2 expr
$4 code
$6 bloc
$8 poem
==
:: ::
++ apse :: by prefix style
?- sty.pic
$head head
$lite lite
$lint lint
$text text
==
:: ::
++ bloc apse:(push %bloc) :: blockquote line
++ fail ..$(err `erz) :: set error position
++ push |=(mite +>(hac [cur hac], cur [+< ~])) :: deeper stack
++ expr (push %expr) :: hoon expression
++ code (push %code) :: code literal
++ poem (push %poem) :: verse literal
++ head !!
++ lent :: list entry
|= ord/?
:: 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
:: push item context
::
=< (push %lime)
:: if not already in list, start list
::
=+ ?:(ord %lord %list)
?:(=(- p.cur) . (push -))
::
++ lint (lent &) :: numbered list
++ lite (lent |) :: unnumbered list
++ text :: plain text
:: except in lists, continue in current flow
::
?. ?=(?($list $lord) p.cur) .
:: in lists, finish current and switch to text
::
?> ?=(^ hac)
.(hac [[p.i.hac [cur q.i.hac]]], cur [%text ~])
--
2017-07-16 03:59:57 +03:00
--
2017-07-18 04:07:47 +03:00
--