|
@@ -12,6 +12,10 @@ Parser :: struct {
|
|
|
file: ^ast.File,
|
|
|
tok: tokenizer.Tokenizer,
|
|
|
|
|
|
+ // If optional_semicolons is true, semicolons are completely as statement terminators
|
|
|
+ // different to .Insert_Semicolon in tok.flags
|
|
|
+ optional_semicolons: bool,
|
|
|
+
|
|
|
warn: Warning_Handler,
|
|
|
err: Error_Handler,
|
|
|
|
|
@@ -128,6 +132,10 @@ parse_file :: proc(p: ^Parser, file: ^ast.File) -> bool {
|
|
|
p.line_comment = nil;
|
|
|
}
|
|
|
|
|
|
+ if p.optional_semicolons {
|
|
|
+ p.tok.flags += {.Insert_Semicolon};
|
|
|
+ }
|
|
|
+
|
|
|
p.file = file;
|
|
|
tokenizer.init(&p.tok, file.src, file.fullpath, p.err);
|
|
|
if p.tok.ch <= 0 {
|
|
@@ -400,6 +408,11 @@ is_semicolon_optional_for_node :: proc(p: ^Parser, node: ^ast.Node) -> bool {
|
|
|
if node == nil {
|
|
|
return false;
|
|
|
}
|
|
|
+
|
|
|
+ if p.optional_semicolons {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
switch n in node.derived {
|
|
|
case ast.Empty_Stmt, ast.Block_Stmt:
|
|
|
return true;
|
|
@@ -439,14 +452,34 @@ is_semicolon_optional_for_node :: proc(p: ^Parser, node: ^ast.Node) -> bool {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+expect_semicolon_newline_error :: proc(p: ^Parser, token: tokenizer.Token, s: ^ast.Node) {
|
|
|
+ if !p.optional_semicolons && .Insert_Semicolon in p.tok.flags && token.text == "\n" {
|
|
|
+ #partial switch token.kind {
|
|
|
+ case .Close_Brace:
|
|
|
+ case .Close_Paren:
|
|
|
+ case .Else:
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if is_semicolon_optional_for_node(p, s) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ tok := token;
|
|
|
+ tok.pos.column -= 1;
|
|
|
+ error(p, tok.pos, "expected ';', got newline");
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
expect_semicolon :: proc(p: ^Parser, node: ^ast.Node) -> bool {
|
|
|
if allow_token(p, .Semicolon) {
|
|
|
+ expect_semicolon_newline_error(p, p.prev_tok, node);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
prev := p.prev_tok;
|
|
|
if prev.kind == .Semicolon {
|
|
|
+ expect_semicolon_newline_error(p, p.prev_tok, node);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
@@ -615,7 +648,7 @@ parse_if_stmt :: proc(p: ^Parser) -> ^ast.If_Stmt {
|
|
|
cond = parse_expr(p, false);
|
|
|
} else {
|
|
|
init = parse_simple_stmt(p, nil);
|
|
|
- if allow_token(p, .Semicolon) {
|
|
|
+ if parse_control_statement_semicolon_separator(p) {
|
|
|
cond = parse_expr(p, false);
|
|
|
} else {
|
|
|
cond = convert_stmt_to_expr(p, init, "boolean expression");
|
|
@@ -668,6 +701,18 @@ parse_if_stmt :: proc(p: ^Parser) -> ^ast.If_Stmt {
|
|
|
return if_stmt;
|
|
|
}
|
|
|
|
|
|
+parse_control_statement_semicolon_separator :: proc(p: ^Parser) -> bool {
|
|
|
+ tok := peek_token(p);
|
|
|
+ if tok.kind != .Open_Brace {
|
|
|
+ return allow_token(p, .Semicolon);
|
|
|
+ }
|
|
|
+ if tok.text == ";" {
|
|
|
+ return allow_token(p, .Semicolon);
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
parse_for_stmt :: proc(p: ^Parser) -> ^ast.Stmt {
|
|
|
if p.curr_proc == nil {
|
|
|
error(p, p.curr_tok.pos, "you cannot use a for statement in the file scope");
|
|
@@ -716,7 +761,7 @@ parse_for_stmt :: proc(p: ^Parser) -> ^ast.Stmt {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if !is_range && allow_token(p, .Semicolon) {
|
|
|
+ if !is_range && parse_control_statement_semicolon_separator(p) {
|
|
|
init = cond;
|
|
|
cond = nil;
|
|
|
if p.curr_tok.kind != .Semicolon {
|
|
@@ -820,7 +865,7 @@ parse_switch_stmt :: proc(p: ^Parser) -> ^ast.Stmt {
|
|
|
tag = parse_simple_stmt(p, {Stmt_Allow_Flag.In});
|
|
|
if as, ok := tag.derived.(ast.Assign_Stmt); ok && as.op.kind == .In {
|
|
|
is_type_switch = true;
|
|
|
- } else if allow_token(p, .Semicolon) {
|
|
|
+ } else if parse_control_statement_semicolon_separator(p) {
|
|
|
init = tag;
|
|
|
tag = nil;
|
|
|
if p.curr_tok.kind != .Open_Brace {
|
|
@@ -831,6 +876,7 @@ parse_switch_stmt :: proc(p: ^Parser) -> ^ast.Stmt {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ skip_possible_newline(p);
|
|
|
open := expect_token(p, .Open_Brace);
|
|
|
|
|
|
for p.curr_tok.kind == .Case {
|
|
@@ -958,6 +1004,7 @@ parse_foreign_block :: proc(p: ^Parser, tok: tokenizer.Token) -> ^ast.Foreign_Bl
|
|
|
defer p.in_foreign_block = prev_in_foreign_block;
|
|
|
p.in_foreign_block = true;
|
|
|
|
|
|
+ skip_possible_newline_for_literal(p);
|
|
|
open := expect_token(p, .Open_Brace);
|
|
|
for p.curr_tok.kind != .Close_Brace && p.curr_tok.kind != .EOF {
|
|
|
decl := parse_foreign_block_decl(p);
|
|
@@ -1287,7 +1334,7 @@ token_precedence :: proc(p: ^Parser, kind: tokenizer.Token_Kind) -> int {
|
|
|
#partial switch kind {
|
|
|
case .Question, .If, .When:
|
|
|
return 1;
|
|
|
- case .Ellipsis, .Range_Half:
|
|
|
+ case .Ellipsis, .Range_Half, .Range_Full:
|
|
|
if !p.allow_range {
|
|
|
return 0;
|
|
|
}
|
|
@@ -2233,6 +2280,8 @@ parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr {
|
|
|
}
|
|
|
body: ^ast.Stmt;
|
|
|
|
|
|
+ skip_possible_newline_for_literal(p);
|
|
|
+
|
|
|
if allow_token(p, .Undef) {
|
|
|
body = nil;
|
|
|
if where_token.kind != .Invalid {
|
|
@@ -2405,6 +2454,7 @@ parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr {
|
|
|
p.expr_level = where_prev_level;
|
|
|
}
|
|
|
|
|
|
+ skip_possible_newline_for_literal(p);
|
|
|
expect_token(p, .Open_Brace);
|
|
|
fields, name_count = parse_field_list(p, .Close_Brace, ast.Field_Flags_Struct);
|
|
|
close := expect_token(p, .Close_Brace);
|
|
@@ -2473,6 +2523,7 @@ parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr {
|
|
|
|
|
|
variants: [dynamic]^ast.Expr;
|
|
|
|
|
|
+ skip_possible_newline_for_literal(p);
|
|
|
expect_token_after(p, .Open_Brace, "union");
|
|
|
|
|
|
for p.curr_tok.kind != .Close_Brace && p.curr_tok.kind != .EOF {
|
|
@@ -2503,6 +2554,8 @@ parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr {
|
|
|
if p.curr_tok.kind != .Open_Brace {
|
|
|
base_type = parse_type(p);
|
|
|
}
|
|
|
+
|
|
|
+ skip_possible_newline_for_literal(p);
|
|
|
open := expect_token(p, .Open_Brace);
|
|
|
fields := parse_elem_list(p);
|
|
|
close := expect_token(p, .Close_Brace);
|
|
@@ -2601,6 +2654,7 @@ parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ skip_possible_newline_for_literal(p);
|
|
|
open := expect_token(p, .Open_Brace);
|
|
|
asm_string := parse_expr(p, false);
|
|
|
expect_token(p, .Comma);
|
|
@@ -2811,7 +2865,7 @@ parse_atom_expr :: proc(p: ^Parser, value: ^ast.Expr, lhs: bool) -> (operand: ^a
|
|
|
open := expect_token(p, .Open_Bracket);
|
|
|
|
|
|
#partial switch p.curr_tok.kind {
|
|
|
- case .Colon, .Ellipsis, .Range_Half:
|
|
|
+ case .Colon, .Ellipsis, .Range_Half, .Range_Full:
|
|
|
// NOTE(bill): Do not err yet
|
|
|
break;
|
|
|
case:
|
|
@@ -2819,7 +2873,7 @@ parse_atom_expr :: proc(p: ^Parser, value: ^ast.Expr, lhs: bool) -> (operand: ^a
|
|
|
}
|
|
|
|
|
|
#partial switch p.curr_tok.kind {
|
|
|
- case .Ellipsis, .Range_Half:
|
|
|
+ case .Ellipsis, .Range_Half, .Range_Full:
|
|
|
error(p, p.curr_tok.pos, "expected a colon, not a range");
|
|
|
fallthrough;
|
|
|
case .Colon:
|