Skip to main content

boa_ast/expression/
parenthesized.rs

1use super::Expression;
2use crate::{
3    Span, Spanned,
4    visitor::{VisitWith, Visitor, VisitorMut},
5};
6use boa_interner::{Interner, ToInternedString};
7use core::ops::ControlFlow;
8
9/// A parenthesized expression.
10///
11/// More information:
12///  - [ECMAScript reference][spec]
13///  - [MDN documentation][mdn]
14///
15/// [spec]: https://tc39.es/ecma262/#sec-grouping-operator
16/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Grouping
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
19#[derive(Clone, Debug, PartialEq)]
20pub struct Parenthesized {
21    pub(crate) expression: Box<Expression>,
22    span: Span,
23}
24
25impl Parenthesized {
26    /// Creates a parenthesized expression.
27    #[inline]
28    #[must_use]
29    pub fn new(expression: Expression, span: Span) -> Self {
30        Self {
31            expression: Box::new(expression),
32            span,
33        }
34    }
35
36    /// Gets the expression of this parenthesized expression.
37    #[inline]
38    #[must_use]
39    pub const fn expression(&self) -> &Expression {
40        &self.expression
41    }
42}
43
44impl Spanned for Parenthesized {
45    #[inline]
46    fn span(&self) -> Span {
47        self.span
48    }
49}
50
51impl From<Parenthesized> for Expression {
52    fn from(p: Parenthesized) -> Self {
53        Self::Parenthesized(p)
54    }
55}
56
57impl ToInternedString for Parenthesized {
58    #[inline]
59    fn to_interned_string(&self, interner: &Interner) -> String {
60        format!("({})", self.expression.to_interned_string(interner))
61    }
62}
63
64impl VisitWith for Parenthesized {
65    fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
66    where
67        V: Visitor<'a>,
68    {
69        visitor.visit_expression(&self.expression)
70    }
71
72    fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy>
73    where
74        V: VisitorMut<'a>,
75    {
76        visitor.visit_expression_mut(&mut self.expression)
77    }
78}