1 ! Copyright (C) 2008, 2009 Doug Coleman, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays kernel math regexp.classes sequences ;
13 C: <at-least> at-least
15 TUPLE: tagged-epsilon tag ;
16 C: <tagged-epsilon> tagged-epsilon
18 CONSTANT: epsilon T{ tagged-epsilon { tag t } }
20 TUPLE: concatenation first second ;
22 : <concatenation> ( seq -- concatenation )
23 [ epsilon ] [ [ ] [ concatenation boa ] map-reduce ] if-empty ;
25 TUPLE: alternation first second ;
27 : <alternation> ( seq -- alternation )
28 [ ] [ alternation boa ] map-reduce ;
33 TUPLE: with-options tree options ;
34 C: <with-options> with-options
36 TUPLE: options on off ;
39 SINGLETONS: unix-lines dotall multiline case-insensitive reversed-regexp ;
41 : <maybe> ( term -- term' )
42 f <concatenation> 2array <alternation> ;
44 : <plus> ( term -- term' )
45 dup <star> 2array <concatenation> ;
47 : repetition ( n term -- term' )
48 <array> <concatenation> ;
50 GENERIC: <times> ( term times -- term' )
53 n>> swap [ repetition ] [ <star> ] bi 2array <concatenation> ;
55 : to-times ( term n -- ast )
57 [ dupd 1 - to-times 2array <concatenation> <maybe> ]
61 [ n>> swap repetition ]
62 [ [ m>> ] [ n>> ] bi - to-times ] 2bi
63 2array <concatenation> ;
65 : char-class ( ranges ? -- term )
66 [ <or-class> ] dip [ <not-class> ] when ;
68 TUPLE: lookahead term ;
69 C: <lookahead> lookahead
71 TUPLE: lookbehind term ;
72 C: <lookbehind> lookbehind