#! @file declarations.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the parsing of declarations: functions and their parameters and #! where clauses, variables, properties and their accessors, structs, unions, #! traits and their members, enums, and type aliases. module sakura::analyzer; #! The name of a declaration, as `name`, `name`, or `box::name`. struct parser::declared { var receiver: u32; #! The type it is declared through, a `type_name` of every segment but the last, or 0. var name: u32; #! The token of its name. var params: u32; #! The run of its own type parameters. } #! The parameters and result of a function or lambda. struct parser::signature { var params: u32; #! The run of `param` nodes. var result: u32; #! The result type, 0 where a lambda leaves it out. } #! A declaration at the top level of a file, or within a `when` there, with the #! attributes and `pub` before it. A declaration's token is its name. #! @param this The parser. #! @returns The declaration's node, or an `error` node holding its attributes #! where it is none. def parser::declaration(&mut this): u32 { var lead := parser::lead { start = this.index, attrs = this.attributes() }; return this.error(this.index, lead.attrs) if this.panic; if this.accept(tk::kw_pub) { lead.exported = true; if this.check(tk::attr) { this.report(ec::token, "Attributes are written before `pub`, as `@name pub def f(): void {}`."); lead.attrs = this.join_runs(lead.attrs, this.attributes()); return this.error(this.index, lead.attrs) if this.panic; } if this.check(tk::kw_pub) { this.fail(ec::token, "A declaration is `pub` once."); return this.error(this.index, lead.attrs); } } # An import, a module, and the `when` around declarations name nothing of their # own, so nothing describes them or exports them. val unnamed := this.check(tk::kw_imp) || this.check(tk::kw_mod) || this.check(tk::kw_extern) || this.check(tk::kw_when); if unnamed && lead.exported { this.report(ec::token, "Only a declaration of a name can be `pub`, and this declares none."); } else if unnamed && lead.attrs != 0 { this.report(ec::token, "Attributes describe a declaration of a name, and this declares none."); } val made := match this.kind { tk::kw_imp => this.path_decl(nk::import_decl), tk::kw_mod => this.path_decl(nk::module_decl), tk::kw_extern => this.extern_decl(), tk::kw_when => this.when_block(true), tk::kw_def => this.function(&lead, false, parser::place::top), tk::kw_const => this.peek(1) == tk::kw_def ? this.const_function(&lead, parser::place::top) : this.variable(&lead, parser::place::top), tk::kw_val, tk::kw_var => this.variable(&lead, parser::place::top), tk::kw_struct, tk::kw_union, tk::kw_trait => this.aggregate(&lead), tk::kw_enum => this.enumeration(&lead), tk::kw_type => this.alias(&lead), else => this.no_declaration() }; return this.declared_at(made, &lead); } def parser::no_declaration(&mut this): u32 { this.fail(ec::decl, "Expected an import, module, `extern`, `when`, function, struct, enum, trait, type, variable, or constant."); return this.error_here(); } #! `import path;`, or `module path;` and `module path when condition;`. #! @param this The parser. #! @param kind `import_decl` or `module_decl`. #! @returns The node. def parser::path_decl(&mut this, kind: nk): u32 { val token := this.index; this.advance(); val path := this.path(); var condition: u32 = 0; if kind == nk::module_decl && this.accept(tk::kw_when) { condition = this.condition(); } this.expect(tk::semicolon, "Expected `;` after the path."); return this.make_node(kind, token, path, condition); } #! `extern import "library";` or `extern module path;`, each with a `when` #! condition where it is written. A section without one is parsed, so the #! checker says what it lacks. #! @param this The parser. #! @returns The `extern_import` or `extern_module` node, or an `error` node. def parser::extern_decl(&mut this): u32 { val token := this.index; this.advance(); if this.check(tk::kw_mod) { this.advance(); val path := this.path(); val condition := this.accept(tk::kw_when) ? this.condition() : 0; this.expect(tk::semicolon, "Expected `;` after the path."); return this.make_node(nk::extern_module, token, path, condition); } if !this.accept(tk::kw_imp) { this.fail(ec::token, "Expected `import` or `module` after `extern`: `extern import \"library\";` names a library, and `extern module path when ...;` begins C's declarations."); return this.error_here(); } if !this.check(tk::string) { this.fail(ec::token, "Expected the library, as `\"libvulkan.so.1\"` or `\"kernel32.dll\"`."); return this.error_here(); } val library := this.literal(nk::string); val condition := this.accept(tk::kw_when) ? this.condition() : 0; this.expect(tk::semicolon, "Expected `;` after the library."); return this.make_node(nk::extern_import, token, library, condition); } #! The name of a declaration. Where receivers are allowed, `T::name` gives the #! receiver as a `type_name` of every segment but the last, whose type #! parameters are each segment's, as `T` of `box::get`. #! @param this The parser. #! @param receivers Whether the name may be declared through a type. #! @returns The receiver, the name's token, and the type parameters. def parser::decl_name(&mut this, receivers: bool): parser::declared { if !this.check(tk::name) { this.fail(ec::ident, "Expected a name for the declaration."); return parser::declared { name = this.index }; } var last := this.index; this.advance(); val mark := this.mark; var params := this.check(tk::less) ? this.type_params() : 0; while receivers && this.check(tk::scope) { this.advance(); if !this.check(tk::name) { this.fail(ec::ident, "Expected a name after `::`."); break; } this.push(this.make_node(nk::segment, last, params)); last = this.index; this.advance(); params = this.check(tk::less) ? this.type_params() : 0; } # The receiver ends where its last segment does, short of the name after it. var receiver: u32 = 0; if this.mark > mark { val segments := this.scratch.span; val first: u32 = this.syntax.node_tokens[segments[mark]]; val end: u32 = this.syntax.node_end(segments[this.mark - 1]); receiver = this.make_node(nk::type_name, first, this.make_run(mark)); this.syntax.set_node_end(receiver, end); } return parser::declared { receiver = receiver, name = last, params = params }; } #! `const def`, a function constants can call. #! @param this The parser. #! @param lead What came before it. #! @param place Where it is written. #! @returns The `function` node. def parser::const_function(&mut this, lead: &parser::lead, place: parser::place): u32 { this.advance(); return this.function(lead, true, place); } #! A function, from its `def`. One ending at its signature, rather than a body, #! is one a trait requires, one the compiler gives, or one of C's, which the #! checker tells apart by its attributes. A method of a trait takes no receiver. #! @param this The parser. #! @param lead What came before it. #! @param constant Whether it is a `const def`. #! @param place Where it is written. #! @returns The `function` node, or an `error` node. def parser::function(&mut this, lead: &parser::lead, constant: bool, place: parser::place): u32 { this.advance(); val in_trait := place == parser::place::member; val declared := this.decl_name(!in_trait); val signature := this.params(false, in_trait || declared.receiver != 0); val outer := this.where_clause(declared.params, declared.receiver); val body := this.accept(tk::semicolon) ? 0 : this.block(); val record := this.make_record(lead.attrs, declared.receiver, declared.params, signature.params, signature.result, body, outer); val flags := parser::bit(lead.exported, nf::exported) | parser::bit(constant, nf::constant); return this.make_node(nk::function, declared.name, record, flags); } #! The parameters and result of a function, from `(` through the result type. A #! lambda may leave out its parameter types and its result, and a method may #! begin with `&this` or `&mut this`. #! @param this The parser. #! @param lambda Whether they are a lambda's. #! @param methods Whether they are a method's, which may take `this`. #! @returns The run of parameters and the result type. def parser::params(&mut this, lambda: bool, methods: bool): parser::signature { if !this.expect(tk::lparen, "Expected `(` to begin the parameters.") { return parser::signature {}; } val mark := this.mark; if !lambda && this.check(tk::bitand) { this.push(this.this_param(methods)); if !this.check(tk::rparen) { this.expect(tk::comma, "Expected `,` or `)` after `this`."); } } var defaulted := false; var variadic := false; while !this.check(tk::rparen) && !this.check(tk::eof) { # A parameter's attributes come before its name, as a declaration's do. An # anonymous function's parameters, which are no declaration's, take none. val attrs := this.check(tk::attr) ? this.attributes() : 0; if lambda && attrs != 0 { this.report(ec::token, "An anonymous function's parameters take no attributes; a declared function's do."); } if !this.check(tk::name) { this.fail(ec::ident, "Expected a parameter name."); break if !this.skip_item(tk::rparen) || !this.accept(tk::comma); continue; } val token := this.index; this.advance(); if variadic { this.report_at(token, ec::token, "A variadic parameter takes every argument after the parameters before it, so it is the last."); } var type_expr: u32 = 0; if !lambda || this.check(tk::colon) { this.expect(tk::colon, "Expected `:` followed by the parameter type."); type_expr = this.type_expr(); } # `name: T...` takes the arguments after the parameters before it. var flags: u32 = 0; if type_expr != 0 && this.check(tk::spread) { if lambda { this.report(ec::token, "Only a function declared by name takes a variadic parameter; an anonymous function takes a `span`."); } this.advance(); flags = parser::bit(!lambda, nf::variadic); variadic = variadic || !lambda; } # A parameter may be given the value a call takes where it leaves the argument # out. Every parameter after one is given one too, so the arguments a call does # give are always the first parameters. var value: u32 = 0; if flags != 0 && this.check(tk::assign) { this.report(ec::token, "A variadic parameter takes no default value, since a call giving no arguments for it gives it none."); this.advance(); value = this.expression(); } else if this.accept(tk::assign) { value = this.expression(); defaulted = true; } else if defaulted && flags == 0 { this.report_at(token, ec::token, "A parameter after one with a default value is given one too, so a call leaving arguments out always leaves out the last."); } this.push(this.make_node(nk::param, token, type_expr, this.make_record(value, flags, attrs))); break if this.panic && !this.skip_item(tk::rparen); break if !this.accept(tk::comma); } val params := this.make_run(mark); this.expect(tk::rparen, "Expected `)` to close the parameters."); if lambda && !this.check(tk::colon) { return parser::signature { params = params }; } this.expect(tk::colon, "Expected `:` followed by the return type."); return parser::signature { params = params, result = this.type_expr() }; } #! `&this` or `&mut this`, the first parameter of a method, from its `&`. Its #! type is left out, since it is a reference to the receiver. #! @param this The parser. #! @param methods Whether it is a method's, which may take `this`. #! @returns The `param` node, or an `error` node. def parser::this_param(&mut this, methods: bool): u32 { this.advance(); val mutable := this.accept(tk::kw_mut); if !this.check_word(names::this_id) { this.fail(ec::ident, "Expected `this`; other parameters are written `name: type`."); return this.error_here(); } val token := this.index; this.advance(); if !methods { this.report_at(token, ec::token, "Only methods, declared as `type::name`, take `this`."); } return this.make_node(nk::param, token, 0, this.make_record(0, parser::bit(mutable, nf::mutable), 0)); } #! The `where` clauses of a declaration, one for each type parameter it bounds, #! as `where K: hashable, equatable where V: Show`. The bounds become those of #! the `type_param` nodes of the declaration, or of its receiver, as `T` of #! `box::get`. A trait's method may also bound the trait's type parameters #! and `self`, which only that method then asks of them, as `where self: #! backward_iterator`: each is a `type_param` of the name, made here. #! @param this The parser. #! @param params The run of the declaration's type parameters. #! @param receiver The receiver's `type_name`, or 0. #! @returns The run of the bounds a trait's method puts on the trait's type #! parameters and `self`, or 0. def parser::where_clause(&mut this, params: u32, receiver: u32): u32 { val outer_mark := this.mark; var outer := vector::::new(); while this.accept(tk::kw_where) { if !this.check(tk::name) { this.fail(ec::ident, "Expected the name of a type parameter to bound."); break; } val name := this.index; val word: u32 = this.syntax.token_names[this.index]; var param := this.find_param(params, receiver, word); if param == 0 && this.in_members && (word == names::self_id || this.find_in(this.member_params, word) != 0) { for &each in outer { param = this.name_of(each) == word ? each : param; } if param == 0 { param = this.make_node(nk::type_param, name); outer.append(param); } } if param == 0 { this.report(ec::token, this.in_members ? "Only the type parameters of the method, of its trait, and `self` are bounded here." : "Only the type parameters of the declaration, and of its receiver, are bounded here."); } this.advance(); # A pack's bounds are each of its types', written `where Ts...: Show`. val dotted := this.accept(tk::spread); val packed := param != 0 && this.syntax.node_kinds[param] == nk::type_param && (this.syntax.node_rights[param] & nf::packed as u32) != 0; if dotted && param != 0 && !packed { this.report_at(name, ec::token, "Only a pack, declared `Ts...`, is bounded as `Ts...`."); } else if packed && !dotted { this.report_at(name, ec::token, "A pack's bounds are each of its types', written `where Ts...: Show`."); } this.expect(tk::colon, "Expected `:` followed by the bounds."); # The bounds it already has come first, since each `where` adds to them. val mark := this.mark; if param != 0 { val had := this.syntax.node_lefts[param]; for &node in this.run_view(had) { this.push(node); } } while true { this.push(this.type_name()); break if !this.accept(tk::comma); } if param != 0 { val bounds := this.make_run(mark); this.syntax.node_lefts[param] = bounds; } else { this.drop(mark); } } return 0 if outer.length == 0; for &each in outer { this.push(each); } return this.make_run(outer_mark); } #! The `type_param` of the name among the type parameters, or those of the #! receiver's segments, or 0 where there is none. #! @param this The parser. #! @param params The run of the declaration's type parameters. #! @param receiver The receiver's `type_name`, or 0. #! @param name The ID of the name. #! @returns The `type_param` node, or 0. def parser::find_param(&this, params: u32, receiver: u32, name: u32): u32 { val own := this.find_in(params, name); return own if own != 0 || receiver == 0; val segments := this.syntax.node_lefts[receiver]; for &segment in this.run_view(segments) { val found := this.find_in(this.syntax.node_lefts[segment], name); return found if found != 0; } return 0; } def parser::find_in(&this, params: u32, name: u32): u32 { for ¶m in this.run_view(params) { # A pattern's type parameters are its own parameters' and its result's. if this.syntax.node_kinds[param] == nk::type_pattern { val inner := this.find_in(this.syntax.node_lefts[param], name); return inner if inner != 0; val result := this.syntax.node_rights[param]; return result if result != 0 && this.name_of(result) == name; continue; } # An array pattern's are its element's and its length's. if this.syntax.node_kinds[param] == nk::array_pattern { val element := this.syntax.node_lefts[param]; return element if this.name_of(element) == name; val length := this.syntax.node_rights[param]; return length if length != 0 && this.name_of(length) == name; continue; } return param if this.name_of(param) == name; } return 0; } #! A variable, constant, or field, from its `val`, `var`, or `const`, or the #! property it begins where `{` follows its type. Where it is written says what #! it may be: one at the top level may be declared through a type, as #! `rect::unit`, one of a struct or union is a `field`, and one at the top level #! or of a trait may be a property. #! @param this The parser. #! @param lead What came before it. #! @param place Where it is written. #! @returns The `variable`, `field`, or `property` node, or an `error` node. def parser::variable(&mut this, lead: &parser::lead, place: parser::place): u32 { val mutable := this.check(tk::kw_var); val constant := this.check(tk::kw_const); val kind := place == parser::place::field ? nk::field : nk::variable; this.advance(); val declared := this.decl_name(place == parser::place::top); if declared.params != 0 { this.report_at(declared.name, ec::token, "Only functions, types, and aliases take type parameters of their own; a member of a generic type is declared through it, as `box::empty`."); } this.expect(tk::colon, "Expected `:` after the name, followed by a type, `=`, or both."); var type_expr: u32 = 0; if !this.check(tk::assign) && !this.check(tk::semicolon) { type_expr = kind == nk::field && this.check(tk::spread) ? this.spread() : this.type_expr(); } # `name: Ts...` holds a value of each of a pack's types, which only a struct's # field does, as the checker says. var packed := false; if type_expr != 0 && this.syntax.node_kinds[type_expr] != nk::spread && this.check(tk::spread) { this.advance(); packed = true; } # `{` after the type opens the accessors of a property. val properties := place == parser::place::top || place == parser::place::member; if properties && !constant && type_expr != 0 && this.check(tk::lbrace) { if packed { this.report(ec::token, "A property's value is one value, so its type is no pack."); } if place == parser::place::top && declared.receiver == 0 { this.report_at(declared.name, ec::token, "A property belongs to a type, so it is declared as `type::name`."); } return this.property(lead, declared, type_expr, mutable, place); } # `= undefined;` leaves a variable's memory as it is, and is a word only there. var value: u32 = 0; if this.accept(tk::assign) { val left := this.check_word(names::undefined_id) && this.peek(1) == tk::semicolon; value = left ? this.literal(nk::undefined) : this.expression(); } if type_expr == 0 && value == 0 { this.fail(ec::kind, "Expected a type or `=` followed by a value to infer it from."); } this.expect(tk::semicolon, "Expected `;` after the declaration."); val record := this.make_record(lead.attrs, declared.receiver, type_expr, value); val flags := parser::bit(lead.exported, nf::exported) | parser::bit(mutable, nf::mutable) | parser::bit(constant, nf::constant) | parser::bit(packed, nf::variadic); return this.make_node(kind, declared.name, record, flags); } #! The `{ get ... set ... }` of a property, from its `{`. Each accessor is as #! visible as the property unless it takes its own `pub`, which is how a #! property reads everywhere but is written only where it is declared. A #! `get mut` reads it where the value can be written, beside or instead of the #! `get`, as a method's `&mut this` overload is called there. #! @param this The parser. #! @param lead What came before it. #! @param declared Its name and receiver. #! @param type_expr Its type's node. #! @param mutable Whether it is declared `var`. #! @param place Where it is written. #! @returns The `property` node. def parser::property( &mut this, lead: &parser::lead, declared: parser::declared, type_expr: u32, mutable: bool, place: parser::place ): u32 { this.advance(); val in_trait := place == parser::place::member; val methods := in_trait || declared.receiver != 0; val mark := this.mark; var getter := false; var writer := false; var setter := false; while !this.check(tk::rbrace) && !this.check(tk::eof) { # A trait's members are as visible as the trait, so it takes none. val marker := this.index; var own := this.accept(tk::kw_pub); if own && in_trait { this.report_at(marker, ec::token, "A trait's members are as visible as the trait, so its accessors are never `pub`."); own = false; } val get := this.check_word(names::get_id); val set := this.check_word(names::set_id); if !get && !set { this.fail(ec::token, own ? "Expected `get` or `set` after `pub`, the accessors of a property." : "Expected `get` or `set`, the accessors of a property."); break; } # `get mut` is the `get` of a value that can be written. val writes := get && this.peek(1) == tk::kw_mut; if (get && !writes && getter) || (writes && writer) || (set && setter) { this.fail(ec::token, writes ? "The property already has a `get mut`." : get ? "The property already has a `get`." : "The property already has a `set`."); break; } if writes && in_trait { this.fail(ec::token, "A trait asks for a property's `get` and `set`; a `get mut` is the implementer's own."); break; } if set && !mutable { this.fail(ec::token, "A `val` property only reads, so it has no `set`; declare it with `var` to write it."); break; } this.push(this.accessor(own, methods, get, writes)); getter = getter || (get && !writes); writer = writer || writes; setter = setter || set; break if this.panic; } # Past a malformed accessor, skip to the brace closing them, so what follows # is read as the declarations it is rather than as more of the property. if this.panic { this.skip_to_close(); } val accessors := this.make_run(mark); this.expect(tk::rbrace, "Expected `}` to close the accessors."); if !getter && !writer { this.report_at(declared.name, ec::decl, "A property is read through `get`, or `get mut`, which it must declare."); } val flags := parser::bit(mutable, nf::mutable) | parser::bit(lead.exported, nf::exported); val record := this.make_record(lead.attrs, declared.receiver, type_expr, flags); return this.make_node(nk::property, declared.name, record, accessors); } #! One accessor of a property, `get`, `get mut`, or `set`, from its name, #! followed by its body: a block, `=> expression;`, or `;` where a trait asks for #! it. Its parameters are implied, and made here: `this`, which a `get` reads #! and a `get mut` and a `set` write, and a `set`'s `value`, of the property's #! type. `=>` gives what a getter returns, and is what a `set` does. #! @param this The parser. #! @param exported Whether the property is `pub`. #! @param methods Whether it is declared through a type, so its accessors #! take `this`. #! @param get Whether it is a getter. #! @param writes Whether it is the `get mut`, taking `&mut this`. #! @returns The `accessor` node, or an `error` node. def parser::accessor(&mut this, exported: bool, methods: bool, get: bool, writes: bool): u32 { val token := this.index; this.advance(); if writes { this.advance(); } val mark := this.mark; if this.check(tk::lparen) { # The form before, read on so what follows is checked as it means. this.report(ec::token, get ? "A `get` takes no parameters written: it has `this`, as `get => this.w;` or `get { return this.w; }`." : "A `set` takes no parameters written: it has `this`, and `value` of the property's type, as `set => this.w = value;` or `set { this.w = value; }`."); this.written_params(methods, get); } else { this.push(this.make_node(nk::param, token, 0, this.make_record(0, parser::bit(!get || writes, nf::mutable) | nf::implied as u32, 0))); if !get { this.push(this.make_node(nk::param, token, 0, this.make_record(0, nf::implied as u32, 0))); } } val params := this.make_run(mark); var body: u32 = 0; if this.check(tk::arrow) { val arrow := this.index; this.advance(); val made := this.expression(); this.expect(tk::semicolon, "Expected `;` after the accessor's expression."); val stated := this.make_node(get ? nk::return_stmt : nk::expr_stmt, arrow, made); body = this.make_node(nk::block, arrow, this.run_of(stated)); } else if !this.accept(tk::semicolon) { body = this.block(); } return this.make_node(nk::accessor, token, params, this.make_record(body, parser::bit(exported, nf::exported))); } #! The parameters of an accessor written out, `(&this)` or `(&mut this, to)`, #! as they were before they were implied, from its `(`, which a note refuses. #! @param this The parser. #! @param methods Whether it is declared through a type, so it takes `this`. #! @param get Whether it is the getter. def parser::written_params(&mut this, methods: bool, get: bool): void { this.advance(); if this.check(tk::bitand) { this.push(this.this_param(methods)); if !this.check(tk::rparen) { this.expect(tk::comma, "Expected `,` or `)` after `this`."); } } else { this.report(ec::token, get ? "A getter reads the value it belongs to, so it takes `&this`." : "A setter changes the value it belongs to, so it takes `&mut this` and the value to store."); } while !this.check(tk::rparen) && !this.check(tk::eof) { if !this.check(tk::name) { this.fail(ec::ident, "Expected a parameter name."); break if !this.skip_item(tk::rparen) || !this.accept(tk::comma); continue; } val name := this.index; this.advance(); val type_expr := this.accept(tk::colon) ? this.type_expr() : 0; this.push(this.make_node(nk::param, name, type_expr)); break if this.panic && !this.skip_item(tk::rparen); break if !this.accept(tk::comma); } this.expect(tk::rparen, "Expected `)` to close the parameters of the accessor."); } #! A struct, union, or trait, from its keyword. A struct or union names the #! traits it implements, and a trait names the traits it composes. #! @param this The parser. #! @param lead What came before it. #! @returns The `struct_decl`, `union_decl`, or `trait_decl` node. def parser::aggregate(&mut this, lead: &parser::lead): u32 { val kind := match this.kind { tk::kw_struct => nk::struct_decl, tk::kw_union => nk::union_decl, else => nk::trait_decl }; val in_trait := kind == nk::trait_decl; this.advance(); val declared := this.decl_name(true); var traits: u32 = 0; if this.accept(tk::colon) { val mark := this.mark; while true { this.push(this.type_name()); break if !this.accept(tk::comma); } traits = this.make_run(mark); } this.where_clause(declared.params, 0); val flags := parser::bit(lead.exported, nf::exported); val record := this.make_record(lead.attrs, declared.receiver, declared.params, traits, flags); if !this.expect(tk::lbrace, "Expected `{` to begin the members.") { return this.make_node(kind, declared.name, record); } val scope := in_trait ? parser::scope::members : parser::scope::fields; val mark := this.mark; val was_in_members := this.in_members; val was_params := this.member_params; this.in_members = in_trait; this.member_params = in_trait ? declared.params : 0; while !this.check(tk::rbrace) && !this.check(tk::eof) { var member_lead := parser::lead { start = this.index, attrs = this.attributes() }; if this.panic { this.push(this.error(this.index, member_lead.attrs)); this.synchronize(scope); continue; } val member := in_trait ? this.trait_member(&member_lead) : this.field_member(&mut member_lead); if member != 0 { this.push(this.declared_at(member, &member_lead)); } if this.panic { this.synchronize(scope); } } this.in_members = was_in_members; this.member_params = was_params; val members := this.make_run(mark); this.expect(tk::rbrace, "Expected `}` to close the members."); return this.make_node(kind, declared.name, record, members); } #! A field of a struct or union, which `pub` lets modules other than its own #! reach. #! @param this The parser. #! @param lead What came before it, which `pub` is added to. #! @returns The `field` node, or an `error` node holding the attributes before #! what is not one, or 0 where there were none. def parser::field_member(&mut this, lead: &mut parser::lead): u32 { if this.accept(tk::kw_pub) { lead.exported = true; if this.check(tk::attr) { this.report(ec::token, "Attributes are written before `pub`, as `@name pub var x: T;`."); lead.attrs = this.join_runs(lead.attrs, this.attributes()); return this.error(this.index, lead.attrs) if this.panic; } if this.check(tk::kw_pub) { this.fail(ec::token, "A declaration is `pub` once."); return this.error(this.index, lead.attrs); } } if this.check(tk::kw_var) || this.check(tk::kw_val) { return this.variable(lead, parser::place::field); } this.fail(ec::token, "Expected a field declared with `var` or `val`."); return lead.attrs != 0 ? this.error(this.index, lead.attrs) : 0; } #! A member of a trait: a method, which `;` leaves to the implementer, a #! constant, a property, or a type it asks for. #! @param this The parser. #! @param lead What came before it. #! @returns The member's node, or an `error` node holding what was parsed of #! what is not one, or 0 where nothing was. def parser::trait_member(&mut this, lead: &parser::lead): u32 { val place := parser::place::member; match this.kind { tk::kw_def => return this.function(lead, false, place); tk::kw_type => return this.assoc_type(lead); tk::kw_const => { return this.const_function(lead, place) if this.peek(1) == tk::kw_def; return this.variable(lead, place); } # A trait requires a property by declaring its accessors without bodies, and # `var` and `val` declare nothing else of a trait. tk::kw_var, tk::kw_val => { val keyword := this.index; val member := this.variable(lead, place); return member if this.syntax.node_kinds[member] == nk::property; this.fail_at(keyword, ec::token, "A trait declares a property with its accessors, as `val area: int { get; }`, and a constant with `const`."); return this.error(keyword, this.run_of(member)); } tk::kw_pub => this.fail(ec::token, "A trait's members are as visible as the trait, so they are never `pub`."); else => this.fail(ec::token, "Expected a method signature declared with `def`, a property with `var` or `val`, a constant with `const`, or a type with `type`."); } return lead.attrs != 0 ? this.error(this.index, lead.attrs) : 0; } #! `type name: A, B = T;`, a type a trait asks of what implements it, which the #! bounds constrain and the default stands in for where the implementer gives #! none. #! @param this The parser. #! @param lead What came before it. #! @returns The `assoc_type` node. def parser::assoc_type(&mut this, lead: &parser::lead): u32 { this.advance(); if !this.check(tk::name) { this.fail(ec::ident, "Expected the name of the type the trait asks for."); return this.error_here(); } val token := this.index; this.advance(); # `type params...;` asks for a pack, which only the compiler gives. val packed := this.accept(tk::spread); var bounds: u32 = 0; if this.accept(tk::colon) { val mark := this.mark; while true { this.push(this.type_name()); break if !this.accept(tk::comma); } bounds = this.make_run(mark); } val fallback := this.accept(tk::assign) ? this.type_expr() : 0; this.expect(tk::semicolon, "Expected `;` after the type the trait asks for."); return this.make_node(nk::assoc_type, token, this.make_record(lead.attrs, bounds, parser::bit(packed, nf::packed)), fallback); } #! `enum name: type { member, member = value, ... }`, whose type may be left #! out. An enum is never generic, but it may be nested in a type that is, as #! `enum list::state`, naming that type's parameters. #! @param this The parser. #! @param lead What came before it. #! @returns The `enumeration` node. def parser::enumeration(&mut this, lead: &parser::lead): u32 { this.advance(); if !this.check(tk::name) { this.fail(ec::ident, "Expected the name of the enumeration."); return this.error_here(); } val declared := this.decl_name(true); if declared.params != 0 { this.report_at(declared.name, ec::token, "An enum is never generic, though it may be nested in a type that is."); } val integer := this.accept(tk::colon) ? this.type_expr() : 0; val record := this.make_record(lead.attrs, declared.receiver, integer, parser::bit(lead.exported, nf::exported)); if !this.accept(tk::lbrace) { this.fail(ec::token, "Expected `{` to begin the members."); return this.make_node(nk::enumeration, declared.name, record); } val members := this.list(parser::item::enumerator, tk::rbrace, "Expected `,` between the members, or `}` to close them."); return this.make_node(nk::enumeration, declared.name, record, members); } #! `type name = type where T: bound;`. #! @param this The parser. #! @param lead What came before it. #! @returns The `alias` node. def parser::alias(&mut this, lead: &parser::lead): u32 { this.advance(); val declared := this.decl_name(true); this.expect(tk::assign, "Expected `=` followed by the aliased type."); val aliased := this.type_expr(); this.where_clause(declared.params, 0); if this.check(tk::bitor) { this.fail(ec::token, "A value that is one of several types is a `union`, as `union name { var a: A; var b: B; }`."); } else if this.check(tk::bitand) { this.fail(ec::token, "Types are combined as the fields of a `struct`, as `struct name { var a: A; var b: B; }`."); } else { this.expect(tk::semicolon, "Expected `;` after the type."); } val record := this.make_record(lead.attrs, declared.receiver, declared.params, parser::bit(lead.exported, nf::exported)); return this.make_node(nk::alias, declared.name, record, aliased); } #! A member of an enum, with the attributes before it, and the value it is #! given, which may be left out. #! @param this The parser. #! @returns The `enumerator` node, or an `error` node. def parser::enumerator(&mut this): u32 { val lead := parser::lead { start = this.index, attrs = this.attributes() }; return this.error(this.index, lead.attrs) if this.panic; if !this.check(tk::name) { this.fail(ec::ident, "Expected the name of a member."); return this.error(this.index, lead.attrs); } val token := this.index; this.advance(); val value := this.accept(tk::assign) ? this.expression() : 0; return this.declared_at(this.make_node(nk::enumerator, token, lead.attrs, value), &lead); }