1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! [ruby/spec](https://github.com/ruby/spec) compliant implementation of
//! [`Regexp`](https://ruby-doc.org/core-2.6.3/Regexp.html).
//!
//! Each function on `Regexp` is implemented as its own module which contains
//! the `Args` struct for invoking the function.

use onig::{Regex, Syntax};
use std::hash::{Hash, Hasher};
use std::mem;
use std::rc::Rc;

use crate::convert::{FromMrb, RustBackedValue};
use crate::def::{rust_data_free, ClassLike, Define};
use crate::eval::MrbEval;
use crate::extn::core::error::{RubyException, RuntimeError, SyntaxError, TypeError};
use crate::sys;
use crate::value::Value;
use crate::{Mrb, MrbError};

pub mod enc;
pub mod opts;
pub mod syntax;

pub mod case_compare;
pub mod casefold;
pub mod eql;
pub mod escape;
pub mod fixed_encoding;
pub mod hash;
pub mod initialize;
pub mod inspect;
pub mod match_;
pub mod match_operator;
pub mod match_q;
pub mod named_captures;
pub mod names;
pub mod options;
pub mod source;
pub mod to_s;
pub mod union;

pub fn init(interp: &Mrb) -> Result<(), MrbError> {
    interp.eval(include_str!("regexp.rb"))?;
    let regexp =
        interp
            .borrow_mut()
            .def_class::<Regexp>("Regexp", None, Some(rust_data_free::<Regexp>));
    regexp.borrow_mut().mrb_value_is_rust_backed(true);
    regexp.borrow_mut().add_method(
        "initialize",
        Regexp::initialize,
        sys::mrb_args_req_and_opt(1, 2),
    );
    regexp
        .borrow_mut()
        .add_self_method("compile", Regexp::compile, sys::mrb_args_rest());
    regexp
        .borrow_mut()
        .add_self_method("escape", Regexp::escape, sys::mrb_args_req(1));
    regexp
        .borrow_mut()
        .add_self_method("quote", Regexp::escape, sys::mrb_args_req(1));
    regexp
        .borrow_mut()
        .add_self_method("union", Regexp::union, sys::mrb_args_rest());
    regexp
        .borrow_mut()
        .add_method("==", Regexp::eql, sys::mrb_args_req(1));
    regexp
        .borrow_mut()
        .add_method("===", Regexp::case_compare, sys::mrb_args_req(1));
    regexp
        .borrow_mut()
        .add_method("=~", Regexp::match_operator, sys::mrb_args_req(1));
    regexp
        .borrow_mut()
        .add_method("casefold?", Regexp::casefold, sys::mrb_args_none());
    regexp
        .borrow_mut()
        .add_method("eql?", Regexp::eql, sys::mrb_args_req(1));
    regexp.borrow_mut().add_method(
        "fixed_encoding?",
        Regexp::fixed_encoding,
        sys::mrb_args_none(),
    );
    regexp
        .borrow_mut()
        .add_method("hash", Regexp::hash, sys::mrb_args_none());
    regexp
        .borrow_mut()
        .add_method("inspect", Regexp::inspect, sys::mrb_args_none());
    regexp
        .borrow_mut()
        .add_method("match?", Regexp::match_q, sys::mrb_args_req_and_opt(1, 1));
    regexp
        .borrow_mut()
        .add_method("match", Regexp::match_, sys::mrb_args_req_and_opt(1, 1));
    regexp.borrow_mut().add_method(
        "named_captures",
        Regexp::named_captures,
        sys::mrb_args_none(),
    );
    regexp
        .borrow_mut()
        .add_method("names", Regexp::names, sys::mrb_args_none());
    regexp
        .borrow_mut()
        .add_method("options", Regexp::options, sys::mrb_args_none());
    regexp
        .borrow_mut()
        .add_method("source", Regexp::source, sys::mrb_args_none());
    regexp
        .borrow_mut()
        .add_method("to_s", Regexp::to_s, sys::mrb_args_none());
    regexp.borrow().define(&interp)?;
    // TODO: Add proper constant defs to class::Spec, see GH-158.
    interp.eval(format!(
        "class Regexp; IGNORECASE = {}; EXTENDED = {}; MULTILINE = {}; FIXEDENCODING = {}; NOENCODING = {}; end",
        Regexp::IGNORECASE,
        Regexp::EXTENDED,
        Regexp::MULTILINE,
        Regexp::FIXEDENCODING,
        Regexp::NOENCODING,
    ))?;
    Ok(())
}

#[derive(Debug, Clone, Default)]
pub struct Regexp {
    literal_pattern: String,
    pattern: String,
    literal_options: opts::Options,
    options: opts::Options,
    encoding: enc::Encoding,
    pub regex: Rc<Option<Regex>>,
}

impl Hash for Regexp {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.literal_pattern.hash(state);
        self.literal_options.hash(state);
    }
}

