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
//! Types used for regular expressions.

use std::fmt::Display;
use std::hash::Hash;
use std::ops::Deref;

use kempt::Map;
use refuse::{CollectionGuard, ContainsNoRefs, Trace};
use regex::{Captures, Regex};

use crate::compiler::syntax::token::RegexLiteral;
use crate::runtime::string::MuseString;
use crate::runtime::symbol::{Symbol, SymbolRef};
use crate::runtime::value::{
    AnyDynamic, CustomType, Rooted, RustFunctionTable, RustType, TypeRef, Value,
};
use crate::vm::{Fault, Register, VmContext};

/// A compiled [`RegexLiteral`].
#[derive(Debug, Clone)]
pub struct MuseRegex {
    expr: Regex,
    literal: RegexLiteral,
}

impl MuseRegex {
    /// Returns a compiled regular expression.
    ///
    /// # Errors
    ///
    /// All errors returned from this function are directly from the [`regex`]
    /// crate.
    pub fn new(literal: &RegexLiteral) -> Result<Self, regex::Error> {
        let expr = regex::RegexBuilder::new(&literal.pattern)
            .ignore_whitespace(literal.expanded)
            .crlf(true)
            .unicode(literal.unicode)
            .dot_matches_new_line(literal.dot_matches_all)
            .case_insensitive(literal.ignore_case)
            .multi_line(literal.multiline)
            .build()?;

        Ok(Self {
            expr,
            literal: literal.clone(),
        })
    }

    /// Compilex the regex literal and returns it loaded for the runtime.
    pub fn load(literal: &RegexLiteral, guard: &CollectionGuard<'_>) -> Result<Value, Fault> {
        MuseRegex::new(literal)
            .map(|r| Value::dynamic(r, guard))
            .map_err(|err| {
                Fault::Exception(Value::dynamic(MuseString::from(err.to_string()), guard))
            })
    }

    /// Returns the literal used to compile this regular expression.
    #[must_use]
    pub const fn literal(&self) -> &RegexLiteral {
        &self.literal
    }

    /// Returns the captures when searching in `haystack`.
    #[must_use]
    pub fn captures(&self, haystack: &str, guard: &CollectionGuard) -> Option<MuseCaptures> {
        self.expr
            .captures(haystack)
            .map(|captures| MuseCaptures::new(&captures, haystack, self, guard))
    }
}

impl Deref for MuseRegex {
    type Target = Regex;

    fn deref(&self) -> &Self::Target {
        &self.expr
    }
}

impl CustomType for MuseRegex {
    fn muse_type(&self) -> &TypeRef {
        static TYPE: RustType<MuseRegex> = RustType::new("Regex", |t| {
            t.with_function_table(
                RustFunctionTable::new()
                    .with_fn(
                        "total_captures",
                        0,
                        |_vm: &mut VmContext<'_, '_>, this: &Rooted<MuseRegex>| {
                            Value::try_from(this.captures_len())
                        },
                    )
                    .with_fn(
                        "total_static_captures",
                        0,
                        |_vm: &mut VmContext<'_, '_>, this: &Rooted<MuseRegex>| {
                            Ok(this
                                .static_captures_len()
                                .map(Value::try_from)
                                .transpose()?
                                .unwrap_or_default())
                        },
                    )
                    .with_fn(
                        Symbol::captures_symbol(),
                        1,
                        |vm: &mut VmContext<'_, '_>, this: &Rooted<MuseRegex>| {
                            let haystack = vm[Register(0)].take();
                            haystack.map_str(vm, |vm, haystack| {
                                this.captures(haystack, vm.as_ref())
                                    .map(|value| Value::dynamic(value, vm))
                                    .unwrap_or_default()
                            })
                        },
                    ),
            )
            .with_hash(|_| {
                |this, _vm, hasher| {
                    this.expr.as_str().hash(hasher);
                }
            })
            .with_eq(|_| {
                |this, vm, rhs| {
                    if let Some(rhs) = rhs.as_downcast_ref::<MuseRegex>(vm.as_ref()) {
                        Ok(this.expr.as_str() == rhs.expr.as_str())
                    } else {
                        Ok(false)
                    }
                }
            })
            .with_total_cmp(|_| {
                |this, vm, rhs| {
                    if let Some(rhs) = rhs.as_downcast_ref::<MuseRegex>(vm.as_ref()) {
                        Ok(this.expr.as_str().cmp(rhs.expr.as_str()))
                    } else if rhs.as_any_dynamic().is_none() {
                        // Dynamics sort after primitive values
                        Ok(std::cmp::Ordering::Greater)
                    } else {
                        Err(Fault::UnsupportedOperation)
                    }
                }
            })
            .with_truthy(|_| |this, _vm| !this.expr.as_str().is_empty())
            .with_to_string(|_| |this, _vm| Ok(SymbolRef::from(this.expr.as_str().to_string())))
            .with_clone()
            .with_matches(|_| {
                |this, vm, rhs| {
                    if let Some(rhs) = rhs.as_downcast_ref::<MuseString>(vm.as_ref()) {
                        Ok(this.is_match(&rhs.lock()))
                    } else {
                        rhs.map_str(vm, |_vm, rhs| this.is_match(rhs))
                    }
                }
            })
        });
        &TYPE
    }
}

