| author | |
| committer | |
| log | 873bb29c984b976021fb9ca95ad3298e03a8b3ff |
| tree | 5731533a3903e43279d0435c00ffe726560aec6d |
| parent | 03cdb4fb5853109e46bdc08d8a849a23780093ae |
* std.zig.parse is moved to std.zig.Ast.parse
* the new function has an additional parameter that requires passing
Mode.zig or Mode.zon
* moved parser.zig code to Parse.zig
* added parseZon function next to parseRoot function10 files changed, 3898 insertions(+), 3876 deletions(-)
CMakeLists.txt+1-1| ... | @@ -513,7 +513,7 @@ set(ZIG_STAGE2_SOURCES | ... | @@ -513,7 +513,7 @@ set(ZIG_STAGE2_SOURCES |
| 513 | "${CMAKE_SOURCE_DIR}/lib/std/zig/Ast.zig" | 513 | "${CMAKE_SOURCE_DIR}/lib/std/zig/Ast.zig" |
| 514 | "${CMAKE_SOURCE_DIR}/lib/std/zig/CrossTarget.zig" | 514 | "${CMAKE_SOURCE_DIR}/lib/std/zig/CrossTarget.zig" |
| 515 | "${CMAKE_SOURCE_DIR}/lib/std/zig/c_builtins.zig" | 515 | "${CMAKE_SOURCE_DIR}/lib/std/zig/c_builtins.zig" |
| 516 | "${CMAKE_SOURCE_DIR}/lib/std/zig/parse.zig" | 516 | "${CMAKE_SOURCE_DIR}/lib/std/zig/Parse.zig" |
| 517 | "${CMAKE_SOURCE_DIR}/lib/std/zig/render.zig" | 517 | "${CMAKE_SOURCE_DIR}/lib/std/zig/render.zig" |
| 518 | "${CMAKE_SOURCE_DIR}/lib/std/zig/string_literal.zig" | 518 | "${CMAKE_SOURCE_DIR}/lib/std/zig/string_literal.zig" |
| 519 | "${CMAKE_SOURCE_DIR}/lib/std/zig/system.zig" | 519 | "${CMAKE_SOURCE_DIR}/lib/std/zig/system.zig" |
lib/std/Build/OptionsStep.zig+1-1| ... | @@ -367,5 +367,5 @@ test "OptionsStep" { | ... | @@ -367,5 +367,5 @@ test "OptionsStep" { |
| 367 | \\ | 367 | \\ |
| 368 | , options.contents.items); | 368 | , options.contents.items); |
| 369 | 369 | ||
| 370 | _ = try std.zig.parse(arena.allocator(), try options.contents.toOwnedSliceSentinel(0)); | 370 | _ = try std.zig.Ast.parse(arena.allocator(), try options.contents.toOwnedSliceSentinel(0), .zig); |
| 371 | } | 371 | } |
lib/std/zig.zig-1| ... | @@ -8,7 +8,6 @@ pub const Tokenizer = tokenizer.Tokenizer; | ... | @@ -8,7 +8,6 @@ pub const Tokenizer = tokenizer.Tokenizer; |
| 8 | pub const fmtId = fmt.fmtId; | 8 | pub const fmtId = fmt.fmtId; |
| 9 | pub const fmtEscapes = fmt.fmtEscapes; | 9 | pub const fmtEscapes = fmt.fmtEscapes; |
| 10 | pub const isValidId = fmt.isValidId; | 10 | pub const isValidId = fmt.isValidId; |
| 11 | pub const parse = @import("zig/parse.zig").parse; | ||
| 12 | pub const string_literal = @import("zig/string_literal.zig"); | 11 | pub const string_literal = @import("zig/string_literal.zig"); |
| 13 | pub const number_literal = @import("zig/number_literal.zig"); | 12 | pub const number_literal = @import("zig/number_literal.zig"); |
| 14 | pub const primitives = @import("zig/primitives.zig"); | 13 | pub const primitives = @import("zig/primitives.zig"); |
lib/std/zig/Ast.zig+69-9| ... | @@ -11,13 +11,6 @@ extra_data: []Node.Index, | ... | @@ -11,13 +11,6 @@ extra_data: []Node.Index, |
| 11 | 11 | ||
| 12 | errors: []const Error, | 12 | errors: []const Error, |
| 13 | 13 | ||
| 14 | const std = @import("../std.zig"); | ||
| 15 | const assert = std.debug.assert; | ||
| 16 | const testing = std.testing; | ||
| 17 | const mem = std.mem; | ||
| 18 | const Token = std.zig.Token; | ||
| 19 | const Ast = @This(); | ||
| 20 | |||
| 21 | pub const TokenIndex = u32; | 14 | pub const TokenIndex = u32; |
| 22 | pub const ByteOffset = u32; | 15 | pub const ByteOffset = u32; |
| 23 | 16 | ||
| ... | @@ -34,7 +27,7 @@ pub const Location = struct { | ... | @@ -34,7 +27,7 @@ pub const Location = struct { |
| 34 | line_end: usize, | 27 | line_end: usize, |
| 35 | }; | 28 | }; |
| 36 | 29 | ||
| 37 | pub fn deinit(tree: *Ast, gpa: mem.Allocator) void { | 30 | pub fn deinit(tree: *Ast, gpa: Allocator) void { |
| 38 | tree.tokens.deinit(gpa); | 31 | tree.tokens.deinit(gpa); |
| 39 | tree.nodes.deinit(gpa); | 32 | tree.nodes.deinit(gpa); |
| 40 | gpa.free(tree.extra_data); | 33 | gpa.free(tree.extra_data); |
| ... | @@ -48,11 +41,69 @@ pub const RenderError = error{ | ... | @@ -48,11 +41,69 @@ pub const RenderError = error{ |
| 48 | OutOfMemory, | 41 | OutOfMemory, |
| 49 | }; | 42 | }; |
| 50 | 43 | ||
| 44 | pub const Mode = enum { zig, zon }; | ||
| 45 | |||
| 46 | /// Result should be freed with tree.deinit() when there are | ||
| 47 | /// no more references to any of the tokens or nodes. | ||
| 48 | pub fn parse(gpa: Allocator, source: [:0]const u8, mode: Mode) Allocator.Error!Ast { | ||
| 49 | var tokens = Ast.TokenList{}; | ||
| 50 | defer tokens.deinit(gpa); | ||
| 51 | |||
| 52 | // Empirically, the zig std lib has an 8:1 ratio of source bytes to token count. | ||
| 53 | const estimated_token_count = source.len / 8; | ||
| 54 | try tokens.ensureTotalCapacity(gpa, estimated_token_count); | ||
| 55 | |||
| 56 | var tokenizer = std.zig.Tokenizer.init(source); | ||
| 57 | while (true) { | ||
| 58 | const token = tokenizer.next(); | ||
| 59 | try tokens.append(gpa, .{ | ||
| 60 | .tag = token.tag, | ||
| 61 | .start = @intCast(u32, token.loc.start), | ||
| 62 | }); | ||
| 63 | if (token.tag == .eof) break; | ||
| 64 | } | ||
| 65 | |||
| 66 | var parser: Parse = .{ | ||
| 67 | .source = source, | ||
| 68 | .gpa = gpa, | ||
| 69 | .token_tags = tokens.items(.tag), | ||
| 70 | .token_starts = tokens.items(.start), | ||
| 71 | .errors = .{}, | ||
| 72 | .nodes = .{}, | ||
| 73 | .extra_data = .{}, | ||
| 74 | .scratch = .{}, | ||
| 75 | .tok_i = 0, | ||
| 76 | }; | ||
| 77 | defer parser.errors.deinit(gpa); | ||
| 78 | defer parser.nodes.deinit(gpa); | ||
| 79 | defer parser.extra_data.deinit(gpa); | ||
| 80 | defer parser.scratch.deinit(gpa); | ||
| 81 | |||
| 82 | // Empirically, Zig source code has a 2:1 ratio of tokens to AST nodes. | ||
| 83 | // Make sure at least 1 so we can use appendAssumeCapacity on the root node below. | ||
| 84 | const estimated_node_count = (tokens.len + 2) / 2; | ||
| 85 | try parser.nodes.ensureTotalCapacity(gpa, estimated_node_count); | ||
| 86 | |||
| 87 | switch (mode) { | ||
| 88 | .zig => try parser.parseRoot(), | ||
| 89 | .zon => try parser.parseZon(), | ||
| 90 | } | ||
| 91 | |||
| 92 | // TODO experiment with compacting the MultiArrayList slices here | ||
| 93 | return Ast{ | ||
| 94 | .source = source, | ||
| 95 | .tokens = tokens.toOwnedSlice(), | ||
| 96 | .nodes = parser.nodes.toOwnedSlice(), | ||
| 97 | .extra_data = try parser.extra_data.toOwnedSlice(gpa), | ||
| 98 | .errors = try parser.errors.toOwnedSlice(gpa), | ||
| 99 | }; | ||
| 100 | } | ||
| 101 | |||
| 51 | /// `gpa` is used for allocating the resulting formatted source code, as well as | 102 | /// `gpa` is used for allocating the resulting formatted source code, as well as |
| 52 | /// for allocating extra stack memory if needed, because this function utilizes recursion. | 103 | /// for allocating extra stack memory if needed, because this function utilizes recursion. |
| 53 | /// Note: that's not actually true yet, see https://github.com/ziglang/zig/issues/1006. | 104 | /// Note: that's not actually true yet, see https://github.com/ziglang/zig/issues/1006. |
| 54 | /// Caller owns the returned slice of bytes, allocated with `gpa`. | 105 | /// Caller owns the returned slice of bytes, allocated with `gpa`. |
| 55 | pub fn render(tree: Ast, gpa: mem.Allocator) RenderError![]u8 { | 106 | pub fn render(tree: Ast, gpa: Allocator) RenderError![]u8 { |
| 56 | var buffer = std.ArrayList(u8).init(gpa); | 107 | var buffer = std.ArrayList(u8).init(gpa); |
| 57 | defer buffer.deinit(); | 108 | defer buffer.deinit(); |
| 58 | 109 | ||
| ... | @@ -3347,3 +3398,12 @@ pub const Node = struct { | ... | @@ -3347,3 +3398,12 @@ pub const Node = struct { |
| 3347 | rparen: TokenIndex, | 3398 | rparen: TokenIndex, |
| 3348 | }; | 3399 | }; |
| 3349 | }; | 3400 | }; |
| 3401 | |||
| 3402 | const std = @import("../std.zig"); | ||
| 3403 | const assert = std.debug.assert; | ||
| 3404 | const testing = std.testing; | ||
| 3405 | const mem = std.mem; | ||
| 3406 | const Token = std.zig.Token; | ||
| 3407 | const Ast = @This(); | ||
| 3408 | const Allocator = std.mem.Allocator; | ||
| 3409 | const Parse = @import("Parse.zig"); |
lib/std/zig/Parse.zig created+3816| ... | @@ -0,0 +1,3816 @@ | ||
| 1 | //! Represents in-progress parsing, will be converted to an Ast after completion. | ||
| 2 | |||
| 3 | pub const Error = error{ParseError} || Allocator.Error; | ||
| 4 | |||
| 5 | gpa: Allocator, | ||
| 6 | source: []const u8, | ||
| 7 | token_tags: []const Token.Tag, | ||
| 8 | token_starts: []const Ast.ByteOffset, | ||
| 9 | tok_i: TokenIndex, | ||
| 10 | errors: std.ArrayListUnmanaged(AstError), | ||
| 11 | nodes: Ast.NodeList, | ||
| 12 | extra_data: std.ArrayListUnmanaged(Node.Index), | ||
| 13 | scratch: std.ArrayListUnmanaged(Node.Index), | ||
| 14 | |||
| 15 | const SmallSpan = union(enum) { | ||
| 16 | zero_or_one: Node.Index, | ||
| 17 | multi: Node.SubRange, | ||
| 18 | }; | ||
| 19 | |||
| 20 | const Members = struct { | ||
| 21 | len: usize, | ||
| 22 | lhs: Node.Index, | ||
| 23 | rhs: Node.Index, | ||
| 24 | trailing: bool, | ||
| 25 | |||
| 26 | fn toSpan(self: Members, p: *Parse) !Node.SubRange { | ||
| 27 | if (self.len <= 2) { | ||
| 28 | const nodes = [2]Node.Index{ self.lhs, self.rhs }; | ||
| 29 | return p.listToSpan(nodes[0..self.len]); | ||
| 30 | } else { | ||
| 31 | return Node.SubRange{ .start = self.lhs, .end = self.rhs }; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | }; | ||
| 35 | |||
| 36 | fn listToSpan(p: *Parse, list: []const Node.Index) !Node.SubRange { | ||
| 37 | try p.extra_data.appendSlice(p.gpa, list); | ||
| 38 | return Node.SubRange{ | ||
| 39 | .start = @intCast(Node.Index, p.extra_data.items.len - list.len), | ||
| 40 | .end = @intCast(Node.Index, p.extra_data.items.len), | ||
| 41 | }; | ||
| 42 | } | ||
| 43 | |||
| 44 | fn addNode(p: *Parse, elem: Ast.NodeList.Elem) Allocator.Error!Node.Index { | ||
| 45 | const result = @intCast(Node.Index, p.nodes.len); | ||
| 46 | try p.nodes.append(p.gpa, elem); | ||
| 47 | return result; | ||
| 48 | } | ||
| 49 | |||
| 50 | fn setNode(p: *Parse, i: usize, elem: Ast.NodeList.Elem) Node.Index { | ||
| 51 | p.nodes.set(i, elem); | ||
| 52 | return @intCast(Node.Index, i); | ||
| 53 | } | ||
| 54 | |||
| 55 | fn reserveNode(p: *Parse, tag: Ast.Node.Tag) !usize { | ||
| 56 | try p.nodes.resize(p.gpa, p.nodes.len + 1); | ||
| 57 | p.nodes.items(.tag)[p.nodes.len - 1] = tag; | ||
| 58 | return p.nodes.len - 1; | ||
| 59 | } | ||
| 60 | |||
| 61 | fn unreserveNode(p: *Parse, node_index: usize) void { | ||
| 62 | if (p.nodes.len == node_index) { | ||
| 63 | p.nodes.resize(p.gpa, p.nodes.len - 1) catch unreachable; | ||
| 64 | } else { | ||
| 65 | // There is zombie node left in the tree, let's make it as inoffensive as possible | ||
| 66 | // (sadly there's no no-op node) | ||
| 67 | p.nodes.items(.tag)[node_index] = .unreachable_literal; | ||
| 68 | p.nodes.items(.main_token)[node_index] = p.tok_i; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | fn addExtra(p: *Parse, extra: anytype) Allocator.Error!Node.Index { | ||
| 73 | const fields = std.meta.fields(@TypeOf(extra)); | ||
| 74 | try p.extra_data.ensureUnusedCapacity(p.gpa, fields.len); | ||
| 75 | const result = @intCast(u32, p.extra_data.items.len); | ||
| 76 | inline for (fields) |field| { | ||
| 77 | comptime assert(field.type == Node.Index); | ||
| 78 | p.extra_data.appendAssumeCapacity(@field(extra, field.name)); | ||
| 79 | } | ||
| 80 | return result; | ||
| 81 | } | ||
| 82 | |||
| 83 | fn warnExpected(p: *Parse, expected_token: Token.Tag) error{OutOfMemory}!void { | ||
| 84 | @setCold(true); | ||
| 85 | try p.warnMsg(.{ | ||
| 86 | .tag = .expected_token, | ||
| 87 | .token = p.tok_i, | ||
| 88 | .extra = .{ .expected_tag = expected_token }, | ||
| 89 | }); | ||
| 90 | } | ||
| 91 | |||
| 92 | fn warn(p: *Parse, error_tag: AstError.Tag) error{OutOfMemory}!void { | ||
| 93 | @setCold(true); | ||
| 94 | try p.warnMsg(.{ .tag = error_tag, .token = p.tok_i }); | ||
| 95 | } | ||
| 96 | |||
| 97 | fn warnMsg(p: *Parse, msg: Ast.Error) error{OutOfMemory}!void { | ||
| 98 | @setCold(true); | ||
| 99 | switch (msg.tag) { | ||
| 100 | .expected_semi_after_decl, | ||
| 101 | .expected_semi_after_stmt, | ||
| 102 | .expected_comma_after_field, | ||
| 103 | .expected_comma_after_arg, | ||
| 104 | .expected_comma_after_param, | ||
| 105 | .expected_comma_after_initializer, | ||
| 106 | .expected_comma_after_switch_prong, | ||
| 107 | .expected_semi_or_else, | ||
| 108 | .expected_semi_or_lbrace, | ||
| 109 | .expected_token, | ||
| 110 | .expected_block, | ||
| 111 | .expected_block_or_assignment, | ||
| 112 | .expected_block_or_expr, | ||
| 113 | .expected_block_or_field, | ||
| 114 | .expected_expr, | ||
| 115 | .expected_expr_or_assignment, | ||
| 116 | .expected_fn, | ||
| 117 | .expected_inlinable, | ||
| 118 | .expected_labelable, | ||
| 119 | .expected_param_list, | ||
| 120 | .expected_prefix_expr, | ||
| 121 | .expected_primary_type_expr, | ||
| 122 | .expected_pub_item, | ||
| 123 | .expected_return_type, | ||
| 124 | .expected_suffix_op, | ||
| 125 | .expected_type_expr, | ||
| 126 | .expected_var_decl, | ||
| 127 | .expected_var_decl_or_fn, | ||
| 128 | .expected_loop_payload, | ||
| 129 | .expected_container, | ||
| 130 | => if (msg.token != 0 and !p.tokensOnSameLine(msg.token - 1, msg.token)) { | ||
| 131 | var copy = msg; | ||
| 132 | copy.token_is_prev = true; | ||
| 133 | copy.token -= 1; | ||
| 134 | return p.errors.append(p.gpa, copy); | ||
| 135 | }, | ||
| 136 | else => {}, | ||
| 137 | } | ||
| 138 | try p.errors.append(p.gpa, msg); | ||
| 139 | } | ||
| 140 | |||
| 141 | fn fail(p: *Parse, tag: Ast.Error.Tag) error{ ParseError, OutOfMemory } { | ||
| 142 | @setCold(true); | ||
| 143 | return p.failMsg(.{ .tag = tag, .token = p.tok_i }); | ||
| 144 | } | ||
| 145 | |||
| 146 | fn failExpected(p: *Parse, expected_token: Token.Tag) error{ ParseError, OutOfMemory } { | ||
| 147 | @setCold(true); | ||
| 148 | return p.failMsg(.{ | ||
| 149 | .tag = .expected_token, | ||
| 150 | .token = p.tok_i, | ||
| 151 | .extra = .{ .expected_tag = expected_token }, | ||
| 152 | }); | ||
| 153 | } | ||
| 154 | |||
| 155 | fn failMsg(p: *Parse, msg: Ast.Error) error{ ParseError, OutOfMemory } { | ||
| 156 | @setCold(true); | ||
| 157 | try p.warnMsg(msg); | ||
| 158 | return error.ParseError; | ||
| 159 | } | ||
| 160 | |||
| 161 | /// Root <- skip container_doc_comment? ContainerMembers eof | ||
| 162 | pub fn parseRoot(p: *Parse) !void { | ||
| 163 | // Root node must be index 0. | ||
| 164 | p.nodes.appendAssumeCapacity(.{ | ||
| 165 | .tag = .root, | ||
| 166 | .main_token = 0, | ||
| 167 | .data = undefined, | ||
| 168 | }); | ||
| 169 | const root_members = try p.parseContainerMembers(); | ||
| 170 | const root_decls = try root_members.toSpan(p); | ||
| 171 | if (p.token_tags[p.tok_i] != .eof) { | ||
| 172 | try p.warnExpected(.eof); | ||
| 173 | } | ||
| 174 | p.nodes.items(.data)[0] = .{ | ||
| 175 | .lhs = root_decls.start, | ||
| 176 | .rhs = root_decls.end, | ||
| 177 | }; | ||
| 178 | } | ||
| 179 | |||
| 180 | /// Parse in ZON mode. Subset of the language. | ||
| 181 | /// TODO: set a flag in Parse struct, and honor that flag | ||
| 182 | /// by emitting compilation errors when non-zon nodes are encountered. | ||
| 183 | pub fn parseZon(p: *Parse) !void { | ||
| 184 | const node_index = p.parseExpr() catch |err| switch (err) { | ||
| 185 | error.ParseError => { | ||
| 186 | assert(p.errors.items.len > 0); | ||
| 187 | return; | ||
| 188 | }, | ||
| 189 | else => |e| return e, | ||
| 190 | }; | ||
| 191 | assert(node_index == 0); | ||
| 192 | if (p.token_tags[p.tok_i] != .eof) { | ||
| 193 | try p.warnExpected(.eof); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | /// ContainerMembers <- ContainerDeclarations (ContainerField COMMA)* (ContainerField / ContainerDeclarations) | ||
| 198 | /// | ||
| 199 | /// ContainerDeclarations | ||
| 200 | /// <- TestDecl ContainerDeclarations | ||
| 201 | /// / ComptimeDecl ContainerDeclarations | ||
| 202 | /// / doc_comment? KEYWORD_pub? Decl ContainerDeclarations | ||
| 203 | /// / | ||
| 204 | /// | ||
| 205 | /// ComptimeDecl <- KEYWORD_comptime Block | ||
| 206 | fn parseContainerMembers(p: *Parse) !Members { | ||
| 207 | const scratch_top = p.scratch.items.len; | ||
| 208 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 209 | |||
| 210 | var field_state: union(enum) { | ||
| 211 | /// No fields have been seen. | ||
| 212 | none, | ||
| 213 | /// Currently parsing fields. | ||
| 214 | seen, | ||
| 215 | /// Saw fields and then a declaration after them. | ||
| 216 | /// Payload is first token of previous declaration. | ||
| 217 | end: Node.Index, | ||
| 218 | /// There was a declaration between fields, don't report more errors. | ||
| 219 | err, | ||
| 220 | } = .none; | ||
| 221 | |||
| 222 | var last_field: TokenIndex = undefined; | ||
| 223 | |||
| 224 | // Skip container doc comments. | ||
| 225 | while (p.eatToken(.container_doc_comment)) |_| {} | ||
| 226 | |||
| 227 | var trailing = false; | ||
| 228 | while (true) { | ||
| 229 | const doc_comment = try p.eatDocComments(); | ||
| 230 | |||
| 231 | switch (p.token_tags[p.tok_i]) { | ||
| 232 | .keyword_test => { | ||
| 233 | if (doc_comment) |some| { | ||
| 234 | try p.warnMsg(.{ .tag = .test_doc_comment, .token = some }); | ||
| 235 | } | ||
| 236 | const test_decl_node = try p.expectTestDeclRecoverable(); | ||
| 237 | if (test_decl_node != 0) { | ||
| 238 | if (field_state == .seen) { | ||
| 239 | field_state = .{ .end = test_decl_node }; | ||
| 240 | } | ||
| 241 | try p.scratch.append(p.gpa, test_decl_node); | ||
| 242 | } | ||
| 243 | trailing = false; | ||
| 244 | }, | ||
| 245 | .keyword_comptime => switch (p.token_tags[p.tok_i + 1]) { | ||
| 246 | .l_brace => { | ||
| 247 | if (doc_comment) |some| { | ||
| 248 | try p.warnMsg(.{ .tag = .comptime_doc_comment, .token = some }); | ||
| 249 | } | ||
| 250 | const comptime_token = p.nextToken(); | ||
| 251 | const block = p.parseBlock() catch |err| switch (err) { | ||
| 252 | error.OutOfMemory => return error.OutOfMemory, | ||
| 253 | error.ParseError => blk: { | ||
| 254 | p.findNextContainerMember(); | ||
| 255 | break :blk null_node; | ||
| 256 | }, | ||
| 257 | }; | ||
| 258 | if (block != 0) { | ||
| 259 | const comptime_node = try p.addNode(.{ | ||
| 260 | .tag = .@"comptime", | ||
| 261 | .main_token = comptime_token, | ||
| 262 | .data = .{ | ||
| 263 | .lhs = block, | ||
| 264 | .rhs = undefined, | ||
| 265 | }, | ||
| 266 | }); | ||
| 267 | if (field_state == .seen) { | ||
| 268 | field_state = .{ .end = comptime_node }; | ||
| 269 | } | ||
| 270 | try p.scratch.append(p.gpa, comptime_node); | ||
| 271 | } | ||
| 272 | trailing = false; | ||
| 273 | }, | ||
| 274 | else => { | ||
| 275 | const identifier = p.tok_i; | ||
| 276 | defer last_field = identifier; | ||
| 277 | const container_field = p.expectContainerField() catch |err| switch (err) { | ||
| 278 | error.OutOfMemory => return error.OutOfMemory, | ||
| 279 | error.ParseError => { | ||
| 280 | p.findNextContainerMember(); | ||
| 281 | continue; | ||
| 282 | }, | ||
| 283 | }; | ||
| 284 | switch (field_state) { | ||
| 285 | .none => field_state = .seen, | ||
| 286 | .err, .seen => {}, | ||
| 287 | .end => |node| { | ||
| 288 | try p.warnMsg(.{ | ||
| 289 | .tag = .decl_between_fields, | ||
| 290 | .token = p.nodes.items(.main_token)[node], | ||
| 291 | }); | ||
| 292 | try p.warnMsg(.{ | ||
| 293 | .tag = .previous_field, | ||
| 294 | .is_note = true, | ||
| 295 | .token = last_field, | ||
| 296 | }); | ||
| 297 | try p.warnMsg(.{ | ||
| 298 | .tag = .next_field, | ||
| 299 | .is_note = true, | ||
| 300 | .token = identifier, | ||
| 301 | }); | ||
| 302 | // Continue parsing; error will be reported later. | ||
| 303 | field_state = .err; | ||
| 304 | }, | ||
| 305 | } | ||
| 306 | try p.scratch.append(p.gpa, container_field); | ||
| 307 | switch (p.token_tags[p.tok_i]) { | ||
| 308 | .comma => { | ||
| 309 | p.tok_i += 1; | ||
| 310 | trailing = true; | ||
| 311 | continue; | ||
| 312 | }, | ||
| 313 | .r_brace, .eof => { | ||
| 314 | trailing = false; | ||
| 315 | break; | ||
| 316 | }, | ||
| 317 | else => {}, | ||
| 318 | } | ||
| 319 | // There is not allowed to be a decl after a field with no comma. | ||
| 320 | // Report error but recover parser. | ||
| 321 | try p.warn(.expected_comma_after_field); | ||
| 322 | p.findNextContainerMember(); | ||
| 323 | }, | ||
| 324 | }, | ||
| 325 | .keyword_pub => { | ||
| 326 | p.tok_i += 1; | ||
| 327 | const top_level_decl = try p.expectTopLevelDeclRecoverable(); | ||
| 328 | if (top_level_decl != 0) { | ||
| 329 | if (field_state == .seen) { | ||
| 330 | field_state = .{ .end = top_level_decl }; | ||
| 331 | } | ||
| 332 | try p.scratch.append(p.gpa, top_level_decl); | ||
| 333 | } | ||
| 334 | trailing = p.token_tags[p.tok_i - 1] == .semicolon; | ||
| 335 | }, | ||
| 336 | .keyword_usingnamespace => { | ||
| 337 | const node = try p.expectUsingNamespaceRecoverable(); | ||
| 338 | if (node != 0) { | ||
| 339 | if (field_state == .seen) { | ||
| 340 | field_state = .{ .end = node }; | ||
| 341 | } | ||
| 342 | try p.scratch.append(p.gpa, node); | ||
| 343 | } | ||
| 344 | trailing = p.token_tags[p.tok_i - 1] == .semicolon; | ||
| 345 | }, | ||
| 346 | .keyword_const, | ||
| 347 | .keyword_var, | ||
| 348 | .keyword_threadlocal, | ||
| 349 | .keyword_export, | ||
| 350 | .keyword_extern, | ||
| 351 | .keyword_inline, | ||
| 352 | .keyword_noinline, | ||
| 353 | .keyword_fn, | ||
| 354 | => { | ||
| 355 | const top_level_decl = try p.expectTopLevelDeclRecoverable(); | ||
| 356 | if (top_level_decl != 0) { | ||
| 357 | if (field_state == .seen) { | ||
| 358 | field_state = .{ .end = top_level_decl }; | ||
| 359 | } | ||
| 360 | try p.scratch.append(p.gpa, top_level_decl); | ||
| 361 | } | ||
| 362 | trailing = p.token_tags[p.tok_i - 1] == .semicolon; | ||
| 363 | }, | ||
| 364 | .eof, .r_brace => { | ||
| 365 | if (doc_comment) |tok| { | ||
| 366 | try p.warnMsg(.{ | ||
| 367 | .tag = .unattached_doc_comment, | ||
| 368 | .token = tok, | ||
| 369 | }); | ||
| 370 | } | ||
| 371 | break; | ||
| 372 | }, | ||
| 373 | else => { | ||
| 374 | const c_container = p.parseCStyleContainer() catch |err| switch (err) { | ||
| 375 | error.OutOfMemory => return error.OutOfMemory, | ||
| 376 | error.ParseError => false, | ||
| 377 | }; | ||
| 378 | if (c_container) continue; | ||
| 379 | |||
| 380 | const identifier = p.tok_i; | ||
| 381 | defer last_field = identifier; | ||
| 382 | const container_field = p.expectContainerField() catch |err| switch (err) { | ||
| 383 | error.OutOfMemory => return error.OutOfMemory, | ||
| 384 | error.ParseError => { | ||
| 385 | p.findNextContainerMember(); | ||
| 386 | continue; | ||
| 387 | }, | ||
| 388 | }; | ||
| 389 | switch (field_state) { | ||
| 390 | .none => field_state = .seen, | ||
| 391 | .err, .seen => {}, | ||
| 392 | .end => |node| { | ||
| 393 | try p.warnMsg(.{ | ||
| 394 | .tag = .decl_between_fields, | ||
| 395 | .token = p.nodes.items(.main_token)[node], | ||
| 396 | }); | ||
| 397 | try p.warnMsg(.{ | ||
| 398 | .tag = .previous_field, | ||
| 399 | .is_note = true, | ||
| 400 | .token = last_field, | ||
| 401 | }); | ||
| 402 | try p.warnMsg(.{ | ||
| 403 | .tag = .next_field, | ||
| 404 | .is_note = true, | ||
| 405 | .token = identifier, | ||
| 406 | }); | ||
| 407 | // Continue parsing; error will be reported later. | ||
| 408 | field_state = .err; | ||
| 409 | }, | ||
| 410 | } | ||
| 411 | try p.scratch.append(p.gpa, container_field); | ||
| 412 | switch (p.token_tags[p.tok_i]) { | ||
| 413 | .comma => { | ||
| 414 | p.tok_i += 1; | ||
| 415 | trailing = true; | ||
| 416 | continue; | ||
| 417 | }, | ||
| 418 | .r_brace, .eof => { | ||
| 419 | trailing = false; | ||
| 420 | break; | ||
| 421 | }, | ||
| 422 | else => {}, | ||
| 423 | } | ||
| 424 | // There is not allowed to be a decl after a field with no comma. | ||
| 425 | // Report error but recover parser. | ||
| 426 | try p.warn(.expected_comma_after_field); | ||
| 427 | if (p.token_tags[p.tok_i] == .semicolon and p.token_tags[identifier] == .identifier) { | ||
| 428 | try p.warnMsg(.{ | ||
| 429 | .tag = .var_const_decl, | ||
| 430 | .is_note = true, | ||
| 431 | .token = identifier, | ||
| 432 | }); | ||
| 433 | } | ||
| 434 | p.findNextContainerMember(); | ||
| 435 | continue; | ||
| 436 | }, | ||
| 437 | } | ||
| 438 | } | ||
| 439 | |||
| 440 | const items = p.scratch.items[scratch_top..]; | ||
| 441 | switch (items.len) { | ||
| 442 | 0 => return Members{ | ||
| 443 | .len = 0, | ||
| 444 | .lhs = 0, | ||
| 445 | .rhs = 0, | ||
| 446 | .trailing = trailing, | ||
| 447 | }, | ||
| 448 | 1 => return Members{ | ||
| 449 | .len = 1, | ||
| 450 | .lhs = items[0], | ||
| 451 | .rhs = 0, | ||
| 452 | .trailing = trailing, | ||
| 453 | }, | ||
| 454 | 2 => return Members{ | ||
| 455 | .len = 2, | ||
| 456 | .lhs = items[0], | ||
| 457 | .rhs = items[1], | ||
| 458 | .trailing = trailing, | ||
| 459 | }, | ||
| 460 | else => { | ||
| 461 | const span = try p.listToSpan(items); | ||
| 462 | return Members{ | ||
| 463 | .len = items.len, | ||
| 464 | .lhs = span.start, | ||
| 465 | .rhs = span.end, | ||
| 466 | .trailing = trailing, | ||
| 467 | }; | ||
| 468 | }, | ||
| 469 | } | ||
| 470 | } | ||
| 471 | |||
| 472 | /// Attempts to find next container member by searching for certain tokens | ||
| 473 | fn findNextContainerMember(p: *Parse) void { | ||
| 474 | var level: u32 = 0; | ||
| 475 | while (true) { | ||
| 476 | const tok = p.nextToken(); | ||
| 477 | switch (p.token_tags[tok]) { | ||
| 478 | // Any of these can start a new top level declaration. | ||
| 479 | .keyword_test, | ||
| 480 | .keyword_comptime, | ||
| 481 | .keyword_pub, | ||
| 482 | .keyword_export, | ||
| 483 | .keyword_extern, | ||
| 484 | .keyword_inline, | ||
| 485 | .keyword_noinline, | ||
| 486 | .keyword_usingnamespace, | ||
| 487 | .keyword_threadlocal, | ||
| 488 | .keyword_const, | ||
| 489 | .keyword_var, | ||
| 490 | .keyword_fn, | ||
| 491 | => { | ||
| 492 | if (level == 0) { | ||
| 493 | p.tok_i -= 1; | ||
| 494 | return; | ||
| 495 | } | ||
| 496 | }, | ||
| 497 | .identifier => { | ||
| 498 | if (p.token_tags[tok + 1] == .comma and level == 0) { | ||
| 499 | p.tok_i -= 1; | ||
| 500 | return; | ||
| 501 | } | ||
| 502 | }, | ||
| 503 | .comma, .semicolon => { | ||
| 504 | // this decl was likely meant to end here | ||
| 505 | if (level == 0) { | ||
| 506 | return; | ||
| 507 | } | ||
| 508 | }, | ||
| 509 | .l_paren, .l_bracket, .l_brace => level += 1, | ||
| 510 | .r_paren, .r_bracket => { | ||
| 511 | if (level != 0) level -= 1; | ||
| 512 | }, | ||
| 513 | .r_brace => { | ||
| 514 | if (level == 0) { | ||
| 515 | // end of container, exit | ||
| 516 | p.tok_i -= 1; | ||
| 517 | return; | ||
| 518 | } | ||
| 519 | level -= 1; | ||
| 520 | }, | ||
| 521 | .eof => { | ||
| 522 | p.tok_i -= 1; | ||
| 523 | return; | ||
| 524 | }, | ||
| 525 | else => {}, | ||
| 526 | } | ||
| 527 | } | ||
| 528 | } | ||
| 529 | |||
| 530 | /// Attempts to find the next statement by searching for a semicolon | ||
| 531 | fn findNextStmt(p: *Parse) void { | ||
| 532 | var level: u32 = 0; | ||
| 533 | while (true) { | ||
| 534 | const tok = p.nextToken(); | ||
| 535 | switch (p.token_tags[tok]) { | ||
| 536 | .l_brace => level += 1, | ||
| 537 | .r_brace => { | ||
| 538 | if (level == 0) { | ||
| 539 | p.tok_i -= 1; | ||
| 540 | return; | ||
| 541 | } | ||
| 542 | level -= 1; | ||
| 543 | }, | ||
| 544 | .semicolon => { | ||
| 545 | if (level == 0) { | ||
| 546 | return; | ||
| 547 | } | ||
| 548 | }, | ||
| 549 | .eof => { | ||
| 550 | p.tok_i -= 1; | ||
| 551 | return; | ||
| 552 | }, | ||
| 553 | else => {}, | ||
| 554 | } | ||
| 555 | } | ||
| 556 | } | ||
| 557 | |||
| 558 | /// TestDecl <- KEYWORD_test (STRINGLITERALSINGLE / IDENTIFIER)? Block | ||
| 559 | fn expectTestDecl(p: *Parse) !Node.Index { | ||
| 560 | const test_token = p.assertToken(.keyword_test); | ||
| 561 | const name_token = switch (p.token_tags[p.nextToken()]) { | ||
| 562 | .string_literal, .identifier => p.tok_i - 1, | ||
| 563 | else => blk: { | ||
| 564 | p.tok_i -= 1; | ||
| 565 | break :blk null; | ||
| 566 | }, | ||
| 567 | }; | ||
| 568 | const block_node = try p.parseBlock(); | ||
| 569 | if (block_node == 0) return p.fail(.expected_block); | ||
| 570 | return p.addNode(.{ | ||
| 571 | .tag = .test_decl, | ||
| 572 | .main_token = test_token, | ||
| 573 | .data = .{ | ||
| 574 | .lhs = name_token orelse 0, | ||
| 575 | .rhs = block_node, | ||
| 576 | }, | ||
| 577 | }); | ||
| 578 | } | ||
| 579 | |||
| 580 | fn expectTestDeclRecoverable(p: *Parse) error{OutOfMemory}!Node.Index { | ||
| 581 | return p.expectTestDecl() catch |err| switch (err) { | ||
| 582 | error.OutOfMemory => return error.OutOfMemory, | ||
| 583 | error.ParseError => { | ||
| 584 | p.findNextContainerMember(); | ||
| 585 | return null_node; | ||
| 586 | }, | ||
| 587 | }; | ||
| 588 | } | ||
| 589 | |||
| 590 | /// Decl | ||
| 591 | /// <- (KEYWORD_export / KEYWORD_extern STRINGLITERALSINGLE? / (KEYWORD_inline / KEYWORD_noinline))? FnProto (SEMICOLON / Block) | ||
| 592 | /// / (KEYWORD_export / KEYWORD_extern STRINGLITERALSINGLE?)? KEYWORD_threadlocal? VarDecl | ||
| 593 | /// / KEYWORD_usingnamespace Expr SEMICOLON | ||
| 594 | fn expectTopLevelDecl(p: *Parse) !Node.Index { | ||
| 595 | const extern_export_inline_token = p.nextToken(); | ||
| 596 | var is_extern: bool = false; | ||
| 597 | var expect_fn: bool = false; | ||
| 598 | var expect_var_or_fn: bool = false; | ||
| 599 | switch (p.token_tags[extern_export_inline_token]) { | ||
| 600 | .keyword_extern => { | ||
| 601 | _ = p.eatToken(.string_literal); | ||
| 602 | is_extern = true; | ||
| 603 | expect_var_or_fn = true; | ||
| 604 | }, | ||
| 605 | .keyword_export => expect_var_or_fn = true, | ||
| 606 | .keyword_inline, .keyword_noinline => expect_fn = true, | ||
| 607 | else => p.tok_i -= 1, | ||
| 608 | } | ||
| 609 | const fn_proto = try p.parseFnProto(); | ||
| 610 | if (fn_proto != 0) { | ||
| 611 | switch (p.token_tags[p.tok_i]) { | ||
| 612 | .semicolon => { | ||
| 613 | p.tok_i += 1; | ||
| 614 | return fn_proto; | ||
| 615 | }, | ||
| 616 | .l_brace => { | ||
| 617 | if (is_extern) { | ||
| 618 | try p.warnMsg(.{ .tag = .extern_fn_body, .token = extern_export_inline_token }); | ||
| 619 | return null_node; | ||
| 620 | } | ||
| 621 | const fn_decl_index = try p.reserveNode(.fn_decl); | ||
| 622 | errdefer p.unreserveNode(fn_decl_index); | ||
| 623 | |||
| 624 | const body_block = try p.parseBlock(); | ||
| 625 | assert(body_block != 0); | ||
| 626 | return p.setNode(fn_decl_index, .{ | ||
| 627 | .tag = .fn_decl, | ||
| 628 | .main_token = p.nodes.items(.main_token)[fn_proto], | ||
| 629 | .data = .{ | ||
| 630 | .lhs = fn_proto, | ||
| 631 | .rhs = body_block, | ||
| 632 | }, | ||
| 633 | }); | ||
| 634 | }, | ||
| 635 | else => { | ||
| 636 | // Since parseBlock only return error.ParseError on | ||
| 637 | // a missing '}' we can assume this function was | ||
| 638 | // supposed to end here. | ||
| 639 | try p.warn(.expected_semi_or_lbrace); | ||
| 640 | return null_node; | ||
| 641 | }, | ||
| 642 | } | ||
| 643 | } | ||
| 644 | if (expect_fn) { | ||
| 645 | try p.warn(.expected_fn); | ||
| 646 | return error.ParseError; | ||
| 647 | } | ||
| 648 | |||
| 649 | const thread_local_token = p.eatToken(.keyword_threadlocal); | ||
| 650 | const var_decl = try p.parseVarDecl(); | ||
| 651 | if (var_decl != 0) { | ||
| 652 | try p.expectSemicolon(.expected_semi_after_decl, false); | ||
| 653 | return var_decl; | ||
| 654 | } | ||
| 655 | if (thread_local_token != null) { | ||
| 656 | return p.fail(.expected_var_decl); | ||
| 657 | } | ||
| 658 | if (expect_var_or_fn) { | ||
| 659 | return p.fail(.expected_var_decl_or_fn); | ||
| 660 | } | ||
| 661 | if (p.token_tags[p.tok_i] != .keyword_usingnamespace) { | ||
| 662 | return p.fail(.expected_pub_item); | ||
| 663 | } | ||
| 664 | return p.expectUsingNamespace(); | ||
| 665 | } | ||
| 666 | |||
| 667 | fn expectTopLevelDeclRecoverable(p: *Parse) error{OutOfMemory}!Node.Index { | ||
| 668 | return p.expectTopLevelDecl() catch |err| switch (err) { | ||
| 669 | error.OutOfMemory => return error.OutOfMemory, | ||
| 670 | error.ParseError => { | ||
| 671 | p.findNextContainerMember(); | ||
| 672 | return null_node; | ||
| 673 | }, | ||
| 674 | }; | ||
| 675 | } | ||
| 676 | |||
| 677 | fn expectUsingNamespace(p: *Parse) !Node.Index { | ||
| 678 | const usingnamespace_token = p.assertToken(.keyword_usingnamespace); | ||
| 679 | const expr = try p.expectExpr(); | ||
| 680 | try p.expectSemicolon(.expected_semi_after_decl, false); | ||
| 681 | return p.addNode(.{ | ||
| 682 | .tag = .@"usingnamespace", | ||
| 683 | .main_token = usingnamespace_token, | ||
| 684 | .data = .{ | ||
| 685 | .lhs = expr, | ||
| 686 | .rhs = undefined, | ||
| 687 | }, | ||
| 688 | }); | ||
| 689 | } | ||
| 690 | |||
| 691 | fn expectUsingNamespaceRecoverable(p: *Parse) error{OutOfMemory}!Node.Index { | ||
| 692 | return p.expectUsingNamespace() catch |err| switch (err) { | ||
| 693 | error.OutOfMemory => return error.OutOfMemory, | ||
| 694 | error.ParseError => { | ||
| 695 | p.findNextContainerMember(); | ||
| 696 | return null_node; | ||
| 697 | }, | ||
| 698 | }; | ||
| 699 | } | ||
| 700 | |||
| 701 | /// FnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? AddrSpace? LinkSection? CallConv? EXCLAMATIONMARK? TypeExpr | ||
| 702 | fn parseFnProto(p: *Parse) !Node.Index { | ||
| 703 | const fn_token = p.eatToken(.keyword_fn) orelse return null_node; | ||
| 704 | |||
| 705 | // We want the fn proto node to be before its children in the array. | ||
| 706 | const fn_proto_index = try p.reserveNode(.fn_proto); | ||
| 707 | errdefer p.unreserveNode(fn_proto_index); | ||
| 708 | |||
| 709 | _ = p.eatToken(.identifier); | ||
| 710 | const params = try p.parseParamDeclList(); | ||
| 711 | const align_expr = try p.parseByteAlign(); | ||
| 712 | const addrspace_expr = try p.parseAddrSpace(); | ||
| 713 | const section_expr = try p.parseLinkSection(); | ||
| 714 | const callconv_expr = try p.parseCallconv(); | ||
| 715 | _ = p.eatToken(.bang); | ||
| 716 | |||
| 717 | const return_type_expr = try p.parseTypeExpr(); | ||
| 718 | if (return_type_expr == 0) { | ||
| 719 | // most likely the user forgot to specify the return type. | ||
| 720 | // Mark return type as invalid and try to continue. | ||
| 721 | try p.warn(.expected_return_type); | ||
| 722 | } | ||
| 723 | |||
| 724 | if (align_expr == 0 and section_expr == 0 and callconv_expr == 0 and addrspace_expr == 0) { | ||
| 725 | switch (params) { | ||
| 726 | .zero_or_one => |param| return p.setNode(fn_proto_index, .{ | ||
| 727 | .tag = .fn_proto_simple, | ||
| 728 | .main_token = fn_token, | ||
| 729 | .data = .{ | ||
| 730 | .lhs = param, | ||
| 731 | .rhs = return_type_expr, | ||
| 732 | }, | ||
| 733 | }), | ||
| 734 | .multi => |span| { | ||
| 735 | return p.setNode(fn_proto_index, .{ | ||
| 736 | .tag = .fn_proto_multi, | ||
| 737 | .main_token = fn_token, | ||
| 738 | .data = .{ | ||
| 739 | .lhs = try p.addExtra(Node.SubRange{ | ||
| 740 | .start = span.start, | ||
| 741 | .end = span.end, | ||
| 742 | }), | ||
| 743 | .rhs = return_type_expr, | ||
| 744 | }, | ||
| 745 | }); | ||
| 746 | }, | ||
| 747 | } | ||
| 748 | } | ||
| 749 | switch (params) { | ||
| 750 | .zero_or_one => |param| return p.setNode(fn_proto_index, .{ | ||
| 751 | .tag = .fn_proto_one, | ||
| 752 | .main_token = fn_token, | ||
| 753 | .data = .{ | ||
| 754 | .lhs = try p.addExtra(Node.FnProtoOne{ | ||
| 755 | .param = param, | ||
| 756 | .align_expr = align_expr, | ||
| 757 | .addrspace_expr = addrspace_expr, | ||
| 758 | .section_expr = section_expr, | ||
| 759 | .callconv_expr = callconv_expr, | ||
| 760 | }), | ||
| 761 | .rhs = return_type_expr, | ||
| 762 | }, | ||
| 763 | }), | ||
| 764 | .multi => |span| { | ||
| 765 | return p.setNode(fn_proto_index, .{ | ||
| 766 | .tag = .fn_proto, | ||
| 767 | .main_token = fn_token, | ||
| 768 | .data = .{ | ||
| 769 | .lhs = try p.addExtra(Node.FnProto{ | ||
| 770 | .params_start = span.start, | ||
| 771 | .params_end = span.end, | ||
| 772 | .align_expr = align_expr, | ||
| 773 | .addrspace_expr = addrspace_expr, | ||
| 774 | .section_expr = section_expr, | ||
| 775 | .callconv_expr = callconv_expr, | ||
| 776 | }), | ||
| 777 | .rhs = return_type_expr, | ||
| 778 | }, | ||
| 779 | }); | ||
| 780 | }, | ||
| 781 | } | ||
| 782 | } | ||
| 783 | |||
| 784 | /// VarDecl <- (KEYWORD_const / KEYWORD_var) IDENTIFIER (COLON TypeExpr)? ByteAlign? AddrSpace? LinkSection? (EQUAL Expr)? SEMICOLON | ||
| 785 | fn parseVarDecl(p: *Parse) !Node.Index { | ||
| 786 | const mut_token = p.eatToken(.keyword_const) orelse | ||
| 787 | p.eatToken(.keyword_var) orelse | ||
| 788 | return null_node; | ||
| 789 | |||
| 790 | _ = try p.expectToken(.identifier); | ||
| 791 | const type_node: Node.Index = if (p.eatToken(.colon) == null) 0 else try p.expectTypeExpr(); | ||
| 792 | const align_node = try p.parseByteAlign(); | ||
| 793 | const addrspace_node = try p.parseAddrSpace(); | ||
| 794 | const section_node = try p.parseLinkSection(); | ||
| 795 | const init_node: Node.Index = switch (p.token_tags[p.tok_i]) { | ||
| 796 | .equal_equal => blk: { | ||
| 797 | try p.warn(.wrong_equal_var_decl); | ||
| 798 | p.tok_i += 1; | ||
| 799 | break :blk try p.expectExpr(); | ||
| 800 | }, | ||
| 801 | .equal => blk: { | ||
| 802 | p.tok_i += 1; | ||
| 803 | break :blk try p.expectExpr(); | ||
| 804 | }, | ||
| 805 | else => 0, | ||
| 806 | }; | ||
| 807 | if (section_node == 0 and addrspace_node == 0) { | ||
| 808 | if (align_node == 0) { | ||
| 809 | return p.addNode(.{ | ||
| 810 | .tag = .simple_var_decl, | ||
| 811 | .main_token = mut_token, | ||
| 812 | .data = .{ | ||
| 813 | .lhs = type_node, | ||
| 814 | .rhs = init_node, | ||
| 815 | }, | ||
| 816 | }); | ||
| 817 | } else if (type_node == 0) { | ||
| 818 | return p.addNode(.{ | ||
| 819 | .tag = .aligned_var_decl, | ||
| 820 | .main_token = mut_token, | ||
| 821 | .data = .{ | ||
| 822 | .lhs = align_node, | ||
| 823 | .rhs = init_node, | ||
| 824 | }, | ||
| 825 | }); | ||
| 826 | } else { | ||
| 827 | return p.addNode(.{ | ||
| 828 | .tag = .local_var_decl, | ||
| 829 | .main_token = mut_token, | ||
| 830 | .data = .{ | ||
| 831 | .lhs = try p.addExtra(Node.LocalVarDecl{ | ||
| 832 | .type_node = type_node, | ||
| 833 | .align_node = align_node, | ||
| 834 | }), | ||
| 835 | .rhs = init_node, | ||
| 836 | }, | ||
| 837 | }); | ||
| 838 | } | ||
| 839 | } else { | ||
| 840 | return p.addNode(.{ | ||
| 841 | .tag = .global_var_decl, | ||
| 842 | .main_token = mut_token, | ||
| 843 | .data = .{ | ||
| 844 | .lhs = try p.addExtra(Node.GlobalVarDecl{ | ||
| 845 | .type_node = type_node, | ||
| 846 | .align_node = align_node, | ||
| 847 | .addrspace_node = addrspace_node, | ||
| 848 | .section_node = section_node, | ||
| 849 | }), | ||
| 850 | .rhs = init_node, | ||
| 851 | }, | ||
| 852 | }); | ||
| 853 | } | ||
| 854 | } | ||
| 855 | |||
| 856 | /// ContainerField | ||
| 857 | /// <- doc_comment? KEYWORD_comptime? IDENTIFIER (COLON TypeExpr)? ByteAlign? (EQUAL Expr)? | ||
| 858 | /// / doc_comment? KEYWORD_comptime? (IDENTIFIER COLON)? !KEYWORD_fn TypeExpr ByteAlign? (EQUAL Expr)? | ||
| 859 | fn expectContainerField(p: *Parse) !Node.Index { | ||
| 860 | var main_token = p.tok_i; | ||
| 861 | _ = p.eatToken(.keyword_comptime); | ||
| 862 | const tuple_like = p.token_tags[p.tok_i] != .identifier or p.token_tags[p.tok_i + 1] != .colon; | ||
| 863 | if (!tuple_like) { | ||
| 864 | main_token = p.assertToken(.identifier); | ||
| 865 | } | ||
| 866 | |||
| 867 | var align_expr: Node.Index = 0; | ||
| 868 | var type_expr: Node.Index = 0; | ||
| 869 | if (p.eatToken(.colon) != null or tuple_like) { | ||
| 870 | type_expr = try p.expectTypeExpr(); | ||
| 871 | align_expr = try p.parseByteAlign(); | ||
| 872 | } | ||
| 873 | |||
| 874 | const value_expr: Node.Index = if (p.eatToken(.equal) == null) 0 else try p.expectExpr(); | ||
| 875 | |||
| 876 | if (align_expr == 0) { | ||
| 877 | return p.addNode(.{ | ||
| 878 | .tag = .container_field_init, | ||
| 879 | .main_token = main_token, | ||
| 880 | .data = .{ | ||
| 881 | .lhs = type_expr, | ||
| 882 | .rhs = value_expr, | ||
| 883 | }, | ||
| 884 | }); | ||
| 885 | } else if (value_expr == 0) { | ||
| 886 | return p.addNode(.{ | ||
| 887 | .tag = .container_field_align, | ||
| 888 | .main_token = main_token, | ||
| 889 | .data = .{ | ||
| 890 | .lhs = type_expr, | ||
| 891 | .rhs = align_expr, | ||
| 892 | }, | ||
| 893 | }); | ||
| 894 | } else { | ||
| 895 | return p.addNode(.{ | ||
| 896 | .tag = .container_field, | ||
| 897 | .main_token = main_token, | ||
| 898 | .data = .{ | ||
| 899 | .lhs = type_expr, | ||
| 900 | .rhs = try p.addExtra(Node.ContainerField{ | ||
| 901 | .value_expr = value_expr, | ||
| 902 | .align_expr = align_expr, | ||
| 903 | }), | ||
| 904 | }, | ||
| 905 | }); | ||
| 906 | } | ||
| 907 | } | ||
| 908 | |||
| 909 | /// Statement | ||
| 910 | /// <- KEYWORD_comptime? VarDecl | ||
| 911 | /// / KEYWORD_comptime BlockExprStatement | ||
| 912 | /// / KEYWORD_nosuspend BlockExprStatement | ||
| 913 | /// / KEYWORD_suspend BlockExprStatement | ||
| 914 | /// / KEYWORD_defer BlockExprStatement | ||
| 915 | /// / KEYWORD_errdefer Payload? BlockExprStatement | ||
| 916 | /// / IfStatement | ||
| 917 | /// / LabeledStatement | ||
| 918 | /// / SwitchExpr | ||
| 919 | /// / AssignExpr SEMICOLON | ||
| 920 | fn parseStatement(p: *Parse, allow_defer_var: bool) Error!Node.Index { | ||
| 921 | const comptime_token = p.eatToken(.keyword_comptime); | ||
| 922 | |||
| 923 | if (allow_defer_var) { | ||
| 924 | const var_decl = try p.parseVarDecl(); | ||
| 925 | if (var_decl != 0) { | ||
| 926 | try p.expectSemicolon(.expected_semi_after_decl, true); | ||
| 927 | return var_decl; | ||
| 928 | } | ||
| 929 | } | ||
| 930 | |||
| 931 | if (comptime_token) |token| { | ||
| 932 | return p.addNode(.{ | ||
| 933 | .tag = .@"comptime", | ||
| 934 | .main_token = token, | ||
| 935 | .data = .{ | ||
| 936 | .lhs = try p.expectBlockExprStatement(), | ||
| 937 | .rhs = undefined, | ||
| 938 | }, | ||
| 939 | }); | ||
| 940 | } | ||
| 941 | |||
| 942 | switch (p.token_tags[p.tok_i]) { | ||
| 943 | .keyword_nosuspend => { | ||
| 944 | return p.addNode(.{ | ||
| 945 | .tag = .@"nosuspend", | ||
| 946 | .main_token = p.nextToken(), | ||
| 947 | .data = .{ | ||
| 948 | .lhs = try p.expectBlockExprStatement(), | ||
| 949 | .rhs = undefined, | ||
| 950 | }, | ||
| 951 | }); | ||
| 952 | }, | ||
| 953 | .keyword_suspend => { | ||
| 954 | const token = p.nextToken(); | ||
| 955 | const block_expr = try p.expectBlockExprStatement(); | ||
| 956 | return p.addNode(.{ | ||
| 957 | .tag = .@"suspend", | ||
| 958 | .main_token = token, | ||
| 959 | .data = .{ | ||
| 960 | .lhs = block_expr, | ||
| 961 | .rhs = undefined, | ||
| 962 | }, | ||
| 963 | }); | ||
| 964 | }, | ||
| 965 | .keyword_defer => if (allow_defer_var) return p.addNode(.{ | ||
| 966 | .tag = .@"defer", | ||
| 967 | .main_token = p.nextToken(), | ||
| 968 | .data = .{ | ||
| 969 | .lhs = undefined, | ||
| 970 | .rhs = try p.expectBlockExprStatement(), | ||
| 971 | }, | ||
| 972 | }), | ||
| 973 | .keyword_errdefer => if (allow_defer_var) return p.addNode(.{ | ||
| 974 | .tag = .@"errdefer", | ||
| 975 | .main_token = p.nextToken(), | ||
| 976 | .data = .{ | ||
| 977 | .lhs = try p.parsePayload(), | ||
| 978 | .rhs = try p.expectBlockExprStatement(), | ||
| 979 | }, | ||
| 980 | }), | ||
| 981 | .keyword_switch => return p.expectSwitchExpr(), | ||
| 982 | .keyword_if => return p.expectIfStatement(), | ||
| 983 | .keyword_enum, .keyword_struct, .keyword_union => { | ||
| 984 | const identifier = p.tok_i + 1; | ||
| 985 | if (try p.parseCStyleContainer()) { | ||
| 986 | // Return something so that `expectStatement` is happy. | ||
| 987 | return p.addNode(.{ | ||
| 988 | .tag = .identifier, | ||
| 989 | .main_token = identifier, | ||
| 990 | .data = .{ | ||
| 991 | .lhs = undefined, | ||
| 992 | .rhs = undefined, | ||
| 993 | }, | ||
| 994 | }); | ||
| 995 | } | ||
| 996 | }, | ||
| 997 | else => {}, | ||
| 998 | } | ||
| 999 | |||
| 1000 | const labeled_statement = try p.parseLabeledStatement(); | ||
| 1001 | if (labeled_statement != 0) return labeled_statement; | ||
| 1002 | |||
| 1003 | const assign_expr = try p.parseAssignExpr(); | ||
| 1004 | if (assign_expr != 0) { | ||
| 1005 | try p.expectSemicolon(.expected_semi_after_stmt, true); | ||
| 1006 | return assign_expr; | ||
| 1007 | } | ||
| 1008 | |||
| 1009 | return null_node; | ||
| 1010 | } | ||
| 1011 | |||
| 1012 | fn expectStatement(p: *Parse, allow_defer_var: bool) !Node.Index { | ||
| 1013 | const statement = try p.parseStatement(allow_defer_var); | ||
| 1014 | if (statement == 0) { | ||
| 1015 | return p.fail(.expected_statement); | ||
| 1016 | } | ||
| 1017 | return statement; | ||
| 1018 | } | ||
| 1019 | |||
| 1020 | /// If a parse error occurs, reports an error, but then finds the next statement | ||
| 1021 | /// and returns that one instead. If a parse error occurs but there is no following | ||
| 1022 | /// statement, returns 0. | ||
| 1023 | fn expectStatementRecoverable(p: *Parse) Error!Node.Index { | ||
| 1024 | while (true) { | ||
| 1025 | return p.expectStatement(true) catch |err| switch (err) { | ||
| 1026 | error.OutOfMemory => return error.OutOfMemory, | ||
| 1027 | error.ParseError => { | ||
| 1028 | p.findNextStmt(); // Try to skip to the next statement. | ||
| 1029 | switch (p.token_tags[p.tok_i]) { | ||
| 1030 | .r_brace => return null_node, | ||
| 1031 | .eof => return error.ParseError, | ||
| 1032 | else => continue, | ||
| 1033 | } | ||
| 1034 | }, | ||
| 1035 | }; | ||
| 1036 | } | ||
| 1037 | } | ||
| 1038 | |||
| 1039 | /// IfStatement | ||
| 1040 | /// <- IfPrefix BlockExpr ( KEYWORD_else Payload? Statement )? | ||
| 1041 | /// / IfPrefix AssignExpr ( SEMICOLON / KEYWORD_else Payload? Statement ) | ||
| 1042 | fn expectIfStatement(p: *Parse) !Node.Index { | ||
| 1043 | const if_token = p.assertToken(.keyword_if); | ||
| 1044 | _ = try p.expectToken(.l_paren); | ||
| 1045 | const condition = try p.expectExpr(); | ||
| 1046 | _ = try p.expectToken(.r_paren); | ||
| 1047 | _ = try p.parsePtrPayload(); | ||
| 1048 | |||
| 1049 | // TODO propose to change the syntax so that semicolons are always required | ||
| 1050 | // inside if statements, even if there is an `else`. | ||
| 1051 | var else_required = false; | ||
| 1052 | const then_expr = blk: { | ||
| 1053 | const block_expr = try p.parseBlockExpr(); | ||
| 1054 | if (block_expr != 0) break :blk block_expr; | ||
| 1055 | const assign_expr = try p.parseAssignExpr(); | ||
| 1056 | if (assign_expr == 0) { | ||
| 1057 | return p.fail(.expected_block_or_assignment); | ||
| 1058 | } | ||
| 1059 | if (p.eatToken(.semicolon)) |_| { | ||
| 1060 | return p.addNode(.{ | ||
| 1061 | .tag = .if_simple, | ||
| 1062 | .main_token = if_token, | ||
| 1063 | .data = .{ | ||
| 1064 | .lhs = condition, | ||
| 1065 | .rhs = assign_expr, | ||
| 1066 | }, | ||
| 1067 | }); | ||
| 1068 | } | ||
| 1069 | else_required = true; | ||
| 1070 | break :blk assign_expr; | ||
| 1071 | }; | ||
| 1072 | _ = p.eatToken(.keyword_else) orelse { | ||
| 1073 | if (else_required) { | ||
| 1074 | try p.warn(.expected_semi_or_else); | ||
| 1075 | } | ||
| 1076 | return p.addNode(.{ | ||
| 1077 | .tag = .if_simple, | ||
| 1078 | .main_token = if_token, | ||
| 1079 | .data = .{ | ||
| 1080 | .lhs = condition, | ||
| 1081 | .rhs = then_expr, | ||
| 1082 | }, | ||
| 1083 | }); | ||
| 1084 | }; | ||
| 1085 | _ = try p.parsePayload(); | ||
| 1086 | const else_expr = try p.expectStatement(false); | ||
| 1087 | return p.addNode(.{ | ||
| 1088 | .tag = .@"if", | ||
| 1089 | .main_token = if_token, | ||
| 1090 | .data = .{ | ||
| 1091 | .lhs = condition, | ||
| 1092 | .rhs = try p.addExtra(Node.If{ | ||
| 1093 | .then_expr = then_expr, | ||
| 1094 | .else_expr = else_expr, | ||
| 1095 | }), | ||
| 1096 | }, | ||
| 1097 | }); | ||
| 1098 | } | ||
| 1099 | |||
| 1100 | /// LabeledStatement <- BlockLabel? (Block / LoopStatement) | ||
| 1101 | fn parseLabeledStatement(p: *Parse) !Node.Index { | ||
| 1102 | const label_token = p.parseBlockLabel(); | ||
| 1103 | const block = try p.parseBlock(); | ||
| 1104 | if (block != 0) return block; | ||
| 1105 | |||
| 1106 | const loop_stmt = try p.parseLoopStatement(); | ||
| 1107 | if (loop_stmt != 0) return loop_stmt; | ||
| 1108 | |||
| 1109 | if (label_token != 0) { | ||
| 1110 | const after_colon = p.tok_i; | ||
| 1111 | const node = try p.parseTypeExpr(); | ||
| 1112 | if (node != 0) { | ||
| 1113 | const a = try p.parseByteAlign(); | ||
| 1114 | const b = try p.parseAddrSpace(); | ||
| 1115 | const c = try p.parseLinkSection(); | ||
| 1116 | const d = if (p.eatToken(.equal) == null) 0 else try p.expectExpr(); | ||
| 1117 | if (a != 0 or b != 0 or c != 0 or d != 0) { | ||
| 1118 | return p.failMsg(.{ .tag = .expected_var_const, .token = label_token }); | ||
| 1119 | } | ||
| 1120 | } | ||
| 1121 | return p.failMsg(.{ .tag = .expected_labelable, .token = after_colon }); | ||
| 1122 | } | ||
| 1123 | |||
| 1124 | return null_node; | ||
| 1125 | } | ||
| 1126 | |||
| 1127 | /// LoopStatement <- KEYWORD_inline? (ForStatement / WhileStatement) | ||
| 1128 | fn parseLoopStatement(p: *Parse) !Node.Index { | ||
| 1129 | const inline_token = p.eatToken(.keyword_inline); | ||
| 1130 | |||
| 1131 | const for_statement = try p.parseForStatement(); | ||
| 1132 | if (for_statement != 0) return for_statement; | ||
| 1133 | |||
| 1134 | const while_statement = try p.parseWhileStatement(); | ||
| 1135 | if (while_statement != 0) return while_statement; | ||
| 1136 | |||
| 1137 | if (inline_token == null) return null_node; | ||
| 1138 | |||
| 1139 | // If we've seen "inline", there should have been a "for" or "while" | ||
| 1140 | return p.fail(.expected_inlinable); | ||
| 1141 | } | ||
| 1142 | |||
| 1143 | /// ForPrefix <- KEYWORD_for LPAREN Expr RPAREN PtrIndexPayload | ||
| 1144 | /// | ||
| 1145 | /// ForStatement | ||
| 1146 | /// <- ForPrefix BlockExpr ( KEYWORD_else Statement )? | ||
| 1147 | /// / ForPrefix AssignExpr ( SEMICOLON / KEYWORD_else Statement ) | ||
| 1148 | fn parseForStatement(p: *Parse) !Node.Index { | ||
| 1149 | const for_token = p.eatToken(.keyword_for) orelse return null_node; | ||
| 1150 | _ = try p.expectToken(.l_paren); | ||
| 1151 | const array_expr = try p.expectExpr(); | ||
| 1152 | _ = try p.expectToken(.r_paren); | ||
| 1153 | const found_payload = try p.parsePtrIndexPayload(); | ||
| 1154 | if (found_payload == 0) try p.warn(.expected_loop_payload); | ||
| 1155 | |||
| 1156 | // TODO propose to change the syntax so that semicolons are always required | ||
| 1157 | // inside while statements, even if there is an `else`. | ||
| 1158 | var else_required = false; | ||
| 1159 | const then_expr = blk: { | ||
| 1160 | const block_expr = try p.parseBlockExpr(); | ||
| 1161 | if (block_expr != 0) break :blk block_expr; | ||
| 1162 | const assign_expr = try p.parseAssignExpr(); | ||
| 1163 | if (assign_expr == 0) { | ||
| 1164 | return p.fail(.expected_block_or_assignment); | ||
| 1165 | } | ||
| 1166 | if (p.eatToken(.semicolon)) |_| { | ||
| 1167 | return p.addNode(.{ | ||
| 1168 | .tag = .for_simple, | ||
| 1169 | .main_token = for_token, | ||
| 1170 | .data = .{ | ||
| 1171 | .lhs = array_expr, | ||
| 1172 | .rhs = assign_expr, | ||
| 1173 | }, | ||
| 1174 | }); | ||
| 1175 | } | ||
| 1176 | else_required = true; | ||
| 1177 | break :blk assign_expr; | ||
| 1178 | }; | ||
| 1179 | _ = p.eatToken(.keyword_else) orelse { | ||
| 1180 | if (else_required) { | ||
| 1181 | try p.warn(.expected_semi_or_else); | ||
| 1182 | } | ||
| 1183 | return p.addNode(.{ | ||
| 1184 | .tag = .for_simple, | ||
| 1185 | .main_token = for_token, | ||
| 1186 | .data = .{ | ||
| 1187 | .lhs = array_expr, | ||
| 1188 | .rhs = then_expr, | ||
| 1189 | }, | ||
| 1190 | }); | ||
| 1191 | }; | ||
| 1192 | return p.addNode(.{ | ||
| 1193 | .tag = .@"for", | ||
| 1194 | .main_token = for_token, | ||
| 1195 | .data = .{ | ||
| 1196 | .lhs = array_expr, | ||
| 1197 | .rhs = try p.addExtra(Node.If{ | ||
| 1198 | .then_expr = then_expr, | ||
| 1199 | .else_expr = try p.expectStatement(false), | ||
| 1200 | }), | ||
| 1201 | }, | ||
| 1202 | }); | ||
| 1203 | } | ||
| 1204 | |||
| 1205 | /// WhilePrefix <- KEYWORD_while LPAREN Expr RPAREN PtrPayload? WhileContinueExpr? | ||
| 1206 | /// | ||
| 1207 | /// WhileStatement | ||
| 1208 | /// <- WhilePrefix BlockExpr ( KEYWORD_else Payload? Statement )? | ||
| 1209 | /// / WhilePrefix AssignExpr ( SEMICOLON / KEYWORD_else Payload? Statement ) | ||
| 1210 | fn parseWhileStatement(p: *Parse) !Node.Index { | ||
| 1211 | const while_token = p.eatToken(.keyword_while) orelse return null_node; | ||
| 1212 | _ = try p.expectToken(.l_paren); | ||
| 1213 | const condition = try p.expectExpr(); | ||
| 1214 | _ = try p.expectToken(.r_paren); | ||
| 1215 | _ = try p.parsePtrPayload(); | ||
| 1216 | const cont_expr = try p.parseWhileContinueExpr(); | ||
| 1217 | |||
| 1218 | // TODO propose to change the syntax so that semicolons are always required | ||
| 1219 | // inside while statements, even if there is an `else`. | ||
| 1220 | var else_required = false; | ||
| 1221 | const then_expr = blk: { | ||
| 1222 | const block_expr = try p.parseBlockExpr(); | ||
| 1223 | if (block_expr != 0) break :blk block_expr; | ||
| 1224 | const assign_expr = try p.parseAssignExpr(); | ||
| 1225 | if (assign_expr == 0) { | ||
| 1226 | return p.fail(.expected_block_or_assignment); | ||
| 1227 | } | ||
| 1228 | if (p.eatToken(.semicolon)) |_| { | ||
| 1229 | if (cont_expr == 0) { | ||
| 1230 | return p.addNode(.{ | ||
| 1231 | .tag = .while_simple, | ||
| 1232 | .main_token = while_token, | ||
| 1233 | .data = .{ | ||
| 1234 | .lhs = condition, | ||
| 1235 | .rhs = assign_expr, | ||
| 1236 | }, | ||
| 1237 | }); | ||
| 1238 | } else { | ||
| 1239 | return p.addNode(.{ | ||
| 1240 | .tag = .while_cont, | ||
| 1241 | .main_token = while_token, | ||
| 1242 | .data = .{ | ||
| 1243 | .lhs = condition, | ||
| 1244 | .rhs = try p.addExtra(Node.WhileCont{ | ||
| 1245 | .cont_expr = cont_expr, | ||
| 1246 | .then_expr = assign_expr, | ||
| 1247 | }), | ||
| 1248 | }, | ||
| 1249 | }); | ||
| 1250 | } | ||
| 1251 | } | ||
| 1252 | else_required = true; | ||
| 1253 | break :blk assign_expr; | ||
| 1254 | }; | ||
| 1255 | _ = p.eatToken(.keyword_else) orelse { | ||
| 1256 | if (else_required) { | ||
| 1257 | try p.warn(.expected_semi_or_else); | ||
| 1258 | } | ||
| 1259 | if (cont_expr == 0) { | ||
| 1260 | return p.addNode(.{ | ||
| 1261 | .tag = .while_simple, | ||
| 1262 | .main_token = while_token, | ||
| 1263 | .data = .{ | ||
| 1264 | .lhs = condition, | ||
| 1265 | .rhs = then_expr, | ||
| 1266 | }, | ||
| 1267 | }); | ||
| 1268 | } else { | ||
| 1269 | return p.addNode(.{ | ||
| 1270 | .tag = .while_cont, | ||
| 1271 | .main_token = while_token, | ||
| 1272 | .data = .{ | ||
| 1273 | .lhs = condition, | ||
| 1274 | .rhs = try p.addExtra(Node.WhileCont{ | ||
| 1275 | .cont_expr = cont_expr, | ||
| 1276 | .then_expr = then_expr, | ||
| 1277 | }), | ||
| 1278 | }, | ||
| 1279 | }); | ||
| 1280 | } | ||
| 1281 | }; | ||
| 1282 | _ = try p.parsePayload(); | ||
| 1283 | const else_expr = try p.expectStatement(false); | ||
| 1284 | return p.addNode(.{ | ||
| 1285 | .tag = .@"while", | ||
| 1286 | .main_token = while_token, | ||
| 1287 | .data = .{ | ||
| 1288 | .lhs = condition, | ||
| 1289 | .rhs = try p.addExtra(Node.While{ | ||
| 1290 | .cont_expr = cont_expr, | ||
| 1291 | .then_expr = then_expr, | ||
| 1292 | .else_expr = else_expr, | ||
| 1293 | }), | ||
| 1294 | }, | ||
| 1295 | }); | ||
| 1296 | } | ||
| 1297 | |||
| 1298 | /// BlockExprStatement | ||
| 1299 | /// <- BlockExpr | ||
| 1300 | /// / AssignExpr SEMICOLON | ||
| 1301 | fn parseBlockExprStatement(p: *Parse) !Node.Index { | ||
| 1302 | const block_expr = try p.parseBlockExpr(); | ||
| 1303 | if (block_expr != 0) { | ||
| 1304 | return block_expr; | ||
| 1305 | } | ||
| 1306 | const assign_expr = try p.parseAssignExpr(); | ||
| 1307 | if (assign_expr != 0) { | ||
| 1308 | try p.expectSemicolon(.expected_semi_after_stmt, true); | ||
| 1309 | return assign_expr; | ||
| 1310 | } | ||
| 1311 | return null_node; | ||
| 1312 | } | ||
| 1313 | |||
| 1314 | fn expectBlockExprStatement(p: *Parse) !Node.Index { | ||
| 1315 | const node = try p.parseBlockExprStatement(); | ||
| 1316 | if (node == 0) { | ||
| 1317 | return p.fail(.expected_block_or_expr); | ||
| 1318 | } | ||
| 1319 | return node; | ||
| 1320 | } | ||
| 1321 | |||
| 1322 | /// BlockExpr <- BlockLabel? Block | ||
| 1323 | fn parseBlockExpr(p: *Parse) Error!Node.Index { | ||
| 1324 | switch (p.token_tags[p.tok_i]) { | ||
| 1325 | .identifier => { | ||
| 1326 | if (p.token_tags[p.tok_i + 1] == .colon and | ||
| 1327 | p.token_tags[p.tok_i + 2] == .l_brace) | ||
| 1328 | { | ||
| 1329 | p.tok_i += 2; | ||
| 1330 | return p.parseBlock(); | ||
| 1331 | } else { | ||
| 1332 | return null_node; | ||
| 1333 | } | ||
| 1334 | }, | ||
| 1335 | .l_brace => return p.parseBlock(), | ||
| 1336 | else => return null_node, | ||
| 1337 | } | ||
| 1338 | } | ||
| 1339 | |||
| 1340 | /// AssignExpr <- Expr (AssignOp Expr)? | ||
| 1341 | /// | ||
| 1342 | /// AssignOp | ||
| 1343 | /// <- ASTERISKEQUAL | ||
| 1344 | /// / ASTERISKPIPEEQUAL | ||
| 1345 | /// / SLASHEQUAL | ||
| 1346 | /// / PERCENTEQUAL | ||
| 1347 | /// / PLUSEQUAL | ||
| 1348 | /// / PLUSPIPEEQUAL | ||
| 1349 | /// / MINUSEQUAL | ||
| 1350 | /// / MINUSPIPEEQUAL | ||
| 1351 | /// / LARROW2EQUAL | ||
| 1352 | /// / LARROW2PIPEEQUAL | ||
| 1353 | /// / RARROW2EQUAL | ||
| 1354 | /// / AMPERSANDEQUAL | ||
| 1355 | /// / CARETEQUAL | ||
| 1356 | /// / PIPEEQUAL | ||
| 1357 | /// / ASTERISKPERCENTEQUAL | ||
| 1358 | /// / PLUSPERCENTEQUAL | ||
| 1359 | /// / MINUSPERCENTEQUAL | ||
| 1360 | /// / EQUAL | ||
| 1361 | fn parseAssignExpr(p: *Parse) !Node.Index { | ||
| 1362 | const expr = try p.parseExpr(); | ||
| 1363 | if (expr == 0) return null_node; | ||
| 1364 | |||
| 1365 | const tag: Node.Tag = switch (p.token_tags[p.tok_i]) { | ||
| 1366 | .asterisk_equal => .assign_mul, | ||
| 1367 | .slash_equal => .assign_div, | ||
| 1368 | .percent_equal => .assign_mod, | ||
| 1369 | .plus_equal => .assign_add, | ||
| 1370 | .minus_equal => .assign_sub, | ||
| 1371 | .angle_bracket_angle_bracket_left_equal => .assign_shl, | ||
| 1372 | .angle_bracket_angle_bracket_left_pipe_equal => .assign_shl_sat, | ||
| 1373 | .angle_bracket_angle_bracket_right_equal => .assign_shr, | ||
| 1374 | .ampersand_equal => .assign_bit_and, | ||
| 1375 | .caret_equal => .assign_bit_xor, | ||
| 1376 | .pipe_equal => .assign_bit_or, | ||
| 1377 | .asterisk_percent_equal => .assign_mul_wrap, | ||
| 1378 | .plus_percent_equal => .assign_add_wrap, | ||
| 1379 | .minus_percent_equal => .assign_sub_wrap, | ||
| 1380 | .asterisk_pipe_equal => .assign_mul_sat, | ||
| 1381 | .plus_pipe_equal => .assign_add_sat, | ||
| 1382 | .minus_pipe_equal => .assign_sub_sat, | ||
| 1383 | .equal => .assign, | ||
| 1384 | else => return expr, | ||
| 1385 | }; | ||
| 1386 | return p.addNode(.{ | ||
| 1387 | .tag = tag, | ||
| 1388 | .main_token = p.nextToken(), | ||
| 1389 | .data = .{ | ||
| 1390 | .lhs = expr, | ||
| 1391 | .rhs = try p.expectExpr(), | ||
| 1392 | }, | ||
| 1393 | }); | ||
| 1394 | } | ||
| 1395 | |||
| 1396 | fn expectAssignExpr(p: *Parse) !Node.Index { | ||
| 1397 | const expr = try p.parseAssignExpr(); | ||
| 1398 | if (expr == 0) { | ||
| 1399 | return p.fail(.expected_expr_or_assignment); | ||
| 1400 | } | ||
| 1401 | return expr; | ||
| 1402 | } | ||
| 1403 | |||
| 1404 | fn parseExpr(p: *Parse) Error!Node.Index { | ||
| 1405 | return p.parseExprPrecedence(0); | ||
| 1406 | } | ||
| 1407 | |||
| 1408 | fn expectExpr(p: *Parse) Error!Node.Index { | ||
| 1409 | const node = try p.parseExpr(); | ||
| 1410 | if (node == 0) { | ||
| 1411 | return p.fail(.expected_expr); | ||
| 1412 | } else { | ||
| 1413 | return node; | ||
| 1414 | } | ||
| 1415 | } | ||
| 1416 | |||
| 1417 | const Assoc = enum { | ||
| 1418 | left, | ||
| 1419 | none, | ||
| 1420 | }; | ||
| 1421 | |||
| 1422 | const OperInfo = struct { | ||
| 1423 | prec: i8, | ||
| 1424 | tag: Node.Tag, | ||
| 1425 | assoc: Assoc = Assoc.left, | ||
| 1426 | }; | ||
| 1427 | |||
| 1428 | // A table of binary operator information. Higher precedence numbers are | ||
| 1429 | // stickier. All operators at the same precedence level should have the same | ||
| 1430 | // associativity. | ||
| 1431 | const operTable = std.enums.directEnumArrayDefault(Token.Tag, OperInfo, .{ .prec = -1, .tag = Node.Tag.root }, 0, .{ | ||
| 1432 | .keyword_or = .{ .prec = 10, .tag = .bool_or }, | ||
| 1433 | |||
| 1434 | .keyword_and = .{ .prec = 20, .tag = .bool_and }, | ||
| 1435 | |||
| 1436 | .equal_equal = .{ .prec = 30, .tag = .equal_equal, .assoc = Assoc.none }, | ||
| 1437 | .bang_equal = .{ .prec = 30, .tag = .bang_equal, .assoc = Assoc.none }, | ||
| 1438 | .angle_bracket_left = .{ .prec = 30, .tag = .less_than, .assoc = Assoc.none }, | ||
| 1439 | .angle_bracket_right = .{ .prec = 30, .tag = .greater_than, .assoc = Assoc.none }, | ||
| 1440 | .angle_bracket_left_equal = .{ .prec = 30, .tag = .less_or_equal, .assoc = Assoc.none }, | ||
| 1441 | .angle_bracket_right_equal = .{ .prec = 30, .tag = .greater_or_equal, .assoc = Assoc.none }, | ||
| 1442 | |||
| 1443 | .ampersand = .{ .prec = 40, .tag = .bit_and }, | ||
| 1444 | .caret = .{ .prec = 40, .tag = .bit_xor }, | ||
| 1445 | .pipe = .{ .prec = 40, .tag = .bit_or }, | ||
| 1446 | .keyword_orelse = .{ .prec = 40, .tag = .@"orelse" }, | ||
| 1447 | .keyword_catch = .{ .prec = 40, .tag = .@"catch" }, | ||
| 1448 | |||
| 1449 | .angle_bracket_angle_bracket_left = .{ .prec = 50, .tag = .shl }, | ||
| 1450 | .angle_bracket_angle_bracket_left_pipe = .{ .prec = 50, .tag = .shl_sat }, | ||
| 1451 | .angle_bracket_angle_bracket_right = .{ .prec = 50, .tag = .shr }, | ||
| 1452 | |||
| 1453 | .plus = .{ .prec = 60, .tag = .add }, | ||
| 1454 | .minus = .{ .prec = 60, .tag = .sub }, | ||
| 1455 | .plus_plus = .{ .prec = 60, .tag = .array_cat }, | ||
| 1456 | .plus_percent = .{ .prec = 60, .tag = .add_wrap }, | ||
| 1457 | .minus_percent = .{ .prec = 60, .tag = .sub_wrap }, | ||
| 1458 | .plus_pipe = .{ .prec = 60, .tag = .add_sat }, | ||
| 1459 | .minus_pipe = .{ .prec = 60, .tag = .sub_sat }, | ||
| 1460 | |||
| 1461 | .pipe_pipe = .{ .prec = 70, .tag = .merge_error_sets }, | ||
| 1462 | .asterisk = .{ .prec = 70, .tag = .mul }, | ||
| 1463 | .slash = .{ .prec = 70, .tag = .div }, | ||
| 1464 | .percent = .{ .prec = 70, .tag = .mod }, | ||
| 1465 | .asterisk_asterisk = .{ .prec = 70, .tag = .array_mult }, | ||
| 1466 | .asterisk_percent = .{ .prec = 70, .tag = .mul_wrap }, | ||
| 1467 | .asterisk_pipe = .{ .prec = 70, .tag = .mul_sat }, | ||
| 1468 | }); | ||
| 1469 | |||
| 1470 | fn parseExprPrecedence(p: *Parse, min_prec: i32) Error!Node.Index { | ||
| 1471 | assert(min_prec >= 0); | ||
| 1472 | var node = try p.parsePrefixExpr(); | ||
| 1473 | if (node == 0) { | ||
| 1474 | return null_node; | ||
| 1475 | } | ||
| 1476 | |||
| 1477 | var banned_prec: i8 = -1; | ||
| 1478 | |||
| 1479 | while (true) { | ||
| 1480 | const tok_tag = p.token_tags[p.tok_i]; | ||
| 1481 | const info = operTable[@intCast(usize, @enumToInt(tok_tag))]; | ||
| 1482 | if (info.prec < min_prec) { | ||
| 1483 | break; | ||
| 1484 | } | ||
| 1485 | if (info.prec == banned_prec) { | ||
| 1486 | return p.fail(.chained_comparison_operators); | ||
| 1487 | } | ||
| 1488 | |||
| 1489 | const oper_token = p.nextToken(); | ||
| 1490 | // Special-case handling for "catch" | ||
| 1491 | if (tok_tag == .keyword_catch) { | ||
| 1492 | _ = try p.parsePayload(); | ||
| 1493 | } | ||
| 1494 | const rhs = try p.parseExprPrecedence(info.prec + 1); | ||
| 1495 | if (rhs == 0) { | ||
| 1496 | try p.warn(.expected_expr); | ||
| 1497 | return node; | ||
| 1498 | } | ||
| 1499 | |||
| 1500 | { | ||
| 1501 | const tok_len = tok_tag.lexeme().?.len; | ||
| 1502 | const char_before = p.source[p.token_starts[oper_token] - 1]; | ||
| 1503 | const char_after = p.source[p.token_starts[oper_token] + tok_len]; | ||
| 1504 | if (tok_tag == .ampersand and char_after == '&') { | ||
| 1505 | // without types we don't know if '&&' was intended as 'bitwise_and address_of', or a c-style logical_and | ||
| 1506 | // The best the parser can do is recommend changing it to 'and' or ' & &' | ||
| 1507 | try p.warnMsg(.{ .tag = .invalid_ampersand_ampersand, .token = oper_token }); | ||
| 1508 | } else if (std.ascii.isWhitespace(char_before) != std.ascii.isWhitespace(char_after)) { | ||
| 1509 | try p.warnMsg(.{ .tag = .mismatched_binary_op_whitespace, .token = oper_token }); | ||
| 1510 | } | ||
| 1511 | } | ||
| 1512 | |||
| 1513 | node = try p.addNode(.{ | ||
| 1514 | .tag = info.tag, | ||
| 1515 | .main_token = oper_token, | ||
| 1516 | .data = .{ | ||
| 1517 | .lhs = node, | ||
| 1518 | .rhs = rhs, | ||
| 1519 | }, | ||
| 1520 | }); | ||
| 1521 | |||
| 1522 | if (info.assoc == Assoc.none) { | ||
| 1523 | banned_prec = info.prec; | ||
| 1524 | } | ||
| 1525 | } | ||
| 1526 | |||
| 1527 | return node; | ||
| 1528 | } | ||
| 1529 | |||
| 1530 | /// PrefixExpr <- PrefixOp* PrimaryExpr | ||
| 1531 | /// | ||
| 1532 | /// PrefixOp | ||
| 1533 | /// <- EXCLAMATIONMARK | ||
| 1534 | /// / MINUS | ||
| 1535 | /// / TILDE | ||
| 1536 | /// / MINUSPERCENT | ||
| 1537 | /// / AMPERSAND | ||
| 1538 | /// / KEYWORD_try | ||
| 1539 | /// / KEYWORD_await | ||
| 1540 | fn parsePrefixExpr(p: *Parse) Error!Node.Index { | ||
| 1541 | const tag: Node.Tag = switch (p.token_tags[p.tok_i]) { | ||
| 1542 | .bang => .bool_not, | ||
| 1543 | .minus => .negation, | ||
| 1544 | .tilde => .bit_not, | ||
| 1545 | .minus_percent => .negation_wrap, | ||
| 1546 | .ampersand => .address_of, | ||
| 1547 | .keyword_try => .@"try", | ||
| 1548 | .keyword_await => .@"await", | ||
| 1549 | else => return p.parsePrimaryExpr(), | ||
| 1550 | }; | ||
| 1551 | return p.addNode(.{ | ||
| 1552 | .tag = tag, | ||
| 1553 | .main_token = p.nextToken(), | ||
| 1554 | .data = .{ | ||
| 1555 | .lhs = try p.expectPrefixExpr(), | ||
| 1556 | .rhs = undefined, | ||
| 1557 | }, | ||
| 1558 | }); | ||
| 1559 | } | ||
| 1560 | |||
| 1561 | fn expectPrefixExpr(p: *Parse) Error!Node.Index { | ||
| 1562 | const node = try p.parsePrefixExpr(); | ||
| 1563 | if (node == 0) { | ||
| 1564 | return p.fail(.expected_prefix_expr); | ||
| 1565 | } | ||
| 1566 | return node; | ||
| 1567 | } | ||
| 1568 | |||
| 1569 | /// TypeExpr <- PrefixTypeOp* ErrorUnionExpr | ||
| 1570 | /// | ||
| 1571 | /// PrefixTypeOp | ||
| 1572 | /// <- QUESTIONMARK | ||
| 1573 | /// / KEYWORD_anyframe MINUSRARROW | ||
| 1574 | /// / SliceTypeStart (ByteAlign / AddrSpace / KEYWORD_const / KEYWORD_volatile / KEYWORD_allowzero)* | ||
| 1575 | /// / PtrTypeStart (AddrSpace / KEYWORD_align LPAREN Expr (COLON Expr COLON Expr)? RPAREN / KEYWORD_const / KEYWORD_volatile / KEYWORD_allowzero)* | ||
| 1576 | /// / ArrayTypeStart | ||
| 1577 | /// | ||
| 1578 | /// SliceTypeStart <- LBRACKET (COLON Expr)? RBRACKET | ||
| 1579 | /// | ||
| 1580 | /// PtrTypeStart | ||
| 1581 | /// <- ASTERISK | ||
| 1582 | /// / ASTERISK2 | ||
| 1583 | /// / LBRACKET ASTERISK (LETTERC / COLON Expr)? RBRACKET | ||
| 1584 | /// | ||
| 1585 | /// ArrayTypeStart <- LBRACKET Expr (COLON Expr)? RBRACKET | ||
| 1586 | fn parseTypeExpr(p: *Parse) Error!Node.Index { | ||
| 1587 | switch (p.token_tags[p.tok_i]) { | ||
| 1588 | .question_mark => return p.addNode(.{ | ||
| 1589 | .tag = .optional_type, | ||
| 1590 | .main_token = p.nextToken(), | ||
| 1591 | .data = .{ | ||
| 1592 | .lhs = try p.expectTypeExpr(), | ||
| 1593 | .rhs = undefined, | ||
| 1594 | }, | ||
| 1595 | }), | ||
| 1596 | .keyword_anyframe => switch (p.token_tags[p.tok_i + 1]) { | ||
| 1597 | .arrow => return p.addNode(.{ | ||
| 1598 | .tag = .anyframe_type, | ||
| 1599 | .main_token = p.nextToken(), | ||
| 1600 | .data = .{ | ||
| 1601 | .lhs = p.nextToken(), | ||
| 1602 | .rhs = try p.expectTypeExpr(), | ||
| 1603 | }, | ||
| 1604 | }), | ||
| 1605 | else => return p.parseErrorUnionExpr(), | ||
| 1606 | }, | ||
| 1607 | .asterisk => { | ||
| 1608 | const asterisk = p.nextToken(); | ||
| 1609 | const mods = try p.parsePtrModifiers(); | ||
| 1610 | const elem_type = try p.expectTypeExpr(); | ||
| 1611 | if (mods.bit_range_start != 0) { | ||
| 1612 | return p.addNode(.{ | ||
| 1613 | .tag = .ptr_type_bit_range, | ||
| 1614 | .main_token = asterisk, | ||
| 1615 | .data = .{ | ||
| 1616 | .lhs = try p.addExtra(Node.PtrTypeBitRange{ | ||
| 1617 | .sentinel = 0, | ||
| 1618 | .align_node = mods.align_node, | ||
| 1619 | .addrspace_node = mods.addrspace_node, | ||
| 1620 | .bit_range_start = mods.bit_range_start, | ||
| 1621 | .bit_range_end = mods.bit_range_end, | ||
| 1622 | }), | ||
| 1623 | .rhs = elem_type, | ||
| 1624 | }, | ||
| 1625 | }); | ||
| 1626 | } else if (mods.addrspace_node != 0) { | ||
| 1627 | return p.addNode(.{ | ||
| 1628 | .tag = .ptr_type, | ||
| 1629 | .main_token = asterisk, | ||
| 1630 | .data = .{ | ||
| 1631 | .lhs = try p.addExtra(Node.PtrType{ | ||
| 1632 | .sentinel = 0, | ||
| 1633 | .align_node = mods.align_node, | ||
| 1634 | .addrspace_node = mods.addrspace_node, | ||
| 1635 | }), | ||
| 1636 | .rhs = elem_type, | ||
| 1637 | }, | ||
| 1638 | }); | ||
| 1639 | } else { | ||
| 1640 | return p.addNode(.{ | ||
| 1641 | .tag = .ptr_type_aligned, | ||
| 1642 | .main_token = asterisk, | ||
| 1643 | .data = .{ | ||
| 1644 | .lhs = mods.align_node, | ||
| 1645 | .rhs = elem_type, | ||
| 1646 | }, | ||
| 1647 | }); | ||
| 1648 | } | ||
| 1649 | }, | ||
| 1650 | .asterisk_asterisk => { | ||
| 1651 | const asterisk = p.nextToken(); | ||
| 1652 | const mods = try p.parsePtrModifiers(); | ||
| 1653 | const elem_type = try p.expectTypeExpr(); | ||
| 1654 | const inner: Node.Index = inner: { | ||
| 1655 | if (mods.bit_range_start != 0) { | ||
| 1656 | break :inner try p.addNode(.{ | ||
| 1657 | .tag = .ptr_type_bit_range, | ||
| 1658 | .main_token = asterisk, | ||
| 1659 | .data = .{ | ||
| 1660 | .lhs = try p.addExtra(Node.PtrTypeBitRange{ | ||
| 1661 | .sentinel = 0, | ||
| 1662 | .align_node = mods.align_node, | ||
| 1663 | .addrspace_node = mods.addrspace_node, | ||
| 1664 | .bit_range_start = mods.bit_range_start, | ||
| 1665 | .bit_range_end = mods.bit_range_end, | ||
| 1666 | }), | ||
| 1667 | .rhs = elem_type, | ||
| 1668 | }, | ||
| 1669 | }); | ||
| 1670 | } else if (mods.addrspace_node != 0) { | ||
| 1671 | break :inner try p.addNode(.{ | ||
| 1672 | .tag = .ptr_type, | ||
| 1673 | .main_token = asterisk, | ||
| 1674 | .data = .{ | ||
| 1675 | .lhs = try p.addExtra(Node.PtrType{ | ||
| 1676 | .sentinel = 0, | ||
| 1677 | .align_node = mods.align_node, | ||
| 1678 | .addrspace_node = mods.addrspace_node, | ||
| 1679 | }), | ||
| 1680 | .rhs = elem_type, | ||
| 1681 | }, | ||
| 1682 | }); | ||
| 1683 | } else { | ||
| 1684 | break :inner try p.addNode(.{ | ||
| 1685 | .tag = .ptr_type_aligned, | ||
| 1686 | .main_token = asterisk, | ||
| 1687 | .data = .{ | ||
| 1688 | .lhs = mods.align_node, | ||
| 1689 | .rhs = elem_type, | ||
| 1690 | }, | ||
| 1691 | }); | ||
| 1692 | } | ||
| 1693 | }; | ||
| 1694 | return p.addNode(.{ | ||
| 1695 | .tag = .ptr_type_aligned, | ||
| 1696 | .main_token = asterisk, | ||
| 1697 | .data = .{ | ||
| 1698 | .lhs = 0, | ||
| 1699 | .rhs = inner, | ||
| 1700 | }, | ||
| 1701 | }); | ||
| 1702 | }, | ||
| 1703 | .l_bracket => switch (p.token_tags[p.tok_i + 1]) { | ||
| 1704 | .asterisk => { | ||
| 1705 | _ = p.nextToken(); | ||
| 1706 | const asterisk = p.nextToken(); | ||
| 1707 | var sentinel: Node.Index = 0; | ||
| 1708 | if (p.eatToken(.identifier)) |ident| { | ||
| 1709 | const ident_slice = p.source[p.token_starts[ident]..p.token_starts[ident + 1]]; | ||
| 1710 | if (!std.mem.eql(u8, std.mem.trimRight(u8, ident_slice, &std.ascii.whitespace), "c")) { | ||
| 1711 | p.tok_i -= 1; | ||
| 1712 | } | ||
| 1713 | } else if (p.eatToken(.colon)) |_| { | ||
| 1714 | sentinel = try p.expectExpr(); | ||
| 1715 | } | ||
| 1716 | _ = try p.expectToken(.r_bracket); | ||
| 1717 | const mods = try p.parsePtrModifiers(); | ||
| 1718 | const elem_type = try p.expectTypeExpr(); | ||
| 1719 | if (mods.bit_range_start == 0) { | ||
| 1720 | if (sentinel == 0 and mods.addrspace_node == 0) { | ||
| 1721 | return p.addNode(.{ | ||
| 1722 | .tag = .ptr_type_aligned, | ||
| 1723 | .main_token = asterisk, | ||
| 1724 | .data = .{ | ||
| 1725 | .lhs = mods.align_node, | ||
| 1726 | .rhs = elem_type, | ||
| 1727 | }, | ||
| 1728 | }); | ||
| 1729 | } else if (mods.align_node == 0 and mods.addrspace_node == 0) { | ||
| 1730 | return p.addNode(.{ | ||
| 1731 | .tag = .ptr_type_sentinel, | ||
| 1732 | .main_token = asterisk, | ||
| 1733 | .data = .{ | ||
| 1734 | .lhs = sentinel, | ||
| 1735 | .rhs = elem_type, | ||
| 1736 | }, | ||
| 1737 | }); | ||
| 1738 | } else { | ||
| 1739 | return p.addNode(.{ | ||
| 1740 | .tag = .ptr_type, | ||
| 1741 | .main_token = asterisk, | ||
| 1742 | .data = .{ | ||
| 1743 | .lhs = try p.addExtra(Node.PtrType{ | ||
| 1744 | .sentinel = sentinel, | ||
| 1745 | .align_node = mods.align_node, | ||
| 1746 | .addrspace_node = mods.addrspace_node, | ||
| 1747 | }), | ||
| 1748 | .rhs = elem_type, | ||
| 1749 | }, | ||
| 1750 | }); | ||
| 1751 | } | ||
| 1752 | } else { | ||
| 1753 | return p.addNode(.{ | ||
| 1754 | .tag = .ptr_type_bit_range, | ||
| 1755 | .main_token = asterisk, | ||
| 1756 | .data = .{ | ||
| 1757 | .lhs = try p.addExtra(Node.PtrTypeBitRange{ | ||
| 1758 | .sentinel = sentinel, | ||
| 1759 | .align_node = mods.align_node, | ||
| 1760 | .addrspace_node = mods.addrspace_node, | ||
| 1761 | .bit_range_start = mods.bit_range_start, | ||
| 1762 | .bit_range_end = mods.bit_range_end, | ||
| 1763 | }), | ||
| 1764 | .rhs = elem_type, | ||
| 1765 | }, | ||
| 1766 | }); | ||
| 1767 | } | ||
| 1768 | }, | ||
| 1769 | else => { | ||
| 1770 | const lbracket = p.nextToken(); | ||
| 1771 | const len_expr = try p.parseExpr(); | ||
| 1772 | const sentinel: Node.Index = if (p.eatToken(.colon)) |_| | ||
| 1773 | try p.expectExpr() | ||
| 1774 | else | ||
| 1775 | 0; | ||
| 1776 | _ = try p.expectToken(.r_bracket); | ||
| 1777 | if (len_expr == 0) { | ||
| 1778 | const mods = try p.parsePtrModifiers(); | ||
| 1779 | const elem_type = try p.expectTypeExpr(); | ||
| 1780 | if (mods.bit_range_start != 0) { | ||
| 1781 | try p.warnMsg(.{ | ||
| 1782 | .tag = .invalid_bit_range, | ||
| 1783 | .token = p.nodes.items(.main_token)[mods.bit_range_start], | ||
| 1784 | }); | ||
| 1785 | } | ||
| 1786 | if (sentinel == 0 and mods.addrspace_node == 0) { | ||
| 1787 | return p.addNode(.{ | ||
| 1788 | .tag = .ptr_type_aligned, | ||
| 1789 | .main_token = lbracket, | ||
| 1790 | .data = .{ | ||
| 1791 | .lhs = mods.align_node, | ||
| 1792 | .rhs = elem_type, | ||
| 1793 | }, | ||
| 1794 | }); | ||
| 1795 | } else if (mods.align_node == 0 and mods.addrspace_node == 0) { | ||
| 1796 | return p.addNode(.{ | ||
| 1797 | .tag = .ptr_type_sentinel, | ||
| 1798 | .main_token = lbracket, | ||
| 1799 | .data = .{ | ||
| 1800 | .lhs = sentinel, | ||
| 1801 | .rhs = elem_type, | ||
| 1802 | }, | ||
| 1803 | }); | ||
| 1804 | } else { | ||
| 1805 | return p.addNode(.{ | ||
| 1806 | .tag = .ptr_type, | ||
| 1807 | .main_token = lbracket, | ||
| 1808 | .data = .{ | ||
| 1809 | .lhs = try p.addExtra(Node.PtrType{ | ||
| 1810 | .sentinel = sentinel, | ||
| 1811 | .align_node = mods.align_node, | ||
| 1812 | .addrspace_node = mods.addrspace_node, | ||
| 1813 | }), | ||
| 1814 | .rhs = elem_type, | ||
| 1815 | }, | ||
| 1816 | }); | ||
| 1817 | } | ||
| 1818 | } else { | ||
| 1819 | switch (p.token_tags[p.tok_i]) { | ||
| 1820 | .keyword_align, | ||
| 1821 | .keyword_const, | ||
| 1822 | .keyword_volatile, | ||
| 1823 | .keyword_allowzero, | ||
| 1824 | .keyword_addrspace, | ||
| 1825 | => return p.fail(.ptr_mod_on_array_child_type), | ||
| 1826 | else => {}, | ||
| 1827 | } | ||
| 1828 | const elem_type = try p.expectTypeExpr(); | ||
| 1829 | if (sentinel == 0) { | ||
| 1830 | return p.addNode(.{ | ||
| 1831 | .tag = .array_type, | ||
| 1832 | .main_token = lbracket, | ||
| 1833 | .data = .{ | ||
| 1834 | .lhs = len_expr, | ||
| 1835 | .rhs = elem_type, | ||
| 1836 | }, | ||
| 1837 | }); | ||
| 1838 | } else { | ||
| 1839 | return p.addNode(.{ | ||
| 1840 | .tag = .array_type_sentinel, | ||
| 1841 | .main_token = lbracket, | ||
| 1842 | .data = .{ | ||
| 1843 | .lhs = len_expr, | ||
| 1844 | .rhs = try p.addExtra(.{ | ||
| 1845 | .elem_type = elem_type, | ||
| 1846 | .sentinel = sentinel, | ||
| 1847 | }), | ||
| 1848 | }, | ||
| 1849 | }); | ||
| 1850 | } | ||
| 1851 | } | ||
| 1852 | }, | ||
| 1853 | }, | ||
| 1854 | else => return p.parseErrorUnionExpr(), | ||
| 1855 | } | ||
| 1856 | } | ||
| 1857 | |||
| 1858 | fn expectTypeExpr(p: *Parse) Error!Node.Index { | ||
| 1859 | const node = try p.parseTypeExpr(); | ||
| 1860 | if (node == 0) { | ||
| 1861 | return p.fail(.expected_type_expr); | ||
| 1862 | } | ||
| 1863 | return node; | ||
| 1864 | } | ||
| 1865 | |||
| 1866 | /// PrimaryExpr | ||
| 1867 | /// <- AsmExpr | ||
| 1868 | /// / IfExpr | ||
| 1869 | /// / KEYWORD_break BreakLabel? Expr? | ||
| 1870 | /// / KEYWORD_comptime Expr | ||
| 1871 | /// / KEYWORD_nosuspend Expr | ||
| 1872 | /// / KEYWORD_continue BreakLabel? | ||
| 1873 | /// / KEYWORD_resume Expr | ||
| 1874 | /// / KEYWORD_return Expr? | ||
| 1875 | /// / BlockLabel? LoopExpr | ||
| 1876 | /// / Block | ||
| 1877 | /// / CurlySuffixExpr | ||
| 1878 | fn parsePrimaryExpr(p: *Parse) !Node.Index { | ||
| 1879 | switch (p.token_tags[p.tok_i]) { | ||
| 1880 | .keyword_asm => return p.expectAsmExpr(), | ||
| 1881 | .keyword_if => return p.parseIfExpr(), | ||
| 1882 | .keyword_break => { | ||
| 1883 | p.tok_i += 1; | ||
| 1884 | return p.addNode(.{ | ||
| 1885 | .tag = .@"break", | ||
| 1886 | .main_token = p.tok_i - 1, | ||
| 1887 | .data = .{ | ||
| 1888 | .lhs = try p.parseBreakLabel(), | ||
| 1889 | .rhs = try p.parseExpr(), | ||
| 1890 | }, | ||
| 1891 | }); | ||
| 1892 | }, | ||
| 1893 | .keyword_continue => { | ||
| 1894 | p.tok_i += 1; | ||
| 1895 | return p.addNode(.{ | ||
| 1896 | .tag = .@"continue", | ||
| 1897 | .main_token = p.tok_i - 1, | ||
| 1898 | .data = .{ | ||
| 1899 | .lhs = try p.parseBreakLabel(), | ||
| 1900 | .rhs = undefined, | ||
| 1901 | }, | ||
| 1902 | }); | ||
| 1903 | }, | ||
| 1904 | .keyword_comptime => { | ||
| 1905 | p.tok_i += 1; | ||
| 1906 | return p.addNode(.{ | ||
| 1907 | .tag = .@"comptime", | ||
| 1908 | .main_token = p.tok_i - 1, | ||
| 1909 | .data = .{ | ||
| 1910 | .lhs = try p.expectExpr(), | ||
| 1911 | .rhs = undefined, | ||
| 1912 | }, | ||
| 1913 | }); | ||
| 1914 | }, | ||
| 1915 | .keyword_nosuspend => { | ||
| 1916 | p.tok_i += 1; | ||
| 1917 | return p.addNode(.{ | ||
| 1918 | .tag = .@"nosuspend", | ||
| 1919 | .main_token = p.tok_i - 1, | ||
| 1920 | .data = .{ | ||
| 1921 | .lhs = try p.expectExpr(), | ||
| 1922 | .rhs = undefined, | ||
| 1923 | }, | ||
| 1924 | }); | ||
| 1925 | }, | ||
| 1926 | .keyword_resume => { | ||
| 1927 | p.tok_i += 1; | ||
| 1928 | return p.addNode(.{ | ||
| 1929 | .tag = .@"resume", | ||
| 1930 | .main_token = p.tok_i - 1, | ||
| 1931 | .data = .{ | ||
| 1932 | .lhs = try p.expectExpr(), | ||
| 1933 | .rhs = undefined, | ||
| 1934 | }, | ||
| 1935 | }); | ||
| 1936 | }, | ||
| 1937 | .keyword_return => { | ||
| 1938 | p.tok_i += 1; | ||
| 1939 | return p.addNode(.{ | ||
| 1940 | .tag = .@"return", | ||
| 1941 | .main_token = p.tok_i - 1, | ||
| 1942 | .data = .{ | ||
| 1943 | .lhs = try p.parseExpr(), | ||
| 1944 | .rhs = undefined, | ||
| 1945 | }, | ||
| 1946 | }); | ||
| 1947 | }, | ||
| 1948 | .identifier => { | ||
| 1949 | if (p.token_tags[p.tok_i + 1] == .colon) { | ||
| 1950 | switch (p.token_tags[p.tok_i + 2]) { | ||
| 1951 | .keyword_inline => { | ||
| 1952 | p.tok_i += 3; | ||
| 1953 | switch (p.token_tags[p.tok_i]) { | ||
| 1954 | .keyword_for => return p.parseForExpr(), | ||
| 1955 | .keyword_while => return p.parseWhileExpr(), | ||
| 1956 | else => return p.fail(.expected_inlinable), | ||
| 1957 | } | ||
| 1958 | }, | ||
| 1959 | .keyword_for => { | ||
| 1960 | p.tok_i += 2; | ||
| 1961 | return p.parseForExpr(); | ||
| 1962 | }, | ||
| 1963 | .keyword_while => { | ||
| 1964 | p.tok_i += 2; | ||
| 1965 | return p.parseWhileExpr(); | ||
| 1966 | }, | ||
| 1967 | .l_brace => { | ||
| 1968 | p.tok_i += 2; | ||
| 1969 | return p.parseBlock(); | ||
| 1970 | }, | ||
| 1971 | else => return p.parseCurlySuffixExpr(), | ||
| 1972 | } | ||
| 1973 | } else { | ||
| 1974 | return p.parseCurlySuffixExpr(); | ||
| 1975 | } | ||
| 1976 | }, | ||
| 1977 | .keyword_inline => { | ||
| 1978 | p.tok_i += 1; | ||
| 1979 | switch (p.token_tags[p.tok_i]) { | ||
| 1980 | .keyword_for => return p.parseForExpr(), | ||
| 1981 | .keyword_while => return p.parseWhileExpr(), | ||
| 1982 | else => return p.fail(.expected_inlinable), | ||
| 1983 | } | ||
| 1984 | }, | ||
| 1985 | .keyword_for => return p.parseForExpr(), | ||
| 1986 | .keyword_while => return p.parseWhileExpr(), | ||
| 1987 | .l_brace => return p.parseBlock(), | ||
| 1988 | else => return p.parseCurlySuffixExpr(), | ||
| 1989 | } | ||
| 1990 | } | ||
| 1991 | |||
| 1992 | /// IfExpr <- IfPrefix Expr (KEYWORD_else Payload? Expr)? | ||
| 1993 | fn parseIfExpr(p: *Parse) !Node.Index { | ||
| 1994 | return p.parseIf(expectExpr); | ||
| 1995 | } | ||
| 1996 | |||
| 1997 | /// Block <- LBRACE Statement* RBRACE | ||
| 1998 | fn parseBlock(p: *Parse) !Node.Index { | ||
| 1999 | const lbrace = p.eatToken(.l_brace) orelse return null_node; | ||
| 2000 | const scratch_top = p.scratch.items.len; | ||
| 2001 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 2002 | while (true) { | ||
| 2003 | if (p.token_tags[p.tok_i] == .r_brace) break; | ||
| 2004 | const statement = try p.expectStatementRecoverable(); | ||
| 2005 | if (statement == 0) break; | ||
| 2006 | try p.scratch.append(p.gpa, statement); | ||
| 2007 | } | ||
| 2008 | _ = try p.expectToken(.r_brace); | ||
| 2009 | const semicolon = (p.token_tags[p.tok_i - 2] == .semicolon); | ||
| 2010 | const statements = p.scratch.items[scratch_top..]; | ||
| 2011 | switch (statements.len) { | ||
| 2012 | 0 => return p.addNode(.{ | ||
| 2013 | .tag = .block_two, | ||
| 2014 | .main_token = lbrace, | ||
| 2015 | .data = .{ | ||
| 2016 | .lhs = 0, | ||
| 2017 | .rhs = 0, | ||
| 2018 | }, | ||
| 2019 | }), | ||
| 2020 | 1 => return p.addNode(.{ | ||
| 2021 | .tag = if (semicolon) .block_two_semicolon else .block_two, | ||
| 2022 | .main_token = lbrace, | ||
| 2023 | .data = .{ | ||
| 2024 | .lhs = statements[0], | ||
| 2025 | .rhs = 0, | ||
| 2026 | }, | ||
| 2027 | }), | ||
| 2028 | 2 => return p.addNode(.{ | ||
| 2029 | .tag = if (semicolon) .block_two_semicolon else .block_two, | ||
| 2030 | .main_token = lbrace, | ||
| 2031 | .data = .{ | ||
| 2032 | .lhs = statements[0], | ||
| 2033 | .rhs = statements[1], | ||
| 2034 | }, | ||
| 2035 | }), | ||
| 2036 | else => { | ||
| 2037 | const span = try p.listToSpan(statements); | ||
| 2038 | return p.addNode(.{ | ||
| 2039 | .tag = if (semicolon) .block_semicolon else .block, | ||
| 2040 | .main_token = lbrace, | ||
| 2041 | .data = .{ | ||
| 2042 | .lhs = span.start, | ||
| 2043 | .rhs = span.end, | ||
| 2044 | }, | ||
| 2045 | }); | ||
| 2046 | }, | ||
| 2047 | } | ||
| 2048 | } | ||
| 2049 | |||
| 2050 | /// ForPrefix <- KEYWORD_for LPAREN Expr RPAREN PtrIndexPayload | ||
| 2051 | /// | ||
| 2052 | /// ForExpr <- ForPrefix Expr (KEYWORD_else Expr)? | ||
| 2053 | fn parseForExpr(p: *Parse) !Node.Index { | ||
| 2054 | const for_token = p.eatToken(.keyword_for) orelse return null_node; | ||
| 2055 | _ = try p.expectToken(.l_paren); | ||
| 2056 | const array_expr = try p.expectExpr(); | ||
| 2057 | _ = try p.expectToken(.r_paren); | ||
| 2058 | const found_payload = try p.parsePtrIndexPayload(); | ||
| 2059 | if (found_payload == 0) try p.warn(.expected_loop_payload); | ||
| 2060 | |||
| 2061 | const then_expr = try p.expectExpr(); | ||
| 2062 | _ = p.eatToken(.keyword_else) orelse { | ||
| 2063 | return p.addNode(.{ | ||
| 2064 | .tag = .for_simple, | ||
| 2065 | .main_token = for_token, | ||
| 2066 | .data = .{ | ||
| 2067 | .lhs = array_expr, | ||
| 2068 | .rhs = then_expr, | ||
| 2069 | }, | ||
| 2070 | }); | ||
| 2071 | }; | ||
| 2072 | const else_expr = try p.expectExpr(); | ||
| 2073 | return p.addNode(.{ | ||
| 2074 | .tag = .@"for", | ||
| 2075 | .main_token = for_token, | ||
| 2076 | .data = .{ | ||
| 2077 | .lhs = array_expr, | ||
| 2078 | .rhs = try p.addExtra(Node.If{ | ||
| 2079 | .then_expr = then_expr, | ||
| 2080 | .else_expr = else_expr, | ||
| 2081 | }), | ||
| 2082 | }, | ||
| 2083 | }); | ||
| 2084 | } | ||
| 2085 | |||
| 2086 | /// WhilePrefix <- KEYWORD_while LPAREN Expr RPAREN PtrPayload? WhileContinueExpr? | ||
| 2087 | /// | ||
| 2088 | /// WhileExpr <- WhilePrefix Expr (KEYWORD_else Payload? Expr)? | ||
| 2089 | fn parseWhileExpr(p: *Parse) !Node.Index { | ||
| 2090 | const while_token = p.eatToken(.keyword_while) orelse return null_node; | ||
| 2091 | _ = try p.expectToken(.l_paren); | ||
| 2092 | const condition = try p.expectExpr(); | ||
| 2093 | _ = try p.expectToken(.r_paren); | ||
| 2094 | _ = try p.parsePtrPayload(); | ||
| 2095 | const cont_expr = try p.parseWhileContinueExpr(); | ||
| 2096 | |||
| 2097 | const then_expr = try p.expectExpr(); | ||
| 2098 | _ = p.eatToken(.keyword_else) orelse { | ||
| 2099 | if (cont_expr == 0) { | ||
| 2100 | return p.addNode(.{ | ||
| 2101 | .tag = .while_simple, | ||
| 2102 | .main_token = while_token, | ||
| 2103 | .data = .{ | ||
| 2104 | .lhs = condition, | ||
| 2105 | .rhs = then_expr, | ||
| 2106 | }, | ||
| 2107 | }); | ||
| 2108 | } else { | ||
| 2109 | return p.addNode(.{ | ||
| 2110 | .tag = .while_cont, | ||
| 2111 | .main_token = while_token, | ||
| 2112 | .data = .{ | ||
| 2113 | .lhs = condition, | ||
| 2114 | .rhs = try p.addExtra(Node.WhileCont{ | ||
| 2115 | .cont_expr = cont_expr, | ||
| 2116 | .then_expr = then_expr, | ||
| 2117 | }), | ||
| 2118 | }, | ||
| 2119 | }); | ||
| 2120 | } | ||
| 2121 | }; | ||
| 2122 | _ = try p.parsePayload(); | ||
| 2123 | const else_expr = try p.expectExpr(); | ||
| 2124 | return p.addNode(.{ | ||
| 2125 | .tag = .@"while", | ||
| 2126 | .main_token = while_token, | ||
| 2127 | .data = .{ | ||
| 2128 | .lhs = condition, | ||
| 2129 | .rhs = try p.addExtra(Node.While{ | ||
| 2130 | .cont_expr = cont_expr, | ||
| 2131 | .then_expr = then_expr, | ||
| 2132 | .else_expr = else_expr, | ||
| 2133 | }), | ||
| 2134 | }, | ||
| 2135 | }); | ||
| 2136 | } | ||
| 2137 | |||
| 2138 | /// CurlySuffixExpr <- TypeExpr InitList? | ||
| 2139 | /// | ||
| 2140 | /// InitList | ||
| 2141 | /// <- LBRACE FieldInit (COMMA FieldInit)* COMMA? RBRACE | ||
| 2142 | /// / LBRACE Expr (COMMA Expr)* COMMA? RBRACE | ||
| 2143 | /// / LBRACE RBRACE | ||
| 2144 | fn parseCurlySuffixExpr(p: *Parse) !Node.Index { | ||
| 2145 | const lhs = try p.parseTypeExpr(); | ||
| 2146 | if (lhs == 0) return null_node; | ||
| 2147 | const lbrace = p.eatToken(.l_brace) orelse return lhs; | ||
| 2148 | |||
| 2149 | // If there are 0 or 1 items, we can use ArrayInitOne/StructInitOne; | ||
| 2150 | // otherwise we use the full ArrayInit/StructInit. | ||
| 2151 | |||
| 2152 | const scratch_top = p.scratch.items.len; | ||
| 2153 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 2154 | const field_init = try p.parseFieldInit(); | ||
| 2155 | if (field_init != 0) { | ||
| 2156 | try p.scratch.append(p.gpa, field_init); | ||
| 2157 | while (true) { | ||
| 2158 | switch (p.token_tags[p.tok_i]) { | ||
| 2159 | .comma => p.tok_i += 1, | ||
| 2160 | .r_brace => { | ||
| 2161 | p.tok_i += 1; | ||
| 2162 | break; | ||
| 2163 | }, | ||
| 2164 | .colon, .r_paren, .r_bracket => return p.failExpected(.r_brace), | ||
| 2165 | // Likely just a missing comma; give error but continue parsing. | ||
| 2166 | else => try p.warn(.expected_comma_after_initializer), | ||
| 2167 | } | ||
| 2168 | if (p.eatToken(.r_brace)) |_| break; | ||
| 2169 | const next = try p.expectFieldInit(); | ||
| 2170 | try p.scratch.append(p.gpa, next); | ||
| 2171 | } | ||
| 2172 | const comma = (p.token_tags[p.tok_i - 2] == .comma); | ||
| 2173 | const inits = p.scratch.items[scratch_top..]; | ||
| 2174 | switch (inits.len) { | ||
| 2175 | 0 => unreachable, | ||
| 2176 | 1 => return p.addNode(.{ | ||
| 2177 | .tag = if (comma) .struct_init_one_comma else .struct_init_one, | ||
| 2178 | .main_token = lbrace, | ||
| 2179 | .data = .{ | ||
| 2180 | .lhs = lhs, | ||
| 2181 | .rhs = inits[0], | ||
| 2182 | }, | ||
| 2183 | }), | ||
| 2184 | else => return p.addNode(.{ | ||
| 2185 | .tag = if (comma) .struct_init_comma else .struct_init, | ||
| 2186 | .main_token = lbrace, | ||
| 2187 | .data = .{ | ||
| 2188 | .lhs = lhs, | ||
| 2189 | .rhs = try p.addExtra(try p.listToSpan(inits)), | ||
| 2190 | }, | ||
| 2191 | }), | ||
| 2192 | } | ||
| 2193 | } | ||
| 2194 | |||
| 2195 | while (true) { | ||
| 2196 | if (p.eatToken(.r_brace)) |_| break; | ||
| 2197 | const elem_init = try p.expectExpr(); | ||
| 2198 | try p.scratch.append(p.gpa, elem_init); | ||
| 2199 | switch (p.token_tags[p.tok_i]) { | ||
| 2200 | .comma => p.tok_i += 1, | ||
| 2201 | .r_brace => { | ||
| 2202 | p.tok_i += 1; | ||
| 2203 | break; | ||
| 2204 | }, | ||
| 2205 | .colon, .r_paren, .r_bracket => return p.failExpected(.r_brace), | ||
| 2206 | // Likely just a missing comma; give error but continue parsing. | ||
| 2207 | else => try p.warn(.expected_comma_after_initializer), | ||
| 2208 | } | ||
| 2209 | } | ||
| 2210 | const comma = (p.token_tags[p.tok_i - 2] == .comma); | ||
| 2211 | const inits = p.scratch.items[scratch_top..]; | ||
| 2212 | switch (inits.len) { | ||
| 2213 | 0 => return p.addNode(.{ | ||
| 2214 | .tag = .struct_init_one, | ||
| 2215 | .main_token = lbrace, | ||
| 2216 | .data = .{ | ||
| 2217 | .lhs = lhs, | ||
| 2218 | .rhs = 0, | ||
| 2219 | }, | ||
| 2220 | }), | ||
| 2221 | 1 => return p.addNode(.{ | ||
| 2222 | .tag = if (comma) .array_init_one_comma else .array_init_one, | ||
| 2223 | .main_token = lbrace, | ||
| 2224 | .data = .{ | ||
| 2225 | .lhs = lhs, | ||
| 2226 | .rhs = inits[0], | ||
| 2227 | }, | ||
| 2228 | }), | ||
| 2229 | else => return p.addNode(.{ | ||
| 2230 | .tag = if (comma) .array_init_comma else .array_init, | ||
| 2231 | .main_token = lbrace, | ||
| 2232 | .data = .{ | ||
| 2233 | .lhs = lhs, | ||
| 2234 | .rhs = try p.addExtra(try p.listToSpan(inits)), | ||
| 2235 | }, | ||
| 2236 | }), | ||
| 2237 | } | ||
| 2238 | } | ||
| 2239 | |||
| 2240 | /// ErrorUnionExpr <- SuffixExpr (EXCLAMATIONMARK TypeExpr)? | ||
| 2241 | fn parseErrorUnionExpr(p: *Parse) !Node.Index { | ||
| 2242 | const suffix_expr = try p.parseSuffixExpr(); | ||
| 2243 | if (suffix_expr == 0) return null_node; | ||
| 2244 | const bang = p.eatToken(.bang) orelse return suffix_expr; | ||
| 2245 | return p.addNode(.{ | ||
| 2246 | .tag = .error_union, | ||
| 2247 | .main_token = bang, | ||
| 2248 | .data = .{ | ||
| 2249 | .lhs = suffix_expr, | ||
| 2250 | .rhs = try p.expectTypeExpr(), | ||
| 2251 | }, | ||
| 2252 | }); | ||
| 2253 | } | ||
| 2254 | |||
| 2255 | /// SuffixExpr | ||
| 2256 | /// <- KEYWORD_async PrimaryTypeExpr SuffixOp* FnCallArguments | ||
| 2257 | /// / PrimaryTypeExpr (SuffixOp / FnCallArguments)* | ||
| 2258 | /// | ||
| 2259 | /// FnCallArguments <- LPAREN ExprList RPAREN | ||
| 2260 | /// | ||
| 2261 | /// ExprList <- (Expr COMMA)* Expr? | ||
| 2262 | fn parseSuffixExpr(p: *Parse) !Node.Index { | ||
| 2263 | if (p.eatToken(.keyword_async)) |_| { | ||
| 2264 | var res = try p.expectPrimaryTypeExpr(); | ||
| 2265 | while (true) { | ||
| 2266 | const node = try p.parseSuffixOp(res); | ||
| 2267 | if (node == 0) break; | ||
| 2268 | res = node; | ||
| 2269 | } | ||
| 2270 | const lparen = p.eatToken(.l_paren) orelse { | ||
| 2271 | try p.warn(.expected_param_list); | ||
| 2272 | return res; | ||
| 2273 | }; | ||
| 2274 | const scratch_top = p.scratch.items.len; | ||
| 2275 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 2276 | while (true) { | ||
| 2277 | if (p.eatToken(.r_paren)) |_| break; | ||
| 2278 | const param = try p.expectExpr(); | ||
| 2279 | try p.scratch.append(p.gpa, param); | ||
| 2280 | switch (p.token_tags[p.tok_i]) { | ||
| 2281 | .comma => p.tok_i += 1, | ||
| 2282 | .r_paren => { | ||
| 2283 | p.tok_i += 1; | ||
| 2284 | break; | ||
| 2285 | }, | ||
| 2286 | .colon, .r_brace, .r_bracket => return p.failExpected(.r_paren), | ||
| 2287 | // Likely just a missing comma; give error but continue parsing. | ||
| 2288 | else => try p.warn(.expected_comma_after_arg), | ||
| 2289 | } | ||
| 2290 | } | ||
| 2291 | const comma = (p.token_tags[p.tok_i - 2] == .comma); | ||
| 2292 | const params = p.scratch.items[scratch_top..]; | ||
| 2293 | switch (params.len) { | ||
| 2294 | 0 => return p.addNode(.{ | ||
| 2295 | .tag = if (comma) .async_call_one_comma else .async_call_one, | ||
| 2296 | .main_token = lparen, | ||
| 2297 | .data = .{ | ||
| 2298 | .lhs = res, | ||
| 2299 | .rhs = 0, | ||
| 2300 | }, | ||
| 2301 | }), | ||
| 2302 | 1 => return p.addNode(.{ | ||
| 2303 | .tag = if (comma) .async_call_one_comma else .async_call_one, | ||
| 2304 | .main_token = lparen, | ||
| 2305 | .data = .{ | ||
| 2306 | .lhs = res, | ||
| 2307 | .rhs = params[0], | ||
| 2308 | }, | ||
| 2309 | }), | ||
| 2310 | else => return p.addNode(.{ | ||
| 2311 | .tag = if (comma) .async_call_comma else .async_call, | ||
| 2312 | .main_token = lparen, | ||
| 2313 | .data = .{ | ||
| 2314 | .lhs = res, | ||
| 2315 | .rhs = try p.addExtra(try p.listToSpan(params)), | ||
| 2316 | }, | ||
| 2317 | }), | ||
| 2318 | } | ||
| 2319 | } | ||
| 2320 | |||
| 2321 | var res = try p.parsePrimaryTypeExpr(); | ||
| 2322 | if (res == 0) return res; | ||
| 2323 | while (true) { | ||
| 2324 | const suffix_op = try p.parseSuffixOp(res); | ||
| 2325 | if (suffix_op != 0) { | ||
| 2326 | res = suffix_op; | ||
| 2327 | continue; | ||
| 2328 | } | ||
| 2329 | const lparen = p.eatToken(.l_paren) orelse return res; | ||
| 2330 | const scratch_top = p.scratch.items.len; | ||
| 2331 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 2332 | while (true) { | ||
| 2333 | if (p.eatToken(.r_paren)) |_| break; | ||
| 2334 | const param = try p.expectExpr(); | ||
| 2335 | try p.scratch.append(p.gpa, param); | ||
| 2336 | switch (p.token_tags[p.tok_i]) { | ||
| 2337 | .comma => p.tok_i += 1, | ||
| 2338 | .r_paren => { | ||
| 2339 | p.tok_i += 1; | ||
| 2340 | break; | ||
| 2341 | }, | ||
| 2342 | .colon, .r_brace, .r_bracket => return p.failExpected(.r_paren), | ||
| 2343 | // Likely just a missing comma; give error but continue parsing. | ||
| 2344 | else => try p.warn(.expected_comma_after_arg), | ||
| 2345 | } | ||
| 2346 | } | ||
| 2347 | const comma = (p.token_tags[p.tok_i - 2] == .comma); | ||
| 2348 | const params = p.scratch.items[scratch_top..]; | ||
| 2349 | res = switch (params.len) { | ||
| 2350 | 0 => try p.addNode(.{ | ||
| 2351 | .tag = if (comma) .call_one_comma else .call_one, | ||
| 2352 | .main_token = lparen, | ||
| 2353 | .data = .{ | ||
| 2354 | .lhs = res, | ||
| 2355 | .rhs = 0, | ||
| 2356 | }, | ||
| 2357 | }), | ||
| 2358 | 1 => try p.addNode(.{ | ||
| 2359 | .tag = if (comma) .call_one_comma else .call_one, | ||
| 2360 | .main_token = lparen, | ||
| 2361 | .data = .{ | ||
| 2362 | .lhs = res, | ||
| 2363 | .rhs = params[0], | ||
| 2364 | }, | ||
| 2365 | }), | ||
| 2366 | else => try p.addNode(.{ | ||
| 2367 | .tag = if (comma) .call_comma else .call, | ||
| 2368 | .main_token = lparen, | ||
| 2369 | .data = .{ | ||
| 2370 | .lhs = res, | ||
| 2371 | .rhs = try p.addExtra(try p.listToSpan(params)), | ||
| 2372 | }, | ||
| 2373 | }), | ||
| 2374 | }; | ||
| 2375 | } | ||
| 2376 | } | ||
| 2377 | |||
| 2378 | /// PrimaryTypeExpr | ||
| 2379 | /// <- BUILTINIDENTIFIER FnCallArguments | ||
| 2380 | /// / CHAR_LITERAL | ||
| 2381 | /// / ContainerDecl | ||
| 2382 | /// / DOT IDENTIFIER | ||
| 2383 | /// / DOT InitList | ||
| 2384 | /// / ErrorSetDecl | ||
| 2385 | /// / FLOAT | ||
| 2386 | /// / FnProto | ||
| 2387 | /// / GroupedExpr | ||
| 2388 | /// / LabeledTypeExpr | ||
| 2389 | /// / IDENTIFIER | ||
| 2390 | /// / IfTypeExpr | ||
| 2391 | /// / INTEGER | ||
| 2392 | /// / KEYWORD_comptime TypeExpr | ||
| 2393 | /// / KEYWORD_error DOT IDENTIFIER | ||
| 2394 | /// / KEYWORD_anyframe | ||
| 2395 | /// / KEYWORD_unreachable | ||
| 2396 | /// / STRINGLITERAL | ||
| 2397 | /// / SwitchExpr | ||
| 2398 | /// | ||
| 2399 | /// ContainerDecl <- (KEYWORD_extern / KEYWORD_packed)? ContainerDeclAuto | ||
| 2400 | /// | ||
| 2401 | /// ContainerDeclAuto <- ContainerDeclType LBRACE container_doc_comment? ContainerMembers RBRACE | ||
| 2402 | /// | ||
| 2403 | /// InitList | ||
| 2404 | /// <- LBRACE FieldInit (COMMA FieldInit)* COMMA? RBRACE | ||
| 2405 | /// / LBRACE Expr (COMMA Expr)* COMMA? RBRACE | ||
| 2406 | /// / LBRACE RBRACE | ||
| 2407 | /// | ||
| 2408 | /// ErrorSetDecl <- KEYWORD_error LBRACE IdentifierList RBRACE | ||
| 2409 | /// | ||
| 2410 | /// GroupedExpr <- LPAREN Expr RPAREN | ||
| 2411 | /// | ||
| 2412 | /// IfTypeExpr <- IfPrefix TypeExpr (KEYWORD_else Payload? TypeExpr)? | ||
| 2413 | /// | ||
| 2414 | /// LabeledTypeExpr | ||
| 2415 | /// <- BlockLabel Block | ||
| 2416 | /// / BlockLabel? LoopTypeExpr | ||
| 2417 | /// | ||
| 2418 | /// LoopTypeExpr <- KEYWORD_inline? (ForTypeExpr / WhileTypeExpr) | ||
| 2419 | fn parsePrimaryTypeExpr(p: *Parse) !Node.Index { | ||
| 2420 | switch (p.token_tags[p.tok_i]) { | ||
| 2421 | .char_literal => return p.addNode(.{ | ||
| 2422 | .tag = .char_literal, | ||
| 2423 | .main_token = p.nextToken(), | ||
| 2424 | .data = .{ | ||
| 2425 | .lhs = undefined, | ||
| 2426 | .rhs = undefined, | ||
| 2427 | }, | ||
| 2428 | }), | ||
| 2429 | .number_literal => return p.addNode(.{ | ||
| 2430 | .tag = .number_literal, | ||
| 2431 | .main_token = p.nextToken(), | ||
| 2432 | .data = .{ | ||
| 2433 | .lhs = undefined, | ||
| 2434 | .rhs = undefined, | ||
| 2435 | }, | ||
| 2436 | }), | ||
| 2437 | .keyword_unreachable => return p.addNode(.{ | ||
| 2438 | .tag = .unreachable_literal, | ||
| 2439 | .main_token = p.nextToken(), | ||
| 2440 | .data = .{ | ||
| 2441 | .lhs = undefined, | ||
| 2442 | .rhs = undefined, | ||
| 2443 | }, | ||
| 2444 | }), | ||
| 2445 | .keyword_anyframe => return p.addNode(.{ | ||
| 2446 | .tag = .anyframe_literal, | ||
| 2447 | .main_token = p.nextToken(), | ||
| 2448 | .data = .{ | ||
| 2449 | .lhs = undefined, | ||
| 2450 | .rhs = undefined, | ||
| 2451 | }, | ||
| 2452 | }), | ||
| 2453 | .string_literal => { | ||
| 2454 | const main_token = p.nextToken(); | ||
| 2455 | return p.addNode(.{ | ||
| 2456 | .tag = .string_literal, | ||
| 2457 | .main_token = main_token, | ||
| 2458 | .data = .{ | ||
| 2459 | .lhs = undefined, | ||
| 2460 | .rhs = undefined, | ||
| 2461 | }, | ||
| 2462 | }); | ||
| 2463 | }, | ||
| 2464 | |||
| 2465 | .builtin => return p.parseBuiltinCall(), | ||
| 2466 | .keyword_fn => return p.parseFnProto(), | ||
| 2467 | .keyword_if => return p.parseIf(expectTypeExpr), | ||
| 2468 | .keyword_switch => return p.expectSwitchExpr(), | ||
| 2469 | |||
| 2470 | .keyword_extern, | ||
| 2471 | .keyword_packed, | ||
| 2472 | => { | ||
| 2473 | p.tok_i += 1; | ||
| 2474 | return p.parseContainerDeclAuto(); | ||
| 2475 | }, | ||
| 2476 | |||
| 2477 | .keyword_struct, | ||
| 2478 | .keyword_opaque, | ||
| 2479 | .keyword_enum, | ||
| 2480 | .keyword_union, | ||
| 2481 | => return p.parseContainerDeclAuto(), | ||
| 2482 | |||
| 2483 | .keyword_comptime => return p.addNode(.{ | ||
| 2484 | .tag = .@"comptime", | ||
| 2485 | .main_token = p.nextToken(), | ||
| 2486 | .data = .{ | ||
| 2487 | .lhs = try p.expectTypeExpr(), | ||
| 2488 | .rhs = undefined, | ||
| 2489 | }, | ||
| 2490 | }), | ||
| 2491 | .multiline_string_literal_line => { | ||
| 2492 | const first_line = p.nextToken(); | ||
| 2493 | while (p.token_tags[p.tok_i] == .multiline_string_literal_line) { | ||
| 2494 | p.tok_i += 1; | ||
| 2495 | } | ||
| 2496 | return p.addNode(.{ | ||
| 2497 | .tag = .multiline_string_literal, | ||
| 2498 | .main_token = first_line, | ||
| 2499 | .data = .{ | ||
| 2500 | .lhs = first_line, | ||
| 2501 | .rhs = p.tok_i - 1, | ||
| 2502 | }, | ||
| 2503 | }); | ||
| 2504 | }, | ||
| 2505 | .identifier => switch (p.token_tags[p.tok_i + 1]) { | ||
| 2506 | .colon => switch (p.token_tags[p.tok_i + 2]) { | ||
| 2507 | .keyword_inline => { | ||
| 2508 | p.tok_i += 3; | ||
| 2509 | switch (p.token_tags[p.tok_i]) { | ||
| 2510 | .keyword_for => return p.parseForTypeExpr(), | ||
| 2511 | .keyword_while => return p.parseWhileTypeExpr(), | ||
| 2512 | else => return p.fail(.expected_inlinable), | ||
| 2513 | } | ||
| 2514 | }, | ||
| 2515 | .keyword_for => { | ||
| 2516 | p.tok_i += 2; | ||
| 2517 | return p.parseForTypeExpr(); | ||
| 2518 | }, | ||
| 2519 | .keyword_while => { | ||
| 2520 | p.tok_i += 2; | ||
| 2521 | return p.parseWhileTypeExpr(); | ||
| 2522 | }, | ||
| 2523 | .l_brace => { | ||
| 2524 | p.tok_i += 2; | ||
| 2525 | return p.parseBlock(); | ||
| 2526 | }, | ||
| 2527 | else => return p.addNode(.{ | ||
| 2528 | .tag = .identifier, | ||
| 2529 | .main_token = p.nextToken(), | ||
| 2530 | .data = .{ | ||
| 2531 | .lhs = undefined, | ||
| 2532 | .rhs = undefined, | ||
| 2533 | }, | ||
| 2534 | }), | ||
| 2535 | }, | ||
| 2536 | else => return p.addNode(.{ | ||
| 2537 | .tag = .identifier, | ||
| 2538 | .main_token = p.nextToken(), | ||
| 2539 | .data = .{ | ||
| 2540 | .lhs = undefined, | ||
| 2541 | .rhs = undefined, | ||
| 2542 | }, | ||
| 2543 | }), | ||
| 2544 | }, | ||
| 2545 | .keyword_inline => { | ||
| 2546 | p.tok_i += 1; | ||
| 2547 | switch (p.token_tags[p.tok_i]) { | ||
| 2548 | .keyword_for => return p.parseForTypeExpr(), | ||
| 2549 | .keyword_while => return p.parseWhileTypeExpr(), | ||
| 2550 | else => return p.fail(.expected_inlinable), | ||
| 2551 | } | ||
| 2552 | }, | ||
| 2553 | .keyword_for => return p.parseForTypeExpr(), | ||
| 2554 | .keyword_while => return p.parseWhileTypeExpr(), | ||
| 2555 | .period => switch (p.token_tags[p.tok_i + 1]) { | ||
| 2556 | .identifier => return p.addNode(.{ | ||
| 2557 | .tag = .enum_literal, | ||
| 2558 | .data = .{ | ||
| 2559 | .lhs = p.nextToken(), // dot | ||
| 2560 | .rhs = undefined, | ||
| 2561 | }, | ||
| 2562 | .main_token = p.nextToken(), // identifier | ||
| 2563 | }), | ||
| 2564 | .l_brace => { | ||
| 2565 | const lbrace = p.tok_i + 1; | ||
| 2566 | p.tok_i = lbrace + 1; | ||
| 2567 | |||
| 2568 | // If there are 0, 1, or 2 items, we can use ArrayInitDotTwo/StructInitDotTwo; | ||
| 2569 | // otherwise we use the full ArrayInitDot/StructInitDot. | ||
| 2570 | |||
| 2571 | const scratch_top = p.scratch.items.len; | ||
| 2572 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 2573 | const field_init = try p.parseFieldInit(); | ||
| 2574 | if (field_init != 0) { | ||
| 2575 | try p.scratch.append(p.gpa, field_init); | ||
| 2576 | while (true) { | ||
| 2577 | switch (p.token_tags[p.tok_i]) { | ||
| 2578 | .comma => p.tok_i += 1, | ||
| 2579 | .r_brace => { | ||
| 2580 | p.tok_i += 1; | ||
| 2581 | break; | ||
| 2582 | }, | ||
| 2583 | .colon, .r_paren, .r_bracket => return p.failExpected(.r_brace), | ||
| 2584 | // Likely just a missing comma; give error but continue parsing. | ||
| 2585 | else => try p.warn(.expected_comma_after_initializer), | ||
| 2586 | } | ||
| 2587 | if (p.eatToken(.r_brace)) |_| break; | ||
| 2588 | const next = try p.expectFieldInit(); | ||
| 2589 | try p.scratch.append(p.gpa, next); | ||
| 2590 | } | ||
| 2591 | const comma = (p.token_tags[p.tok_i - 2] == .comma); | ||
| 2592 | const inits = p.scratch.items[scratch_top..]; | ||
| 2593 | switch (inits.len) { | ||
| 2594 | 0 => unreachable, | ||
| 2595 | 1 => return p.addNode(.{ | ||
| 2596 | .tag = if (comma) .struct_init_dot_two_comma else .struct_init_dot_two, | ||
| 2597 | .main_token = lbrace, | ||
| 2598 | .data = .{ | ||
| 2599 | .lhs = inits[0], | ||
| 2600 | .rhs = 0, | ||
| 2601 | }, | ||
| 2602 | }), | ||
| 2603 | 2 => return p.addNode(.{ | ||
| 2604 | .tag = if (comma) .struct_init_dot_two_comma else .struct_init_dot_two, | ||
| 2605 | .main_token = lbrace, | ||
| 2606 | .data = .{ | ||
| 2607 | .lhs = inits[0], | ||
| 2608 | .rhs = inits[1], | ||
| 2609 | }, | ||
| 2610 | }), | ||
| 2611 | else => { | ||
| 2612 | const span = try p.listToSpan(inits); | ||
| 2613 | return p.addNode(.{ | ||
| 2614 | .tag = if (comma) .struct_init_dot_comma else .struct_init_dot, | ||
| 2615 | .main_token = lbrace, | ||
| 2616 | .data = .{ | ||
| 2617 | .lhs = span.start, | ||
| 2618 | .rhs = span.end, | ||
| 2619 | }, | ||
| 2620 | }); | ||
| 2621 | }, | ||
| 2622 | } | ||
| 2623 | } | ||
| 2624 | |||
| 2625 | while (true) { | ||
| 2626 | if (p.eatToken(.r_brace)) |_| break; | ||
| 2627 | const elem_init = try p.expectExpr(); | ||
| 2628 | try p.scratch.append(p.gpa, elem_init); | ||
| 2629 | switch (p.token_tags[p.tok_i]) { | ||
| 2630 | .comma => p.tok_i += 1, | ||
| 2631 | .r_brace => { | ||
| 2632 | p.tok_i += 1; | ||
| 2633 | break; | ||
| 2634 | }, | ||
| 2635 | .colon, .r_paren, .r_bracket => return p.failExpected(.r_brace), | ||
| 2636 | // Likely just a missing comma; give error but continue parsing. | ||
| 2637 | else => try p.warn(.expected_comma_after_initializer), | ||
| 2638 | } | ||
| 2639 | } | ||
| 2640 | const comma = (p.token_tags[p.tok_i - 2] == .comma); | ||
| 2641 | const inits = p.scratch.items[scratch_top..]; | ||
| 2642 | switch (inits.len) { | ||
| 2643 | 0 => return p.addNode(.{ | ||
| 2644 | .tag = .struct_init_dot_two, | ||
| 2645 | .main_token = lbrace, | ||
| 2646 | .data = .{ | ||
| 2647 | .lhs = 0, | ||
| 2648 | .rhs = 0, | ||
| 2649 | }, | ||
| 2650 | }), | ||
| 2651 | 1 => return p.addNode(.{ | ||
| 2652 | .tag = if (comma) .array_init_dot_two_comma else .array_init_dot_two, | ||
| 2653 | .main_token = lbrace, | ||
| 2654 | .data = .{ | ||
| 2655 | .lhs = inits[0], | ||
| 2656 | .rhs = 0, | ||
| 2657 | }, | ||
| 2658 | }), | ||
| 2659 | 2 => return p.addNode(.{ | ||
| 2660 | .tag = if (comma) .array_init_dot_two_comma else .array_init_dot_two, | ||
| 2661 | .main_token = lbrace, | ||
| 2662 | .data = .{ | ||
| 2663 | .lhs = inits[0], | ||
| 2664 | .rhs = inits[1], | ||
| 2665 | }, | ||
| 2666 | }), | ||
| 2667 | else => { | ||
| 2668 | const span = try p.listToSpan(inits); | ||
| 2669 | return p.addNode(.{ | ||
| 2670 | .tag = if (comma) .array_init_dot_comma else .array_init_dot, | ||
| 2671 | .main_token = lbrace, | ||
| 2672 | .data = .{ | ||
| 2673 | .lhs = span.start, | ||
| 2674 | .rhs = span.end, | ||
| 2675 | }, | ||
| 2676 | }); | ||
| 2677 | }, | ||
| 2678 | } | ||
| 2679 | }, | ||
| 2680 | else => return null_node, | ||
| 2681 | }, | ||
| 2682 | .keyword_error => switch (p.token_tags[p.tok_i + 1]) { | ||
| 2683 | .l_brace => { | ||
| 2684 | const error_token = p.tok_i; | ||
| 2685 | p.tok_i += 2; | ||
| 2686 | while (true) { | ||
| 2687 | if (p.eatToken(.r_brace)) |_| break; | ||
| 2688 | _ = try p.eatDocComments(); | ||
| 2689 | _ = try p.expectToken(.identifier); | ||
| 2690 | switch (p.token_tags[p.tok_i]) { | ||
| 2691 | .comma => p.tok_i += 1, | ||
| 2692 | .r_brace => { | ||
| 2693 | p.tok_i += 1; | ||
| 2694 | break; | ||
| 2695 | }, | ||
| 2696 | .colon, .r_paren, .r_bracket => return p.failExpected(.r_brace), | ||
| 2697 | // Likely just a missing comma; give error but continue parsing. | ||
| 2698 | else => try p.warn(.expected_comma_after_field), | ||
| 2699 | } | ||
| 2700 | } | ||
| 2701 | return p.addNode(.{ | ||
| 2702 | .tag = .error_set_decl, | ||
| 2703 | .main_token = error_token, | ||
| 2704 | .data = .{ | ||
| 2705 | .lhs = undefined, | ||
| 2706 | .rhs = p.tok_i - 1, // rbrace | ||
| 2707 | }, | ||
| 2708 | }); | ||
| 2709 | }, | ||
| 2710 | else => { | ||
| 2711 | const main_token = p.nextToken(); | ||
| 2712 | const period = p.eatToken(.period); | ||
| 2713 | if (period == null) try p.warnExpected(.period); | ||
| 2714 | const identifier = p.eatToken(.identifier); | ||
| 2715 | if (identifier == null) try p.warnExpected(.identifier); | ||
| 2716 | return p.addNode(.{ | ||
| 2717 | .tag = .error_value, | ||
| 2718 | .main_token = main_token, | ||
| 2719 | .data = .{ | ||
| 2720 | .lhs = period orelse 0, | ||
| 2721 | .rhs = identifier orelse 0, | ||
| 2722 | }, | ||
| 2723 | }); | ||
| 2724 | }, | ||
| 2725 | }, | ||
| 2726 | .l_paren => return p.addNode(.{ | ||
| 2727 | .tag = .grouped_expression, | ||
| 2728 | .main_token = p.nextToken(), | ||
| 2729 | .data = .{ | ||
| 2730 | .lhs = try p.expectExpr(), | ||
| 2731 | .rhs = try p.expectToken(.r_paren), | ||
| 2732 | }, | ||
| 2733 | }), | ||
| 2734 | else => return null_node, | ||
| 2735 | } | ||
| 2736 | } | ||
| 2737 | |||
| 2738 | fn expectPrimaryTypeExpr(p: *Parse) !Node.Index { | ||
| 2739 | const node = try p.parsePrimaryTypeExpr(); | ||
| 2740 | if (node == 0) { | ||
| 2741 | return p.fail(.expected_primary_type_expr); | ||
| 2742 | } | ||
| 2743 | return node; | ||
| 2744 | } | ||
| 2745 | |||
| 2746 | /// ForPrefix <- KEYWORD_for LPAREN Expr RPAREN PtrIndexPayload | ||
| 2747 | /// | ||
| 2748 | /// ForTypeExpr <- ForPrefix TypeExpr (KEYWORD_else TypeExpr)? | ||
| 2749 | fn parseForTypeExpr(p: *Parse) !Node.Index { | ||
| 2750 | const for_token = p.eatToken(.keyword_for) orelse return null_node; | ||
| 2751 | _ = try p.expectToken(.l_paren); | ||
| 2752 | const array_expr = try p.expectExpr(); | ||
| 2753 | _ = try p.expectToken(.r_paren); | ||
| 2754 | const found_payload = try p.parsePtrIndexPayload(); | ||
| 2755 | if (found_payload == 0) try p.warn(.expected_loop_payload); | ||
| 2756 | |||
| 2757 | const then_expr = try p.expectTypeExpr(); | ||
| 2758 | _ = p.eatToken(.keyword_else) orelse { | ||
| 2759 | return p.addNode(.{ | ||
| 2760 | .tag = .for_simple, | ||
| 2761 | .main_token = for_token, | ||
| 2762 | .data = .{ | ||
| 2763 | .lhs = array_expr, | ||
| 2764 | .rhs = then_expr, | ||
| 2765 | }, | ||
| 2766 | }); | ||
| 2767 | }; | ||
| 2768 | const else_expr = try p.expectTypeExpr(); | ||
| 2769 | return p.addNode(.{ | ||
| 2770 | .tag = .@"for", | ||
| 2771 | .main_token = for_token, | ||
| 2772 | .data = .{ | ||
| 2773 | .lhs = array_expr, | ||
| 2774 | .rhs = try p.addExtra(Node.If{ | ||
| 2775 | .then_expr = then_expr, | ||
| 2776 | .else_expr = else_expr, | ||
| 2777 | }), | ||
| 2778 | }, | ||
| 2779 | }); | ||
| 2780 | } | ||
| 2781 | |||
| 2782 | /// WhilePrefix <- KEYWORD_while LPAREN Expr RPAREN PtrPayload? WhileContinueExpr? | ||
| 2783 | /// | ||
| 2784 | /// WhileTypeExpr <- WhilePrefix TypeExpr (KEYWORD_else Payload? TypeExpr)? | ||
| 2785 | fn parseWhileTypeExpr(p: *Parse) !Node.Index { | ||
| 2786 | const while_token = p.eatToken(.keyword_while) orelse return null_node; | ||
| 2787 | _ = try p.expectToken(.l_paren); | ||
| 2788 | const condition = try p.expectExpr(); | ||
| 2789 | _ = try p.expectToken(.r_paren); | ||
| 2790 | _ = try p.parsePtrPayload(); | ||
| 2791 | const cont_expr = try p.parseWhileContinueExpr(); | ||
| 2792 | |||
| 2793 | const then_expr = try p.expectTypeExpr(); | ||
| 2794 | _ = p.eatToken(.keyword_else) orelse { | ||
| 2795 | if (cont_expr == 0) { | ||
| 2796 | return p.addNode(.{ | ||
| 2797 | .tag = .while_simple, | ||
| 2798 | .main_token = while_token, | ||
| 2799 | .data = .{ | ||
| 2800 | .lhs = condition, | ||
| 2801 | .rhs = then_expr, | ||
| 2802 | }, | ||
| 2803 | }); | ||
| 2804 | } else { | ||
| 2805 | return p.addNode(.{ | ||
| 2806 | .tag = .while_cont, | ||
| 2807 | .main_token = while_token, | ||
| 2808 | .data = .{ | ||
| 2809 | .lhs = condition, | ||
| 2810 | .rhs = try p.addExtra(Node.WhileCont{ | ||
| 2811 | .cont_expr = cont_expr, | ||
| 2812 | .then_expr = then_expr, | ||
| 2813 | }), | ||
| 2814 | }, | ||
| 2815 | }); | ||
| 2816 | } | ||
| 2817 | }; | ||
| 2818 | _ = try p.parsePayload(); | ||
| 2819 | const else_expr = try p.expectTypeExpr(); | ||
| 2820 | return p.addNode(.{ | ||
| 2821 | .tag = .@"while", | ||
| 2822 | .main_token = while_token, | ||
| 2823 | .data = .{ | ||
| 2824 | .lhs = condition, | ||
| 2825 | .rhs = try p.addExtra(Node.While{ | ||
| 2826 | .cont_expr = cont_expr, | ||
| 2827 | .then_expr = then_expr, | ||
| 2828 | .else_expr = else_expr, | ||
| 2829 | }), | ||
| 2830 | }, | ||
| 2831 | }); | ||
| 2832 | } | ||
| 2833 | |||
| 2834 | /// SwitchExpr <- KEYWORD_switch LPAREN Expr RPAREN LBRACE SwitchProngList RBRACE | ||
| 2835 | fn expectSwitchExpr(p: *Parse) !Node.Index { | ||
| 2836 | const switch_token = p.assertToken(.keyword_switch); | ||
| 2837 | _ = try p.expectToken(.l_paren); | ||
| 2838 | const expr_node = try p.expectExpr(); | ||
| 2839 | _ = try p.expectToken(.r_paren); | ||
| 2840 | _ = try p.expectToken(.l_brace); | ||
| 2841 | const cases = try p.parseSwitchProngList(); | ||
| 2842 | const trailing_comma = p.token_tags[p.tok_i - 1] == .comma; | ||
| 2843 | _ = try p.expectToken(.r_brace); | ||
| 2844 | |||
| 2845 | return p.addNode(.{ | ||
| 2846 | .tag = if (trailing_comma) .switch_comma else .@"switch", | ||
| 2847 | .main_token = switch_token, | ||
| 2848 | .data = .{ | ||
| 2849 | .lhs = expr_node, | ||
| 2850 | .rhs = try p.addExtra(Node.SubRange{ | ||
| 2851 | .start = cases.start, | ||
| 2852 | .end = cases.end, | ||
| 2853 | }), | ||
| 2854 | }, | ||
| 2855 | }); | ||
| 2856 | } | ||
| 2857 | |||
| 2858 | /// AsmExpr <- KEYWORD_asm KEYWORD_volatile? LPAREN Expr AsmOutput? RPAREN | ||
| 2859 | /// | ||
| 2860 | /// AsmOutput <- COLON AsmOutputList AsmInput? | ||
| 2861 | /// | ||
| 2862 | /// AsmInput <- COLON AsmInputList AsmClobbers? | ||
| 2863 | /// | ||
| 2864 | /// AsmClobbers <- COLON StringList | ||
| 2865 | /// | ||
| 2866 | /// StringList <- (STRINGLITERAL COMMA)* STRINGLITERAL? | ||
| 2867 | /// | ||
| 2868 | /// AsmOutputList <- (AsmOutputItem COMMA)* AsmOutputItem? | ||
| 2869 | /// | ||
| 2870 | /// AsmInputList <- (AsmInputItem COMMA)* AsmInputItem? | ||
| 2871 | fn expectAsmExpr(p: *Parse) !Node.Index { | ||
| 2872 | const asm_token = p.assertToken(.keyword_asm); | ||
| 2873 | _ = p.eatToken(.keyword_volatile); | ||
| 2874 | _ = try p.expectToken(.l_paren); | ||
| 2875 | const template = try p.expectExpr(); | ||
| 2876 | |||
| 2877 | if (p.eatToken(.r_paren)) |rparen| { | ||
| 2878 | return p.addNode(.{ | ||
| 2879 | .tag = .asm_simple, | ||
| 2880 | .main_token = asm_token, | ||
| 2881 | .data = .{ | ||
| 2882 | .lhs = template, | ||
| 2883 | .rhs = rparen, | ||
| 2884 | }, | ||
| 2885 | }); | ||
| 2886 | } | ||
| 2887 | |||
| 2888 | _ = try p.expectToken(.colon); | ||
| 2889 | |||
| 2890 | const scratch_top = p.scratch.items.len; | ||
| 2891 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 2892 | |||
| 2893 | while (true) { | ||
| 2894 | const output_item = try p.parseAsmOutputItem(); | ||
| 2895 | if (output_item == 0) break; | ||
| 2896 | try p.scratch.append(p.gpa, output_item); | ||
| 2897 | switch (p.token_tags[p.tok_i]) { | ||
| 2898 | .comma => p.tok_i += 1, | ||
| 2899 | // All possible delimiters. | ||
| 2900 | .colon, .r_paren, .r_brace, .r_bracket => break, | ||
| 2901 | // Likely just a missing comma; give error but continue parsing. | ||
| 2902 | else => try p.warnExpected(.comma), | ||
| 2903 | } | ||
| 2904 | } | ||
| 2905 | if (p.eatToken(.colon)) |_| { | ||
| 2906 | while (true) { | ||
| 2907 | const input_item = try p.parseAsmInputItem(); | ||
| 2908 | if (input_item == 0) break; | ||
| 2909 | try p.scratch.append(p.gpa, input_item); | ||
| 2910 | switch (p.token_tags[p.tok_i]) { | ||
| 2911 | .comma => p.tok_i += 1, | ||
| 2912 | // All possible delimiters. | ||
| 2913 | .colon, .r_paren, .r_brace, .r_bracket => break, | ||
| 2914 | // Likely just a missing comma; give error but continue parsing. | ||
| 2915 | else => try p.warnExpected(.comma), | ||
| 2916 | } | ||
| 2917 | } | ||
| 2918 | if (p.eatToken(.colon)) |_| { | ||
| 2919 | while (p.eatToken(.string_literal)) |_| { | ||
| 2920 | switch (p.token_tags[p.tok_i]) { | ||
| 2921 | .comma => p.tok_i += 1, | ||
| 2922 | .colon, .r_paren, .r_brace, .r_bracket => break, | ||
| 2923 | // Likely just a missing comma; give error but continue parsing. | ||
| 2924 | else => try p.warnExpected(.comma), | ||
| 2925 | } | ||
| 2926 | } | ||
| 2927 | } | ||
| 2928 | } | ||
| 2929 | const rparen = try p.expectToken(.r_paren); | ||
| 2930 | const span = try p.listToSpan(p.scratch.items[scratch_top..]); | ||
| 2931 | return p.addNode(.{ | ||
| 2932 | .tag = .@"asm", | ||
| 2933 | .main_token = asm_token, | ||
| 2934 | .data = .{ | ||
| 2935 | .lhs = template, | ||
| 2936 | .rhs = try p.addExtra(Node.Asm{ | ||
| 2937 | .items_start = span.start, | ||
| 2938 | .items_end = span.end, | ||
| 2939 | .rparen = rparen, | ||
| 2940 | }), | ||
| 2941 | }, | ||
| 2942 | }); | ||
| 2943 | } | ||
| 2944 | |||
| 2945 | /// AsmOutputItem <- LBRACKET IDENTIFIER RBRACKET STRINGLITERAL LPAREN (MINUSRARROW TypeExpr / IDENTIFIER) RPAREN | ||
| 2946 | fn parseAsmOutputItem(p: *Parse) !Node.Index { | ||
| 2947 | _ = p.eatToken(.l_bracket) orelse return null_node; | ||
| 2948 | const identifier = try p.expectToken(.identifier); | ||
| 2949 | _ = try p.expectToken(.r_bracket); | ||
| 2950 | _ = try p.expectToken(.string_literal); | ||
| 2951 | _ = try p.expectToken(.l_paren); | ||
| 2952 | const type_expr: Node.Index = blk: { | ||
| 2953 | if (p.eatToken(.arrow)) |_| { | ||
| 2954 | break :blk try p.expectTypeExpr(); | ||
| 2955 | } else { | ||
| 2956 | _ = try p.expectToken(.identifier); | ||
| 2957 | break :blk null_node; | ||
| 2958 | } | ||
| 2959 | }; | ||
| 2960 | const rparen = try p.expectToken(.r_paren); | ||
| 2961 | return p.addNode(.{ | ||
| 2962 | .tag = .asm_output, | ||
| 2963 | .main_token = identifier, | ||
| 2964 | .data = .{ | ||
| 2965 | .lhs = type_expr, | ||
| 2966 | .rhs = rparen, | ||
| 2967 | }, | ||
| 2968 | }); | ||
| 2969 | } | ||
| 2970 | |||
| 2971 | /// AsmInputItem <- LBRACKET IDENTIFIER RBRACKET STRINGLITERAL LPAREN Expr RPAREN | ||
| 2972 | fn parseAsmInputItem(p: *Parse) !Node.Index { | ||
| 2973 | _ = p.eatToken(.l_bracket) orelse return null_node; | ||
| 2974 | const identifier = try p.expectToken(.identifier); | ||
| 2975 | _ = try p.expectToken(.r_bracket); | ||
| 2976 | _ = try p.expectToken(.string_literal); | ||
| 2977 | _ = try p.expectToken(.l_paren); | ||
| 2978 | const expr = try p.expectExpr(); | ||
| 2979 | const rparen = try p.expectToken(.r_paren); | ||
| 2980 | return p.addNode(.{ | ||
| 2981 | .tag = .asm_input, | ||
| 2982 | .main_token = identifier, | ||
| 2983 | .data = .{ | ||
| 2984 | .lhs = expr, | ||
| 2985 | .rhs = rparen, | ||
| 2986 | }, | ||
| 2987 | }); | ||
| 2988 | } | ||
| 2989 | |||
| 2990 | /// BreakLabel <- COLON IDENTIFIER | ||
| 2991 | fn parseBreakLabel(p: *Parse) !TokenIndex { | ||
| 2992 | _ = p.eatToken(.colon) orelse return @as(TokenIndex, 0); | ||
| 2993 | return p.expectToken(.identifier); | ||
| 2994 | } | ||
| 2995 | |||
| 2996 | /// BlockLabel <- IDENTIFIER COLON | ||
| 2997 | fn parseBlockLabel(p: *Parse) TokenIndex { | ||
| 2998 | if (p.token_tags[p.tok_i] == .identifier and | ||
| 2999 | p.token_tags[p.tok_i + 1] == .colon) | ||
| 3000 | { | ||
| 3001 | const identifier = p.tok_i; | ||
| 3002 | p.tok_i += 2; | ||
| 3003 | return identifier; | ||
| 3004 | } | ||
| 3005 | return null_node; | ||
| 3006 | } | ||
| 3007 | |||
| 3008 | /// FieldInit <- DOT IDENTIFIER EQUAL Expr | ||
| 3009 | fn parseFieldInit(p: *Parse) !Node.Index { | ||
| 3010 | if (p.token_tags[p.tok_i + 0] == .period and | ||
| 3011 | p.token_tags[p.tok_i + 1] == .identifier and | ||
| 3012 | p.token_tags[p.tok_i + 2] == .equal) | ||
| 3013 | { | ||
| 3014 | p.tok_i += 3; | ||
| 3015 | return p.expectExpr(); | ||
| 3016 | } else { | ||
| 3017 | return null_node; | ||
| 3018 | } | ||
| 3019 | } | ||
| 3020 | |||
| 3021 | fn expectFieldInit(p: *Parse) !Node.Index { | ||
| 3022 | if (p.token_tags[p.tok_i] != .period or | ||
| 3023 | p.token_tags[p.tok_i + 1] != .identifier or | ||
| 3024 | p.token_tags[p.tok_i + 2] != .equal) | ||
| 3025 | return p.fail(.expected_initializer); | ||
| 3026 | |||
| 3027 | p.tok_i += 3; | ||
| 3028 | return p.expectExpr(); | ||
| 3029 | } | ||
| 3030 | |||
| 3031 | /// WhileContinueExpr <- COLON LPAREN AssignExpr RPAREN | ||
| 3032 | fn parseWhileContinueExpr(p: *Parse) !Node.Index { | ||
| 3033 | _ = p.eatToken(.colon) orelse { | ||
| 3034 | if (p.token_tags[p.tok_i] == .l_paren and | ||
| 3035 | p.tokensOnSameLine(p.tok_i - 1, p.tok_i)) | ||
| 3036 | return p.fail(.expected_continue_expr); | ||
| 3037 | return null_node; | ||
| 3038 | }; | ||
| 3039 | _ = try p.expectToken(.l_paren); | ||
| 3040 | const node = try p.parseAssignExpr(); | ||
| 3041 | if (node == 0) return p.fail(.expected_expr_or_assignment); | ||
| 3042 | _ = try p.expectToken(.r_paren); | ||
| 3043 | return node; | ||
| 3044 | } | ||
| 3045 | |||
| 3046 | /// LinkSection <- KEYWORD_linksection LPAREN Expr RPAREN | ||
| 3047 | fn parseLinkSection(p: *Parse) !Node.Index { | ||
| 3048 | _ = p.eatToken(.keyword_linksection) orelse return null_node; | ||
| 3049 | _ = try p.expectToken(.l_paren); | ||
| 3050 | const expr_node = try p.expectExpr(); | ||
| 3051 | _ = try p.expectToken(.r_paren); | ||
| 3052 | return expr_node; | ||
| 3053 | } | ||
| 3054 | |||
| 3055 | /// CallConv <- KEYWORD_callconv LPAREN Expr RPAREN | ||
| 3056 | fn parseCallconv(p: *Parse) !Node.Index { | ||
| 3057 | _ = p.eatToken(.keyword_callconv) orelse return null_node; | ||
| 3058 | _ = try p.expectToken(.l_paren); | ||
| 3059 | const expr_node = try p.expectExpr(); | ||
| 3060 | _ = try p.expectToken(.r_paren); | ||
| 3061 | return expr_node; | ||
| 3062 | } | ||
| 3063 | |||
| 3064 | /// AddrSpace <- KEYWORD_addrspace LPAREN Expr RPAREN | ||
| 3065 | fn parseAddrSpace(p: *Parse) !Node.Index { | ||
| 3066 | _ = p.eatToken(.keyword_addrspace) orelse return null_node; | ||
| 3067 | _ = try p.expectToken(.l_paren); | ||
| 3068 | const expr_node = try p.expectExpr(); | ||
| 3069 | _ = try p.expectToken(.r_paren); | ||
| 3070 | return expr_node; | ||
| 3071 | } | ||
| 3072 | |||
| 3073 | /// This function can return null nodes and then still return nodes afterwards, | ||
| 3074 | /// such as in the case of anytype and `...`. Caller must look for rparen to find | ||
| 3075 | /// out when there are no more param decls left. | ||
| 3076 | /// | ||
| 3077 | /// ParamDecl | ||
| 3078 | /// <- doc_comment? (KEYWORD_noalias / KEYWORD_comptime)? (IDENTIFIER COLON)? ParamType | ||
| 3079 | /// / DOT3 | ||
| 3080 | /// | ||
| 3081 | /// ParamType | ||
| 3082 | /// <- KEYWORD_anytype | ||
| 3083 | /// / TypeExpr | ||
| 3084 | fn expectParamDecl(p: *Parse) !Node.Index { | ||
| 3085 | _ = try p.eatDocComments(); | ||
| 3086 | switch (p.token_tags[p.tok_i]) { | ||
| 3087 | .keyword_noalias, .keyword_comptime => p.tok_i += 1, | ||
| 3088 | .ellipsis3 => { | ||
| 3089 | p.tok_i += 1; | ||
| 3090 | return null_node; | ||
| 3091 | }, | ||
| 3092 | else => {}, | ||
| 3093 | } | ||
| 3094 | if (p.token_tags[p.tok_i] == .identifier and | ||
| 3095 | p.token_tags[p.tok_i + 1] == .colon) | ||
| 3096 | { | ||
| 3097 | p.tok_i += 2; | ||
| 3098 | } | ||
| 3099 | switch (p.token_tags[p.tok_i]) { | ||
| 3100 | .keyword_anytype => { | ||
| 3101 | p.tok_i += 1; | ||
| 3102 | return null_node; | ||
| 3103 | }, | ||
| 3104 | else => return p.expectTypeExpr(), | ||
| 3105 | } | ||
| 3106 | } | ||
| 3107 | |||
| 3108 | /// Payload <- PIPE IDENTIFIER PIPE | ||
| 3109 | fn parsePayload(p: *Parse) !TokenIndex { | ||
| 3110 | _ = p.eatToken(.pipe) orelse return @as(TokenIndex, 0); | ||
| 3111 | const identifier = try p.expectToken(.identifier); | ||
| 3112 | _ = try p.expectToken(.pipe); | ||
| 3113 | return identifier; | ||
| 3114 | } | ||
| 3115 | |||
| 3116 | /// PtrPayload <- PIPE ASTERISK? IDENTIFIER PIPE | ||
| 3117 | fn parsePtrPayload(p: *Parse) !TokenIndex { | ||
| 3118 | _ = p.eatToken(.pipe) orelse return @as(TokenIndex, 0); | ||
| 3119 | _ = p.eatToken(.asterisk); | ||
| 3120 | const identifier = try p.expectToken(.identifier); | ||
| 3121 | _ = try p.expectToken(.pipe); | ||
| 3122 | return identifier; | ||
| 3123 | } | ||
| 3124 | |||
| 3125 | /// Returns the first identifier token, if any. | ||
| 3126 | /// | ||
| 3127 | /// PtrIndexPayload <- PIPE ASTERISK? IDENTIFIER (COMMA IDENTIFIER)? PIPE | ||
| 3128 | fn parsePtrIndexPayload(p: *Parse) !TokenIndex { | ||
| 3129 | _ = p.eatToken(.pipe) orelse return @as(TokenIndex, 0); | ||
| 3130 | _ = p.eatToken(.asterisk); | ||
| 3131 | const identifier = try p.expectToken(.identifier); | ||
| 3132 | if (p.eatToken(.comma) != null) { | ||
| 3133 | _ = try p.expectToken(.identifier); | ||
| 3134 | } | ||
| 3135 | _ = try p.expectToken(.pipe); | ||
| 3136 | return identifier; | ||
| 3137 | } | ||
| 3138 | |||
| 3139 | /// SwitchProng <- KEYWORD_inline? SwitchCase EQUALRARROW PtrIndexPayload? AssignExpr | ||
| 3140 | /// | ||
| 3141 | /// SwitchCase | ||
| 3142 | /// <- SwitchItem (COMMA SwitchItem)* COMMA? | ||
| 3143 | /// / KEYWORD_else | ||
| 3144 | fn parseSwitchProng(p: *Parse) !Node.Index { | ||
| 3145 | const scratch_top = p.scratch.items.len; | ||
| 3146 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 3147 | |||
| 3148 | const is_inline = p.eatToken(.keyword_inline) != null; | ||
| 3149 | |||
| 3150 | if (p.eatToken(.keyword_else) == null) { | ||
| 3151 | while (true) { | ||
| 3152 | const item = try p.parseSwitchItem(); | ||
| 3153 | if (item == 0) break; | ||
| 3154 | try p.scratch.append(p.gpa, item); | ||
| 3155 | if (p.eatToken(.comma) == null) break; | ||
| 3156 | } | ||
| 3157 | if (scratch_top == p.scratch.items.len) { | ||
| 3158 | if (is_inline) p.tok_i -= 1; | ||
| 3159 | return null_node; | ||
| 3160 | } | ||
| 3161 | } | ||
| 3162 | const arrow_token = try p.expectToken(.equal_angle_bracket_right); | ||
| 3163 | _ = try p.parsePtrIndexPayload(); | ||
| 3164 | |||
| 3165 | const items = p.scratch.items[scratch_top..]; | ||
| 3166 | switch (items.len) { | ||
| 3167 | 0 => return p.addNode(.{ | ||
| 3168 | .tag = if (is_inline) .switch_case_inline_one else .switch_case_one, | ||
| 3169 | .main_token = arrow_token, | ||
| 3170 | .data = .{ | ||
| 3171 | .lhs = 0, | ||
| 3172 | .rhs = try p.expectAssignExpr(), | ||
| 3173 | }, | ||
| 3174 | }), | ||
| 3175 | 1 => return p.addNode(.{ | ||
| 3176 | .tag = if (is_inline) .switch_case_inline_one else .switch_case_one, | ||
| 3177 | .main_token = arrow_token, | ||
| 3178 | .data = .{ | ||
| 3179 | .lhs = items[0], | ||
| 3180 | .rhs = try p.expectAssignExpr(), | ||
| 3181 | }, | ||
| 3182 | }), | ||
| 3183 | else => return p.addNode(.{ | ||
| 3184 | .tag = if (is_inline) .switch_case_inline else .switch_case, | ||
| 3185 | .main_token = arrow_token, | ||
| 3186 | .data = .{ | ||
| 3187 | .lhs = try p.addExtra(try p.listToSpan(items)), | ||
| 3188 | .rhs = try p.expectAssignExpr(), | ||
| 3189 | }, | ||
| 3190 | }), | ||
| 3191 | } | ||
| 3192 | } | ||
| 3193 | |||
| 3194 | /// SwitchItem <- Expr (DOT3 Expr)? | ||
| 3195 | fn parseSwitchItem(p: *Parse) !Node.Index { | ||
| 3196 | const expr = try p.parseExpr(); | ||
| 3197 | if (expr == 0) return null_node; | ||
| 3198 | |||
| 3199 | if (p.eatToken(.ellipsis3)) |token| { | ||
| 3200 | return p.addNode(.{ | ||
| 3201 | .tag = .switch_range, | ||
| 3202 | .main_token = token, | ||
| 3203 | .data = .{ | ||
| 3204 | .lhs = expr, | ||
| 3205 | .rhs = try p.expectExpr(), | ||
| 3206 | }, | ||
| 3207 | }); | ||
| 3208 | } | ||
| 3209 | return expr; | ||
| 3210 | } | ||
| 3211 | |||
| 3212 | const PtrModifiers = struct { | ||
| 3213 | align_node: Node.Index, | ||
| 3214 | addrspace_node: Node.Index, | ||
| 3215 | bit_range_start: Node.Index, | ||
| 3216 | bit_range_end: Node.Index, | ||
| 3217 | }; | ||
| 3218 | |||
| 3219 | fn parsePtrModifiers(p: *Parse) !PtrModifiers { | ||
| 3220 | var result: PtrModifiers = .{ | ||
| 3221 | .align_node = 0, | ||
| 3222 | .addrspace_node = 0, | ||
| 3223 | .bit_range_start = 0, | ||
| 3224 | .bit_range_end = 0, | ||
| 3225 | }; | ||
| 3226 | var saw_const = false; | ||
| 3227 | var saw_volatile = false; | ||
| 3228 | var saw_allowzero = false; | ||
| 3229 | var saw_addrspace = false; | ||
| 3230 | while (true) { | ||
| 3231 | switch (p.token_tags[p.tok_i]) { | ||
| 3232 | .keyword_align => { | ||
| 3233 | if (result.align_node != 0) { | ||
| 3234 | try p.warn(.extra_align_qualifier); | ||
| 3235 | } | ||
| 3236 | p.tok_i += 1; | ||
| 3237 | _ = try p.expectToken(.l_paren); | ||
| 3238 | result.align_node = try p.expectExpr(); | ||
| 3239 | |||
| 3240 | if (p.eatToken(.colon)) |_| { | ||
| 3241 | result.bit_range_start = try p.expectExpr(); | ||
| 3242 | _ = try p.expectToken(.colon); | ||
| 3243 | result.bit_range_end = try p.expectExpr(); | ||
| 3244 | } | ||
| 3245 | |||
| 3246 | _ = try p.expectToken(.r_paren); | ||
| 3247 | }, | ||
| 3248 | .keyword_const => { | ||
| 3249 | if (saw_const) { | ||
| 3250 | try p.warn(.extra_const_qualifier); | ||
| 3251 | } | ||
| 3252 | p.tok_i += 1; | ||
| 3253 | saw_const = true; | ||
| 3254 | }, | ||
| 3255 | .keyword_volatile => { | ||
| 3256 | if (saw_volatile) { | ||
| 3257 | try p.warn(.extra_volatile_qualifier); | ||
| 3258 | } | ||
| 3259 | p.tok_i += 1; | ||
| 3260 | saw_volatile = true; | ||
| 3261 | }, | ||
| 3262 | .keyword_allowzero => { | ||
| 3263 | if (saw_allowzero) { | ||
| 3264 | try p.warn(.extra_allowzero_qualifier); | ||
| 3265 | } | ||
| 3266 | p.tok_i += 1; | ||
| 3267 | saw_allowzero = true; | ||
| 3268 | }, | ||
| 3269 | .keyword_addrspace => { | ||
| 3270 | if (saw_addrspace) { | ||
| 3271 | try p.warn(.extra_addrspace_qualifier); | ||
| 3272 | } | ||
| 3273 | result.addrspace_node = try p.parseAddrSpace(); | ||
| 3274 | }, | ||
| 3275 | else => return result, | ||
| 3276 | } | ||
| 3277 | } | ||
| 3278 | } | ||
| 3279 | |||
| 3280 | /// SuffixOp | ||
| 3281 | /// <- LBRACKET Expr (DOT2 (Expr? (COLON Expr)?)?)? RBRACKET | ||
| 3282 | /// / DOT IDENTIFIER | ||
| 3283 | /// / DOTASTERISK | ||
| 3284 | /// / DOTQUESTIONMARK | ||
| 3285 | fn parseSuffixOp(p: *Parse, lhs: Node.Index) !Node.Index { | ||
| 3286 | switch (p.token_tags[p.tok_i]) { | ||
| 3287 | .l_bracket => { | ||
| 3288 | const lbracket = p.nextToken(); | ||
| 3289 | const index_expr = try p.expectExpr(); | ||
| 3290 | |||
| 3291 | if (p.eatToken(.ellipsis2)) |_| { | ||
| 3292 | const end_expr = try p.parseExpr(); | ||
| 3293 | if (p.eatToken(.colon)) |_| { | ||
| 3294 | const sentinel = try p.expectExpr(); | ||
| 3295 | _ = try p.expectToken(.r_bracket); | ||
| 3296 | return p.addNode(.{ | ||
| 3297 | .tag = .slice_sentinel, | ||
| 3298 | .main_token = lbracket, | ||
| 3299 | .data = .{ | ||
| 3300 | .lhs = lhs, | ||
| 3301 | .rhs = try p.addExtra(Node.SliceSentinel{ | ||
| 3302 | .start = index_expr, | ||
| 3303 | .end = end_expr, | ||
| 3304 | .sentinel = sentinel, | ||
| 3305 | }), | ||
| 3306 | }, | ||
| 3307 | }); | ||
| 3308 | } | ||
| 3309 | _ = try p.expectToken(.r_bracket); | ||
| 3310 | if (end_expr == 0) { | ||
| 3311 | return p.addNode(.{ | ||
| 3312 | .tag = .slice_open, | ||
| 3313 | .main_token = lbracket, | ||
| 3314 | .data = .{ | ||
| 3315 | .lhs = lhs, | ||
| 3316 | .rhs = index_expr, | ||
| 3317 | }, | ||
| 3318 | }); | ||
| 3319 | } | ||
| 3320 | return p.addNode(.{ | ||
| 3321 | .tag = .slice, | ||
| 3322 | .main_token = lbracket, | ||
| 3323 | .data = .{ | ||
| 3324 | .lhs = lhs, | ||
| 3325 | .rhs = try p.addExtra(Node.Slice{ | ||
| 3326 | .start = index_expr, | ||
| 3327 | .end = end_expr, | ||
| 3328 | }), | ||
| 3329 | }, | ||
| 3330 | }); | ||
| 3331 | } | ||
| 3332 | _ = try p.expectToken(.r_bracket); | ||
| 3333 | return p.addNode(.{ | ||
| 3334 | .tag = .array_access, | ||
| 3335 | .main_token = lbracket, | ||
| 3336 | .data = .{ | ||
| 3337 | .lhs = lhs, | ||
| 3338 | .rhs = index_expr, | ||
| 3339 | }, | ||
| 3340 | }); | ||
| 3341 | }, | ||
| 3342 | .period_asterisk => return p.addNode(.{ | ||
| 3343 | .tag = .deref, | ||
| 3344 | .main_token = p.nextToken(), | ||
| 3345 | .data = .{ | ||
| 3346 | .lhs = lhs, | ||
| 3347 | .rhs = undefined, | ||
| 3348 | }, | ||
| 3349 | }), | ||
| 3350 | .invalid_periodasterisks => { | ||
| 3351 | try p.warn(.asterisk_after_ptr_deref); | ||
| 3352 | return p.addNode(.{ | ||
| 3353 | .tag = .deref, | ||
| 3354 | .main_token = p.nextToken(), | ||
| 3355 | .data = .{ | ||
| 3356 | .lhs = lhs, | ||
| 3357 | .rhs = undefined, | ||
| 3358 | }, | ||
| 3359 | }); | ||
| 3360 | }, | ||
| 3361 | .period => switch (p.token_tags[p.tok_i + 1]) { | ||
| 3362 | .identifier => return p.addNode(.{ | ||
| 3363 | .tag = .field_access, | ||
| 3364 | .main_token = p.nextToken(), | ||
| 3365 | .data = .{ | ||
| 3366 | .lhs = lhs, | ||
| 3367 | .rhs = p.nextToken(), | ||
| 3368 | }, | ||
| 3369 | }), | ||
| 3370 | .question_mark => return p.addNode(.{ | ||
| 3371 | .tag = .unwrap_optional, | ||
| 3372 | .main_token = p.nextToken(), | ||
| 3373 | .data = .{ | ||
| 3374 | .lhs = lhs, | ||
| 3375 | .rhs = p.nextToken(), | ||
| 3376 | }, | ||
| 3377 | }), | ||
| 3378 | .l_brace => { | ||
| 3379 | // this a misplaced `.{`, handle the error somewhere else | ||
| 3380 | return null_node; | ||
| 3381 | }, | ||
| 3382 | else => { | ||
| 3383 | p.tok_i += 1; | ||
| 3384 | try p.warn(.expected_suffix_op); | ||
| 3385 | return null_node; | ||
| 3386 | }, | ||
| 3387 | }, | ||
| 3388 | else => return null_node, | ||
| 3389 | } | ||
| 3390 | } | ||
| 3391 | |||
| 3392 | /// Caller must have already verified the first token. | ||
| 3393 | /// | ||
| 3394 | /// ContainerDeclAuto <- ContainerDeclType LBRACE container_doc_comment? ContainerMembers RBRACE | ||
| 3395 | /// | ||
| 3396 | /// ContainerDeclType | ||
| 3397 | /// <- KEYWORD_struct (LPAREN Expr RPAREN)? | ||
| 3398 | /// / KEYWORD_opaque | ||
| 3399 | /// / KEYWORD_enum (LPAREN Expr RPAREN)? | ||
| 3400 | /// / KEYWORD_union (LPAREN (KEYWORD_enum (LPAREN Expr RPAREN)? / Expr) RPAREN)? | ||
| 3401 | fn parseContainerDeclAuto(p: *Parse) !Node.Index { | ||
| 3402 | const main_token = p.nextToken(); | ||
| 3403 | const arg_expr = switch (p.token_tags[main_token]) { | ||
| 3404 | .keyword_opaque => null_node, | ||
| 3405 | .keyword_struct, .keyword_enum => blk: { | ||
| 3406 | if (p.eatToken(.l_paren)) |_| { | ||
| 3407 | const expr = try p.expectExpr(); | ||
| 3408 | _ = try p.expectToken(.r_paren); | ||
| 3409 | break :blk expr; | ||
| 3410 | } else { | ||
| 3411 | break :blk null_node; | ||
| 3412 | } | ||
| 3413 | }, | ||
| 3414 | .keyword_union => blk: { | ||
| 3415 | if (p.eatToken(.l_paren)) |_| { | ||
| 3416 | if (p.eatToken(.keyword_enum)) |_| { | ||
| 3417 | if (p.eatToken(.l_paren)) |_| { | ||
| 3418 | const enum_tag_expr = try p.expectExpr(); | ||
| 3419 | _ = try p.expectToken(.r_paren); | ||
| 3420 | _ = try p.expectToken(.r_paren); | ||
| 3421 | |||
| 3422 | _ = try p.expectToken(.l_brace); | ||
| 3423 | const members = try p.parseContainerMembers(); | ||
| 3424 | const members_span = try members.toSpan(p); | ||
| 3425 | _ = try p.expectToken(.r_brace); | ||
| 3426 | return p.addNode(.{ | ||
| 3427 | .tag = switch (members.trailing) { | ||
| 3428 | true => .tagged_union_enum_tag_trailing, | ||
| 3429 | false => .tagged_union_enum_tag, | ||
| 3430 | }, | ||
| 3431 | .main_token = main_token, | ||
| 3432 | .data = .{ | ||
| 3433 | .lhs = enum_tag_expr, | ||
| 3434 | .rhs = try p.addExtra(members_span), | ||
| 3435 | }, | ||
| 3436 | }); | ||
| 3437 | } else { | ||
| 3438 | _ = try p.expectToken(.r_paren); | ||
| 3439 | |||
| 3440 | _ = try p.expectToken(.l_brace); | ||
| 3441 | const members = try p.parseContainerMembers(); | ||
| 3442 | _ = try p.expectToken(.r_brace); | ||
| 3443 | if (members.len <= 2) { | ||
| 3444 | return p.addNode(.{ | ||
| 3445 | .tag = switch (members.trailing) { | ||
| 3446 | true => .tagged_union_two_trailing, | ||
| 3447 | false => .tagged_union_two, | ||
| 3448 | }, | ||
| 3449 | .main_token = main_token, | ||
| 3450 | .data = .{ | ||
| 3451 | .lhs = members.lhs, | ||
| 3452 | .rhs = members.rhs, | ||
| 3453 | }, | ||
| 3454 | }); | ||
| 3455 | } else { | ||
| 3456 | const span = try members.toSpan(p); | ||
| 3457 | return p.addNode(.{ | ||
| 3458 | .tag = switch (members.trailing) { | ||
| 3459 | true => .tagged_union_trailing, | ||
| 3460 | false => .tagged_union, | ||
| 3461 | }, | ||
| 3462 | .main_token = main_token, | ||
| 3463 | .data = .{ | ||
| 3464 | .lhs = span.start, | ||
| 3465 | .rhs = span.end, | ||
| 3466 | }, | ||
| 3467 | }); | ||
| 3468 | } | ||
| 3469 | } | ||
| 3470 | } else { | ||
| 3471 | const expr = try p.expectExpr(); | ||
| 3472 | _ = try p.expectToken(.r_paren); | ||
| 3473 | break :blk expr; | ||
| 3474 | } | ||
| 3475 | } else { | ||
| 3476 | break :blk null_node; | ||
| 3477 | } | ||
| 3478 | }, | ||
| 3479 | else => { | ||
| 3480 | p.tok_i -= 1; | ||
| 3481 | return p.fail(.expected_container); | ||
| 3482 | }, | ||
| 3483 | }; | ||
| 3484 | _ = try p.expectToken(.l_brace); | ||
| 3485 | const members = try p.parseContainerMembers(); | ||
| 3486 | _ = try p.expectToken(.r_brace); | ||
| 3487 | if (arg_expr == 0) { | ||
| 3488 | if (members.len <= 2) { | ||
| 3489 | return p.addNode(.{ | ||
| 3490 | .tag = switch (members.trailing) { | ||
| 3491 | true => .container_decl_two_trailing, | ||
| 3492 | false => .container_decl_two, | ||
| 3493 | }, | ||
| 3494 | .main_token = main_token, | ||
| 3495 | .data = .{ | ||
| 3496 | .lhs = members.lhs, | ||
| 3497 | .rhs = members.rhs, | ||
| 3498 | }, | ||
| 3499 | }); | ||
| 3500 | } else { | ||
| 3501 | const span = try members.toSpan(p); | ||
| 3502 | return p.addNode(.{ | ||
| 3503 | .tag = switch (members.trailing) { | ||
| 3504 | true => .container_decl_trailing, | ||
| 3505 | false => .container_decl, | ||
| 3506 | }, | ||
| 3507 | .main_token = main_token, | ||
| 3508 | .data = .{ | ||
| 3509 | .lhs = span.start, | ||
| 3510 | .rhs = span.end, | ||
| 3511 | }, | ||
| 3512 | }); | ||
| 3513 | } | ||
| 3514 | } else { | ||
| 3515 | const span = try members.toSpan(p); | ||
| 3516 | return p.addNode(.{ | ||
| 3517 | .tag = switch (members.trailing) { | ||
| 3518 | true => .container_decl_arg_trailing, | ||
| 3519 | false => .container_decl_arg, | ||
| 3520 | }, | ||
| 3521 | .main_token = main_token, | ||
| 3522 | .data = .{ | ||
| 3523 | .lhs = arg_expr, | ||
| 3524 | .rhs = try p.addExtra(Node.SubRange{ | ||
| 3525 | .start = span.start, | ||
| 3526 | .end = span.end, | ||
| 3527 | }), | ||
| 3528 | }, | ||
| 3529 | }); | ||
| 3530 | } | ||
| 3531 | } | ||
| 3532 | |||
| 3533 | /// Give a helpful error message for those transitioning from | ||
| 3534 | /// C's 'struct Foo {};' to Zig's 'const Foo = struct {};'. | ||
| 3535 | fn parseCStyleContainer(p: *Parse) Error!bool { | ||
| 3536 | const main_token = p.tok_i; | ||
| 3537 | switch (p.token_tags[p.tok_i]) { | ||
| 3538 | .keyword_enum, .keyword_union, .keyword_struct => {}, | ||
| 3539 | else => return false, | ||
| 3540 | } | ||
| 3541 | const identifier = p.tok_i + 1; | ||
| 3542 | if (p.token_tags[identifier] != .identifier) return false; | ||
| 3543 | p.tok_i += 2; | ||
| 3544 | |||
| 3545 | try p.warnMsg(.{ | ||
| 3546 | .tag = .c_style_container, | ||
| 3547 | .token = identifier, | ||
| 3548 | .extra = .{ .expected_tag = p.token_tags[main_token] }, | ||
| 3549 | }); | ||
| 3550 | try p.warnMsg(.{ | ||
| 3551 | .tag = .zig_style_container, | ||
| 3552 | .is_note = true, | ||
| 3553 | .token = identifier, | ||
| 3554 | .extra = .{ .expected_tag = p.token_tags[main_token] }, | ||
| 3555 | }); | ||
| 3556 | |||
| 3557 | _ = try p.expectToken(.l_brace); | ||
| 3558 | _ = try p.parseContainerMembers(); | ||
| 3559 | _ = try p.expectToken(.r_brace); | ||
| 3560 | try p.expectSemicolon(.expected_semi_after_decl, true); | ||
| 3561 | return true; | ||
| 3562 | } | ||
| 3563 | |||
| 3564 | /// Holds temporary data until we are ready to construct the full ContainerDecl AST node. | ||
| 3565 | /// | ||
| 3566 | /// ByteAlign <- KEYWORD_align LPAREN Expr RPAREN | ||
| 3567 | fn parseByteAlign(p: *Parse) !Node.Index { | ||
| 3568 | _ = p.eatToken(.keyword_align) orelse return null_node; | ||
| 3569 | _ = try p.expectToken(.l_paren); | ||
| 3570 | const expr = try p.expectExpr(); | ||
| 3571 | _ = try p.expectToken(.r_paren); | ||
| 3572 | return expr; | ||
| 3573 | } | ||
| 3574 | |||
| 3575 | /// SwitchProngList <- (SwitchProng COMMA)* SwitchProng? | ||
| 3576 | fn parseSwitchProngList(p: *Parse) !Node.SubRange { | ||
| 3577 | const scratch_top = p.scratch.items.len; | ||
| 3578 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 3579 | |||
| 3580 | while (true) { | ||
| 3581 | const item = try parseSwitchProng(p); | ||
| 3582 | if (item == 0) break; | ||
| 3583 | |||
| 3584 | try p.scratch.append(p.gpa, item); | ||
| 3585 | |||
| 3586 | switch (p.token_tags[p.tok_i]) { | ||
| 3587 | .comma => p.tok_i += 1, | ||
| 3588 | // All possible delimiters. | ||
| 3589 | .colon, .r_paren, .r_brace, .r_bracket => break, | ||
| 3590 | // Likely just a missing comma; give error but continue parsing. | ||
| 3591 | else => try p.warn(.expected_comma_after_switch_prong), | ||
| 3592 | } | ||
| 3593 | } | ||
| 3594 | return p.listToSpan(p.scratch.items[scratch_top..]); | ||
| 3595 | } | ||
| 3596 | |||
| 3597 | /// ParamDeclList <- (ParamDecl COMMA)* ParamDecl? | ||
| 3598 | fn parseParamDeclList(p: *Parse) !SmallSpan { | ||
| 3599 | _ = try p.expectToken(.l_paren); | ||
| 3600 | const scratch_top = p.scratch.items.len; | ||
| 3601 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 3602 | var varargs: union(enum) { none, seen, nonfinal: TokenIndex } = .none; | ||
| 3603 | while (true) { | ||
| 3604 | if (p.eatToken(.r_paren)) |_| break; | ||
| 3605 | if (varargs == .seen) varargs = .{ .nonfinal = p.tok_i }; | ||
| 3606 | const param = try p.expectParamDecl(); | ||
| 3607 | if (param != 0) { | ||
| 3608 | try p.scratch.append(p.gpa, param); | ||
| 3609 | } else if (p.token_tags[p.tok_i - 1] == .ellipsis3) { | ||
| 3610 | if (varargs == .none) varargs = .seen; | ||
| 3611 | } | ||
| 3612 | switch (p.token_tags[p.tok_i]) { | ||
| 3613 | .comma => p.tok_i += 1, | ||
| 3614 | .r_paren => { | ||
| 3615 | p.tok_i += 1; | ||
| 3616 | break; | ||
| 3617 | }, | ||
| 3618 | .colon, .r_brace, .r_bracket => return p.failExpected(.r_paren), | ||
| 3619 | // Likely just a missing comma; give error but continue parsing. | ||
| 3620 | else => try p.warn(.expected_comma_after_param), | ||
| 3621 | } | ||
| 3622 | } | ||
| 3623 | if (varargs == .nonfinal) { | ||
| 3624 | try p.warnMsg(.{ .tag = .varargs_nonfinal, .token = varargs.nonfinal }); | ||
| 3625 | } | ||
| 3626 | const params = p.scratch.items[scratch_top..]; | ||
| 3627 | return switch (params.len) { | ||
| 3628 | 0 => SmallSpan{ .zero_or_one = 0 }, | ||
| 3629 | 1 => SmallSpan{ .zero_or_one = params[0] }, | ||
| 3630 | else => SmallSpan{ .multi = try p.listToSpan(params) }, | ||
| 3631 | }; | ||
| 3632 | } | ||
| 3633 | |||
| 3634 | /// FnCallArguments <- LPAREN ExprList RPAREN | ||
| 3635 | /// | ||
| 3636 | /// ExprList <- (Expr COMMA)* Expr? | ||
| 3637 | fn parseBuiltinCall(p: *Parse) !Node.Index { | ||
| 3638 | const builtin_token = p.assertToken(.builtin); | ||
| 3639 | if (p.token_tags[p.nextToken()] != .l_paren) { | ||
| 3640 | p.tok_i -= 1; | ||
| 3641 | try p.warn(.expected_param_list); | ||
| 3642 | // Pretend this was an identifier so we can continue parsing. | ||
| 3643 | return p.addNode(.{ | ||
| 3644 | .tag = .identifier, | ||
| 3645 | .main_token = builtin_token, | ||
| 3646 | .data = .{ | ||
| 3647 | .lhs = undefined, | ||
| 3648 | .rhs = undefined, | ||
| 3649 | }, | ||
| 3650 | }); | ||
| 3651 | } | ||
| 3652 | const scratch_top = p.scratch.items.len; | ||
| 3653 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 3654 | while (true) { | ||
| 3655 | if (p.eatToken(.r_paren)) |_| break; | ||
| 3656 | const param = try p.expectExpr(); | ||
| 3657 | try p.scratch.append(p.gpa, param); | ||
| 3658 | switch (p.token_tags[p.tok_i]) { | ||
| 3659 | .comma => p.tok_i += 1, | ||
| 3660 | .r_paren => { | ||
| 3661 | p.tok_i += 1; | ||
| 3662 | break; | ||
| 3663 | }, | ||
| 3664 | // Likely just a missing comma; give error but continue parsing. | ||
| 3665 | else => try p.warn(.expected_comma_after_arg), | ||
| 3666 | } | ||
| 3667 | } | ||
| 3668 | const comma = (p.token_tags[p.tok_i - 2] == .comma); | ||
| 3669 | const params = p.scratch.items[scratch_top..]; | ||
| 3670 | switch (params.len) { | ||
| 3671 | 0 => return p.addNode(.{ | ||
| 3672 | .tag = .builtin_call_two, | ||
| 3673 | .main_token = builtin_token, | ||
| 3674 | .data = .{ | ||
| 3675 | .lhs = 0, | ||
| 3676 | .rhs = 0, | ||
| 3677 | }, | ||
| 3678 | }), | ||
| 3679 | 1 => return p.addNode(.{ | ||
| 3680 | .tag = if (comma) .builtin_call_two_comma else .builtin_call_two, | ||
| 3681 | .main_token = builtin_token, | ||
| 3682 | .data = .{ | ||
| 3683 | .lhs = params[0], | ||
| 3684 | .rhs = 0, | ||
| 3685 | }, | ||
| 3686 | }), | ||
| 3687 | 2 => return p.addNode(.{ | ||
| 3688 | .tag = if (comma) .builtin_call_two_comma else .builtin_call_two, | ||
| 3689 | .main_token = builtin_token, | ||
| 3690 | .data = .{ | ||
| 3691 | .lhs = params[0], | ||
| 3692 | .rhs = params[1], | ||
| 3693 | }, | ||
| 3694 | }), | ||
| 3695 | else => { | ||
| 3696 | const span = try p.listToSpan(params); | ||
| 3697 | return p.addNode(.{ | ||
| 3698 | .tag = if (comma) .builtin_call_comma else .builtin_call, | ||
| 3699 | .main_token = builtin_token, | ||
| 3700 | .data = .{ | ||
| 3701 | .lhs = span.start, | ||
| 3702 | .rhs = span.end, | ||
| 3703 | }, | ||
| 3704 | }); | ||
| 3705 | }, | ||
| 3706 | } | ||
| 3707 | } | ||
| 3708 | |||
| 3709 | /// IfPrefix <- KEYWORD_if LPAREN Expr RPAREN PtrPayload? | ||
| 3710 | fn parseIf(p: *Parse, comptime bodyParseFn: fn (p: *Parse) Error!Node.Index) !Node.Index { | ||
| 3711 | const if_token = p.eatToken(.keyword_if) orelse return null_node; | ||
| 3712 | _ = try p.expectToken(.l_paren); | ||
| 3713 | const condition = try p.expectExpr(); | ||
| 3714 | _ = try p.expectToken(.r_paren); | ||
| 3715 | _ = try p.parsePtrPayload(); | ||
| 3716 | |||
| 3717 | const then_expr = try bodyParseFn(p); | ||
| 3718 | assert(then_expr != 0); | ||
| 3719 | |||
| 3720 | _ = p.eatToken(.keyword_else) orelse return p.addNode(.{ | ||
| 3721 | .tag = .if_simple, | ||
| 3722 | .main_token = if_token, | ||
| 3723 | .data = .{ | ||
| 3724 | .lhs = condition, | ||
| 3725 | .rhs = then_expr, | ||
| 3726 | }, | ||
| 3727 | }); | ||
| 3728 | _ = try p.parsePayload(); | ||
| 3729 | const else_expr = try bodyParseFn(p); | ||
| 3730 | assert(then_expr != 0); | ||
| 3731 | |||
| 3732 | return p.addNode(.{ | ||
| 3733 | .tag = .@"if", | ||
| 3734 | .main_token = if_token, | ||
| 3735 | .data = .{ | ||
| 3736 | .lhs = condition, | ||
| 3737 | .rhs = try p.addExtra(Node.If{ | ||
| 3738 | .then_expr = then_expr, | ||
| 3739 | .else_expr = else_expr, | ||
| 3740 | }), | ||
| 3741 | }, | ||
| 3742 | }); | ||
| 3743 | } | ||
| 3744 | |||
| 3745 | /// Skips over doc comment tokens. Returns the first one, if any. | ||
| 3746 | fn eatDocComments(p: *Parse) !?TokenIndex { | ||
| 3747 | if (p.eatToken(.doc_comment)) |tok| { | ||
| 3748 | var first_line = tok; | ||
| 3749 | if (tok > 0 and tokensOnSameLine(p, tok - 1, tok)) { | ||
| 3750 | try p.warnMsg(.{ | ||
| 3751 | .tag = .same_line_doc_comment, | ||
| 3752 | .token = tok, | ||
| 3753 | }); | ||
| 3754 | first_line = p.eatToken(.doc_comment) orelse return null; | ||
| 3755 | } | ||
| 3756 | while (p.eatToken(.doc_comment)) |_| {} | ||
| 3757 | return first_line; | ||
| 3758 | } | ||
| 3759 | return null; | ||
| 3760 | } | ||
| 3761 | |||
| 3762 | fn tokensOnSameLine(p: *Parse, token1: TokenIndex, token2: TokenIndex) bool { | ||
| 3763 | return std.mem.indexOfScalar(u8, p.source[p.token_starts[token1]..p.token_starts[token2]], '\n') == null; | ||
| 3764 | } | ||
| 3765 | |||
| 3766 | fn eatToken(p: *Parse, tag: Token.Tag) ?TokenIndex { | ||
| 3767 | return if (p.token_tags[p.tok_i] == tag) p.nextToken() else null; | ||
| 3768 | } | ||
| 3769 | |||
| 3770 | fn assertToken(p: *Parse, tag: Token.Tag) TokenIndex { | ||
| 3771 | const token = p.nextToken(); | ||
| 3772 | assert(p.token_tags[token] == tag); | ||
| 3773 | return token; | ||
| 3774 | } | ||
| 3775 | |||
| 3776 | fn expectToken(p: *Parse, tag: Token.Tag) Error!TokenIndex { | ||
| 3777 | if (p.token_tags[p.tok_i] != tag) { | ||
| 3778 | return p.failMsg(.{ | ||
| 3779 | .tag = .expected_token, | ||
| 3780 | .token = p.tok_i, | ||
| 3781 | .extra = .{ .expected_tag = tag }, | ||
| 3782 | }); | ||
| 3783 | } | ||
| 3784 | return p.nextToken(); | ||
| 3785 | } | ||
| 3786 | |||
| 3787 | fn expectSemicolon(p: *Parse, error_tag: AstError.Tag, recoverable: bool) Error!void { | ||
| 3788 | if (p.token_tags[p.tok_i] == .semicolon) { | ||
| 3789 | _ = p.nextToken(); | ||
| 3790 | return; | ||
| 3791 | } | ||
| 3792 | try p.warn(error_tag); | ||
| 3793 | if (!recoverable) return error.ParseError; | ||
| 3794 | } | ||
| 3795 | |||
| 3796 | fn nextToken(p: *Parse) TokenIndex { | ||
| 3797 | const result = p.tok_i; | ||
| 3798 | p.tok_i += 1; | ||
| 3799 | return result; | ||
| 3800 | } | ||
| 3801 | |||
| 3802 | const null_node: Node.Index = 0; | ||
| 3803 | |||
| 3804 | const Parse = @This(); | ||
| 3805 | const std = @import("../std.zig"); | ||
| 3806 | const assert = std.debug.assert; | ||
| 3807 | const Allocator = std.mem.Allocator; | ||
| 3808 | const Ast = std.zig.Ast; | ||
| 3809 | const Node = Ast.Node; | ||
| 3810 | const AstError = Ast.Error; | ||
| 3811 | const TokenIndex = Ast.TokenIndex; | ||
| 3812 | const Token = std.zig.Token; | ||
| 3813 | |||
| 3814 | test { | ||
| 3815 | _ = @import("parser_test.zig"); | ||
| 3816 | } | ||
lib/std/zig/parse.zig deleted-3852| ... | @@ -1,3852 +0,0 @@ | ||
| 1 | const std = @import("../std.zig"); | ||
| 2 | const assert = std.debug.assert; | ||
| 3 | const Allocator = std.mem.Allocator; | ||
| 4 | const Ast = std.zig.Ast; | ||
| 5 | const Node = Ast.Node; | ||
| 6 | const AstError = Ast.Error; | ||
| 7 | const TokenIndex = Ast.TokenIndex; | ||
| 8 | const Token = std.zig.Token; | ||
| 9 | |||
| 10 | pub const Error = error{ParseError} || Allocator.Error; | ||
| 11 | |||
| 12 | /// Result should be freed with tree.deinit() when there are | ||
| 13 | /// no more references to any of the tokens or nodes. | ||
| 14 | pub fn parse(gpa: Allocator, source: [:0]const u8) Allocator.Error!Ast { | ||
| 15 | var tokens = Ast.TokenList{}; | ||
| 16 | defer tokens.deinit(gpa); | ||
| 17 | |||
| 18 | // Empirically, the zig std lib has an 8:1 ratio of source bytes to token count. | ||
| 19 | const estimated_token_count = source.len / 8; | ||
| 20 | try tokens.ensureTotalCapacity(gpa, estimated_token_count); | ||
| 21 | |||
| 22 | var tokenizer = std.zig.Tokenizer.init(source); | ||
| 23 | while (true) { | ||
| 24 | const token = tokenizer.next(); | ||
| 25 | try tokens.append(gpa, .{ | ||
| 26 | .tag = token.tag, | ||
| 27 | .start = @intCast(u32, token.loc.start), | ||
| 28 | }); | ||
| 29 | if (token.tag == .eof) break; | ||
| 30 | } | ||
| 31 | |||
| 32 | var parser: Parser = .{ | ||
| 33 | .source = source, | ||
| 34 | .gpa = gpa, | ||
| 35 | .token_tags = tokens.items(.tag), | ||
| 36 | .token_starts = tokens.items(.start), | ||
| 37 | .errors = .{}, | ||
| 38 | .nodes = .{}, | ||
| 39 | .extra_data = .{}, | ||
| 40 | .scratch = .{}, | ||
| 41 | .tok_i = 0, | ||
| 42 | }; | ||
| 43 | defer parser.errors.deinit(gpa); | ||
| 44 | defer parser.nodes.deinit(gpa); | ||
| 45 | defer parser.extra_data.deinit(gpa); | ||
| 46 | defer parser.scratch.deinit(gpa); | ||
| 47 | |||
| 48 | // Empirically, Zig source code has a 2:1 ratio of tokens to AST nodes. | ||
| 49 | // Make sure at least 1 so we can use appendAssumeCapacity on the root node below. | ||
| 50 | const estimated_node_count = (tokens.len + 2) / 2; | ||
| 51 | try parser.nodes.ensureTotalCapacity(gpa, estimated_node_count); | ||
| 52 | |||
| 53 | try parser.parseRoot(); | ||
| 54 | |||
| 55 | // TODO experiment with compacting the MultiArrayList slices here | ||
| 56 | return Ast{ | ||
| 57 | .source = source, | ||
| 58 | .tokens = tokens.toOwnedSlice(), | ||
| 59 | .nodes = parser.nodes.toOwnedSlice(), | ||
| 60 | .extra_data = try parser.extra_data.toOwnedSlice(gpa), | ||
| 61 | .errors = try parser.errors.toOwnedSlice(gpa), | ||
| 62 | }; | ||
| 63 | } | ||
| 64 | |||
| 65 | const null_node: Node.Index = 0; | ||
| 66 | |||
| 67 | /// Represents in-progress parsing, will be converted to an Ast after completion. | ||
| 68 | const Parser = struct { | ||
| 69 | gpa: Allocator, | ||
| 70 | source: []const u8, | ||
| 71 | token_tags: []const Token.Tag, | ||
| 72 | token_starts: []const Ast.ByteOffset, | ||
| 73 | tok_i: TokenIndex, | ||
| 74 | errors: std.ArrayListUnmanaged(AstError), | ||
| 75 | nodes: Ast.NodeList, | ||
| 76 | extra_data: std.ArrayListUnmanaged(Node.Index), | ||
| 77 | scratch: std.ArrayListUnmanaged(Node.Index), | ||
| 78 | |||
| 79 | const SmallSpan = union(enum) { | ||
| 80 | zero_or_one: Node.Index, | ||
| 81 | multi: Node.SubRange, | ||
| 82 | }; | ||
| 83 | |||
| 84 | const Members = struct { | ||
| 85 | len: usize, | ||
| 86 | lhs: Node.Index, | ||
| 87 | rhs: Node.Index, | ||
| 88 | trailing: bool, | ||
| 89 | |||
| 90 | fn toSpan(self: Members, p: *Parser) !Node.SubRange { | ||
| 91 | if (self.len <= 2) { | ||
| 92 | const nodes = [2]Node.Index{ self.lhs, self.rhs }; | ||
| 93 | return p.listToSpan(nodes[0..self.len]); | ||
| 94 | } else { | ||
| 95 | return Node.SubRange{ .start = self.lhs, .end = self.rhs }; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | }; | ||
| 99 | |||
| 100 | fn listToSpan(p: *Parser, list: []const Node.Index) !Node.SubRange { | ||
| 101 | try p.extra_data.appendSlice(p.gpa, list); | ||
| 102 | return Node.SubRange{ | ||
| 103 | .start = @intCast(Node.Index, p.extra_data.items.len - list.len), | ||
| 104 | .end = @intCast(Node.Index, p.extra_data.items.len), | ||
| 105 | }; | ||
| 106 | } | ||
| 107 | |||
| 108 | fn addNode(p: *Parser, elem: Ast.NodeList.Elem) Allocator.Error!Node.Index { | ||
| 109 | const result = @intCast(Node.Index, p.nodes.len); | ||
| 110 | try p.nodes.append(p.gpa, elem); | ||
| 111 | return result; | ||
| 112 | } | ||
| 113 | |||
| 114 | fn setNode(p: *Parser, i: usize, elem: Ast.NodeList.Elem) Node.Index { | ||
| 115 | p.nodes.set(i, elem); | ||
| 116 | return @intCast(Node.Index, i); | ||
| 117 | } | ||
| 118 | |||
| 119 | fn reserveNode(p: *Parser, tag: Ast.Node.Tag) !usize { | ||
| 120 | try p.nodes.resize(p.gpa, p.nodes.len + 1); | ||
| 121 | p.nodes.items(.tag)[p.nodes.len - 1] = tag; | ||
| 122 | return p.nodes.len - 1; | ||
| 123 | } | ||
| 124 | |||
| 125 | fn unreserveNode(p: *Parser, node_index: usize) void { | ||
| 126 | if (p.nodes.len == node_index) { | ||
| 127 | p.nodes.resize(p.gpa, p.nodes.len - 1) catch unreachable; | ||
| 128 | } else { | ||
| 129 | // There is zombie node left in the tree, let's make it as inoffensive as possible | ||
| 130 | // (sadly there's no no-op node) | ||
| 131 | p.nodes.items(.tag)[node_index] = .unreachable_literal; | ||
| 132 | p.nodes.items(.main_token)[node_index] = p.tok_i; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | fn addExtra(p: *Parser, extra: anytype) Allocator.Error!Node.Index { | ||
| 137 | const fields = std.meta.fields(@TypeOf(extra)); | ||
| 138 | try p.extra_data.ensureUnusedCapacity(p.gpa, fields.len); | ||
| 139 | const result = @intCast(u32, p.extra_data.items.len); | ||
| 140 | inline for (fields) |field| { | ||
| 141 | comptime assert(field.type == Node.Index); | ||
| 142 | p.extra_data.appendAssumeCapacity(@field(extra, field.name)); | ||
| 143 | } | ||
| 144 | return result; | ||
| 145 | } | ||
| 146 | |||
| 147 | fn warnExpected(p: *Parser, expected_token: Token.Tag) error{OutOfMemory}!void { | ||
| 148 | @setCold(true); | ||
| 149 | try p.warnMsg(.{ | ||
| 150 | .tag = .expected_token, | ||
| 151 | .token = p.tok_i, | ||
| 152 | .extra = .{ .expected_tag = expected_token }, | ||
| 153 | }); | ||
| 154 | } | ||
| 155 | |||
| 156 | fn warn(p: *Parser, error_tag: AstError.Tag) error{OutOfMemory}!void { | ||
| 157 | @setCold(true); | ||
| 158 | try p.warnMsg(.{ .tag = error_tag, .token = p.tok_i }); | ||
| 159 | } | ||
| 160 | |||
| 161 | fn warnMsg(p: *Parser, msg: Ast.Error) error{OutOfMemory}!void { | ||
| 162 | @setCold(true); | ||
| 163 | switch (msg.tag) { | ||
| 164 | .expected_semi_after_decl, | ||
| 165 | .expected_semi_after_stmt, | ||
| 166 | .expected_comma_after_field, | ||
| 167 | .expected_comma_after_arg, | ||
| 168 | .expected_comma_after_param, | ||
| 169 | .expected_comma_after_initializer, | ||
| 170 | .expected_comma_after_switch_prong, | ||
| 171 | .expected_semi_or_else, | ||
| 172 | .expected_semi_or_lbrace, | ||
| 173 | .expected_token, | ||
| 174 | .expected_block, | ||
| 175 | .expected_block_or_assignment, | ||
| 176 | .expected_block_or_expr, | ||
| 177 | .expected_block_or_field, | ||
| 178 | .expected_expr, | ||
| 179 | .expected_expr_or_assignment, | ||
| 180 | .expected_fn, | ||
| 181 | .expected_inlinable, | ||
| 182 | .expected_labelable, | ||
| 183 | .expected_param_list, | ||
| 184 | .expected_prefix_expr, | ||
| 185 | .expected_primary_type_expr, | ||
| 186 | .expected_pub_item, | ||
| 187 | .expected_return_type, | ||
| 188 | .expected_suffix_op, | ||
| 189 | .expected_type_expr, | ||
| 190 | .expected_var_decl, | ||
| 191 | .expected_var_decl_or_fn, | ||
| 192 | .expected_loop_payload, | ||
| 193 | .expected_container, | ||
| 194 | => if (msg.token != 0 and !p.tokensOnSameLine(msg.token - 1, msg.token)) { | ||
| 195 | var copy = msg; | ||
| 196 | copy.token_is_prev = true; | ||
| 197 | copy.token -= 1; | ||
| 198 | return p.errors.append(p.gpa, copy); | ||
| 199 | }, | ||
| 200 | else => {}, | ||
| 201 | } | ||
| 202 | try p.errors.append(p.gpa, msg); | ||
| 203 | } | ||
| 204 | |||
| 205 | fn fail(p: *Parser, tag: Ast.Error.Tag) error{ ParseError, OutOfMemory } { | ||
| 206 | @setCold(true); | ||
| 207 | return p.failMsg(.{ .tag = tag, .token = p.tok_i }); | ||
| 208 | } | ||
| 209 | |||
| 210 | fn failExpected(p: *Parser, expected_token: Token.Tag) error{ ParseError, OutOfMemory } { | ||
| 211 | @setCold(true); | ||
| 212 | return p.failMsg(.{ | ||
| 213 | .tag = .expected_token, | ||
| 214 | .token = p.tok_i, | ||
| 215 | .extra = .{ .expected_tag = expected_token }, | ||
| 216 | }); | ||
| 217 | } | ||
| 218 | |||
| 219 | fn failMsg(p: *Parser, msg: Ast.Error) error{ ParseError, OutOfMemory } { | ||
| 220 | @setCold(true); | ||
| 221 | try p.warnMsg(msg); | ||
| 222 | return error.ParseError; | ||
| 223 | } | ||
| 224 | |||
| 225 | /// Root <- skip container_doc_comment? ContainerMembers eof | ||
| 226 | fn parseRoot(p: *Parser) !void { | ||
| 227 | // Root node must be index 0. | ||
| 228 | p.nodes.appendAssumeCapacity(.{ | ||
| 229 | .tag = .root, | ||
| 230 | .main_token = 0, | ||
| 231 | .data = undefined, | ||
| 232 | }); | ||
| 233 | const root_members = try p.parseContainerMembers(); | ||
| 234 | const root_decls = try root_members.toSpan(p); | ||
| 235 | if (p.token_tags[p.tok_i] != .eof) { | ||
| 236 | try p.warnExpected(.eof); | ||
| 237 | } | ||
| 238 | p.nodes.items(.data)[0] = .{ | ||
| 239 | .lhs = root_decls.start, | ||
| 240 | .rhs = root_decls.end, | ||
| 241 | }; | ||
| 242 | } | ||
| 243 | |||
| 244 | /// ContainerMembers <- ContainerDeclarations (ContainerField COMMA)* (ContainerField / ContainerDeclarations) | ||
| 245 | /// | ||
| 246 | /// ContainerDeclarations | ||
| 247 | /// <- TestDecl ContainerDeclarations | ||
| 248 | /// / ComptimeDecl ContainerDeclarations | ||
| 249 | /// / doc_comment? KEYWORD_pub? Decl ContainerDeclarations | ||
| 250 | /// / | ||
| 251 | /// | ||
| 252 | /// ComptimeDecl <- KEYWORD_comptime Block | ||
| 253 | fn parseContainerMembers(p: *Parser) !Members { | ||
| 254 | const scratch_top = p.scratch.items.len; | ||
| 255 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 256 | |||
| 257 | var field_state: union(enum) { | ||
| 258 | /// No fields have been seen. | ||
| 259 | none, | ||
| 260 | /// Currently parsing fields. | ||
| 261 | seen, | ||
| 262 | /// Saw fields and then a declaration after them. | ||
| 263 | /// Payload is first token of previous declaration. | ||
| 264 | end: Node.Index, | ||
| 265 | /// There was a declaration between fields, don't report more errors. | ||
| 266 | err, | ||
| 267 | } = .none; | ||
| 268 | |||
| 269 | var last_field: TokenIndex = undefined; | ||
| 270 | |||
| 271 | // Skip container doc comments. | ||
| 272 | while (p.eatToken(.container_doc_comment)) |_| {} | ||
| 273 | |||
| 274 | var trailing = false; | ||
| 275 | while (true) { | ||
| 276 | const doc_comment = try p.eatDocComments(); | ||
| 277 | |||
| 278 | switch (p.token_tags[p.tok_i]) { | ||
| 279 | .keyword_test => { | ||
| 280 | if (doc_comment) |some| { | ||
| 281 | try p.warnMsg(.{ .tag = .test_doc_comment, .token = some }); | ||
| 282 | } | ||
| 283 | const test_decl_node = try p.expectTestDeclRecoverable(); | ||
| 284 | if (test_decl_node != 0) { | ||
| 285 | if (field_state == .seen) { | ||
| 286 | field_state = .{ .end = test_decl_node }; | ||
| 287 | } | ||
| 288 | try p.scratch.append(p.gpa, test_decl_node); | ||
| 289 | } | ||
| 290 | trailing = false; | ||
| 291 | }, | ||
| 292 | .keyword_comptime => switch (p.token_tags[p.tok_i + 1]) { | ||
| 293 | .l_brace => { | ||
| 294 | if (doc_comment) |some| { | ||
| 295 | try p.warnMsg(.{ .tag = .comptime_doc_comment, .token = some }); | ||
| 296 | } | ||
| 297 | const comptime_token = p.nextToken(); | ||
| 298 | const block = p.parseBlock() catch |err| switch (err) { | ||
| 299 | error.OutOfMemory => return error.OutOfMemory, | ||
| 300 | error.ParseError => blk: { | ||
| 301 | p.findNextContainerMember(); | ||
| 302 | break :blk null_node; | ||
| 303 | }, | ||
| 304 | }; | ||
| 305 | if (block != 0) { | ||
| 306 | const comptime_node = try p.addNode(.{ | ||
| 307 | .tag = .@"comptime", | ||
| 308 | .main_token = comptime_token, | ||
| 309 | .data = .{ | ||
| 310 | .lhs = block, | ||
| 311 | .rhs = undefined, | ||
| 312 | }, | ||
| 313 | }); | ||
| 314 | if (field_state == .seen) { | ||
| 315 | field_state = .{ .end = comptime_node }; | ||
| 316 | } | ||
| 317 | try p.scratch.append(p.gpa, comptime_node); | ||
| 318 | } | ||
| 319 | trailing = false; | ||
| 320 | }, | ||
| 321 | else => { | ||
| 322 | const identifier = p.tok_i; | ||
| 323 | defer last_field = identifier; | ||
| 324 | const container_field = p.expectContainerField() catch |err| switch (err) { | ||
| 325 | error.OutOfMemory => return error.OutOfMemory, | ||
| 326 | error.ParseError => { | ||
| 327 | p.findNextContainerMember(); | ||
| 328 | continue; | ||
| 329 | }, | ||
| 330 | }; | ||
| 331 | switch (field_state) { | ||
| 332 | .none => field_state = .seen, | ||
| 333 | .err, .seen => {}, | ||
| 334 | .end => |node| { | ||
| 335 | try p.warnMsg(.{ | ||
| 336 | .tag = .decl_between_fields, | ||
| 337 | .token = p.nodes.items(.main_token)[node], | ||
| 338 | }); | ||
| 339 | try p.warnMsg(.{ | ||
| 340 | .tag = .previous_field, | ||
| 341 | .is_note = true, | ||
| 342 | .token = last_field, | ||
| 343 | }); | ||
| 344 | try p.warnMsg(.{ | ||
| 345 | .tag = .next_field, | ||
| 346 | .is_note = true, | ||
| 347 | .token = identifier, | ||
| 348 | }); | ||
| 349 | // Continue parsing; error will be reported later. | ||
| 350 | field_state = .err; | ||
| 351 | }, | ||
| 352 | } | ||
| 353 | try p.scratch.append(p.gpa, container_field); | ||
| 354 | switch (p.token_tags[p.tok_i]) { | ||
| 355 | .comma => { | ||
| 356 | p.tok_i += 1; | ||
| 357 | trailing = true; | ||
| 358 | continue; | ||
| 359 | }, | ||
| 360 | .r_brace, .eof => { | ||
| 361 | trailing = false; | ||
| 362 | break; | ||
| 363 | }, | ||
| 364 | else => {}, | ||
| 365 | } | ||
| 366 | // There is not allowed to be a decl after a field with no comma. | ||
| 367 | // Report error but recover parser. | ||
| 368 | try p.warn(.expected_comma_after_field); | ||
| 369 | p.findNextContainerMember(); | ||
| 370 | }, | ||
| 371 | }, | ||
| 372 | .keyword_pub => { | ||
| 373 | p.tok_i += 1; | ||
| 374 | const top_level_decl = try p.expectTopLevelDeclRecoverable(); | ||
| 375 | if (top_level_decl != 0) { | ||
| 376 | if (field_state == .seen) { | ||
| 377 | field_state = .{ .end = top_level_decl }; | ||
| 378 | } | ||
| 379 | try p.scratch.append(p.gpa, top_level_decl); | ||
| 380 | } | ||
| 381 | trailing = p.token_tags[p.tok_i - 1] == .semicolon; | ||
| 382 | }, | ||
| 383 | .keyword_usingnamespace => { | ||
| 384 | const node = try p.expectUsingNamespaceRecoverable(); | ||
| 385 | if (node != 0) { | ||
| 386 | if (field_state == .seen) { | ||
| 387 | field_state = .{ .end = node }; | ||
| 388 | } | ||
| 389 | try p.scratch.append(p.gpa, node); | ||
| 390 | } | ||
| 391 | trailing = p.token_tags[p.tok_i - 1] == .semicolon; | ||
| 392 | }, | ||
| 393 | .keyword_const, | ||
| 394 | .keyword_var, | ||
| 395 | .keyword_threadlocal, | ||
| 396 | .keyword_export, | ||
| 397 | .keyword_extern, | ||
| 398 | .keyword_inline, | ||
| 399 | .keyword_noinline, | ||
| 400 | .keyword_fn, | ||
| 401 | => { | ||
| 402 | const top_level_decl = try p.expectTopLevelDeclRecoverable(); | ||
| 403 | if (top_level_decl != 0) { | ||
| 404 | if (field_state == .seen) { | ||
| 405 | field_state = .{ .end = top_level_decl }; | ||
| 406 | } | ||
| 407 | try p.scratch.append(p.gpa, top_level_decl); | ||
| 408 | } | ||
| 409 | trailing = p.token_tags[p.tok_i - 1] == .semicolon; | ||
| 410 | }, | ||
| 411 | .eof, .r_brace => { | ||
| 412 | if (doc_comment) |tok| { | ||
| 413 | try p.warnMsg(.{ | ||
| 414 | .tag = .unattached_doc_comment, | ||
| 415 | .token = tok, | ||
| 416 | }); | ||
| 417 | } | ||
| 418 | break; | ||
| 419 | }, | ||
| 420 | else => { | ||
| 421 | const c_container = p.parseCStyleContainer() catch |err| switch (err) { | ||
| 422 | error.OutOfMemory => return error.OutOfMemory, | ||
| 423 | error.ParseError => false, | ||
| 424 | }; | ||
| 425 | if (c_container) continue; | ||
| 426 | |||
| 427 | const identifier = p.tok_i; | ||
| 428 | defer last_field = identifier; | ||
| 429 | const container_field = p.expectContainerField() catch |err| switch (err) { | ||
| 430 | error.OutOfMemory => return error.OutOfMemory, | ||
| 431 | error.ParseError => { | ||
| 432 | p.findNextContainerMember(); | ||
| 433 | continue; | ||
| 434 | }, | ||
| 435 | }; | ||
| 436 | switch (field_state) { | ||
| 437 | .none => field_state = .seen, | ||
| 438 | .err, .seen => {}, | ||
| 439 | .end => |node| { | ||
| 440 | try p.warnMsg(.{ | ||
| 441 | .tag = .decl_between_fields, | ||
| 442 | .token = p.nodes.items(.main_token)[node], | ||
| 443 | }); | ||
| 444 | try p.warnMsg(.{ | ||
| 445 | .tag = .previous_field, | ||
| 446 | .is_note = true, | ||
| 447 | .token = last_field, | ||
| 448 | }); | ||
| 449 | try p.warnMsg(.{ | ||
| 450 | .tag = .next_field, | ||
| 451 | .is_note = true, | ||
| 452 | .token = identifier, | ||
| 453 | }); | ||
| 454 | // Continue parsing; error will be reported later. | ||
| 455 | field_state = .err; | ||
| 456 | }, | ||
| 457 | } | ||
| 458 | try p.scratch.append(p.gpa, container_field); | ||
| 459 | switch (p.token_tags[p.tok_i]) { | ||
| 460 | .comma => { | ||
| 461 | p.tok_i += 1; | ||
| 462 | trailing = true; | ||
| 463 | continue; | ||
| 464 | }, | ||
| 465 | .r_brace, .eof => { | ||
| 466 | trailing = false; | ||
| 467 | break; | ||
| 468 | }, | ||
| 469 | else => {}, | ||
| 470 | } | ||
| 471 | // There is not allowed to be a decl after a field with no comma. | ||
| 472 | // Report error but recover parser. | ||
| 473 | try p.warn(.expected_comma_after_field); | ||
| 474 | if (p.token_tags[p.tok_i] == .semicolon and p.token_tags[identifier] == .identifier) { | ||
| 475 | try p.warnMsg(.{ | ||
| 476 | .tag = .var_const_decl, | ||
| 477 | .is_note = true, | ||
| 478 | .token = identifier, | ||
| 479 | }); | ||
| 480 | } | ||
| 481 | p.findNextContainerMember(); | ||
| 482 | continue; | ||
| 483 | }, | ||
| 484 | } | ||
| 485 | } | ||
| 486 | |||
| 487 | const items = p.scratch.items[scratch_top..]; | ||
| 488 | switch (items.len) { | ||
| 489 | 0 => return Members{ | ||
| 490 | .len = 0, | ||
| 491 | .lhs = 0, | ||
| 492 | .rhs = 0, | ||
| 493 | .trailing = trailing, | ||
| 494 | }, | ||
| 495 | 1 => return Members{ | ||
| 496 | .len = 1, | ||
| 497 | .lhs = items[0], | ||
| 498 | .rhs = 0, | ||
| 499 | .trailing = trailing, | ||
| 500 | }, | ||
| 501 | 2 => return Members{ | ||
| 502 | .len = 2, | ||
| 503 | .lhs = items[0], | ||
| 504 | .rhs = items[1], | ||
| 505 | .trailing = trailing, | ||
| 506 | }, | ||
| 507 | else => { | ||
| 508 | const span = try p.listToSpan(items); | ||
| 509 | return Members{ | ||
| 510 | .len = items.len, | ||
| 511 | .lhs = span.start, | ||
| 512 | .rhs = span.end, | ||
| 513 | .trailing = trailing, | ||
| 514 | }; | ||
| 515 | }, | ||
| 516 | } | ||
| 517 | } | ||
| 518 | |||
| 519 | /// Attempts to find next container member by searching for certain tokens | ||
| 520 | fn findNextContainerMember(p: *Parser) void { | ||
| 521 | var level: u32 = 0; | ||
| 522 | while (true) { | ||
| 523 | const tok = p.nextToken(); | ||
| 524 | switch (p.token_tags[tok]) { | ||
| 525 | // Any of these can start a new top level declaration. | ||
| 526 | .keyword_test, | ||
| 527 | .keyword_comptime, | ||
| 528 | .keyword_pub, | ||
| 529 | .keyword_export, | ||
| 530 | .keyword_extern, | ||
| 531 | .keyword_inline, | ||
| 532 | .keyword_noinline, | ||
| 533 | .keyword_usingnamespace, | ||
| 534 | .keyword_threadlocal, | ||
| 535 | .keyword_const, | ||
| 536 | .keyword_var, | ||
| 537 | .keyword_fn, | ||
| 538 | => { | ||
| 539 | if (level == 0) { | ||
| 540 | p.tok_i -= 1; | ||
| 541 | return; | ||
| 542 | } | ||
| 543 | }, | ||
| 544 | .identifier => { | ||
| 545 | if (p.token_tags[tok + 1] == .comma and level == 0) { | ||
| 546 | p.tok_i -= 1; | ||
| 547 | return; | ||
| 548 | } | ||
| 549 | }, | ||
| 550 | .comma, .semicolon => { | ||
| 551 | // this decl was likely meant to end here | ||
| 552 | if (level == 0) { | ||
| 553 | return; | ||
| 554 | } | ||
| 555 | }, | ||
| 556 | .l_paren, .l_bracket, .l_brace => level += 1, | ||
| 557 | .r_paren, .r_bracket => { | ||
| 558 | if (level != 0) level -= 1; | ||
| 559 | }, | ||
| 560 | .r_brace => { | ||
| 561 | if (level == 0) { | ||
| 562 | // end of container, exit | ||
| 563 | p.tok_i -= 1; | ||
| 564 | return; | ||
| 565 | } | ||
| 566 | level -= 1; | ||
| 567 | }, | ||
| 568 | .eof => { | ||
| 569 | p.tok_i -= 1; | ||
| 570 | return; | ||
| 571 | }, | ||
| 572 | else => {}, | ||
| 573 | } | ||
| 574 | } | ||
| 575 | } | ||
| 576 | |||
| 577 | /// Attempts to find the next statement by searching for a semicolon | ||
| 578 | fn findNextStmt(p: *Parser) void { | ||
| 579 | var level: u32 = 0; | ||
| 580 | while (true) { | ||
| 581 | const tok = p.nextToken(); | ||
| 582 | switch (p.token_tags[tok]) { | ||
| 583 | .l_brace => level += 1, | ||
| 584 | .r_brace => { | ||
| 585 | if (level == 0) { | ||
| 586 | p.tok_i -= 1; | ||
| 587 | return; | ||
| 588 | } | ||
| 589 | level -= 1; | ||
| 590 | }, | ||
| 591 | .semicolon => { | ||
| 592 | if (level == 0) { | ||
| 593 | return; | ||
| 594 | } | ||
| 595 | }, | ||
| 596 | .eof => { | ||
| 597 | p.tok_i -= 1; | ||
| 598 | return; | ||
| 599 | }, | ||
| 600 | else => {}, | ||
| 601 | } | ||
| 602 | } | ||
| 603 | } | ||
| 604 | |||
| 605 | /// TestDecl <- KEYWORD_test (STRINGLITERALSINGLE / IDENTIFIER)? Block | ||
| 606 | fn expectTestDecl(p: *Parser) !Node.Index { | ||
| 607 | const test_token = p.assertToken(.keyword_test); | ||
| 608 | const name_token = switch (p.token_tags[p.nextToken()]) { | ||
| 609 | .string_literal, .identifier => p.tok_i - 1, | ||
| 610 | else => blk: { | ||
| 611 | p.tok_i -= 1; | ||
| 612 | break :blk null; | ||
| 613 | }, | ||
| 614 | }; | ||
| 615 | const block_node = try p.parseBlock(); | ||
| 616 | if (block_node == 0) return p.fail(.expected_block); | ||
| 617 | return p.addNode(.{ | ||
| 618 | .tag = .test_decl, | ||
| 619 | .main_token = test_token, | ||
| 620 | .data = .{ | ||
| 621 | .lhs = name_token orelse 0, | ||
| 622 | .rhs = block_node, | ||
| 623 | }, | ||
| 624 | }); | ||
| 625 | } | ||
| 626 | |||
| 627 | fn expectTestDeclRecoverable(p: *Parser) error{OutOfMemory}!Node.Index { | ||
| 628 | return p.expectTestDecl() catch |err| switch (err) { | ||
| 629 | error.OutOfMemory => return error.OutOfMemory, | ||
| 630 | error.ParseError => { | ||
| 631 | p.findNextContainerMember(); | ||
| 632 | return null_node; | ||
| 633 | }, | ||
| 634 | }; | ||
| 635 | } | ||
| 636 | |||
| 637 | /// Decl | ||
| 638 | /// <- (KEYWORD_export / KEYWORD_extern STRINGLITERALSINGLE? / (KEYWORD_inline / KEYWORD_noinline))? FnProto (SEMICOLON / Block) | ||
| 639 | /// / (KEYWORD_export / KEYWORD_extern STRINGLITERALSINGLE?)? KEYWORD_threadlocal? VarDecl | ||
| 640 | /// / KEYWORD_usingnamespace Expr SEMICOLON | ||
| 641 | fn expectTopLevelDecl(p: *Parser) !Node.Index { | ||
| 642 | const extern_export_inline_token = p.nextToken(); | ||
| 643 | var is_extern: bool = false; | ||
| 644 | var expect_fn: bool = false; | ||
| 645 | var expect_var_or_fn: bool = false; | ||
| 646 | switch (p.token_tags[extern_export_inline_token]) { | ||
| 647 | .keyword_extern => { | ||
| 648 | _ = p.eatToken(.string_literal); | ||
| 649 | is_extern = true; | ||
| 650 | expect_var_or_fn = true; | ||
| 651 | }, | ||
| 652 | .keyword_export => expect_var_or_fn = true, | ||
| 653 | .keyword_inline, .keyword_noinline => expect_fn = true, | ||
| 654 | else => p.tok_i -= 1, | ||
| 655 | } | ||
| 656 | const fn_proto = try p.parseFnProto(); | ||
| 657 | if (fn_proto != 0) { | ||
| 658 | switch (p.token_tags[p.tok_i]) { | ||
| 659 | .semicolon => { | ||
| 660 | p.tok_i += 1; | ||
| 661 | return fn_proto; | ||
| 662 | }, | ||
| 663 | .l_brace => { | ||
| 664 | if (is_extern) { | ||
| 665 | try p.warnMsg(.{ .tag = .extern_fn_body, .token = extern_export_inline_token }); | ||
| 666 | return null_node; | ||
| 667 | } | ||
| 668 | const fn_decl_index = try p.reserveNode(.fn_decl); | ||
| 669 | errdefer p.unreserveNode(fn_decl_index); | ||
| 670 | |||
| 671 | const body_block = try p.parseBlock(); | ||
| 672 | assert(body_block != 0); | ||
| 673 | return p.setNode(fn_decl_index, .{ | ||
| 674 | .tag = .fn_decl, | ||
| 675 | .main_token = p.nodes.items(.main_token)[fn_proto], | ||
| 676 | .data = .{ | ||
| 677 | .lhs = fn_proto, | ||
| 678 | .rhs = body_block, | ||
| 679 | }, | ||
| 680 | }); | ||
| 681 | }, | ||
| 682 | else => { | ||
| 683 | // Since parseBlock only return error.ParseError on | ||
| 684 | // a missing '}' we can assume this function was | ||
| 685 | // supposed to end here. | ||
| 686 | try p.warn(.expected_semi_or_lbrace); | ||
| 687 | return null_node; | ||
| 688 | }, | ||
| 689 | } | ||
| 690 | } | ||
| 691 | if (expect_fn) { | ||
| 692 | try p.warn(.expected_fn); | ||
| 693 | return error.ParseError; | ||
| 694 | } | ||
| 695 | |||
| 696 | const thread_local_token = p.eatToken(.keyword_threadlocal); | ||
| 697 | const var_decl = try p.parseVarDecl(); | ||
| 698 | if (var_decl != 0) { | ||
| 699 | try p.expectSemicolon(.expected_semi_after_decl, false); | ||
| 700 | return var_decl; | ||
| 701 | } | ||
| 702 | if (thread_local_token != null) { | ||
| 703 | return p.fail(.expected_var_decl); | ||
| 704 | } | ||
| 705 | if (expect_var_or_fn) { | ||
| 706 | return p.fail(.expected_var_decl_or_fn); | ||
| 707 | } | ||
| 708 | if (p.token_tags[p.tok_i] != .keyword_usingnamespace) { | ||
| 709 | return p.fail(.expected_pub_item); | ||
| 710 | } | ||
| 711 | return p.expectUsingNamespace(); | ||
| 712 | } | ||
| 713 | |||
| 714 | fn expectTopLevelDeclRecoverable(p: *Parser) error{OutOfMemory}!Node.Index { | ||
| 715 | return p.expectTopLevelDecl() catch |err| switch (err) { | ||
| 716 | error.OutOfMemory => return error.OutOfMemory, | ||
| 717 | error.ParseError => { | ||
| 718 | p.findNextContainerMember(); | ||
| 719 | return null_node; | ||
| 720 | }, | ||
| 721 | }; | ||
| 722 | } | ||
| 723 | |||
| 724 | fn expectUsingNamespace(p: *Parser) !Node.Index { | ||
| 725 | const usingnamespace_token = p.assertToken(.keyword_usingnamespace); | ||
| 726 | const expr = try p.expectExpr(); | ||
| 727 | try p.expectSemicolon(.expected_semi_after_decl, false); | ||
| 728 | return p.addNode(.{ | ||
| 729 | .tag = .@"usingnamespace", | ||
| 730 | .main_token = usingnamespace_token, | ||
| 731 | .data = .{ | ||
| 732 | .lhs = expr, | ||
| 733 | .rhs = undefined, | ||
| 734 | }, | ||
| 735 | }); | ||
| 736 | } | ||
| 737 | |||
| 738 | fn expectUsingNamespaceRecoverable(p: *Parser) error{OutOfMemory}!Node.Index { | ||
| 739 | return p.expectUsingNamespace() catch |err| switch (err) { | ||
| 740 | error.OutOfMemory => return error.OutOfMemory, | ||
| 741 | error.ParseError => { | ||
| 742 | p.findNextContainerMember(); | ||
| 743 | return null_node; | ||
| 744 | }, | ||
| 745 | }; | ||
| 746 | } | ||
| 747 | |||
| 748 | /// FnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? AddrSpace? LinkSection? CallConv? EXCLAMATIONMARK? TypeExpr | ||
| 749 | fn parseFnProto(p: *Parser) !Node.Index { | ||
| 750 | const fn_token = p.eatToken(.keyword_fn) orelse return null_node; | ||
| 751 | |||
| 752 | // We want the fn proto node to be before its children in the array. | ||
| 753 | const fn_proto_index = try p.reserveNode(.fn_proto); | ||
| 754 | errdefer p.unreserveNode(fn_proto_index); | ||
| 755 | |||
| 756 | _ = p.eatToken(.identifier); | ||
| 757 | const params = try p.parseParamDeclList(); | ||
| 758 | const align_expr = try p.parseByteAlign(); | ||
| 759 | const addrspace_expr = try p.parseAddrSpace(); | ||
| 760 | const section_expr = try p.parseLinkSection(); | ||
| 761 | const callconv_expr = try p.parseCallconv(); | ||
| 762 | _ = p.eatToken(.bang); | ||
| 763 | |||
| 764 | const return_type_expr = try p.parseTypeExpr(); | ||
| 765 | if (return_type_expr == 0) { | ||
| 766 | // most likely the user forgot to specify the return type. | ||
| 767 | // Mark return type as invalid and try to continue. | ||
| 768 | try p.warn(.expected_return_type); | ||
| 769 | } | ||
| 770 | |||
| 771 | if (align_expr == 0 and section_expr == 0 and callconv_expr == 0 and addrspace_expr == 0) { | ||
| 772 | switch (params) { | ||
| 773 | .zero_or_one => |param| return p.setNode(fn_proto_index, .{ | ||
| 774 | .tag = .fn_proto_simple, | ||
| 775 | .main_token = fn_token, | ||
| 776 | .data = .{ | ||
| 777 | .lhs = param, | ||
| 778 | .rhs = return_type_expr, | ||
| 779 | }, | ||
| 780 | }), | ||
| 781 | .multi => |span| { | ||
| 782 | return p.setNode(fn_proto_index, .{ | ||
| 783 | .tag = .fn_proto_multi, | ||
| 784 | .main_token = fn_token, | ||
| 785 | .data = .{ | ||
| 786 | .lhs = try p.addExtra(Node.SubRange{ | ||
| 787 | .start = span.start, | ||
| 788 | .end = span.end, | ||
| 789 | }), | ||
| 790 | .rhs = return_type_expr, | ||
| 791 | }, | ||
| 792 | }); | ||
| 793 | }, | ||
| 794 | } | ||
| 795 | } | ||
| 796 | switch (params) { | ||
| 797 | .zero_or_one => |param| return p.setNode(fn_proto_index, .{ | ||
| 798 | .tag = .fn_proto_one, | ||
| 799 | .main_token = fn_token, | ||
| 800 | .data = .{ | ||
| 801 | .lhs = try p.addExtra(Node.FnProtoOne{ | ||
| 802 | .param = param, | ||
| 803 | .align_expr = align_expr, | ||
| 804 | .addrspace_expr = addrspace_expr, | ||
| 805 | .section_expr = section_expr, | ||
| 806 | .callconv_expr = callconv_expr, | ||
| 807 | }), | ||
| 808 | .rhs = return_type_expr, | ||
| 809 | }, | ||
| 810 | }), | ||
| 811 | .multi => |span| { | ||
| 812 | return p.setNode(fn_proto_index, .{ | ||
| 813 | .tag = .fn_proto, | ||
| 814 | .main_token = fn_token, | ||
| 815 | .data = .{ | ||
| 816 | .lhs = try p.addExtra(Node.FnProto{ | ||
| 817 | .params_start = span.start, | ||
| 818 | .params_end = span.end, | ||
| 819 | .align_expr = align_expr, | ||
| 820 | .addrspace_expr = addrspace_expr, | ||
| 821 | .section_expr = section_expr, | ||
| 822 | .callconv_expr = callconv_expr, | ||
| 823 | }), | ||
| 824 | .rhs = return_type_expr, | ||
| 825 | }, | ||
| 826 | }); | ||
| 827 | }, | ||
| 828 | } | ||
| 829 | } | ||
| 830 | |||
| 831 | /// VarDecl <- (KEYWORD_const / KEYWORD_var) IDENTIFIER (COLON TypeExpr)? ByteAlign? AddrSpace? LinkSection? (EQUAL Expr)? SEMICOLON | ||
| 832 | fn parseVarDecl(p: *Parser) !Node.Index { | ||
| 833 | const mut_token = p.eatToken(.keyword_const) orelse | ||
| 834 | p.eatToken(.keyword_var) orelse | ||
| 835 | return null_node; | ||
| 836 | |||
| 837 | _ = try p.expectToken(.identifier); | ||
| 838 | const type_node: Node.Index = if (p.eatToken(.colon) == null) 0 else try p.expectTypeExpr(); | ||
| 839 | const align_node = try p.parseByteAlign(); | ||
| 840 | const addrspace_node = try p.parseAddrSpace(); | ||
| 841 | const section_node = try p.parseLinkSection(); | ||
| 842 | const init_node: Node.Index = switch (p.token_tags[p.tok_i]) { | ||
| 843 | .equal_equal => blk: { | ||
| 844 | try p.warn(.wrong_equal_var_decl); | ||
| 845 | p.tok_i += 1; | ||
| 846 | break :blk try p.expectExpr(); | ||
| 847 | }, | ||
| 848 | .equal => blk: { | ||
| 849 | p.tok_i += 1; | ||
| 850 | break :blk try p.expectExpr(); | ||
| 851 | }, | ||
| 852 | else => 0, | ||
| 853 | }; | ||
| 854 | if (section_node == 0 and addrspace_node == 0) { | ||
| 855 | if (align_node == 0) { | ||
| 856 | return p.addNode(.{ | ||
| 857 | .tag = .simple_var_decl, | ||
| 858 | .main_token = mut_token, | ||
| 859 | .data = .{ | ||
| 860 | .lhs = type_node, | ||
| 861 | .rhs = init_node, | ||
| 862 | }, | ||
| 863 | }); | ||
| 864 | } else if (type_node == 0) { | ||
| 865 | return p.addNode(.{ | ||
| 866 | .tag = .aligned_var_decl, | ||
| 867 | .main_token = mut_token, | ||
| 868 | .data = .{ | ||
| 869 | .lhs = align_node, | ||
| 870 | .rhs = init_node, | ||
| 871 | }, | ||
| 872 | }); | ||
| 873 | } else { | ||
| 874 | return p.addNode(.{ | ||
| 875 | .tag = .local_var_decl, | ||
| 876 | .main_token = mut_token, | ||
| 877 | .data = .{ | ||
| 878 | .lhs = try p.addExtra(Node.LocalVarDecl{ | ||
| 879 | .type_node = type_node, | ||
| 880 | .align_node = align_node, | ||
| 881 | }), | ||
| 882 | .rhs = init_node, | ||
| 883 | }, | ||
| 884 | }); | ||
| 885 | } | ||
| 886 | } else { | ||
| 887 | return p.addNode(.{ | ||
| 888 | .tag = .global_var_decl, | ||
| 889 | .main_token = mut_token, | ||
| 890 | .data = .{ | ||
| 891 | .lhs = try p.addExtra(Node.GlobalVarDecl{ | ||
| 892 | .type_node = type_node, | ||
| 893 | .align_node = align_node, | ||
| 894 | .addrspace_node = addrspace_node, | ||
| 895 | .section_node = section_node, | ||
| 896 | }), | ||
| 897 | .rhs = init_node, | ||
| 898 | }, | ||
| 899 | }); | ||
| 900 | } | ||
| 901 | } | ||
| 902 | |||
| 903 | /// ContainerField | ||
| 904 | /// <- doc_comment? KEYWORD_comptime? IDENTIFIER (COLON TypeExpr)? ByteAlign? (EQUAL Expr)? | ||
| 905 | /// / doc_comment? KEYWORD_comptime? (IDENTIFIER COLON)? !KEYWORD_fn TypeExpr ByteAlign? (EQUAL Expr)? | ||
| 906 | fn expectContainerField(p: *Parser) !Node.Index { | ||
| 907 | var main_token = p.tok_i; | ||
| 908 | _ = p.eatToken(.keyword_comptime); | ||
| 909 | const tuple_like = p.token_tags[p.tok_i] != .identifier or p.token_tags[p.tok_i + 1] != .colon; | ||
| 910 | if (!tuple_like) { | ||
| 911 | main_token = p.assertToken(.identifier); | ||
| 912 | } | ||
| 913 | |||
| 914 | var align_expr: Node.Index = 0; | ||
| 915 | var type_expr: Node.Index = 0; | ||
| 916 | if (p.eatToken(.colon) != null or tuple_like) { | ||
| 917 | type_expr = try p.expectTypeExpr(); | ||
| 918 | align_expr = try p.parseByteAlign(); | ||
| 919 | } | ||
| 920 | |||
| 921 | const value_expr: Node.Index = if (p.eatToken(.equal) == null) 0 else try p.expectExpr(); | ||
| 922 | |||
| 923 | if (align_expr == 0) { | ||
| 924 | return p.addNode(.{ | ||
| 925 | .tag = .container_field_init, | ||
| 926 | .main_token = main_token, | ||
| 927 | .data = .{ | ||
| 928 | .lhs = type_expr, | ||
| 929 | .rhs = value_expr, | ||
| 930 | }, | ||
| 931 | }); | ||
| 932 | } else if (value_expr == 0) { | ||
| 933 | return p.addNode(.{ | ||
| 934 | .tag = .container_field_align, | ||
| 935 | .main_token = main_token, | ||
| 936 | .data = .{ | ||
| 937 | .lhs = type_expr, | ||
| 938 | .rhs = align_expr, | ||
| 939 | }, | ||
| 940 | }); | ||
| 941 | } else { | ||
| 942 | return p.addNode(.{ | ||
| 943 | .tag = .container_field, | ||
| 944 | .main_token = main_token, | ||
| 945 | .data = .{ | ||
| 946 | .lhs = type_expr, | ||
| 947 | .rhs = try p.addExtra(Node.ContainerField{ | ||
| 948 | .value_expr = value_expr, | ||
| 949 | .align_expr = align_expr, | ||
| 950 | }), | ||
| 951 | }, | ||
| 952 | }); | ||
| 953 | } | ||
| 954 | } | ||
| 955 | |||
| 956 | /// Statement | ||
| 957 | /// <- KEYWORD_comptime? VarDecl | ||
| 958 | /// / KEYWORD_comptime BlockExprStatement | ||
| 959 | /// / KEYWORD_nosuspend BlockExprStatement | ||
| 960 | /// / KEYWORD_suspend BlockExprStatement | ||
| 961 | /// / KEYWORD_defer BlockExprStatement | ||
| 962 | /// / KEYWORD_errdefer Payload? BlockExprStatement | ||
| 963 | /// / IfStatement | ||
| 964 | /// / LabeledStatement | ||
| 965 | /// / SwitchExpr | ||
| 966 | /// / AssignExpr SEMICOLON | ||
| 967 | fn parseStatement(p: *Parser, allow_defer_var: bool) Error!Node.Index { | ||
| 968 | const comptime_token = p.eatToken(.keyword_comptime); | ||
| 969 | |||
| 970 | if (allow_defer_var) { | ||
| 971 | const var_decl = try p.parseVarDecl(); | ||
| 972 | if (var_decl != 0) { | ||
| 973 | try p.expectSemicolon(.expected_semi_after_decl, true); | ||
| 974 | return var_decl; | ||
| 975 | } | ||
| 976 | } | ||
| 977 | |||
| 978 | if (comptime_token) |token| { | ||
| 979 | return p.addNode(.{ | ||
| 980 | .tag = .@"comptime", | ||
| 981 | .main_token = token, | ||
| 982 | .data = .{ | ||
| 983 | .lhs = try p.expectBlockExprStatement(), | ||
| 984 | .rhs = undefined, | ||
| 985 | }, | ||
| 986 | }); | ||
| 987 | } | ||
| 988 | |||
| 989 | switch (p.token_tags[p.tok_i]) { | ||
| 990 | .keyword_nosuspend => { | ||
| 991 | return p.addNode(.{ | ||
| 992 | .tag = .@"nosuspend", | ||
| 993 | .main_token = p.nextToken(), | ||
| 994 | .data = .{ | ||
| 995 | .lhs = try p.expectBlockExprStatement(), | ||
| 996 | .rhs = undefined, | ||
| 997 | }, | ||
| 998 | }); | ||
| 999 | }, | ||
| 1000 | .keyword_suspend => { | ||
| 1001 | const token = p.nextToken(); | ||
| 1002 | const block_expr = try p.expectBlockExprStatement(); | ||
| 1003 | return p.addNode(.{ | ||
| 1004 | .tag = .@"suspend", | ||
| 1005 | .main_token = token, | ||
| 1006 | .data = .{ | ||
| 1007 | .lhs = block_expr, | ||
| 1008 | .rhs = undefined, | ||
| 1009 | }, | ||
| 1010 | }); | ||
| 1011 | }, | ||
| 1012 | .keyword_defer => if (allow_defer_var) return p.addNode(.{ | ||
| 1013 | .tag = .@"defer", | ||
| 1014 | .main_token = p.nextToken(), | ||
| 1015 | .data = .{ | ||
| 1016 | .lhs = undefined, | ||
| 1017 | .rhs = try p.expectBlockExprStatement(), | ||
| 1018 | }, | ||
| 1019 | }), | ||
| 1020 | .keyword_errdefer => if (allow_defer_var) return p.addNode(.{ | ||
| 1021 | .tag = .@"errdefer", | ||
| 1022 | .main_token = p.nextToken(), | ||
| 1023 | .data = .{ | ||
| 1024 | .lhs = try p.parsePayload(), | ||
| 1025 | .rhs = try p.expectBlockExprStatement(), | ||
| 1026 | }, | ||
| 1027 | }), | ||
| 1028 | .keyword_switch => return p.expectSwitchExpr(), | ||
| 1029 | .keyword_if => return p.expectIfStatement(), | ||
| 1030 | .keyword_enum, .keyword_struct, .keyword_union => { | ||
| 1031 | const identifier = p.tok_i + 1; | ||
| 1032 | if (try p.parseCStyleContainer()) { | ||
| 1033 | // Return something so that `expectStatement` is happy. | ||
| 1034 | return p.addNode(.{ | ||
| 1035 | .tag = .identifier, | ||
| 1036 | .main_token = identifier, | ||
| 1037 | .data = .{ | ||
| 1038 | .lhs = undefined, | ||
| 1039 | .rhs = undefined, | ||
| 1040 | }, | ||
| 1041 | }); | ||
| 1042 | } | ||
| 1043 | }, | ||
| 1044 | else => {}, | ||
| 1045 | } | ||
| 1046 | |||
| 1047 | const labeled_statement = try p.parseLabeledStatement(); | ||
| 1048 | if (labeled_statement != 0) return labeled_statement; | ||
| 1049 | |||
| 1050 | const assign_expr = try p.parseAssignExpr(); | ||
| 1051 | if (assign_expr != 0) { | ||
| 1052 | try p.expectSemicolon(.expected_semi_after_stmt, true); | ||
| 1053 | return assign_expr; | ||
| 1054 | } | ||
| 1055 | |||
| 1056 | return null_node; | ||
| 1057 | } | ||
| 1058 | |||
| 1059 | fn expectStatement(p: *Parser, allow_defer_var: bool) !Node.Index { | ||
| 1060 | const statement = try p.parseStatement(allow_defer_var); | ||
| 1061 | if (statement == 0) { | ||
| 1062 | return p.fail(.expected_statement); | ||
| 1063 | } | ||
| 1064 | return statement; | ||
| 1065 | } | ||
| 1066 | |||
| 1067 | /// If a parse error occurs, reports an error, but then finds the next statement | ||
| 1068 | /// and returns that one instead. If a parse error occurs but there is no following | ||
| 1069 | /// statement, returns 0. | ||
| 1070 | fn expectStatementRecoverable(p: *Parser) Error!Node.Index { | ||
| 1071 | while (true) { | ||
| 1072 | return p.expectStatement(true) catch |err| switch (err) { | ||
| 1073 | error.OutOfMemory => return error.OutOfMemory, | ||
| 1074 | error.ParseError => { | ||
| 1075 | p.findNextStmt(); // Try to skip to the next statement. | ||
| 1076 | switch (p.token_tags[p.tok_i]) { | ||
| 1077 | .r_brace => return null_node, | ||
| 1078 | .eof => return error.ParseError, | ||
| 1079 | else => continue, | ||
| 1080 | } | ||
| 1081 | }, | ||
| 1082 | }; | ||
| 1083 | } | ||
| 1084 | } | ||
| 1085 | |||
| 1086 | /// IfStatement | ||
| 1087 | /// <- IfPrefix BlockExpr ( KEYWORD_else Payload? Statement )? | ||
| 1088 | /// / IfPrefix AssignExpr ( SEMICOLON / KEYWORD_else Payload? Statement ) | ||
| 1089 | fn expectIfStatement(p: *Parser) !Node.Index { | ||
| 1090 | const if_token = p.assertToken(.keyword_if); | ||
| 1091 | _ = try p.expectToken(.l_paren); | ||
| 1092 | const condition = try p.expectExpr(); | ||
| 1093 | _ = try p.expectToken(.r_paren); | ||
| 1094 | _ = try p.parsePtrPayload(); | ||
| 1095 | |||
| 1096 | // TODO propose to change the syntax so that semicolons are always required | ||
| 1097 | // inside if statements, even if there is an `else`. | ||
| 1098 | var else_required = false; | ||
| 1099 | const then_expr = blk: { | ||
| 1100 | const block_expr = try p.parseBlockExpr(); | ||
| 1101 | if (block_expr != 0) break :blk block_expr; | ||
| 1102 | const assign_expr = try p.parseAssignExpr(); | ||
| 1103 | if (assign_expr == 0) { | ||
| 1104 | return p.fail(.expected_block_or_assignment); | ||
| 1105 | } | ||
| 1106 | if (p.eatToken(.semicolon)) |_| { | ||
| 1107 | return p.addNode(.{ | ||
| 1108 | .tag = .if_simple, | ||
| 1109 | .main_token = if_token, | ||
| 1110 | .data = .{ | ||
| 1111 | .lhs = condition, | ||
| 1112 | .rhs = assign_expr, | ||
| 1113 | }, | ||
| 1114 | }); | ||
| 1115 | } | ||
| 1116 | else_required = true; | ||
| 1117 | break :blk assign_expr; | ||
| 1118 | }; | ||
| 1119 | _ = p.eatToken(.keyword_else) orelse { | ||
| 1120 | if (else_required) { | ||
| 1121 | try p.warn(.expected_semi_or_else); | ||
| 1122 | } | ||
| 1123 | return p.addNode(.{ | ||
| 1124 | .tag = .if_simple, | ||
| 1125 | .main_token = if_token, | ||
| 1126 | .data = .{ | ||
| 1127 | .lhs = condition, | ||
| 1128 | .rhs = then_expr, | ||
| 1129 | }, | ||
| 1130 | }); | ||
| 1131 | }; | ||
| 1132 | _ = try p.parsePayload(); | ||
| 1133 | const else_expr = try p.expectStatement(false); | ||
| 1134 | return p.addNode(.{ | ||
| 1135 | .tag = .@"if", | ||
| 1136 | .main_token = if_token, | ||
| 1137 | .data = .{ | ||
| 1138 | .lhs = condition, | ||
| 1139 | .rhs = try p.addExtra(Node.If{ | ||
| 1140 | .then_expr = then_expr, | ||
| 1141 | .else_expr = else_expr, | ||
| 1142 | }), | ||
| 1143 | }, | ||
| 1144 | }); | ||
| 1145 | } | ||
| 1146 | |||
| 1147 | /// LabeledStatement <- BlockLabel? (Block / LoopStatement) | ||
| 1148 | fn parseLabeledStatement(p: *Parser) !Node.Index { | ||
| 1149 | const label_token = p.parseBlockLabel(); | ||
| 1150 | const block = try p.parseBlock(); | ||
| 1151 | if (block != 0) return block; | ||
| 1152 | |||
| 1153 | const loop_stmt = try p.parseLoopStatement(); | ||
| 1154 | if (loop_stmt != 0) return loop_stmt; | ||
| 1155 | |||
| 1156 | if (label_token != 0) { | ||
| 1157 | const after_colon = p.tok_i; | ||
| 1158 | const node = try p.parseTypeExpr(); | ||
| 1159 | if (node != 0) { | ||
| 1160 | const a = try p.parseByteAlign(); | ||
| 1161 | const b = try p.parseAddrSpace(); | ||
| 1162 | const c = try p.parseLinkSection(); | ||
| 1163 | const d = if (p.eatToken(.equal) == null) 0 else try p.expectExpr(); | ||
| 1164 | if (a != 0 or b != 0 or c != 0 or d != 0) { | ||
| 1165 | return p.failMsg(.{ .tag = .expected_var_const, .token = label_token }); | ||
| 1166 | } | ||
| 1167 | } | ||
| 1168 | return p.failMsg(.{ .tag = .expected_labelable, .token = after_colon }); | ||
| 1169 | } | ||
| 1170 | |||
| 1171 | return null_node; | ||
| 1172 | } | ||
| 1173 | |||
| 1174 | /// LoopStatement <- KEYWORD_inline? (ForStatement / WhileStatement) | ||
| 1175 | fn parseLoopStatement(p: *Parser) !Node.Index { | ||
| 1176 | const inline_token = p.eatToken(.keyword_inline); | ||
| 1177 | |||
| 1178 | const for_statement = try p.parseForStatement(); | ||
| 1179 | if (for_statement != 0) return for_statement; | ||
| 1180 | |||
| 1181 | const while_statement = try p.parseWhileStatement(); | ||
| 1182 | if (while_statement != 0) return while_statement; | ||
| 1183 | |||
| 1184 | if (inline_token == null) return null_node; | ||
| 1185 | |||
| 1186 | // If we've seen "inline", there should have been a "for" or "while" | ||
| 1187 | return p.fail(.expected_inlinable); | ||
| 1188 | } | ||
| 1189 | |||
| 1190 | /// ForPrefix <- KEYWORD_for LPAREN Expr RPAREN PtrIndexPayload | ||
| 1191 | /// | ||
| 1192 | /// ForStatement | ||
| 1193 | /// <- ForPrefix BlockExpr ( KEYWORD_else Statement )? | ||
| 1194 | /// / ForPrefix AssignExpr ( SEMICOLON / KEYWORD_else Statement ) | ||
| 1195 | fn parseForStatement(p: *Parser) !Node.Index { | ||
| 1196 | const for_token = p.eatToken(.keyword_for) orelse return null_node; | ||
| 1197 | _ = try p.expectToken(.l_paren); | ||
| 1198 | const array_expr = try p.expectExpr(); | ||
| 1199 | _ = try p.expectToken(.r_paren); | ||
| 1200 | const found_payload = try p.parsePtrIndexPayload(); | ||
| 1201 | if (found_payload == 0) try p.warn(.expected_loop_payload); | ||
| 1202 | |||
| 1203 | // TODO propose to change the syntax so that semicolons are always required | ||
| 1204 | // inside while statements, even if there is an `else`. | ||
| 1205 | var else_required = false; | ||
| 1206 | const then_expr = blk: { | ||
| 1207 | const block_expr = try p.parseBlockExpr(); | ||
| 1208 | if (block_expr != 0) break :blk block_expr; | ||
| 1209 | const assign_expr = try p.parseAssignExpr(); | ||
| 1210 | if (assign_expr == 0) { | ||
| 1211 | return p.fail(.expected_block_or_assignment); | ||
| 1212 | } | ||
| 1213 | if (p.eatToken(.semicolon)) |_| { | ||
| 1214 | return p.addNode(.{ | ||
| 1215 | .tag = .for_simple, | ||
| 1216 | .main_token = for_token, | ||
| 1217 | .data = .{ | ||
| 1218 | .lhs = array_expr, | ||
| 1219 | .rhs = assign_expr, | ||
| 1220 | }, | ||
| 1221 | }); | ||
| 1222 | } | ||
| 1223 | else_required = true; | ||
| 1224 | break :blk assign_expr; | ||
| 1225 | }; | ||
| 1226 | _ = p.eatToken(.keyword_else) orelse { | ||
| 1227 | if (else_required) { | ||
| 1228 | try p.warn(.expected_semi_or_else); | ||
| 1229 | } | ||
| 1230 | return p.addNode(.{ | ||
| 1231 | .tag = .for_simple, | ||
| 1232 | .main_token = for_token, | ||
| 1233 | .data = .{ | ||
| 1234 | .lhs = array_expr, | ||
| 1235 | .rhs = then_expr, | ||
| 1236 | }, | ||
| 1237 | }); | ||
| 1238 | }; | ||
| 1239 | return p.addNode(.{ | ||
| 1240 | .tag = .@"for", | ||
| 1241 | .main_token = for_token, | ||
| 1242 | .data = .{ | ||
| 1243 | .lhs = array_expr, | ||
| 1244 | .rhs = try p.addExtra(Node.If{ | ||
| 1245 | .then_expr = then_expr, | ||
| 1246 | .else_expr = try p.expectStatement(false), | ||
| 1247 | }), | ||
| 1248 | }, | ||
| 1249 | }); | ||
| 1250 | } | ||
| 1251 | |||
| 1252 | /// WhilePrefix <- KEYWORD_while LPAREN Expr RPAREN PtrPayload? WhileContinueExpr? | ||
| 1253 | /// | ||
| 1254 | /// WhileStatement | ||
| 1255 | /// <- WhilePrefix BlockExpr ( KEYWORD_else Payload? Statement )? | ||
| 1256 | /// / WhilePrefix AssignExpr ( SEMICOLON / KEYWORD_else Payload? Statement ) | ||
| 1257 | fn parseWhileStatement(p: *Parser) !Node.Index { | ||
| 1258 | const while_token = p.eatToken(.keyword_while) orelse return null_node; | ||
| 1259 | _ = try p.expectToken(.l_paren); | ||
| 1260 | const condition = try p.expectExpr(); | ||
| 1261 | _ = try p.expectToken(.r_paren); | ||
| 1262 | _ = try p.parsePtrPayload(); | ||
| 1263 | const cont_expr = try p.parseWhileContinueExpr(); | ||
| 1264 | |||
| 1265 | // TODO propose to change the syntax so that semicolons are always required | ||
| 1266 | // inside while statements, even if there is an `else`. | ||
| 1267 | var else_required = false; | ||
| 1268 | const then_expr = blk: { | ||
| 1269 | const block_expr = try p.parseBlockExpr(); | ||
| 1270 | if (block_expr != 0) break :blk block_expr; | ||
| 1271 | const assign_expr = try p.parseAssignExpr(); | ||
| 1272 | if (assign_expr == 0) { | ||
| 1273 | return p.fail(.expected_block_or_assignment); | ||
| 1274 | } | ||
| 1275 | if (p.eatToken(.semicolon)) |_| { | ||
| 1276 | if (cont_expr == 0) { | ||
| 1277 | return p.addNode(.{ | ||
| 1278 | .tag = .while_simple, | ||
| 1279 | .main_token = while_token, | ||
| 1280 | .data = .{ | ||
| 1281 | .lhs = condition, | ||
| 1282 | .rhs = assign_expr, | ||
| 1283 | }, | ||
| 1284 | }); | ||
| 1285 | } else { | ||
| 1286 | return p.addNode(.{ | ||
| 1287 | .tag = .while_cont, | ||
| 1288 | .main_token = while_token, | ||
| 1289 | .data = .{ | ||
| 1290 | .lhs = condition, | ||
| 1291 | .rhs = try p.addExtra(Node.WhileCont{ | ||
| 1292 | .cont_expr = cont_expr, | ||
| 1293 | .then_expr = assign_expr, | ||
| 1294 | }), | ||
| 1295 | }, | ||
| 1296 | }); | ||
| 1297 | } | ||
| 1298 | } | ||
| 1299 | else_required = true; | ||
| 1300 | break :blk assign_expr; | ||
| 1301 | }; | ||
| 1302 | _ = p.eatToken(.keyword_else) orelse { | ||
| 1303 | if (else_required) { | ||
| 1304 | try p.warn(.expected_semi_or_else); | ||
| 1305 | } | ||
| 1306 | if (cont_expr == 0) { | ||
| 1307 | return p.addNode(.{ | ||
| 1308 | .tag = .while_simple, | ||
| 1309 | .main_token = while_token, | ||
| 1310 | .data = .{ | ||
| 1311 | .lhs = condition, | ||
| 1312 | .rhs = then_expr, | ||
| 1313 | }, | ||
| 1314 | }); | ||
| 1315 | } else { | ||
| 1316 | return p.addNode(.{ | ||
| 1317 | .tag = .while_cont, | ||
| 1318 | .main_token = while_token, | ||
| 1319 | .data = .{ | ||
| 1320 | .lhs = condition, | ||
| 1321 | .rhs = try p.addExtra(Node.WhileCont{ | ||
| 1322 | .cont_expr = cont_expr, | ||
| 1323 | .then_expr = then_expr, | ||
| 1324 | }), | ||
| 1325 | }, | ||
| 1326 | }); | ||
| 1327 | } | ||
| 1328 | }; | ||
| 1329 | _ = try p.parsePayload(); | ||
| 1330 | const else_expr = try p.expectStatement(false); | ||
| 1331 | return p.addNode(.{ | ||
| 1332 | .tag = .@"while", | ||
| 1333 | .main_token = while_token, | ||
| 1334 | .data = .{ | ||
| 1335 | .lhs = condition, | ||
| 1336 | .rhs = try p.addExtra(Node.While{ | ||
| 1337 | .cont_expr = cont_expr, | ||
| 1338 | .then_expr = then_expr, | ||
| 1339 | .else_expr = else_expr, | ||
| 1340 | }), | ||
| 1341 | }, | ||
| 1342 | }); | ||
| 1343 | } | ||
| 1344 | |||
| 1345 | /// BlockExprStatement | ||
| 1346 | /// <- BlockExpr | ||
| 1347 | /// / AssignExpr SEMICOLON | ||
| 1348 | fn parseBlockExprStatement(p: *Parser) !Node.Index { | ||
| 1349 | const block_expr = try p.parseBlockExpr(); | ||
| 1350 | if (block_expr != 0) { | ||
| 1351 | return block_expr; | ||
| 1352 | } | ||
| 1353 | const assign_expr = try p.parseAssignExpr(); | ||
| 1354 | if (assign_expr != 0) { | ||
| 1355 | try p.expectSemicolon(.expected_semi_after_stmt, true); | ||
| 1356 | return assign_expr; | ||
| 1357 | } | ||
| 1358 | return null_node; | ||
| 1359 | } | ||
| 1360 | |||
| 1361 | fn expectBlockExprStatement(p: *Parser) !Node.Index { | ||
| 1362 | const node = try p.parseBlockExprStatement(); | ||
| 1363 | if (node == 0) { | ||
| 1364 | return p.fail(.expected_block_or_expr); | ||
| 1365 | } | ||
| 1366 | return node; | ||
| 1367 | } | ||
| 1368 | |||
| 1369 | /// BlockExpr <- BlockLabel? Block | ||
| 1370 | fn parseBlockExpr(p: *Parser) Error!Node.Index { | ||
| 1371 | switch (p.token_tags[p.tok_i]) { | ||
| 1372 | .identifier => { | ||
| 1373 | if (p.token_tags[p.tok_i + 1] == .colon and | ||
| 1374 | p.token_tags[p.tok_i + 2] == .l_brace) | ||
| 1375 | { | ||
| 1376 | p.tok_i += 2; | ||
| 1377 | return p.parseBlock(); | ||
| 1378 | } else { | ||
| 1379 | return null_node; | ||
| 1380 | } | ||
| 1381 | }, | ||
| 1382 | .l_brace => return p.parseBlock(), | ||
| 1383 | else => return null_node, | ||
| 1384 | } | ||
| 1385 | } | ||
| 1386 | |||
| 1387 | /// AssignExpr <- Expr (AssignOp Expr)? | ||
| 1388 | /// | ||
| 1389 | /// AssignOp | ||
| 1390 | /// <- ASTERISKEQUAL | ||
| 1391 | /// / ASTERISKPIPEEQUAL | ||
| 1392 | /// / SLASHEQUAL | ||
| 1393 | /// / PERCENTEQUAL | ||
| 1394 | /// / PLUSEQUAL | ||
| 1395 | /// / PLUSPIPEEQUAL | ||
| 1396 | /// / MINUSEQUAL | ||
| 1397 | /// / MINUSPIPEEQUAL | ||
| 1398 | /// / LARROW2EQUAL | ||
| 1399 | /// / LARROW2PIPEEQUAL | ||
| 1400 | /// / RARROW2EQUAL | ||
| 1401 | /// / AMPERSANDEQUAL | ||
| 1402 | /// / CARETEQUAL | ||
| 1403 | /// / PIPEEQUAL | ||
| 1404 | /// / ASTERISKPERCENTEQUAL | ||
| 1405 | /// / PLUSPERCENTEQUAL | ||
| 1406 | /// / MINUSPERCENTEQUAL | ||
| 1407 | /// / EQUAL | ||
| 1408 | fn parseAssignExpr(p: *Parser) !Node.Index { | ||
| 1409 | const expr = try p.parseExpr(); | ||
| 1410 | if (expr == 0) return null_node; | ||
| 1411 | |||
| 1412 | const tag: Node.Tag = switch (p.token_tags[p.tok_i]) { | ||
| 1413 | .asterisk_equal => .assign_mul, | ||
| 1414 | .slash_equal => .assign_div, | ||
| 1415 | .percent_equal => .assign_mod, | ||
| 1416 | .plus_equal => .assign_add, | ||
| 1417 | .minus_equal => .assign_sub, | ||
| 1418 | .angle_bracket_angle_bracket_left_equal => .assign_shl, | ||
| 1419 | .angle_bracket_angle_bracket_left_pipe_equal => .assign_shl_sat, | ||
| 1420 | .angle_bracket_angle_bracket_right_equal => .assign_shr, | ||
| 1421 | .ampersand_equal => .assign_bit_and, | ||
| 1422 | .caret_equal => .assign_bit_xor, | ||
| 1423 | .pipe_equal => .assign_bit_or, | ||
| 1424 | .asterisk_percent_equal => .assign_mul_wrap, | ||
| 1425 | .plus_percent_equal => .assign_add_wrap, | ||
| 1426 | .minus_percent_equal => .assign_sub_wrap, | ||
| 1427 | .asterisk_pipe_equal => .assign_mul_sat, | ||
| 1428 | .plus_pipe_equal => .assign_add_sat, | ||
| 1429 | .minus_pipe_equal => .assign_sub_sat, | ||
| 1430 | .equal => .assign, | ||
| 1431 | else => return expr, | ||
| 1432 | }; | ||
| 1433 | return p.addNode(.{ | ||
| 1434 | .tag = tag, | ||
| 1435 | .main_token = p.nextToken(), | ||
| 1436 | .data = .{ | ||
| 1437 | .lhs = expr, | ||
| 1438 | .rhs = try p.expectExpr(), | ||
| 1439 | }, | ||
| 1440 | }); | ||
| 1441 | } | ||
| 1442 | |||
| 1443 | fn expectAssignExpr(p: *Parser) !Node.Index { | ||
| 1444 | const expr = try p.parseAssignExpr(); | ||
| 1445 | if (expr == 0) { | ||
| 1446 | return p.fail(.expected_expr_or_assignment); | ||
| 1447 | } | ||
| 1448 | return expr; | ||
| 1449 | } | ||
| 1450 | |||
| 1451 | fn parseExpr(p: *Parser) Error!Node.Index { | ||
| 1452 | return p.parseExprPrecedence(0); | ||
| 1453 | } | ||
| 1454 | |||
| 1455 | fn expectExpr(p: *Parser) Error!Node.Index { | ||
| 1456 | const node = try p.parseExpr(); | ||
| 1457 | if (node == 0) { | ||
| 1458 | return p.fail(.expected_expr); | ||
| 1459 | } else { | ||
| 1460 | return node; | ||
| 1461 | } | ||
| 1462 | } | ||
| 1463 | |||
| 1464 | const Assoc = enum { | ||
| 1465 | left, | ||
| 1466 | none, | ||
| 1467 | }; | ||
| 1468 | |||
| 1469 | const OperInfo = struct { | ||
| 1470 | prec: i8, | ||
| 1471 | tag: Node.Tag, | ||
| 1472 | assoc: Assoc = Assoc.left, | ||
| 1473 | }; | ||
| 1474 | |||
| 1475 | // A table of binary operator information. Higher precedence numbers are | ||
| 1476 | // stickier. All operators at the same precedence level should have the same | ||
| 1477 | // associativity. | ||
| 1478 | const operTable = std.enums.directEnumArrayDefault(Token.Tag, OperInfo, .{ .prec = -1, .tag = Node.Tag.root }, 0, .{ | ||
| 1479 | .keyword_or = .{ .prec = 10, .tag = .bool_or }, | ||
| 1480 | |||
| 1481 | .keyword_and = .{ .prec = 20, .tag = .bool_and }, | ||
| 1482 | |||
| 1483 | .equal_equal = .{ .prec = 30, .tag = .equal_equal, .assoc = Assoc.none }, | ||
| 1484 | .bang_equal = .{ .prec = 30, .tag = .bang_equal, .assoc = Assoc.none }, | ||
| 1485 | .angle_bracket_left = .{ .prec = 30, .tag = .less_than, .assoc = Assoc.none }, | ||
| 1486 | .angle_bracket_right = .{ .prec = 30, .tag = .greater_than, .assoc = Assoc.none }, | ||
| 1487 | .angle_bracket_left_equal = .{ .prec = 30, .tag = .less_or_equal, .assoc = Assoc.none }, | ||
| 1488 | .angle_bracket_right_equal = .{ .prec = 30, .tag = .greater_or_equal, .assoc = Assoc.none }, | ||
| 1489 | |||
| 1490 | .ampersand = .{ .prec = 40, .tag = .bit_and }, | ||
| 1491 | .caret = .{ .prec = 40, .tag = .bit_xor }, | ||
| 1492 | .pipe = .{ .prec = 40, .tag = .bit_or }, | ||
| 1493 | .keyword_orelse = .{ .prec = 40, .tag = .@"orelse" }, | ||
| 1494 | .keyword_catch = .{ .prec = 40, .tag = .@"catch" }, | ||
| 1495 | |||
| 1496 | .angle_bracket_angle_bracket_left = .{ .prec = 50, .tag = .shl }, | ||
| 1497 | .angle_bracket_angle_bracket_left_pipe = .{ .prec = 50, .tag = .shl_sat }, | ||
| 1498 | .angle_bracket_angle_bracket_right = .{ .prec = 50, .tag = .shr }, | ||
| 1499 | |||
| 1500 | .plus = .{ .prec = 60, .tag = .add }, | ||
| 1501 | .minus = .{ .prec = 60, .tag = .sub }, | ||
| 1502 | .plus_plus = .{ .prec = 60, .tag = .array_cat }, | ||
| 1503 | .plus_percent = .{ .prec = 60, .tag = .add_wrap }, | ||
| 1504 | .minus_percent = .{ .prec = 60, .tag = .sub_wrap }, | ||
| 1505 | .plus_pipe = .{ .prec = 60, .tag = .add_sat }, | ||
| 1506 | .minus_pipe = .{ .prec = 60, .tag = .sub_sat }, | ||
| 1507 | |||
| 1508 | .pipe_pipe = .{ .prec = 70, .tag = .merge_error_sets }, | ||
| 1509 | .asterisk = .{ .prec = 70, .tag = .mul }, | ||
| 1510 | .slash = .{ .prec = 70, .tag = .div }, | ||
| 1511 | .percent = .{ .prec = 70, .tag = .mod }, | ||
| 1512 | .asterisk_asterisk = .{ .prec = 70, .tag = .array_mult }, | ||
| 1513 | .asterisk_percent = .{ .prec = 70, .tag = .mul_wrap }, | ||
| 1514 | .asterisk_pipe = .{ .prec = 70, .tag = .mul_sat }, | ||
| 1515 | }); | ||
| 1516 | |||
| 1517 | fn parseExprPrecedence(p: *Parser, min_prec: i32) Error!Node.Index { | ||
| 1518 | assert(min_prec >= 0); | ||
| 1519 | var node = try p.parsePrefixExpr(); | ||
| 1520 | if (node == 0) { | ||
| 1521 | return null_node; | ||
| 1522 | } | ||
| 1523 | |||
| 1524 | var banned_prec: i8 = -1; | ||
| 1525 | |||
| 1526 | while (true) { | ||
| 1527 | const tok_tag = p.token_tags[p.tok_i]; | ||
| 1528 | const info = operTable[@intCast(usize, @enumToInt(tok_tag))]; | ||
| 1529 | if (info.prec < min_prec) { | ||
| 1530 | break; | ||
| 1531 | } | ||
| 1532 | if (info.prec == banned_prec) { | ||
| 1533 | return p.fail(.chained_comparison_operators); | ||
| 1534 | } | ||
| 1535 | |||
| 1536 | const oper_token = p.nextToken(); | ||
| 1537 | // Special-case handling for "catch" | ||
| 1538 | if (tok_tag == .keyword_catch) { | ||
| 1539 | _ = try p.parsePayload(); | ||
| 1540 | } | ||
| 1541 | const rhs = try p.parseExprPrecedence(info.prec + 1); | ||
| 1542 | if (rhs == 0) { | ||
| 1543 | try p.warn(.expected_expr); | ||
| 1544 | return node; | ||
| 1545 | } | ||
| 1546 | |||
| 1547 | { | ||
| 1548 | const tok_len = tok_tag.lexeme().?.len; | ||
| 1549 | const char_before = p.source[p.token_starts[oper_token] - 1]; | ||
| 1550 | const char_after = p.source[p.token_starts[oper_token] + tok_len]; | ||
| 1551 | if (tok_tag == .ampersand and char_after == '&') { | ||
| 1552 | // without types we don't know if '&&' was intended as 'bitwise_and address_of', or a c-style logical_and | ||
| 1553 | // The best the parser can do is recommend changing it to 'and' or ' & &' | ||
| 1554 | try p.warnMsg(.{ .tag = .invalid_ampersand_ampersand, .token = oper_token }); | ||
| 1555 | } else if (std.ascii.isWhitespace(char_before) != std.ascii.isWhitespace(char_after)) { | ||
| 1556 | try p.warnMsg(.{ .tag = .mismatched_binary_op_whitespace, .token = oper_token }); | ||
| 1557 | } | ||
| 1558 | } | ||
| 1559 | |||
| 1560 | node = try p.addNode(.{ | ||
| 1561 | .tag = info.tag, | ||
| 1562 | .main_token = oper_token, | ||
| 1563 | .data = .{ | ||
| 1564 | .lhs = node, | ||
| 1565 | .rhs = rhs, | ||
| 1566 | }, | ||
| 1567 | }); | ||
| 1568 | |||
| 1569 | if (info.assoc == Assoc.none) { | ||
| 1570 | banned_prec = info.prec; | ||
| 1571 | } | ||
| 1572 | } | ||
| 1573 | |||
| 1574 | return node; | ||
| 1575 | } | ||
| 1576 | |||
| 1577 | /// PrefixExpr <- PrefixOp* PrimaryExpr | ||
| 1578 | /// | ||
| 1579 | /// PrefixOp | ||
| 1580 | /// <- EXCLAMATIONMARK | ||
| 1581 | /// / MINUS | ||
| 1582 | /// / TILDE | ||
| 1583 | /// / MINUSPERCENT | ||
| 1584 | /// / AMPERSAND | ||
| 1585 | /// / KEYWORD_try | ||
| 1586 | /// / KEYWORD_await | ||
| 1587 | fn parsePrefixExpr(p: *Parser) Error!Node.Index { | ||
| 1588 | const tag: Node.Tag = switch (p.token_tags[p.tok_i]) { | ||
| 1589 | .bang => .bool_not, | ||
| 1590 | .minus => .negation, | ||
| 1591 | .tilde => .bit_not, | ||
| 1592 | .minus_percent => .negation_wrap, | ||
| 1593 | .ampersand => .address_of, | ||
| 1594 | .keyword_try => .@"try", | ||
| 1595 | .keyword_await => .@"await", | ||
| 1596 | else => return p.parsePrimaryExpr(), | ||
| 1597 | }; | ||
| 1598 | return p.addNode(.{ | ||
| 1599 | .tag = tag, | ||
| 1600 | .main_token = p.nextToken(), | ||
| 1601 | .data = .{ | ||
| 1602 | .lhs = try p.expectPrefixExpr(), | ||
| 1603 | .rhs = undefined, | ||
| 1604 | }, | ||
| 1605 | }); | ||
| 1606 | } | ||
| 1607 | |||
| 1608 | fn expectPrefixExpr(p: *Parser) Error!Node.Index { | ||
| 1609 | const node = try p.parsePrefixExpr(); | ||
| 1610 | if (node == 0) { | ||
| 1611 | return p.fail(.expected_prefix_expr); | ||
| 1612 | } | ||
| 1613 | return node; | ||
| 1614 | } | ||
| 1615 | |||
| 1616 | /// TypeExpr <- PrefixTypeOp* ErrorUnionExpr | ||
| 1617 | /// | ||
| 1618 | /// PrefixTypeOp | ||
| 1619 | /// <- QUESTIONMARK | ||
| 1620 | /// / KEYWORD_anyframe MINUSRARROW | ||
| 1621 | /// / SliceTypeStart (ByteAlign / AddrSpace / KEYWORD_const / KEYWORD_volatile / KEYWORD_allowzero)* | ||
| 1622 | /// / PtrTypeStart (AddrSpace / KEYWORD_align LPAREN Expr (COLON Expr COLON Expr)? RPAREN / KEYWORD_const / KEYWORD_volatile / KEYWORD_allowzero)* | ||
| 1623 | /// / ArrayTypeStart | ||
| 1624 | /// | ||
| 1625 | /// SliceTypeStart <- LBRACKET (COLON Expr)? RBRACKET | ||
| 1626 | /// | ||
| 1627 | /// PtrTypeStart | ||
| 1628 | /// <- ASTERISK | ||
| 1629 | /// / ASTERISK2 | ||
| 1630 | /// / LBRACKET ASTERISK (LETTERC / COLON Expr)? RBRACKET | ||
| 1631 | /// | ||
| 1632 | /// ArrayTypeStart <- LBRACKET Expr (COLON Expr)? RBRACKET | ||
| 1633 | fn parseTypeExpr(p: *Parser) Error!Node.Index { | ||
| 1634 | switch (p.token_tags[p.tok_i]) { | ||
| 1635 | .question_mark => return p.addNode(.{ | ||
| 1636 | .tag = .optional_type, | ||
| 1637 | .main_token = p.nextToken(), | ||
| 1638 | .data = .{ | ||
| 1639 | .lhs = try p.expectTypeExpr(), | ||
| 1640 | .rhs = undefined, | ||
| 1641 | }, | ||
| 1642 | }), | ||
| 1643 | .keyword_anyframe => switch (p.token_tags[p.tok_i + 1]) { | ||
| 1644 | .arrow => return p.addNode(.{ | ||
| 1645 | .tag = .anyframe_type, | ||
| 1646 | .main_token = p.nextToken(), | ||
| 1647 | .data = .{ | ||
| 1648 | .lhs = p.nextToken(), | ||
| 1649 | .rhs = try p.expectTypeExpr(), | ||
| 1650 | }, | ||
| 1651 | }), | ||
| 1652 | else => return p.parseErrorUnionExpr(), | ||
| 1653 | }, | ||
| 1654 | .asterisk => { | ||
| 1655 | const asterisk = p.nextToken(); | ||
| 1656 | const mods = try p.parsePtrModifiers(); | ||
| 1657 | const elem_type = try p.expectTypeExpr(); | ||
| 1658 | if (mods.bit_range_start != 0) { | ||
| 1659 | return p.addNode(.{ | ||
| 1660 | .tag = .ptr_type_bit_range, | ||
| 1661 | .main_token = asterisk, | ||
| 1662 | .data = .{ | ||
| 1663 | .lhs = try p.addExtra(Node.PtrTypeBitRange{ | ||
| 1664 | .sentinel = 0, | ||
| 1665 | .align_node = mods.align_node, | ||
| 1666 | .addrspace_node = mods.addrspace_node, | ||
| 1667 | .bit_range_start = mods.bit_range_start, | ||
| 1668 | .bit_range_end = mods.bit_range_end, | ||
| 1669 | }), | ||
| 1670 | .rhs = elem_type, | ||
| 1671 | }, | ||
| 1672 | }); | ||
| 1673 | } else if (mods.addrspace_node != 0) { | ||
| 1674 | return p.addNode(.{ | ||
| 1675 | .tag = .ptr_type, | ||
| 1676 | .main_token = asterisk, | ||
| 1677 | .data = .{ | ||
| 1678 | .lhs = try p.addExtra(Node.PtrType{ | ||
| 1679 | .sentinel = 0, | ||
| 1680 | .align_node = mods.align_node, | ||
| 1681 | .addrspace_node = mods.addrspace_node, | ||
| 1682 | }), | ||
| 1683 | .rhs = elem_type, | ||
| 1684 | }, | ||
| 1685 | }); | ||
| 1686 | } else { | ||
| 1687 | return p.addNode(.{ | ||
| 1688 | .tag = .ptr_type_aligned, | ||
| 1689 | .main_token = asterisk, | ||
| 1690 | .data = .{ | ||
| 1691 | .lhs = mods.align_node, | ||
| 1692 | .rhs = elem_type, | ||
| 1693 | }, | ||
| 1694 | }); | ||
| 1695 | } | ||
| 1696 | }, | ||
| 1697 | .asterisk_asterisk => { | ||
| 1698 | const asterisk = p.nextToken(); | ||
| 1699 | const mods = try p.parsePtrModifiers(); | ||
| 1700 | const elem_type = try p.expectTypeExpr(); | ||
| 1701 | const inner: Node.Index = inner: { | ||
| 1702 | if (mods.bit_range_start != 0) { | ||
| 1703 | break :inner try p.addNode(.{ | ||
| 1704 | .tag = .ptr_type_bit_range, | ||
| 1705 | .main_token = asterisk, | ||
| 1706 | .data = .{ | ||
| 1707 | .lhs = try p.addExtra(Node.PtrTypeBitRange{ | ||
| 1708 | .sentinel = 0, | ||
| 1709 | .align_node = mods.align_node, | ||
| 1710 | .addrspace_node = mods.addrspace_node, | ||
| 1711 | .bit_range_start = mods.bit_range_start, | ||
| 1712 | .bit_range_end = mods.bit_range_end, | ||
| 1713 | }), | ||
| 1714 | .rhs = elem_type, | ||
| 1715 | }, | ||
| 1716 | }); | ||
| 1717 | } else if (mods.addrspace_node != 0) { | ||
| 1718 | break :inner try p.addNode(.{ | ||
| 1719 | .tag = .ptr_type, | ||
| 1720 | .main_token = asterisk, | ||
| 1721 | .data = .{ | ||
| 1722 | .lhs = try p.addExtra(Node.PtrType{ | ||
| 1723 | .sentinel = 0, | ||
| 1724 | .align_node = mods.align_node, | ||
| 1725 | .addrspace_node = mods.addrspace_node, | ||
| 1726 | }), | ||
| 1727 | .rhs = elem_type, | ||
| 1728 | }, | ||
| 1729 | }); | ||
| 1730 | } else { | ||
| 1731 | break :inner try p.addNode(.{ | ||
| 1732 | .tag = .ptr_type_aligned, | ||
| 1733 | .main_token = asterisk, | ||
| 1734 | .data = .{ | ||
| 1735 | .lhs = mods.align_node, | ||
| 1736 | .rhs = elem_type, | ||
| 1737 | }, | ||
| 1738 | }); | ||
| 1739 | } | ||
| 1740 | }; | ||
| 1741 | return p.addNode(.{ | ||
| 1742 | .tag = .ptr_type_aligned, | ||
| 1743 | .main_token = asterisk, | ||
| 1744 | .data = .{ | ||
| 1745 | .lhs = 0, | ||
| 1746 | .rhs = inner, | ||
| 1747 | }, | ||
| 1748 | }); | ||
| 1749 | }, | ||
| 1750 | .l_bracket => switch (p.token_tags[p.tok_i + 1]) { | ||
| 1751 | .asterisk => { | ||
| 1752 | _ = p.nextToken(); | ||
| 1753 | const asterisk = p.nextToken(); | ||
| 1754 | var sentinel: Node.Index = 0; | ||
| 1755 | if (p.eatToken(.identifier)) |ident| { | ||
| 1756 | const ident_slice = p.source[p.token_starts[ident]..p.token_starts[ident + 1]]; | ||
| 1757 | if (!std.mem.eql(u8, std.mem.trimRight(u8, ident_slice, &std.ascii.whitespace), "c")) { | ||
| 1758 | p.tok_i -= 1; | ||
| 1759 | } | ||
| 1760 | } else if (p.eatToken(.colon)) |_| { | ||
| 1761 | sentinel = try p.expectExpr(); | ||
| 1762 | } | ||
| 1763 | _ = try p.expectToken(.r_bracket); | ||
| 1764 | const mods = try p.parsePtrModifiers(); | ||
| 1765 | const elem_type = try p.expectTypeExpr(); | ||
| 1766 | if (mods.bit_range_start == 0) { | ||
| 1767 | if (sentinel == 0 and mods.addrspace_node == 0) { | ||
| 1768 | return p.addNode(.{ | ||
| 1769 | .tag = .ptr_type_aligned, | ||
| 1770 | .main_token = asterisk, | ||
| 1771 | .data = .{ | ||
| 1772 | .lhs = mods.align_node, | ||
| 1773 | .rhs = elem_type, | ||
| 1774 | }, | ||
| 1775 | }); | ||
| 1776 | } else if (mods.align_node == 0 and mods.addrspace_node == 0) { | ||
| 1777 | return p.addNode(.{ | ||
| 1778 | .tag = .ptr_type_sentinel, | ||
| 1779 | .main_token = asterisk, | ||
| 1780 | .data = .{ | ||
| 1781 | .lhs = sentinel, | ||
| 1782 | .rhs = elem_type, | ||
| 1783 | }, | ||
| 1784 | }); | ||
| 1785 | } else { | ||
| 1786 | return p.addNode(.{ | ||
| 1787 | .tag = .ptr_type, | ||
| 1788 | .main_token = asterisk, | ||
| 1789 | .data = .{ | ||
| 1790 | .lhs = try p.addExtra(Node.PtrType{ | ||
| 1791 | .sentinel = sentinel, | ||
| 1792 | .align_node = mods.align_node, | ||
| 1793 | .addrspace_node = mods.addrspace_node, | ||
| 1794 | }), | ||
| 1795 | .rhs = elem_type, | ||
| 1796 | }, | ||
| 1797 | }); | ||
| 1798 | } | ||
| 1799 | } else { | ||
| 1800 | return p.addNode(.{ | ||
| 1801 | .tag = .ptr_type_bit_range, | ||
| 1802 | .main_token = asterisk, | ||
| 1803 | .data = .{ | ||
| 1804 | .lhs = try p.addExtra(Node.PtrTypeBitRange{ | ||
| 1805 | .sentinel = sentinel, | ||
| 1806 | .align_node = mods.align_node, | ||
| 1807 | .addrspace_node = mods.addrspace_node, | ||
| 1808 | .bit_range_start = mods.bit_range_start, | ||
| 1809 | .bit_range_end = mods.bit_range_end, | ||
| 1810 | }), | ||
| 1811 | .rhs = elem_type, | ||
| 1812 | }, | ||
| 1813 | }); | ||
| 1814 | } | ||
| 1815 | }, | ||
| 1816 | else => { | ||
| 1817 | const lbracket = p.nextToken(); | ||
| 1818 | const len_expr = try p.parseExpr(); | ||
| 1819 | const sentinel: Node.Index = if (p.eatToken(.colon)) |_| | ||
| 1820 | try p.expectExpr() | ||
| 1821 | else | ||
| 1822 | 0; | ||
| 1823 | _ = try p.expectToken(.r_bracket); | ||
| 1824 | if (len_expr == 0) { | ||
| 1825 | const mods = try p.parsePtrModifiers(); | ||
| 1826 | const elem_type = try p.expectTypeExpr(); | ||
| 1827 | if (mods.bit_range_start != 0) { | ||
| 1828 | try p.warnMsg(.{ | ||
| 1829 | .tag = .invalid_bit_range, | ||
| 1830 | .token = p.nodes.items(.main_token)[mods.bit_range_start], | ||
| 1831 | }); | ||
| 1832 | } | ||
| 1833 | if (sentinel == 0 and mods.addrspace_node == 0) { | ||
| 1834 | return p.addNode(.{ | ||
| 1835 | .tag = .ptr_type_aligned, | ||
| 1836 | .main_token = lbracket, | ||
| 1837 | .data = .{ | ||
| 1838 | .lhs = mods.align_node, | ||
| 1839 | .rhs = elem_type, | ||
| 1840 | }, | ||
| 1841 | }); | ||
| 1842 | } else if (mods.align_node == 0 and mods.addrspace_node == 0) { | ||
| 1843 | return p.addNode(.{ | ||
| 1844 | .tag = .ptr_type_sentinel, | ||
| 1845 | .main_token = lbracket, | ||
| 1846 | .data = .{ | ||
| 1847 | .lhs = sentinel, | ||
| 1848 | .rhs = elem_type, | ||
| 1849 | }, | ||
| 1850 | }); | ||
| 1851 | } else { | ||
| 1852 | return p.addNode(.{ | ||
| 1853 | .tag = .ptr_type, | ||
| 1854 | .main_token = lbracket, | ||
| 1855 | .data = .{ | ||
| 1856 | .lhs = try p.addExtra(Node.PtrType{ | ||
| 1857 | .sentinel = sentinel, | ||
| 1858 | .align_node = mods.align_node, | ||
| 1859 | .addrspace_node = mods.addrspace_node, | ||
| 1860 | }), | ||
| 1861 | .rhs = elem_type, | ||
| 1862 | }, | ||
| 1863 | }); | ||
| 1864 | } | ||
| 1865 | } else { | ||
| 1866 | switch (p.token_tags[p.tok_i]) { | ||
| 1867 | .keyword_align, | ||
| 1868 | .keyword_const, | ||
| 1869 | .keyword_volatile, | ||
| 1870 | .keyword_allowzero, | ||
| 1871 | .keyword_addrspace, | ||
| 1872 | => return p.fail(.ptr_mod_on_array_child_type), | ||
| 1873 | else => {}, | ||
| 1874 | } | ||
| 1875 | const elem_type = try p.expectTypeExpr(); | ||
| 1876 | if (sentinel == 0) { | ||
| 1877 | return p.addNode(.{ | ||
| 1878 | .tag = .array_type, | ||
| 1879 | .main_token = lbracket, | ||
| 1880 | .data = .{ | ||
| 1881 | .lhs = len_expr, | ||
| 1882 | .rhs = elem_type, | ||
| 1883 | }, | ||
| 1884 | }); | ||
| 1885 | } else { | ||
| 1886 | return p.addNode(.{ | ||
| 1887 | .tag = .array_type_sentinel, | ||
| 1888 | .main_token = lbracket, | ||
| 1889 | .data = .{ | ||
| 1890 | .lhs = len_expr, | ||
| 1891 | .rhs = try p.addExtra(.{ | ||
| 1892 | .elem_type = elem_type, | ||
| 1893 | .sentinel = sentinel, | ||
| 1894 | }), | ||
| 1895 | }, | ||
| 1896 | }); | ||
| 1897 | } | ||
| 1898 | } | ||
| 1899 | }, | ||
| 1900 | }, | ||
| 1901 | else => return p.parseErrorUnionExpr(), | ||
| 1902 | } | ||
| 1903 | } | ||
| 1904 | |||
| 1905 | fn expectTypeExpr(p: *Parser) Error!Node.Index { | ||
| 1906 | const node = try p.parseTypeExpr(); | ||
| 1907 | if (node == 0) { | ||
| 1908 | return p.fail(.expected_type_expr); | ||
| 1909 | } | ||
| 1910 | return node; | ||
| 1911 | } | ||
| 1912 | |||
| 1913 | /// PrimaryExpr | ||
| 1914 | /// <- AsmExpr | ||
| 1915 | /// / IfExpr | ||
| 1916 | /// / KEYWORD_break BreakLabel? Expr? | ||
| 1917 | /// / KEYWORD_comptime Expr | ||
| 1918 | /// / KEYWORD_nosuspend Expr | ||
| 1919 | /// / KEYWORD_continue BreakLabel? | ||
| 1920 | /// / KEYWORD_resume Expr | ||
| 1921 | /// / KEYWORD_return Expr? | ||
| 1922 | /// / BlockLabel? LoopExpr | ||
| 1923 | /// / Block | ||
| 1924 | /// / CurlySuffixExpr | ||
| 1925 | fn parsePrimaryExpr(p: *Parser) !Node.Index { | ||
| 1926 | switch (p.token_tags[p.tok_i]) { | ||
| 1927 | .keyword_asm => return p.expectAsmExpr(), | ||
| 1928 | .keyword_if => return p.parseIfExpr(), | ||
| 1929 | .keyword_break => { | ||
| 1930 | p.tok_i += 1; | ||
| 1931 | return p.addNode(.{ | ||
| 1932 | .tag = .@"break", | ||
| 1933 | .main_token = p.tok_i - 1, | ||
| 1934 | .data = .{ | ||
| 1935 | .lhs = try p.parseBreakLabel(), | ||
| 1936 | .rhs = try p.parseExpr(), | ||
| 1937 | }, | ||
| 1938 | }); | ||
| 1939 | }, | ||
| 1940 | .keyword_continue => { | ||
| 1941 | p.tok_i += 1; | ||
| 1942 | return p.addNode(.{ | ||
| 1943 | .tag = .@"continue", | ||
| 1944 | .main_token = p.tok_i - 1, | ||
| 1945 | .data = .{ | ||
| 1946 | .lhs = try p.parseBreakLabel(), | ||
| 1947 | .rhs = undefined, | ||
| 1948 | }, | ||
| 1949 | }); | ||
| 1950 | }, | ||
| 1951 | .keyword_comptime => { | ||
| 1952 | p.tok_i += 1; | ||
| 1953 | return p.addNode(.{ | ||
| 1954 | .tag = .@"comptime", | ||
| 1955 | .main_token = p.tok_i - 1, | ||
| 1956 | .data = .{ | ||
| 1957 | .lhs = try p.expectExpr(), | ||
| 1958 | .rhs = undefined, | ||
| 1959 | }, | ||
| 1960 | }); | ||
| 1961 | }, | ||
| 1962 | .keyword_nosuspend => { | ||
| 1963 | p.tok_i += 1; | ||
| 1964 | return p.addNode(.{ | ||
| 1965 | .tag = .@"nosuspend", | ||
| 1966 | .main_token = p.tok_i - 1, | ||
| 1967 | .data = .{ | ||
| 1968 | .lhs = try p.expectExpr(), | ||
| 1969 | .rhs = undefined, | ||
| 1970 | }, | ||
| 1971 | }); | ||
| 1972 | }, | ||
| 1973 | .keyword_resume => { | ||
| 1974 | p.tok_i += 1; | ||
| 1975 | return p.addNode(.{ | ||
| 1976 | .tag = .@"resume", | ||
| 1977 | .main_token = p.tok_i - 1, | ||
| 1978 | .data = .{ | ||
| 1979 | .lhs = try p.expectExpr(), | ||
| 1980 | .rhs = undefined, | ||
| 1981 | }, | ||
| 1982 | }); | ||
| 1983 | }, | ||
| 1984 | .keyword_return => { | ||
| 1985 | p.tok_i += 1; | ||
| 1986 | return p.addNode(.{ | ||
| 1987 | .tag = .@"return", | ||
| 1988 | .main_token = p.tok_i - 1, | ||
| 1989 | .data = .{ | ||
| 1990 | .lhs = try p.parseExpr(), | ||
| 1991 | .rhs = undefined, | ||
| 1992 | }, | ||
| 1993 | }); | ||
| 1994 | }, | ||
| 1995 | .identifier => { | ||
| 1996 | if (p.token_tags[p.tok_i + 1] == .colon) { | ||
| 1997 | switch (p.token_tags[p.tok_i + 2]) { | ||
| 1998 | .keyword_inline => { | ||
| 1999 | p.tok_i += 3; | ||
| 2000 | switch (p.token_tags[p.tok_i]) { | ||
| 2001 | .keyword_for => return p.parseForExpr(), | ||
| 2002 | .keyword_while => return p.parseWhileExpr(), | ||
| 2003 | else => return p.fail(.expected_inlinable), | ||
| 2004 | } | ||
| 2005 | }, | ||
| 2006 | .keyword_for => { | ||
| 2007 | p.tok_i += 2; | ||
| 2008 | return p.parseForExpr(); | ||
| 2009 | }, | ||
| 2010 | .keyword_while => { | ||
| 2011 | p.tok_i += 2; | ||
| 2012 | return p.parseWhileExpr(); | ||
| 2013 | }, | ||
| 2014 | .l_brace => { | ||
| 2015 | p.tok_i += 2; | ||
| 2016 | return p.parseBlock(); | ||
| 2017 | }, | ||
| 2018 | else => return p.parseCurlySuffixExpr(), | ||
| 2019 | } | ||
| 2020 | } else { | ||
| 2021 | return p.parseCurlySuffixExpr(); | ||
| 2022 | } | ||
| 2023 | }, | ||
| 2024 | .keyword_inline => { | ||
| 2025 | p.tok_i += 1; | ||
| 2026 | switch (p.token_tags[p.tok_i]) { | ||
| 2027 | .keyword_for => return p.parseForExpr(), | ||
| 2028 | .keyword_while => return p.parseWhileExpr(), | ||
| 2029 | else => return p.fail(.expected_inlinable), | ||
| 2030 | } | ||
| 2031 | }, | ||
| 2032 | .keyword_for => return p.parseForExpr(), | ||
| 2033 | .keyword_while => return p.parseWhileExpr(), | ||
| 2034 | .l_brace => return p.parseBlock(), | ||
| 2035 | else => return p.parseCurlySuffixExpr(), | ||
| 2036 | } | ||
| 2037 | } | ||
| 2038 | |||
| 2039 | /// IfExpr <- IfPrefix Expr (KEYWORD_else Payload? Expr)? | ||
| 2040 | fn parseIfExpr(p: *Parser) !Node.Index { | ||
| 2041 | return p.parseIf(expectExpr); | ||
| 2042 | } | ||
| 2043 | |||
| 2044 | /// Block <- LBRACE Statement* RBRACE | ||
| 2045 | fn parseBlock(p: *Parser) !Node.Index { | ||
| 2046 | const lbrace = p.eatToken(.l_brace) orelse return null_node; | ||
| 2047 | const scratch_top = p.scratch.items.len; | ||
| 2048 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 2049 | while (true) { | ||
| 2050 | if (p.token_tags[p.tok_i] == .r_brace) break; | ||
| 2051 | const statement = try p.expectStatementRecoverable(); | ||
| 2052 | if (statement == 0) break; | ||
| 2053 | try p.scratch.append(p.gpa, statement); | ||
| 2054 | } | ||
| 2055 | _ = try p.expectToken(.r_brace); | ||
| 2056 | const semicolon = (p.token_tags[p.tok_i - 2] == .semicolon); | ||
| 2057 | const statements = p.scratch.items[scratch_top..]; | ||
| 2058 | switch (statements.len) { | ||
| 2059 | 0 => return p.addNode(.{ | ||
| 2060 | .tag = .block_two, | ||
| 2061 | .main_token = lbrace, | ||
| 2062 | .data = .{ | ||
| 2063 | .lhs = 0, | ||
| 2064 | .rhs = 0, | ||
| 2065 | }, | ||
| 2066 | }), | ||
| 2067 | 1 => return p.addNode(.{ | ||
| 2068 | .tag = if (semicolon) .block_two_semicolon else .block_two, | ||
| 2069 | .main_token = lbrace, | ||
| 2070 | .data = .{ | ||
| 2071 | .lhs = statements[0], | ||
| 2072 | .rhs = 0, | ||
| 2073 | }, | ||
| 2074 | }), | ||
| 2075 | 2 => return p.addNode(.{ | ||
| 2076 | .tag = if (semicolon) .block_two_semicolon else .block_two, | ||
| 2077 | .main_token = lbrace, | ||
| 2078 | .data = .{ | ||
| 2079 | .lhs = statements[0], | ||
| 2080 | .rhs = statements[1], | ||
| 2081 | }, | ||
| 2082 | }), | ||
| 2083 | else => { | ||
| 2084 | const span = try p.listToSpan(statements); | ||
| 2085 | return p.addNode(.{ | ||
| 2086 | .tag = if (semicolon) .block_semicolon else .block, | ||
| 2087 | .main_token = lbrace, | ||
| 2088 | .data = .{ | ||
| 2089 | .lhs = span.start, | ||
| 2090 | .rhs = span.end, | ||
| 2091 | }, | ||
| 2092 | }); | ||
| 2093 | }, | ||
| 2094 | } | ||
| 2095 | } | ||
| 2096 | |||
| 2097 | /// ForPrefix <- KEYWORD_for LPAREN Expr RPAREN PtrIndexPayload | ||
| 2098 | /// | ||
| 2099 | /// ForExpr <- ForPrefix Expr (KEYWORD_else Expr)? | ||
| 2100 | fn parseForExpr(p: *Parser) !Node.Index { | ||
| 2101 | const for_token = p.eatToken(.keyword_for) orelse return null_node; | ||
| 2102 | _ = try p.expectToken(.l_paren); | ||
| 2103 | const array_expr = try p.expectExpr(); | ||
| 2104 | _ = try p.expectToken(.r_paren); | ||
| 2105 | const found_payload = try p.parsePtrIndexPayload(); | ||
| 2106 | if (found_payload == 0) try p.warn(.expected_loop_payload); | ||
| 2107 | |||
| 2108 | const then_expr = try p.expectExpr(); | ||
| 2109 | _ = p.eatToken(.keyword_else) orelse { | ||
| 2110 | return p.addNode(.{ | ||
| 2111 | .tag = .for_simple, | ||
| 2112 | .main_token = for_token, | ||
| 2113 | .data = .{ | ||
| 2114 | .lhs = array_expr, | ||
| 2115 | .rhs = then_expr, | ||
| 2116 | }, | ||
| 2117 | }); | ||
| 2118 | }; | ||
| 2119 | const else_expr = try p.expectExpr(); | ||
| 2120 | return p.addNode(.{ | ||
| 2121 | .tag = .@"for", | ||
| 2122 | .main_token = for_token, | ||
| 2123 | .data = .{ | ||
| 2124 | .lhs = array_expr, | ||
| 2125 | .rhs = try p.addExtra(Node.If{ | ||
| 2126 | .then_expr = then_expr, | ||
| 2127 | .else_expr = else_expr, | ||
| 2128 | }), | ||
| 2129 | }, | ||
| 2130 | }); | ||
| 2131 | } | ||
| 2132 | |||
| 2133 | /// WhilePrefix <- KEYWORD_while LPAREN Expr RPAREN PtrPayload? WhileContinueExpr? | ||
| 2134 | /// | ||
| 2135 | /// WhileExpr <- WhilePrefix Expr (KEYWORD_else Payload? Expr)? | ||
| 2136 | fn parseWhileExpr(p: *Parser) !Node.Index { | ||
| 2137 | const while_token = p.eatToken(.keyword_while) orelse return null_node; | ||
| 2138 | _ = try p.expectToken(.l_paren); | ||
| 2139 | const condition = try p.expectExpr(); | ||
| 2140 | _ = try p.expectToken(.r_paren); | ||
| 2141 | _ = try p.parsePtrPayload(); | ||
| 2142 | const cont_expr = try p.parseWhileContinueExpr(); | ||
| 2143 | |||
| 2144 | const then_expr = try p.expectExpr(); | ||
| 2145 | _ = p.eatToken(.keyword_else) orelse { | ||
| 2146 | if (cont_expr == 0) { | ||
| 2147 | return p.addNode(.{ | ||
| 2148 | .tag = .while_simple, | ||
| 2149 | .main_token = while_token, | ||
| 2150 | .data = .{ | ||
| 2151 | .lhs = condition, | ||
| 2152 | .rhs = then_expr, | ||
| 2153 | }, | ||
| 2154 | }); | ||
| 2155 | } else { | ||
| 2156 | return p.addNode(.{ | ||
| 2157 | .tag = .while_cont, | ||
| 2158 | .main_token = while_token, | ||
| 2159 | .data = .{ | ||
| 2160 | .lhs = condition, | ||
| 2161 | .rhs = try p.addExtra(Node.WhileCont{ | ||
| 2162 | .cont_expr = cont_expr, | ||
| 2163 | .then_expr = then_expr, | ||
| 2164 | }), | ||
| 2165 | }, | ||
| 2166 | }); | ||
| 2167 | } | ||
| 2168 | }; | ||
| 2169 | _ = try p.parsePayload(); | ||
| 2170 | const else_expr = try p.expectExpr(); | ||
| 2171 | return p.addNode(.{ | ||
| 2172 | .tag = .@"while", | ||
| 2173 | .main_token = while_token, | ||
| 2174 | .data = .{ | ||
| 2175 | .lhs = condition, | ||
| 2176 | .rhs = try p.addExtra(Node.While{ | ||
| 2177 | .cont_expr = cont_expr, | ||
| 2178 | .then_expr = then_expr, | ||
| 2179 | .else_expr = else_expr, | ||
| 2180 | }), | ||
| 2181 | }, | ||
| 2182 | }); | ||
| 2183 | } | ||
| 2184 | |||
| 2185 | /// CurlySuffixExpr <- TypeExpr InitList? | ||
| 2186 | /// | ||
| 2187 | /// InitList | ||
| 2188 | /// <- LBRACE FieldInit (COMMA FieldInit)* COMMA? RBRACE | ||
| 2189 | /// / LBRACE Expr (COMMA Expr)* COMMA? RBRACE | ||
| 2190 | /// / LBRACE RBRACE | ||
| 2191 | fn parseCurlySuffixExpr(p: *Parser) !Node.Index { | ||
| 2192 | const lhs = try p.parseTypeExpr(); | ||
| 2193 | if (lhs == 0) return null_node; | ||
| 2194 | const lbrace = p.eatToken(.l_brace) orelse return lhs; | ||
| 2195 | |||
| 2196 | // If there are 0 or 1 items, we can use ArrayInitOne/StructInitOne; | ||
| 2197 | // otherwise we use the full ArrayInit/StructInit. | ||
| 2198 | |||
| 2199 | const scratch_top = p.scratch.items.len; | ||
| 2200 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 2201 | const field_init = try p.parseFieldInit(); | ||
| 2202 | if (field_init != 0) { | ||
| 2203 | try p.scratch.append(p.gpa, field_init); | ||
| 2204 | while (true) { | ||
| 2205 | switch (p.token_tags[p.tok_i]) { | ||
| 2206 | .comma => p.tok_i += 1, | ||
| 2207 | .r_brace => { | ||
| 2208 | p.tok_i += 1; | ||
| 2209 | break; | ||
| 2210 | }, | ||
| 2211 | .colon, .r_paren, .r_bracket => return p.failExpected(.r_brace), | ||
| 2212 | // Likely just a missing comma; give error but continue parsing. | ||
| 2213 | else => try p.warn(.expected_comma_after_initializer), | ||
| 2214 | } | ||
| 2215 | if (p.eatToken(.r_brace)) |_| break; | ||
| 2216 | const next = try p.expectFieldInit(); | ||
| 2217 | try p.scratch.append(p.gpa, next); | ||
| 2218 | } | ||
| 2219 | const comma = (p.token_tags[p.tok_i - 2] == .comma); | ||
| 2220 | const inits = p.scratch.items[scratch_top..]; | ||
| 2221 | switch (inits.len) { | ||
| 2222 | 0 => unreachable, | ||
| 2223 | 1 => return p.addNode(.{ | ||
| 2224 | .tag = if (comma) .struct_init_one_comma else .struct_init_one, | ||
| 2225 | .main_token = lbrace, | ||
| 2226 | .data = .{ | ||
| 2227 | .lhs = lhs, | ||
| 2228 | .rhs = inits[0], | ||
| 2229 | }, | ||
| 2230 | }), | ||
| 2231 | else => return p.addNode(.{ | ||
| 2232 | .tag = if (comma) .struct_init_comma else .struct_init, | ||
| 2233 | .main_token = lbrace, | ||
| 2234 | .data = .{ | ||
| 2235 | .lhs = lhs, | ||
| 2236 | .rhs = try p.addExtra(try p.listToSpan(inits)), | ||
| 2237 | }, | ||
| 2238 | }), | ||
| 2239 | } | ||
| 2240 | } | ||
| 2241 | |||
| 2242 | while (true) { | ||
| 2243 | if (p.eatToken(.r_brace)) |_| break; | ||
| 2244 | const elem_init = try p.expectExpr(); | ||
| 2245 | try p.scratch.append(p.gpa, elem_init); | ||
| 2246 | switch (p.token_tags[p.tok_i]) { | ||
| 2247 | .comma => p.tok_i += 1, | ||
| 2248 | .r_brace => { | ||
| 2249 | p.tok_i += 1; | ||
| 2250 | break; | ||
| 2251 | }, | ||
| 2252 | .colon, .r_paren, .r_bracket => return p.failExpected(.r_brace), | ||
| 2253 | // Likely just a missing comma; give error but continue parsing. | ||
| 2254 | else => try p.warn(.expected_comma_after_initializer), | ||
| 2255 | } | ||
| 2256 | } | ||
| 2257 | const comma = (p.token_tags[p.tok_i - 2] == .comma); | ||
| 2258 | const inits = p.scratch.items[scratch_top..]; | ||
| 2259 | switch (inits.len) { | ||
| 2260 | 0 => return p.addNode(.{ | ||
| 2261 | .tag = .struct_init_one, | ||
| 2262 | .main_token = lbrace, | ||
| 2263 | .data = .{ | ||
| 2264 | .lhs = lhs, | ||
| 2265 | .rhs = 0, | ||
| 2266 | }, | ||
| 2267 | }), | ||
| 2268 | 1 => return p.addNode(.{ | ||
| 2269 | .tag = if (comma) .array_init_one_comma else .array_init_one, | ||
| 2270 | .main_token = lbrace, | ||
| 2271 | .data = .{ | ||
| 2272 | .lhs = lhs, | ||
| 2273 | .rhs = inits[0], | ||
| 2274 | }, | ||
| 2275 | }), | ||
| 2276 | else => return p.addNode(.{ | ||
| 2277 | .tag = if (comma) .array_init_comma else .array_init, | ||
| 2278 | .main_token = lbrace, | ||
| 2279 | .data = .{ | ||
| 2280 | .lhs = lhs, | ||
| 2281 | .rhs = try p.addExtra(try p.listToSpan(inits)), | ||
| 2282 | }, | ||
| 2283 | }), | ||
| 2284 | } | ||
| 2285 | } | ||
| 2286 | |||
| 2287 | /// ErrorUnionExpr <- SuffixExpr (EXCLAMATIONMARK TypeExpr)? | ||
| 2288 | fn parseErrorUnionExpr(p: *Parser) !Node.Index { | ||
| 2289 | const suffix_expr = try p.parseSuffixExpr(); | ||
| 2290 | if (suffix_expr == 0) return null_node; | ||
| 2291 | const bang = p.eatToken(.bang) orelse return suffix_expr; | ||
| 2292 | return p.addNode(.{ | ||
| 2293 | .tag = .error_union, | ||
| 2294 | .main_token = bang, | ||
| 2295 | .data = .{ | ||
| 2296 | .lhs = suffix_expr, | ||
| 2297 | .rhs = try p.expectTypeExpr(), | ||
| 2298 | }, | ||
| 2299 | }); | ||
| 2300 | } | ||
| 2301 | |||
| 2302 | /// SuffixExpr | ||
| 2303 | /// <- KEYWORD_async PrimaryTypeExpr SuffixOp* FnCallArguments | ||
| 2304 | /// / PrimaryTypeExpr (SuffixOp / FnCallArguments)* | ||
| 2305 | /// | ||
| 2306 | /// FnCallArguments <- LPAREN ExprList RPAREN | ||
| 2307 | /// | ||
| 2308 | /// ExprList <- (Expr COMMA)* Expr? | ||
| 2309 | fn parseSuffixExpr(p: *Parser) !Node.Index { | ||
| 2310 | if (p.eatToken(.keyword_async)) |_| { | ||
| 2311 | var res = try p.expectPrimaryTypeExpr(); | ||
| 2312 | while (true) { | ||
| 2313 | const node = try p.parseSuffixOp(res); | ||
| 2314 | if (node == 0) break; | ||
| 2315 | res = node; | ||
| 2316 | } | ||
| 2317 | const lparen = p.eatToken(.l_paren) orelse { | ||
| 2318 | try p.warn(.expected_param_list); | ||
| 2319 | return res; | ||
| 2320 | }; | ||
| 2321 | const scratch_top = p.scratch.items.len; | ||
| 2322 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 2323 | while (true) { | ||
| 2324 | if (p.eatToken(.r_paren)) |_| break; | ||
| 2325 | const param = try p.expectExpr(); | ||
| 2326 | try p.scratch.append(p.gpa, param); | ||
| 2327 | switch (p.token_tags[p.tok_i]) { | ||
| 2328 | .comma => p.tok_i += 1, | ||
| 2329 | .r_paren => { | ||
| 2330 | p.tok_i += 1; | ||
| 2331 | break; | ||
| 2332 | }, | ||
| 2333 | .colon, .r_brace, .r_bracket => return p.failExpected(.r_paren), | ||
| 2334 | // Likely just a missing comma; give error but continue parsing. | ||
| 2335 | else => try p.warn(.expected_comma_after_arg), | ||
| 2336 | } | ||
| 2337 | } | ||
| 2338 | const comma = (p.token_tags[p.tok_i - 2] == .comma); | ||
| 2339 | const params = p.scratch.items[scratch_top..]; | ||
| 2340 | switch (params.len) { | ||
| 2341 | 0 => return p.addNode(.{ | ||
| 2342 | .tag = if (comma) .async_call_one_comma else .async_call_one, | ||
| 2343 | .main_token = lparen, | ||
| 2344 | .data = .{ | ||
| 2345 | .lhs = res, | ||
| 2346 | .rhs = 0, | ||
| 2347 | }, | ||
| 2348 | }), | ||
| 2349 | 1 => return p.addNode(.{ | ||
| 2350 | .tag = if (comma) .async_call_one_comma else .async_call_one, | ||
| 2351 | .main_token = lparen, | ||
| 2352 | .data = .{ | ||
| 2353 | .lhs = res, | ||
| 2354 | .rhs = params[0], | ||
| 2355 | }, | ||
| 2356 | }), | ||
| 2357 | else => return p.addNode(.{ | ||
| 2358 | .tag = if (comma) .async_call_comma else .async_call, | ||
| 2359 | .main_token = lparen, | ||
| 2360 | .data = .{ | ||
| 2361 | .lhs = res, | ||
| 2362 | .rhs = try p.addExtra(try p.listToSpan(params)), | ||
| 2363 | }, | ||
| 2364 | }), | ||
| 2365 | } | ||
| 2366 | } | ||
| 2367 | |||
| 2368 | var res = try p.parsePrimaryTypeExpr(); | ||
| 2369 | if (res == 0) return res; | ||
| 2370 | while (true) { | ||
| 2371 | const suffix_op = try p.parseSuffixOp(res); | ||
| 2372 | if (suffix_op != 0) { | ||
| 2373 | res = suffix_op; | ||
| 2374 | continue; | ||
| 2375 | } | ||
| 2376 | const lparen = p.eatToken(.l_paren) orelse return res; | ||
| 2377 | const scratch_top = p.scratch.items.len; | ||
| 2378 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 2379 | while (true) { | ||
| 2380 | if (p.eatToken(.r_paren)) |_| break; | ||
| 2381 | const param = try p.expectExpr(); | ||
| 2382 | try p.scratch.append(p.gpa, param); | ||
| 2383 | switch (p.token_tags[p.tok_i]) { | ||
| 2384 | .comma => p.tok_i += 1, | ||
| 2385 | .r_paren => { | ||
| 2386 | p.tok_i += 1; | ||
| 2387 | break; | ||
| 2388 | }, | ||
| 2389 | .colon, .r_brace, .r_bracket => return p.failExpected(.r_paren), | ||
| 2390 | // Likely just a missing comma; give error but continue parsing. | ||
| 2391 | else => try p.warn(.expected_comma_after_arg), | ||
| 2392 | } | ||
| 2393 | } | ||
| 2394 | const comma = (p.token_tags[p.tok_i - 2] == .comma); | ||
| 2395 | const params = p.scratch.items[scratch_top..]; | ||
| 2396 | res = switch (params.len) { | ||
| 2397 | 0 => try p.addNode(.{ | ||
| 2398 | .tag = if (comma) .call_one_comma else .call_one, | ||
| 2399 | .main_token = lparen, | ||
| 2400 | .data = .{ | ||
| 2401 | .lhs = res, | ||
| 2402 | .rhs = 0, | ||
| 2403 | }, | ||
| 2404 | }), | ||
| 2405 | 1 => try p.addNode(.{ | ||
| 2406 | .tag = if (comma) .call_one_comma else .call_one, | ||
| 2407 | .main_token = lparen, | ||
| 2408 | .data = .{ | ||
| 2409 | .lhs = res, | ||
| 2410 | .rhs = params[0], | ||
| 2411 | }, | ||
| 2412 | }), | ||
| 2413 | else => try p.addNode(.{ | ||
| 2414 | .tag = if (comma) .call_comma else .call, | ||
| 2415 | .main_token = lparen, | ||
| 2416 | .data = .{ | ||
| 2417 | .lhs = res, | ||
| 2418 | .rhs = try p.addExtra(try p.listToSpan(params)), | ||
| 2419 | }, | ||
| 2420 | }), | ||
| 2421 | }; | ||
| 2422 | } | ||
| 2423 | } | ||
| 2424 | |||
| 2425 | /// PrimaryTypeExpr | ||
| 2426 | /// <- BUILTINIDENTIFIER FnCallArguments | ||
| 2427 | /// / CHAR_LITERAL | ||
| 2428 | /// / ContainerDecl | ||
| 2429 | /// / DOT IDENTIFIER | ||
| 2430 | /// / DOT InitList | ||
| 2431 | /// / ErrorSetDecl | ||
| 2432 | /// / FLOAT | ||
| 2433 | /// / FnProto | ||
| 2434 | /// / GroupedExpr | ||
| 2435 | /// / LabeledTypeExpr | ||
| 2436 | /// / IDENTIFIER | ||
| 2437 | /// / IfTypeExpr | ||
| 2438 | /// / INTEGER | ||
| 2439 | /// / KEYWORD_comptime TypeExpr | ||
| 2440 | /// / KEYWORD_error DOT IDENTIFIER | ||
| 2441 | /// / KEYWORD_anyframe | ||
| 2442 | /// / KEYWORD_unreachable | ||
| 2443 | /// / STRINGLITERAL | ||
| 2444 | /// / SwitchExpr | ||
| 2445 | /// | ||
| 2446 | /// ContainerDecl <- (KEYWORD_extern / KEYWORD_packed)? ContainerDeclAuto | ||
| 2447 | /// | ||
| 2448 | /// ContainerDeclAuto <- ContainerDeclType LBRACE container_doc_comment? ContainerMembers RBRACE | ||
| 2449 | /// | ||
| 2450 | /// InitList | ||
| 2451 | /// <- LBRACE FieldInit (COMMA FieldInit)* COMMA? RBRACE | ||
| 2452 | /// / LBRACE Expr (COMMA Expr)* COMMA? RBRACE | ||
| 2453 | /// / LBRACE RBRACE | ||
| 2454 | /// | ||
| 2455 | /// ErrorSetDecl <- KEYWORD_error LBRACE IdentifierList RBRACE | ||
| 2456 | /// | ||
| 2457 | /// GroupedExpr <- LPAREN Expr RPAREN | ||
| 2458 | /// | ||
| 2459 | /// IfTypeExpr <- IfPrefix TypeExpr (KEYWORD_else Payload? TypeExpr)? | ||
| 2460 | /// | ||
| 2461 | /// LabeledTypeExpr | ||
| 2462 | /// <- BlockLabel Block | ||
| 2463 | /// / BlockLabel? LoopTypeExpr | ||
| 2464 | /// | ||
| 2465 | /// LoopTypeExpr <- KEYWORD_inline? (ForTypeExpr / WhileTypeExpr) | ||
| 2466 | fn parsePrimaryTypeExpr(p: *Parser) !Node.Index { | ||
| 2467 | switch (p.token_tags[p.tok_i]) { | ||
| 2468 | .char_literal => return p.addNode(.{ | ||
| 2469 | .tag = .char_literal, | ||
| 2470 | .main_token = p.nextToken(), | ||
| 2471 | .data = .{ | ||
| 2472 | .lhs = undefined, | ||
| 2473 | .rhs = undefined, | ||
| 2474 | }, | ||
| 2475 | }), | ||
| 2476 | .number_literal => return p.addNode(.{ | ||
| 2477 | .tag = .number_literal, | ||
| 2478 | .main_token = p.nextToken(), | ||
| 2479 | .data = .{ | ||
| 2480 | .lhs = undefined, | ||
| 2481 | .rhs = undefined, | ||
| 2482 | }, | ||
| 2483 | }), | ||
| 2484 | .keyword_unreachable => return p.addNode(.{ | ||
| 2485 | .tag = .unreachable_literal, | ||
| 2486 | .main_token = p.nextToken(), | ||
| 2487 | .data = .{ | ||
| 2488 | .lhs = undefined, | ||
| 2489 | .rhs = undefined, | ||
| 2490 | }, | ||
| 2491 | }), | ||
| 2492 | .keyword_anyframe => return p.addNode(.{ | ||
| 2493 | .tag = .anyframe_literal, | ||
| 2494 | .main_token = p.nextToken(), | ||
| 2495 | .data = .{ | ||
| 2496 | .lhs = undefined, | ||
| 2497 | .rhs = undefined, | ||
| 2498 | }, | ||
| 2499 | }), | ||
| 2500 | .string_literal => { | ||
| 2501 | const main_token = p.nextToken(); | ||
| 2502 | return p.addNode(.{ | ||
| 2503 | .tag = .string_literal, | ||
| 2504 | .main_token = main_token, | ||
| 2505 | .data = .{ | ||
| 2506 | .lhs = undefined, | ||
| 2507 | .rhs = undefined, | ||
| 2508 | }, | ||
| 2509 | }); | ||
| 2510 | }, | ||
| 2511 | |||
| 2512 | .builtin => return p.parseBuiltinCall(), | ||
| 2513 | .keyword_fn => return p.parseFnProto(), | ||
| 2514 | .keyword_if => return p.parseIf(expectTypeExpr), | ||
| 2515 | .keyword_switch => return p.expectSwitchExpr(), | ||
| 2516 | |||
| 2517 | .keyword_extern, | ||
| 2518 | .keyword_packed, | ||
| 2519 | => { | ||
| 2520 | p.tok_i += 1; | ||
| 2521 | return p.parseContainerDeclAuto(); | ||
| 2522 | }, | ||
| 2523 | |||
| 2524 | .keyword_struct, | ||
| 2525 | .keyword_opaque, | ||
| 2526 | .keyword_enum, | ||
| 2527 | .keyword_union, | ||
| 2528 | => return p.parseContainerDeclAuto(), | ||
| 2529 | |||
| 2530 | .keyword_comptime => return p.addNode(.{ | ||
| 2531 | .tag = .@"comptime", | ||
| 2532 | .main_token = p.nextToken(), | ||
| 2533 | .data = .{ | ||
| 2534 | .lhs = try p.expectTypeExpr(), | ||
| 2535 | .rhs = undefined, | ||
| 2536 | }, | ||
| 2537 | }), | ||
| 2538 | .multiline_string_literal_line => { | ||
| 2539 | const first_line = p.nextToken(); | ||
| 2540 | while (p.token_tags[p.tok_i] == .multiline_string_literal_line) { | ||
| 2541 | p.tok_i += 1; | ||
| 2542 | } | ||
| 2543 | return p.addNode(.{ | ||
| 2544 | .tag = .multiline_string_literal, | ||
| 2545 | .main_token = first_line, | ||
| 2546 | .data = .{ | ||
| 2547 | .lhs = first_line, | ||
| 2548 | .rhs = p.tok_i - 1, | ||
| 2549 | }, | ||
| 2550 | }); | ||
| 2551 | }, | ||
| 2552 | .identifier => switch (p.token_tags[p.tok_i + 1]) { | ||
| 2553 | .colon => switch (p.token_tags[p.tok_i + 2]) { | ||
| 2554 | .keyword_inline => { | ||
| 2555 | p.tok_i += 3; | ||
| 2556 | switch (p.token_tags[p.tok_i]) { | ||
| 2557 | .keyword_for => return p.parseForTypeExpr(), | ||
| 2558 | .keyword_while => return p.parseWhileTypeExpr(), | ||
| 2559 | else => return p.fail(.expected_inlinable), | ||
| 2560 | } | ||
| 2561 | }, | ||
| 2562 | .keyword_for => { | ||
| 2563 | p.tok_i += 2; | ||
| 2564 | return p.parseForTypeExpr(); | ||
| 2565 | }, | ||
| 2566 | .keyword_while => { | ||
| 2567 | p.tok_i += 2; | ||
| 2568 | return p.parseWhileTypeExpr(); | ||
| 2569 | }, | ||
| 2570 | .l_brace => { | ||
| 2571 | p.tok_i += 2; | ||
| 2572 | return p.parseBlock(); | ||
| 2573 | }, | ||
| 2574 | else => return p.addNode(.{ | ||
| 2575 | .tag = .identifier, | ||
| 2576 | .main_token = p.nextToken(), | ||
| 2577 | .data = .{ | ||
| 2578 | .lhs = undefined, | ||
| 2579 | .rhs = undefined, | ||
| 2580 | }, | ||
| 2581 | }), | ||
| 2582 | }, | ||
| 2583 | else => return p.addNode(.{ | ||
| 2584 | .tag = .identifier, | ||
| 2585 | .main_token = p.nextToken(), | ||
| 2586 | .data = .{ | ||
| 2587 | .lhs = undefined, | ||
| 2588 | .rhs = undefined, | ||
| 2589 | }, | ||
| 2590 | }), | ||
| 2591 | }, | ||
| 2592 | .keyword_inline => { | ||
| 2593 | p.tok_i += 1; | ||
| 2594 | switch (p.token_tags[p.tok_i]) { | ||
| 2595 | .keyword_for => return p.parseForTypeExpr(), | ||
| 2596 | .keyword_while => return p.parseWhileTypeExpr(), | ||
| 2597 | else => return p.fail(.expected_inlinable), | ||
| 2598 | } | ||
| 2599 | }, | ||
| 2600 | .keyword_for => return p.parseForTypeExpr(), | ||
| 2601 | .keyword_while => return p.parseWhileTypeExpr(), | ||
| 2602 | .period => switch (p.token_tags[p.tok_i + 1]) { | ||
| 2603 | .identifier => return p.addNode(.{ | ||
| 2604 | .tag = .enum_literal, | ||
| 2605 | .data = .{ | ||
| 2606 | .lhs = p.nextToken(), // dot | ||
| 2607 | .rhs = undefined, | ||
| 2608 | }, | ||
| 2609 | .main_token = p.nextToken(), // identifier | ||
| 2610 | }), | ||
| 2611 | .l_brace => { | ||
| 2612 | const lbrace = p.tok_i + 1; | ||
| 2613 | p.tok_i = lbrace + 1; | ||
| 2614 | |||
| 2615 | // If there are 0, 1, or 2 items, we can use ArrayInitDotTwo/StructInitDotTwo; | ||
| 2616 | // otherwise we use the full ArrayInitDot/StructInitDot. | ||
| 2617 | |||
| 2618 | const scratch_top = p.scratch.items.len; | ||
| 2619 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 2620 | const field_init = try p.parseFieldInit(); | ||
| 2621 | if (field_init != 0) { | ||
| 2622 | try p.scratch.append(p.gpa, field_init); | ||
| 2623 | while (true) { | ||
| 2624 | switch (p.token_tags[p.tok_i]) { | ||
| 2625 | .comma => p.tok_i += 1, | ||
| 2626 | .r_brace => { | ||
| 2627 | p.tok_i += 1; | ||
| 2628 | break; | ||
| 2629 | }, | ||
| 2630 | .colon, .r_paren, .r_bracket => return p.failExpected(.r_brace), | ||
| 2631 | // Likely just a missing comma; give error but continue parsing. | ||
| 2632 | else => try p.warn(.expected_comma_after_initializer), | ||
| 2633 | } | ||
| 2634 | if (p.eatToken(.r_brace)) |_| break; | ||
| 2635 | const next = try p.expectFieldInit(); | ||
| 2636 | try p.scratch.append(p.gpa, next); | ||
| 2637 | } | ||
| 2638 | const comma = (p.token_tags[p.tok_i - 2] == .comma); | ||
| 2639 | const inits = p.scratch.items[scratch_top..]; | ||
| 2640 | switch (inits.len) { | ||
| 2641 | 0 => unreachable, | ||
| 2642 | 1 => return p.addNode(.{ | ||
| 2643 | .tag = if (comma) .struct_init_dot_two_comma else .struct_init_dot_two, | ||
| 2644 | .main_token = lbrace, | ||
| 2645 | .data = .{ | ||
| 2646 | .lhs = inits[0], | ||
| 2647 | .rhs = 0, | ||
| 2648 | }, | ||
| 2649 | }), | ||
| 2650 | 2 => return p.addNode(.{ | ||
| 2651 | .tag = if (comma) .struct_init_dot_two_comma else .struct_init_dot_two, | ||
| 2652 | .main_token = lbrace, | ||
| 2653 | .data = .{ | ||
| 2654 | .lhs = inits[0], | ||
| 2655 | .rhs = inits[1], | ||
| 2656 | }, | ||
| 2657 | }), | ||
| 2658 | else => { | ||
| 2659 | const span = try p.listToSpan(inits); | ||
| 2660 | return p.addNode(.{ | ||
| 2661 | .tag = if (comma) .struct_init_dot_comma else .struct_init_dot, | ||
| 2662 | .main_token = lbrace, | ||
| 2663 | .data = .{ | ||
| 2664 | .lhs = span.start, | ||
| 2665 | .rhs = span.end, | ||
| 2666 | }, | ||
| 2667 | }); | ||
| 2668 | }, | ||
| 2669 | } | ||
| 2670 | } | ||
| 2671 | |||
| 2672 | while (true) { | ||
| 2673 | if (p.eatToken(.r_brace)) |_| break; | ||
| 2674 | const elem_init = try p.expectExpr(); | ||
| 2675 | try p.scratch.append(p.gpa, elem_init); | ||
| 2676 | switch (p.token_tags[p.tok_i]) { | ||
| 2677 | .comma => p.tok_i += 1, | ||
| 2678 | .r_brace => { | ||
| 2679 | p.tok_i += 1; | ||
| 2680 | break; | ||
| 2681 | }, | ||
| 2682 | .colon, .r_paren, .r_bracket => return p.failExpected(.r_brace), | ||
| 2683 | // Likely just a missing comma; give error but continue parsing. | ||
| 2684 | else => try p.warn(.expected_comma_after_initializer), | ||
| 2685 | } | ||
| 2686 | } | ||
| 2687 | const comma = (p.token_tags[p.tok_i - 2] == .comma); | ||
| 2688 | const inits = p.scratch.items[scratch_top..]; | ||
| 2689 | switch (inits.len) { | ||
| 2690 | 0 => return p.addNode(.{ | ||
| 2691 | .tag = .struct_init_dot_two, | ||
| 2692 | .main_token = lbrace, | ||
| 2693 | .data = .{ | ||
| 2694 | .lhs = 0, | ||
| 2695 | .rhs = 0, | ||
| 2696 | }, | ||
| 2697 | }), | ||
| 2698 | 1 => return p.addNode(.{ | ||
| 2699 | .tag = if (comma) .array_init_dot_two_comma else .array_init_dot_two, | ||
| 2700 | .main_token = lbrace, | ||
| 2701 | .data = .{ | ||
| 2702 | .lhs = inits[0], | ||
| 2703 | .rhs = 0, | ||
| 2704 | }, | ||
| 2705 | }), | ||
| 2706 | 2 => return p.addNode(.{ | ||
| 2707 | .tag = if (comma) .array_init_dot_two_comma else .array_init_dot_two, | ||
| 2708 | .main_token = lbrace, | ||
| 2709 | .data = .{ | ||
| 2710 | .lhs = inits[0], | ||
| 2711 | .rhs = inits[1], | ||
| 2712 | }, | ||
| 2713 | }), | ||
| 2714 | else => { | ||
| 2715 | const span = try p.listToSpan(inits); | ||
| 2716 | return p.addNode(.{ | ||
| 2717 | .tag = if (comma) .array_init_dot_comma else .array_init_dot, | ||
| 2718 | .main_token = lbrace, | ||
| 2719 | .data = .{ | ||
| 2720 | .lhs = span.start, | ||
| 2721 | .rhs = span.end, | ||
| 2722 | }, | ||
| 2723 | }); | ||
| 2724 | }, | ||
| 2725 | } | ||
| 2726 | }, | ||
| 2727 | else => return null_node, | ||
| 2728 | }, | ||
| 2729 | .keyword_error => switch (p.token_tags[p.tok_i + 1]) { | ||
| 2730 | .l_brace => { | ||
| 2731 | const error_token = p.tok_i; | ||
| 2732 | p.tok_i += 2; | ||
| 2733 | while (true) { | ||
| 2734 | if (p.eatToken(.r_brace)) |_| break; | ||
| 2735 | _ = try p.eatDocComments(); | ||
| 2736 | _ = try p.expectToken(.identifier); | ||
| 2737 | switch (p.token_tags[p.tok_i]) { | ||
| 2738 | .comma => p.tok_i += 1, | ||
| 2739 | .r_brace => { | ||
| 2740 | p.tok_i += 1; | ||
| 2741 | break; | ||
| 2742 | }, | ||
| 2743 | .colon, .r_paren, .r_bracket => return p.failExpected(.r_brace), | ||
| 2744 | // Likely just a missing comma; give error but continue parsing. | ||
| 2745 | else => try p.warn(.expected_comma_after_field), | ||
| 2746 | } | ||
| 2747 | } | ||
| 2748 | return p.addNode(.{ | ||
| 2749 | .tag = .error_set_decl, | ||
| 2750 | .main_token = error_token, | ||
| 2751 | .data = .{ | ||
| 2752 | .lhs = undefined, | ||
| 2753 | .rhs = p.tok_i - 1, // rbrace | ||
| 2754 | }, | ||
| 2755 | }); | ||
| 2756 | }, | ||
| 2757 | else => { | ||
| 2758 | const main_token = p.nextToken(); | ||
| 2759 | const period = p.eatToken(.period); | ||
| 2760 | if (period == null) try p.warnExpected(.period); | ||
| 2761 | const identifier = p.eatToken(.identifier); | ||
| 2762 | if (identifier == null) try p.warnExpected(.identifier); | ||
| 2763 | return p.addNode(.{ | ||
| 2764 | .tag = .error_value, | ||
| 2765 | .main_token = main_token, | ||
| 2766 | .data = .{ | ||
| 2767 | .lhs = period orelse 0, | ||
| 2768 | .rhs = identifier orelse 0, | ||
| 2769 | }, | ||
| 2770 | }); | ||
| 2771 | }, | ||
| 2772 | }, | ||
| 2773 | .l_paren => return p.addNode(.{ | ||
| 2774 | .tag = .grouped_expression, | ||
| 2775 | .main_token = p.nextToken(), | ||
| 2776 | .data = .{ | ||
| 2777 | .lhs = try p.expectExpr(), | ||
| 2778 | .rhs = try p.expectToken(.r_paren), | ||
| 2779 | }, | ||
| 2780 | }), | ||
| 2781 | else => return null_node, | ||
| 2782 | } | ||
| 2783 | } | ||
| 2784 | |||
| 2785 | fn expectPrimaryTypeExpr(p: *Parser) !Node.Index { | ||
| 2786 | const node = try p.parsePrimaryTypeExpr(); | ||
| 2787 | if (node == 0) { | ||
| 2788 | return p.fail(.expected_primary_type_expr); | ||
| 2789 | } | ||
| 2790 | return node; | ||
| 2791 | } | ||
| 2792 | |||
| 2793 | /// ForPrefix <- KEYWORD_for LPAREN Expr RPAREN PtrIndexPayload | ||
| 2794 | /// | ||
| 2795 | /// ForTypeExpr <- ForPrefix TypeExpr (KEYWORD_else TypeExpr)? | ||
| 2796 | fn parseForTypeExpr(p: *Parser) !Node.Index { | ||
| 2797 | const for_token = p.eatToken(.keyword_for) orelse return null_node; | ||
| 2798 | _ = try p.expectToken(.l_paren); | ||
| 2799 | const array_expr = try p.expectExpr(); | ||
| 2800 | _ = try p.expectToken(.r_paren); | ||
| 2801 | const found_payload = try p.parsePtrIndexPayload(); | ||
| 2802 | if (found_payload == 0) try p.warn(.expected_loop_payload); | ||
| 2803 | |||
| 2804 | const then_expr = try p.expectTypeExpr(); | ||
| 2805 | _ = p.eatToken(.keyword_else) orelse { | ||
| 2806 | return p.addNode(.{ | ||
| 2807 | .tag = .for_simple, | ||
| 2808 | .main_token = for_token, | ||
| 2809 | .data = .{ | ||
| 2810 | .lhs = array_expr, | ||
| 2811 | .rhs = then_expr, | ||
| 2812 | }, | ||
| 2813 | }); | ||
| 2814 | }; | ||
| 2815 | const else_expr = try p.expectTypeExpr(); | ||
| 2816 | return p.addNode(.{ | ||
| 2817 | .tag = .@"for", | ||
| 2818 | .main_token = for_token, | ||
| 2819 | .data = .{ | ||
| 2820 | .lhs = array_expr, | ||
| 2821 | .rhs = try p.addExtra(Node.If{ | ||
| 2822 | .then_expr = then_expr, | ||
| 2823 | .else_expr = else_expr, | ||
| 2824 | }), | ||
| 2825 | }, | ||
| 2826 | }); | ||
| 2827 | } | ||
| 2828 | |||
| 2829 | /// WhilePrefix <- KEYWORD_while LPAREN Expr RPAREN PtrPayload? WhileContinueExpr? | ||
| 2830 | /// | ||
| 2831 | /// WhileTypeExpr <- WhilePrefix TypeExpr (KEYWORD_else Payload? TypeExpr)? | ||
| 2832 | fn parseWhileTypeExpr(p: *Parser) !Node.Index { | ||
| 2833 | const while_token = p.eatToken(.keyword_while) orelse return null_node; | ||
| 2834 | _ = try p.expectToken(.l_paren); | ||
| 2835 | const condition = try p.expectExpr(); | ||
| 2836 | _ = try p.expectToken(.r_paren); | ||
| 2837 | _ = try p.parsePtrPayload(); | ||
| 2838 | const cont_expr = try p.parseWhileContinueExpr(); | ||
| 2839 | |||
| 2840 | const then_expr = try p.expectTypeExpr(); | ||
| 2841 | _ = p.eatToken(.keyword_else) orelse { | ||
| 2842 | if (cont_expr == 0) { | ||
| 2843 | return p.addNode(.{ | ||
| 2844 | .tag = .while_simple, | ||
| 2845 | .main_token = while_token, | ||
| 2846 | .data = .{ | ||
| 2847 | .lhs = condition, | ||
| 2848 | .rhs = then_expr, | ||
| 2849 | }, | ||
| 2850 | }); | ||
| 2851 | } else { | ||
| 2852 | return p.addNode(.{ | ||
| 2853 | .tag = .while_cont, | ||
| 2854 | .main_token = while_token, | ||
| 2855 | .data = .{ | ||
| 2856 | .lhs = condition, | ||
| 2857 | .rhs = try p.addExtra(Node.WhileCont{ | ||
| 2858 | .cont_expr = cont_expr, | ||
| 2859 | .then_expr = then_expr, | ||
| 2860 | }), | ||
| 2861 | }, | ||
| 2862 | }); | ||
| 2863 | } | ||
| 2864 | }; | ||
| 2865 | _ = try p.parsePayload(); | ||
| 2866 | const else_expr = try p.expectTypeExpr(); | ||
| 2867 | return p.addNode(.{ | ||
| 2868 | .tag = .@"while", | ||
| 2869 | .main_token = while_token, | ||
| 2870 | .data = .{ | ||
| 2871 | .lhs = condition, | ||
| 2872 | .rhs = try p.addExtra(Node.While{ | ||
| 2873 | .cont_expr = cont_expr, | ||
| 2874 | .then_expr = then_expr, | ||
| 2875 | .else_expr = else_expr, | ||
| 2876 | }), | ||
| 2877 | }, | ||
| 2878 | }); | ||
| 2879 | } | ||
| 2880 | |||
| 2881 | /// SwitchExpr <- KEYWORD_switch LPAREN Expr RPAREN LBRACE SwitchProngList RBRACE | ||
| 2882 | fn expectSwitchExpr(p: *Parser) !Node.Index { | ||
| 2883 | const switch_token = p.assertToken(.keyword_switch); | ||
| 2884 | _ = try p.expectToken(.l_paren); | ||
| 2885 | const expr_node = try p.expectExpr(); | ||
| 2886 | _ = try p.expectToken(.r_paren); | ||
| 2887 | _ = try p.expectToken(.l_brace); | ||
| 2888 | const cases = try p.parseSwitchProngList(); | ||
| 2889 | const trailing_comma = p.token_tags[p.tok_i - 1] == .comma; | ||
| 2890 | _ = try p.expectToken(.r_brace); | ||
| 2891 | |||
| 2892 | return p.addNode(.{ | ||
| 2893 | .tag = if (trailing_comma) .switch_comma else .@"switch", | ||
| 2894 | .main_token = switch_token, | ||
| 2895 | .data = .{ | ||
| 2896 | .lhs = expr_node, | ||
| 2897 | .rhs = try p.addExtra(Node.SubRange{ | ||
| 2898 | .start = cases.start, | ||
| 2899 | .end = cases.end, | ||
| 2900 | }), | ||
| 2901 | }, | ||
| 2902 | }); | ||
| 2903 | } | ||
| 2904 | |||
| 2905 | /// AsmExpr <- KEYWORD_asm KEYWORD_volatile? LPAREN Expr AsmOutput? RPAREN | ||
| 2906 | /// | ||
| 2907 | /// AsmOutput <- COLON AsmOutputList AsmInput? | ||
| 2908 | /// | ||
| 2909 | /// AsmInput <- COLON AsmInputList AsmClobbers? | ||
| 2910 | /// | ||
| 2911 | /// AsmClobbers <- COLON StringList | ||
| 2912 | /// | ||
| 2913 | /// StringList <- (STRINGLITERAL COMMA)* STRINGLITERAL? | ||
| 2914 | /// | ||
| 2915 | /// AsmOutputList <- (AsmOutputItem COMMA)* AsmOutputItem? | ||
| 2916 | /// | ||
| 2917 | /// AsmInputList <- (AsmInputItem COMMA)* AsmInputItem? | ||
| 2918 | fn expectAsmExpr(p: *Parser) !Node.Index { | ||
| 2919 | const asm_token = p.assertToken(.keyword_asm); | ||
| 2920 | _ = p.eatToken(.keyword_volatile); | ||
| 2921 | _ = try p.expectToken(.l_paren); | ||
| 2922 | const template = try p.expectExpr(); | ||
| 2923 | |||
| 2924 | if (p.eatToken(.r_paren)) |rparen| { | ||
| 2925 | return p.addNode(.{ | ||
| 2926 | .tag = .asm_simple, | ||
| 2927 | .main_token = asm_token, | ||
| 2928 | .data = .{ | ||
| 2929 | .lhs = template, | ||
| 2930 | .rhs = rparen, | ||
| 2931 | }, | ||
| 2932 | }); | ||
| 2933 | } | ||
| 2934 | |||
| 2935 | _ = try p.expectToken(.colon); | ||
| 2936 | |||
| 2937 | const scratch_top = p.scratch.items.len; | ||
| 2938 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 2939 | |||
| 2940 | while (true) { | ||
| 2941 | const output_item = try p.parseAsmOutputItem(); | ||
| 2942 | if (output_item == 0) break; | ||
| 2943 | try p.scratch.append(p.gpa, output_item); | ||
| 2944 | switch (p.token_tags[p.tok_i]) { | ||
| 2945 | .comma => p.tok_i += 1, | ||
| 2946 | // All possible delimiters. | ||
| 2947 | .colon, .r_paren, .r_brace, .r_bracket => break, | ||
| 2948 | // Likely just a missing comma; give error but continue parsing. | ||
| 2949 | else => try p.warnExpected(.comma), | ||
| 2950 | } | ||
| 2951 | } | ||
| 2952 | if (p.eatToken(.colon)) |_| { | ||
| 2953 | while (true) { | ||
| 2954 | const input_item = try p.parseAsmInputItem(); | ||
| 2955 | if (input_item == 0) break; | ||
| 2956 | try p.scratch.append(p.gpa, input_item); | ||
| 2957 | switch (p.token_tags[p.tok_i]) { | ||
| 2958 | .comma => p.tok_i += 1, | ||
| 2959 | // All possible delimiters. | ||
| 2960 | .colon, .r_paren, .r_brace, .r_bracket => break, | ||
| 2961 | // Likely just a missing comma; give error but continue parsing. | ||
| 2962 | else => try p.warnExpected(.comma), | ||
| 2963 | } | ||
| 2964 | } | ||
| 2965 | if (p.eatToken(.colon)) |_| { | ||
| 2966 | while (p.eatToken(.string_literal)) |_| { | ||
| 2967 | switch (p.token_tags[p.tok_i]) { | ||
| 2968 | .comma => p.tok_i += 1, | ||
| 2969 | .colon, .r_paren, .r_brace, .r_bracket => break, | ||
| 2970 | // Likely just a missing comma; give error but continue parsing. | ||
| 2971 | else => try p.warnExpected(.comma), | ||
| 2972 | } | ||
| 2973 | } | ||
| 2974 | } | ||
| 2975 | } | ||
| 2976 | const rparen = try p.expectToken(.r_paren); | ||
| 2977 | const span = try p.listToSpan(p.scratch.items[scratch_top..]); | ||
| 2978 | return p.addNode(.{ | ||
| 2979 | .tag = .@"asm", | ||
| 2980 | .main_token = asm_token, | ||
| 2981 | .data = .{ | ||
| 2982 | .lhs = template, | ||
| 2983 | .rhs = try p.addExtra(Node.Asm{ | ||
| 2984 | .items_start = span.start, | ||
| 2985 | .items_end = span.end, | ||
| 2986 | .rparen = rparen, | ||
| 2987 | }), | ||
| 2988 | }, | ||
| 2989 | }); | ||
| 2990 | } | ||
| 2991 | |||
| 2992 | /// AsmOutputItem <- LBRACKET IDENTIFIER RBRACKET STRINGLITERAL LPAREN (MINUSRARROW TypeExpr / IDENTIFIER) RPAREN | ||
| 2993 | fn parseAsmOutputItem(p: *Parser) !Node.Index { | ||
| 2994 | _ = p.eatToken(.l_bracket) orelse return null_node; | ||
| 2995 | const identifier = try p.expectToken(.identifier); | ||
| 2996 | _ = try p.expectToken(.r_bracket); | ||
| 2997 | _ = try p.expectToken(.string_literal); | ||
| 2998 | _ = try p.expectToken(.l_paren); | ||
| 2999 | const type_expr: Node.Index = blk: { | ||
| 3000 | if (p.eatToken(.arrow)) |_| { | ||
| 3001 | break :blk try p.expectTypeExpr(); | ||
| 3002 | } else { | ||
| 3003 | _ = try p.expectToken(.identifier); | ||
| 3004 | break :blk null_node; | ||
| 3005 | } | ||
| 3006 | }; | ||
| 3007 | const rparen = try p.expectToken(.r_paren); | ||
| 3008 | return p.addNode(.{ | ||
| 3009 | .tag = .asm_output, | ||
| 3010 | .main_token = identifier, | ||
| 3011 | .data = .{ | ||
| 3012 | .lhs = type_expr, | ||
| 3013 | .rhs = rparen, | ||
| 3014 | }, | ||
| 3015 | }); | ||
| 3016 | } | ||
| 3017 | |||
| 3018 | /// AsmInputItem <- LBRACKET IDENTIFIER RBRACKET STRINGLITERAL LPAREN Expr RPAREN | ||
| 3019 | fn parseAsmInputItem(p: *Parser) !Node.Index { | ||
| 3020 | _ = p.eatToken(.l_bracket) orelse return null_node; | ||
| 3021 | const identifier = try p.expectToken(.identifier); | ||
| 3022 | _ = try p.expectToken(.r_bracket); | ||
| 3023 | _ = try p.expectToken(.string_literal); | ||
| 3024 | _ = try p.expectToken(.l_paren); | ||
| 3025 | const expr = try p.expectExpr(); | ||
| 3026 | const rparen = try p.expectToken(.r_paren); | ||
| 3027 | return p.addNode(.{ | ||
| 3028 | .tag = .asm_input, | ||
| 3029 | .main_token = identifier, | ||
| 3030 | .data = .{ | ||
| 3031 | .lhs = expr, | ||
| 3032 | .rhs = rparen, | ||
| 3033 | }, | ||
| 3034 | }); | ||
| 3035 | } | ||
| 3036 | |||
| 3037 | /// BreakLabel <- COLON IDENTIFIER | ||
| 3038 | fn parseBreakLabel(p: *Parser) !TokenIndex { | ||
| 3039 | _ = p.eatToken(.colon) orelse return @as(TokenIndex, 0); | ||
| 3040 | return p.expectToken(.identifier); | ||
| 3041 | } | ||
| 3042 | |||
| 3043 | /// BlockLabel <- IDENTIFIER COLON | ||
| 3044 | fn parseBlockLabel(p: *Parser) TokenIndex { | ||
| 3045 | if (p.token_tags[p.tok_i] == .identifier and | ||
| 3046 | p.token_tags[p.tok_i + 1] == .colon) | ||
| 3047 | { | ||
| 3048 | const identifier = p.tok_i; | ||
| 3049 | p.tok_i += 2; | ||
| 3050 | return identifier; | ||
| 3051 | } | ||
| 3052 | return null_node; | ||
| 3053 | } | ||
| 3054 | |||
| 3055 | /// FieldInit <- DOT IDENTIFIER EQUAL Expr | ||
| 3056 | fn parseFieldInit(p: *Parser) !Node.Index { | ||
| 3057 | if (p.token_tags[p.tok_i + 0] == .period and | ||
| 3058 | p.token_tags[p.tok_i + 1] == .identifier and | ||
| 3059 | p.token_tags[p.tok_i + 2] == .equal) | ||
| 3060 | { | ||
| 3061 | p.tok_i += 3; | ||
| 3062 | return p.expectExpr(); | ||
| 3063 | } else { | ||
| 3064 | return null_node; | ||
| 3065 | } | ||
| 3066 | } | ||
| 3067 | |||
| 3068 | fn expectFieldInit(p: *Parser) !Node.Index { | ||
| 3069 | if (p.token_tags[p.tok_i] != .period or | ||
| 3070 | p.token_tags[p.tok_i + 1] != .identifier or | ||
| 3071 | p.token_tags[p.tok_i + 2] != .equal) | ||
| 3072 | return p.fail(.expected_initializer); | ||
| 3073 | |||
| 3074 | p.tok_i += 3; | ||
| 3075 | return p.expectExpr(); | ||
| 3076 | } | ||
| 3077 | |||
| 3078 | /// WhileContinueExpr <- COLON LPAREN AssignExpr RPAREN | ||
| 3079 | fn parseWhileContinueExpr(p: *Parser) !Node.Index { | ||
| 3080 | _ = p.eatToken(.colon) orelse { | ||
| 3081 | if (p.token_tags[p.tok_i] == .l_paren and | ||
| 3082 | p.tokensOnSameLine(p.tok_i - 1, p.tok_i)) | ||
| 3083 | return p.fail(.expected_continue_expr); | ||
| 3084 | return null_node; | ||
| 3085 | }; | ||
| 3086 | _ = try p.expectToken(.l_paren); | ||
| 3087 | const node = try p.parseAssignExpr(); | ||
| 3088 | if (node == 0) return p.fail(.expected_expr_or_assignment); | ||
| 3089 | _ = try p.expectToken(.r_paren); | ||
| 3090 | return node; | ||
| 3091 | } | ||
| 3092 | |||
| 3093 | /// LinkSection <- KEYWORD_linksection LPAREN Expr RPAREN | ||
| 3094 | fn parseLinkSection(p: *Parser) !Node.Index { | ||
| 3095 | _ = p.eatToken(.keyword_linksection) orelse return null_node; | ||
| 3096 | _ = try p.expectToken(.l_paren); | ||
| 3097 | const expr_node = try p.expectExpr(); | ||
| 3098 | _ = try p.expectToken(.r_paren); | ||
| 3099 | return expr_node; | ||
| 3100 | } | ||
| 3101 | |||
| 3102 | /// CallConv <- KEYWORD_callconv LPAREN Expr RPAREN | ||
| 3103 | fn parseCallconv(p: *Parser) !Node.Index { | ||
| 3104 | _ = p.eatToken(.keyword_callconv) orelse return null_node; | ||
| 3105 | _ = try p.expectToken(.l_paren); | ||
| 3106 | const expr_node = try p.expectExpr(); | ||
| 3107 | _ = try p.expectToken(.r_paren); | ||
| 3108 | return expr_node; | ||
| 3109 | } | ||
| 3110 | |||
| 3111 | /// AddrSpace <- KEYWORD_addrspace LPAREN Expr RPAREN | ||
| 3112 | fn parseAddrSpace(p: *Parser) !Node.Index { | ||
| 3113 | _ = p.eatToken(.keyword_addrspace) orelse return null_node; | ||
| 3114 | _ = try p.expectToken(.l_paren); | ||
| 3115 | const expr_node = try p.expectExpr(); | ||
| 3116 | _ = try p.expectToken(.r_paren); | ||
| 3117 | return expr_node; | ||
| 3118 | } | ||
| 3119 | |||
| 3120 | /// This function can return null nodes and then still return nodes afterwards, | ||
| 3121 | /// such as in the case of anytype and `...`. Caller must look for rparen to find | ||
| 3122 | /// out when there are no more param decls left. | ||
| 3123 | /// | ||
| 3124 | /// ParamDecl | ||
| 3125 | /// <- doc_comment? (KEYWORD_noalias / KEYWORD_comptime)? (IDENTIFIER COLON)? ParamType | ||
| 3126 | /// / DOT3 | ||
| 3127 | /// | ||
| 3128 | /// ParamType | ||
| 3129 | /// <- KEYWORD_anytype | ||
| 3130 | /// / TypeExpr | ||
| 3131 | fn expectParamDecl(p: *Parser) !Node.Index { | ||
| 3132 | _ = try p.eatDocComments(); | ||
| 3133 | switch (p.token_tags[p.tok_i]) { | ||
| 3134 | .keyword_noalias, .keyword_comptime => p.tok_i += 1, | ||
| 3135 | .ellipsis3 => { | ||
| 3136 | p.tok_i += 1; | ||
| 3137 | return null_node; | ||
| 3138 | }, | ||
| 3139 | else => {}, | ||
| 3140 | } | ||
| 3141 | if (p.token_tags[p.tok_i] == .identifier and | ||
| 3142 | p.token_tags[p.tok_i + 1] == .colon) | ||
| 3143 | { | ||
| 3144 | p.tok_i += 2; | ||
| 3145 | } | ||
| 3146 | switch (p.token_tags[p.tok_i]) { | ||
| 3147 | .keyword_anytype => { | ||
| 3148 | p.tok_i += 1; | ||
| 3149 | return null_node; | ||
| 3150 | }, | ||
| 3151 | else => return p.expectTypeExpr(), | ||
| 3152 | } | ||
| 3153 | } | ||
| 3154 | |||
| 3155 | /// Payload <- PIPE IDENTIFIER PIPE | ||
| 3156 | fn parsePayload(p: *Parser) !TokenIndex { | ||
| 3157 | _ = p.eatToken(.pipe) orelse return @as(TokenIndex, 0); | ||
| 3158 | const identifier = try p.expectToken(.identifier); | ||
| 3159 | _ = try p.expectToken(.pipe); | ||
| 3160 | return identifier; | ||
| 3161 | } | ||
| 3162 | |||
| 3163 | /// PtrPayload <- PIPE ASTERISK? IDENTIFIER PIPE | ||
| 3164 | fn parsePtrPayload(p: *Parser) !TokenIndex { | ||
| 3165 | _ = p.eatToken(.pipe) orelse return @as(TokenIndex, 0); | ||
| 3166 | _ = p.eatToken(.asterisk); | ||
| 3167 | const identifier = try p.expectToken(.identifier); | ||
| 3168 | _ = try p.expectToken(.pipe); | ||
| 3169 | return identifier; | ||
| 3170 | } | ||
| 3171 | |||
| 3172 | /// Returns the first identifier token, if any. | ||
| 3173 | /// | ||
| 3174 | /// PtrIndexPayload <- PIPE ASTERISK? IDENTIFIER (COMMA IDENTIFIER)? PIPE | ||
| 3175 | fn parsePtrIndexPayload(p: *Parser) !TokenIndex { | ||
| 3176 | _ = p.eatToken(.pipe) orelse return @as(TokenIndex, 0); | ||
| 3177 | _ = p.eatToken(.asterisk); | ||
| 3178 | const identifier = try p.expectToken(.identifier); | ||
| 3179 | if (p.eatToken(.comma) != null) { | ||
| 3180 | _ = try p.expectToken(.identifier); | ||
| 3181 | } | ||
| 3182 | _ = try p.expectToken(.pipe); | ||
| 3183 | return identifier; | ||
| 3184 | } | ||
| 3185 | |||
| 3186 | /// SwitchProng <- KEYWORD_inline? SwitchCase EQUALRARROW PtrIndexPayload? AssignExpr | ||
| 3187 | /// | ||
| 3188 | /// SwitchCase | ||
| 3189 | /// <- SwitchItem (COMMA SwitchItem)* COMMA? | ||
| 3190 | /// / KEYWORD_else | ||
| 3191 | fn parseSwitchProng(p: *Parser) !Node.Index { | ||
| 3192 | const scratch_top = p.scratch.items.len; | ||
| 3193 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 3194 | |||
| 3195 | const is_inline = p.eatToken(.keyword_inline) != null; | ||
| 3196 | |||
| 3197 | if (p.eatToken(.keyword_else) == null) { | ||
| 3198 | while (true) { | ||
| 3199 | const item = try p.parseSwitchItem(); | ||
| 3200 | if (item == 0) break; | ||
| 3201 | try p.scratch.append(p.gpa, item); | ||
| 3202 | if (p.eatToken(.comma) == null) break; | ||
| 3203 | } | ||
| 3204 | if (scratch_top == p.scratch.items.len) { | ||
| 3205 | if (is_inline) p.tok_i -= 1; | ||
| 3206 | return null_node; | ||
| 3207 | } | ||
| 3208 | } | ||
| 3209 | const arrow_token = try p.expectToken(.equal_angle_bracket_right); | ||
| 3210 | _ = try p.parsePtrIndexPayload(); | ||
| 3211 | |||
| 3212 | const items = p.scratch.items[scratch_top..]; | ||
| 3213 | switch (items.len) { | ||
| 3214 | 0 => return p.addNode(.{ | ||
| 3215 | .tag = if (is_inline) .switch_case_inline_one else .switch_case_one, | ||
| 3216 | .main_token = arrow_token, | ||
| 3217 | .data = .{ | ||
| 3218 | .lhs = 0, | ||
| 3219 | .rhs = try p.expectAssignExpr(), | ||
| 3220 | }, | ||
| 3221 | }), | ||
| 3222 | 1 => return p.addNode(.{ | ||
| 3223 | .tag = if (is_inline) .switch_case_inline_one else .switch_case_one, | ||
| 3224 | .main_token = arrow_token, | ||
| 3225 | .data = .{ | ||
| 3226 | .lhs = items[0], | ||
| 3227 | .rhs = try p.expectAssignExpr(), | ||
| 3228 | }, | ||
| 3229 | }), | ||
| 3230 | else => return p.addNode(.{ | ||
| 3231 | .tag = if (is_inline) .switch_case_inline else .switch_case, | ||
| 3232 | .main_token = arrow_token, | ||
| 3233 | .data = .{ | ||
| 3234 | .lhs = try p.addExtra(try p.listToSpan(items)), | ||
| 3235 | .rhs = try p.expectAssignExpr(), | ||
| 3236 | }, | ||
| 3237 | }), | ||
| 3238 | } | ||
| 3239 | } | ||
| 3240 | |||
| 3241 | /// SwitchItem <- Expr (DOT3 Expr)? | ||
| 3242 | fn parseSwitchItem(p: *Parser) !Node.Index { | ||
| 3243 | const expr = try p.parseExpr(); | ||
| 3244 | if (expr == 0) return null_node; | ||
| 3245 | |||
| 3246 | if (p.eatToken(.ellipsis3)) |token| { | ||
| 3247 | return p.addNode(.{ | ||
| 3248 | .tag = .switch_range, | ||
| 3249 | .main_token = token, | ||
| 3250 | .data = .{ | ||
| 3251 | .lhs = expr, | ||
| 3252 | .rhs = try p.expectExpr(), | ||
| 3253 | }, | ||
| 3254 | }); | ||
| 3255 | } | ||
| 3256 | return expr; | ||
| 3257 | } | ||
| 3258 | |||
| 3259 | const PtrModifiers = struct { | ||
| 3260 | align_node: Node.Index, | ||
| 3261 | addrspace_node: Node.Index, | ||
| 3262 | bit_range_start: Node.Index, | ||
| 3263 | bit_range_end: Node.Index, | ||
| 3264 | }; | ||
| 3265 | |||
| 3266 | fn parsePtrModifiers(p: *Parser) !PtrModifiers { | ||
| 3267 | var result: PtrModifiers = .{ | ||
| 3268 | .align_node = 0, | ||
| 3269 | .addrspace_node = 0, | ||
| 3270 | .bit_range_start = 0, | ||
| 3271 | .bit_range_end = 0, | ||
| 3272 | }; | ||
| 3273 | var saw_const = false; | ||
| 3274 | var saw_volatile = false; | ||
| 3275 | var saw_allowzero = false; | ||
| 3276 | var saw_addrspace = false; | ||
| 3277 | while (true) { | ||
| 3278 | switch (p.token_tags[p.tok_i]) { | ||
| 3279 | .keyword_align => { | ||
| 3280 | if (result.align_node != 0) { | ||
| 3281 | try p.warn(.extra_align_qualifier); | ||
| 3282 | } | ||
| 3283 | p.tok_i += 1; | ||
| 3284 | _ = try p.expectToken(.l_paren); | ||
| 3285 | result.align_node = try p.expectExpr(); | ||
| 3286 | |||
| 3287 | if (p.eatToken(.colon)) |_| { | ||
| 3288 | result.bit_range_start = try p.expectExpr(); | ||
| 3289 | _ = try p.expectToken(.colon); | ||
| 3290 | result.bit_range_end = try p.expectExpr(); | ||
| 3291 | } | ||
| 3292 | |||
| 3293 | _ = try p.expectToken(.r_paren); | ||
| 3294 | }, | ||
| 3295 | .keyword_const => { | ||
| 3296 | if (saw_const) { | ||
| 3297 | try p.warn(.extra_const_qualifier); | ||
| 3298 | } | ||
| 3299 | p.tok_i += 1; | ||
| 3300 | saw_const = true; | ||
| 3301 | }, | ||
| 3302 | .keyword_volatile => { | ||
| 3303 | if (saw_volatile) { | ||
| 3304 | try p.warn(.extra_volatile_qualifier); | ||
| 3305 | } | ||
| 3306 | p.tok_i += 1; | ||
| 3307 | saw_volatile = true; | ||
| 3308 | }, | ||
| 3309 | .keyword_allowzero => { | ||
| 3310 | if (saw_allowzero) { | ||
| 3311 | try p.warn(.extra_allowzero_qualifier); | ||
| 3312 | } | ||
| 3313 | p.tok_i += 1; | ||
| 3314 | saw_allowzero = true; | ||
| 3315 | }, | ||
| 3316 | .keyword_addrspace => { | ||
| 3317 | if (saw_addrspace) { | ||
| 3318 | try p.warn(.extra_addrspace_qualifier); | ||
| 3319 | } | ||
| 3320 | result.addrspace_node = try p.parseAddrSpace(); | ||
| 3321 | }, | ||
| 3322 | else => return result, | ||
| 3323 | } | ||
| 3324 | } | ||
| 3325 | } | ||
| 3326 | |||
| 3327 | /// SuffixOp | ||
| 3328 | /// <- LBRACKET Expr (DOT2 (Expr? (COLON Expr)?)?)? RBRACKET | ||
| 3329 | /// / DOT IDENTIFIER | ||
| 3330 | /// / DOTASTERISK | ||
| 3331 | /// / DOTQUESTIONMARK | ||
| 3332 | fn parseSuffixOp(p: *Parser, lhs: Node.Index) !Node.Index { | ||
| 3333 | switch (p.token_tags[p.tok_i]) { | ||
| 3334 | .l_bracket => { | ||
| 3335 | const lbracket = p.nextToken(); | ||
| 3336 | const index_expr = try p.expectExpr(); | ||
| 3337 | |||
| 3338 | if (p.eatToken(.ellipsis2)) |_| { | ||
| 3339 | const end_expr = try p.parseExpr(); | ||
| 3340 | if (p.eatToken(.colon)) |_| { | ||
| 3341 | const sentinel = try p.expectExpr(); | ||
| 3342 | _ = try p.expectToken(.r_bracket); | ||
| 3343 | return p.addNode(.{ | ||
| 3344 | .tag = .slice_sentinel, | ||
| 3345 | .main_token = lbracket, | ||
| 3346 | .data = .{ | ||
| 3347 | .lhs = lhs, | ||
| 3348 | .rhs = try p.addExtra(Node.SliceSentinel{ | ||
| 3349 | .start = index_expr, | ||
| 3350 | .end = end_expr, | ||
| 3351 | .sentinel = sentinel, | ||
| 3352 | }), | ||
| 3353 | }, | ||
| 3354 | }); | ||
| 3355 | } | ||
| 3356 | _ = try p.expectToken(.r_bracket); | ||
| 3357 | if (end_expr == 0) { | ||
| 3358 | return p.addNode(.{ | ||
| 3359 | .tag = .slice_open, | ||
| 3360 | .main_token = lbracket, | ||
| 3361 | .data = .{ | ||
| 3362 | .lhs = lhs, | ||
| 3363 | .rhs = index_expr, | ||
| 3364 | }, | ||
| 3365 | }); | ||
| 3366 | } | ||
| 3367 | return p.addNode(.{ | ||
| 3368 | .tag = .slice, | ||
| 3369 | .main_token = lbracket, | ||
| 3370 | .data = .{ | ||
| 3371 | .lhs = lhs, | ||
| 3372 | .rhs = try p.addExtra(Node.Slice{ | ||
| 3373 | .start = index_expr, | ||
| 3374 | .end = end_expr, | ||
| 3375 | }), | ||
| 3376 | }, | ||
| 3377 | }); | ||
| 3378 | } | ||
| 3379 | _ = try p.expectToken(.r_bracket); | ||
| 3380 | return p.addNode(.{ | ||
| 3381 | .tag = .array_access, | ||
| 3382 | .main_token = lbracket, | ||
| 3383 | .data = .{ | ||
| 3384 | .lhs = lhs, | ||
| 3385 | .rhs = index_expr, | ||
| 3386 | }, | ||
| 3387 | }); | ||
| 3388 | }, | ||
| 3389 | .period_asterisk => return p.addNode(.{ | ||
| 3390 | .tag = .deref, | ||
| 3391 | .main_token = p.nextToken(), | ||
| 3392 | .data = .{ | ||
| 3393 | .lhs = lhs, | ||
| 3394 | .rhs = undefined, | ||
| 3395 | }, | ||
| 3396 | }), | ||
| 3397 | .invalid_periodasterisks => { | ||
| 3398 | try p.warn(.asterisk_after_ptr_deref); | ||
| 3399 | return p.addNode(.{ | ||
| 3400 | .tag = .deref, | ||
| 3401 | .main_token = p.nextToken(), | ||
| 3402 | .data = .{ | ||
| 3403 | .lhs = lhs, | ||
| 3404 | .rhs = undefined, | ||
| 3405 | }, | ||
| 3406 | }); | ||
| 3407 | }, | ||
| 3408 | .period => switch (p.token_tags[p.tok_i + 1]) { | ||
| 3409 | .identifier => return p.addNode(.{ | ||
| 3410 | .tag = .field_access, | ||
| 3411 | .main_token = p.nextToken(), | ||
| 3412 | .data = .{ | ||
| 3413 | .lhs = lhs, | ||
| 3414 | .rhs = p.nextToken(), | ||
| 3415 | }, | ||
| 3416 | }), | ||
| 3417 | .question_mark => return p.addNode(.{ | ||
| 3418 | .tag = .unwrap_optional, | ||
| 3419 | .main_token = p.nextToken(), | ||
| 3420 | .data = .{ | ||
| 3421 | .lhs = lhs, | ||
| 3422 | .rhs = p.nextToken(), | ||
| 3423 | }, | ||
| 3424 | }), | ||
| 3425 | .l_brace => { | ||
| 3426 | // this a misplaced `.{`, handle the error somewhere else | ||
| 3427 | return null_node; | ||
| 3428 | }, | ||
| 3429 | else => { | ||
| 3430 | p.tok_i += 1; | ||
| 3431 | try p.warn(.expected_suffix_op); | ||
| 3432 | return null_node; | ||
| 3433 | }, | ||
| 3434 | }, | ||
| 3435 | else => return null_node, | ||
| 3436 | } | ||
| 3437 | } | ||
| 3438 | |||
| 3439 | /// Caller must have already verified the first token. | ||
| 3440 | /// | ||
| 3441 | /// ContainerDeclAuto <- ContainerDeclType LBRACE container_doc_comment? ContainerMembers RBRACE | ||
| 3442 | /// | ||
| 3443 | /// ContainerDeclType | ||
| 3444 | /// <- KEYWORD_struct (LPAREN Expr RPAREN)? | ||
| 3445 | /// / KEYWORD_opaque | ||
| 3446 | /// / KEYWORD_enum (LPAREN Expr RPAREN)? | ||
| 3447 | /// / KEYWORD_union (LPAREN (KEYWORD_enum (LPAREN Expr RPAREN)? / Expr) RPAREN)? | ||
| 3448 | fn parseContainerDeclAuto(p: *Parser) !Node.Index { | ||
| 3449 | const main_token = p.nextToken(); | ||
| 3450 | const arg_expr = switch (p.token_tags[main_token]) { | ||
| 3451 | .keyword_opaque => null_node, | ||
| 3452 | .keyword_struct, .keyword_enum => blk: { | ||
| 3453 | if (p.eatToken(.l_paren)) |_| { | ||
| 3454 | const expr = try p.expectExpr(); | ||
| 3455 | _ = try p.expectToken(.r_paren); | ||
| 3456 | break :blk expr; | ||
| 3457 | } else { | ||
| 3458 | break :blk null_node; | ||
| 3459 | } | ||
| 3460 | }, | ||
| 3461 | .keyword_union => blk: { | ||
| 3462 | if (p.eatToken(.l_paren)) |_| { | ||
| 3463 | if (p.eatToken(.keyword_enum)) |_| { | ||
| 3464 | if (p.eatToken(.l_paren)) |_| { | ||
| 3465 | const enum_tag_expr = try p.expectExpr(); | ||
| 3466 | _ = try p.expectToken(.r_paren); | ||
| 3467 | _ = try p.expectToken(.r_paren); | ||
| 3468 | |||
| 3469 | _ = try p.expectToken(.l_brace); | ||
| 3470 | const members = try p.parseContainerMembers(); | ||
| 3471 | const members_span = try members.toSpan(p); | ||
| 3472 | _ = try p.expectToken(.r_brace); | ||
| 3473 | return p.addNode(.{ | ||
| 3474 | .tag = switch (members.trailing) { | ||
| 3475 | true => .tagged_union_enum_tag_trailing, | ||
| 3476 | false => .tagged_union_enum_tag, | ||
| 3477 | }, | ||
| 3478 | .main_token = main_token, | ||
| 3479 | .data = .{ | ||
| 3480 | .lhs = enum_tag_expr, | ||
| 3481 | .rhs = try p.addExtra(members_span), | ||
| 3482 | }, | ||
| 3483 | }); | ||
| 3484 | } else { | ||
| 3485 | _ = try p.expectToken(.r_paren); | ||
| 3486 | |||
| 3487 | _ = try p.expectToken(.l_brace); | ||
| 3488 | const members = try p.parseContainerMembers(); | ||
| 3489 | _ = try p.expectToken(.r_brace); | ||
| 3490 | if (members.len <= 2) { | ||
| 3491 | return p.addNode(.{ | ||
| 3492 | .tag = switch (members.trailing) { | ||
| 3493 | true => .tagged_union_two_trailing, | ||
| 3494 | false => .tagged_union_two, | ||
| 3495 | }, | ||
| 3496 | .main_token = main_token, | ||
| 3497 | .data = .{ | ||
| 3498 | .lhs = members.lhs, | ||
| 3499 | .rhs = members.rhs, | ||
| 3500 | }, | ||
| 3501 | }); | ||
| 3502 | } else { | ||
| 3503 | const span = try members.toSpan(p); | ||
| 3504 | return p.addNode(.{ | ||
| 3505 | .tag = switch (members.trailing) { | ||
| 3506 | true => .tagged_union_trailing, | ||
| 3507 | false => .tagged_union, | ||
| 3508 | }, | ||
| 3509 | .main_token = main_token, | ||
| 3510 | .data = .{ | ||
| 3511 | .lhs = span.start, | ||
| 3512 | .rhs = span.end, | ||
| 3513 | }, | ||
| 3514 | }); | ||
| 3515 | } | ||
| 3516 | } | ||
| 3517 | } else { | ||
| 3518 | const expr = try p.expectExpr(); | ||
| 3519 | _ = try p.expectToken(.r_paren); | ||
| 3520 | break :blk expr; | ||
| 3521 | } | ||
| 3522 | } else { | ||
| 3523 | break :blk null_node; | ||
| 3524 | } | ||
| 3525 | }, | ||
| 3526 | else => { | ||
| 3527 | p.tok_i -= 1; | ||
| 3528 | return p.fail(.expected_container); | ||
| 3529 | }, | ||
| 3530 | }; | ||
| 3531 | _ = try p.expectToken(.l_brace); | ||
| 3532 | const members = try p.parseContainerMembers(); | ||
| 3533 | _ = try p.expectToken(.r_brace); | ||
| 3534 | if (arg_expr == 0) { | ||
| 3535 | if (members.len <= 2) { | ||
| 3536 | return p.addNode(.{ | ||
| 3537 | .tag = switch (members.trailing) { | ||
| 3538 | true => .container_decl_two_trailing, | ||
| 3539 | false => .container_decl_two, | ||
| 3540 | }, | ||
| 3541 | .main_token = main_token, | ||
| 3542 | .data = .{ | ||
| 3543 | .lhs = members.lhs, | ||
| 3544 | .rhs = members.rhs, | ||
| 3545 | }, | ||
| 3546 | }); | ||
| 3547 | } else { | ||
| 3548 | const span = try members.toSpan(p); | ||
| 3549 | return p.addNode(.{ | ||
| 3550 | .tag = switch (members.trailing) { | ||
| 3551 | true => .container_decl_trailing, | ||
| 3552 | false => .container_decl, | ||
| 3553 | }, | ||
| 3554 | .main_token = main_token, | ||
| 3555 | .data = .{ | ||
| 3556 | .lhs = span.start, | ||
| 3557 | .rhs = span.end, | ||
| 3558 | }, | ||
| 3559 | }); | ||
| 3560 | } | ||
| 3561 | } else { | ||
| 3562 | const span = try members.toSpan(p); | ||
| 3563 | return p.addNode(.{ | ||
| 3564 | .tag = switch (members.trailing) { | ||
| 3565 | true => .container_decl_arg_trailing, | ||
| 3566 | false => .container_decl_arg, | ||
| 3567 | }, | ||
| 3568 | .main_token = main_token, | ||
| 3569 | .data = .{ | ||
| 3570 | .lhs = arg_expr, | ||
| 3571 | .rhs = try p.addExtra(Node.SubRange{ | ||
| 3572 | .start = span.start, | ||
| 3573 | .end = span.end, | ||
| 3574 | }), | ||
| 3575 | }, | ||
| 3576 | }); | ||
| 3577 | } | ||
| 3578 | } | ||
| 3579 | |||
| 3580 | /// Give a helpful error message for those transitioning from | ||
| 3581 | /// C's 'struct Foo {};' to Zig's 'const Foo = struct {};'. | ||
| 3582 | fn parseCStyleContainer(p: *Parser) Error!bool { | ||
| 3583 | const main_token = p.tok_i; | ||
| 3584 | switch (p.token_tags[p.tok_i]) { | ||
| 3585 | .keyword_enum, .keyword_union, .keyword_struct => {}, | ||
| 3586 | else => return false, | ||
| 3587 | } | ||
| 3588 | const identifier = p.tok_i + 1; | ||
| 3589 | if (p.token_tags[identifier] != .identifier) return false; | ||
| 3590 | p.tok_i += 2; | ||
| 3591 | |||
| 3592 | try p.warnMsg(.{ | ||
| 3593 | .tag = .c_style_container, | ||
| 3594 | .token = identifier, | ||
| 3595 | .extra = .{ .expected_tag = p.token_tags[main_token] }, | ||
| 3596 | }); | ||
| 3597 | try p.warnMsg(.{ | ||
| 3598 | .tag = .zig_style_container, | ||
| 3599 | .is_note = true, | ||
| 3600 | .token = identifier, | ||
| 3601 | .extra = .{ .expected_tag = p.token_tags[main_token] }, | ||
| 3602 | }); | ||
| 3603 | |||
| 3604 | _ = try p.expectToken(.l_brace); | ||
| 3605 | _ = try p.parseContainerMembers(); | ||
| 3606 | _ = try p.expectToken(.r_brace); | ||
| 3607 | try p.expectSemicolon(.expected_semi_after_decl, true); | ||
| 3608 | return true; | ||
| 3609 | } | ||
| 3610 | |||
| 3611 | /// Holds temporary data until we are ready to construct the full ContainerDecl AST node. | ||
| 3612 | /// | ||
| 3613 | /// ByteAlign <- KEYWORD_align LPAREN Expr RPAREN | ||
| 3614 | fn parseByteAlign(p: *Parser) !Node.Index { | ||
| 3615 | _ = p.eatToken(.keyword_align) orelse return null_node; | ||
| 3616 | _ = try p.expectToken(.l_paren); | ||
| 3617 | const expr = try p.expectExpr(); | ||
| 3618 | _ = try p.expectToken(.r_paren); | ||
| 3619 | return expr; | ||
| 3620 | } | ||
| 3621 | |||
| 3622 | /// SwitchProngList <- (SwitchProng COMMA)* SwitchProng? | ||
| 3623 | fn parseSwitchProngList(p: *Parser) !Node.SubRange { | ||
| 3624 | const scratch_top = p.scratch.items.len; | ||
| 3625 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 3626 | |||
| 3627 | while (true) { | ||
| 3628 | const item = try parseSwitchProng(p); | ||
| 3629 | if (item == 0) break; | ||
| 3630 | |||
| 3631 | try p.scratch.append(p.gpa, item); | ||
| 3632 | |||
| 3633 | switch (p.token_tags[p.tok_i]) { | ||
| 3634 | .comma => p.tok_i += 1, | ||
| 3635 | // All possible delimiters. | ||
| 3636 | .colon, .r_paren, .r_brace, .r_bracket => break, | ||
| 3637 | // Likely just a missing comma; give error but continue parsing. | ||
| 3638 | else => try p.warn(.expected_comma_after_switch_prong), | ||
| 3639 | } | ||
| 3640 | } | ||
| 3641 | return p.listToSpan(p.scratch.items[scratch_top..]); | ||
| 3642 | } | ||
| 3643 | |||
| 3644 | /// ParamDeclList <- (ParamDecl COMMA)* ParamDecl? | ||
| 3645 | fn parseParamDeclList(p: *Parser) !SmallSpan { | ||
| 3646 | _ = try p.expectToken(.l_paren); | ||
| 3647 | const scratch_top = p.scratch.items.len; | ||
| 3648 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 3649 | var varargs: union(enum) { none, seen, nonfinal: TokenIndex } = .none; | ||
| 3650 | while (true) { | ||
| 3651 | if (p.eatToken(.r_paren)) |_| break; | ||
| 3652 | if (varargs == .seen) varargs = .{ .nonfinal = p.tok_i }; | ||
| 3653 | const param = try p.expectParamDecl(); | ||
| 3654 | if (param != 0) { | ||
| 3655 | try p.scratch.append(p.gpa, param); | ||
| 3656 | } else if (p.token_tags[p.tok_i - 1] == .ellipsis3) { | ||
| 3657 | if (varargs == .none) varargs = .seen; | ||
| 3658 | } | ||
| 3659 | switch (p.token_tags[p.tok_i]) { | ||
| 3660 | .comma => p.tok_i += 1, | ||
| 3661 | .r_paren => { | ||
| 3662 | p.tok_i += 1; | ||
| 3663 | break; | ||
| 3664 | }, | ||
| 3665 | .colon, .r_brace, .r_bracket => return p.failExpected(.r_paren), | ||
| 3666 | // Likely just a missing comma; give error but continue parsing. | ||
| 3667 | else => try p.warn(.expected_comma_after_param), | ||
| 3668 | } | ||
| 3669 | } | ||
| 3670 | if (varargs == .nonfinal) { | ||
| 3671 | try p.warnMsg(.{ .tag = .varargs_nonfinal, .token = varargs.nonfinal }); | ||
| 3672 | } | ||
| 3673 | const params = p.scratch.items[scratch_top..]; | ||
| 3674 | return switch (params.len) { | ||
| 3675 | 0 => SmallSpan{ .zero_or_one = 0 }, | ||
| 3676 | 1 => SmallSpan{ .zero_or_one = params[0] }, | ||
| 3677 | else => SmallSpan{ .multi = try p.listToSpan(params) }, | ||
| 3678 | }; | ||
| 3679 | } | ||
| 3680 | |||
| 3681 | /// FnCallArguments <- LPAREN ExprList RPAREN | ||
| 3682 | /// | ||
| 3683 | /// ExprList <- (Expr COMMA)* Expr? | ||
| 3684 | fn parseBuiltinCall(p: *Parser) !Node.Index { | ||
| 3685 | const builtin_token = p.assertToken(.builtin); | ||
| 3686 | if (p.token_tags[p.nextToken()] != .l_paren) { | ||
| 3687 | p.tok_i -= 1; | ||
| 3688 | try p.warn(.expected_param_list); | ||
| 3689 | // Pretend this was an identifier so we can continue parsing. | ||
| 3690 | return p.addNode(.{ | ||
| 3691 | .tag = .identifier, | ||
| 3692 | .main_token = builtin_token, | ||
| 3693 | .data = .{ | ||
| 3694 | .lhs = undefined, | ||
| 3695 | .rhs = undefined, | ||
| 3696 | }, | ||
| 3697 | }); | ||
| 3698 | } | ||
| 3699 | const scratch_top = p.scratch.items.len; | ||
| 3700 | defer p.scratch.shrinkRetainingCapacity(scratch_top); | ||
| 3701 | while (true) { | ||
| 3702 | if (p.eatToken(.r_paren)) |_| break; | ||
| 3703 | const param = try p.expectExpr(); | ||
| 3704 | try p.scratch.append(p.gpa, param); | ||
| 3705 | switch (p.token_tags[p.tok_i]) { | ||
| 3706 | .comma => p.tok_i += 1, | ||
| 3707 | .r_paren => { | ||
| 3708 | p.tok_i += 1; | ||
| 3709 | break; | ||
| 3710 | }, | ||
| 3711 | // Likely just a missing comma; give error but continue parsing. | ||
| 3712 | else => try p.warn(.expected_comma_after_arg), | ||
| 3713 | } | ||
| 3714 | } | ||
| 3715 | const comma = (p.token_tags[p.tok_i - 2] == .comma); | ||
| 3716 | const params = p.scratch.items[scratch_top..]; | ||
| 3717 | switch (params.len) { | ||
| 3718 | 0 => return p.addNode(.{ | ||
| 3719 | .tag = .builtin_call_two, | ||
| 3720 | .main_token = builtin_token, | ||
| 3721 | .data = .{ | ||
| 3722 | .lhs = 0, | ||
| 3723 | .rhs = 0, | ||
| 3724 | }, | ||
| 3725 | }), | ||
| 3726 | 1 => return p.addNode(.{ | ||
| 3727 | .tag = if (comma) .builtin_call_two_comma else .builtin_call_two, | ||
| 3728 | .main_token = builtin_token, | ||
| 3729 | .data = .{ | ||
| 3730 | .lhs = params[0], | ||
| 3731 | .rhs = 0, | ||
| 3732 | }, | ||
| 3733 | }), | ||
| 3734 | 2 => return p.addNode(.{ | ||
| 3735 | .tag = if (comma) .builtin_call_two_comma else .builtin_call_two, | ||
| 3736 | .main_token = builtin_token, | ||
| 3737 | .data = .{ | ||
| 3738 | .lhs = params[0], | ||
| 3739 | .rhs = params[1], | ||
| 3740 | }, | ||
| 3741 | }), | ||
| 3742 | else => { | ||
| 3743 | const span = try p.listToSpan(params); | ||
| 3744 | return p.addNode(.{ | ||
| 3745 | .tag = if (comma) .builtin_call_comma else .builtin_call, | ||
| 3746 | .main_token = builtin_token, | ||
| 3747 | .data = .{ | ||
| 3748 | .lhs = span.start, | ||
| 3749 | .rhs = span.end, | ||
| 3750 | }, | ||
| 3751 | }); | ||
| 3752 | }, | ||
| 3753 | } | ||
| 3754 | } | ||
| 3755 | |||
| 3756 | /// IfPrefix <- KEYWORD_if LPAREN Expr RPAREN PtrPayload? | ||
| 3757 | fn parseIf(p: *Parser, comptime bodyParseFn: fn (p: *Parser) Error!Node.Index) !Node.Index { | ||
| 3758 | const if_token = p.eatToken(.keyword_if) orelse return null_node; | ||
| 3759 | _ = try p.expectToken(.l_paren); | ||
| 3760 | const condition = try p.expectExpr(); | ||
| 3761 | _ = try p.expectToken(.r_paren); | ||
| 3762 | _ = try p.parsePtrPayload(); | ||
| 3763 | |||
| 3764 | const then_expr = try bodyParseFn(p); | ||
| 3765 | assert(then_expr != 0); | ||
| 3766 | |||
| 3767 | _ = p.eatToken(.keyword_else) orelse return p.addNode(.{ | ||
| 3768 | .tag = .if_simple, | ||
| 3769 | .main_token = if_token, | ||
| 3770 | .data = .{ | ||
| 3771 | .lhs = condition, | ||
| 3772 | .rhs = then_expr, | ||
| 3773 | }, | ||
| 3774 | }); | ||
| 3775 | _ = try p.parsePayload(); | ||
| 3776 | const else_expr = try bodyParseFn(p); | ||
| 3777 | assert(then_expr != 0); | ||
| 3778 | |||
| 3779 | return p.addNode(.{ | ||
| 3780 | .tag = .@"if", | ||
| 3781 | .main_token = if_token, | ||
| 3782 | .data = .{ | ||
| 3783 | .lhs = condition, | ||
| 3784 | .rhs = try p.addExtra(Node.If{ | ||
| 3785 | .then_expr = then_expr, | ||
| 3786 | .else_expr = else_expr, | ||
| 3787 | }), | ||
| 3788 | }, | ||
| 3789 | }); | ||
| 3790 | } | ||
| 3791 | |||
| 3792 | /// Skips over doc comment tokens. Returns the first one, if any. | ||
| 3793 | fn eatDocComments(p: *Parser) !?TokenIndex { | ||
| 3794 | if (p.eatToken(.doc_comment)) |tok| { | ||
| 3795 | var first_line = tok; | ||
| 3796 | if (tok > 0 and tokensOnSameLine(p, tok - 1, tok)) { | ||
| 3797 | try p.warnMsg(.{ | ||
| 3798 | .tag = .same_line_doc_comment, | ||
| 3799 | .token = tok, | ||
| 3800 | }); | ||
| 3801 | first_line = p.eatToken(.doc_comment) orelse return null; | ||
| 3802 | } | ||
| 3803 | while (p.eatToken(.doc_comment)) |_| {} | ||
| 3804 | return first_line; | ||
| 3805 | } | ||
| 3806 | return null; | ||
| 3807 | } | ||
| 3808 | |||
| 3809 | fn tokensOnSameLine(p: *Parser, token1: TokenIndex, token2: TokenIndex) bool { | ||
| 3810 | return std.mem.indexOfScalar(u8, p.source[p.token_starts[token1]..p.token_starts[token2]], '\n') == null; | ||
| 3811 | } | ||
| 3812 | |||
| 3813 | fn eatToken(p: *Parser, tag: Token.Tag) ?TokenIndex { | ||
| 3814 | return if (p.token_tags[p.tok_i] == tag) p.nextToken() else null; | ||
| 3815 | } | ||
| 3816 | |||
| 3817 | fn assertToken(p: *Parser, tag: Token.Tag) TokenIndex { | ||
| 3818 | const token = p.nextToken(); | ||
| 3819 | assert(p.token_tags[token] == tag); | ||
| 3820 | return token; | ||
| 3821 | } | ||
| 3822 | |||
| 3823 | fn expectToken(p: *Parser, tag: Token.Tag) Error!TokenIndex { | ||
| 3824 | if (p.token_tags[p.tok_i] != tag) { | ||
| 3825 | return p.failMsg(.{ | ||
| 3826 | .tag = .expected_token, | ||
| 3827 | .token = p.tok_i, | ||
| 3828 | .extra = .{ .expected_tag = tag }, | ||
| 3829 | }); | ||
| 3830 | } | ||
| 3831 | return p.nextToken(); | ||
| 3832 | } | ||
| 3833 | |||
| 3834 | fn expectSemicolon(p: *Parser, error_tag: AstError.Tag, recoverable: bool) Error!void { | ||
| 3835 | if (p.token_tags[p.tok_i] == .semicolon) { | ||
| 3836 | _ = p.nextToken(); | ||
| 3837 | return; | ||
| 3838 | } | ||
| 3839 | try p.warn(error_tag); | ||
| 3840 | if (!recoverable) return error.ParseError; | ||
| 3841 | } | ||
| 3842 | |||
| 3843 | fn nextToken(p: *Parser) TokenIndex { | ||
| 3844 | const result = p.tok_i; | ||
| 3845 | p.tok_i += 1; | ||
| 3846 | return result; | ||
| 3847 | } | ||
| 3848 | }; | ||
| 3849 | |||
| 3850 | test { | ||
| 3851 | _ = @import("parser_test.zig"); | ||
| 3852 | } | ||
lib/std/zig/parser_test.zig+2-2| ... | @@ -6073,7 +6073,7 @@ var fixed_buffer_mem: [100 * 1024]u8 = undefined; | ... | @@ -6073,7 +6073,7 @@ var fixed_buffer_mem: [100 * 1024]u8 = undefined; |
| 6073 | fn testParse(source: [:0]const u8, allocator: mem.Allocator, anything_changed: *bool) ![]u8 { | 6073 | fn testParse(source: [:0]const u8, allocator: mem.Allocator, anything_changed: *bool) ![]u8 { |
| 6074 | const stderr = io.getStdErr().writer(); | 6074 | const stderr = io.getStdErr().writer(); |
| 6075 | 6075 | ||
| 6076 | var tree = try std.zig.parse(allocator, source); | 6076 | var tree = try std.zig.Ast.parse(allocator, source, .zig); |
| 6077 | defer tree.deinit(allocator); | 6077 | defer tree.deinit(allocator); |
| 6078 | 6078 | ||
| 6079 | for (tree.errors) |parse_error| { | 6079 | for (tree.errors) |parse_error| { |
| ... | @@ -6124,7 +6124,7 @@ fn testCanonical(source: [:0]const u8) !void { | ... | @@ -6124,7 +6124,7 @@ fn testCanonical(source: [:0]const u8) !void { |
| 6124 | const Error = std.zig.Ast.Error.Tag; | 6124 | const Error = std.zig.Ast.Error.Tag; |
| 6125 | 6125 | ||
| 6126 | fn testError(source: [:0]const u8, expected_errors: []const Error) !void { | 6126 | fn testError(source: [:0]const u8, expected_errors: []const Error) !void { |
| 6127 | var tree = try std.zig.parse(std.testing.allocator, source); | 6127 | var tree = try std.zig.Ast.parse(std.testing.allocator, source, .zig); |
| 6128 | defer tree.deinit(std.testing.allocator); | 6128 | defer tree.deinit(std.testing.allocator); |
| 6129 | 6129 | ||
| 6130 | std.testing.expectEqual(expected_errors.len, tree.errors.len) catch |err| { | 6130 | std.testing.expectEqual(expected_errors.len, tree.errors.len) catch |err| { |
lib/std/zig/perf_test.zig+1-2| ... | @@ -1,7 +1,6 @@ | ... | @@ -1,7 +1,6 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const mem = std.mem; | 2 | const mem = std.mem; |
| 3 | const Tokenizer = std.zig.Tokenizer; | 3 | const Tokenizer = std.zig.Tokenizer; |
| 4 | const Parser = std.zig.Parser; | ||
| 5 | const io = std.io; | 4 | const io = std.io; |
| 6 | const fmtIntSizeBin = std.fmt.fmtIntSizeBin; | 5 | const fmtIntSizeBin = std.fmt.fmtIntSizeBin; |
| 7 | 6 | ||
| ... | @@ -34,6 +33,6 @@ pub fn main() !void { | ... | @@ -34,6 +33,6 @@ pub fn main() !void { |
| 34 | fn testOnce() usize { | 33 | fn testOnce() usize { |
| 35 | var fixed_buf_alloc = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); | 34 | var fixed_buf_alloc = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); |
| 36 | var allocator = fixed_buf_alloc.allocator(); | 35 | var allocator = fixed_buf_alloc.allocator(); |
| 37 | _ = std.zig.parse(allocator, source) catch @panic("parse failure"); | 36 | _ = std.zig.Ast.parse(allocator, source, .zig) catch @panic("parse failure"); |
| 38 | return fixed_buf_alloc.end_index; | 37 | return fixed_buf_alloc.end_index; |
| 39 | } | 38 | } |
src/Module.zig+3-3| ... | @@ -2057,7 +2057,7 @@ pub const File = struct { | ... | @@ -2057,7 +2057,7 @@ pub const File = struct { |
| 2057 | if (file.tree_loaded) return &file.tree; | 2057 | if (file.tree_loaded) return &file.tree; |
| 2058 | 2058 | ||
| 2059 | const source = try file.getSource(gpa); | 2059 | const source = try file.getSource(gpa); |
| 2060 | file.tree = try std.zig.parse(gpa, source.bytes); | 2060 | file.tree = try Ast.parse(gpa, source.bytes, .zig); |
| 2061 | file.tree_loaded = true; | 2061 | file.tree_loaded = true; |
| 2062 | return &file.tree; | 2062 | return &file.tree; |
| 2063 | } | 2063 | } |
| ... | @@ -3662,7 +3662,7 @@ pub fn astGenFile(mod: *Module, file: *File) !void { | ... | @@ -3662,7 +3662,7 @@ pub fn astGenFile(mod: *Module, file: *File) !void { |
| 3662 | file.source = source; | 3662 | file.source = source; |
| 3663 | file.source_loaded = true; | 3663 | file.source_loaded = true; |
| 3664 | 3664 | ||
| 3665 | file.tree = try std.zig.parse(gpa, source); | 3665 | file.tree = try Ast.parse(gpa, source, .zig); |
| 3666 | defer if (!file.tree_loaded) file.tree.deinit(gpa); | 3666 | defer if (!file.tree_loaded) file.tree.deinit(gpa); |
| 3667 | 3667 | ||
| 3668 | if (file.tree.errors.len != 0) { | 3668 | if (file.tree.errors.len != 0) { |
| ... | @@ -3977,7 +3977,7 @@ pub fn populateBuiltinFile(mod: *Module) !void { | ... | @@ -3977,7 +3977,7 @@ pub fn populateBuiltinFile(mod: *Module) !void { |
| 3977 | else => |e| return e, | 3977 | else => |e| return e, |
| 3978 | } | 3978 | } |
| 3979 | 3979 | ||
| 3980 | file.tree = try std.zig.parse(gpa, file.source); | 3980 | file.tree = try Ast.parse(gpa, file.source, .zig); |
| 3981 | file.tree_loaded = true; | 3981 | file.tree_loaded = true; |
| 3982 | assert(file.tree.errors.len == 0); // builtin.zig must parse | 3982 | assert(file.tree.errors.len == 0); // builtin.zig must parse |
| 3983 | 3983 |
src/main.zig+5-5| ... | @@ -4361,7 +4361,7 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void | ... | @@ -4361,7 +4361,7 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void |
| 4361 | }; | 4361 | }; |
| 4362 | defer gpa.free(source_code); | 4362 | defer gpa.free(source_code); |
| 4363 | 4363 | ||
| 4364 | var tree = std.zig.parse(gpa, source_code) catch |err| { | 4364 | var tree = Ast.parse(gpa, source_code, .zig) catch |err| { |
| 4365 | fatal("error parsing stdin: {}", .{err}); | 4365 | fatal("error parsing stdin: {}", .{err}); |
| 4366 | }; | 4366 | }; |
| 4367 | defer tree.deinit(gpa); | 4367 | defer tree.deinit(gpa); |
| ... | @@ -4566,7 +4566,7 @@ fn fmtPathFile( | ... | @@ -4566,7 +4566,7 @@ fn fmtPathFile( |
| 4566 | // Add to set after no longer possible to get error.IsDir. | 4566 | // Add to set after no longer possible to get error.IsDir. |
| 4567 | if (try fmt.seen.fetchPut(stat.inode, {})) |_| return; | 4567 | if (try fmt.seen.fetchPut(stat.inode, {})) |_| return; |
| 4568 | 4568 | ||
| 4569 | var tree = try std.zig.parse(fmt.gpa, source_code); | 4569 | var tree = try Ast.parse(fmt.gpa, source_code, .zig); |
| 4570 | defer tree.deinit(fmt.gpa); | 4570 | defer tree.deinit(fmt.gpa); |
| 4571 | 4571 | ||
| 4572 | try printErrsMsgToStdErr(fmt.gpa, fmt.arena, tree.errors, tree, file_path, fmt.color); | 4572 | try printErrsMsgToStdErr(fmt.gpa, fmt.arena, tree.errors, tree, file_path, fmt.color); |
| ... | @@ -5312,7 +5312,7 @@ pub fn cmdAstCheck( | ... | @@ -5312,7 +5312,7 @@ pub fn cmdAstCheck( |
| 5312 | file.pkg = try Package.create(gpa, "root", null, file.sub_file_path); | 5312 | file.pkg = try Package.create(gpa, "root", null, file.sub_file_path); |
| 5313 | defer file.pkg.destroy(gpa); | 5313 | defer file.pkg.destroy(gpa); |
| 5314 | 5314 | ||
| 5315 | file.tree = try std.zig.parse(gpa, file.source); | 5315 | file.tree = try Ast.parse(gpa, file.source, .zig); |
| 5316 | file.tree_loaded = true; | 5316 | file.tree_loaded = true; |
| 5317 | defer file.tree.deinit(gpa); | 5317 | defer file.tree.deinit(gpa); |
| 5318 | 5318 | ||
| ... | @@ -5438,7 +5438,7 @@ pub fn cmdChangelist( | ... | @@ -5438,7 +5438,7 @@ pub fn cmdChangelist( |
| 5438 | file.source = source; | 5438 | file.source = source; |
| 5439 | file.source_loaded = true; | 5439 | file.source_loaded = true; |
| 5440 | 5440 | ||
| 5441 | file.tree = try std.zig.parse(gpa, file.source); | 5441 | file.tree = try Ast.parse(gpa, file.source, .zig); |
| 5442 | file.tree_loaded = true; | 5442 | file.tree_loaded = true; |
| 5443 | defer file.tree.deinit(gpa); | 5443 | defer file.tree.deinit(gpa); |
| 5444 | 5444 | ||
| ... | @@ -5476,7 +5476,7 @@ pub fn cmdChangelist( | ... | @@ -5476,7 +5476,7 @@ pub fn cmdChangelist( |
| 5476 | if (new_amt != new_stat.size) | 5476 | if (new_amt != new_stat.size) |
| 5477 | return error.UnexpectedEndOfFile; | 5477 | return error.UnexpectedEndOfFile; |
| 5478 | 5478 | ||
| 5479 | var new_tree = try std.zig.parse(gpa, new_source); | 5479 | var new_tree = try Ast.parse(gpa, new_source, .zig); |
| 5480 | defer new_tree.deinit(gpa); | 5480 | defer new_tree.deinit(gpa); |
| 5481 | 5481 | ||
| 5482 | try printErrsMsgToStdErr(gpa, arena, new_tree.errors, new_tree, new_source_file, .auto); | 5482 | try printErrsMsgToStdErr(gpa, arena, new_tree.errors, new_tree, new_source_file, .auto); |