impl RustBackedValue for Regexp {
    fn new_obj_args(&self, interp: &Mrb) -> Vec<sys::mrb_value> {
        vec![
            Value::from_mrb(interp, self.literal_pattern.as_str()).inner(),
            Value::from_mrb(interp, self.literal_options.flags().bits()).inner(),
            Value::from_mrb(interp, self.encoding.flags()).inner(),
        ]
    }
}

impl Regexp {
    pub const IGNORECASE: i64 = 1;
    pub const EXTENDED: i64 = 2;
    pub const MULTILINE: i64 = 4;

    pub const ALL_REGEXP_OPTS: i64 = Self::IGNORECASE | Self::EXTENDED | Self::MULTILINE;

    pub const FIXEDENCODING: i64 = 16;
    pub const NOENCODING: i64 = 32;

    pub const ALL_ENCODING_OPTS: i64 = Self::FIXEDENCODING | Self::NOENCODING;

    pub fn new(
        literal_pattern: String,
        pattern: String,
        literal_options: opts::Options,
        options: opts::Options,
        encoding: enc::Encoding,
    ) -> Option<Self> {
        let regex = Regex::with_options(&pattern, options.flags(), Syntax::ruby()).ok()?;
        let regex = Rc::new(Some(regex));
        let regexp = Self {
            literal_pattern,
            pattern,
            literal_options,
            options,
            encoding,
            regex,
        };
        Some(regexp)
    }

    unsafe extern "C" fn initialize(
        mrb: *mut sys::mrb_state,
        slf: sys::mrb_value,
    ) -> sys::mrb_value {
        let interp = unwrap_interpreter!(mrb);
        let result = initialize::Args::extract(&interp)
            .and_then(|args| initialize::method(&interp, args, slf));
        match result {
            Ok(value) => value.inner(),
            Err(initialize::Error::NoImplicitConversionToString) => {
                TypeError::raise(interp, "no implicit conversion into String");
                unreachable!("raise unwinds the stack with longjmp");
            }
            Err(initialize::Error::Syntax) => {
                SyntaxError::raise(interp, "Failed to parse Regexp pattern");
                unreachable!("raise unwinds the stack with longjmp");
            }
            Err(initialize::Error::Unicode) => {
                RuntimeError::raise(interp, "Pattern is invalid UTF-8");
                unreachable!("raise unwinds the stack with longjmp");
            }
            Err(initialize::Error::Fatal) => {
                RuntimeError::raise(interp, "Fatal Regexp#initialize error");
                unreachable!("raise unwinds the stack with longjmp");
            }
        }
    }

    unsafe extern "C" fn compile(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
        let mut args = <mem::MaybeUninit<*const sys::mrb_value>>::uninit();
        let mut count = <mem::MaybeUninit<sys::mrb_int>>::uninit();
        sys::mrb_get_args(
            mrb,
            b"*\0".as_ptr() as *const i8,
            args.as_mut_ptr(),
            count.as_mut_ptr(),
        );
        sys::mrb_obj_new(
            mrb,
            sys::mrb_sys_class_ptr(slf),
            count.assume_init(),
            args.assume_init(),
        )
    }