impl ContainsNoRefs for MuseRegex {}

impl Display for MuseRegex {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Display::fmt(&self.expr, f)
    }
}

/// A set of captures from a regular expression search.
#[derive(Debug, Clone, Trace)]
pub struct MuseCaptures {
    matches: Vec<Value>,
    by_name: Map<Symbol, usize>,
}

impl MuseCaptures {
    fn new(
        captures: &Captures<'_>,
        haystack: &str,
        regex: &Regex,
        guard: &CollectionGuard,
    ) -> Self {
        let named_captures = regex.capture_names().flatten().count();
        let by_name = if named_captures == 0 {
            Map::new()
        } else {
            regex
                .capture_names()
                .enumerate()
                .filter_map(|(index, name)| name.map(|name| (Symbol::from(name), index)))
                .collect()
        };

        let matches = captures
            .iter()
            .map(|capture| {
                capture
                    .map(|capture| {
                        Value::dynamic(
                            MuseMatch {
                                content: AnyDynamic::new(
                                    MuseString::from(&haystack[capture.range()]),
                                    guard,
                                ),
                                start: capture.start(),
                            },
                            guard,
                        )
                    })
                    .unwrap_or_default()
            })
            .collect();

        Self { matches, by_name }
    }
}

impl CustomType for MuseCaptures {
    fn muse_type(&self) -> &TypeRef {
        static TYPE: RustType<MuseCaptures> = RustType::new("RegexCaptures", |t| {
            t.with_function_table(
                RustFunctionTable::new()
                    .with_fn(
                        Symbol::get_symbol(),
                        1,
                        |vm: &mut VmContext<'_, '_>, this: &Rooted<MuseCaptures>| {
                            let index = vm[Register(0)].take();
                            let index = if let Some(index) = index.as_usize() {
                                index
                            } else {
                                let name = index.to_string(vm)?.try_upgrade(vm.guard())?;
                                let Some(index) = this.by_name.get(&name).copied() else {
                                    return Ok(Value::NIL);
                                };
                                index
                            };
                            this.matches.get(index).copied().ok_or(Fault::OutOfBounds)
                        },
                    )
                    .with_fn(
                        Symbol::nth_symbol(),
                        1,
                        |vm: &mut VmContext<'_, '_>, this: &Rooted<MuseCaptures>| {
                            let index = vm[Register(0)].as_usize().ok_or(Fault::OutOfBounds)?;
                            this.matches.get(index).copied().ok_or(Fault::OutOfBounds)
                        },
                    ),
            )
        });
        &TYPE
    }
}

/// A regular expression match.
#[derive(Clone, Debug, Trace)]
pub struct MuseMatch {
    /// The content of the match.
    pub content: AnyDynamic,
    /// The offset within the haystack that this match occurred.
    pub start: usize,
}

impl CustomType for MuseMatch {
    fn muse_type(&self) -> &TypeRef {
        static TYPE: RustType<MuseMatch> = RustType::new("RegexMatch", |t| {
            t.with_function_table(
                RustFunctionTable::new()
                    .with_fn(
                        "content",
                        0,
                        |_vm: &mut VmContext<'_, '_>, this: &Rooted<MuseMatch>| {
                            Ok(Value::Dynamic(this.content))
                        },
                    )
                    .with_fn(
                        "start",
                        0,
                        |_vm: &mut VmContext<'_, '_>, this: &Rooted<MuseMatch>| {
                            Value::try_from(this.start)
                        },
                    ),
            )
            .with_to_string(|_| |this, vm| this.content.to_string(vm))
        });
        &TYPE
    }
}