13
13
#![ allow( visible_private_types) ]
14
14
15
15
use std:: cmp;
16
+ use std:: iter;
16
17
use parse;
17
18
use parse:: {
18
19
Flags , FLAG_EMPTY ,
@@ -88,7 +89,7 @@ pub struct Program {
88
89
89
90
impl Program {
90
91
/// Compiles a Regex given its AST.
91
- pub fn new ( ast : parse:: Ast ) -> ( Program , Vec < Option < ~str > > ) {
92
+ pub fn new ( ast : ~ parse:: Ast ) -> ( Program , ~ [ Option < ~str > ] ) {
92
93
let mut c = Compiler {
93
94
insts : Vec :: with_capacity ( 100 ) ,
94
95
names : Vec :: with_capacity ( 10 ) ,
@@ -103,16 +104,16 @@ impl Program {
103
104
// This is a bit hacky since we have to skip over the initial
104
105
// 'Save' instruction.
105
106
let mut pre = StrBuf :: with_capacity ( 5 ) ;
106
- for inst in c. insts . slice_from ( 1 ) . iter ( ) {
107
- match * inst {
107
+ for i in iter :: range ( 1 , c. insts . len ( ) ) {
108
+ match * c . insts . get ( i ) {
108
109
OneChar ( c, FLAG_EMPTY ) => pre. push_char ( c) ,
109
110
_ => break
110
111
}
111
112
}
112
113
113
- let Compiler { insts , names } = c;
114
+ let names = c. names . as_slice ( ) . into_owned ( ) ;
114
115
let prog = Program {
115
- insts : insts,
116
+ insts : c . insts ,
116
117
prefix : pre. into_owned ( ) ,
117
118
} ;
118
119
( prog, names)
@@ -143,48 +144,48 @@ struct Compiler<'r> {
143
144
// The only tricky thing here is patching jump/split instructions to point to
144
145
// the right instruction.
145
146
impl < ' r > Compiler < ' r > {
146
- fn compile ( & mut self , ast : parse:: Ast ) {
147
+ fn compile ( & mut self , ast : ~ parse:: Ast ) {
147
148
match ast {
148
- Nothing => { } ,
149
- Literal ( c, flags) => self . push ( OneChar ( c, flags) ) ,
150
- Dot ( nl) => self . push ( Any ( nl) ) ,
151
- Class ( ranges, flags) =>
149
+ ~ Nothing => { } ,
150
+ ~ Literal ( c, flags) => self . push ( OneChar ( c, flags) ) ,
151
+ ~ Dot ( nl) => self . push ( Any ( nl) ) ,
152
+ ~ Class ( ranges, flags) =>
152
153
self . push ( CharClass ( ranges, flags) ) ,
153
- Begin ( flags) => self . push ( EmptyBegin ( flags) ) ,
154
- End ( flags) => self . push ( EmptyEnd ( flags) ) ,
155
- WordBoundary ( flags) => self . push ( EmptyWordBoundary ( flags) ) ,
156
- Capture ( cap, name, x) => {
154
+ ~ Begin ( flags) => self . push ( EmptyBegin ( flags) ) ,
155
+ ~ End ( flags) => self . push ( EmptyEnd ( flags) ) ,
156
+ ~ WordBoundary ( flags) => self . push ( EmptyWordBoundary ( flags) ) ,
157
+ ~ Capture ( cap, name, x) => {
157
158
let len = self . names . len ( ) ;
158
159
if cap >= len {
159
160
self . names . grow ( 10 + cap - len, & None )
160
161
}
161
162
* self . names . get_mut ( cap) = name;
162
163
163
164
self . push ( Save ( 2 * cap) ) ;
164
- self . compile ( * x) ;
165
+ self . compile ( x) ;
165
166
self . push ( Save ( 2 * cap + 1 ) ) ;
166
167
}
167
- Cat ( xs) => {
168
+ ~ Cat ( xs) => {
168
169
for x in xs. move_iter ( ) {
169
170
self . compile ( x)
170
171
}
171
172
}
172
- Alt ( x, y) => {
173
+ ~ Alt ( x, y) => {
173
174
let split = self . empty_split ( ) ; // push: split 0, 0
174
175
let j1 = self . insts . len ( ) ;
175
- self . compile ( * x) ; // push: insts for x
176
+ self . compile ( x) ; // push: insts for x
176
177
let jmp = self . empty_jump ( ) ; // push: jmp 0
177
178
let j2 = self . insts . len ( ) ;
178
- self . compile ( * y) ; // push: insts for y
179
+ self . compile ( y) ; // push: insts for y
179
180
let j3 = self . insts . len ( ) ;
180
181
181
182
self . set_split ( split, j1, j2) ; // split 0, 0 -> split j1, j2
182
183
self . set_jump ( jmp, j3) ; // jmp 0 -> jmp j3
183
184
}
184
- Rep ( x, ZeroOne , g) => {
185
+ ~ Rep ( x, ZeroOne , g) => {
185
186
let split = self . empty_split ( ) ;
186
187
let j1 = self . insts . len ( ) ;
187
- self . compile ( * x) ;
188
+ self . compile ( x) ;
188
189
let j2 = self . insts . len ( ) ;
189
190
190
191
if g. is_greedy ( ) {
@@ -193,11 +194,11 @@ impl<'r> Compiler<'r> {
193
194
self . set_split ( split, j2, j1) ;
194
195
}
195
196
}
196
- Rep ( x, ZeroMore , g) => {
197
+ ~ Rep ( x, ZeroMore , g) => {
197
198
let j1 = self . insts . len ( ) ;
198
199
let split = self . empty_split ( ) ;
199
200
let j2 = self . insts . len ( ) ;
200
- self . compile ( * x) ;
201
+ self . compile ( x) ;
201
202
let jmp = self . empty_jump ( ) ;
202
203
let j3 = self . insts . len ( ) ;
203
204
@@ -208,9 +209,9 @@ impl<'r> Compiler<'r> {
208
209
self . set_split ( split, j3, j2) ;
209
210
}
210
211
}
211
- Rep ( x, OneMore , g) => {
212
+ ~ Rep ( x, OneMore , g) => {
212
213
let j1 = self . insts . len ( ) ;
213
- self . compile ( * x) ;
214
+ self . compile ( x) ;
214
215
let split = self . empty_split ( ) ;
215
216
let j2 = self . insts . len ( ) ;
216
217
0 commit comments