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
18 : next-state ( -- state )
19 state [ get ] [ inc ] bi ;
23 : set-each ( keys value hashtable -- )
24 '[ _ swap _ set-at ] each ;
26 : options>hash ( options -- hashtable )
28 [ [ on>> t ] dip set-each ]
29 [ [ off>> f ] dip set-each ] 2bi
32 : using-options ( options quot -- )
33 [ options>hash option-stack [ ?push ] change ] dip
34 call option-stack get pop* ; inline
36 : option? ( obj -- ? )
37 option-stack get assoc-stack ;
39 GENERIC: nfa-node ( node -- start-state end-state )
41 : add-simple-entry ( obj -- start-state end-state )
42 [ next-state next-state 2dup ] dip
43 nfa-table get add-transition ;
45 : epsilon-transition ( source target -- )
46 epsilon nfa-table get add-transition ;
48 M:: star nfa-node ( node -- start end )
49 node term>> nfa-node :> s1 :> s0
52 s1 s0 epsilon-transition
53 s2 s0 epsilon-transition
54 s2 s3 epsilon-transition
55 s1 s3 epsilon-transition
58 M: tagged-epsilon nfa-node
61 M: concatenation nfa-node ( node -- start end )
62 [ first>> ] [ second>> ] bi
63 reversed-regexp option? [ swap ] when
65 [ epsilon-transition ] dip ;
67 :: alternate-nodes ( s0 s1 s2 s3 -- start end )
70 s4 s0 epsilon-transition
71 s4 s2 epsilon-transition
72 s1 s5 epsilon-transition
73 s3 s5 epsilon-transition
76 M: alternation nfa-node ( node -- start end )
77 [ first>> ] [ second>> ] bi
81 GENERIC: modify-class ( char-class -- char-class' )
83 M: object modify-class ;
85 M: integer modify-class
86 case-insensitive option? [
88 [ ch>lower ] [ ch>upper ] bi 2array <or-class>
92 M: integer nfa-node ( node -- start end )
93 modify-class add-simple-entry ;
95 M: primitive-class modify-class
96 class>> modify-class <primitive-class> ;
98 M: or-class modify-class
99 seq>> [ modify-class ] map <or-class> ;
101 M: not-class modify-class
102 class>> modify-class <not-class> ;
104 M: any-char modify-class
105 drop dotall option? t any-char-no-nl ? ;
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 ;
112 : cased-range? ( range -- ? )
113 [ from>> ] [ to>> ] bi {
114 [ [ letter? ] bi@ and ]
115 [ [ LETTER? ] bi@ and ]
118 M: range modify-class
119 case-insensitive option? [
121 [ from>> ] [ to>> ] bi
122 [ [ ch>lower ] bi@ <range> ]
123 [ [ ch>upper ] bi@ <range> ] 2bi
129 modify-class add-simple-entry ;
131 M: with-options nfa-node ( node -- start end )
132 dup options>> [ tree>> nfa-node ] using-options ;
134 : construct-nfa ( ast -- nfa-table )
137 <transition-table> nfa-table set
140 swap dup associate >>final-states