| 
									
										
										
										
											2008-08-26 21:24:14 -04:00
										 |  |  | ! Copyright (C) 2008 Doug Coleman. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-09-22 03:28:10 -04:00
										 |  |  | USING: accessors combinators kernel math math.ranges sequences | 
					
						
							|  |  |  | sets assocs prettyprint.backend make lexer namespaces parser | 
					
						
							|  |  |  | arrays fry regexp.backend regexp.utils regexp.parser regexp.nfa | 
					
						
							|  |  |  | regexp.dfa regexp.traversal regexp.transition-tables ;
 | 
					
						
							| 
									
										
										
										
											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-08-26 21:24:14 -04:00
										 |  |  |         reset-regexp ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : construct-regexp ( regexp -- regexp' )
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         [ parse-regexp ] | 
					
						
							|  |  |  |         [ construct-nfa ] | 
					
						
							|  |  |  |         [ construct-dfa ] | 
					
						
							|  |  |  |         [ ] | 
					
						
							|  |  |  |     } cleave ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : match ( string regexp -- pair )
 | 
					
						
							|  |  |  |     <dfa-traverser> do-match return-match ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-22 21:09:42 -04:00
										 |  |  | : match* ( string regexp -- pair )
 | 
					
						
							|  |  |  |     <dfa-traverser> do-match [ return-match ] [ captured-groups>> ] bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-26 21:24:14 -04:00
										 |  |  | : matches? ( string regexp -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-08-28 14:45:04 -04:00
										 |  |  |     dupd match | 
					
						
							| 
									
										
										
										
											2008-09-09 20:16:11 -04:00
										 |  |  |     [ [ length ] [ length>> 1- ] bi* = ] [ drop f ] if* ;
 | 
					
						
							| 
									
										
										
										
											2008-08-26 21:24:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-19 16:14:05 -04:00
										 |  |  | : match-head ( string regexp -- end/f ) match [ length>> 1- ] [ f ] if* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : 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-09-22 03:28:10 -04:00
										 |  |  | : first-match ( string regexp -- pair/f )
 | 
					
						
							|  |  |  |     0 swap match-range dup [ 2array ] [ 2drop f ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : re-cut ( string regexp -- end/f start )
 | 
					
						
							|  |  |  |     dupd first-match | 
					
						
							|  |  |  |     [ [ second tail-slice ] [ first head ] 2bi ] | 
					
						
							|  |  |  |     [ "" like f swap ] | 
					
						
							|  |  |  |     if* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : re-split ( string regexp -- seq )
 | 
					
						
							|  |  |  |     [ dup ] swap '[ _ re-cut ] [ ] produce nip ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : re-replace ( string regexp replacement -- result )
 | 
					
						
							|  |  |  |     [ re-split ] dip join ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : next-match ( string regexp -- end/f match/f )
 | 
					
						
							|  |  |  |     dupd first-match dup
 | 
					
						
							|  |  |  |     [ [ second tail-slice ] keep ] | 
					
						
							|  |  |  |     [ 2drop f f ] | 
					
						
							|  |  |  |     if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : all-matches ( string regexp -- seq )
 | 
					
						
							|  |  |  |     [ dup ] swap '[ _ next-match ] [ ] produce nip ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : count-matches ( string regexp -- n )
 | 
					
						
							|  |  |  |     all-matches length 1- ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-26 21:24:14 -04:00
										 |  |  | : initial-option ( regexp option -- regexp' )
 | 
					
						
							|  |  |  |     over options>> conjoin ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <regexp> ( string -- regexp )
 | 
					
						
							|  |  |  |     default-regexp construct-regexp ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <iregexp> ( string -- regexp )
 | 
					
						
							|  |  |  |     default-regexp | 
					
						
							|  |  |  |     case-insensitive initial-option | 
					
						
							|  |  |  |     construct-regexp ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <rregexp> ( string -- regexp )
 | 
					
						
							|  |  |  |     default-regexp | 
					
						
							|  |  |  |     reversed-regexp initial-option | 
					
						
							|  |  |  |     construct-regexp ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  |     "i" = [ <iregexp> ] [ <regexp> ] if parsed ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : 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
										 |  |  | 
 | 
					
						
							|  |  |  | : 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 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : option? ( option regexp -- ? )
 | 
					
						
							|  |  |  |     options>> key? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-22 14:37:27 -04:00
										 |  |  | USE: multiline | 
					
						
							|  |  |  | /* | 
					
						
							| 
									
										
										
										
											2008-09-12 22:56:25 -04:00
										 |  |  | M: regexp pprint* | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             dup raw>> | 
					
						
							|  |  |  |             dup find-regexp-syntax swap % swap % % | 
					
						
							|  |  |  |             case-insensitive swap option? [ "i" % ] when
 | 
					
						
							|  |  |  |         ] "" make | 
					
						
							|  |  |  |     ] keep present-text ;
 | 
					
						
							| 
									
										
										
										
											2008-09-22 14:37:27 -04:00
										 |  |  | */ |