]> gitweb.factorcode.org Git - factor.git/blob - basis/regexp/ast/ast.factor
factor: More using list cleanup
[factor.git] / basis / regexp / ast / ast.factor
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 ;
4 IN: regexp.ast
5
6 TUPLE: negation term ;
7 C: <negation> negation
8
9 TUPLE: from-to n m ;
10 C: <from-to> from-to
11
12 TUPLE: at-least n ;
13 C: <at-least> at-least
14
15 TUPLE: tagged-epsilon tag ;
16 C: <tagged-epsilon> tagged-epsilon
17
18 CONSTANT: epsilon T{ tagged-epsilon { tag t } }
19
20 TUPLE: concatenation first second ;
21
22 : <concatenation> ( seq -- concatenation )
23     [ epsilon ] [ [ ] [ concatenation boa ] map-reduce ] if-empty ;
24
25 TUPLE: alternation first second ;
26
27 : <alternation> ( seq -- alternation )
28     [ ] [ alternation boa ] map-reduce ;
29
30 TUPLE: star term ;
31 C: <star> star
32
33 TUPLE: with-options tree options ;
34 C: <with-options> with-options
35
36 TUPLE: options on off ;
37 C: <options> options
38
39 SINGLETONS: unix-lines dotall multiline case-insensitive reversed-regexp ;
40
41 : <maybe> ( term -- term' )
42     f <concatenation> 2array <alternation> ;
43
44 : <plus> ( term -- term' )
45     dup <star> 2array <concatenation> ;
46
47 : repetition ( n term -- term' )
48     <array> <concatenation> ;
49
50 GENERIC: <times> ( term times -- term' )
51
52 M: at-least <times>
53     n>> swap [ repetition ] [ <star> ] bi 2array <concatenation> ;
54
55 : to-times ( term n -- ast )
56     [ drop epsilon ]
57     [ dupd 1 - to-times 2array <concatenation> <maybe> ]
58     if-zero ;
59
60 M: from-to <times>
61     [ n>> swap repetition ]
62     [ [ m>> ] [ n>> ] bi - to-times ] 2bi
63     2array <concatenation> ;
64
65 : char-class ( ranges ? -- term )
66     [ <or-class> ] dip [ <not-class> ] when ;
67
68 TUPLE: lookahead term ;
69 C: <lookahead> lookahead
70
71 TUPLE: lookbehind term ;
72 C: <lookbehind> lookbehind