]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/regexp/classes/classes.factor
use radix literals
[factor.git] / basis / regexp / classes / classes.factor
index 4ddd47018998a52f9ea679bd251e8c65afbb2a23..0f0128a5a58f9dbdb7cdf62051ebe4e911787f27 100644 (file)
@@ -1,21 +1,36 @@
 ! 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 ;
+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 ;
+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 to ;
+C: <range-class> range-class
+
+TUPLE: primitive-class class ;
+C: <primitive-class> primitive-class
+
+TUPLE: category-class category ;
+C: <category-class> category-class
+
+TUPLE: category-range-class category ;
+C: <category-range-class> category-range-class
+
+TUPLE: script-class script ;
+C: <script-class> script-class
 
 GENERIC: class-member? ( obj class -- ? )
 
@@ -23,15 +38,9 @@ M: t class-member? ( obj class -- ? ) 2drop t ;
 
 M: integer class-member? ( obj class -- ? ) = ;
 
-M: range class-member? ( obj class -- ? )
+M: range-class class-member? ( obj class -- ? )
     [ from>> ] [ to>> ] bi between? ;
 
-M: any-char class-member? ( obj class -- ? )
-    2drop t ;
-
-M: any-char-no-nl class-member? ( obj class -- ? )
-    drop CHAR: \n = not ;
-
 M: letter-class class-member? ( obj class -- ? )
     drop letter? ;
             
@@ -87,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 -- ? )
@@ -99,108 +108,146 @@ M: unmatchable-class class-member? ( obj class -- ? )
 M: terminator-class class-member? ( obj class -- ? )
     drop "\r\n\u000085\u002029\u002028" member? ;
 
-M: ^ class-member? ( obj class -- ? )
-    2drop f ;
-
-M: $ class-member? ( obj class -- ? )
-    2drop f ;
-
 M: f class-member? 2drop f ;
 
-TUPLE: primitive-class class ;
-C: <primitive-class> primitive-class
-
-TUPLE: or-class seq ;
-
-TUPLE: not-class class ;
-
-TUPLE: and-class seq ;
-
-GENERIC: combine-and ( class1 class2 -- combined ? )
+: same? ( obj1 obj2 quot1: ( obj1 -- val1 ) quot2: ( obj2 -- val2 ) -- ? )
+    bi* = ; inline
 
-: replace-if-= ( object object -- object ? )
-    over = ;
+M: script-class class-member?
+    [ script-of ] [ script>> ] same? ;
 
-M: object combine-and replace-if-= ;
+M: category-class class-member?
+    [ category ] [ category>> ] same? ;
 
-M: t combine-and
-    drop t ;
+M: category-range-class class-member?
+    [ category first ] [ category>> ] same? ;
 
-M: f combine-and
-    nip t ;
+TUPLE: not-class class ;
 
-M: not-class combine-and
-    class>> 2dup = [ 2drop f t ] [
-        dup integer? [
-            2dup swap class-member?
-            [ 2drop f f ]
-            [ drop t ] if
-        ] [ 2drop f f ] if
-    ] if ;
+PREDICATE: not-integer < not-class class>> integer? ;
 
-M: integer combine-and
-    swap 2dup class-member? [ drop t ] [ 2drop f t ] if ;
+UNION: simple-class
+    primitive-class range-class dot ;
+PREDICATE: not-simple < not-class class>> simple-class? ;
 
-GENERIC: combine-or ( class1 class2 -- combined ? )
+M: not-class class-member?
+    class>> class-member? not ;
 
-M: object combine-or replace-if-= ;
+TUPLE: or-class seq ;
 
-M: t combine-or
-    nip t ;
+M: or-class class-member?
+    seq>> [ class-member? ] with any? ;
 
-M: f combine-or
-    drop t ;
+TUPLE: and-class seq ;
 
-M: not-class combine-or
-    class>> = [ t t ] [ f f ] if ;
+M: and-class class-member?
+    seq>> [ class-member? ] with all? ;
 
-M: integer combine-or
-    2dup swap class-member? [ drop t ] [ 2drop f f ] if ;
+DEFER: substitute
 
 : flatten ( seq class -- newseq )
     '[ dup _ instance? [ seq>> ] [ 1array ] if ] map concat ; inline
 
-: try-combine ( elt1 elt2 quot -- combined/f ? )
-    3dup call [ [ 3drop ] dip t ] [ drop swapd call ] if ; inline
-
-DEFER: answer
-
-:: try-cancel ( elt1 elt2 empty -- combined/f ? )
-    [ elt1 elt2 empty answer dup elt1 = not ] try-combine ;
-
-:: prefix-combining ( seq elt quot: ( elt1 elt2 -- combined/f ? ) -- newseq )
-    f :> combined!
-    seq [ elt quot call swap combined! ] find drop
-    [ seq remove-nth combined prefix ]
-    [ seq elt prefix ] if* ; inline
-
-: combine-by ( seq quot -- new-seq )
-    { } swap '[ _ prefix-combining ] reduce ; inline
-
 :: seq>instance ( seq empty class -- instance )
     seq length {
         { 0 [ empty ] }
         { 1 [ seq first ] }
-        [ drop class new seq >>seq ]
+        [ drop class new seq { } like >>seq ]
     } case ; inline
 
