]> gitweb.factorcode.org Git - factor.git/blob - basis/regexp/compiler/compiler.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / regexp / compiler / compiler.factor
1 ! Copyright (C) 2009 Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: regexp.classes kernel sequences regexp.negation
4 quotations regexp.minimize assocs fry math locals combinators
5 accessors words compiler.units kernel.private strings
6 sequences.private arrays regexp.matchers call namespaces
7 regexp.transition-tables combinators.short-circuit ;
8 IN: regexp.compiler
9
10 GENERIC: question>quot ( question -- quot )
11
12 <PRIVATE
13
14 SYMBOL: shortest?
15 SYMBOL: backwards?
16
17 M: t question>quot drop [ 2drop t ] ;
18
19 M: beginning-of-input question>quot
20     drop [ drop zero? ] ;
21
22 M: end-of-input question>quot
23     drop [ length = ] ;
24
25 M: end-of-file question>quot
26     drop [
27         {
28             [ length swap - 2 <= ]
29             [ swap tail { "\n" "\r\n" "\r" "" } member? ]
30         } 2&&
31     ] ;
32
33 M: $ question>quot
34     drop [ { [ length = ] [ ?nth "\r\n" member? ] } 2|| ] ;
35
36 M: ^ question>quot
37     drop [ { [ drop zero? ] [ [ 1- ] dip ?nth "\r\n" member? ] } 2|| ] ;
38
39 : (execution-quot) ( next-state -- quot )
40     ! The conditions here are for lookaround and anchors, etc
41     dup condition? [
42         [ question>> question>quot ] [ yes>> ] [ no>> ] tri
43         [ (execution-quot) ] bi@
44         '[ 2dup @ _ _ if ]
45     ] [ '[ _ execute ] ] if ;
46
47 : execution-quot ( next-state -- quot )
48     dup sequence? [ first ] when
49     (execution-quot) ;
50
51 TUPLE: box contents ;
52 C: <box> box
53
54 : condition>quot ( condition -- quot )
55     ! Conditions here are for different classes
56     dup condition? [
57         [ question>> ] [ yes>> ] [ no>> ] tri
58         [ condition>quot ] bi@
59         '[ dup _ class-member? _ _ if ]
60     ] [
61         contents>>
62         [ [ 3drop ] ] [ execution-quot '[ drop @ ] ] if-empty
63     ] if ;
64
65 : non-literals>dispatch ( literals non-literals  -- quot )
66     [ swap ] assoc-map ! we want state => predicate, and get the opposite as input
67     swap keys f assoc-answers
68     table>condition [ <box> ] condition-map condition>quot ;
69
70 : literals>cases ( literal-transitions -- case-body )
71     [ execution-quot ] assoc-map ;
72
73 : expand-one-or ( or-class transition -- alist )
74     [ seq>> ] dip '[ _ 2array ] map ;
75
76 : expand-or ( alist -- new-alist )
77     [
78         first2 over or-class?
79         [ expand-one-or ] [ 2array 1array ] if
80     ] map concat ;
81
82 : split-literals ( transitions -- case default )
83     >alist expand-or [ first integer? ] partition
84     [ [ literals>cases ] keep ] dip non-literals>dispatch ;
85
86 :: step ( last-match index str quot final? direction -- last-index/f )
87     final? index last-match ?
88     index str bounds-check? [
89         index direction + str
90         index str nth-unsafe
91         quot call
92     ] when ; inline
93
94 : direction ( -- n )
95     backwards? get -1 1 ? ;
96
97 : transitions>quot ( transitions final-state? -- quot )
98     dup shortest? get and [ 2drop [ drop nip ] ] [
99         [ split-literals swap case>quot ] dip direction
100         '[ { array-capacity string } declare _ _ _ step ]
101     ] if ;
102
103 : word>quot ( word dfa -- quot )
104     [ transitions>> at ]
105     [ final-states>> key? ] 2bi
106     transitions>quot ;
107
108 : states>code ( words dfa -- )
109     [ ! with-compilation-unit doesn't compile, so we need call( -- )
110         [
111             '[
112                 dup _ word>quot
113                 (( last-match index string -- ? ))
114                 define-declared
115             ] each
116         ] with-compilation-unit
117     ] call( words dfa -- ) ;
118
119 : states>words ( dfa -- words dfa )
120     dup transitions>> keys [ gensym ] H{ } map>assoc
121     [ transitions-at ]
122     [ values ]
123     bi swap ; 
124
125 : dfa>word ( dfa -- word )
126     states>words [ states>code ] keep start-state>> ;
127
128 : check-string ( string -- string )
129     ! Make this configurable
130     dup string? [ "String required" throw ] unless ;
131
132 : setup-regexp ( start-index string -- f start-index string )
133     [ f ] [ >fixnum ] [ check-string ] tri* ; inline
134
135 PRIVATE>
136
137 ! The quotation returned is ( start-index string -- i/f )
138
139 : dfa>quotation ( dfa -- quot )
140     dfa>word execution-quot '[ setup-regexp @ ] ;
141
142 : dfa>shortest-quotation ( dfa -- quot )
143     t shortest? [ dfa>quotation ] with-variable ;
144
145 : dfa>reverse-quotation ( dfa -- quot )
146     t backwards? [ dfa>quotation ] with-variable ;
147
148 : dfa>reverse-shortest-quotation ( dfa -- quot )
149     t backwards? [ dfa>shortest-quotation ] with-variable ;
150
151 TUPLE: quot-matcher quot ;
152 C: <quot-matcher> quot-matcher
153
154 M: quot-matcher match-index-from
155     quot>> call( index string -- i/f ) ;