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