mirror of https://github.com/boa-dev/boa.git
Browse Source
* Fix regular expression construction The previous implementation regular expression (e.g. /abc/) used the global `RegExp`, which caused errors when the `RegExp` was overwritten. * Apply Reviewpull/3339/head
Haled Odat
8 months ago
committed by
GitHub
11 changed files with 217 additions and 39 deletions
@ -0,0 +1,94 @@
|
||||
//! This module contains the ECMAScript representation regular expressions.
|
||||
//!
|
||||
//! More information:
|
||||
//! - [ECMAScript reference][spec]
|
||||
//! - [MDN documentation][mdn]
|
||||
//!
|
||||
//! [spec]: https://tc39.es/ecma262/#sec-literals-regular-expression-literals
|
||||
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions
|
||||
|
||||
use std::ops::ControlFlow; |
||||
|
||||
use boa_interner::{Interner, Sym, ToInternedString}; |
||||
|
||||
use crate::{ |
||||
try_break, |
||||
visitor::{VisitWith, Visitor, VisitorMut}, |
||||
}; |
||||
|
||||
use super::Expression; |
||||
|
||||
/// Regular expressions in ECMAScript.
|
||||
///
|
||||
/// More information:
|
||||
/// - [ECMAScript reference][spec]
|
||||
/// - [MDN documentation][mdn]
|
||||
///
|
||||
/// [spec]: https://tc39.es/ecma262/#sec-literals-regular-expression-literals
|
||||
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
||||
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] |
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
||||
pub struct RegExpLiteral { |
||||
pattern: Sym, |
||||
flags: Sym, |
||||
} |
||||
|
||||
impl RegExpLiteral { |
||||
/// Create a new [`RegExp`].
|
||||
#[inline] |
||||
#[must_use] |
||||
pub const fn new(pattern: Sym, flags: Sym) -> Self { |
||||
Self { pattern, flags } |
||||
} |
||||
|
||||
/// Get the pattern part of the [`RegExp`].
|
||||
#[inline] |
||||
#[must_use] |
||||
pub const fn pattern(&self) -> Sym { |
||||
self.pattern |
||||
} |
||||
|
||||
/// Get the flags part of the [`RegExp`].
|
||||
#[inline] |
||||
#[must_use] |
||||
pub const fn flags(&self) -> Sym { |
||||
self.flags |
||||
} |
||||
} |
||||
|
||||
impl ToInternedString for RegExpLiteral { |
||||
#[inline] |
||||
fn to_interned_string(&self, interner: &Interner) -> String { |
||||
let pattern = interner.resolve_expect(self.pattern); |
||||
let flags = interner.resolve_expect(self.flags); |
||||
format!("/{pattern}/{flags}") |
||||
} |
||||
} |
||||
|
||||
impl From<RegExpLiteral> for Expression { |
||||
#[inline] |
||||
fn from(value: RegExpLiteral) -> Self { |
||||
Self::RegExpLiteral(value) |
||||
} |
||||
} |
||||
|
||||
impl VisitWith for RegExpLiteral { |
||||
#[inline] |
||||
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy> |
||||
where |
||||
V: Visitor<'a>, |
||||
{ |
||||
try_break!(visitor.visit_sym(&self.pattern)); |
||||
visitor.visit_sym(&self.flags) |
||||
} |
||||
|
||||
#[inline] |
||||
fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy> |
||||
where |
||||
V: VisitorMut<'a>, |
||||
{ |
||||
try_break!(visitor.visit_sym_mut(&mut self.pattern)); |
||||
visitor.visit_sym_mut(&mut self.flags) |
||||
} |
||||
} |
Loading…
Reference in new issue