| ... | @@ -640,12 +640,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree { | ... | @@ -640,12 +640,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree { |
| 640 | switch (token_ptr.id) { | 640 | switch (token_ptr.id) { |
| 641 | Token.Id.Equal => { | 641 | Token.Id.Equal => { |
| 642 | var_decl.eq_token = token_index; | 642 | var_decl.eq_token = token_index; |
| 643 | stack.append(State { | 643 | stack.append(State { .VarDeclSemiColon = var_decl }) catch unreachable; |
| 644 | .ExpectTokenSave = ExpectTokenSave { | | |
| 645 | .id = Token.Id.Semicolon, | | |
| 646 | .ptr = &var_decl.semicolon_token, | | |
| 647 | }, | | |
| 648 | }) catch unreachable; | | |
| 649 | try stack.append(State { .Expression = OptionalCtx { .RequiredNull = &var_decl.init_node } }); | 644 | try stack.append(State { .Expression = OptionalCtx { .RequiredNull = &var_decl.init_node } }); |
| 650 | continue; | 645 | continue; |
| 651 | }, | 646 | }, |
| ... | @@ -662,6 +657,30 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree { | ... | @@ -662,6 +657,30 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree { |
| 662 | } | 657 | } |
| 663 | }, | 658 | }, |
| 664 | | 659 | |
| | 660 | State.VarDeclSemiColon => |var_decl| { |
| | 661 | const semicolon_token = nextToken(&tok_it, &tree); |
| | 662 | |
| | 663 | if (semicolon_token.ptr.id != Token.Id.Semicolon) { |
| | 664 | *(try tree.errors.addOne()) = Error { |
| | 665 | .ExpectedToken = Error.ExpectedToken { |
| | 666 | .token = semicolon_token.index, |
| | 667 | .expected_id = Token.Id.Semicolon, |
| | 668 | }, |
| | 669 | }; |
| | 670 | return tree; |
| | 671 | } |
| | 672 | |
| | 673 | var_decl.semicolon_token = semicolon_token.index; |
| | 674 | |
| | 675 | if (eatToken(&tok_it, &tree, Token.Id.DocComment)) |doc_comment_token| { |
| | 676 | const loc = tree.tokenLocation(semicolon_token.ptr.end, doc_comment_token); |
| | 677 | if (loc.line == 0) { |
| | 678 | try pushDocComment(arena, doc_comment_token, &var_decl.doc_comments); |
| | 679 | } else { |
| | 680 | putBackToken(&tok_it, &tree); |
| | 681 | } |
| | 682 | } |
| | 683 | }, |
| 665 | | 684 | |
| 666 | State.FnDef => |fn_proto| { | 685 | State.FnDef => |fn_proto| { |
| 667 | const token = nextToken(&tok_it, &tree); | 686 | const token = nextToken(&tok_it, &tree); |
| ... | @@ -2938,6 +2957,7 @@ const State = union(enum) { | ... | @@ -2938,6 +2957,7 @@ const State = union(enum) { |
| 2938 | VarDecl: VarDeclCtx, | 2957 | VarDecl: VarDeclCtx, |
| 2939 | VarDeclAlign: &ast.Node.VarDecl, | 2958 | VarDeclAlign: &ast.Node.VarDecl, |
| 2940 | VarDeclEq: &ast.Node.VarDecl, | 2959 | VarDeclEq: &ast.Node.VarDecl, |
| | 2960 | VarDeclSemiColon: &ast.Node.VarDecl, |
| 2941 | | 2961 | |
| 2942 | FnDef: &ast.Node.FnProto, | 2962 | FnDef: &ast.Node.FnProto, |
| 2943 | FnProto: &ast.Node.FnProto, | 2963 | FnProto: &ast.Node.FnProto, |
| ... | @@ -3042,25 +3062,29 @@ const State = union(enum) { | ... | @@ -3042,25 +3062,29 @@ const State = union(enum) { |
| 3042 | OptionalTokenSave: OptionalTokenSave, | 3062 | OptionalTokenSave: OptionalTokenSave, |
| 3043 | }; | 3063 | }; |
| 3044 | | 3064 | |
| | 3065 | fn pushDocComment(arena: &mem.Allocator, line_comment: TokenIndex, result: &?&ast.Node.DocComment) !void { |
| | 3066 | const node = blk: { |
| | 3067 | if (*result) |comment_node| { |
| | 3068 | break :blk comment_node; |
| | 3069 | } else { |
| | 3070 | const comment_node = try arena.construct(ast.Node.DocComment { |
| | 3071 | .base = ast.Node { |
| | 3072 | .id = ast.Node.Id.DocComment, |
| | 3073 | }, |
| | 3074 | .lines = ast.Node.DocComment.LineList.init(arena), |
| | 3075 | }); |
| | 3076 | *result = comment_node; |
| | 3077 | break :blk comment_node; |
| | 3078 | } |
| | 3079 | }; |
| | 3080 | try node.lines.push(line_comment); |
| | 3081 | } |
| | 3082 | |
| 3045 | fn eatDocComments(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree) !?&ast.Node.DocComment { | 3083 | fn eatDocComments(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree) !?&ast.Node.DocComment { |
| 3046 | var result: ?&ast.Node.DocComment = null; | 3084 | var result: ?&ast.Node.DocComment = null; |
| 3047 | while (true) { | 3085 | while (true) { |
| 3048 | if (eatToken(tok_it, tree, Token.Id.DocComment)) |line_comment| { | 3086 | if (eatToken(tok_it, tree, Token.Id.DocComment)) |line_comment| { |
| 3049 | const node = blk: { | 3087 | try pushDocComment(arena, line_comment, &result); |
| 3050 | if (result) |comment_node| { | | |
| 3051 | break :blk comment_node; | | |
| 3052 | } else { | | |
| 3053 | const comment_node = try arena.construct(ast.Node.DocComment { | | |
| 3054 | .base = ast.Node { | | |
| 3055 | .id = ast.Node.Id.DocComment, | | |
| 3056 | }, | | |
| 3057 | .lines = ast.Node.DocComment.LineList.init(arena), | | |
| 3058 | }); | | |
| 3059 | result = comment_node; | | |
| 3060 | break :blk comment_node; | | |
| 3061 | } | | |
| 3062 | }; | | |
| 3063 | try node.lines.push(line_comment); | | |
| 3064 | continue; | 3088 | continue; |
| 3065 | } | 3089 | } |
| 3066 | break; | 3090 | break; |