Skip to content

Commit

Permalink
[parser][printer] Consistent comment attachment and printing for expr…
Browse files Browse the repository at this point in the history
…essions (#1167)
  • Loading branch information
SamChou19815 authored Dec 31, 2023
1 parent f6a2f63 commit b0cf59e
Show file tree
Hide file tree
Showing 17 changed files with 1,189 additions and 737 deletions.
52 changes: 37 additions & 15 deletions crates/samlang-ast/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ pub enum CommentKind {

#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Comment {
pub location: Location,
pub kind: CommentKind,
pub text: PStr,
}

#[derive(Clone, PartialEq, Eq)]
pub enum CommentsNode {
NoComment,
Comments(Location, Vec<Comment>),
Comments(Vec<Comment>),
}

static EMPTY_COMMENTS: Vec<Comment> = vec![];
Expand All @@ -29,14 +28,13 @@ impl CommentsNode {
pub fn iter(&self) -> std::slice::Iter<'_, Comment> {
match self {
CommentsNode::NoComment => EMPTY_COMMENTS.iter(),
CommentsNode::Comments(_, comments) => comments.iter(),
CommentsNode::Comments(comments) => comments.iter(),
}
}

pub fn from(comments: Vec<Comment>) -> CommentsNode {
if !comments.is_empty() {
let loc = comments.iter().map(|it| it.location).reduce(|l1, l2| l1.union(&l2)).unwrap();
CommentsNode::Comments(loc, comments)
CommentsNode::Comments(comments)
} else {
CommentsNode::NoComment
}
Expand Down Expand Up @@ -122,10 +120,9 @@ impl Literal {
}

pub mod annotation {
use samlang_heap::ModuleReference;

use super::super::Location;
use super::CommentReference;
use crate::Location;
use samlang_heap::ModuleReference;

#[derive(Copy, Clone, PartialEq, Eq)]
pub enum PrimitiveTypeKind {
Expand Down Expand Up @@ -400,10 +397,18 @@ pub mod expr {
}
}

#[derive(Clone, PartialEq, Eq)]
pub struct ParenthesizedExpressionList<T: Clone> {
pub loc: Location,
pub start_associated_comments: CommentReference,
pub ending_associated_comments: CommentReference,
pub expressions: Vec<E<T>>,
}

#[derive(Clone, PartialEq, Eq)]
pub struct FieldAccess<T: Clone> {
pub common: ExpressionCommon<T>,
pub explicit_type_arguments: Vec<annotation::T>,
pub explicit_type_arguments: Option<super::annotation::TypeArguments>,
pub inferred_type_arguments: Vec<T>,
pub object: Box<E<T>>,
pub field_name: Id,
Expand All @@ -413,7 +418,7 @@ pub mod expr {
#[derive(Clone, PartialEq, Eq)]
pub struct MethodAccess<T: Clone> {
pub common: ExpressionCommon<T>,
pub explicit_type_arguments: Vec<annotation::T>,
pub explicit_type_arguments: Option<super::annotation::TypeArguments>,
pub inferred_type_arguments: Vec<T>,
pub object: Box<E<T>>,
pub method_name: Id,
Expand Down Expand Up @@ -445,7 +450,7 @@ pub mod expr {
pub struct Call<T: Clone> {
pub common: ExpressionCommon<T>,
pub callee: Box<E<T>>,
pub arguments: Vec<E<T>>,
pub arguments: ParenthesizedExpressionList<T>,
}

#[derive(Copy, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -536,6 +541,7 @@ pub mod expr {
pub loc: Location,
pub pattern: pattern::MatchingPattern<T>,
pub body: Box<E<T>>,
pub ending_associated_comments: CommentReference,
}

#[derive(Clone, PartialEq, Eq)]
Expand All @@ -545,10 +551,17 @@ pub mod expr {
pub cases: Vec<VariantPatternToExpression<T>>,
}

#[derive(Clone, PartialEq, Eq)]
pub struct LambdaParameters<T: Clone> {
pub loc: Location,
pub parameters: Vec<super::OptionallyAnnotatedId<T>>,
pub ending_associated_comments: CommentReference,
}

#[derive(Clone, PartialEq, Eq)]
pub struct Lambda<T: Clone> {
pub common: ExpressionCommon<T>,
pub parameters: Vec<super::OptionallyAnnotatedId<T>>,
pub parameters: LambdaParameters<T>,
pub captured: HashMap<PStr, T>,
pub body: Box<E<T>>,
}
Expand All @@ -567,14 +580,15 @@ pub mod expr {
pub common: ExpressionCommon<T>,
pub statements: Vec<DeclarationStatement<T>>,
pub expression: Option<Box<E<T>>>,
pub ending_associated_comments: CommentReference,
}

#[derive(Clone, PartialEq, Eq)]
pub enum E<T: Clone> {
Literal(ExpressionCommon<T>, Literal),
LocalId(ExpressionCommon<T>, Id),
ClassId(ExpressionCommon<T>, ModuleReference, Id),
Tuple(ExpressionCommon<T>, Vec<E<T>>),
Tuple(ExpressionCommon<T>, ParenthesizedExpressionList<T>),
FieldAccess(FieldAccess<T>),
MethodAccess(MethodAccess<T>),
Unary(Unary<T>),
Expand Down Expand Up @@ -645,6 +659,14 @@ pub mod expr {
}
}

#[derive(Clone, PartialEq, Eq)]
pub struct FunctionParameters {
pub location: Location,
pub start_associated_comments: CommentReference,
pub ending_associated_comments: CommentReference,
pub parameters: Rc<Vec<AnnotatedId<()>>>,
}

#[derive(Clone, PartialEq, Eq)]
pub struct ClassMemberDeclaration {
pub loc: Location,
Expand All @@ -653,8 +675,8 @@ pub struct ClassMemberDeclaration {
pub is_method: bool,
pub name: Id,
pub type_parameters: Option<annotation::TypeParameters>,
pub type_: annotation::Function,
pub parameters: Rc<Vec<AnnotatedId<()>>>,
pub parameters: FunctionParameters,
pub return_type: annotation::T,
}

#[derive(Clone, PartialEq, Eq)]
Expand Down
81 changes: 55 additions & 26 deletions crates/samlang-ast/src/source_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ mod tests {

#[test]
fn boilterplate() {
let comment = Comment {
location: Location::dummy(),
kind: CommentKind::BLOCK,
text: Heap::new().alloc_str_for_test("d"),
};
let comment = Comment { kind: CommentKind::BLOCK, text: Heap::new().alloc_str_for_test("d") };

assert!(CommentKind::DOC == CommentKind::DOC.clone());
format!("{:?}", comment.clone().text);
Expand All @@ -29,8 +25,7 @@ mod tests {
.iter()
.collect_vec()
.is_empty());
assert!(CommentsNode::Comments(Location::dummy(), vec![comment])
.eq(&CommentsNode::Comments(Location::dummy(), vec![comment])));
assert!(CommentsNode::Comments(vec![comment]).eq(&CommentsNode::Comments(vec![comment])));
assert!(CommentStore::new().eq(&CommentStore::new()));

assert_eq!("!", expr::UnaryOperator::NOT.clone().to_string());
Expand Down Expand Up @@ -326,26 +321,39 @@ mod tests {
ModuleReference::DUMMY,
Id::from(heap.alloc_str_for_test("s")),
));
coverage_hack_for_expr(E::Tuple(common.clone(), vec![zero_expr.clone(), zero_expr.clone()]));
coverage_hack_for_expr(E::Tuple(
common.clone(),
ParenthesizedExpressionList {
loc: Location::dummy(),
start_associated_comments: NO_COMMENT_REFERENCE,
ending_associated_comments: NO_COMMENT_REFERENCE,
expressions: vec![zero_expr.clone(), zero_expr.clone()],
},
));
coverage_hack_for_expr(E::FieldAccess(FieldAccess {
common: common.clone(),
explicit_type_arguments: vec![],
explicit_type_arguments: None,
inferred_type_arguments: vec![],
object: Box::new(zero_expr.clone()),
field_name: Id::from(heap.alloc_str_for_test("name")),
field_order: 1,
}));
coverage_hack_for_expr(E::MethodAccess(MethodAccess {
common: common.clone(),
explicit_type_arguments: vec![],
explicit_type_arguments: None,
inferred_type_arguments: vec![],
object: Box::new(zero_expr.clone()),
method_name: Id::from(heap.alloc_str_for_test("name")),
}));
coverage_hack_for_expr(E::Call(Call {
common: common.clone(),
callee: Box::new(zero_expr.clone()),
arguments: vec![],
arguments: ParenthesizedExpressionList {
loc: Location::dummy(),
start_associated_comments: NO_COMMENT_REFERENCE,
ending_associated_comments: NO_COMMENT_REFERENCE,
expressions: vec![],
},
}));
coverage_hack_for_expr(E::Unary(Unary {
common: common.clone(),
Expand Down Expand Up @@ -387,15 +395,20 @@ mod tests {
associated_comments: NO_COMMENT_REFERENCE,
},
body: Box::new(zero_expr.clone()),
ending_associated_comments: NO_COMMENT_REFERENCE,
}],
}));
coverage_hack_for_expr(E::Lambda(Lambda {
common: common.clone(),
parameters: vec![OptionallyAnnotatedId {
name: Id::from(heap.alloc_str_for_test("name")),
type_: (),
annotation: None,
}],
parameters: LambdaParameters {
loc: Location::dummy(),
parameters: vec![OptionallyAnnotatedId {
name: Id::from(heap.alloc_str_for_test("name")),
type_: (),
annotation: None,
}],
ending_associated_comments: NO_COMMENT_REFERENCE,
},
captured: HashMap::new(),
body: Box::new(zero_expr.clone()),
}));
Expand Down Expand Up @@ -508,6 +521,7 @@ mod tests {
},
],
expression: Some(Box::new(zero_expr.clone())),
ending_associated_comments: NO_COMMENT_REFERENCE,
}));
}

Expand Down Expand Up @@ -565,12 +579,17 @@ mod tests {
ending_associated_comments: NO_COMMENT_REFERENCE,
parameters: vec![]
}),
type_: builder.fn_annot_unwrapped(vec![], builder.int_annot()),
parameters: Rc::new(vec![AnnotatedId {
name: Id::from(PStr::LOWER_A),
type_: (),
annotation: builder.int_annot()
}])
parameters: FunctionParameters {
location: Location::dummy(),
start_associated_comments: NO_COMMENT_REFERENCE,
ending_associated_comments: NO_COMMENT_REFERENCE,
parameters: Rc::new(vec![AnnotatedId {
name: Id::from(PStr::LOWER_A),
type_: (),
annotation: builder.int_annot()
}])
},
return_type: builder.int_annot(),
}]
}
.clone()
Expand Down Expand Up @@ -657,8 +676,13 @@ mod tests {
ending_associated_comments: NO_COMMENT_REFERENCE,
parameters: vec![],
}),
type_: builder.fn_annot_unwrapped(vec![], builder.int_annot()),
parameters: Rc::new(vec![]),
parameters: FunctionParameters {
location: Location::dummy(),
start_associated_comments: NO_COMMENT_REFERENCE,
ending_associated_comments: NO_COMMENT_REFERENCE,
parameters: Rc::new(vec![]),
},
return_type: builder.int_annot(),
},
body: expr::E::Literal(expr::ExpressionCommon::dummy(()), Literal::Int(0)),
}],
Expand Down Expand Up @@ -693,8 +717,13 @@ mod tests {
ending_associated_comments: NO_COMMENT_REFERENCE,
parameters: vec![],
}),
type_: builder.fn_annot_unwrapped(vec![], builder.int_annot()),
parameters: Rc::new(vec![]),
parameters: FunctionParameters {
location: Location::dummy(),
start_associated_comments: NO_COMMENT_REFERENCE,
ending_associated_comments: NO_COMMENT_REFERENCE,
parameters: Rc::new(vec![]),
},
return_type: builder.int_annot(),
}],
});
assert!(interface.clone().eq(&interface));
Expand Down
Loading

0 comments on commit b0cf59e

Please sign in to comment.