Skip to content

Commit

Permalink
Implement fmt for binding pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
raskad committed Jul 18, 2021
1 parent e8be435 commit 3ae0ac4
Showing 1 changed file with 143 additions and 37 deletions.
180 changes: 143 additions & 37 deletions boa/src/syntax/ast/node/declaration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,8 @@ impl fmt::Display for Declaration {
write!(f, " = {}", init)?;
}
}
// TODO: fmt for binding pattern
Self::Pattern(_) => {
fmt::Display::fmt("binding pattern fmt", f)?;
Self::Pattern(pattern) => {
fmt::Display::fmt(&pattern, f)?;
}
}
Ok(())
Expand Down Expand Up @@ -296,13 +295,73 @@ impl Declaration {
}
}

#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
pub enum DeclarationPattern {
Object(DeclarationPatternObject),
Array(DeclarationPatternArray),
}

impl fmt::Display for DeclarationPattern {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self {
DeclarationPattern::Object(o) => {
write!(f, "{{ {} }}", o)?;
}
DeclarationPattern::Array(a) => {
write!(f, "[ {} ]", a)?;
}
}
Ok(())
}
}

impl DeclarationPattern {
pub(in crate::syntax) fn run(
&self,
init: Option<Value>,
context: &mut Context,
) -> Result<Vec<(Box<str>, Value)>> {
match &self {
DeclarationPattern::Object(pattern) => pattern.run(init, context),
DeclarationPattern::Array(pattern) => pattern.run(init, context),
}
}

pub fn idents(&self) -> Vec<&str> {
match &self {
DeclarationPattern::Object(pattern) => pattern.idents(),
DeclarationPattern::Array(pattern) => pattern.idents(),
}
}

pub fn init(&self) -> Option<&Node> {
match &self {
DeclarationPattern::Object(pattern) => pattern.init(),
DeclarationPattern::Array(pattern) => pattern.init(),
}
}
}

#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
pub struct DeclarationPatternObject {
bindings: Vec<BindingPatternTypeObject>,
init: Option<Node>,
}

impl fmt::Display for DeclarationPatternObject {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for binding in &self.bindings {
write!(f, " {},", binding)?;
}
if let Some(ref init) = self.init {
write!(f, " = {}", init)?;
}
Ok(())
}
}

impl DeclarationPatternObject {
pub(in crate::syntax) fn new(
bindings: Vec<BindingPatternTypeObject>,
Expand Down Expand Up @@ -476,6 +535,18 @@ pub struct DeclarationPatternArray {
init: Option<Node>,
}

impl fmt::Display for DeclarationPatternArray {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for binding in &self.bindings {
write!(f, " {},", binding)?;
}
if let Some(ref init) = self.init {
write!(f, " = {}", init)?;
}
Ok(())
}
}

impl DeclarationPatternArray {
pub(in crate::syntax) fn new(
bindings: Vec<BindingPatternTypeArray>,
Expand Down Expand Up @@ -708,40 +779,6 @@ impl DeclarationPatternArray {
}
}

#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
pub enum DeclarationPattern {
Object(DeclarationPatternObject),
Array(DeclarationPatternArray),
}

impl DeclarationPattern {
pub(in crate::syntax) fn run(
&self,
init: Option<Value>,
context: &mut Context,
) -> Result<Vec<(Box<str>, Value)>> {
match &self {
DeclarationPattern::Object(pattern) => pattern.run(init, context),
DeclarationPattern::Array(pattern) => pattern.run(init, context),
}
}

pub fn idents(&self) -> Vec<&str> {
match &self {
DeclarationPattern::Object(pattern) => pattern.idents(),
DeclarationPattern::Array(pattern) => pattern.idents(),
}
}

pub fn init(&self) -> Option<&Node> {
match &self {
DeclarationPattern::Object(pattern) => pattern.init(),
DeclarationPattern::Array(pattern) => pattern.init(),
}
}
}

#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
pub enum BindingPatternTypeObject {
Expand All @@ -762,6 +799,45 @@ pub enum BindingPatternTypeObject {
},
}

impl fmt::Display for BindingPatternTypeObject {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self {
BindingPatternTypeObject::Empty => {}
BindingPatternTypeObject::SingleName {
ident,
property_name,
default_init,
} => {
if ident == property_name {
fmt::Display::fmt(ident, f)?;
} else {
write!(f, "{} : {}", property_name, ident)?;
}
if let Some(ref init) = default_init {
write!(f, " = {}", init)?;
}
}
BindingPatternTypeObject::RestProperty {
property_name,
excluded_keys: _,
} => {
write!(f, "... {}", property_name)?;
}
BindingPatternTypeObject::BindingPattern {
property_name,
pattern,
default_init,
} => {
write!(f, "{} : {}", property_name, pattern)?;
if let Some(ref init) = default_init {
write!(f, " = {}", init)?;
}
}
}
Ok(())
}
}

#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
pub enum BindingPatternTypeArray {
Expand All @@ -781,3 +857,33 @@ pub enum BindingPatternTypeArray {
pattern: DeclarationPattern,
},
}

impl fmt::Display for BindingPatternTypeArray {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self {
BindingPatternTypeArray::Empty => {}
BindingPatternTypeArray::Elision => {
fmt::Display::fmt(",", f)?;
}
BindingPatternTypeArray::SingleName {
ident,
default_init,
} => {
fmt::Display::fmt(ident, f)?;
if let Some(ref init) = default_init {
write!(f, " = {}", init)?;
}
}
BindingPatternTypeArray::BindingPattern { pattern } => {
fmt::Display::fmt(pattern, f)?;
}
BindingPatternTypeArray::SingleNameRest { ident } => {
fmt::Display::fmt(ident, f)?;
}
BindingPatternTypeArray::BindingPatternRest { pattern } => {
write!(f, "... {}", pattern)?;
}
}
Ok(())
}
}

0 comments on commit 3ae0ac4

Please sign in to comment.