Making regexp AST building linear time rather than quadratic for a{n}
parent
ba9938c30f
commit
8157a6a52f
|
@ -1,7 +1,7 @@
|
||||||
! Copyright (C) 2008, 2009 Doug Coleman, Daniel Ehrenberg.
|
! Copyright (C) 2008, 2009 Doug Coleman, Daniel Ehrenberg.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: kernel arrays accessors fry sequences regexp.classes ;
|
USING: kernel arrays accessors fry sequences regexp.classes
|
||||||
FROM: math.ranges => [a,b] ;
|
math.ranges math ;
|
||||||
IN: regexp.ast
|
IN: regexp.ast
|
||||||
|
|
||||||
TUPLE: negation term ;
|
TUPLE: negation term ;
|
||||||
|
@ -49,10 +49,20 @@ SINGLETONS: unix-lines dotall multiline case-insensitive reversed-regexp ;
|
||||||
<array> <concatenation> ;
|
<array> <concatenation> ;
|
||||||
|
|
||||||
GENERIC: <times> ( term times -- term' )
|
GENERIC: <times> ( term times -- term' )
|
||||||
|
|
||||||
M: at-least <times>
|
M: at-least <times>
|
||||||
n>> swap [ repetition ] [ <star> ] bi 2array <concatenation> ;
|
n>> swap [ repetition ] [ <star> ] bi 2array <concatenation> ;
|
||||||
|
|
||||||
|
: to-times ( term n -- ast )
|
||||||
|
dup zero?
|
||||||
|
[ 2drop epsilon ]
|
||||||
|
[ dupd 1- to-times 2array <concatenation> <maybe> ]
|
||||||
|
if ;
|
||||||
|
|
||||||
M: from-to <times>
|
M: from-to <times>
|
||||||
[ n>> ] [ m>> ] bi [a,b] swap '[ _ repetition ] map <alternation> ;
|
[ n>> swap repetition ]
|
||||||
|
[ [ m>> ] [ n>> ] bi - to-times ] 2bi
|
||||||
|
2array <concatenation> ;
|
||||||
|
|
||||||
: char-class ( ranges ? -- term )
|
: char-class ( ranges ? -- term )
|
||||||
[ <or-class> ] dip [ <not-class> ] when ;
|
[ <or-class> ] dip [ <not-class> ] when ;
|
||||||
|
|
Loading…
Reference in New Issue