1 ! Copyright (C) 2016 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs fry kernel locals math sequences
4 sequences.deep sequences.extras sequences.private strings
8 : >strings ( seq -- str )
9 [ dup slice? [ >string ] when ] deep-map ;
11 : matching-delimiter ( ch -- ch' )
20 : matching-delimiter-string ( string -- string' )
21 [ matching-delimiter ] map ;
23 : matching-section-delimiter ( string -- string' )
25 rest but-last ";" ">" surround
30 : accept1 ( n string quot: ( ch -- ? ) -- n/n' string ch/f )
31 [ 2dup nth ] dip keep swap [ [ 1 + ] 2dip ] [ drop f ] if ; inline
33 ERROR: unexpected-end n string ;
34 : nth-check-eof ( n string -- nth )
35 2dup ?nth [ 2nip ] [ unexpected-end ] if* ;
37 : peek1-from ( n/f string -- ch )
38 over [ ?nth ] [ 2drop f ] if ;
40 : peek-from ( n/f string m -- string )
41 over [ [ swap tail-slice ] dip head-slice ] [ 3drop f ] if ;
43 : previous-from ( n/f string -- ch )
44 over [ [ 1 - ] dip ?nth ] [ 2drop f ] if ;
47 : next-char-from ( n/f string -- n'/f string ch/f )
49 2dup ?nth [ [ 1 + ] 2dip ] [ f ] if*
51 [ 2drop f ] [ nip ] 2bi f
54 : prev-char-from-slice-end ( slice -- ch/f )
55 [ to>> 2 - ] [ seq>> ] bi ?nth ;
57 : prev-char-from-slice ( slice -- ch/f )
58 [ from>> 1 - ] [ seq>> ] bi ?nth ;
60 : next-char-from-slice ( slice -- ch/f )
61 [ to>> ] [ seq>> ] bi ?nth ;
63 : char-before-slice ( slice -- ch/f )
64 [ from>> 1 - ] [ seq>> ] bi ?nth ;
66 : char-after-slice ( slice -- ch/f )
67 [ to>> ] [ seq>> ] bi ?nth ;
69 : find-from* ( ... n seq quot: ( ... elt -- ... ? ) -- ... i elt ? )
71 pick [ drop t ] [ length -rot nip f ] if ; inline
73 : skip-blank-from ( n string -- n' string )
75 [ [ blank? not ] find-from* 2drop ] keep
78 : skip-til-eol-from ( n string -- n' string )
79 [ [ "\r\n" member? ] find-from* 2drop ] keep ; inline
81 ERROR: take-slice-error n string count ;
82 :: take-slice ( n string count -- n'/f string slice )
83 n [ n string count take-slice-error ] unless
89 ERROR: expected-sequence-error expected actual ;
90 : check-sequence ( expected actual -- actual/* )
91 2dup sequence= [ nip ] [ expected-sequence-error ] if ;
93 : check-sequence-insensitive ( expected actual -- actual/* )
94 2dup [ >lower ] bi@ sequence= [ nip ] [ expected-sequence-error ] if ;
96 : expect-and-span ( n string slice expected-string -- n' string slice' )
97 dup length '[ _ take-slice ] 2dip
98 rot check-sequence span-slices ;
100 : expect-and-span-insensitive ( n string slice expected-string -- n' string slice' )
101 dup length '[ _ take-slice ] 2dip
102 rot check-sequence-insensitive span-slices ;
104 :: split-slice-back ( slice n -- slice1 slice2 )
105 slice [ from>> ] [ to>> ] [ seq>> ] tri :> ( from to seq )
106 from to n - seq <slice>
107 to n - to seq <slice> ;
109 ! Don't include the whitespace in the slice
110 :: slice-til-whitespace ( n string -- n' string slice/f ch/f )
112 n string [ "\s\r\n" member? ] find-from :> ( n' ch )
120 :: (slice-until) ( n string quot -- n' string slice/f ch/f )
121 n string quot find-from :> ( n' ch )
126 : slice-until ( n string quot -- n' string slice/f )
127 (slice-until) drop ; inline
129 :: slice-til-not-whitespace ( n string -- n' string slice/f ch/f )
131 n string [ "\s\r\n\t" member? not ] find-from :> ( n' ch )
139 : skip-whitespace ( n/f string -- n'/f string )
140 slice-til-not-whitespace 2drop ;
142 : empty-slice-end ( seq -- slice )
143 [ length dup ] [ ] bi <slice> ; inline
145 : empty-slice-from ( n seq -- slice )
146 dupd <slice> ; inline
148 :: slice-til-eol ( n string -- n' string slice/f ch/f )
150 n string '[ "\r\n" member? ] find-from :> ( n' ch )
155 n string string empty-slice-end f
158 :: merge-slice-til-eol-slash'' ( n string -- n' string slice/f ch/f )
160 n string '[ "\r\n\\" member? ] find-from :> ( n' ch )
165 n string string empty-slice-end f
168 : merge-slice-til-whitespace ( n string slice -- n' string slice' )
170 [ slice-til-whitespace drop ] dip merge-slices
173 : merge-slice-til-eol ( n string slice -- n' string slice' )
174 [ slice-til-eol drop ] dip merge-slices ;
176 : slice-between ( slice1 slice2 -- slice )
177 ! ensure-same-underlying
180 [ [ from>> 2dup < [ swap ] unless ] [ seq>> ] bi ] bi* <slice> ;
182 : slice-before ( slice -- slice' )
183 [ drop 0 ] [ from>> ] [ seq>> ] tri <slice> ;
185 : (?nth) ( n/f string/f -- obj/f )
186 over [ (?nth) ] [ 2drop f ] if ;
188 :: merge-slice-til-eol-slash' ( n string slice -- n' string slice/f ch/f )
189 n string merge-slice-til-eol-slash'' :> ( n' string' slice' ch' )
191 n' 1 + string' (?nth) "\r\n" member? [
192 n' 2 + string' slice slice' span-slices merge-slice-til-eol-slash'
197 n' string' slice slice' span-slices ch'
200 ! Supports \ at eol (with no space after it)
201 : slice-til-eol-slash ( n string -- n' string slice/f ch/f )
202 2dup empty-slice-from merge-slice-til-eol-slash' ;
204 :: slice-til-separator-inclusive ( n string tokens -- n' string slice/f ch/f )
205 n string '[ tokens member? ] find-from [ dup [ 1 + ] when ] dip :> ( n' ch )
210 : slice-til-separator-exclusive ( n string tokens -- n' string slice/f ch/f )
211 slice-til-separator-inclusive dup [
212 [ [ 1 - ] change-to ] dip
215 ! Takes at least one character if not whitespace
216 :: slice-til-either ( n string tokens -- n'/f string slice/f ch/f )
218 n string '[ tokens member? ] find-from
219 dup "\s\r\n" member? [
225 [ dup [ 1 + ] when ] dip :> ( n' ch )
234 ERROR: subseq-expected-but-got-eof n string expected ;
236 :: slice-til-string ( n string search -- n' string payload end-string )
237 search string n subseq-start-from :> n'
238 n' [ n string search subseq-expected-but-got-eof ] unless
239 n' search length + string
241 n' dup search length + string ?<slice> ;
243 : modify-from ( slice n -- slice' )
244 '[ from>> _ + ] [ to>> ] [ seq>> ] tri <slice> ;
246 : modify-to ( slice n -- slice' )
247 [ [ from>> ] [ to>> ] [ seq>> ] tri ] dip
248 swap [ + ] dip <slice> ;
250 ! { CHAR: \] [ read-closing ] }
251 ! { CHAR: \} [ read-closing ] }
252 ! { CHAR: \) [ read-closing ] }
253 : read-closing ( n string tok -- n string tok )
255 -1 modify-to [ 1 - ] 2dip
258 : rewind-slice ( n string slice -- n' string )
259 2nip [ from>> ] [ seq>> ] bi ; inline
261 :: take-from? ( n seq subseq -- n'/f seq ? )
262 subseq seq n pick length (subseq-start-from) 2nip [
270 : check-slice? ( from to seq -- from to seq ? )
281 :: take-from-insensitive? ( n seq str -- n'/f seq ? )
282 n str length over + seq check-slice? [
283 subseq str [ >lower ] bi@ sequence= [