Skip to content

Commit

Permalink
Add extra check for #no_bounds_check etc being followed by a newlin…
Browse files Browse the repository at this point in the history
…e or empty statement
  • Loading branch information
gingerBill committed May 1, 2021
1 parent 406d2ab commit ced7700
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4341,6 +4341,16 @@ Ast *parse_unrolled_for_loop(AstFile *f, Token unroll_token) {
return ast_unroll_range_stmt(f, unroll_token, for_token, val0, val1, in_token, expr, body);
}

void parse_check_directive_for_empty_statement(Ast *s, Token const &name) {
if (s != nullptr && s->kind == Ast_EmptyStmt) {
if (s->EmptyStmt.token.string == "\n") {
syntax_error(name, "#%.*s cannot be followed by a newline", LIT(name.string));
} else {
syntax_error(name, "#%.*s cannot be applied to an empty statement ';'", LIT(name.string));
}
}
}

Ast *parse_stmt(AstFile *f) {
Ast *s = nullptr;
Token token = f->curr_token;
Expand Down Expand Up @@ -4447,34 +4457,20 @@ Ast *parse_stmt(AstFile *f) {

if (tag == "bounds_check") {
s = parse_stmt(f);
parse_check_directive_for_empty_statement(s, name);
s->state_flags |= StateFlag_bounds_check;
if ((s->state_flags & StateFlag_no_bounds_check) != 0) {
syntax_error(token, "#bounds_check and #no_bounds_check cannot be applied together");
}
return s;
} else if (tag == "no_bounds_check") {
s = parse_stmt(f);
parse_check_directive_for_empty_statement(s, name);
s->state_flags |= StateFlag_no_bounds_check;
if ((s->state_flags & StateFlag_bounds_check) != 0) {
syntax_error(token, "#bounds_check and #no_bounds_check cannot be applied together");
}
return s;
} else if (tag == "complete") {
s = parse_stmt(f);
switch (s->kind) {
case Ast_SwitchStmt:
s->SwitchStmt.partial = false;
syntax_warning(token, "#complete is now the default and has been replaced with its opposite: #partial");
break;
case Ast_TypeSwitchStmt:
s->TypeSwitchStmt.partial = false;
syntax_warning(token, "#complete is now the default and has been replaced with its opposite: #partial");
break;
default:
syntax_error(token, "#complete can only be applied to a switch statement");
break;
}
return s;
} else if (tag == "partial") {
s = parse_stmt(f);
switch (s->kind) {
Expand All @@ -4484,6 +4480,9 @@ Ast *parse_stmt(AstFile *f) {
case Ast_TypeSwitchStmt:
s->TypeSwitchStmt.partial = true;
break;
case Ast_EmptyStmt:
parse_check_directive_for_empty_statement(s, name);
break;
default:
syntax_error(token, "#partial can only be applied to a switch statement");
break;
Expand Down

0 comments on commit ced7700

Please sign in to comment.