Files

63 lines
2.2 KiB
Plaintext
Raw Permalink Normal View History

2026-10-26 10:14:11 -05:00
#! @file fields.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the parsing of the fields of struct literals and attributes, with
#! their spreads and renames.
module sakura::analyzer;
#! The fields of a struct literal or an attribute, from its `{` to its `}`.
#! @param this The parser.
#! @returns The run of `field_init` nodes.
def parser::field_list(&mut this): u32 {
this.advance();
return this.list(parser::item::field, tk::rbrace, "Expected `}` to close the structure literal.");
}
#! A field of a struct literal or an attribute, a `field_init`, whose token is
#! the field where it is named, as `x` of `x = 1`, which its `named` flag says,
#! and the first of its value where it is given by position.
#! @param this The parser.
#! @returns The `field_init` node.
def parser::field_init(&mut this): u32 {
val token := this.index;
val named := this.check(tk::name) && this.peek(1) == tk::assign;
if named {
this.advance();
this.advance();
}
return this.make_node(nk::field_init, token, this.expression(), parser::bit(named, nf::named));
}
#! The type of a field that spreads another's fields into its own, as `...T` or
#! `...T{ a as b }`, from its `...`.
#! @param this The parser.
#! @returns The `spread` node.
def parser::spread(&mut this): u32 {
val token := this.index;
this.advance();
val type_name := this.type_name();
return this.make_node(nk::spread, token, type_name) if !this.accept(tk::lbrace);
val renames := this.list(parser::item::rename, tk::rbrace, "Expected `}` to close the renames.");
return this.make_node(nk::spread, token, type_name, renames);
}
#! `old as new`, which gives a spread field another name.
#! @param this The parser.
#! @returns The `rename` node, or an `error` node.
def parser::rename(&mut this): u32 {
if !this.check(tk::name) {
this.fail(ec::ident, "Expected the name of a field to rename.");
return this.error_here();
}
val old := this.index;
this.advance();
this.expect(tk::kw_as, "Expected `as` followed by the new name.");
return this.make_node(nk::rename, old, this.name_node("Expected the new field name."));
}