Add parser support for keys and indices

This commit is contained in:
kayomn 2023-08-12 14:51:47 +01:00
parent 415dff46b7
commit 931d1fe6ed
2 changed files with 50 additions and 13 deletions

View File

@ -351,14 +351,6 @@ fn parse_factor(self: *Self) ParseError!Expression {
break: parse .{.table_literal = table_literal}; break: parse .{.table_literal = table_literal};
}, },
.symbol_bracket_left => {
self.tokenizer.skip_newlines();
if (self.tokenizer.token != .symbol_equals) {
return self.report("expected expression after identifier");
}
},
.symbol_period => { .symbol_period => {
self.tokenizer.step(); self.tokenizer.step();
@ -370,15 +362,11 @@ fn parse_factor(self: *Self) ParseError!Expression {
self.tokenizer.skip_newlines(); self.tokenizer.skip_newlines();
if (self.tokenizer.token != .symbol_equals) { if (self.tokenizer.token != .symbol_equals) {
return self.report("expected `=` after key"); return self.report("expected `=` after symbol");
} }
self.tokenizer.skip_newlines(); self.tokenizer.skip_newlines();
if (self.tokenizer.token == .end) {
return self.report("unexpected end after `=`");
}
try table_literal.push_one(.{ try table_literal.push_one(.{
.value_expression = try self.parse_expression(), .value_expression = try self.parse_expression(),
.key_expression = .{.symbol_literal = identifier}, .key_expression = .{.symbol_literal = identifier},
@ -397,6 +385,41 @@ fn parse_factor(self: *Self) ParseError!Expression {
} }
}, },
.symbol_bracket_left => {
self.tokenizer.step();
const index_expression = try self.parse_expression();
if (self.tokenizer.token != .symbol_bracket_right) {
return self.report("expected `]` expression");
}
self.tokenizer.skip_newlines();
if (self.tokenizer.token != .symbol_equals) {
return self.report("expected `=` after `]`");
}
self.tokenizer.skip_newlines();
try table_literal.push_one(.{
.value_expression = try self.parse_expression(),
.key_expression = index_expression,
});
switch (self.tokenizer.token) {
.symbol_comma => self.tokenizer.skip_newlines(),
.symbol_brace_right => {
self.tokenizer.skip_newlines();
break: parse .{.table_literal = table_literal};
},
else => return self.report("expected `,` or `}` after expression"),
}
},
else => return self.report("expected `}` or fields in table literal"), else => return self.report("expected `}` or fields in table literal"),
} }
} }

View File

@ -287,6 +287,20 @@ pub const Tokenizer = struct {
return; return;
}, },
'[' => {
self.token = .symbol_bracket_left;
cursor += 1;
return;
},
']' => {
self.token = .symbol_bracket_right;
cursor += 1;
return;
},
',' => { ',' => {
self.token = .symbol_comma; self.token = .symbol_comma;
cursor += 1; cursor += 1;