Add -vet-style and -vet-semicolon

This commit is contained in:
gingerBill
2023-08-01 11:03:15 +01:00
parent 4b57aec1c6
commit c35c58b023
4 changed files with 77 additions and 38 deletions

View File

@@ -1,7 +1,21 @@
#include "parser_pos.cpp"
// #undef at the bottom of this file
#define ALLOW_NEWLINE (!build_context.strict_style)
gb_internal u64 ast_file_vet_flags(AstFile *f) {
if (f->vet_flags_set) {
return f->vet_flags;
}
return build_context.vet_flags;
}
gb_internal bool ast_file_vet_style(AstFile *f) {
return (ast_file_vet_flags(f) & VetFlag_Style) != 0;
}
gb_internal bool file_allow_newline(AstFile *f) {
bool is_strict = build_context.strict_style || ast_file_vet_style(f);
return !is_strict;
}
gb_internal Token token_end_of_line(AstFile *f, Token tok) {
u8 const *start = f->tokenizer.start + tok.pos.offset;
@@ -1567,29 +1581,31 @@ gb_internal void assign_removal_flag_to_semicolon(AstFile *f) {
Token *prev_token = &f->tokens[f->prev_token_index];
Token *curr_token = &f->tokens[f->curr_token_index];
GB_ASSERT(prev_token->kind == Token_Semicolon);
if (prev_token->string == ";") {
bool ok = false;
if (curr_token->pos.line > prev_token->pos.line) {
if (prev_token->string != ";") {
return;
}
bool ok = false;
if (curr_token->pos.line > prev_token->pos.line) {
ok = true;
} else if (curr_token->pos.line == prev_token->pos.line) {
switch (curr_token->kind) {
case Token_CloseBrace:
case Token_CloseParen:
case Token_EOF:
ok = true;
} else if (curr_token->pos.line == prev_token->pos.line) {
switch (curr_token->kind) {
case Token_CloseBrace:
case Token_CloseParen:
case Token_EOF:
ok = true;
break;
}
}
if (ok) {
if (build_context.strict_style) {
syntax_error(*prev_token, "Found unneeded semicolon");
} else if (build_context.strict_style_init_only && f->pkg->kind == Package_Init) {
syntax_error(*prev_token, "Found unneeded semicolon");
}
prev_token->flags |= TokenFlag_Remove;
break;
}
}
if (!ok) {
return;
}
if (build_context.strict_style || (ast_file_vet_flags(f) & VetFlag_Semicolon)) {
syntax_error(*prev_token, "Found unneeded semicolon");
} else if (build_context.strict_style_init_only && f->pkg->kind == Package_Init) {
syntax_error(*prev_token, "Found unneeded semicolon");
}
prev_token->flags |= TokenFlag_Remove;
}
gb_internal void expect_semicolon(AstFile *f) {
@@ -2748,7 +2764,7 @@ gb_internal Ast *parse_call_expr(AstFile *f, Ast *operand) {
isize prev_expr_level = f->expr_level;
bool prev_allow_newline = f->allow_newline;
f->expr_level = 0;
f->allow_newline = ALLOW_NEWLINE;
f->allow_newline = file_allow_newline(f);
open_paren = expect_token(f, Token_OpenParen);
@@ -3147,7 +3163,7 @@ gb_internal Ast *parse_expr(AstFile *f, bool lhs) {
gb_internal Array<Ast *> parse_expr_list(AstFile *f, bool lhs) {
bool allow_newline = f->allow_newline;
f->allow_newline = ALLOW_NEWLINE;
f->allow_newline = file_allow_newline(f);
auto list = array_make<Ast *>(heap_allocator());
for (;;) {
@@ -3472,7 +3488,7 @@ gb_internal Ast *parse_results(AstFile *f, bool *diverging) {
Ast *list = nullptr;
expect_token(f, Token_OpenParen);
list = parse_field_list(f, nullptr, FieldFlag_Results, Token_CloseParen, true, false);
if (ALLOW_NEWLINE) {
if (file_allow_newline(f)) {
skip_possible_newline(f);
}
expect_token_after(f, Token_CloseParen, "parameter list");
@@ -3532,7 +3548,7 @@ gb_internal Ast *parse_proc_type(AstFile *f, Token proc_token) {
expect_token(f, Token_OpenParen);
params = parse_field_list(f, nullptr, FieldFlag_Signature, Token_CloseParen, true, true);
if (ALLOW_NEWLINE) {
if (file_allow_newline(f)) {
skip_possible_newline(f);
}
expect_token_after(f, Token_CloseParen, "parameter list");
@@ -3754,7 +3770,7 @@ gb_internal bool allow_field_separator(AstFile *f) {
}
if (token.kind == Token_Semicolon) {
bool ok = false;
if (ALLOW_NEWLINE && token_is_newline(token)) {
if (file_allow_newline(f) && token_is_newline(token)) {
TokenKind next = peek_token(f).kind;
switch (next) {
case Token_CloseBrace:
@@ -3818,7 +3834,7 @@ gb_internal bool check_procedure_name_list(Array<Ast *> const &names) {
gb_internal Ast *parse_field_list(AstFile *f, isize *name_count_, u32 allowed_flags, TokenKind follow, bool allow_default_parameters, bool allow_typeid_token) {
bool prev_allow_newline = f->allow_newline;
defer (f->allow_newline = prev_allow_newline);
f->allow_newline = ALLOW_NEWLINE;
f->allow_newline = file_allow_newline(f);
Token start_token = f->curr_token;
@@ -6005,6 +6021,3 @@ gb_internal ParseFileError parse_packages(Parser *p, String init_filename) {
return ParseFile_None;
}
#undef ALLOW_NEWLINE