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