]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/regexp/classes/classes.factor
use reject instead of [ ... not ] filter.
[factor.git] / basis / regexp / classes / classes.factor
index d26ff7f69ceab3e20812c1d96a5f34a3b233456b..7ad452a0b03c9fbe993fe5da523b48cdeec7e12a 100644 (file)
@@ -1,78 +1,87 @@
 ! 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 ;
+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: any-char any-char-no-nl
-letter-class LETTER-class Letter-class digit-class
+SINGLETONS: dot letter-class LETTER-class Letter-class digit-class
 alpha-class non-newline-blank-class
 ascii-class punctuation-class java-printable-class blank-class
 control-character-class hex-digit-class java-blank-class c-identifier-class
 unmatchable-class terminator-class word-boundary-class ;
 
-SINGLETONS: beginning-of-input ^ end-of-input $ end-of-file word-break ;
+SINGLETONS: beginning-of-input ^ end-of-input $ end-of-file
+^unix $unix word-break ;
 
-TUPLE: range from to ;
-C: <range> range
+TUPLE: range-class { from read-only } { to read-only } ;
+C: <range-class> range-class
 
-GENERIC: class-member? ( obj class -- ? )
+TUPLE: primitive-class { class read-only } ;
+C: <primitive-class> primitive-class
 
-M: t class-member? ( obj class -- ? ) 2drop t ;
+TUPLE: category-class { category read-only } ;
+C: <category-class> category-class
 
-M: integer class-member? ( obj class -- ? ) = ;
+TUPLE: category-range-class { category read-only } ;
+C: <category-range-class> category-range-class
 
-M: range class-member? ( obj class -- ? )
-    [ from>> ] [ to>> ] bi between? ;
+TUPLE: script-class { script read-only } ;
+C: <script-class> script-class
+
+GENERIC: class-member? ( obj class -- ? )
 
-M: any-char class-member? ( obj class -- ? )
-    2drop t ;
+M: t class-member? ( obj class -- ? ) 2drop t ; inline
 
-M: any-char-no-nl class-member? ( obj class -- ? )
-    drop CHAR: \n = not ;
+M: integer class-member? ( obj class -- ? ) = ; inline
+
+M: range-class class-member? ( obj class -- ? )
+    [ from>> ] [ to>> ] bi between? ; inline
 
 M: letter-class class-member? ( obj class -- ? )
-    drop letter? ;
-            
+    drop letter? ; inline
+
 M: LETTER-class class-member? ( obj class -- ? )
-    drop LETTER? ;
+    drop LETTER? ; inline
 
 M: Letter-class class-member? ( obj class -- ? )
-    drop Letter? ;
+    drop Letter? ; inline
 
 M: ascii-class class-member? ( obj class -- ? )
-    drop ascii? ;
+    drop ascii? ; inline
 
 M: digit-class class-member? ( obj class -- ? )
-    drop digit? ;
+    drop digit? ; inline
 
 : c-identifier-char? ( ch -- ? )
     { [ alpha? ] [ CHAR: _ = ] } 1|| ;
 
 M: c-identifier-class class-member? ( obj class -- ? )
-    drop c-identifier-char? ;
+    drop c-identifier-char? ; inline
 
 M: alpha-class class-member? ( obj class -- ? )
-    drop alpha? ;
+    drop alpha? ; inline
 
 : punct? ( ch -- ? )
     "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" member? ;
 
 M: punctuation-class class-member? ( obj class -- ? )
-    drop punct? ;
+    drop punct? ; inline
 
 : java-printable? ( ch -- ? )
     { [ alpha? ] [ punct? ] } 1|| ;
 
 M: java-printable-class class-member? ( obj class -- ? )
-    drop java-printable? ;
+    drop java-printable? ; inline
 
 M: non-newline-blank-class class-member? ( obj class -- ? )
-    drop { [ blank? ] [ CHAR: \n = not ] } 1&& ;
+    drop { [ blank? ] [ CHAR: \n = not ] } 1&& ; inline
 
 M: control-character-class class-member? ( obj class -- ? )
-    drop control? ;
+    drop control? ; inline
 
 : hex-digit? ( ch -- ? )
     {
@@ -82,96 +91,110 @@ M: control-character-class class-member? ( obj class -- ? )
     } 1|| ;
 
 M: hex-digit-class class-member? ( obj class -- ? )
-    drop hex-digit? ;
+    drop hex-digit? ; inline
 
 : 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 -- ? )
-    drop java-blank? ;
+    drop java-blank? ; inline
 
 M: unmatchable-class class-member? ( obj class -- ? )
-    2drop f ;
+    2drop f ; inline
 
 M: terminator-class class-member? ( obj class -- ? )
-    drop "\r\n\u000085\u002029\u002028" member? ;
+    drop "\r\n\u000085\u002029\u002028" member? ; inline
 
-M: ^ class-member? ( obj class -- ? )
-    2drop f ;
+M: f class-member? 2drop f ; inline
 
-M: $ class-member? ( obj class -- ? )
-    2drop f ;
+: same? ( obj1 obj2 quot1: ( obj1 -- val1 ) quot2: ( obj2 -- val2 ) -- ? )
+    bi* = ; inline
 
-M: f class-member? 2drop f ;
+M: script-class class-member?
+    [ script-of ] [ script>> ] same? ; inline
 
-TUPLE: primitive-class class ;
-C: <primitive-class> primitive-class
+M: category-class class-member?
+    [ category ] [ category>> ] same? ; inline
 
-TUPLE: not-class class ;
+M: category-range-class class-member? inline
+    [ category first ] [ category>> ] same? ; inline
+
+TUPLE: not-class { class read-only } ;
 
 PREDICATE: not-integer < not-class class>> integer? ;
