1 USING: arrays combinators kernel lists math math.parser
2 namespaces parser lexer parser-combinators parser-combinators.simple
3 promises quotations sequences combinators.lib strings math.order
4 assocs prettyprint.backend memoize unicode.case unicode.categories
5 combinators.short-circuit accessors make io ;
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 : <@literal ( parser obj -- action ) [ nip ] curry <@ ;
25 : <@delay ( parser quot -- action ) [ curry ] curry <@ ;
32 : octal-digit? ( n -- ? )
33 CHAR: 0 CHAR: 7 between? ;
35 : decimal-digit? ( n -- ? )
36 CHAR: 0 CHAR: 9 between? ;
38 : hex-digit? ( n -- ? )
40 over CHAR: a CHAR: f between? or
41 swap CHAR: A CHAR: F between? or ;
43 : control-char? ( n -- ? )
44 dup 0 HEX: 1f between?
48 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" member? ;
50 : c-identifier-char? ( ch -- ? )
51 dup alpha? swap CHAR: _ = or ;
53 : java-blank? ( n -- ? )
56 CHAR: \t CHAR: \n CHAR: \r
60 : java-printable? ( n -- ? )
61 dup alpha? swap punct? or ;
63 : 'ordinary-char' ( -- parser )
64 [ "\\^*+?|(){}[$" member? not ] satisfy
67 : 'octal-digit' ( -- parser ) [ octal-digit? ] satisfy ;
69 : 'octal' ( -- parser )
70 "0" token 'octal-digit' 1 3 from-m-to-n &>
73 : 'hex-digit' ( -- parser ) [ hex-digit? ] satisfy ;
76 "x" token 'hex-digit' 2 exactly-n &>
77 "u" token 'hex-digit' 6 exactly-n &> <|>
80 : satisfy-tokens ( assoc -- parser )
81 [ >r token r> <@literal ] { } assoc>map <or-parser> ;
83 : 'simple-escape-char' ( -- parser )
92 } [ char=-quot ] assoc-map satisfy-tokens ;
94 : 'predefined-char-class' ( -- parser )
97 { "D" [ digit? not ] }
98 { "s" [ java-blank? ] }
99 { "S" [ java-blank? not ] }
100 { "w" [ c-identifier-char? ] }
101 { "W" [ c-identifier-char? not ] }
104 : 'posix-character-class' ( -- parser )
106 { "Lower" [ letter? ] }
107 { "Upper" [ LETTER? ] }
108 { "ASCII" [ ascii? ] }
109 { "Alpha" [ Letter? ] }
110 { "Digit" [ digit? ] }
111 { "Alnum" [ alpha? ] }
112 { "Punct" [ punct? ] }
113 { "Graph" [ java-printable? ] }
114 { "Print" [ java-printable? ] }
115 { "Blank" [ " \t" member? ] }
116 { "Cntrl" [ control-char? ] }
117 { "XDigit" [ hex-digit? ] }
118 { "Space" [ java-blank? ] }
119 } satisfy-tokens "p{" "}" surrounded-by ;
121 : 'simple-escape' ( -- parser )
124 "c" token [ LETTER? ] satisfy &> <|>
128 : 'escape' ( -- parser )
131 'predefined-char-class' <|>
132 'posix-character-class' <|>
133 'simple-escape' <|> &> ;
135 : 'any-char' ( -- parser )
136 "." token [ drop t ] <@literal ;
138 : 'char' ( -- parser )
139 'any-char' 'escape' 'ordinary-char' <|> <|> [ satisfy ] <@ ;
143 TUPLE: group-result str ;
145 C: <group-result> group-result
147 : 'non-capturing-group' ( -- parser )
148 "?:" token 'regexp' &> ;
150 : 'positive-lookahead-group' ( -- parser )
151 "?=" token 'regexp' &> [ ensure ] <@ ;
153 : 'negative-lookahead-group' ( -- parser )
154 "?!" token 'regexp' &> [ ensure-not ] <@ ;
156 : 'simple-group' ( -- parser )
157 'regexp' [ [ <group-result> ] <@ ] <@ ;
159 : 'group' ( -- parser )
160 'non-capturing-group'
161 'positive-lookahead-group'
162 'negative-lookahead-group'
163 'simple-group' <|> <|> <|>
164 "(" ")" surrounded-by ;
166 : 'range' ( -- parser )
167 [ CHAR: ] = not ] satisfy "-" token <&
168 [ CHAR: ] = not ] satisfy <&>
169 [ first2 char-between?-quot ] <@ ;
171 : 'character-class-term' ( -- parser )
174 [ "\\]" member? not ] satisfy [ char=-quot ] <@ <|> ;
176 : 'positive-character-class' ( -- parser )
177 "]" token [ CHAR: ] = ] <@literal 'character-class-term' <*> <&:>
178 'character-class-term' <+> <|>
179 [ [ 1|| ] curry ] <@ ;
181 : 'negative-character-class' ( -- parser )
182 "^" token 'positive-character-class' &>
183 [ [ not ] append ] <@ ;
185 : 'character-class' ( -- parser )
186 'negative-character-class' 'positive-character-class' <|>
187 "[" "]" surrounded-by [ satisfy ] <@ ;
189 : 'escaped-seq' ( -- parser )
191 [ ignore-case? get <token-parser> ] <@
192 "\\Q" "\\E" surrounded-by ;
194 : 'break' ( quot -- parser )
195 satisfy ensure epsilon just <|> ;
197 : 'break-escape' ( -- parser )
198 "$" token [ "\r\n" member? ] 'break' <@literal
199 "\\b" token [ blank? ] 'break' <@literal <|>
200 "\\B" token [ blank? not ] 'break' <@literal <|>
201 "\\z" token epsilon just <@literal <|> ;
203 : 'simple' ( -- parser )
207 'character-class' <|>
210 : 'exactly-n' ( -- parser )
211 'integer' [ exactly-n ] <@delay ;
213 : 'at-least-n' ( -- parser )
214 'integer' "," token <& [ at-least-n ] <@delay ;
216 : 'at-most-n' ( -- parser )
217 "," token 'integer' &> [ at-most-n ] <@delay ;
219 : 'from-m-to-n' ( -- parser )
220 'integer' "," token <& 'integer' <&> [ first2 from-m-to-n ] <@delay ;
222 : 'greedy-interval' ( -- parser )
223 'exactly-n' 'at-least-n' <|> 'at-most-n' <|> 'from-m-to-n' <|> ;
225 : 'interval' ( -- parser )
227 'greedy-interval' "?" token <& [ "reluctant {}" print ] <@ <|>
228 'greedy-interval' "+" token <& [ "possessive {}" print ] <@ <|>
229 "{" "}" surrounded-by ;
231 : 'repetition' ( -- parser )
233 "*+" token [ <!*> ] <@literal
234 "++" token [ <!+> ] <@literal <|>
235 "?+" token [ <!?> ] <@literal <|>
237 "*?" token [ <(*)> ] <@literal <|>
238 "+?" token [ <(+)> ] <@literal <|>
239 "??" token [ <(?)> ] <@literal <|>
241 "*" token [ <*> ] <@literal <|>
242 "+" token [ <+> ] <@literal <|>
243 "?" token [ <?> ] <@literal <|> ;
245 : 'dummy' ( -- parser )
246 epsilon [ ] <@literal ;
248 MEMO: 'term' ( -- parser )
250 'repetition' 'interval' 'dummy' <|> <|> <&> [ first2 call ] <@
251 <!+> [ <and-parser> ] <@ ;
253 LAZY: 'regexp' ( -- parser )
254 'term' "|" token nonempty-list-of [ <or-parser> ] <@ ;
255 ! "^" token 'term' "|" token nonempty-list-of [ <or-parser> ] <@
256 ! &> [ "caret" print ] <@ <|>
257 ! 'term' "|" token nonempty-list-of [ <or-parser> ] <@
258 ! "$" token <& [ "dollar" print ] <@ <|>
259 ! "^" token 'term' "|" token nonempty-list-of [ <or-parser> ] <@ &>
260 ! "$" token [ "caret dollar" print ] <@ <& <|> ;
262 TUPLE: regexp source parser ignore-case? ;
264 : <regexp> ( string ignore-case? -- regexp )
267 dup 'regexp' just parse-1
271 : do-ignore-case ( string regexp -- string regexp )
272 dup ignore-case?>> [ >r >upper r> ] when ;
274 : matches? ( string regexp -- ? )
275 do-ignore-case parser>> just parse nil? not ;
277 : match-head ( string regexp -- end )
278 do-ignore-case parser>> parse dup nil?
279 [ drop f ] [ car unparsed>> from>> ] if ;
281 ! Literal syntax for regexps
282 : parse-options ( string -- ? )
289 : parse-regexp ( accum end -- accum )
290 lexer get dup skip-blank
291 [ [ index-from dup 1+ swap ] 2keep swapd subseq swap ] change-lexer-column
292 lexer get dup still-parsing-line?
293 [ (parse-token) parse-options ] [ drop f ] if
296 : R! CHAR: ! parse-regexp ; parsing
297 : R" CHAR: " parse-regexp ; parsing
298 : R# CHAR: # parse-regexp ; parsing
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
308 : find-regexp-syntax ( string -- prefix suffix )
321 } swap [ subseq? not nip ] curry assoc-find drop ;
326 dup find-regexp-syntax swap % swap % %
327 dup ignore-case?>> [ "i" % ] when