fn parseBlock(p: *Parse) !?Node.Index p: *Parse1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974fn parseBlock(p: *Parse) !?Node.Index {
const lbrace = p.eatToken(.l_brace) orelse return null;
const scratch_top = p.scratch.items.len;
defer p.scratch.shrinkRetainingCapacity(scratch_top);
while (true) {
if (p.tokenTag(p.tok_i) == .r_brace) break;
const statement = try p.expectStatementRecoverable() orelse break;
try p.scratch.append(p.gpa, statement);
}
_ = try p.expectToken(.r_brace);
const statements = p.scratch.items[scratch_top..];
const semicolon = statements.len != 0 and (p.tokenTag(p.tok_i - 2)) == .semicolon;
if (statements.len <= 2) {
return try p.addNode(.{
.tag = if (semicolon) .block_two_semicolon else .block_two,
.main_token = lbrace,
.data = .{ .opt_node_and_opt_node = .{
if (statements.len >= 1) statements[0].toOptional() else .none,
if (statements.len >= 2) statements[1].toOptional() else .none,
} },
});
} else {
return try p.addNode(.{
.tag = if (semicolon) .block_semicolon else .block,
.main_token = lbrace,
.data = .{ .extra_range = try p.listToSpan(statements) },
});
}
}