-PREDICATE: not-primitive < not-class class>> primitive-class? ;
+
+UNION: simple-class
+    primitive-class range-class dot ;
+PREDICATE: not-simple < not-class class>> simple-class? ;
 
 M: not-class class-member?
-    class>> class-member? not ;
+    class>> class-member? not ; inline
 
-TUPLE: or-class seq ;
+TUPLE: or-class { seq read-only } ;
 
 M: or-class class-member?
-    seq>> [ class-member? ] with any? ;
+    seq>> [ class-member? ] with any? ; inline
 
-TUPLE: and-class seq ;
+TUPLE: and-class { seq read-only } ;
 
 M: and-class class-member?
-    seq>> [ class-member? ] with all? ;
+    seq>> [ class-member? ] with all? ; inline
 
 DEFER: substitute
 
 : flatten ( seq class -- newseq )
     '[ dup _ instance? [ seq>> ] [ 1array ] if ] map concat ; inline
 
-:: seq>instance ( seq empty class -- instance )
+:: sequence>instance ( seq empty class -- instance )
     seq length {
         { 0 [ empty ] }
         { 1 [ seq first ] }
-        [ drop class new seq { } like >>seq ]
+        [ drop seq { } like class boa ]
     } case ; inline
 
-TUPLE: class-partition integers not-integers primitives not-primitives and or other ;
+TUPLE: class-partition integers not-integers simples not-simples and or other ;
 
 : partition-classes ( seq -- class-partition )
-    prune
+    members
     [ integer? ] partition
     [ not-integer? ] partition
-    [ primitive-class? ] partition ! extend primitive-class to epsilon tags
-    [ not-primitive? ] partition
+    [ simple-class? ] partition
+    [ not-simple? ] partition
     [ and-class? ] partition
     [ or-class? ] partition
     class-partition boa ;
 
-: class-partition>seq ( class-partition -- seq )
-    make-mirror values concat ;
+: class-partition>sequence ( class-partition -- seq )
+    {
+        [ integers>> ]
+        [ not-integers>> ]
+        [ simples>> ]
+        [ not-simples>> ]
+        [ and>> ]
+        [ or>> ]
+        [ other>> ]
+    } cleave>array concat ;
 
 : repartition ( partition -- partition' )
     ! This could be made more efficient; only and and or are effected
-    class-partition>seq partition-classes ;
+    class-partition>sequence partition-classes ;
 
 : filter-not-integers ( partition -- partition' )
     dup
-    [ primitives>> ] [ not-primitives>> ] [ or>> ] tri
+    [ simples>> ] [ not-simples>> ] [ or>> ] tri
     3append and-class boa
     '[ [ class>> _ class-member? ] filter ] change-not-integers ;
 
 : answer-ors ( partition -- partition' )
-    dup [ not-integers>> ] [ not-primitives>> ] [ primitives>> ] tri 3append
+    dup [ not-integers>> ] [ not-simples>> ] [ simples>> ] tri 3append
     '[ [ _ [ t substitute ] each ] map ] change-or ;
 
 : contradiction? ( partition -- ? )
     {
-        [ [ primitives>> ] [ not-primitives>> ] bi intersects? ]
+        [ [ simples>> ] [ not-simples>> ] bi intersects? ]
         [ other>> f swap member? ]
     } 1|| ;
 
@@ -180,7 +203,7 @@ TUPLE: class-partition integers not-integers primitives not-primitives and or ot
     [ 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>sequence members t and-class sequence>instance ] if ;
 
 : <and-class> ( seq -- class )
     dup and-class flatten partition-classes
@@ -192,17 +215,17 @@ TUPLE: class-partition integers not-integers primitives not-primitives and or ot
 
 : filter-integers ( partition -- partition' )
     dup
-    [ primitives>> ] [ not-primitives>> ] [ and>> ] tri
+    [ simples>> ] [ not-simples>> ] [ and>> ] tri
     3append or-class boa
-    '[ [ _ class-member? not ] filter ] change-integers ;
+    '[ [ _ class-member? ] reject ] change-integers ;
 
 : answer-ands ( partition -- partition' )
-    dup [ integers>> ] [ not-primitives>> ] [ primitives>> ] tri 3append
+    dup [ integers>> ] [ not-simples>> ] [ simples>> ] tri 3append
     '[ [ _ [ f substitute ] each ] map ] change-and ;
 
 : tautology? ( partition -- ? )
     {
-        [ [ primitives>> ] [ not-primitives>> ] bi intersects? ]
+        [ [ simples>> ] [ not-simples>> ] bi intersects? ]
         [ other>> t swap member? ]
     } 1|| ;
 
@@ -211,13 +234,16 @@ TUPLE: class-partition integers not-integers primitives not-primitives and or ot
     [ 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>sequence members f or-class sequence>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 ;
 
@@ -238,10 +264,14 @@ M: or-class <not-class>
 M: t <not-class> drop f ;
 M: f <not-class> drop t ;
 
-M: primitive-class class-member?
-    class>> class-member? ;
+: <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> ;
 
-UNION: class primitive-class not-class or-class and-class range ;
+M: primitive-class class-member?
+    class>> class-member? ; inline
 
 TUPLE: condition question yes no ;
 C: <condition> condition
@@ -268,8 +298,7 @@ M: object substitute answer ;
 M: not-class substitute [ <not-class> ] bi@ answer ;
 
 : assoc-answer ( table question answer -- new-table )
-    '[ _ _ substitute ] assoc-map
-    [ nip ] assoc-filter ;
+    '[ _ _ substitute ] assoc-map sift-values ;
 
 : assoc-answers ( table questions answer -- new-table )
     '[ _ assoc-answer ] each ;
@@ -308,7 +337,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 )