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 ;
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
14 GENERIC: remove-lookahead ( syntax-tree -- syntax-tree' )
15 ! This is unfinished and does nothing right now!
17 M: object remove-lookahead ;
19 M: with-options remove-lookahead
20 [ tree>> remove-lookahead ] [ options>> ] bi <with-options> ;
22 M: alternation remove-lookahead
23 [ first>> ] [ second>> ] bi [ remove-lookahead ] bi@ alternation boa ;
25 M: concatenation remove-lookahead ;
31 : next-state ( -- state )
32 state [ get ] [ inc ] bi ;
36 : set-each ( keys value hashtable -- )
37 '[ _ swap _ set-at ] each ;
39 : options>hash ( options -- hashtable )
41 [ [ on>> t ] dip set-each ]
42 [ [ off>> f ] dip set-each ] 2bi
45 : using-options ( options quot -- )
46 [ options>hash option-stack [ ?push ] change ] dip
47 call option-stack get pop* ; inline
49 : option? ( obj -- ? )
50 option-stack get assoc-stack ;
52 GENERIC: nfa-node ( node -- start-state end-state )
54 : add-simple-entry ( obj -- start-state end-state )
55 [ next-state next-state 2dup ] dip
56 nfa-table get add-transition ;
58 : epsilon-transition ( source target -- )
59 epsilon nfa-table get add-transition ;
61 M:: star nfa-node ( node -- start end )
62 node term>> nfa-node :> s1 :> s0
65 s1 s0 epsilon-transition
66 s2 s0 epsilon-transition
67 s2 s3 epsilon-transition
68 s1 s3 epsilon-transition
71 M: tagged-epsilon nfa-node
74 M: concatenation nfa-node ( node -- start end )
75 [ first>> ] [ second>> ] bi
76 reversed-regexp option? [ swap ] when
78 [ epsilon-transition ] dip ;
80 :: alternate-nodes ( s0 s1 s2 s3 -- start end )
83 s4 s0 epsilon-transition
84 s4 s2 epsilon-transition
85 s1 s5 epsilon-transition
86 s3 s5 epsilon-transition
89 M: alternation nfa-node ( node -- start end )
90 [ first>> ] [ second>> ] bi
94 GENERIC: modify-class ( char-class -- char-class' )
96 M: object modify-class ;
98 M: integer modify-class
99 case-insensitive option? [
101 [ ch>lower ] [ ch>upper ] bi 2array <or-class>
105 M: integer nfa-node ( node -- start end )
106 modify-class add-simple-entry ;
108 M: primitive-class modify-class
109 class>> modify-class <primitive-class> ;
111 M: or-class modify-class
112 seq>> [ modify-class ] map <or-class> ;
114 M: not-class modify-class
115 class>> modify-class <not-class> ;
117 M: any-char modify-class
118 drop dotall option? t any-char-no-nl ? ;
120 : modify-letter-class ( class -- newclass )
121 case-insensitive option? [ drop Letter-class ] when ;
122 M: letter-class modify-class modify-letter-class ;
123 M: LETTER-class modify-class modify-letter-class ;
125 : cased-range? ( range -- ? )
126 [ from>> ] [ to>> ] bi {
127 [ [ letter? ] bi@ and ]
128 [ [ LETTER? ] bi@ and ]
131 M: range modify-class
132 case-insensitive option? [
134 [ from>> ] [ to>> ] bi
135 [ [ ch>lower ] bi@ <range> ]
136 [ [ ch>upper ] bi@ <range> ] 2bi
142 modify-class add-simple-entry ;
144 M: with-options nfa-node ( node -- start end )
145 dup options>> [ tree>> nfa-node ] using-options ;
147 : construct-nfa ( ast -- nfa-table )
150 <transition-table> nfa-table set
151 remove-lookahead nfa-node
153 swap dup associate >>final-states