From 6f6a3f2ccf9f11977ded6b2a8f43c3a718729a27 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 9 Dec 2020 23:35:08 +0000 Subject: Fix typos for `-insert-semicolon` --- src/parser.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/parser.cpp') diff --git a/src/parser.cpp b/src/parser.cpp index 46af47c2d..6e64ee9a8 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -1284,8 +1284,7 @@ bool skip_possible_newline(AstFile *f) { if ((f->tokenizer.flags & TokenizerFlag_InsertSemicolon) == 0) { return false; } - Token *prev = &f->curr_token; - if (prev->kind == Token_Semicolon && prev->string == "\n") { + if (token_is_newline(f->curr_token)) { advance_token(f); return true; } @@ -1296,10 +1295,10 @@ bool skip_possible_newline_for_literal(AstFile *f) { if ((f->tokenizer.flags & TokenizerFlag_InsertSemicolon) == 0) { return false; } - TokenPos curr_pos = f->curr_token.pos; - if (token_is_newline(f->curr_token)) { + Token curr = f->curr_token; + if (token_is_newline(curr)) { Token next = peek_token(f); - if (curr_pos.line+1 >= next.pos.line) { + if (curr.pos.line+1 >= next.pos.line) { switch (next.kind) { case Token_OpenBrace: case Token_else: @@ -3795,8 +3794,8 @@ Ast *parse_if_stmt(AstFile *f) { syntax_error(body, "The body of a 'do' be on the same line as if condition"); } } else { - body = parse_block_stmt(f, false); skip_possible_newline_for_literal(f); + body = parse_block_stmt(f, false); } if (allow_token(f, Token_else)) { @@ -3805,6 +3804,7 @@ Ast *parse_if_stmt(AstFile *f) { else_stmt = parse_if_stmt(f); break; case Token_OpenBrace: + skip_possible_newline_for_literal(f); else_stmt = parse_block_stmt(f, false); break; case Token_do: { @@ -3851,8 +3851,8 @@ Ast *parse_when_stmt(AstFile *f) { syntax_error(body, "The body of a 'do' be on the same line as when statement"); } } else { - body = parse_block_stmt(f, true); skip_possible_newline_for_literal(f); + body = parse_block_stmt(f, true); } if (allow_token(f, Token_else)) { @@ -3957,8 +3957,8 @@ Ast *parse_for_stmt(AstFile *f) { syntax_error(body, "The body of a 'do' be on the same line as the 'for' token"); } } else { - body = parse_block_stmt(f, false); skip_possible_newline_for_literal(f); + body = parse_block_stmt(f, false); } return ast_range_stmt(f, token, nullptr, nullptr, in_token, rhs, body); } @@ -3993,8 +3993,8 @@ Ast *parse_for_stmt(AstFile *f) { syntax_error(body, "The body of a 'do' be on the same line as the 'for' token"); } } else { - body = parse_block_stmt(f, false); skip_possible_newline_for_literal(f); + body = parse_block_stmt(f, false); } if (is_range) { @@ -4345,8 +4345,8 @@ Ast *parse_stmt(AstFile *f) { syntax_error(body, "The body of a 'do' be on the same line as the 'for' token"); } } else { - body = parse_block_stmt(f, false); skip_possible_newline_for_literal(f); + body = parse_block_stmt(f, false); } if (bad_stmt) { return ast_bad_stmt(f, inline_token, f->curr_token); -- cgit v1.2.3 From f64584b92a54dceb28121461f2ef4fd02c7b30de Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 9 Dec 2020 23:40:45 +0000 Subject: Improve -insert-semicolon rules --- core/odin/parser/parser.odin | 9 +++------ src/parser.cpp | 9 +++------ 2 files changed, 6 insertions(+), 12 deletions(-) (limited to 'src/parser.cpp') diff --git a/core/odin/parser/parser.odin b/core/odin/parser/parser.odin index a23aa6a5b..28ff64533 100644 --- a/core/odin/parser/parser.odin +++ b/core/odin/parser/parser.odin @@ -521,6 +521,7 @@ parse_stmt_list :: proc(p: ^Parser) -> []^ast.Stmt { } parse_block_stmt :: proc(p: ^Parser, is_when: bool) -> ^ast.Stmt { + skip_possible_newline_for_literal(p); if !is_when && p.curr_proc == nil { error(p, p.curr_tok.pos, "you cannot use a block statement in the file scope"); } @@ -545,10 +546,10 @@ parse_when_stmt :: proc(p: ^Parser) -> ^ast.When_Stmt { if allow_token(p, .Do) { body = convert_stmt_to_body(p, parse_stmt(p)); } else { - skip_possible_newline_for_literal(p); body = parse_block_stmt(p, true); } + skip_possible_newline_for_literal(p); if allow_token(p, .Else) { #partial switch p.curr_tok.kind { case .When: @@ -621,18 +622,17 @@ parse_if_stmt :: proc(p: ^Parser) -> ^ast.If_Stmt { if allow_token(p, .Do) { body = convert_stmt_to_body(p, parse_stmt(p)); } else { - skip_possible_newline_for_literal(p); body = parse_block_stmt(p, false); } else_tok := p.curr_tok.pos; + skip_possible_newline_for_literal(p); if allow_token(p, .Else) { #partial switch p.curr_tok.kind { case .If: else_stmt = parse_if_stmt(p); case .Open_Brace: - skip_possible_newline_for_literal(p); else_stmt = parse_block_stmt(p, false); case .Do: expect_token(p, .Do); @@ -687,7 +687,6 @@ parse_for_stmt :: proc(p: ^Parser) -> ^ast.Stmt { if allow_token(p, .Do) { body = convert_stmt_to_body(p, parse_stmt(p)); } else { - skip_possible_newline_for_literal(p); body = parse_body(p); } @@ -722,7 +721,6 @@ parse_for_stmt :: proc(p: ^Parser) -> ^ast.Stmt { if allow_token(p, .Do) { body = convert_stmt_to_body(p, parse_stmt(p)); } else { - skip_possible_newline_for_literal(p); body = parse_body(p); } @@ -1094,7 +1092,6 @@ parse_stmt :: proc(p: ^Parser) -> ^ast.Stmt { if allow_token(p, .Do) { body = convert_stmt_to_body(p, parse_stmt(p)); } else { - skip_possible_newline_for_literal(p); body = parse_block_stmt(p, false); } diff --git a/src/parser.cpp b/src/parser.cpp index 6e64ee9a8..59d02090e 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -3181,6 +3181,7 @@ Ast *parse_simple_stmt(AstFile *f, u32 flags) { Ast *parse_block_stmt(AstFile *f, b32 is_when) { + skip_possible_newline_for_literal(f); if (!is_when && f->curr_proc == nullptr) { syntax_error(f->curr_token, "You cannot use a block statement in the file scope"); return ast_bad_stmt(f, f->curr_token, f->curr_token); @@ -3794,17 +3795,16 @@ Ast *parse_if_stmt(AstFile *f) { syntax_error(body, "The body of a 'do' be on the same line as if condition"); } } else { - skip_possible_newline_for_literal(f); body = parse_block_stmt(f, false); } + skip_possible_newline_for_literal(f); if (allow_token(f, Token_else)) { switch (f->curr_token.kind) { case Token_if: else_stmt = parse_if_stmt(f); break; case Token_OpenBrace: - skip_possible_newline_for_literal(f); else_stmt = parse_block_stmt(f, false); break; case Token_do: { @@ -3851,10 +3851,10 @@ Ast *parse_when_stmt(AstFile *f) { syntax_error(body, "The body of a 'do' be on the same line as when statement"); } } else { - skip_possible_newline_for_literal(f); body = parse_block_stmt(f, true); } + skip_possible_newline_for_literal(f); if (allow_token(f, Token_else)) { switch (f->curr_token.kind) { case Token_when: @@ -3957,7 +3957,6 @@ Ast *parse_for_stmt(AstFile *f) { syntax_error(body, "The body of a 'do' be on the same line as the 'for' token"); } } else { - skip_possible_newline_for_literal(f); body = parse_block_stmt(f, false); } return ast_range_stmt(f, token, nullptr, nullptr, in_token, rhs, body); @@ -3993,7 +3992,6 @@ Ast *parse_for_stmt(AstFile *f) { syntax_error(body, "The body of a 'do' be on the same line as the 'for' token"); } } else { - skip_possible_newline_for_literal(f); body = parse_block_stmt(f, false); } @@ -4345,7 +4343,6 @@ Ast *parse_stmt(AstFile *f) { syntax_error(body, "The body of a 'do' be on the same line as the 'for' token"); } } else { - skip_possible_newline_for_literal(f); body = parse_block_stmt(f, false); } if (bad_stmt) { -- cgit v1.2.3