Skip to content

Commit

Permalink
new: allow async as a modifier (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
KennedyTedesco committed Sep 28, 2023
1 parent d1398da commit c0e4653
Show file tree
Hide file tree
Showing 82 changed files with 1,124 additions and 245 deletions.
9 changes: 4 additions & 5 deletions src/parser/internal/definition/function.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::lexer::token::TokenKind;
use crate::parser::internal::definition::modifier;
use crate::parser::internal::definition::parameter;
use crate::parser::internal::definition::r#type;
use crate::parser::internal::definition::template;
Expand All @@ -16,12 +17,10 @@ use crate::tree::definition::function::MethodTypeConstraintGroupDefinition;
use crate::tree::definition::modifier::ModifierGroupDefinition;

pub fn function_definition(state: &mut State) -> ParseResult<FunctionDefinition> {
let comments = state.iterator.comments();
let attributes = state.get_attributes();

Ok(FunctionDefinition {
comments,
attributes,
comments: state.iterator.comments(),
attributes: state.get_attributes(),
modifiers: modifier::collect(state)?,
function: utils::skip_keyword(state, TokenKind::Function)?,
name: identifier::identifier_maybe_soft_reserved(state)?,
templates: if state.iterator.current().kind == TokenKind::LessThan {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/internal/definition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn definition(state: &mut State) -> ParseResult<Definition> {
)));
}

if current.kind == TokenKind::Function {
if matches!(current.kind, TokenKind::Async | TokenKind::Function) {
return Ok(Definition::Function(Box::new(
function::function_definition(state)?,
)));
Expand Down
4 changes: 4 additions & 0 deletions src/parser/internal/definition/modifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn collect(state: &mut State) -> ParseResult<ModifierGroupDefinition> {
TokenKind::Abstract,
TokenKind::Static,
TokenKind::Readonly,
TokenKind::Async,
];

let mut current = state.iterator.current().clone();
Expand Down Expand Up @@ -44,6 +45,9 @@ pub fn collect(state: &mut State) -> ParseResult<ModifierGroupDefinition> {
TokenKind::Readonly => {
ModifierDefinition::Readonly(utils::skip_keyword(state, TokenKind::Readonly)?)
}
TokenKind::Async => {
ModifierDefinition::Async(utils::skip_keyword(state, TokenKind::Async)?)
}
_ => unreachable!(),
});

Expand Down
21 changes: 6 additions & 15 deletions src/parser/internal/expression/function.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::lexer::token::TokenKind;
use crate::parser::internal::definition::modifier;
use crate::parser::internal::definition::parameter;
use crate::parser::internal::definition::r#type;
use crate::parser::internal::expression;
Expand All @@ -18,12 +19,7 @@ pub fn anonymous_function_expression(
) -> ParseResult<AnonymousFunctionExpression> {
let comments = state.iterator.comments();
let attributes = state.get_attributes();
let current = state.iterator.current();
let r#static = if current.kind == TokenKind::Static {
Some(utils::skip_keyword(state, TokenKind::Static)?)
} else {
None
};
let modifiers = modifier::collect(state)?;

let function = utils::skip_keyword(state, TokenKind::Function)?;
let parameters = parameter::function_like_parameter_list_definition(state)?;
Expand Down Expand Up @@ -55,9 +51,9 @@ pub fn anonymous_function_expression(

Ok(AnonymousFunctionExpression {
comments,
r#static,
function,
attributes,
modifiers,
function,
parameters,
use_clause: uses,
return_type: FunctionLikeReturnTypeDefinition {
Expand All @@ -69,19 +65,14 @@ pub fn anonymous_function_expression(
}

pub fn arrow_function_expression(state: &mut State) -> ParseResult<ArrowFunctionExpression> {
let current = state.iterator.current();

let comments = state.iterator.comments();
let attributes = state.get_attributes();
let modifiers = modifier::collect(state)?;

Ok(ArrowFunctionExpression {
comments,
attributes,
r#static: if current.kind == TokenKind::Static {
Some(utils::skip_keyword(state, TokenKind::Static)?)
} else {
None
},
modifiers,
r#fn: utils::skip_keyword(state, TokenKind::Fn)?,
parameters: parameter::function_like_parameter_list_definition(state)?,
return_type: FunctionLikeReturnTypeDefinition {
Expand Down
4 changes: 3 additions & 1 deletion src/tree/definition/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ pub struct FunctionLikeParameterListDefinition {
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct FunctionDefinition {
pub comments: CommentGroup,
pub attributes: Vec<AttributeGroupDefinition>,
pub comments: CommentGroup,
pub modifiers: ModifierGroupDefinition,
pub function: Keyword,
pub name: Identifier,
pub templates: Option<TemplateGroupDefinition>,
Expand Down Expand Up @@ -255,6 +256,7 @@ impl Node for FunctionDefinition {
children.push(templates);
}

children.push(&self.modifiers);
children.push(&self.parameters);
children.push(&self.return_type);
children.push(&self.body);
Expand Down
6 changes: 6 additions & 0 deletions src/tree/definition/modifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub enum ModifierDefinition {
Readonly(Keyword),
Final(Keyword),
Abstract(Keyword),
Async(Keyword),
}

#[derive(Debug, Clone, Hash, Eq, PartialEq, Deserialize, Serialize, Encode, Decode, JsonSchema)]
Expand Down Expand Up @@ -62,6 +63,7 @@ impl Node for ModifierDefinition {
| Self::Readonly(keyword)
| Self::Static(keyword)
| Self::Abstract(keyword)
| Self::Async(keyword)
| Self::Final(keyword) => keyword.initial_position(),
}
}
Expand All @@ -74,6 +76,7 @@ impl Node for ModifierDefinition {
| Self::Readonly(keyword)
| Self::Static(keyword)
| Self::Abstract(keyword)
| Self::Async(keyword)
| Self::Final(keyword) => keyword.final_position(),
}
}
Expand All @@ -86,6 +89,7 @@ impl Node for ModifierDefinition {
| Self::Readonly(keyword)
| Self::Static(keyword)
| Self::Abstract(keyword)
| Self::Async(keyword)
| Self::Final(keyword) => vec![keyword as &dyn Node],
}
}
Expand All @@ -98,6 +102,7 @@ impl Node for ModifierDefinition {
Self::Readonly(_keyword) => "readonly modifier definition".to_string(),
Self::Static(_keyword) => "static modifier definition".to_string(),
Self::Abstract(_keyword) => "abstract modifier definition".to_string(),
Self::Async(_keyword) => "async modifier definition".to_string(),
Self::Final(_keyword) => "final modifier definition".to_string(),
}
}
Expand All @@ -112,6 +117,7 @@ impl std::fmt::Display for ModifierDefinition {
| Self::Readonly(keyword)
| Self::Static(keyword)
| Self::Abstract(keyword)
| Self::Async(keyword)
| Self::Final(keyword) => write!(f, "{}", keyword.value),
}
}
Expand Down
24 changes: 9 additions & 15 deletions src/tree/expression/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::tree::comment::CommentGroup;
use crate::tree::definition::attribute::AttributeGroupDefinition;
use crate::tree::definition::function::FunctionLikeParameterListDefinition;
use crate::tree::definition::function::FunctionLikeReturnTypeDefinition;
use crate::tree::definition::modifier::ModifierGroupDefinition;
use crate::tree::expression::Expression;
use crate::tree::statement::block::BlockStatement;
use crate::tree::token::Keyword;
Expand All @@ -20,7 +21,7 @@ use crate::tree::Node;
pub struct ArrowFunctionExpression {
pub comments: CommentGroup,
pub attributes: Vec<AttributeGroupDefinition>,
pub r#static: Option<Keyword>,
pub modifiers: ModifierGroupDefinition,
pub r#fn: Keyword,
pub parameters: FunctionLikeParameterListDefinition,
pub return_type: FunctionLikeReturnTypeDefinition,
Expand All @@ -33,7 +34,7 @@ pub struct ArrowFunctionExpression {
pub struct AnonymousFunctionExpression {
pub comments: CommentGroup,
pub attributes: Vec<AttributeGroupDefinition>,
pub r#static: Option<Keyword>,
pub modifiers: ModifierGroupDefinition,
pub function: Keyword,
pub parameters: FunctionLikeParameterListDefinition,
pub use_clause: Option<AnonymousFunctionUseClauseExpression>,
Expand Down Expand Up @@ -68,8 +69,8 @@ impl Node for ArrowFunctionExpression {
return attribute.initial_position();
}

if let Some(r#static) = &self.r#static {
return r#static.initial_position();
if !&self.modifiers.modifiers.is_empty() {
return self.modifiers.initial_position();
}

self.r#fn.initial_position()
Expand All @@ -86,12 +87,8 @@ impl Node for ArrowFunctionExpression {
children.push(attribute);
}

if let Some(r#static) = &self.r#static {
children.push(r#static);
}

children.push(&self.r#fn);

children.push(&self.modifiers);
children.push(&self.parameters);
children.push(&self.return_type);
children.push(self.body.as_ref());
Expand All @@ -114,8 +111,8 @@ impl Node for AnonymousFunctionExpression {
return attribute.initial_position();
}

if let Some(r#static) = &self.r#static {
return r#static.initial_position();
if !&self.modifiers.modifiers.is_empty() {
return self.modifiers.initial_position();
}

self.function.initial_position()
Expand All @@ -132,11 +129,8 @@ impl Node for AnonymousFunctionExpression {
children.push(attribute);
}

if let Some(r#static) = &self.r#static {
children.push(r#static);
}

children.push(&self.function);
children.push(&self.modifiers);
children.push(&self.parameters);
children.push(&self.return_type);
children.push(&self.body);
Expand Down
6 changes: 5 additions & 1 deletion tests/samples/0001/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ DefinitionTree {
definitions: [
Function(
FunctionDefinition {
attributes: [],
comments: CommentGroup {
comments: [],
},
attributes: [],
modifiers: ModifierGroupDefinition {
position: 0,
modifiers: [],
},
function: Keyword {
value: "function",
position: 0,
Expand Down
6 changes: 5 additions & 1 deletion tests/samples/0002/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ DefinitionTree {
definitions: [
Function(
FunctionDefinition {
attributes: [],
comments: CommentGroup {
comments: [],
},
attributes: [],
modifiers: ModifierGroupDefinition {
position: 1,
modifiers: [],
},
function: Keyword {
value: "function",
position: 1,
Expand Down
6 changes: 5 additions & 1 deletion tests/samples/0004/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ DefinitionTree {
definitions: [
Function(
FunctionDefinition {
attributes: [],
comments: CommentGroup {
comments: [],
},
attributes: [],
modifiers: ModifierGroupDefinition {
position: 0,
modifiers: [],
},
function: Keyword {
value: "function",
position: 0,
Expand Down
6 changes: 5 additions & 1 deletion tests/samples/0005/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ DefinitionTree {
definitions: [
Function(
FunctionDefinition {
attributes: [],
comments: CommentGroup {
comments: [],
},
attributes: [],
modifiers: ModifierGroupDefinition {
position: 0,
modifiers: [],
},
function: Keyword {
value: "function",
position: 0,
Expand Down
6 changes: 5 additions & 1 deletion tests/samples/0006/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ DefinitionTree {
definitions: [
Function(
FunctionDefinition {
attributes: [],
comments: CommentGroup {
comments: [],
},
attributes: [],
modifiers: ModifierGroupDefinition {
position: 0,
modifiers: [],
},
function: Keyword {
value: "function",
position: 0,
Expand Down
6 changes: 5 additions & 1 deletion tests/samples/0007/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ DefinitionTree {
definitions: [
Function(
FunctionDefinition {
attributes: [],
comments: CommentGroup {
comments: [],
},
attributes: [],
modifiers: ModifierGroupDefinition {
position: 0,
modifiers: [],
},
function: Keyword {
value: "function",
position: 0,
Expand Down
6 changes: 5 additions & 1 deletion tests/samples/0008/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ DefinitionTree {
definitions: [
Function(
FunctionDefinition {
attributes: [],
comments: CommentGroup {
comments: [],
},
attributes: [],
modifiers: ModifierGroupDefinition {
position: 0,
modifiers: [],
},
function: Keyword {
value: "function",
position: 0,
Expand Down
6 changes: 5 additions & 1 deletion tests/samples/0009/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ DefinitionTree {
definitions: [
Function(
FunctionDefinition {
attributes: [],
comments: CommentGroup {
comments: [],
},
attributes: [],
modifiers: ModifierGroupDefinition {
position: 0,
modifiers: [],
},
function: Keyword {
value: "function",
position: 0,
Expand Down
6 changes: 5 additions & 1 deletion tests/samples/0010/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ DefinitionTree {
definitions: [
Function(
FunctionDefinition {
attributes: [],
comments: CommentGroup {
comments: [],
},
attributes: [],
modifiers: ModifierGroupDefinition {
position: 0,
modifiers: [],
},
function: Keyword {
value: "function",
position: 0,
Expand Down
Loading

0 comments on commit c0e4653

Please sign in to comment.