Making regexp AST building linear time rather than quadratic for a{n}

db4
Daniel Ehrenberg 2009-03-18 17:03:38 -05:00
parent ba9938c30f
commit 8157a6a52f
1 changed files with 13 additions and 3 deletions

View File

@ -1,7 +1,7 @@
! Copyright (C) 2008, 2009 Doug Coleman, Daniel Ehrenberg.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel arrays accessors fry sequences regexp.classes ;
FROM: math.ranges => [a,b] ;
USING: kernel arrays accessors fry sequences regexp.classes
math.ranges math ;
IN: regexp.ast
TUPLE: negation term ;
@ -49,10 +49,20 @@ SINGLETONS: unix-lines dotall multiline case-insensitive reversed-regexp ;
<array> <concatenation> ;
GENERIC: <times> ( term times -- term' )
M: at-least <times>
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>
[ n>> ] [ m>> ] bi [a,b] swap '[ _ repetition ] map <alternation> ;
[ n>> swap repetition ]
[ [ m>> ] [ n>> ] bi - to-times ] 2bi
2array <concatenation> ;
: char-class ( ranges ? -- term )
[ <or-class> ] dip [ <not-class> ] when ;