]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/regexp/regexp.factor
regexp: one R/ syntax to rule them all, simpler this way.
[factor.git] / basis / regexp / regexp.factor
index 6c7699bce3b54135ba86cccdeacd51b79b727846..96867381ee3e264c2ff8e738f7727d38becdedb1 100644 (file)
@@ -197,42 +197,26 @@ PRIVATE>
 
 <PRIVATE
 
-! The following two should do some caching
-
-: find-regexp-syntax ( string -- prefix suffix )
-    {
-        { "R/ "  "/"  }
-        { "R! "  "!"  }
-        { "R# "  "#"  }
-        { "R' "  "'"  }
-        { "R( "  ")"  }
-        { "R@ "  "@"  }
-        { "R| "  "|"  }
-    } swap [ subseq? not nip ] curry assoc-find drop ;
-
-: take-until ( end lexer -- string )
+: take-until ( lexer -- string )
     dup skip-blank [
-        [ index-from ] 2keep
-        [ swapd subseq ]
-        [ 2drop 1 + ] 3bi
+        dupd [
+            [ CHAR: / -rot index-from ] keep
+            over [ "Unterminated regexp" throw ] unless
+            2dup [ 1 - ] dip nth CHAR: \\ =
+            [ [ [ 1 + ] dip ] when ] keep
+        ] loop over [ subseq ] dip 1 +
     ] change-lexer-column ;
 
 : parse-noblank-token ( lexer -- str/f )
     dup still-parsing-line? [ (parse-token) ] [ drop f ] if ;
 
-: parsing-regexp ( accum end -- accum )
+: parse-regexp ( accum -- accum )
     lexer get [ take-until ] [ parse-noblank-token ] bi
     <optioned-regexp> compile-next-match suffix! ;
 
 PRIVATE>
 
-SYNTAX: R! CHAR: ! parsing-regexp ;
-SYNTAX: R# CHAR: # parsing-regexp ;
-SYNTAX: R' CHAR: ' parsing-regexp ;
-SYNTAX: R( CHAR: ) parsing-regexp ;
-SYNTAX: R/ CHAR: / parsing-regexp ;
-SYNTAX: R@ CHAR: @ parsing-regexp ;
-SYNTAX: R| CHAR: | parsing-regexp ;
+SYNTAX: R/ parse-regexp ;
 
 USE: vocabs.loader