]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/regexp/classes/classes.factor
use radix literals
[factor.git] / basis / regexp / classes / classes.factor
index 28b0ed1563441aa7a410fa04fc50e434c6f685cd..0f0128a5a58f9dbdb7cdf62051ebe4e911787f27 100644 (file)
@@ -1,9 +1,11 @@
 ! Copyright (C) 2008, 2009 Doug Coleman, Daniel Ehrenberg.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors kernel math math.order words combinators locals
-ascii unicode.categories combinators.short-circuit sequences
-fry macros arrays assocs sets classes mirrors unicode.script
-unicode.data ;
+USING: accessors kernel math math.order words combinators
+combinators.smart combinators.short-circuit locals
+unicode.categories sequences fry macros arrays assocs sets
+classes unicode.script unicode.data ;
+FROM: ascii => ascii? ;
+FROM: sets => members ;
 IN: regexp.classes
 
 SINGLETONS: dot letter-class LETTER-class Letter-class digit-class
@@ -94,7 +96,7 @@ M: hex-digit-class class-member? ( obj class -- ? )
 : java-blank? ( ch -- ? )
     {
         CHAR: \s CHAR: \t CHAR: \n
-        HEX: b HEX: 7 CHAR: \r
+        0xb 0x7 CHAR: \r
     } member? ;
 
 M: java-blank-class class-member? ( obj class -- ? )
@@ -108,21 +110,24 @@ M: terminator-class class-member? ( obj class -- ? )
 
 M: f class-member? 2drop f ;
 
+: same? ( obj1 obj2 quot1: ( obj1 -- val1 ) quot2: ( obj2 -- val2 ) -- ? )
+    bi* = ; inline
+
 M: script-class class-member?
-    [ script-of ] [ script>> ] bi* = ;
+    [ script-of ] [ script>> ] same? ;
 
 M: category-class class-member?
-    [ category# ] [ category>> ] bi* = ;
+    [ category ] [ category>> ] same? ;
 
 M: category-range-class class-member?
-    [ category first ] [ category>> ] bi* = ;
+    [ category first ] [ category>> ] same? ;
 
 TUPLE: not-class class ;
 
 PREDICATE: not-integer < not-class class>> integer? ;
 
 UNION: simple-class
-    primitive-class range-class category-class category-range-class dot ;
+    primitive-class range-class dot ;
 PREDICATE: not-simple < not-class class>> simple-class? ;
 
 M: not-class class-member?
@@ -153,7 +158,7 @@ DEFER: substitute
 TUPLE: class-partition integers not-integers simples not-simples and or other ;
 
 : partition-classes ( seq -- class-partition )
-    prune
+    members
     [ integer? ] partition
     [ not-integer? ] partition
     [ simple-class? ] partition
@@ -163,7 +168,17 @@ TUPLE: class-partition integers not-integers simples not-simples and or other ;
     class-partition boa ;
 
 : class-partition>seq ( class-partition -- seq )
-    make-mirror values concat ;
+    [
+        {
+            [ integers>> ]
+            [ not-integers>> ]
+            [ simples>> ]
+            [ not-simples>> ]
+            [ and>> ]
+            [ or>> ]
+            [ other>> ]
+        } cleave
+    ] output>array concat ;
 
 : repartition ( partition -- partition' )
     ! This could be made more efficient; only and and or are effected
@@ -190,7 +205,7 @@ TUPLE: class-partition integers not-integers simples not-simples and or other ;
     [ t swap remove ] change-other
     dup contradiction?
     [ drop f ]
-    [ filter-not-integers class-partition>seq prune t and-class seq>instance ] if ;
+    [ filter-not-integers class-partition>seq members t and-class seq>instance ] if ;
 
 : <and-class> ( seq -- class )
     dup and-class flatten partition-classes
@@ -221,13 +236,16 @@ TUPLE: class-partition integers not-integers simples not-simples and or other ;
     [ f swap remove ] change-other
     dup tautology?
     [ drop t ]
-    [ filter-integers class-partition>seq prune f or-class seq>instance ] if ;
+    [ filter-integers class-partition>seq members f or-class seq>instance ] if ;
 
 : <or-class> ( seq -- class )
     dup or-class flatten partition-classes
     dup not-integers>> length {
         { 0 [ nip make-or-class ] }
-        { 1 [ not-integers>> first [ class>> '[ _ swap class-member? ] any? ] keep or ] }
+        { 1 [
+            not-integers>> first
+            [ class>> '[ _ swap class-member? ] any? ] keep or
+        ] }
         [ 3drop t ]
     } case ;
 
@@ -248,6 +266,12 @@ M: or-class <not-class>
 M: t <not-class> drop f ;
 M: f <not-class> drop t ;
 
+: <minus-class> ( a b -- a-b )
+    <not-class> 2array <and-class> ;
+
+: <sym-diff-class> ( a b -- a~b )
+    2array [ <or-class> ] [ <and-class> ] bi <minus-class> ;
+
 M: primitive-class class-member?
     class>> class-member? ;
 
@@ -316,7 +340,7 @@ M: object class>questions 1array ;
 : condition-states ( condition -- states )
     dup condition? [
         [ yes>> ] [ no>> ] bi
-        [ condition-states ] bi@ append prune
+        [ condition-states ] bi@ union
     ] [ 1array ] if ;
 
 : condition-at ( condition assoc -- new-condition )