Skip to content

Commit

Permalink
new: add functional expression operation (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
KennedyTedesco committed Jan 23, 2023
1 parent dfa107b commit d1cdeea
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/parser/internal/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::tree::expression::operator::BitwiseOperationExpression;
use crate::tree::expression::operator::ClassOperationExpression;
use crate::tree::expression::operator::ClassOperationInitializationClassExpression;
use crate::tree::expression::operator::ExceptionOperationExpression;
use crate::tree::expression::operator::FunctionalOperationExpression;
use crate::tree::expression::operator::GeneratorOperationExpression;
use crate::tree::expression::operator::LogicalOperationExpression;
use crate::tree::expression::operator::ObjectOperationExpression;
Expand Down Expand Up @@ -144,7 +145,7 @@ macro_rules! expressions {
expressions! {
using(state):

#[before(static_arrow_function), current(TokenKind::Attribute)]
#[before(functional_expression), current(TokenKind::Attribute)]
attributes({
attribute::gather(state)?;

Expand Down Expand Up @@ -174,6 +175,24 @@ expressions! {
}
})

#[before(static_arrow_function), current(TokenKind::Dollar), peek(TokenKind::Generic | TokenKind::LeftParen)]
functional_expression({
let dollar = state.iterator.current().position;
state.iterator.next();
Ok(Expression::FunctionalOperation(FunctionalOperationExpression::Expression {
comments: state.iterator.comments(),
dollar,
generics: if state.iterator.current().kind == TokenKind::Generic {
Some(generic::generic_group(state)?)
} else {
None
},
left_parenthesis: utils::skip_left_parenthesis(state)?,
expression: Box::new(create(state)?),
right_parenthesis: utils::skip_right_parenthesis(state)?,
}))
})

#[before(static_anonymous_function), current(TokenKind::Static), peek(TokenKind::Fn)]
static_arrow_function({
Ok(Expression::ArrowFunction(function::arrow_function_expression(state)?))
Expand Down
26 changes: 25 additions & 1 deletion src/tree/expression/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ pub enum FunctionalOperationExpression {
greater_than: usize,
right: Box<Expression>,
},
Expression {
comments: CommentGroup,
dollar: usize,
generics: Option<GenericGroupExpression>,
left_parenthesis: usize,
expression: Box<Expression>,
right_parenthesis: usize,
},
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
Expand Down Expand Up @@ -645,31 +653,47 @@ impl RangeOperationExpression {
impl Node for FunctionalOperationExpression {
fn comments(&self) -> Option<&CommentGroup> {
match self {
Self::Pipe { comments, .. } => Some(comments),
Self::Pipe { comments, .. } | Self::Expression { comments, .. } => Some(comments),
}
}

fn initial_position(&self) -> usize {
match &self {
Self::Pipe { left, .. } => left.initial_position(),
Self::Expression { dollar, .. } => *dollar,
}
}

fn final_position(&self) -> usize {
match &self {
Self::Pipe { right, .. } => right.final_position(),
Self::Expression {
right_parenthesis, ..
} => *right_parenthesis,
}
}

fn children(&self) -> Vec<&dyn Node> {
match &self {
Self::Pipe { left, right, .. } => vec![left.as_ref(), right.as_ref()],
Self::Expression {
generics,
expression,
..
} => {
let mut children: Vec<&dyn Node> = vec![expression.as_ref()];
if let Some(generics) = generics {
children.push(generics);
}
children
}
}
}

fn get_description(&self) -> String {
match &self {
Self::Pipe { .. } => "pipe functional operation expression".to_string(),
Self::Expression { .. } => "functional operation expression".to_string(),
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/samples/0111/code.ara
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function foo(): void {
$a = $(123);
$b = $::<int>($a);
return $($b);
}
183 changes: 183 additions & 0 deletions tests/samples/0111/tree.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
DefinitionTree {
definitions: [
Function(
FunctionDefinition {
comments: CommentGroup {
comments: [],
},
attributes: [],
function: Keyword {
value: "function",
position: 0,
},
name: Identifier {
position: 9,
value: "foo",
},
templates: None,
parameters: FunctionLikeParameterListDefinition {
comments: CommentGroup {
comments: [],
},
left_parenthesis: 12,
parameters: CommaSeparated {
inner: [],
commas: [],
},
right_parenthesis: 13,
},
return_type: FunctionLikeReturnTypeDefinition {
colon: 14,
type_definition: Void(
Keyword {
value: "void",
position: 16,
},
),
},
body: BlockStatement {
comments: CommentGroup {
comments: [],
},
left_brace: 21,
statements: [
Expression(
ExpressionStatement {
comments: CommentGroup {
comments: [],
},
expression: AssignmentOperation(
Assignment {
comments: CommentGroup {
comments: [],
},
left: Variable(
Variable {
position: 27,
name: "$a",
},
),
equals: 30,
right: FunctionalOperation(
Expression {
comments: CommentGroup {
comments: [],
},
dollar: 32,
generics: None,
left_parenthesis: 33,
expression: Literal(
Integer(
LiteralInteger {
comments: CommentGroup {
comments: [],
},
value: "123",
position: 34,
},
),
),
right_parenthesis: 37,
},
),
},
),
semicolon: 38,
},
),
Expression(
ExpressionStatement {
comments: CommentGroup {
comments: [],
},
expression: AssignmentOperation(
Assignment {
comments: CommentGroup {
comments: [],
},
left: Variable(
Variable {
position: 44,
name: "$b",
},
),
equals: 47,
right: FunctionalOperation(
Expression {
comments: CommentGroup {
comments: [],
},
dollar: 49,
generics: Some(
GenericGroupExpression {
double_colon_less_than: 50,
types: CommaSeparated {
inner: [
SignedInteger(
Default(
Keyword {
value: "int",
position: 53,
},
),
),
],
commas: [],
},
greater_than: 56,
},
),
left_parenthesis: 57,
expression: Variable(
Variable {
position: 58,
name: "$a",
},
),
right_parenthesis: 60,
},
),
},
),
semicolon: 61,
},
),
Return(
Explicit {
comments: CommentGroup {
comments: [],
},
return: Keyword {
value: "return",
position: 67,
},
expression: Some(
FunctionalOperation(
Expression {
comments: CommentGroup {
comments: [],
},
dollar: 74,
generics: None,
left_parenthesis: 75,
expression: Variable(
Variable {
position: 76,
name: "$b",
},
),
right_parenthesis: 78,
},
),
),
semicolon: 79,
},
),
],
right_brace: 81,
},
},
),
],
eof: 83,
}

0 comments on commit d1cdeea

Please sign in to comment.