1 USING: arrays combinators kernel lists math math.parser
2 namespaces parser parser-combinators parser-combinators.simple
3 promises quotations sequences combinators.lib strings math.order
4 assocs prettyprint.backend memoize unicode.case unicode.categories ;
12 : char=-quot ( ch -- quot )
14 [ ch>upper [ swap ch>upper = ] ] [ [ = ] ] if
17 : char-between?-quot ( ch1 ch2 -- quot )
19 [ [ ch>upper ] bi@ [ >r >r ch>upper r> r> between? ] ]
23 : or-predicates ( quots -- quot )
24 [ \ dup prefix ] map [ [ t ] ] f short-circuit \ nip suffix ;
26 : <@literal ( parser obj -- action ) [ nip ] curry <@ ;
28 : <@delay ( parser quot -- action ) [ curry ] curry <@ ;
35 : octal-digit? ( n -- ? )
36 CHAR: 0 CHAR: 7 between? ;
38 : decimal-digit? ( n -- ? )
39 CHAR: 0 CHAR: 9 between? ;
41 : hex-digit? ( n -- ? )
43 over CHAR: a CHAR: f between? or
44 swap CHAR: A CHAR: F between? or ;
46 : control-char? ( n -- ? )
47 dup 0 HEX: 1f between?
51 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" member? ;
53 : c-identifier-char? ( ch -- ? )
54 dup alpha? swap CHAR: _ = or ;
56 : java-blank? ( n -- ? )
59 CHAR: \t CHAR: \n CHAR: \r
63 : java-printable? ( n -- ? )
64 dup alpha? swap punct? or ;
66 : 'ordinary-char' ( -- parser )
67 [ "\\^*+?|(){}[$" member? not ] satisfy
70 : 'octal-digit' ( -- parser ) [ octal-digit? ] satisfy ;
72 : 'octal' ( -- parser )
73 "0" token 'octal-digit' 1 3 from-m-to-n &>
76 : 'hex-digit' ( -- parser ) [ hex-digit? ] satisfy ;
79 "x" token 'hex-digit' 2 exactly-n &>
80 "u" token 'hex-digit' 6 exactly-n &> <|>
83 : satisfy-tokens ( assoc -- parser )
84 [ >r token r> <@literal ] { } assoc>map <or-parser> ;
86 : 'simple-escape-char' ( -- parser )
95 } [ char=-quot ] assoc-map satisfy-tokens ;
97 : 'predefined-char-class' ( -- parser )
100 { "D" [ digit? not ] }
101 { "s" [ java-blank? ] }
102 { "S" [ java-blank? not ] }
103 { "w" [ c-identifier-char? ] }
104 { "W" [ c-identifier-char? not ] }
107 : 'posix-character-class' ( -- parser )
109 { "Lower" [ letter? ] }
110 { "Upper" [ LETTER? ] }
111 { "ASCII" [ ascii? ] }
112 { "Alpha" [ Letter? ] }
113 { "Digit" [ digit? ] }
114 { "Alnum" [ alpha? ] }
115 { "Punct" [ punct? ] }
116 { "Graph" [ java-printable? ] }
117 { "Print" [ java-printable? ] }
118 { "Blank" [ " \t" member? ] }
119 { "Cntrl" [ control-char? ] }
120 { "XDigit" [ hex-digit? ] }
121 { "Space" [ java-blank? ] }
122 } satisfy-tokens "p{" "}" surrounded-by ;
124 : 'simple-escape' ( -- parser )
127 "c" token [ LETTER? ] satisfy &> <|>
131 : 'escape' ( -- parser )
134 'predefined-char-class' <|>
135 'posix-character-class' <|>
136 'simple-escape' <|> &> ;
138 : 'any-char' ( -- parser )
139 "." token [ drop t ] <@literal ;
141 : 'char' ( -- parser )
142 'any-char' 'escape' 'ordinary-char' <|> <|> [ satisfy ] <@ ;
146 TUPLE: group-result str ;
148 C: <group-result> group-result
150 : 'non-capturing-group' ( -- parser )
151 "?:" token 'regexp' &> ;
153 : 'positive-lookahead-group' ( -- parser )
154 "?=" token 'regexp' &> [ ensure ] <@ ;
156 : 'negative-lookahead-group' ( -- parser )
157 "?!" token 'regexp' &> [ ensure-not ] <@ ;
159 : 'simple-group' ( -- parser )
160 'regexp' [ [ <group-result> ] <@ ] <@ ;
162 : 'group' ( -- parser )
163 'non-capturing-group'
164 'positive-lookahead-group'
165 'negative-lookahead-group'
166 'simple-group' <|> <|> <|>
167 "(" ")" surrounded-by ;
169 : 'range' ( -- parser )
170 [ CHAR: ] = not ] satisfy "-" token <&
171 [ CHAR: ] = not ] satisfy <&>
172 [ first2 char-between?-quot ] <@ ;
174 : 'character-class-term' ( -- parser )
177 [ "\\]" member? not ] satisfy [ char=-quot ] <@ <|> ;
179 : 'positive-character-class' ( -- parser )
180 "]" token [ CHAR: ] = ] <@literal 'character-class-term' <*> <&:>
181 'character-class-term' <+> <|>
182 [ or-predicates ] <@ ;
184 : 'negative-character-class' ( -- parser )
185 "^" token 'positive-character-class' &>
186 [ [ not ] append ] <@ ;
188 : 'character-class' ( -- parser )
189 'negative-character-class' 'positive-character-class' <|>
190 "[" "]" surrounded-by [ satisfy ] <@ ;
192 : 'escaped-seq' ( -- parser )
194 [ ignore-case? get <token-parser> ] <@
195 "\\Q" "\\E" surrounded-by ;
197 : 'break' ( quot -- parser )
198 satisfy ensure epsilon just <|> ;
200 : 'break-escape' ( -- parser )
201 "$" token [ "\r\n" member? ] 'break' <@literal
202 "\\b" token [ blank? ] 'break' <@literal <|>
203 "\\B" token [ blank? not ] 'break' <@literal <|>
204 "\\z" token epsilon just <@literal <|> ;
206 : 'simple' ( -- parser )
210 'character-class' <|>
213 : 'exactly-n' ( -- parser )
214 'integer' [ exactly-n ] <@delay ;
216 : 'at-least-n' ( -- parser )
217 'integer' "," token <& [ at-least-n ] <@delay ;
219 : 'at-most-n' ( -- parser )
220 "," token 'integer' &> [ at-most-n ] <@delay ;
222 : 'from-m-to-n' ( -- parser )
223 'integer' "," token <& 'integer' <&> [ first2 from-m-to-n ] <@delay ;
225 : 'greedy-interval' ( -- parser )
226 'exactly-n' 'at-least-n' <|> 'at-most-n' <|> 'from-m-to-n' <|> ;
228 : 'interval' ( -- parser )
230 'greedy-interval' "?" token <& [ "reluctant {}" print ] <@ <|>
231 'greedy-interval' "+" token <& [ "possessive {}" print ] <@ <|>
232 "{" "}" surrounded-by ;
234 : 'repetition' ( -- parser )
236 "*+" token [ <!*> ] <@literal
237 "++" token [ <!+> ] <@literal <|>
238 "?+" token [ <!?> ] <@literal <|>
240 "*?" token [ <(*)> ] <@literal <|>
241 "+?" token [ <(+)> ] <@literal <|>
242 "??" token [ <(?)> ] <@literal <|>
244 "*" token [ <*> ] <@literal <|>
245 "+" token [ <+> ] <@literal <|>
246 "?" token [ <?> ] <@literal <|> ;
248 : 'dummy' ( -- parser )
249 epsilon [ ] <@literal ;
251 MEMO: 'term' ( -- parser )
253 'repetition' 'interval' 'dummy' <|> <|> <&> [ first2 call ] <@
254 <!+> [ <and-parser> ] <@ ;
256 LAZY: 'regexp' ( -- parser )
257 'term' "|" token nonempty-list-of [ <or-parser> ] <@ ;
258 ! "^" token 'term' "|" token nonempty-list-of [ <or-parser> ] <@
259 ! &> [ "caret" print ] <@ <|>
260 ! 'term' "|" token nonempty-list-of [ <or-parser> ] <@
261 ! "$" token <& [ "dollar" print ] <@ <|>
262 ! "^" token 'term' "|" token nonempty-list-of [ <or-parser> ] <@ &>
263 ! "$" token [ "caret dollar" print ] <@ <& <|> ;
265 TUPLE: regexp source parser ignore-case? ;
267 : <regexp> ( string ignore-case? -- regexp )
270 dup 'regexp' just parse-1
274 : do-ignore-case ( string regexp -- string regexp )
275 dup regexp-ignore-case? [ >r >upper r> ] when ;
277 : matches? ( string regexp -- ? )
278 do-ignore-case regexp-parser just parse nil? not ;
280 : match-head ( string regexp -- end )
281 do-ignore-case regexp-parser parse dup nil?
282 [ drop f ] [ car parse-result-unparsed slice-from ] if ;
284 ! Literal syntax for regexps
285 : parse-options ( string -- ? )
292 : parse-regexp ( accum end -- accum )
293 lexer get dup skip-blank
294 [ [ index-from dup 1+ swap ] 2keep swapd subseq swap ] change-lexer-column
295 lexer get dup still-parsing-line?
296 [ (parse-token) parse-options ] [ drop f ] if
299 : R! CHAR: ! parse-regexp ; parsing
300 : R" CHAR: " parse-regexp ; parsing
301 : R# CHAR: # parse-regexp ; parsing
302 : R' CHAR: ' parse-regexp ; parsing
303 : R( CHAR: ) parse-regexp ; parsing
304 : R/ CHAR: / parse-regexp ; parsing
305 : R@ CHAR: @ parse-regexp ; parsing
306 : R[ CHAR: ] parse-regexp ; parsing
307 : R` CHAR: ` parse-regexp ; parsing
308 : R{ CHAR: } parse-regexp ; parsing
309 : R| CHAR: | parse-regexp ; parsing
311 : find-regexp-syntax ( string -- prefix suffix )
324 } swap [ subseq? not nip ] curry assoc-find drop ;
329 dup find-regexp-syntax swap % swap % %
330 dup regexp-ignore-case? [ "i" % ] when