Skip to content

Commit

Permalink
new: add Statement::Empty (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
KennedyTedesco committed Jan 16, 2023
1 parent dc42168 commit 3f56fd4
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 35 deletions.
9 changes: 1 addition & 8 deletions src/parser/internal/statement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ fn statement(state: &mut State) -> ParseResult<Statement> {
return statement(state);
}

if current.kind == TokenKind::SemiColon {
state.iterator.next();

crate::parser_report!(state, unexpected_empty_statement(current));

return statement(state);
}

let statement = match &current.kind {
TokenKind::Do => Statement::DoWhile(Box::new(r#loop::do_while_statement(state)?)),
TokenKind::While => Statement::While(Box::new(r#loop::while_statement(state)?)),
Expand All @@ -62,6 +54,7 @@ fn statement(state: &mut State) -> ParseResult<Statement> {
},
semicolon: utils::skip_semicolon(state)?,
})),
TokenKind::SemiColon => Statement::Empty(utils::skip_semicolon(state)?),
_ => {
let comments = state.iterator.comments();
let expression = expression::create(state)?;
Expand Down
27 changes: 0 additions & 27 deletions src/parser/issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,21 +417,6 @@ pub enum ParserIssueCode {
/// - Remove the try block
TryStatementMustHaveCatchOrFinally = 26,

/// Unexpected empty statement ( code = 27 )
///
/// Example:
///
/// ```ara
/// function foo(): void {
/// ;
/// }
/// ```
///
/// Possible solution(s):
///
/// - Remove the empty statement ( `;` )
UnexpectedEmptyStatement = 27,

/// Unexpected token
///
/// Example:
Expand Down Expand Up @@ -1022,18 +1007,6 @@ pub(crate) fn try_statement_must_have_catch_or_finally(
)
}

pub(crate) fn unexpected_empty_statement(state: &ParserState, current: &Token) -> Issue {
Issue::error(
ParserIssueCode::UnexpectedEmptyStatement,
"unexpected empty statement",
)
.with_source(
state.source.name(),
current.position,
current.position + current.value.len(),
)
}

pub(crate) fn unexpected_token<T: ToString>(
state: &ParserState,
expected: Vec<T>,
Expand Down
4 changes: 4 additions & 0 deletions src/tree/statement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub enum Statement {
Expression(Box<ExpressionStatement>),
Return(Box<ReturnStatement>),
Block(Box<BlockStatement>),
Empty(usize),
}

impl Node for Statement {
Expand All @@ -60,6 +61,7 @@ impl Node for Statement {
Statement::Expression(statement) => statement.initial_position(),
Statement::Return(statement) => statement.initial_position(),
Statement::Block(statement) => statement.initial_position(),
Statement::Empty(position) => *position,
}
}

Expand All @@ -77,6 +79,7 @@ impl Node for Statement {
Statement::Expression(statement) => statement.final_position(),
Statement::Return(statement) => statement.final_position(),
Statement::Block(statement) => statement.final_position(),
Statement::Empty(position) => *position + 1,
}
}

Expand All @@ -94,6 +97,7 @@ impl Node for Statement {
Statement::Expression(statement) => vec![statement.as_ref()],
Statement::Return(statement) => vec![statement.as_ref()],
Statement::Block(statement) => vec![statement.as_ref()],
Statement::Empty(_) => vec![],
}
}

Expand Down
3 changes: 3 additions & 0 deletions tests/samples/0105/code.ara
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function foo(string $foo): Bar {
;
}
76 changes: 76 additions & 0 deletions tests/samples/0105/tree.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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: [
FunctionLikeParameterDefinition {
comments: CommentGroup {
comments: [],
},
attributes: [],
type_definition: String(
Keyword {
value: "string",
position: 13,
},
),
ellipsis: None,
variable: Variable {
position: 20,
name: "$foo",
},
default: None,
},
],
commas: [],
},
right_parenthesis: 24,
},
return_type: FunctionLikeReturnTypeDefinition {
colon: 25,
type_definition: Identifier(
TemplatedIdentifier {
name: Identifier {
position: 27,
value: "Bar",
},
templates: None,
},
),
},
body: BlockStatement {
comments: CommentGroup {
comments: [],
},
left_brace: 31,
statements: [
Empty(
35,
),
],
right_brace: 37,
},
},
),
],
eof: 39,
}

0 comments on commit 3f56fd4

Please sign in to comment.