    unsafe extern "C" fn escape(mrb: *mut sys::mrb_state, _slf: sys::mrb_value) -> sys::mrb_value {
        let interp = unwrap_interpreter!(mrb);
        let result = escape::Args::extract(&interp).and_then(|args| escape::method(&interp, &args));
        match result {
            Ok(result) => result.inner(),
            Err(escape::Error::NoImplicitConversionToString) => {
                TypeError::raise(interp, "no implicit conversion into String")
            }
            Err(escape::Error::Fatal) => RuntimeError::raise(interp, "fatal Regexp::escape error"),
        }
    }

    unsafe extern "C" fn union(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
        let interp = unwrap_interpreter!(mrb);
        let args = union::Args::extract(&interp);
        let result = union::method(&interp, args, slf);
        match result {
            Ok(result) => result.inner(),
            Err(union::Error::NoImplicitConversionToString) => {
                TypeError::raise(interp, "no implicit conversion into String")
            }
            Err(union::Error::Fatal) => RuntimeError::raise(interp, "fatal Regexp::union error"),
        }
    }

    unsafe extern "C" fn match_q(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
        let interp = unwrap_interpreter!(mrb);
        let value = Value::new(&interp, slf);
        let result =
            match_q::Args::extract(&interp).and_then(|args| match_q::method(&interp, args, &value));
        match result {
            Ok(result) => result.inner(),
            Err(match_q::Error::Fatal) => RuntimeError::raise(interp, "fatal Regexp#match? error"),
            Err(match_q::Error::PosType) => {
                TypeError::raise(interp, "No implicit conversion into Integer")
            }
            Err(match_q::Error::StringType) => {
                TypeError::raise(interp, "No implicit conversion into String")
            }
        }
    }

    unsafe extern "C" fn match_(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
        let interp = unwrap_interpreter!(mrb);
        let value = Value::new(&interp, slf);
        let result =
            match_::Args::extract(&interp).and_then(|args| match_::method(&interp, args, &value));
        match result {
            Ok(result) => result.inner(),
            Err(match_::Error::Fatal) => RuntimeError::raise(interp, "fatal Regexp#match error"),
            Err(match_::Error::PosType) => {
                TypeError::raise(interp, "No implicit conversion into Integer")
            }
            Err(match_::Error::StringType) => {
                TypeError::raise(interp, "No implicit conversion into String")
            }
        }
    }

    unsafe extern "C" fn eql(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
        let interp = unwrap_interpreter!(mrb);
        let args = eql::Args::extract(&interp);
        let value = Value::new(&interp, slf);
        match eql::method(&interp, args, &value) {
            Ok(result) => result.inner(),
            Err(eql::Error::Fatal) => RuntimeError::raise(interp, "fatal Regexp#== error"),
        }
    }

    unsafe extern "C" fn case_compare(
        mrb: *mut sys::mrb_state,
        slf: sys::mrb_value,
    ) -> sys::mrb_value {
        let interp = unwrap_interpreter!(mrb);
        let value = Value::new(&interp, slf);
        let result = case_compare::Args::extract(&interp)
            .and_then(|args| case_compare::method(&interp, args, &value));
        match result {
            Ok(result) => result.inner(),
            Err(case_compare::Error::NoImplicitConversionToString) => {
                Value::from_mrb(&interp, false).inner()
            }
            Err(case_compare::Error::Fatal) => {
                RuntimeError::raise(interp, "fatal Regexp#=== error")
            }
        }
    }

