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
^+ +>
=* res +>
=/ buf (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"
==
=+ [beg-pos=@ud txt=tape]=(insert-magic:auto pos (tufa buf.say))
=+ vex=((full parse-command-line:he-parser) [1 1] txt)
?. ?=([* ~ [* @ %ex *] *] vex)
res
=/ typ p:(slop q:he-hoon-head !>(..dawn))
=/ tl (tablist:auto typ p.q.q.p.u.q.vex)
=/ advance (autoadvance:auto typ p.q.q.p.u.q.vex)
=/ tl (tab-list-hoon:auto typ p.q.q.p.u.q.vex)
=/ advance (advance-hoon:auto typ p.q.q.p.u.q.vex)
=? res ?=(^ advance)
=/ to-send (trip (rsh 3 (sub pos beg-pos) u.advance))
=| fxs=(list sole-effect)

View File

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