-:: combine ( seq quot: ( elt1 elt2 -- combined/f ? ) empty class -- newseq )
-    seq class flatten
-    [ quot try-combine ] combine-by
-    ! [ empty try-cancel ] combine-by ! This makes the algorithm O(n^4)
-    empty class seq>instance ; inline
+TUPLE: class-partition integers not-integers simples not-simples and or other ;
+
+: partition-classes ( seq -- class-partition )
+    members
+    [ integer? ] partition
+    [ not-integer? ] partition
+    [ simple-class? ] partition
+    [ not-simple? ] partition
+    [ and-class? ] partition
+    [ or-class? ] partition
+    class-partition boa ;
+
+: class-partition>seq ( class-partition -- seq )
+    [
+        {
+            [ 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
+    class-partition>seq partition-classes ;
+
+: filter-not-integers ( partition -- partition' )
+    dup
+    [ simples>> ] [ not-simples>> ] [ or>> ] tri
+    3append and-class boa
+    '[ [ class>> _ class-member? ] filter ] change-not-integers ;
+
+: answer-ors ( partition -- partition' )
+    dup [ not-integers>> ] [ not-simples>> ] [ simples>> ] tri 3append
+    '[ [ _ [ t substitute ] each ] map ] change-or ;
+
+: contradiction? ( partition -- ? )
+    {
+        [ [ simples>> ] [ not-simples>> ] bi intersects? ]
+        [ other>> f swap member? ]
+    } 1|| ;
+
+: make-and-class ( partition -- and-class )
+    answer-ors repartition
+    [ t swap remove ] change-other
+    dup contradiction?
+    [ drop f ]
+    [ filter-not-integers class-partition>seq members t and-class seq>instance ] if ;
 
 : <and-class> ( seq -- class )
-    [ combine-and ] t and-class combine ;
+    dup and-class flatten partition-classes
+    dup integers>> length {
+        { 0 [ nip make-and-class ] }
+        { 1 [ integers>> first [ '[ _ swap class-member? ] all? ] keep and ] }
+        [ 3drop f ]
+    } case ;
+
+: filter-integers ( partition -- partition' )
+    dup
+    [ simples>> ] [ not-simples>> ] [ and>> ] tri
+    3append or-class boa
+    '[ [ _ class-member? not ] filter ] change-integers ;
+
+: answer-ands ( partition -- partition' )
+    dup [ integers>> ] [ not-simples>> ] [ simples>> ] tri 3append
+    '[ [ _ [ f substitute ] each ] map ] change-and ;
+
+: tautology? ( partition -- ? )
+    {
+        [ [ simples>> ] [ not-simples>> ] bi intersects? ]
+        [ other>> t swap member? ]
+    } 1|| ;
 
-M: and-class class-member?
-    seq>> [ class-member? ] with all? ;
+: make-or-class ( partition -- and-class )
+    answer-ands repartition
+    [ f swap remove ] change-other
+    dup tautology?
+    [ drop t ]
+    [ filter-integers class-partition>seq members f or-class seq>instance ] if ;
 
 : <or-class> ( seq -- class )
-    [ combine-or ] f or-class combine ;
-
-M: or-class class-member?
-    seq>> [ class-member? ] with any? ;
+    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
+        ] }
+        [ 3drop t ]
+    } case ;
 
 GENERIC: <not-class> ( class -- inverse )
 
@@ -219,14 +266,15 @@ M: or-class <not-class>
 M: t <not-class> drop f ;
 M: f <not-class> drop t ;
 
-M: not-class class-member?
-    class>> class-member? not ;
+: <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? ;
 
-UNION: class primitive-class not-class or-class and-class range ;
-
 TUPLE: condition question yes no ;
 C: <condition> condition
 
@@ -247,8 +295,12 @@ M: or-class answer
 M: not-class answer
     [ class>> ] 2dip answer <not-class> ;
 
+GENERIC# substitute 1 ( class from to -- new-class )
+M: object substitute answer ;
+M: not-class substitute [ <not-class> ] bi@ answer ;
+
 : assoc-answer ( table question answer -- new-table )
-    '[ _ _ answer ] assoc-map
+    '[ _ _ substitute ] assoc-map
     [ nip ] assoc-filter ;
 
 : assoc-answers ( table questions answer -- new-table )
@@ -288,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 )