modern: more duplication cleaned up.

modern-harvey3
Doug Coleman 2019-10-16 02:16:58 -05:00
parent 58aacc34bf
commit d1466f2aea
2 changed files with 34 additions and 55 deletions

View File

@ -59,7 +59,7 @@ MACRO:: read-double-matched ( open-ch -- quot: ( string n tag ch -- string n' se
ch { ch {
{ char: = [ { char: = [
tag 1 cut-slice* drop tag! ! tag of (=( is ( here, fix it tag 1 cut-slice* drop tag! ! tag of (=( is ( here, fix it
string n openstr1 slice-til-separator-inclusive [ -1 modify-from ] dip :> ( string' n' opening ch ) string n openstr1 slice-until-include [ -1 modify-from ] dip :> ( string' n' opening ch )
ch open-ch = [ tag openstr2 string n ch long-opening-mismatch ] unless ch open-ch = [ tag openstr2 string n ch long-opening-mismatch ] unless
opening matching-delimiter-string :> needle opening matching-delimiter-string :> needle
@ -133,7 +133,7 @@ MACRO:: read-matched ( ch -- quot: ( string n tag -- string n' slice' ) )
ch dup matching-delimiter { ch dup matching-delimiter {
[ drop "=" swap prefix ] [ drop "=" swap prefix ]
[ nip 1string ] [ nip 1string ]
} 2cleave :> ( openstreq closestr1 ) ! [= ] } 2cleave :> ( openstreq closestr1 ) ! [= ]
|[ string n tag | |[ string n tag |
string n tag string n tag
2over nth-check-eof { 2over nth-check-eof {
@ -142,7 +142,7 @@ MACRO:: read-matched ( ch -- quot: ( string n tag -- string n' slice' ) )
drop dup '[ _ matching-delimiter-string closestr1 2array members lex-until ] dip drop dup '[ _ matching-delimiter-string closestr1 2array members lex-until ] dip
swap unclip-last 3array swap unclip-last 3array
] } ! ( foo ) ] } ! ( foo )
[ drop [ slice-til-whitespace drop ] dip span-slices ] ! (foo) [ drop [ slice-til-whitespace drop ] dip span-slices ] ! (foo)
} cond } cond
] ; ] ;
@ -151,7 +151,7 @@ MACRO:: read-matched ( ch -- quot: ( string n tag -- string n' slice' ) )
: read-paren ( string n slice -- string n' slice' ) char: \( read-matched ; : read-paren ( string n slice -- string n' slice' ) char: \( read-matched ;
: read-string-payload ( string n -- string n' ) : read-string-payload ( string n -- string n' )
dup [ dup [
{ char: \\ char: \" } slice-til-separator-inclusive { { char: \\ char: \" } slice-until-include {
{ f [ drop ] } { f [ drop ] }
{ char: \" [ drop ] } { char: \" [ drop ] }
{ char: \\ [ drop next-char-from drop read-string-payload ] } { char: \\ [ drop next-char-from drop read-string-payload ] }
@ -423,7 +423,7 @@ DEFER: lex-factor-top*
{ char: \" [ read-string ] } { char: \" [ read-string ] }
{ char: \! [ read-exclamation ] } { char: \! [ read-exclamation ] }
{ char: > [ { char: > [
[ [ char: > = not ] slice-until ] dip merge-slices [ [ char: > = not ] slice-until-exclude drop ] dip merge-slices
dup section-close? [ dup section-close? [
[ slice-til-whitespace drop ] dip ?span-slices [ slice-til-whitespace drop ] dip ?span-slices
] unless ] unless

View File

@ -24,17 +24,22 @@ IN: modern.slices
rest ">" append rest ">" append
] if ; ] if ;
: slice-between ( slice1 slice2 -- slice )
! ensure-same-underlying
slice-order-by-from
[ to>> ]
[ [ from>> 2dup < [ swap ] unless ] [ seq>> ] bi ] bi* <slice> ;
: slice-before ( slice -- slice' )
[ drop 0 ] [ from>> ] [ seq>> ] tri <slice> ;
ERROR: unexpected-end string n ; ERROR: unexpected-end string n ;
: nth-check-eof ( string n -- nth ) : nth-check-eof ( string n -- nth )
2dup ?nth-of [ 2nip ] [ unexpected-end ] if* ; 2dup ?nth-of [ 2nip ] [ unexpected-end ] if* ;
! Allow eof ! Allow eof
: next-char-from ( string n/f -- string n'/f ch/f ) : next-char-from ( string n/f -- string n'/f ch/f )
dup [ dup [ 2dup ?nth-of dup [ [ 1 + ] dip ] when ] [ f ] if ;
2dup ?nth-of dup [ [ 1 + ] dip ] when
] [
f
] if ;
: find-from' ( ... seq n quot: ( ... elt -- ... ? ) -- ... i elt ) : find-from' ( ... seq n quot: ( ... elt -- ... ? ) -- ... i elt )
swapd find-from ; inline swapd find-from ; inline
@ -46,72 +51,46 @@ ERROR: unexpected-end string n ;
: find-from*' ( ... seq n quot: ( ... elt -- ... ? ) -- ... i elt ? ) : find-from*' ( ... seq n quot: ( ... elt -- ... ? ) -- ... i elt ? )
swapd find-from* ; inline swapd find-from* ; inline
:: (slice-until) ( string n quot -- string n' slice/f ch/f ) : slice-until-exclude ( string n quot -- string n' slice/f ch/f )
string n quot find-from' :> ( n' ch ) [ drop ]
string n' [ find-from' ] 3bi ! ( string n n' ch )
n n' string ?<slice> [ drop nip ]
ch ; inline [ [ rot ?<slice> ] dip ] 4bi ; inline
: slice-until ( string n quot -- string n' slice/f ) : slice-until-include ( string n tokens -- string n' slice/f ch/f )
(slice-until) drop ; inline '[ _ member? ]
[ drop ]
! Don't include the whitespace in the slice [ find-from' [ ?1+ ] dip ] 3bi ! ( string n n' ch )
:: slice-til-quot ( string n quot -- string n'/f slice/f ch/f ) [ drop nip ]
n [ [ [ rot ?<slice> ] dip ] 4bi ; inline
! BUG: (slice-until) is broken here?!
string n quot find-from' :> ( n' ch )
string n'
n n' string ?<slice>
ch
] [
string f f f
] if ; inline
: slice-til-whitespace ( string n -- string n' slice/f ch/f ) : slice-til-whitespace ( string n -- string n' slice/f ch/f )
[ "\s\r\n" member? ] slice-til-quot ; inline [ "\s\r\n" member? ] slice-until-exclude ; inline
: slice-til-not-whitespace ( string n -- string n' slice/f ch/f ) : slice-til-not-whitespace ( string n -- string n' slice/f ch/f )
[ "\s\r\n" member? not ] slice-til-quot ; inline [ "\s\r\n" member? not ] slice-until-exclude ; inline
: skip-whitespace ( string n/f -- string n'/f ) : skip-whitespace ( string n/f -- string n'/f )
slice-til-not-whitespace 2drop ; slice-til-not-whitespace 2drop ;
: empty-slice-end ( seq -- slice ) : slice-til-eol ( string n -- string n' slice/f ch/f )
[ length dup ] [ ] bi <slice> ; inline [ "\r\n" member? ] slice-until-exclude ; inline
:: slice-til-eol ( string n -- string n' slice/f ch/f ) : merge-slice-til-whitespace ( string n slice -- string n' slice' )
n [
string n '[ "\r\n" member? ] find-from' :> ( n' ch )
string n'
n n' string ?<slice>
ch
] [
string n
string empty-slice-end
f
] if ; inline
: merge-slice-til-whitespace ( string n slice -- string n' slice' )
over [ over [
[ slice-til-whitespace drop ] dip merge-slices [ slice-til-whitespace drop ] dip merge-slices
] when ; ] when ;
: merge-slice-til-not-whitespace ( string n slice -- string n' slice' ) : merge-slice-til-not-whitespace ( string n slice -- string n' slice' )
over [ over [
[ slice-til-not-whitespace drop ] dip merge-slices [ slice-til-not-whitespace drop ] dip merge-slices
] when ; ] when ;
:: slice-til-separator-inclusive ( string n tokens -- string n' slice/f ch/f )
string n '[ tokens member? ] find-from' [ ?1+ ] dip :> ( n' ch )
string
n'
n n' string ?<slice>
ch ; inline
! Takes at least one character if not whitespace ! Takes at least one character if not whitespace
:: slice-til-either ( string n tokens -- string n'/f slice/f ch/f ) :: slice-til-either ( string n tokens -- string n'/f slice/f ch/f )
n [ n [
string n '[ tokens member? ] find-from' string n [ tokens member? ] find-from'
dup "\s\r\n" member? [ [ ?1+ ] dip ] unless :> ( n' ch ) dup "\s\r\n" member? [ [ ?1+ ] dip ] unless :> ( n' ch )
string string
n' n'
@ -123,7 +102,7 @@ ERROR: unexpected-end string n ;
ERROR: subseq-expected-but-got-eof string n expected ; ERROR: subseq-expected-but-got-eof string n expected ;
:: slice-til-string ( string n search -- string n'/f payload end-string ) :: slice-til-string ( string n search -- string n'/f payload end-string )
search string n subseq-start-from :> n' search string n subseq-start-from :> n'
n' [ string n search subseq-expected-but-got-eof ] unless n' [ string n search subseq-expected-but-got-eof ] unless
string string