Skip to content

Commit

Permalink
chore: allow using statements to contain no assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
azjezz committed Jan 26, 2023
1 parent fd67a42 commit 0a4405a
Show file tree
Hide file tree
Showing 4 changed files with 280 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/parser/internal/statement/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ pub fn using_statement(state: &mut State) -> ParseResult<UsingStatement> {
let mut inner = vec![];
let mut commas = vec![];

let mut current = state.iterator.current();
loop {
if current.kind == TokenKind::If || current.kind == TokenKind::LeftBrace {
break;
}

inner.push({
let comments = state.iterator.comments();
let variable = variable::parse(state)?;
Expand All @@ -79,7 +84,7 @@ pub fn using_statement(state: &mut State) -> ParseResult<UsingStatement> {
}
});

let mut current = state.iterator.current();
current = state.iterator.current();
if current.kind != TokenKind::Comma {
break;
}
Expand All @@ -89,10 +94,6 @@ pub fn using_statement(state: &mut State) -> ParseResult<UsingStatement> {
state.iterator.next();

current = state.iterator.current();

if current.kind == TokenKind::If || current.kind == TokenKind::LeftParen {
break;
}
}

let assignments = CommaSeparated { inner, commas };
Expand Down
7 changes: 6 additions & 1 deletion src/parser/internal/statement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ fn statement(state: &mut State) -> ParseResult<Statement> {
TokenKind::If => Statement::If(Box::new(control_flow::if_statement(state)?)),
TokenKind::Try => Statement::Try(Box::new(r#try::try_statement(state)?)),
TokenKind::LeftBrace => Statement::Block(Box::new(block::block_statement(state)?)),
TokenKind::Using if matches!(state.iterator.lookahead(1).kind, TokenKind::Variable) => {
TokenKind::Using
if matches!(
state.iterator.lookahead(1).kind,
TokenKind::Variable | TokenKind::LeftBrace | TokenKind::If
) =>
{
Statement::Using(Box::new(control_flow::using_statement(state)?))
}
TokenKind::Return => Statement::Return(Box::new(ReturnStatement::Explicit {
Expand Down
17 changes: 17 additions & 0 deletions tests/samples/0112/code.ara
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function foo(): void {
using {

}

using if $a {

}

using $a = foo() if $a {

}

using $a = foo() {

}
}
251 changes: 251 additions & 0 deletions tests/samples/0112/tree.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
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: [
Using(
UsingStatement {
comments: CommentGroup {
comments: [],
},
using: Keyword {
value: "using",
position: 27,
},
assignments: CommaSeparated {
inner: [],
commas: [],
},
if_clause: None,
block: BlockStatement {
comments: CommentGroup {
comments: [],
},
left_brace: 33,
statements: [],
right_brace: 40,
},
},
),
Using(
UsingStatement {
comments: CommentGroup {
comments: [],
},
using: Keyword {
value: "using",
position: 47,
},
assignments: CommaSeparated {
inner: [],
commas: [],
},
if_clause: Some(
UsingIfClauseStatement {
comments: CommentGroup {
comments: [],
},
if: Keyword {
value: "if",
position: 53,
},
condition: Variable(
Variable {
position: 56,
name: "$a",
},
),
},
),
block: BlockStatement {
comments: CommentGroup {
comments: [],
},
left_brace: 59,
statements: [],
right_brace: 66,
},
},
),
Using(
UsingStatement {
comments: CommentGroup {
comments: [],
},
using: Keyword {
value: "using",
position: 73,
},
assignments: CommaSeparated {
inner: [
UsingAssignmentStatement {
comments: CommentGroup {
comments: [],
},
variable: Variable {
position: 79,
name: "$a",
},
equals: 82,
expression: FunctionOperation(
Call {
comments: CommentGroup {
comments: [],
},
function: Identifier(
Identifier {
position: 84,
value: "foo",
},
),
generics: None,
arguments: ArgumentListExpression {
comments: CommentGroup {
comments: [],
},
left_parenthesis: 87,
arguments: CommaSeparated {
inner: [],
commas: [],
},
right_parenthesis: 88,
},
},
),
},
],
commas: [],
},
if_clause: Some(
UsingIfClauseStatement {
comments: CommentGroup {
comments: [],
},
if: Keyword {
value: "if",
position: 90,
},
condition: Variable(
Variable {
position: 93,
name: "$a",
},
),
},
),
block: BlockStatement {
comments: CommentGroup {
comments: [],
},
left_brace: 96,
statements: [],
right_brace: 103,
},
},
),
Using(
UsingStatement {
comments: CommentGroup {
comments: [],
},
using: Keyword {
value: "using",
position: 110,
},
assignments: CommaSeparated {
inner: [
UsingAssignmentStatement {
comments: CommentGroup {
comments: [],
},
variable: Variable {
position: 116,
name: "$a",
},
equals: 119,
expression: FunctionOperation(
Call {
comments: CommentGroup {
comments: [],
},
function: Identifier(
Identifier {
position: 121,
value: "foo",
},
),
generics: None,
arguments: ArgumentListExpression {
comments: CommentGroup {
comments: [],
},
left_parenthesis: 124,
arguments: CommaSeparated {
inner: [],
commas: [],
},
right_parenthesis: 125,
},
},
),
},
],
commas: [],
},
if_clause: None,
block: BlockStatement {
comments: CommentGroup {
comments: [],
},
left_brace: 127,
statements: [],
right_brace: 134,
},
},
),
],
right_brace: 136,
},
},
),
],
eof: 138,
}

0 comments on commit 0a4405a

Please sign in to comment.