| 
									
										
										
										
											2008-08-26 21:24:14 -04:00
										 |  |  | ! Copyright (C) 2008 Doug Coleman. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-12-08 15:58:00 -05:00
										 |  |  | USING: accessors combinators kernel math sequences strings sets | 
					
						
							|  |  |  | assocs prettyprint.backend prettyprint.custom make lexer | 
					
						
							|  |  |  | namespaces parser arrays fry regexp.backend regexp.utils | 
					
						
							|  |  |  | regexp.parser regexp.nfa regexp.dfa regexp.traversal | 
					
						
							|  |  |  | regexp.transition-tables splitting sorting ;
 | 
					
						
							| 
									
										
										
										
											2008-09-18 15:42:16 -04:00
										 |  |  | IN: regexp | 
					
						
							| 
									
										
										
										
											2008-08-26 21:24:14 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : default-regexp ( string -- regexp )
 | 
					
						
							|  |  |  |     regexp new
 | 
					
						
							|  |  |  |         swap >>raw | 
					
						
							|  |  |  |         <transition-table> >>nfa-table | 
					
						
							|  |  |  |         <transition-table> >>dfa-table | 
					
						
							|  |  |  |         <transition-table> >>minimized-table | 
					
						
							| 
									
										
										
										
											2008-08-28 23:08:54 -04:00
										 |  |  |         H{ } clone >>nfa-traversal-flags | 
					
						
							|  |  |  |         H{ } clone >>dfa-traversal-flags | 
					
						
							| 
									
										
										
										
											2008-09-12 22:56:25 -04:00
										 |  |  |         H{ } clone >>options | 
					
						
							| 
									
										
										
										
											2008-11-24 23:17:47 -05:00
										 |  |  |         H{ } clone >>matchers | 
					
						
							| 
									
										
										
										
											2008-08-26 21:24:14 -04:00
										 |  |  |         reset-regexp ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : construct-regexp ( regexp -- regexp' )
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         [ parse-regexp ] | 
					
						
							|  |  |  |         [ construct-nfa ] | 
					
						
							|  |  |  |         [ construct-dfa ] | 
					
						
							|  |  |  |         [ ] | 
					
						
							|  |  |  |     } cleave ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-22 19:07:57 -05:00
										 |  |  | : (match) ( string regexp -- dfa-traverser )
 | 
					
						
							|  |  |  |     <dfa-traverser> do-match ; inline
 | 
					
						
							| 
									
										
										
										
											2008-08-26 21:24:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-22 19:07:57 -05:00
										 |  |  | : match ( string regexp -- slice/f )
 | 
					
						
							|  |  |  |     (match) return-match ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : match* ( string regexp -- slice/f captured-groups )
 | 
					
						
							|  |  |  |     (match) [ return-match ] [ captured-groups>> ] bi ;
 | 
					
						
							| 
									
										
										
										
											2008-09-22 21:09:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-26 21:24:14 -04:00
										 |  |  | : matches? ( string regexp -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-08-28 14:45:04 -04:00
										 |  |  |     dupd match | 
					
						
							| 
									
										
										
										
											2008-11-22 18:30:16 -05:00
										 |  |  |     [ [ length ] bi@ = ] [ drop f ] if* ;
 | 
					
						
							| 
									
										
										
										
											2008-08-26 21:24:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-22 18:30:16 -05:00
										 |  |  | : match-head ( string regexp -- end/f ) match [ length ] [ f ] if* ;
 | 
					
						
							| 
									
										
										
										
											2008-09-19 16:14:05 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : match-at ( string m regexp -- n/f finished? )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         2dup swap length > [ 2drop f f ] [ tail-slice t ] if
 | 
					
						
							|  |  |  |     ] dip swap [ match-head f ] [ 2drop f t ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : match-range ( string m regexp -- a/f b/f )
 | 
					
						
							|  |  |  |     3dup match-at over [ | 
					
						
							|  |  |  |         drop nip rot drop dupd +
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         [ 3drop drop f f ] [ drop [ 1+ ] dip match-range ] if
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-08-26 21:24:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-22 21:01:25 -05:00
										 |  |  | : first-match ( string regexp -- slice/f )
 | 
					
						
							| 
									
										
										
										
											2008-11-22 19:07:57 -05:00
										 |  |  |     dupd 0 swap match-range rot over [ <slice> ] [ 3drop f ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-09-22 03:28:10 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : re-cut ( string regexp -- end/f start )
 | 
					
						
							|  |  |  |     dupd first-match | 
					
						
							| 
									
										
										
										
											2008-11-22 21:01:25 -05:00
										 |  |  |     [ split1-slice swap ] [ "" like f swap ] if* ;
 | 
					
						
							| 
									
										
										
										
											2008-09-22 03:28:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-26 00:20:17 -05:00
										 |  |  | : (re-split) ( string regexp -- )
 | 
					
						
							|  |  |  |     over [ [ re-cut , ] keep (re-split) ] [ 2drop ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-22 03:28:10 -04:00
										 |  |  | : re-split ( string regexp -- seq )
 | 
					
						
							| 
									
										
										
										
											2009-01-26 00:20:17 -05:00
										 |  |  |     [ (re-split) ] { } make ;
 | 
					
						
							| 
									
										
										
										
											2008-09-22 03:28:10 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : re-replace ( string regexp replacement -- result )
 | 
					
						
							|  |  |  |     [ re-split ] dip join ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : next-match ( string regexp -- end/f match/f )
 | 
					
						
							|  |  |  |     dupd first-match dup
 | 
					
						
							| 
									
										
										
										
											2008-11-22 21:01:25 -05:00
										 |  |  |     [ [ split1-slice nip ] keep ] [ 2drop f f ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-09-22 03:28:10 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : all-matches ( string regexp -- seq )
 | 
					
						
							| 
									
										
										
										
											2008-11-22 21:01:25 -05:00
										 |  |  |     [ dup ] swap '[ _ next-match ] [ ] produce nip harvest ;
 | 
					
						
							| 
									
										
										
										
											2008-09-22 03:28:10 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : count-matches ( string regexp -- n )
 | 
					
						
							| 
									
										
										
										
											2008-11-22 22:10:53 -05:00
										 |  |  |     all-matches length ;
 | 
					
						
							| 
									
										
										
										
											2008-09-22 03:28:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-24 01:18:27 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							| 
									
										
										
										
											2008-08-26 21:24:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-24 01:18:27 -05:00
										 |  |  | : find-regexp-syntax ( string -- prefix suffix )
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         { "R/ "  "/"  } | 
					
						
							|  |  |  |         { "R! "  "!"  } | 
					
						
							|  |  |  |         { "R\" " "\"" } | 
					
						
							|  |  |  |         { "R# "  "#"  } | 
					
						
							|  |  |  |         { "R' "  "'"  } | 
					
						
							|  |  |  |         { "R( "  ")"  } | 
					
						
							|  |  |  |         { "R@ "  "@"  } | 
					
						
							|  |  |  |         { "R[ "  "]"  } | 
					
						
							|  |  |  |         { "R` "  "`"  } | 
					
						
							|  |  |  |         { "R{ "  "}"  } | 
					
						
							|  |  |  |         { "R| "  "|"  } | 
					
						
							|  |  |  |     } swap [ subseq? not nip ] curry assoc-find drop ;
 | 
					
						
							| 
									
										
										
										
											2008-08-26 21:24:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-24 01:18:27 -05:00
										 |  |  | : string>options ( string -- options )
 | 
					
						
							|  |  |  |     [ ch>option dup ] H{ } map>assoc ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : options>string ( options -- string )
 | 
					
						
							|  |  |  |     keys [ option>ch ] map natural-sort >string ;
 | 
					
						
							| 
									
										
										
										
											2008-08-26 21:24:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-24 01:18:27 -05:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <optioned-regexp> ( string option-string -- regexp )
 | 
					
						
							|  |  |  |     [ default-regexp ] [ string>options ] bi* >>options | 
					
						
							| 
									
										
										
										
											2008-08-26 21:24:14 -04:00
										 |  |  |     construct-regexp ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-24 01:18:27 -05:00
										 |  |  | : <regexp> ( string -- regexp ) "" <optioned-regexp> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-18 15:42:16 -04:00
										 |  |  | : parsing-regexp ( accum end -- accum )
 | 
					
						
							|  |  |  |     lexer get dup skip-blank | 
					
						
							|  |  |  |     [ [ index-from dup 1+ swap ] 2keep swapd subseq swap ] change-lexer-column | 
					
						
							|  |  |  |     lexer get dup still-parsing-line? | 
					
						
							|  |  |  |     [ (parse-token) ] [ drop f ] if
 | 
					
						
							| 
									
										
										
										
											2008-11-24 01:18:27 -05:00
										 |  |  |     <optioned-regexp> parsed ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							| 
									
										
										
										
											2008-09-18 15:42:16 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : R! CHAR: ! parsing-regexp ; parsing | 
					
						
							|  |  |  | : R" CHAR: " parsing-regexp ; parsing | 
					
						
							|  |  |  | : R# CHAR: # parsing-regexp ; parsing | 
					
						
							|  |  |  | : R' CHAR: ' parsing-regexp ; parsing | 
					
						
							|  |  |  | : R( CHAR: ) parsing-regexp ; parsing | 
					
						
							|  |  |  | : R/ CHAR: / parsing-regexp ; parsing | 
					
						
							|  |  |  | : R@ CHAR: @ parsing-regexp ; parsing | 
					
						
							|  |  |  | : R[ CHAR: ] parsing-regexp ; parsing | 
					
						
							|  |  |  | : R` CHAR: ` parsing-regexp ; parsing | 
					
						
							|  |  |  | : R{ CHAR: } parsing-regexp ; parsing | 
					
						
							|  |  |  | : R| CHAR: | parsing-regexp ; parsing | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-12 22:56:25 -04:00
										 |  |  | M: regexp pprint* | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         [ | 
					
						
							| 
									
										
										
										
											2008-11-24 01:18:27 -05:00
										 |  |  |             [ raw>> dup find-regexp-syntax swap % swap % % ] | 
					
						
							|  |  |  |             [ options>> options>string % ] bi
 | 
					
						
							| 
									
										
										
										
											2008-09-12 22:56:25 -04:00
										 |  |  |         ] "" make | 
					
						
							|  |  |  |     ] keep present-text ;
 |