]> gitweb.factorcode.org Git - factor.git/blob - basis/regexp/nfa/nfa.factor
Merge branch 'master' into experimental
[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 ! Potential off-by-one errors when lookaround nested in lookbehind
60
61 M: object modify-epsilon ;
62
63 M: $ modify-epsilon
64     multiline option? [ drop end-of-input ] unless ;
65
66 M: ^ modify-epsilon
67     multiline option? [ drop beginning-of-input ] unless ;
68
69 M: tagged-epsilon nfa-node
70     clone [ modify-epsilon ] change-tag add-simple-entry ;
71
72 M: concatenation nfa-node ( node -- start end )
73     [ first>> ] [ second>> ] bi
74     reversed-regexp option? [ swap ] when
75     [ nfa-node ] bi@
76     [ epsilon-transition ] dip ;
77
78 :: alternate-nodes ( s0 s1 s2 s3 -- start end )
79     next-state :> s4
80     next-state :> s5
81     s4 s0 epsilon-transition
82     s4 s2 epsilon-transition
83     s1 s5 epsilon-transition
84     s3 s5 epsilon-transition
85     s4 s5 ;
86
87 M: alternation nfa-node ( node -- start end )
88     [ first>> ] [ second>> ] bi
89     [ nfa-node ] bi@
90     alternate-nodes ;
91
92 GENERIC: modify-class ( char-class -- char-class' )
93
94 M: object modify-class ;
95
96 M: integer modify-class
97     case-insensitive option? [
98         dup Letter? [
99             [ ch>lower ] [ ch>upper ] bi 2array <or-class>
100         ] when
101     ] when ;
102
103 M: integer nfa-node ( node -- start end )
104     modify-class add-simple-entry ;
105
106 M: primitive-class modify-class
107     class>> modify-class <primitive-class> ;
108
109 M: or-class modify-class
110     seq>> [ modify-class ] map <or-class> ;
111
112 M: not-class modify-class
113     class>> modify-class <not-class> ;
114
115 M: any-char modify-class
116     drop dotall option? t any-char-no-nl ? ;
117
118 : modify-letter-class ( class -- newclass )
119     case-insensitive option? [ drop Letter-class ] when ;
120 M: letter-class modify-class modify-letter-class ;
121 M: LETTER-class modify-class modify-letter-class ;
122
123 : cased-range? ( range -- ? )
124     [ from>> ] [ to>> ] bi {
125         [ [ letter? ] bi@ and ]
126         [ [ LETTER? ] bi@ and ]
127     } 2|| ;
128
129 M: range modify-class
130     case-insensitive option? [
131         dup cased-range? [
132             [ from>> ] [ to>> ] bi
133             [ [ ch>lower ] bi@ <range> ]
134             [ [ ch>upper ] bi@ <range> ] 2bi 
135             2array <or-class>
136         ] when
137     ] when ;
138
139 M: class nfa-node
140     modify-class add-simple-entry ;
141
142 M: with-options nfa-node ( node -- start end )
143     dup options>> [ tree>> nfa-node ] using-options ;
144
145 : construct-nfa ( ast -- nfa-table )
146     [
147         0 state set
148         <transition-table> nfa-table set
149         nfa-node
150         nfa-table get
151             swap dup associate >>final-states
152             swap >>start-state
153     ] with-scope ;