dojo, auto: move insert-magic logic to lib/auto

This changes the entry-points in lib/auto so that clients never have to
handle magic-spoon.  You can specify either a tape of code with a
position index or a preparsed hoon (presumably you ran +insert-magic
before parsing).
This commit is contained in:
Philip Monk 2019-10-31 14:56:54 -07:00
parent 4482997a16
commit ee63e122ea
No known key found for this signature in database
GPG Key ID: B66E1F02604E44EC
2 changed files with 61 additions and 52 deletions

View File

@ -1100,31 +1100,13 @@
|= pos=@ud |= pos=@ud
^+ +> ^+ +>
=* res +> =* res +>
=/ buf (tufa buf.say) =+ [beg-pos=@ud txt=tape]=(insert-magic:auto pos (tufa buf.say))
:: Find beg-pos by searching backward to where the current term
:: begins
=+ ^- [id=(unit term) *]
(scan `tape`(flop (scag pos buf)) ;~(plug (punt sym) (star prn)))
=/ beg-pos
?~ id
pos
(sub pos (met 3 u.id))
=/ txt=tape
;: weld
(scag beg-pos buf)
"magic-spoon"
?~ id
""
"."
(slag beg-pos buf)
"\0a"
==
=+ vex=((full parse-command-line:he-parser) [1 1] txt) =+ vex=((full parse-command-line:he-parser) [1 1] txt)
?. ?=([* ~ [* @ %ex *] *] vex) ?. ?=([* ~ [* @ %ex *] *] vex)
res res
=/ typ p:(slop q:he-hoon-head !>(..dawn)) =/ typ p:(slop q:he-hoon-head !>(..dawn))
=/ tl (tablist:auto typ p.q.q.p.u.q.vex) =/ tl (tab-list-hoon:auto typ p.q.q.p.u.q.vex)
=/ advance (autoadvance:auto typ p.q.q.p.u.q.vex) =/ advance (advance-hoon:auto typ p.q.q.p.u.q.vex)
=? res ?=(^ advance) =? res ?=(^ advance)
=/ to-send (trip (rsh 3 (sub pos beg-pos) u.advance)) =/ to-send (trip (rsh 3 (sub pos beg-pos) u.advance))
=| fxs=(list sole-effect) =| fxs=(list sole-effect)

View File

@ -2,6 +2,9 @@
:: ::
|% |%
+$ ids (list [=term =type]) +$ ids (list [=term =type])
::
:: Get all the identifiers accessible if this type is your subject.
::
++ get-identifiers ++ get-identifiers
|= ty=type |= ty=type
%- flop %- flop
@ -42,6 +45,8 @@
[%hold *] $(ty ~(repo ut ty)) [%hold *] $(ty ~(repo ut ty))
== ==
:: ::
:: Get all the identifiers that start with sid.
::
++ search-prefix ++ search-prefix
|= [sid=term =ids] |= [sid=term =ids]
^- (list [term type]) ^- (list [term type])
@ -49,6 +54,8 @@
|= [id=term ty=type] |= [id=term ty=type]
=(sid (end 3 (met 3 sid) id)) =(sid (end 3 (met 3 sid) id))
:: ::
:: Get the longest prefix of a list of identifiers.
::
++ longest-match ++ longest-match
|= matches=(list [=term =type]) |= matches=(list [=term =type])
^- term ^- term
@ -68,6 +75,9 @@
$(n +(n)) $(n +(n))
(end 3 (dec n) term.i.matches) (end 3 (dec n) term.i.matches)
:: ::
:: Run +find-type safely, printing the first line of the stack trace on
:: error.
::
++ find-type-mule ++ find-type-mule
|= [sut=type gen=hoon] |= [sut=type gen=hoon]
^- (unit [term type]) ^- (unit [term type])
@ -190,49 +200,66 @@
^- (unit [term type]) ^- (unit [term type])
!! !!
:: ::
++ replace-hax :: Insert magic marker in hoon source at the given position.
|= code=tape ::
%+ scan code ++ insert-magic
%+ cook |= [pos=@ud txt=tape]
|= res=(list $@(@ [~ (unit term)])) ^- [beg-pos=@ud txt=tape]
%- trip :: Find beg-pos by searching backward to where the current term
%- crip :: begins
%+ turn res
|= elem=$@(@ [~ (unit term)])
?@ elem
elem
?~ +.elem
'magic-spoon'
(cat 3 'magic-spoon.' u.+.elem)
%- star
;~ pose
;~ less
hax
prn
==
:: ::
(stag ~ ;~(pfix hax (punt sym))) =+ ^- [id=(unit term) *]
(scan `tape`(flop (scag pos txt)) ;~(plug (punt sym) (star prn)))
=/ beg-pos
?~ id
pos
(sub pos (met 3 u.id))
:- beg-pos
:: Insert "magic-spoon" marker so +find-type can identify where to
:: stop.
::
;: weld
(scag beg-pos txt)
"magic-spoon"
?~ id
""
"."
(slag beg-pos txt)
"\0a"
== ==
:: ::
++ autoadvance :: Produce the longest possible advance without choosing between
:: matches.
::
:: Takes a +hoon which has already has a magic-spoon marker. Useful if
:: you want to handle your own parsing.
::
++ advance-hoon
|= [sut=type gen=hoon] |= [sut=type gen=hoon]
%+ bind (find-type-mule sut gen) %+ bind (find-type-mule sut gen)
|= [id=term typ=type] |= [id=term typ=type]
(longest-match (search-prefix id (get-identifiers typ))) (longest-match (search-prefix id (get-identifiers typ)))
:: ::
++ auto-advance :: Same as +advance-hoon, but takes a position and text directly.
|= [sut=type code=cord]
(autoadvance sut (scan (replace-hax (trip code)) vest))
:: ::
++ tablist ++ advance-tape
|= [sut=type pos=@ud code=tape]
(advance-hoon sut (scan txt:(insert-magic pos code) vest))
::
:: Produce a list of matches.
::
:: Takes a +hoon which has already has a magic-spoon marker. Useful if
:: you want to handle your own parsing.
::
++ tab-list-hoon
|= [sut=type gen=hoon] |= [sut=type gen=hoon]
%+ bind (find-type-mule sut gen) %+ bind (find-type-mule sut gen)
|= [id=term typ=type] |= [id=term typ=type]
(search-prefix id (get-identifiers typ)) (search-prefix id (get-identifiers typ))
:: ::
++ tab-list :: Same as +advance-hoon, but takes a position and text directly.
|= [sut=type code=cord] ::
%+ bind (find-type sut (scan (replace-hax (trip code)) vest)) ++ tab-list-tape
|= [id=term typ=type] |= [sut=type pos=@ud code=tape]
(search-prefix id (get-identifiers typ)) (tab-list-hoon sut (scan txt:(insert-magic pos code) vest))
-- --