1 ! Copyright (C) 2016 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs combinators.extras kernel math sequences
4 sequences.deep 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 : take-char ( n/f string -- n'/f string ch/f )
48 2dup ?nth [ [ 1 + ] 2dip ] [ f ] if*
53 : find-from* ( ... n seq quot: ( ... elt -- ... ? ) -- ... i/f elt ? )
55 pick [ drop t ] [ length -rot nip f ] if ; inline
57 : skip-blank-from ( n string -- n' string )
59 [ [ blank? not ] find-from* 2drop ] keep
62 : skip-til-eol-from ( n string -- n' string )
63 [ [ "\r\n" member? ] find-from* 2drop ] keep ; inline
65 ERROR: take-slice-error n string count ;
66 :: take-slice ( n string count -- n'/f string slice )
67 n [ n string count take-slice-error ] unless
73 ERROR: expected-sequence-error expected actual ;
74 : check-sequence ( expected actual -- actual/* )
75 2dup sequence= [ nip ] [ expected-sequence-error ] if ;
77 : check-sequence-insensitive ( expected actual -- actual/* )
78 2dup [ >lower ] bi@ sequence= [ nip ] [ expected-sequence-error ] if ;
80 : expect-and-span ( n string slice expected-string -- n' string slice' )
81 dup length '[ _ take-slice ] 2dip-1up check-sequence span-slices ;
83 : expect-and-span-insensitive ( n string slice expected-string -- n' string slice' )
84 dup length '[ _ take-slice ] 2dip-1up check-sequence-insensitive span-slices ;
86 :: split-slice-back ( slice n -- slice1 slice2 )
87 slice [ from>> ] [ to>> ] [ seq>> ] tri :> ( from to seq )
88 from to n - seq <slice>
89 to n - to seq <slice> ;
91 ! Don't include the whitespace in the slice
92 :: slice-til-whitespace ( n string -- n' string slice/f ch/f )
94 n string [ "\s\r\n" member? ] find-from :> ( n' ch )
102 :: (slice-until) ( n string quot -- n' string slice/f ch/f )
103 n string quot find-from :> ( n' ch )
108 : slice-until ( n string quot -- n' string slice/f )
109 (slice-until) drop ; inline
111 :: slice-til-not-whitespace ( n string -- n' string slice/f ch/f )
113 n string [ "\s\r\n\t" member? not ] find-from :> ( n' ch )
121 : skip-whitespace ( n/f string -- n'/f string )
122 slice-til-not-whitespace 2drop ;
124 : empty-slice-end ( seq -- slice )
125 [ length dup ] [ ] bi <slice> ; inline
127 :: slice-til-eol ( n string -- n' string slice/f ch/f )
129 n string '[ "\r\n" member? ] find-from :> ( n' ch )
134 n string string empty-slice-end f
137 : merge-slice-til-whitespace ( n/f string slice -- n'/f string slice' )
139 [ slice-til-whitespace drop ] dip merge-slices
142 : slice-between ( slice1 slice2 -- slice )
143 ! ensure-same-underlying
146 [ [ from>> 2dup < [ swap ] unless ] [ seq>> ] bi ] bi* <slice> ;
148 : slice-before ( slice -- slice' )
149 [ drop 0 ] [ from>> ] [ seq>> ] tri <slice> ;
151 :: slice-til-separator-inclusive ( n string tokens -- n' string slice/f ch/f )
152 n string '[ tokens member? ] find-from [ dup [ 1 + ] when ] dip :> ( n' ch )
157 : slice-til-separator-exclusive ( n string tokens -- n' string slice/f ch/f )
158 slice-til-separator-inclusive dup [
159 [ [ 1 - ] change-to ] dip
162 ! Takes at least one character if not whitespace
163 :: slice-til-either ( n string tokens -- n'/f string slice/f ch/f )
165 n string '[ tokens member? ] find-from
166 dup "\s\r\n" member? [
172 [ dup [ 1 + ] when ] dip :> ( n' ch )
181 ERROR: subseq-expected-but-got-eof n string expected ;
183 :: slice-til-string ( n string search -- n' string payload end-string )
184 n string search subseq-index-from :> n'
185 n' [ n string search subseq-expected-but-got-eof ] unless
186 n' search length + string
188 n' dup search length + string ?<slice> ;
190 : modify-from ( slice n -- slice' )
191 '[ from>> _ + ] [ to>> ] [ seq>> ] tri <slice> ;
193 : modify-to ( slice n -- slice' )
194 [ from>> ] swap '[ to>> _ + ] [ seq>> ] tri <slice> ; inline
196 ! { CHAR: \] [ read-closing ] }
197 ! { CHAR: \} [ read-closing ] }
198 ! { CHAR: \) [ read-closing ] }
199 : read-closing ( n string tok -- n' string tok )
205 : rewind-slice ( n string slice -- n' string )
206 2nip [ from>> ] [ seq>> ] bi ; inline
208 :: take-from? ( n seq subseq -- n'/f seq ? )
209 n seq subseq subseq-starts-at? [
217 : check-slice? ( from to seq -- from to seq ? )
228 :: take-from-insensitive? ( n seq str -- n'/f seq ? )
229 n str length over + seq check-slice? [
230 subseq str [ >lower ] bi@ sequence= [