1 ! Copyright (C) 2008, 2009 Doug Coleman, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel arrays accessors fry sequences regexp.classes
14 C: <at-least> at-least
16 TUPLE: tagged-epsilon tag ;
17 C: <tagged-epsilon> tagged-epsilon
19 CONSTANT: epsilon T{ tagged-epsilon { tag t } }
21 TUPLE: concatenation first second ;
23 : <concatenation> ( seq -- concatenation )
24 [ epsilon ] [ [ ] [ concatenation boa ] map-reduce ] if-empty ;
26 TUPLE: alternation first second ;
28 : <alternation> ( seq -- alternation )
29 [ ] [ alternation boa ] map-reduce ;
34 TUPLE: with-options tree options ;
35 C: <with-options> with-options
37 TUPLE: options on off ;
40 SINGLETONS: unix-lines dotall multiline case-insensitive reversed-regexp ;
42 : <maybe> ( term -- term' )
43 f <concatenation> 2array <alternation> ;
45 : <plus> ( term -- term' )
46 dup <star> 2array <concatenation> ;
48 : repetition ( n term -- term' )
49 <array> <concatenation> ;
51 GENERIC: <times> ( term times -- term' )
54 n>> swap [ repetition ] [ <star> ] bi 2array <concatenation> ;
56 : to-times ( term n -- ast )
59 [ dupd 1 - to-times 2array <concatenation> <maybe> ]
63 [ n>> swap repetition ]
64 [ [ m>> ] [ n>> ] bi - to-times ] 2bi
65 2array <concatenation> ;
67 : char-class ( ranges ? -- term )
68 [ <or-class> ] dip [ <not-class> ] when ;
70 TUPLE: lookahead term ;
71 C: <lookahead> lookahead
73 TUPLE: lookbehind term ;
74 C: <lookbehind> lookbehind