]> gitweb.factorcode.org Git - factor.git/blob - basis/regexp/nfa/nfa.factor
Merge branch 'master' of git://factorcode.org/git/factor into regexp
[factor.git] / basis / regexp / nfa / nfa.factor
1 ! Copyright (C) 2008, 2009 Doug Coleman, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs grouping kernel
4 locals math namespaces sequences fry quotations
5 math.order math.ranges vectors unicode.categories
6 regexp.transition-tables words sets hashtables combinators.short-circuit
7 unicode.case.private regexp.ast regexp.classes ;
8 IN: regexp.nfa
9
10 ! This uses unicode.case.private for ch>upper and ch>lower
11 ! but case-insensitive matching should be done by case-folding everything
12 ! before processing starts
13
14 SYMBOL: option-stack
15
16 SYMBOL: state
17
18 : next-state ( -- state )
19     state [ get ] [ inc ] bi ;
20
21 SYMBOL: nfa-table
22
23 : set-each ( keys value hashtable -- )
24     '[ _ swap _ set-at ] each ;
25
26 : options>hash ( options -- hashtable )
27     H{ } clone [
28         [ [ on>> t ] dip set-each ]
29         [ [ off>> f ] dip set-each ] 2bi
30     ] keep ;
31
32 : using-options ( options quot -- )
33     [ options>hash option-stack [ ?push ] change ] dip
34     call option-stack get pop* ; inline
35
36 : option? ( obj -- ? )
37     option-stack get assoc-stack ;
38
39 GENERIC: nfa-node ( node -- start-state end-state )
40
41 : add-simple-entry ( obj -- start-state end-state )
42     [ next-state next-state 2dup ] dip
43     nfa-table get add-transition ;
44
45 : epsilon-transition ( source target -- )
46     epsilon nfa-table get add-transition ;
47
48 M:: star nfa-node ( node -- start end )
49     node term>> nfa-node :> s1 :> s0
50     next-state :> s2
51     next-state :> s3
52     s1 s0 epsilon-transition
53     s2 s0 epsilon-transition
54     s2 s3 epsilon-transition
55     s1 s3 epsilon-transition
56     s2 s3 ;
57
58 GENERIC: modify-epsilon ( tag -- newtag )
59
60 M: object modify-epsilon ;
61
62 M: tagged-epsilon nfa-node
63     clone [ modify-epsilon ] change-tag add-simple-entry ;
64
65 M: concatenation nfa-node ( node -- start end )
66     [ first>> ] [ second>> ] bi
67     reversed-regexp option? [ swap ] when
68     [ nfa-node ] bi@
69     [ epsilon-transition ] dip ;
70
71 :: alternate-nodes ( s0 s1 s2 s3 -- start end )
72     next-state :> s4
73     next-state :> s5
74     s4 s0 epsilon-transition
75     s4 s2 epsilon-transition
76     s1 s5 epsilon-transition
77     s3 s5 epsilon-transition
78     s4 s5 ;
79
80 M: alternation nfa-node ( node -- start end )
81     [ first>> ] [ second>> ] bi
82     [ nfa-node ] bi@
83     alternate-nodes ;
84
85 GENERIC: modify-class ( char-class -- char-class' )
86
87 M: object modify-class ;
88
89 M: integer modify-class
90     case-insensitive option? [
91         dup Letter? [
92             [ ch>lower ] [ ch>upper ] bi 2array <or-class>
93         ] when
94     ] when ;
95
96 M: integer nfa-node ( node -- start end )
97     modify-class add-simple-entry ;
98
99 M: primitive-class modify-class
100     class>> modify-class <primitive-class> ;
101
102 M: or-class modify-class
103     seq>> [ modify-class ] map <or-class> ;
104
105 M: not-class modify-class
106     class>> modify-class <not-class> ;
107
108 M: any-char modify-class
109     drop dotall option? t any-char-no-nl ? ;
110
111 : modify-letter-class ( class -- newclass )
112     case-insensitive option? [ drop Letter-class ] when ;
113 M: letter-class modify-class modify-letter-class ;
114 M: LETTER-class modify-class modify-letter-class ;
115
116 : cased-range? ( range -- ? )
117     [ from>> ] [ to>> ] bi {
118         [ [ letter? ] bi@ and ]
119         [ [ LETTER? ] bi@ and ]
120     } 2|| ;
121
122 M: range modify-class
123     case-insensitive option? [
124         dup cased-range? [
125             [ from>> ] [ to>> ] bi
126             [ [ ch>lower ] bi@ <range> ]
127             [ [ ch>upper ] bi@ <range> ] 2bi 
128             2array <or-class>
129         ] when
130     ] when ;
131
132 M: class nfa-node
133     modify-class add-simple-entry ;
134
135 M: with-options nfa-node ( node -- start end )
136     dup options>> [ tree>> nfa-node ] using-options ;
137
138 : construct-nfa ( ast -- nfa-table )
139     [
140         0 state set
141         <transition-table> nfa-table set
142         nfa-node
143         nfa-table get
144             swap dup associate >>final-states
145             swap >>start-state
146     ] with-scope ;