1 ! Copyright (C) 2016 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs kernel math sequences sequences.deep
4 sequences.extras strings unicode ;
7 : >strings ( seq -- str )
8 [ dup slice? [ >string ] when ] deep-map ;
10 : matching-delimiter ( ch -- ch' )
19 : matching-delimiter-string ( string -- string' )
20 [ matching-delimiter ] map ;
22 : matching-section-delimiter ( string -- string' )
24 rest but-last ";" ">" surround
29 : accept1 ( n string quot: ( ch -- ? ) -- n/n' string ch/f )
30 [ 2dup nth ] dip keep swap [ [ 1 + ] 2dip ] [ drop f ] if ; inline
32 ERROR: unexpected-end n string ;
33 : nth-check-eof ( n string -- nth )
34 2dup ?nth [ 2nip ] [ unexpected-end ] if* ;
36 : peek1-from ( n/f string -- ch )
37 over [ ?nth ] [ 2drop f ] if ;
39 : peek-from ( n/f string m -- string )
40 over [ [ swap tail-slice ] dip head-slice ] [ 3drop f ] if ;
42 : previous-from ( n/f string -- ch )
43 over [ [ 1 - ] dip ?nth ] [ 2drop f ] if ;
46 : next-char-from ( n/f string -- n'/f string ch/f )
48 2dup ?nth [ [ 1 + ] 2dip ] [ f ] if*
50 [ 2drop f ] [ nip ] 2bi f
53 : prev-char-from-slice-end ( slice -- ch/f )
54 [ to>> 2 - ] [ seq>> ] bi ?nth ;
56 : prev-char-from-slice ( slice -- ch/f )
57 [ from>> 1 - ] [ seq>> ] bi ?nth ;
59 : next-char-from-slice ( slice -- ch/f )
60 [ to>> ] [ seq>> ] bi ?nth ;
62 : char-before-slice ( slice -- ch/f )
63 [ from>> 1 - ] [ seq>> ] bi ?nth ;
65 : char-after-slice ( slice -- ch/f )
66 [ to>> ] [ seq>> ] bi ?nth ;
68 : find-from* ( ... n seq quot: ( ... elt -- ... ? ) -- ... i elt ? )
70 pick [ drop t ] [ length -rot nip f ] if ; inline
72 : skip-blank-from ( n string -- n' string )
74 [ [ blank? not ] find-from* 2drop ] keep
77 : skip-til-eol-from ( n string -- n' string )
78 [ [ "\r\n" member? ] find-from* 2drop ] keep ; inline
80 ERROR: take-slice-error n string count ;
81 :: take-slice ( n string count -- n'/f string slice )
82 n [ n string count take-slice-error ] unless
88 ERROR: expected-sequence-error expected actual ;
89 : check-sequence ( expected actual -- actual/* )
90 2dup sequence= [ nip ] [ expected-sequence-error ] if ;
92 : check-sequence-insensitive ( expected actual -- actual/* )
93 2dup [ >lower ] bi@ sequence= [ nip ] [ expected-sequence-error ] if ;
95 : expect-and-span ( n string slice expected-string -- n' string slice' )
96 dup length '[ _ take-slice ] 2dip
97 rot check-sequence span-slices ;
99 : expect-and-span-insensitive ( n string slice expected-string -- n' string slice' )
100 dup length '[ _ take-slice ] 2dip
101 rot check-sequence-insensitive span-slices ;
103 :: split-slice-back ( slice n -- slice1 slice2 )
104 slice [ from>> ] [ to>> ] [ seq>> ] tri :> ( from to seq )
105 from to n - seq <slice>
106 to n - to seq <slice> ;
108 ! Don't include the whitespace in the slice
109 :: slice-til-whitespace ( n string -- n' string slice/f ch/f )
111 n string [ "\s\r\n" member? ] find-from :> ( n' ch )
119 :: (slice-until) ( n string quot -- n' string slice/f ch/f )
120 n string quot find-from :> ( n' ch )
125 : slice-until ( n string quot -- n' string slice/f )
126 (slice-until) drop ; inline
128 :: slice-til-not-whitespace ( n string -- n' string slice/f ch/f )
130 n string [ "\s\r\n\t" member? not ] find-from :> ( n' ch )
138 : skip-whitespace ( n/f string -- n'/f string )
139 slice-til-not-whitespace 2drop ;
141 : empty-slice-end ( seq -- slice )
142 [ length dup ] [ ] bi <slice> ; inline
144 : empty-slice-from ( n seq -- slice )
145 dupd <slice> ; inline
147 :: slice-til-eol ( n string -- n' string slice/f ch/f )
149 n string '[ "\r\n" member? ] find-from :> ( n' ch )
154 n string string empty-slice-end f
157 :: merge-slice-til-eol-slash'' ( n string -- n' string slice/f ch/f )
159 n string '[ "\r\n\\" member? ] find-from :> ( n' ch )
164 n string string empty-slice-end f
167 : merge-slice-til-whitespace ( n string slice -- n' string slice' )
169 [ slice-til-whitespace drop ] dip merge-slices
172 : merge-slice-til-eol ( n string slice -- n' string slice' )
173 [ slice-til-eol drop ] dip merge-slices ;
175 : slice-between ( slice1 slice2 -- slice )
176 ! ensure-same-underlying
179 [ [ from>> 2dup < [ swap ] unless ] [ seq>> ] bi ] bi* <slice> ;
181 : slice-before ( slice -- slice' )
182 [ drop 0 ] [ from>> ] [ seq>> ] tri <slice> ;
184 : (?nth) ( n/f string/f -- obj/f )
185 over [ (?nth) ] [ 2drop f ] if ;
187 :: merge-slice-til-eol-slash' ( n string slice -- n' string slice/f ch/f )
188 n string merge-slice-til-eol-slash'' :> ( n' string' slice' ch' )
190 n' 1 + string' (?nth) "\r\n" member? [
191 n' 2 + string' slice slice' span-slices merge-slice-til-eol-slash'
196 n' string' slice slice' span-slices ch'
199 ! Supports \ at eol (with no space after it)
200 : slice-til-eol-slash ( n string -- n' string slice/f ch/f )
201 2dup empty-slice-from merge-slice-til-eol-slash' ;
203 :: slice-til-separator-inclusive ( n string tokens -- n' string slice/f ch/f )
204 n string '[ tokens member? ] find-from [ dup [ 1 + ] when ] dip :> ( n' ch )
209 : slice-til-separator-exclusive ( n string tokens -- n' string slice/f ch/f )
210 slice-til-separator-inclusive dup [
211 [ [ 1 - ] change-to ] dip
214 ! Takes at least one character if not whitespace
215 :: slice-til-either ( n string tokens -- n'/f string slice/f ch/f )
217 n string '[ tokens member? ] find-from
218 dup "\s\r\n" member? [
224 [ dup [ 1 + ] when ] dip :> ( n' ch )
233 ERROR: subseq-expected-but-got-eof n string expected ;
235 :: slice-til-string ( n string search -- n' string payload end-string )
236 search string n subseq-start-from :> n'
237 n' [ n string search subseq-expected-but-got-eof ] unless
238 n' search length + string
240 n' dup search length + string ?<slice> ;
242 : modify-from ( slice n -- slice' )
243 '[ from>> _ + ] [ to>> ] [ seq>> ] tri <slice> ;
245 : modify-to ( slice n -- slice' )
246 [ [ from>> ] [ to>> ] [ seq>> ] tri ] dip
247 swap [ + ] dip <slice> ;
249 ! { CHAR: \] [ read-closing ] }
250 ! { CHAR: \} [ read-closing ] }
251 ! { CHAR: \) [ read-closing ] }
252 : read-closing ( n string tok -- n string tok )
254 -1 modify-to [ 1 - ] 2dip
257 : rewind-slice ( n string slice -- n' string )
259 length swap [ - ] dip
261 [ nip ] dip [ [ length ] bi@ - ] keepd