Merge branch 'master' of git://factorcode.org/git/factor
[factor/jcg.git] / basis / regexp / classes / classes.factor
blob4a807fa51bbc0f815282c086e77d136517707b69
1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel math math.order words regexp.utils
4 unicode.categories combinators.short-circuit ;
5 IN: regexp.classes
7 SINGLETONS: any-char any-char-no-nl
8 letter-class LETTER-class Letter-class digit-class
9 alpha-class non-newline-blank-class
10 ascii-class punctuation-class java-printable-class blank-class
11 control-character-class hex-digit-class java-blank-class c-identifier-class
12 unmatchable-class terminator-class word-boundary-class ;
14 SINGLETONS: beginning-of-input beginning-of-line
15 end-of-input end-of-line ;
17 MIXIN: node
18 TUPLE: character-class-range from to ; INSTANCE: character-class-range node
20 GENERIC: class-member? ( obj class -- ? )
22 M: t class-member? ( obj class -- ? ) 2drop f ;
24 M: integer class-member? ( obj class -- ? ) 2drop f ;
26 M: character-class-range class-member? ( obj class -- ? )
27     [ from>> ] [ to>> ] bi between? ;
29 M: any-char class-member? ( obj class -- ? )
30     2drop t ;
32 M: any-char-no-nl class-member? ( obj class -- ? )
33     drop CHAR: \n = not ;
35 M: letter-class class-member? ( obj class -- ? )
36     drop letter? ;
37             
38 M: LETTER-class class-member? ( obj class -- ? )
39     drop LETTER? ;
41 M: Letter-class class-member? ( obj class -- ? )
42     drop Letter? ;
44 M: ascii-class class-member? ( obj class -- ? )
45     drop ascii? ;
47 M: digit-class class-member? ( obj class -- ? )
48     drop digit? ;
50 M: c-identifier-class class-member? ( obj class -- ? )
51     drop
52     { [ digit? ] [ Letter? ] [ CHAR: _ = ] } 1|| ;
54 M: alpha-class class-member? ( obj class -- ? )
55     drop alpha? ;
57 M: punctuation-class class-member? ( obj class -- ? )
58     drop punct? ;
60 M: java-printable-class class-member? ( obj class -- ? )
61     drop java-printable? ;
63 M: non-newline-blank-class class-member? ( obj class -- ? )
64     drop { [ blank? ] [ CHAR: \n = not ] } 1&& ;
66 M: control-character-class class-member? ( obj class -- ? )
67     drop control-char? ;
69 M: hex-digit-class class-member? ( obj class -- ? )
70     drop hex-digit? ;
72 M: java-blank-class class-member? ( obj class -- ? )
73     drop java-blank? ;
75 M: unmatchable-class class-member? ( obj class -- ? )
76     2drop f ;
78 M: terminator-class class-member? ( obj class -- ? )
79     drop {
80         [ CHAR: \r = ]
81         [ CHAR: \n = ]
82         [ CHAR: \u000085 = ]
83         [ CHAR: \u002028 = ]
84         [ CHAR: \u002029 = ]
85     } 1|| ;
87 M: beginning-of-line class-member? ( obj class -- ? )
88     2drop f ;
90 M: end-of-line class-member? ( obj class -- ? )
91     2drop f ;