Skip to content

Commit

Permalink
new: add Display for all nodes (#1) (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
KennedyTedesco committed Sep 28, 2023
1 parent c0e4653 commit 2dc3b6b
Show file tree
Hide file tree
Showing 35 changed files with 5,722 additions and 35 deletions.
71 changes: 71 additions & 0 deletions src/tree/definition/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,74 @@ impl Node for AttributeDefinition {
"attribute definition".to_string()
}
}

impl std::fmt::Display for AttributeGroupDefinition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "#[{}]", self.members)
}
}

impl std::fmt::Display for AttributeDefinition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)?;
if let Some(arguments) = &self.arguments {
write!(f, "{}", arguments)?;
}
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::lexer::byte_string::ByteString;
use crate::tree::comment::CommentGroup;
use crate::tree::expression::argument::ArgumentExpression;
use crate::tree::expression::literal::Literal::Integer;
use crate::tree::expression::literal::LiteralInteger;
use crate::tree::expression::Expression;

#[test]
fn test_attribute_group_definition_display() {
let attribute_group_definition = AttributeGroupDefinition {
hash_left_bracket: 0,
members: CommaSeparated {
inner: vec![
AttributeDefinition {
name: Identifier {
position: 0,
value: ByteString::from("Foo"),
},
arguments: None,
},
AttributeDefinition {
name: Identifier {
position: 0,
value: ByteString::from("Bar"),
},
arguments: Some(ArgumentListExpression {
comments: CommentGroup { comments: vec![] },
left_parenthesis: 24,
arguments: CommaSeparated {
inner: vec![ArgumentExpression::Value {
comments: CommentGroup { comments: vec![] },
value: Expression::Literal(Integer(LiteralInteger {
comments: CommentGroup { comments: vec![] },
position: 0,
value: ByteString::from("2"),
})),
}],
commas: vec![],
},
right_parenthesis: 30,
}),
},
],
commas: vec![],
},
right_bracket: 32,
};

assert_eq!(attribute_group_definition.to_string(), "#[Foo, Bar(2)]");
}
}
97 changes: 97 additions & 0 deletions src/tree/definition/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,100 @@ impl Node for ClassDefinitionMember {
}
}
}

impl std::fmt::Display for ClassDefinitionExtends {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} {}", self.extends, self.parent)
}
}

impl std::fmt::Display for ClassDefinitionImplements {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} {}", self.implements, self.interfaces)
}
}

impl std::fmt::Display for ClassDefinitionMember {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self {
Self::Constant(constant) => write!(f, "{}", constant),
Self::Property(property) => write!(f, "{}", property),
Self::Method(method) => write!(f, "{}", method),
}
}
}

impl std::fmt::Display for ClassDefinitionBody {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{{ /* ... */ }}")
}
}

impl std::fmt::Display for ClassDefinition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} {} {}", self.modifiers, self.class, self.name)?;

if let Some(templates) = &self.templates {
write!(f, " {}", templates)?;
}

if let Some(extends) = &self.extends {
write!(f, " {}", extends)?;
}

if let Some(implements) = &self.implements {
write!(f, " {}", implements)?;
}

write!(f, " {}", self.body)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::lexer::byte_string::ByteString;
use crate::tree::definition::modifier::ModifierDefinition;

#[test]
fn test_class_definition_display() {
let class_definition = ClassDefinition {
comments: CommentGroup { comments: vec![] },
class: Keyword::new(ByteString::from("class"), 0),
attributes: vec![],
modifiers: ModifierGroupDefinition {
position: 0,
modifiers: vec![ModifierDefinition::Abstract(Keyword::new(
ByteString::from("abstract"),
0,
))],
},
name: Identifier {
position: 0,
value: ByteString::from("Foo"),
},
templates: None,
extends: Some(ClassDefinitionExtends {
extends: Keyword::new(ByteString::from("extends"), 0),
parent: TemplatedIdentifier {
name: Identifier {
position: 0,
value: ByteString::from("Bar"),
},
templates: None,
},
}),
implements: None,
body: ClassDefinitionBody {
left_brace: 19,
members: vec![],
right_brace: 20,
},
};

assert_eq!(
class_definition.to_string(),
"abstract class Foo extends Bar { /* ... */ }"
);
}
}
92 changes: 92 additions & 0 deletions src/tree/definition/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,95 @@ impl Node for ClassishConstantDefinition {
"classish constant definition".to_string()
}
}

impl std::fmt::Display for ConstantDefinition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} {} {} = {};",
self.r#const, self.type_definition, self.name, self.value
)
}
}

impl std::fmt::Display for ClassishConstantDefinition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} {} {} {} = {};",
self.modifiers, self.r#const, self.type_definition, self.name, self.value
)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::lexer::byte_string::ByteString;
use crate::tree::definition::modifier::ModifierDefinition;
use crate::tree::definition::r#type::SignedIntegerTypeDefinition;
use crate::tree::expression::literal::Literal::Integer;
use crate::tree::expression::literal::LiteralInteger;

#[test]
fn test_constant_definition_display() {
let constant_definition = ConstantDefinition {
comments: CommentGroup { comments: vec![] },
r#const: Keyword::new(ByteString::from("const"), 0),
type_definition: TypeDefinition::SignedInteger(SignedIntegerTypeDefinition::I64(
Keyword::new(ByteString::from("i64"), 15),
)),
name: Identifier {
position: 0,
value: ByteString::from("FOO"),
},
equals: 0,
value: Expression::Literal(Integer(LiteralInteger {
comments: CommentGroup { comments: vec![] },
position: 0,
value: ByteString::from("1"),
})),
semicolon: 0,
};

assert_eq!(
constant_definition.to_string(),
"const i64 FOO = 1;".to_string()
);
}

#[test]
fn test_classish_constant_definition_display() {
let classish_constant_definition = ClassishConstantDefinition {
comments: CommentGroup { comments: vec![] },
attributes: vec![],
modifiers: ModifierGroupDefinition {
position: 0,
modifiers: vec![ModifierDefinition::Private(Keyword::new(
ByteString::from("private"),
0,
))],
},
r#const: Keyword::new(ByteString::from("const"), 0),
type_definition: TypeDefinition::SignedInteger(SignedIntegerTypeDefinition::I64(
Keyword::new(ByteString::from("i64"), 15),
)),
name: Identifier {
position: 0,
value: ByteString::from("FOO"),
},
equals: 0,
value: Expression::Literal(Integer(LiteralInteger {
comments: CommentGroup { comments: vec![] },
position: 0,
value: ByteString::from("1"),
})),
semicolon: 0,
};

assert_eq!(
classish_constant_definition.to_string(),
"private const i64 FOO = 1;".to_string()
);
}
}
Loading

0 comments on commit 2dc3b6b

Please sign in to comment.