    unsafe extern "C" fn match_operator(
        mrb: *mut sys::mrb_state,
        slf: sys::mrb_value,
    ) -> sys::mrb_value {
        let interp = unwrap_interpreter!(mrb);
        let value = Value::new(&interp, slf);
        let result = match_operator::Args::extract(&interp)
            .and_then(|args| match_operator::method(&interp, args, &value));
        match result {
            Ok(result) => result.inner(),
            Err(match_operator::Error::NoImplicitConversionToString) => {
                Value::from_mrb(&interp, false).inner()
            }
            Err(match_operator::Error::Fatal) => {
                RuntimeError::raise(interp, "fatal Regexp#=== error")
            }
        }
    }

    unsafe extern "C" fn casefold(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
        let interp = unwrap_interpreter!(mrb);
        let value = Value::new(&interp, slf);
        match casefold::method(&interp, &value) {
            Ok(result) => result.inner(),
            Err(casefold::Error::Fatal) => {
                RuntimeError::raise(interp, "fatal Regexp#casefold? error")
            }
        }
    }

    unsafe extern "C" fn fixed_encoding(
        mrb: *mut sys::mrb_state,
        slf: sys::mrb_value,
    ) -> sys::mrb_value {
        let interp = unwrap_interpreter!(mrb);
        let value = Value::new(&interp, slf);
        match fixed_encoding::method(&interp, &value) {
            Ok(result) => result.inner(),
            Err(fixed_encoding::Error::Fatal) => {
                RuntimeError::raise(interp, "fatal Regexp#fixed_encoding? error")
            }
        }
    }

    unsafe extern "C" fn hash(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
        let interp = unwrap_interpreter!(mrb);
        let value = Value::new(&interp, slf);
        match hash::method(&interp, &value) {
            Ok(result) => result.inner(),
            Err(hash::Error::Fatal) => RuntimeError::raise(interp, "fatal Regexp#hash error"),
        }
    }

    unsafe extern "C" fn inspect(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
        let interp = unwrap_interpreter!(mrb);
        let value = Value::new(&interp, slf);
        match inspect::method(&interp, &value) {
            Ok(result) => result.inner(),
            Err(inspect::Error::Fatal) => RuntimeError::raise(interp, "fatal Regexp#inspect error"),
        }
    }

    unsafe extern "C" fn named_captures(
        mrb: *mut sys::mrb_state,
        slf: sys::mrb_value,
    ) -> sys::mrb_value {
        let interp = unwrap_interpreter!(mrb);
        let value = Value::new(&interp, slf);
        match named_captures::method(&interp, &value) {
            Ok(result) => result.inner(),
            Err(named_captures::Error::Fatal) => {
                RuntimeError::raise(interp, "fatal Regexp#named_captures error")
            }
        }
    }

    unsafe extern "C" fn names(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
        let interp = unwrap_interpreter!(mrb);
        let value = Value::new(&interp, slf);
        match names::method(&interp, &value) {
            Ok(result) => result.inner(),
            Err(names::Error::Fatal) => RuntimeError::raise(interp, "fatal Regexp#names error"),
        }
    }

    unsafe extern "C" fn options(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
        let interp = unwrap_interpreter!(mrb);
        let value = Value::new(&interp, slf);
        match options::method(&interp, &value) {
            Ok(result) => result.inner(),
            Err(options::Error::Fatal) => RuntimeError::raise(interp, "fatal Regexp#options error"),
        }
    }

    unsafe extern "C" fn source(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
        let interp = unwrap_interpreter!(mrb);
        let value = Value::new(&interp, slf);
        match source::method(&interp, &value) {
            Ok(result) => result.inner(),
            Err(source::Error::Fatal) => RuntimeError::raise(interp, "fatal Regexp#source error"),
        }
    }

    #[allow(clippy::wrong_self_convention)]
    unsafe extern "C" fn to_s(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
        let interp = unwrap_interpreter!(mrb);
        let value = Value::new(&interp, slf);
        match to_s::method(&interp, &value) {
            Ok(result) => result.inner(),
            Err(to_s::Error::Fatal) => RuntimeError::raise(interp, "fatal Regexp#to_s error"),
        }
    }
}