2009-02-19 17:48:46 -05:00
|
|
|
! Copyright (C) 2008, 2009 Doug Coleman, Daniel Ehrenberg.
|
2008-08-26 21:24:14 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-02-21 18:13:11 -05:00
|
|
|
USING: accessors kernel math math.order words combinators locals
|
2009-02-18 13:27:07 -05:00
|
|
|
ascii unicode.categories combinators.short-circuit sequences ;
|
2009-02-21 18:13:11 -05:00
|
|
|
QUALIFIED-WITH: multi-methods m
|
2008-09-18 15:42:16 -04:00
|
|
|
IN: regexp.classes
|
2008-08-26 21:24:14 -04:00
|
|
|
|
2008-11-24 23:17:47 -05:00
|
|
|
SINGLETONS: any-char any-char-no-nl
|
|
|
|
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 beginning-of-line
|
|
|
|
end-of-input end-of-line ;
|
|
|
|
|
2009-02-18 13:27:07 -05:00
|
|
|
TUPLE: range from to ;
|
|
|
|
C: <range> range
|
2008-11-24 23:17:47 -05:00
|
|
|
|
2008-08-26 21:24:14 -04:00
|
|
|
GENERIC: class-member? ( obj class -- ? )
|
|
|
|
|
2009-02-19 17:48:46 -05:00
|
|
|
! When does t get put in?
|
2008-11-24 23:17:47 -05:00
|
|
|
M: t class-member? ( obj class -- ? ) 2drop f ;
|
2008-11-24 01:18:27 -05:00
|
|
|
|
2009-02-19 17:48:46 -05:00
|
|
|
M: integer class-member? ( obj class -- ? ) = ;
|
2008-08-26 21:24:14 -04:00
|
|
|
|
2009-02-18 13:27:07 -05:00
|
|
|
M: range class-member? ( obj class -- ? )
|
2008-08-26 21:24:14 -04:00
|
|
|
[ from>> ] [ to>> ] bi between? ;
|
|
|
|
|
|
|
|
M: any-char class-member? ( obj class -- ? )
|
|
|
|
2drop t ;
|
2008-11-22 22:04:09 -05:00
|
|
|
|
|
|
|
M: any-char-no-nl class-member? ( obj class -- ? )
|
|
|
|
drop CHAR: \n = not ;
|
2008-11-24 23:17:47 -05:00
|
|
|
|
2008-08-26 21:24:14 -04:00
|
|
|
M: letter-class class-member? ( obj class -- ? )
|
|
|
|
drop letter? ;
|
|
|
|
|
|
|
|
M: LETTER-class class-member? ( obj class -- ? )
|
|
|
|
drop LETTER? ;
|
|
|
|
|
|
|
|
M: Letter-class class-member? ( obj class -- ? )
|
|
|
|
drop Letter? ;
|
|
|
|
|
|
|
|
M: ascii-class class-member? ( obj class -- ? )
|
|
|
|
drop ascii? ;
|
|
|
|
|
|
|
|
M: digit-class class-member? ( obj class -- ? )
|
|
|
|
drop digit? ;
|
|
|
|
|
2009-02-20 18:54:48 -05:00
|
|
|
: c-identifier-char? ( ch -- ? )
|
|
|
|
{ [ alpha? ] [ CHAR: _ = ] } 1|| ;
|
|
|
|
|
2008-11-18 16:10:24 -05:00
|
|
|
M: c-identifier-class class-member? ( obj class -- ? )
|
2009-02-20 18:54:48 -05:00
|
|
|
drop c-identifier-char? ;
|
2008-11-18 16:10:24 -05:00
|
|
|
|
2008-08-26 21:24:14 -04:00
|
|
|
M: alpha-class class-member? ( obj class -- ? )
|
|
|
|
drop alpha? ;
|
|
|
|
|
2009-02-20 18:54:48 -05:00
|
|
|
: punct? ( ch -- ? )
|
|
|
|
"!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" member? ;
|
|
|
|
|
2008-08-26 21:24:14 -04:00
|
|
|
M: punctuation-class class-member? ( obj class -- ? )
|
|
|
|
drop punct? ;
|
|
|
|
|
2009-02-20 18:54:48 -05:00
|
|
|
: java-printable? ( ch -- ? )
|
|
|
|
{ [ alpha? ] [ punct? ] } 1|| ;
|
|
|
|
|
2008-08-26 21:24:14 -04:00
|
|
|
M: java-printable-class class-member? ( obj class -- ? )
|
|
|
|
drop java-printable? ;
|
|
|
|
|
|
|
|
M: non-newline-blank-class class-member? ( obj class -- ? )
|
|
|
|
drop { [ blank? ] [ CHAR: \n = not ] } 1&& ;
|
|
|
|
|
|
|
|
M: control-character-class class-member? ( obj class -- ? )
|
2009-02-15 15:28:22 -05:00
|
|
|
drop control? ;
|
2008-08-26 21:24:14 -04:00
|
|
|
|
2009-02-20 18:54:48 -05:00
|
|
|
: hex-digit? ( ch -- ? )
|
|
|
|
{
|
|
|
|
[ CHAR: A CHAR: F between? ]
|
|
|
|
[ CHAR: a CHAR: f between? ]
|
|
|
|
[ CHAR: 0 CHAR: 9 between? ]
|
|
|
|
} 1|| ;
|
|
|
|
|
2008-08-26 21:24:14 -04:00
|
|
|
M: hex-digit-class class-member? ( obj class -- ? )
|
|
|
|
drop hex-digit? ;
|
|
|
|
|
2009-02-20 18:54:48 -05:00
|
|
|
: java-blank? ( ch -- ? )
|
|
|
|
{
|
|
|
|
CHAR: \s CHAR: \t CHAR: \n
|
|
|
|
HEX: b HEX: 7 CHAR: \r
|
|
|
|
} member? ;
|
|
|
|
|
2008-08-26 21:24:14 -04:00
|
|
|
M: java-blank-class class-member? ( obj class -- ? )
|
|
|
|
drop java-blank? ;
|
|
|
|
|
|
|
|
M: unmatchable-class class-member? ( obj class -- ? )
|
|
|
|
2drop f ;
|
2008-11-24 01:18:27 -05:00
|
|
|
|
|
|
|
M: terminator-class class-member? ( obj class -- ? )
|
2009-02-20 18:54:48 -05:00
|
|
|
drop "\r\n\u000085\u002029\u002028" member? ;
|
2008-11-24 23:17:47 -05:00
|
|
|
|
|
|
|
M: beginning-of-line class-member? ( obj class -- ? )
|
|
|
|
2drop f ;
|
|
|
|
|
|
|
|
M: end-of-line class-member? ( obj class -- ? )
|
|
|
|
2drop f ;
|
2009-02-19 17:48:46 -05:00
|
|
|
|
2009-02-21 18:13:11 -05:00
|
|
|
M: f class-member? 2drop f ;
|
|
|
|
|
|
|
|
TUPLE: primitive-class class ;
|
|
|
|
C: <primitive-class> primitive-class
|
|
|
|
|
2009-02-19 17:48:46 -05:00
|
|
|
TUPLE: or-class seq ;
|
|
|
|
|
|
|
|
TUPLE: not-class class ;
|
|
|
|
|
2009-02-21 13:09:41 -05:00
|
|
|
TUPLE: and-class seq ;
|
2009-02-20 18:54:48 -05:00
|
|
|
|
2009-02-21 18:13:11 -05:00
|
|
|
m:GENERIC: combine-and ( class1 class2 -- combined ? )
|
|
|
|
|
|
|
|
m:METHOD: combine-and { object object } 2drop f f ;
|
|
|
|
|
|
|
|
m:METHOD: combine-and { integer integer }
|
|
|
|
2dup = [ drop t ] [ 2drop f t ] if ;
|
|
|
|
|
|
|
|
m:METHOD: combine-and { t object }
|
|
|
|
nip t ;
|
|
|
|
|
|
|
|
m:METHOD: combine-and { f object }
|
|
|
|
drop t ;
|
|
|
|
|
|
|
|
m:METHOD: combine-and { integer object }
|
|
|
|
2dup class-member? [ drop t ] [ 2drop f t ] if ;
|
|
|
|
|
|
|
|
m:GENERIC: combine-or ( class1 class2 -- combined ? )
|
|
|
|
|
|
|
|
m:METHOD: combine-or { object object } 2drop f f ;
|
|
|
|
|
|
|
|
m:METHOD: combine-or { integer integer }
|
|
|
|
2dup = [ drop t ] [ 2drop f f ] if ;
|
|
|
|
|
|
|
|
m:METHOD: combine-or { t object }
|
|
|
|
drop t ;
|
|
|
|
|
|
|
|
m:METHOD: combine-or { f object }
|
|
|
|
nip t ;
|
|
|
|
|
|
|
|
m:METHOD: combine-or { integer object }
|
|
|
|
2dup class-member? [ nip t ] [ 2drop f f ] if ;
|
|
|
|
|
|
|
|
: try-combine ( elt1 elt2 quot -- combined/f ? )
|
|
|
|
3dup call [ [ 3drop ] dip t ] [ drop swapd call ] if ; inline
|
|
|
|
|
|
|
|
:: prefix-combining ( seq elt quot: ( elt1 elt2 -- combined/f ? ) -- newseq )
|
|
|
|
f :> combined!
|
|
|
|
seq [ elt quot try-combine swap combined! ] find drop
|
|
|
|
[ seq remove-nth combined prefix ]
|
|
|
|
[ seq elt prefix ] if* ; inline
|
|
|
|
|
|
|
|
:: combine ( seq quot: ( elt1 elt2 -- combined/f ? ) empty class -- newseq )
|
|
|
|
seq { } [ quot prefix-combining ] reduce
|
|
|
|
dup length {
|
|
|
|
{ 0 [ drop empty ] }
|
|
|
|
{ 1 [ first ] }
|
|
|
|
[ drop class new swap >>seq ]
|
|
|
|
} case ; inline
|
2009-02-19 19:28:54 -05:00
|
|
|
|
2009-02-21 13:09:41 -05:00
|
|
|
: <and-class> ( seq -- class )
|
2009-02-21 18:13:11 -05:00
|
|
|
[ combine-and ] t and-class combine ;
|
2009-02-21 13:09:41 -05:00
|
|
|
|
|
|
|
M: and-class class-member?
|
|
|
|
seq>> [ class-member? ] with all? ;
|
|
|
|
|
|
|
|
: <or-class> ( seq -- class )
|
2009-02-21 18:13:11 -05:00
|
|
|
[ combine-or ] t or-class combine ;
|
2009-02-21 13:09:41 -05:00
|
|
|
|
2009-02-19 17:48:46 -05:00
|
|
|
M: or-class class-member?
|
|
|
|
seq>> [ class-member? ] with any? ;
|
|
|
|
|
2009-02-21 13:09:41 -05:00
|
|
|
: <not-class> ( class -- inverse )
|
|
|
|
{
|
|
|
|
{ t [ f ] }
|
|
|
|
{ f [ t ] }
|
|
|
|
[ not-class boa ]
|
|
|
|
} case ;
|
|
|
|
|
2009-02-19 17:48:46 -05:00
|
|
|
M: not-class class-member?
|
|
|
|
class>> class-member? not ;
|
2009-02-19 19:28:54 -05:00
|
|
|
|
|
|
|
M: primitive-class class-member?
|
|
|
|
class>> class-member? ;
|
2009-02-20 18:54:48 -05:00
|
|
|
|
|
|
|
UNION: class primitive-class not-class or-class range ;
|