Initial Commit
This commit is contained in:
789 files changed
+149185
No files matched your search
@@ -0,0 +1,44 @@
|
||||
#! @file attribute.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the parsing of attributes, written `@name` or `@name{ fields }`
|
||||
#! before a declaration.
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! The attributes before a declaration, as `@name` or `@name{ fields }`.
|
||||
#! @param this The parser.
|
||||
#! @returns The run of `attribute` nodes, empty when there are none.
|
||||
def parser::attributes(&mut this): u32 {
|
||||
val mark := this.mark;
|
||||
while this.check(tk::attr) {
|
||||
val made := this.attribute();
|
||||
break if made == 0;
|
||||
this.push(made);
|
||||
}
|
||||
|
||||
return this.make_run(mark);
|
||||
}
|
||||
|
||||
#! One attribute, from its `@`, whose value is the struct literal written, so
|
||||
#! `@name` is `name{}`.
|
||||
#! @param this The parser.
|
||||
#! @returns The `attribute` node, or 0 where no type follows the `@`.
|
||||
def parser::attribute(&mut this): u32 {
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
val type_name := this.type_name();
|
||||
return 0 if this.syntax.node_kinds[type_name] == nk::error;
|
||||
|
||||
|
||||
return this.make_node(
|
||||
nk::attribute,
|
||||
token,
|
||||
type_name,
|
||||
this.check(tk::lbrace)
|
||||
? this.field_list()
|
||||
: 0
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,923 @@
|
||||
#! @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<T>`, or `box<T>::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<T>::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<T>`.");
|
||||
}
|
||||
|
||||
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<T>::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<T>`: 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::<u32>::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<T>::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<T>::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<T> = 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);
|
||||
}
|
||||
@@ -0,0 +1,349 @@
|
||||
#! @file expressions.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the expression parser, a Pratt parser, which reads each operator
|
||||
#! by the binding power `token_powers` gives it. Prefix and postfix operators,
|
||||
#! casts, `is` tests, ternaries, `else` fallbacks, calls, indexes, and members
|
||||
#! are each parsed here.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! How tightly each operator binds, loosest first. An operator continues an
|
||||
#! expression only where it binds tighter than what the expression began under.
|
||||
enum parser::bp : u32 {
|
||||
none, #! Nothing has begun.
|
||||
assign, #! `=`, `+=`, and the rest, which group to the right.
|
||||
fallback, #! `x else y`, which groups to the right.
|
||||
ternary, #! `c ? a : b`, which groups to the right.
|
||||
range, #! `a..b` and `a..=b`.
|
||||
logor, #! `||`
|
||||
logand, #! `&&`
|
||||
bitor, #! `|`
|
||||
bitxor, #! `^`
|
||||
bitand, #! `&`
|
||||
equality, #! `==` and `!=`
|
||||
compare, #! `<`, `<=`, `>`, and `>=`
|
||||
shift, #! `<<` and `>>`
|
||||
sum, #! `+` and `-`
|
||||
product, #! `*`, `/`, and `%`
|
||||
cast, #! `as` and `is`, after a prefix operator's operand, so `-x as u8` is `(-x) as u8`.
|
||||
prefix, #! What a prefix operator applies to.
|
||||
postfix, #! Calls, indexing, members, `++`, and `--`.
|
||||
}
|
||||
|
||||
#! An expression, where struct literals are allowed.
|
||||
#! @param this The parser.
|
||||
#! @returns The expression's node, or an `error` node.
|
||||
def parser::expression(&mut this): u32 {
|
||||
return this.pratt(parser::bp::none, true);
|
||||
}
|
||||
|
||||
#! The expression of a condition, or of what a `for` loop or `match` takes,
|
||||
#! where `{` begins the block after it rather than a struct literal.
|
||||
#! @param this The parser.
|
||||
#! @returns The expression's node, or an `error` node.
|
||||
def parser::condition_expr(&mut this): u32 {
|
||||
return this.pratt(parser::bp::none, false);
|
||||
}
|
||||
|
||||
#! Parses an expression by precedence climbing: an operand, then each operator
|
||||
#! binding tighter than `min` with what it applies to.
|
||||
#! @param this The parser.
|
||||
#! @param min The power an operator must bind tighter than to be taken.
|
||||
#! @param structs Whether a name followed by `{` is a struct literal, which it
|
||||
#! is not in conditions, so that `if x {` opens the block. It
|
||||
#! is passed down rather than kept on the parser, so nothing
|
||||
#! has to put it back.
|
||||
#! @returns The expression's node, or an `error` node.
|
||||
def parser::pratt(&mut this, min: parser::bp, structs: bool): u32 {
|
||||
return this.error_here() if !this.enter();
|
||||
|
||||
var left := this.prefix(structs);
|
||||
while true {
|
||||
val power := this.power(left);
|
||||
break if power as u32 <= min as u32;
|
||||
left = this.infix(left, power, structs);
|
||||
}
|
||||
|
||||
this.leave();
|
||||
return left;
|
||||
}
|
||||
|
||||
#! How tightly the current token binds as an operator after `left`, or `none`
|
||||
#! where it ends the expression.
|
||||
#! @param this The parser.
|
||||
#! @param left The operand before the token.
|
||||
#! @returns The power.
|
||||
def parser::power(&this, left: u32): parser::bp {
|
||||
val power := token_powers[this.kind as u64];
|
||||
|
||||
# `else =>` is the last arm of a match, not a fallback, and `::<` gives type
|
||||
# arguments only to what a call gives, as `value_of(d)::<i64>`.
|
||||
return parser::bp::none if this.check(tk::kw_else) && this.peek(1) == tk::arrow;
|
||||
# Within type arguments, `>` closes them, and nothing is assigned.
|
||||
if this.angled {
|
||||
val kind := this.kind;
|
||||
return parser::bp::none if kind == tk::more || kind == tk::moreeq || kind == tk::rshift || kind == tk::rshifteq
|
||||
|| power == parser::bp::assign;
|
||||
}
|
||||
return parser::bp::none if this.check(tk::scope) && (this.syntax.node_kinds[left] != nk::call || this.peek(1) != tk::less);
|
||||
return power;
|
||||
}
|
||||
|
||||
#! An operand: a prefix operator and what it applies to, or a primary.
|
||||
#! @param this The parser.
|
||||
#! @param structs Whether a name followed by `{` is a struct literal.
|
||||
#! @returns The operand's node, or an `error` node.
|
||||
def parser::prefix(&mut this, structs: bool): u32 {
|
||||
val token := this.index;
|
||||
match this.kind {
|
||||
tk::minus, tk::negate, tk::bitnot, tk::increment, tk::decrement, tk::star => {
|
||||
this.advance();
|
||||
return this.make_node(nk::unary, token, this.pratt(parser::bp::prefix, structs));
|
||||
}
|
||||
tk::bitand => {
|
||||
this.advance();
|
||||
val mutable := this.accept(tk::kw_mut);
|
||||
val operand := this.pratt(parser::bp::prefix, structs);
|
||||
return this.make_node(nk::reference, token, operand, parser::bit(mutable, nf::mutable));
|
||||
}
|
||||
else => return this.primary(structs);
|
||||
}
|
||||
|
||||
return this.primary(structs);
|
||||
}
|
||||
|
||||
#! The operator at the current token applied to `left`, which binds as tightly
|
||||
#! as `power`. What follows an operator grouping to the right is parsed under
|
||||
#! the power just below its own, so another of it continues there.
|
||||
#! @param this The parser.
|
||||
#! @param left The operand before the operator.
|
||||
#! @param power How tightly the operator binds.
|
||||
#! @param structs Whether a name followed by `{` is a struct literal.
|
||||
#! @returns The node of the operator applied.
|
||||
def parser::infix(&mut this, left: u32, power: parser::bp, structs: bool): u32 {
|
||||
val token := this.index;
|
||||
match power {
|
||||
parser::bp::assign => {
|
||||
this.advance();
|
||||
return this.make_node(nk::assign, token, left, this.pratt(parser::bp::none, structs));
|
||||
}
|
||||
parser::bp::fallback => return this.fallback(left, structs);
|
||||
parser::bp::ternary => return this.ternary(left, structs);
|
||||
parser::bp::cast => return this.check(tk::kw_as) ? this.cast_to(left) : this.is_test(left);
|
||||
parser::bp::postfix => return this.postfix(left);
|
||||
parser::bp::range => {
|
||||
this.advance();
|
||||
return this.make_node(nk::range, token, left, this.pratt(power, structs));
|
||||
}
|
||||
else => {
|
||||
this.advance();
|
||||
return this.make_node(nk::binary, token, left, this.pratt(power, structs));
|
||||
}
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
#! `value else fallback`, which takes what a result or a maybe holds, or else
|
||||
#! what stands in for it: `else (e)` names the error, and a block standing in
|
||||
#! must leave, where an expression gives the value instead.
|
||||
#! @param this The parser.
|
||||
#! @param value The node of the value before `else`.
|
||||
#! @param structs Whether a name followed by `{` is a struct literal.
|
||||
#! @returns The `fallback` node.
|
||||
def parser::fallback(&mut this, value: u32, structs: bool): u32 {
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
|
||||
# `(e)` names the error only before the block standing in, so `else (x)`
|
||||
# anywhere else is the value `x`.
|
||||
var error: u32 = 0;
|
||||
if this.check(tk::lparen) && this.peek(1) == tk::name && this.peek(2) == tk::rparen && this.peek(3) == tk::lbrace {
|
||||
this.advance();
|
||||
error = this.name_node("Expected the name of the error.");
|
||||
this.advance();
|
||||
}
|
||||
|
||||
val other := this.check(tk::lbrace)
|
||||
? this.block()
|
||||
: this.pratt(parser::bp::assign, structs);
|
||||
return this.make_node(nk::fallback, token, value, this.make_record(error, other));
|
||||
}
|
||||
|
||||
#! `cond ? then : else`. The branch taken where the condition holds ends at
|
||||
#! `:`, so it may be a fallback, as `ok ? parse(text) else 0 : 1`.
|
||||
#! @param this The parser.
|
||||
#! @param condition The condition's node.
|
||||
#! @param structs Whether a name followed by `{` is a struct literal.
|
||||
#! @returns The `ternary` node.
|
||||
def parser::ternary(&mut this, condition: u32, structs: bool): u32 {
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
|
||||
val then := this.pratt(parser::bp::assign, structs);
|
||||
this.expect(tk::colon, "Expected `:` between the ternary branches.");
|
||||
val other := this.pratt(parser::bp::fallback, structs);
|
||||
return this.make_node(nk::ternary, token, condition, this.make_record(then, other));
|
||||
}
|
||||
|
||||
#! `x as T`.
|
||||
#! @param this The parser.
|
||||
#! @param value The node of the value converted.
|
||||
#! @returns The `cast` node.
|
||||
def parser::cast_to(&mut this, value: u32): u32 {
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
if this.check(tk::kw_not) {
|
||||
this.fail(ec::token,
|
||||
"`not` follows `is`, which it denies; `as` converts a value and has nothing to deny.");
|
||||
}
|
||||
|
||||
this.plain_type = true;
|
||||
val target := this.type_expr();
|
||||
return this.make_node(nk::cast, token, value, target);
|
||||
}
|
||||
|
||||
#! `x is T`, `x is not field(value)`, and `x is (d)`.
|
||||
#! @param this The parser.
|
||||
#! @param value The node of what is tested.
|
||||
#! @returns The `is_test` node.
|
||||
def parser::is_test(&mut this, value: u32): u32 {
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
|
||||
# `x is not T` denies the test, which saves writing `!(x is T)`.
|
||||
var flags := parser::bit(this.accept(tk::kw_not), nf::negated);
|
||||
|
||||
# `x is (d)` names the field of a union a declaration known while compiling
|
||||
# describes, as `x is field` would.
|
||||
if this.accept(tk::lparen) {
|
||||
val named := this.expression();
|
||||
this.expect(tk::rparen, "Expected `)` after the declaration naming the field.");
|
||||
flags |= nf::computed as u32;
|
||||
return this.make_node(nk::is_test, token, value, this.make_record(named, 0, flags));
|
||||
}
|
||||
|
||||
this.plain_type = true;
|
||||
val target := this.type_expr();
|
||||
|
||||
# `x is field(value)` names what the field holds, as an arm of a match does.
|
||||
var binding: u32 = 0;
|
||||
if this.accept(tk::lparen) {
|
||||
binding = this.binding();
|
||||
this.expect(tk::rparen, "Expected `)` after the name of the value.");
|
||||
}
|
||||
|
||||
return this.make_node(nk::is_test, token, value, this.make_record(target, binding, flags));
|
||||
}
|
||||
|
||||
#! The name given what a field holds, as `value` of `is field(value)`. `&name`
|
||||
#! and `&mut name` refer to the field where the union holds it, rather than
|
||||
#! naming a copy of what it holds.
|
||||
#! @param this The parser.
|
||||
#! @returns The `name` or `reference` node, or an `error` node.
|
||||
def parser::binding(&mut this): u32 {
|
||||
val detail := "Expected the name to give the value the field holds, as `is field(value)`.";
|
||||
return this.name_node(detail) if !this.check(tk::bitand);
|
||||
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
val mutable := this.accept(tk::kw_mut);
|
||||
return this.make_node(nk::reference, token, this.name_node(detail), parser::bit(mutable, nf::mutable));
|
||||
}
|
||||
|
||||
#! One postfix operator applied to `left`: a call, indexing or slicing, a
|
||||
#! member, type arguments, `++`, or `--`.
|
||||
#! @param this The parser.
|
||||
#! @param left The operand.
|
||||
#! @returns The node of the operator applied.
|
||||
def parser::postfix(&mut this, left: u32): u32 {
|
||||
val token := this.index;
|
||||
match this.kind {
|
||||
tk::lparen => {
|
||||
this.advance();
|
||||
val args := this.list(parser::item::argument, tk::rparen, "Expected `)` to close the arguments.");
|
||||
return this.make_node(nk::call, token, left, args);
|
||||
}
|
||||
tk::lbracket => return this.subscript(left);
|
||||
tk::dot => return this.member(left);
|
||||
tk::scope => {
|
||||
this.advance();
|
||||
return this.make_node(nk::type_args, token, left, this.type_args());
|
||||
}
|
||||
else => {
|
||||
this.advance();
|
||||
return this.make_node(nk::postfix, token, left);
|
||||
}
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
#! `xs[i]`, or a slice, `xs[a:n]`, either of whose bounds may be left out.
|
||||
#! @param this The parser.
|
||||
#! @param object The node of what is indexed.
|
||||
#! @returns The `index` or `slice` node.
|
||||
def parser::subscript(&mut this, object: u32): u32 {
|
||||
val token := this.index;
|
||||
val angled := this.angled;
|
||||
this.angled = false;
|
||||
this.advance();
|
||||
|
||||
val start := this.check(tk::colon) ? 0 : this.expression();
|
||||
if !this.accept(tk::colon) {
|
||||
this.expect(tk::rbracket, "Expected `]` to close the index.");
|
||||
this.angled = angled;
|
||||
return this.make_node(nk::index, token, object, start);
|
||||
}
|
||||
|
||||
val count := this.check(tk::rbracket) ? 0 : this.expression();
|
||||
this.expect(tk::rbracket, "Expected `]` to close the slice.");
|
||||
this.angled = angled;
|
||||
return this.make_node(nk::slice, token, object, this.make_record(start, count));
|
||||
}
|
||||
|
||||
#! `x.name`, `x.name::<T>`, or `x.(d)`, which names the member a declaration
|
||||
#! known while compiling describes.
|
||||
#! @param this The parser.
|
||||
#! @param object The node of the value.
|
||||
#! @returns The `member` or `member_of` node.
|
||||
def parser::member(&mut this, object: u32): u32 {
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
if this.accept(tk::lparen) {
|
||||
val named := this.expression();
|
||||
this.expect(tk::rparen, "Expected `)` after the declaration naming the member.");
|
||||
return this.make_node(nk::member_of, token, object, named);
|
||||
}
|
||||
|
||||
if !this.check(tk::name) {
|
||||
this.fail(ec::ident, "Expected a member name after `.`.");
|
||||
return object;
|
||||
}
|
||||
|
||||
val name := this.index;
|
||||
this.advance();
|
||||
var types: u32 = 0;
|
||||
if this.at_value_args() {
|
||||
this.advance();
|
||||
types = this.type_args();
|
||||
}
|
||||
|
||||
return this.make_node(nk::member, name, object, types);
|
||||
}
|
||||
|
||||
#! An argument of a call. `...xs` spreads a view or a pack into the arguments,
|
||||
#! and is only an argument.
|
||||
#! @param this The parser.
|
||||
#! @returns The argument's node.
|
||||
def parser::argument(&mut this): u32 {
|
||||
return this.expression() if !this.check(tk::spread);
|
||||
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
return this.make_node(nk::unary, token, this.expression());
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
#! @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."));
|
||||
}
|
||||
@@ -0,0 +1,637 @@
|
||||
#! @file helpers.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains what the rest of the parser is made from: moving through the tokens,
|
||||
#! making nodes, records, and runs, starting declarations, making notes, lists,
|
||||
#! and recovering from what is wrong to parse what follows.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
# Tokens
|
||||
|
||||
#! Reads the kind of the current token into `kind`, which is `eof` at the limit.
|
||||
#! @param this The parser.
|
||||
def parser::sync(&mut this): void {
|
||||
this.kind = this.index < this.limit
|
||||
? this.syntax.token_kinds[this.index]
|
||||
: tk::eof;
|
||||
}
|
||||
|
||||
#! The kind of the token `ahead` past the current one, or `eof` past the limit.
|
||||
#! @param this The parser.
|
||||
#! @param ahead How many tokens past the current one, where 0 is the current
|
||||
#! one.
|
||||
#! @returns The token's kind.
|
||||
def parser::peek(&this, ahead: u32): tk {
|
||||
val at := this.index + ahead;
|
||||
return at < this.limit
|
||||
? this.syntax.token_kinds[at]
|
||||
: tk::eof;
|
||||
}
|
||||
|
||||
#! Moves to the next token, staying on the `eof` that ends them.
|
||||
#!
|
||||
#! A `>>`, `>=`, or `>>=` is followed by a token at each `>` and `=` after its
|
||||
#! first, which `close_angle` takes one at a time, and which an operator is
|
||||
#! moved past with, as one token for each byte it spans.
|
||||
#! @param this The parser.
|
||||
def parser::advance(&mut this): void {
|
||||
return if this.kind == tk::eof;
|
||||
if this.kind.has(tt::angle) {
|
||||
this.index += this.kind.width;
|
||||
} else {
|
||||
this.index++;
|
||||
}
|
||||
this.sync();
|
||||
}
|
||||
|
||||
def parser::check(&this, kind: tk): bool {
|
||||
return this.kind == kind;
|
||||
}
|
||||
|
||||
#! Whether the current token is the name of the ID, as `names::this_id`.
|
||||
#! @param this The parser.
|
||||
#! @param word The ID of the name.
|
||||
#! @returns Whether the current token is that name.
|
||||
def parser::check_word(&this, word: u32): bool {
|
||||
return this.kind == tk::name && this.syntax.token_names[this.index] == word;
|
||||
}
|
||||
|
||||
#! Whether the current token's kind has the trait.
|
||||
#! @param this The parser.
|
||||
#! @param which The trait to ask of the token's kind.
|
||||
#! @returns Whether its kind has it.
|
||||
def parser::at(&this, which: tt): bool {
|
||||
return this.kind.has(which);
|
||||
}
|
||||
|
||||
#! Moves past the current token when it is of the kind.
|
||||
#! @param this The parser.
|
||||
#! @param kind The kind of token to move past.
|
||||
#! @returns Whether it was, and so was moved past.
|
||||
def parser::accept(&mut this, kind: tk): bool {
|
||||
return false if this.kind != kind;
|
||||
this.advance();
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Moves past the current token when it is of the kind, or else reports the
|
||||
#! detail and panics.
|
||||
#! @param this The parser.
|
||||
#! @param kind The kind of token wanted.
|
||||
#! @param detail What the note says where it is not, as `Expected ";" after
|
||||
#! the statement.`.
|
||||
#! @returns Whether it was of the kind.
|
||||
def parser::expect(&mut this, kind: tk, detail: strview): bool {
|
||||
return true if this.accept(kind);
|
||||
this.fail(ec::token, detail);
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Moves past the `>` closing type arguments or parameters, which may be the
|
||||
#! first of `>>`, `>=`, or `>>=`. The lexer made a token of what is left of
|
||||
#! those, which becomes the current token: `>`, `=`, or `>=`.
|
||||
#! @param this The parser.
|
||||
#! @param detail What the note says where no `>` is there.
|
||||
#! @returns Whether a `>` was moved past.
|
||||
def parser::close_angle(&mut this, detail: strview): bool {
|
||||
if !this.at(tt::angle) {
|
||||
this.fail(ec::token, detail);
|
||||
return false;
|
||||
}
|
||||
|
||||
this.index++;
|
||||
this.sync();
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Whether the type whose `<` is at the token follows `as` or `is`, where a `<`
|
||||
#! always begins type arguments, so what looks like a comparison is not one.
|
||||
#! @param this The parser.
|
||||
#! @param at The token of the `<`.
|
||||
#! @returns Whether `as` or `is` comes before the type.
|
||||
def parser::follows_cast(&this, at: u32): bool {
|
||||
var back := at;
|
||||
while back > 0 {
|
||||
back--;
|
||||
val kind := this.syntax.token_kinds[back];
|
||||
return kind == tk::kw_as || kind == tk::kw_is if !kind.has(tt::piece);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
# Nodes
|
||||
|
||||
#! Appends a node, whose last token is the last the parser moved past, or its
|
||||
#! own where it moved past none since.
|
||||
#! @param this The parser.
|
||||
#! @param kind The node's kind.
|
||||
#! @param token Its token.
|
||||
#! @param left Its first slot, which its kind's shape says the meaning of.
|
||||
#! @param right Its second slot.
|
||||
#! @returns The node.
|
||||
def parser::make_node(
|
||||
&mut this,
|
||||
kind: nk,
|
||||
token: u32,
|
||||
left: u32 = 0,
|
||||
right: u32 = 0
|
||||
): u32 {
|
||||
val end := this.index > token ? this.index - 1 : token;
|
||||
return this.syntax.add_node(kind, token, left, right, end);
|
||||
}
|
||||
|
||||
#! An `error` node at the token, standing where the parser made no sense of the
|
||||
#! source, holding the run of what it did parse there, so that tools still find
|
||||
#! what is within it.
|
||||
#! @param this The parser.
|
||||
#! @param token The token it stands at.
|
||||
#! @param held The run of what was parsed there, or 0.
|
||||
#! @returns The node.
|
||||
def parser::error(&mut this, token: u32, held: u32 = 0): u32 {
|
||||
return this.make_node(nk::error, token, held);
|
||||
}
|
||||
|
||||
#! An `error` node at the current token, holding nothing.
|
||||
#! @param this The parser.
|
||||
#! @returns The node.
|
||||
def parser::error_here(&mut this): u32 {
|
||||
return this.error(this.index);
|
||||
}
|
||||
|
||||
#! A `when_block`, `if_stmt`, `if_const`, or `ternary`: a condition, and what is
|
||||
#! taken where it holds and where it does not.
|
||||
#! @param this The parser.
|
||||
#! @param kind The node's kind.
|
||||
#! @param token Its token.
|
||||
#! @param condition The condition.
|
||||
#! @param then What is taken where it holds.
|
||||
#! @param other What is taken where it does not, or 0.
|
||||
#! @returns The node.
|
||||
def parser::branch(&mut this, kind: nk, token: u32, condition: u32, then: u32, other: u32): u32 {
|
||||
return this.make_node(kind, token, condition, this.make_record(then, other));
|
||||
}
|
||||
|
||||
#! A `name` node of the current token, which is moved past.
|
||||
#! @param this The parser.
|
||||
#! @param detail What the note says where the current token is no name.
|
||||
#! @returns The node, or an `error` node where the token is no name.
|
||||
def parser::name_node(&mut this, detail: strview): u32 {
|
||||
if !this.check(tk::name) {
|
||||
this.fail(ec::ident, detail);
|
||||
return this.error_here();
|
||||
}
|
||||
|
||||
this.advance();
|
||||
return this.make_node(nk::name, this.index - 1);
|
||||
}
|
||||
|
||||
#! Writes a record of a node's slots to `extras`, unless every slot is 0, where
|
||||
#! the zeros `extras` begins with stand for it.
|
||||
#! @param this The parser.
|
||||
#! @param slots The node's slots, in the order its shape lists them.
|
||||
#! @returns Where the record begins, for the slot of the node holding it.
|
||||
def parser::make_record(&mut this, slots: u32...): u32 {
|
||||
var empty := true;
|
||||
for slot in slots {
|
||||
empty = empty && slot == 0;
|
||||
}
|
||||
return 0 if empty;
|
||||
|
||||
val at := this.syntax.extras.length as u32;
|
||||
this.syntax.extras.extend(slots);
|
||||
return at;
|
||||
}
|
||||
|
||||
#! Where the nodes of a run begin on the scratchpad, for `make_run`.
|
||||
#! @param this The parser.
|
||||
#! @returns The scratchpad's length.
|
||||
val parser::mark: u64 { get => this.scratch.length; }
|
||||
|
||||
#! Holds a node of a run being parsed on the scratchpad.
|
||||
#! @param this The parser.
|
||||
#! @param node The node.
|
||||
def parser::push(&mut this, node: u32): void {
|
||||
this.scratch.push(node);
|
||||
}
|
||||
|
||||
#! Moves the nodes held on the scratchpad since the mark to a run in `extras`.
|
||||
#! @param this The parser.
|
||||
#! @param mark Where the nodes begin, as `mark` gave it.
|
||||
#! @returns Where the run begins, or 0 for a run of no nodes.
|
||||
def parser::make_run(&mut this, mark: u64): u32 {
|
||||
val count := this.scratch.length - mark;
|
||||
return 0 if count == 0;
|
||||
|
||||
val at := this.syntax.extras.length as u32;
|
||||
this.syntax.extras.append(count as u32);
|
||||
this.syntax.extras.extend(this.scratch[mark:count]);
|
||||
|
||||
this.drop(mark);
|
||||
return at;
|
||||
}
|
||||
|
||||
#! A run of the nodes given, leaving out those that are absent.
|
||||
#! @param this The parser.
|
||||
#! @param nodes The nodes, of which 0 is left out.
|
||||
#! @returns Where the run begins, or 0 where it holds none.
|
||||
def parser::run_of(&mut this, nodes: u32...): u32 {
|
||||
val mark := this.mark;
|
||||
for node in nodes {
|
||||
if node != 0 {
|
||||
this.push(node);
|
||||
}
|
||||
}
|
||||
return this.make_run(mark);
|
||||
}
|
||||
|
||||
#! Lets go of the nodes held on the scratchpad since the mark.
|
||||
#! @param this The parser.
|
||||
#! @param mark Where the nodes begin, as `mark` gave it.
|
||||
def parser::drop(&mut this, mark: u64): void {
|
||||
this.scratch.resize(mark);
|
||||
}
|
||||
|
||||
#! A run of the nodes of `first` followed by those of `second`.
|
||||
#! @param this The parser.
|
||||
#! @param first The run whose nodes come first.
|
||||
#! @param second The run whose nodes come after.
|
||||
#! @returns The joined run, or 0 where both are empty.
|
||||
def parser::join_runs(&mut this, first: u32, second: u32): u32 {
|
||||
return second if first == 0;
|
||||
return first if second == 0;
|
||||
|
||||
val mark := this.mark;
|
||||
for &node in this.run_view(first) { this.push(node); }
|
||||
for &node in this.run_view(second) { this.push(node); }
|
||||
return this.make_run(mark);
|
||||
}
|
||||
|
||||
#! How many nodes the run holds.
|
||||
#! @param this The parser.
|
||||
#! @param run Where the run begins.
|
||||
#! @returns How many nodes it holds.
|
||||
def parser::run_length(&this, run: u32): u32 {
|
||||
return this.syntax.run_length(run);
|
||||
}
|
||||
|
||||
#! The node at the index of the run.
|
||||
#! @param this The parser.
|
||||
#! @param run Where the run begins.
|
||||
#! @param index The position, below its length.
|
||||
#! @returns The node.
|
||||
def parser::run_at(&this, run: u32, index: u32): u32 {
|
||||
return this.syntax.run_at(run, index);
|
||||
}
|
||||
|
||||
#! The nodes of the run, where they are.
|
||||
#! @param this The parser.
|
||||
#! @param run Where the run begins.
|
||||
#! @returns The nodes, a view only good until another run or record is made.
|
||||
def parser::run_view(&this, run: u32): span<u32> {
|
||||
return this.syntax.run_view(run);
|
||||
}
|
||||
|
||||
#! The ID of the name the node's token is.
|
||||
#! @param this The parser.
|
||||
#! @param node The node.
|
||||
#! @returns The name's ID.
|
||||
def parser::name_of(&this, node: u32): u32 {
|
||||
return this.syntax.token_names[this.syntax.node_tokens[node]];
|
||||
}
|
||||
|
||||
#! The bit of a flag when it is on, to be or'd with others into a mask, as a
|
||||
#! `flags` slot.
|
||||
#! @tparam T The enum of flags.
|
||||
#! @param on Whether the flag is on.
|
||||
#! @param flag The flag.
|
||||
#! @returns The flag's bit, or 0 where it is off.
|
||||
def parser::bit<T>(on: bool, flag: T): u32 where T: ordinal {
|
||||
return on ? flag as u32 : 0;
|
||||
}
|
||||
|
||||
|
||||
# Declarations
|
||||
|
||||
#! What comes before a declaration: its attributes, `pub`, and the token it
|
||||
#! begins at, which its documentation is written before.
|
||||
struct parser::lead {
|
||||
var attrs: u32; #! The run of its attributes.
|
||||
var exported: bool; #! `pub`.
|
||||
var start: u32; #! The token it begins at.
|
||||
}
|
||||
|
||||
#! Where a declaration is written, which says what it may be.
|
||||
enum parser::place {
|
||||
top, #! At the top level of a file, or within a `when` there.
|
||||
local, #! Within a function.
|
||||
field, #! Within a struct or union.
|
||||
member, #! Within a trait.
|
||||
}
|
||||
|
||||
#! The lead of a declaration beginning at the current token, with nothing before
|
||||
#! it.
|
||||
#! @param this The parser.
|
||||
#! @returns The lead.
|
||||
def parser::bare_lead(&this): parser::lead {
|
||||
return parser::lead { start = this.index };
|
||||
}
|
||||
|
||||
#! Records where a declaration began, which says what documents it.
|
||||
#! @param this The parser.
|
||||
#! @param made The declaration's node.
|
||||
#! @param lead What came before it: where it began, and its documentation.
|
||||
#! @returns The declaration.
|
||||
def parser::declared_at(&mut this, made: u32, lead: &parser::lead): u32 {
|
||||
if made != 0 && this.syntax.node_kinds[made] != nk::error {
|
||||
this.syntax.starts.set(made as u64, lead.start);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
|
||||
# Notes
|
||||
|
||||
#! Reports an error at the current token, then panics, which silences notes
|
||||
#! until the parser resynchronizes so that one mistake is one note.
|
||||
#! @param this The parser.
|
||||
#! @param code What kind of error it is.
|
||||
#! @param detail What the note says.
|
||||
def parser::fail(&mut this, code: ec, detail: strview): void {
|
||||
this.fail_at(this.index, code, detail);
|
||||
}
|
||||
|
||||
def parser::fail_at(&mut this, token: u32, code: ec, detail: strview): void {
|
||||
# Failing at a limit short of the end of the tokens says a reparse cut what it
|
||||
# parsed short, which a whole parse would have read on past.
|
||||
if this.kind == tk::eof && this.limit + 1 < this.syntax.token_count as u32 {
|
||||
this.cut = true;
|
||||
}
|
||||
|
||||
return if this.panic;
|
||||
this.report_at(token, code, detail);
|
||||
this.panic = true;
|
||||
}
|
||||
|
||||
#! Reports an error at the current token without panicking, for mistakes the
|
||||
#! parser reads past knowing what was meant.
|
||||
#! @param this The parser.
|
||||
#! @param code What kind of error it is.
|
||||
#! @param detail What the note says.
|
||||
def parser::report(&mut this, code: ec, detail: strview): void {
|
||||
this.report_at(this.index, code, detail);
|
||||
}
|
||||
|
||||
#! Reports an error at the token. A note saying what was expected says what was
|
||||
#! found instead, the token it is at, which `note::message` spells.
|
||||
#! @param this The parser.
|
||||
#! @param token The token the error is at.
|
||||
#! @param code What kind of error it is.
|
||||
#! @param detail What the note says.
|
||||
def parser::report_at(&mut this, token: u32, code: ec, detail: strview): void {
|
||||
return if this.panic || this.halted || !this.sink.admits();
|
||||
var made := note {
|
||||
file = this.file,
|
||||
offset = this.syntax.token_offsets[token],
|
||||
length = this.syntax.token_length(token, this.names),
|
||||
code = code,
|
||||
detail = string::from(detail, nothing {}),
|
||||
found = detail.starts_with("Expected")
|
||||
};
|
||||
this.sink.give(&mut made);
|
||||
}
|
||||
|
||||
|
||||
# Nesting
|
||||
|
||||
#! Goes one level deeper, unless that is past `max_depth`, where the rest of the
|
||||
#! declaration is given up on: the tokens seem to end where it does, so that
|
||||
#! everything parsing it returns, and the top level resumes after it.
|
||||
#! @param this The parser.
|
||||
#! @returns Whether the parser went deeper, and must `leave` again.
|
||||
def parser::enter(&mut this): bool {
|
||||
if this.depth < parser::max_depth {
|
||||
this.depth++;
|
||||
return true;
|
||||
}
|
||||
|
||||
if !this.halted {
|
||||
this.panic = false;
|
||||
this.fail(ec::syntax, "Nesting exceeds the maximum depth.");
|
||||
|
||||
val end := this.declaration_end();
|
||||
this.index = end;
|
||||
this.limit = end;
|
||||
this.halted = true;
|
||||
this.sync();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
def parser::leave(&mut this): void {
|
||||
this.depth--;
|
||||
}
|
||||
|
||||
#! Where the top-level declaration the parser is within ends, from where it
|
||||
#! began: past the `}` closing the braces it opens, or the `;` ending it outside
|
||||
#! any, whichever comes past the current token.
|
||||
#! @param this The parser.
|
||||
#! @returns The token after the declaration.
|
||||
def parser::declaration_end(&this): u32 {
|
||||
val last := this.syntax.token_count as u32 - 1;
|
||||
var braces: u32 = 0;
|
||||
for at in this.begun..last {
|
||||
val kind := this.syntax.token_kinds[at];
|
||||
if kind == tk::lbrace {
|
||||
braces++;
|
||||
} else if kind == tk::rbrace {
|
||||
return at + 1 if braces <= 1 && at >= this.index;
|
||||
braces = braces > 0 ? braces - 1 : 0;
|
||||
} else if kind == tk::semicolon && braces == 0 && at >= this.index {
|
||||
return at + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return last;
|
||||
}
|
||||
|
||||
#! Takes up the tokens after a declaration given up on, through the limit.
|
||||
#! @param this The parser.
|
||||
#! @param limit The last token to take up.
|
||||
def parser::resume(&mut this, limit: u32): void {
|
||||
this.limit = limit;
|
||||
this.halted = false;
|
||||
this.panic = false;
|
||||
this.sync();
|
||||
}
|
||||
|
||||
|
||||
# Recovery
|
||||
|
||||
#! Where the parser resumes after an error, which decides the tokens it stops
|
||||
#! at.
|
||||
enum parser::scope {
|
||||
document, #! The top level of a file.
|
||||
declarations, #! Within a `when` at the top level.
|
||||
block, #! Statements.
|
||||
fields, #! The members of a struct or union.
|
||||
members, #! The members of a trait.
|
||||
}
|
||||
|
||||
#! Whether the current token begins the next item of the scope.
|
||||
#! @param this The parser.
|
||||
#! @param scope What the parser is within.
|
||||
#! @returns Whether the token begins an item of it.
|
||||
def parser::resumes(&this, scope: parser::scope): bool {
|
||||
return match scope {
|
||||
parser::scope::document => this.at(tt::declares) || this.at(tt::imports),
|
||||
parser::scope::declarations => this.at(tt::declares),
|
||||
parser::scope::block => this.at(tt::states),
|
||||
parser::scope::fields => this.at(tt::fields),
|
||||
parser::scope::members => this.at(tt::members),
|
||||
else => false
|
||||
};
|
||||
}
|
||||
|
||||
#! Skips tokens until one that can begin the next item of the scope, stepping
|
||||
#! over balanced braces, then stops panicking. Every token it stops on is
|
||||
#! consumed by the item parser, or ends the enclosing loop, so parsing always
|
||||
#! makes progress.
|
||||
#! @param this The parser.
|
||||
#! @param scope What the parser is within.
|
||||
def parser::synchronize(&mut this, scope: parser::scope): void {
|
||||
var braces: u32 = 0;
|
||||
while !this.check(tk::eof) {
|
||||
if braces == 0 {
|
||||
break if this.resumes(scope);
|
||||
if this.check(tk::semicolon) {
|
||||
this.advance();
|
||||
break;
|
||||
}
|
||||
|
||||
if this.check(tk::rbrace) {
|
||||
if scope == parser::scope::document {
|
||||
this.advance();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if this.check(tk::lbrace) {
|
||||
braces++;
|
||||
} else if this.check(tk::rbrace) {
|
||||
braces--;
|
||||
if braces == 0 {
|
||||
this.advance();
|
||||
break;
|
||||
}
|
||||
}
|
||||
this.advance();
|
||||
}
|
||||
|
||||
this.panic = false;
|
||||
}
|
||||
|
||||
#! Skips to the `}` closing the braces the parser is within, without moving
|
||||
#! past it, then stops panicking.
|
||||
#! @param this The parser.
|
||||
def parser::skip_to_close(&mut this): void {
|
||||
var braces: u32 = 0;
|
||||
while !this.check(tk::eof) {
|
||||
if this.check(tk::lbrace) {
|
||||
braces++;
|
||||
} else if this.check(tk::rbrace) {
|
||||
break if braces == 0;
|
||||
braces--;
|
||||
}
|
||||
this.advance();
|
||||
}
|
||||
|
||||
this.panic = false;
|
||||
}
|
||||
|
||||
|
||||
# Lists
|
||||
|
||||
#! What a list holds, which `list_item` parses one of.
|
||||
enum parser::item {
|
||||
argument, #! Of a call.
|
||||
element, #! Of an array.
|
||||
field, #! Of a struct literal or an attribute.
|
||||
param_type, #! Of a function type.
|
||||
rename, #! Of a spread field.
|
||||
enumerator, #! Of an enum.
|
||||
value_arm, #! Of a match used as a value.
|
||||
const_arm, #! Of a `match const` used as a value, whose patterns are traits or types.
|
||||
type_arm, #! Of a `match const` that is a type, whose bodies are types too.
|
||||
}
|
||||
|
||||
def parser::list_item(&mut this, kind: parser::item): u32 {
|
||||
return match kind {
|
||||
parser::item::argument => this.argument(),
|
||||
parser::item::element => this.expression(),
|
||||
parser::item::field => this.field_init(),
|
||||
parser::item::param_type => this.spread_type(),
|
||||
parser::item::rename => this.rename(),
|
||||
parser::item::enumerator => this.enumerator(),
|
||||
parser::item::value_arm => this.arm(true),
|
||||
parser::item::const_arm => this.arm(true, true),
|
||||
parser::item::type_arm => this.arm(true, true, true),
|
||||
else => this.error_here()
|
||||
};
|
||||
}
|
||||
|
||||
#! The items of a list separated by commas, which may end with one, through the
|
||||
#! token closing it. An item that fails is skipped to the comma or closing token
|
||||
#! after it, so one mistake leaves the rest of the list parsed.
|
||||
#! @param this The parser.
|
||||
#! @param kind What the items are.
|
||||
#! @param close The kind of token closing the list.
|
||||
#! @param detail What the note says where the closing token is missing.
|
||||
#! @returns The run of items.
|
||||
def parser::list(&mut this, kind: parser::item, close: tk, detail: strview): u32 {
|
||||
val angled := this.angled;
|
||||
this.angled = false;
|
||||
val mark := this.mark;
|
||||
while !this.check(close) && !this.check(tk::eof) {
|
||||
this.push(this.list_item(kind));
|
||||
break if this.panic && !this.skip_item(close);
|
||||
break if !this.accept(tk::comma);
|
||||
}
|
||||
|
||||
val items := this.make_run(mark);
|
||||
this.expect(close, detail);
|
||||
this.angled = angled;
|
||||
return items;
|
||||
}
|
||||
|
||||
#! Skips what is left of an item that failed, stepping over what is nested in
|
||||
#! it, to the comma or closing token after it, then stops panicking. It gives up
|
||||
#! at a `;`, or at a closing token of something outside the list, which the
|
||||
#! enclosing parser recovers at instead.
|
||||
#! @param this The parser.
|
||||
#! @param close The kind of token closing the list.
|
||||
#! @returns Whether the list goes on.
|
||||
def parser::skip_item(&mut this, close: tk): bool {
|
||||
var depth: u32 = 0;
|
||||
while !this.check(tk::eof) {
|
||||
if depth == 0 && (this.check(tk::comma) || this.check(close)) {
|
||||
this.panic = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
val closes := this.at(tt::closes);
|
||||
return false if depth == 0 && (closes || this.check(tk::semicolon) || this.check(tk::istring_mid));
|
||||
|
||||
if this.at(tt::opens) { depth++; }
|
||||
if closes { depth--; }
|
||||
this.advance();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
#! @file primary.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the parsing of primaries, which operators are applied to: literals,
|
||||
#! interpolated strings, names and paths with their struct literals, arrays,
|
||||
#! lambdas, `type_of`, and expressions in parentheses.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! What an expression is built from: a literal, a name or path, a struct
|
||||
#! literal, an expression in parentheses, a lambda, an array, a match used as a
|
||||
#! value, or `type_of(d)`, a type named to test it with `is`.
|
||||
#! @param this The parser.
|
||||
#! @param structs Whether a name followed by `{` is a struct literal.
|
||||
#! @returns The node, or an `error` node.
|
||||
def parser::primary(&mut this, structs: bool): u32 {
|
||||
# `type_of(d)` is a type, which an expression names only to test it, as
|
||||
# `type_of(d) is copyable`.
|
||||
if this.check_word(names::type_of_id) && this.peek(1) == tk::lparen {
|
||||
return this.type_primary();
|
||||
}
|
||||
# Within an `ensures`, `old(e)` is what `e` was where it was reached.
|
||||
if this.in_ensures && this.check_word(names::old_id) && this.peek(1) == tk::lparen {
|
||||
return this.old_value();
|
||||
}
|
||||
|
||||
val token := this.index;
|
||||
match this.kind {
|
||||
tk::signed, tk::unsigned, tk::float => return this.literal(nk::number);
|
||||
tk::char => return this.literal(nk::character);
|
||||
tk::string => return this.literal(nk::string);
|
||||
tk::kw_true, tk::kw_false => return this.literal(nk::boolean);
|
||||
tk::kw_null => return this.literal(nk::null_lit);
|
||||
tk::istring, tk::istring_beg => return this.istring();
|
||||
tk::kw_match => return this.match_expr(true);
|
||||
tk::kw_def => return this.lambda();
|
||||
tk::lbracket => return this.array();
|
||||
tk::name => return this.named(structs);
|
||||
|
||||
# What is between parentheses may be a struct literal.
|
||||
tk::lparen => {
|
||||
val angled := this.angled;
|
||||
this.angled = false;
|
||||
this.advance();
|
||||
val inner := this.expression();
|
||||
this.expect(tk::rparen, "Expected `)` to close the expression.");
|
||||
this.angled = angled;
|
||||
return inner;
|
||||
}
|
||||
|
||||
# The lexer reported what is wrong with a malformed literal, so it stands as
|
||||
# an error with nothing more said.
|
||||
tk::bad_number, tk::bad_quote => {}
|
||||
else => {
|
||||
this.fail(ec::expr, "Expected an expression.");
|
||||
return this.error_here();
|
||||
}
|
||||
}
|
||||
|
||||
this.advance();
|
||||
return this.error(token);
|
||||
}
|
||||
|
||||
#! `old(e)` within an `ensures`: what `e` was where the `ensures` was reached.
|
||||
#! @param this The parser.
|
||||
#! @returns The `old_value` node.
|
||||
def parser::old_value(&mut this): u32 {
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
this.advance();
|
||||
val inner := this.expression();
|
||||
this.expect(tk::rparen, "Expected `)` to close `old`, which takes one expression.");
|
||||
return this.make_node(nk::old_value, token, inner);
|
||||
}
|
||||
|
||||
#! A literal of the kind, which its token alone describes.
|
||||
#! @param this The parser.
|
||||
#! @param kind The node's kind.
|
||||
#! @returns The node.
|
||||
def parser::literal(&mut this, kind: nk): u32 {
|
||||
this.advance();
|
||||
return this.make_node(kind, this.index - 1);
|
||||
}
|
||||
|
||||
#! An interpolated string, whose run holds its text as `string` nodes, the token
|
||||
#! of each a piece of it, between which come the expressions of its holes: the
|
||||
#! `istring_beg` before the first hole, an `istring_mid` between two holes, and
|
||||
#! the `istring_end` after the last. One with no holes is one `istring` piece.
|
||||
#! @param this The parser.
|
||||
#! @returns The `istring` node.
|
||||
def parser::istring(&mut this): u32 {
|
||||
val token := this.index;
|
||||
val mark := this.mark;
|
||||
this.push(this.literal(nk::string));
|
||||
val angled := this.angled;
|
||||
this.angled = false;
|
||||
if this.syntax.token_kinds[token] == tk::istring_beg {
|
||||
while true {
|
||||
this.push(this.expression());
|
||||
if this.check(tk::istring_mid) {
|
||||
this.push(this.literal(nk::string));
|
||||
continue;
|
||||
}
|
||||
|
||||
if this.check(tk::istring_end) {
|
||||
this.push(this.literal(nk::string));
|
||||
break;
|
||||
}
|
||||
|
||||
this.fail(ec::token, "Expected `}` after the interpolated expression.");
|
||||
this.skip_hole();
|
||||
break;
|
||||
}
|
||||
}
|
||||
this.angled = angled;
|
||||
|
||||
return this.make_node(nk::istring, token, this.make_run(mark));
|
||||
}
|
||||
|
||||
#! Skips past the end of the interpolated string whose hole the parser is
|
||||
#! within, stepping over the strings within it.
|
||||
#! @param this The parser.
|
||||
def parser::skip_hole(&mut this): void {
|
||||
var depth: u32 = 0;
|
||||
while !this.check(tk::eof) {
|
||||
if this.check(tk::istring_beg) {
|
||||
depth++;
|
||||
} else if this.check(tk::istring_end) {
|
||||
if depth == 0 {
|
||||
this.advance();
|
||||
break;
|
||||
}
|
||||
depth--;
|
||||
}
|
||||
this.advance();
|
||||
}
|
||||
}
|
||||
|
||||
#! A name, a path, as `math::max::<u8>`, or a struct literal of either, where
|
||||
#! they are allowed.
|
||||
#! @param this The parser.
|
||||
#! @param structs Whether a struct literal may follow.
|
||||
#! @returns The `name`, `path`, or `construct` node.
|
||||
def parser::named(&mut this, structs: bool): u32 {
|
||||
val token := this.index;
|
||||
var made: u32 = 0;
|
||||
if this.peek(1) == tk::scope {
|
||||
made = this.make_node(nk::path, token, this.segments(parser::args::values));
|
||||
} else {
|
||||
this.advance();
|
||||
made = this.make_node(nk::name, token);
|
||||
}
|
||||
|
||||
return made if !structs || !this.check(tk::lbrace);
|
||||
|
||||
# The literal's type is the name read as a type: a path's segments are a type
|
||||
# name's, and a name is a type name of one segment, which holds no run.
|
||||
this.syntax.node_kinds[made] = nk::type_name;
|
||||
|
||||
val brace := this.index;
|
||||
return this.make_node(nk::construct, brace, made, this.field_list());
|
||||
}
|
||||
|
||||
#! `[a, b, c]`.
|
||||
#! @param this The parser.
|
||||
#! @returns The `array` node.
|
||||
def parser::array(&mut this): u32 {
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
|
||||
val elements := this.list(parser::item::element, tk::rbracket, "Expected `]` to close the array.");
|
||||
return this.make_node(nk::array, token, elements);
|
||||
}
|
||||
|
||||
#! `def(params): result { body }`, or `def(params) => value`, which returns the
|
||||
#! value, or evaluates it for `void`. Its parameter types and result may be left
|
||||
#! out.
|
||||
#! @param this The parser.
|
||||
#! @returns The `lambda` node.
|
||||
def parser::lambda(&mut this): u32 {
|
||||
val token := this.index;
|
||||
val angled := this.angled;
|
||||
this.angled = false;
|
||||
# A lambda's body is checked where it is called, which no `old` reaches back
|
||||
# from.
|
||||
val ensures := this.in_ensures;
|
||||
this.in_ensures = false;
|
||||
this.advance();
|
||||
|
||||
val signature := this.params(true, false);
|
||||
val body := this.accept(tk::arrow)
|
||||
? this.expression()
|
||||
: this.block();
|
||||
this.angled = angled;
|
||||
this.in_ensures = ensures;
|
||||
return this.make_node(nk::lambda, token, signature.params, this.make_record(signature.result, body));
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
#! @file reparse.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains `syntax::reparse`, which parses only the top-level declarations an
|
||||
#! edit touched, and keeps the trees of the rest.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! Reparses a file after an edit, keeping the trees of the top-level
|
||||
#! declarations the edit did not touch, and parsing again only those it did.
|
||||
#!
|
||||
#! The edited text is lexed whole into `fresh`, which is cheap, and its tokens
|
||||
#! are compared with the old ones from each end to find what changed. The
|
||||
#! declarations around the change are parsed again from the fresh tokens, and
|
||||
#! those after it keep their nodes, whose tokens move by how many the edit added
|
||||
#! or took away. Where a declaration parsed again runs into where the change
|
||||
#! ends, as one that lost its closing `}` does, the file is parsed whole
|
||||
#! instead, so the tree is always the one a whole parse makes.
|
||||
#!
|
||||
#! The nodes of the declarations parsed again are left behind, unreachable. Once
|
||||
#! there are as many nodes or extras again as the last whole parse made, the
|
||||
#! file is parsed whole, which lets go of them, so a file edited for a long
|
||||
#! while does not keep growing. The indexes of every node change when it does.
|
||||
#! @param this The syntax tree.
|
||||
#! @param fresh The edited text, lexed: its tokens, documentation, and
|
||||
#! comments.
|
||||
#! @param names The names `fresh` was lexed with.
|
||||
#! @param document The `document` node of this syntax.
|
||||
#! @param notes The notes of this file, both the lexer's and the
|
||||
#! parser's.
|
||||
#! @param fresh_notes What the lexer noted of the edited text, which are moved
|
||||
#! into `notes`.
|
||||
#! @param file The index of the file.
|
||||
#! @returns The `document` node.
|
||||
pub def syntax::reparse(
|
||||
&mut this,
|
||||
fresh: &mut syntax,
|
||||
names: &names,
|
||||
document: u32,
|
||||
notes: &mut vector<note>,
|
||||
fresh_notes: &mut vector<note>,
|
||||
file: u32
|
||||
): u32 {
|
||||
val old_count := this.token_count as u32;
|
||||
val new_count := fresh.token_count as u32;
|
||||
val bytes := fresh.token_offsets[new_count - 1] as i64 - this.token_offsets[old_count - 1] as i64;
|
||||
val shift := new_count as i64 - old_count as i64;
|
||||
|
||||
# What is the same before the change, and after it, where it has moved.
|
||||
var same_before: u32 = 0;
|
||||
while same_before < old_count - 1 && same_before < new_count - 1
|
||||
&& this.same_token(fresh, same_before, same_before, 0) {
|
||||
same_before++;
|
||||
}
|
||||
|
||||
var same_after: u32 = 0;
|
||||
while same_after < old_count - 1 - same_before && same_after < new_count - 1 - same_before
|
||||
&& this.same_token(fresh, old_count - 2 - same_after, new_count - 2 - same_after, bytes) {
|
||||
same_after++;
|
||||
}
|
||||
|
||||
val changed_end := old_count - 1 - same_after;
|
||||
|
||||
# The declarations the change touches, from `first` through `last`, and the
|
||||
# tokens they and the change span.
|
||||
val decls: u32 = this.node_lefts[document];
|
||||
val count := this.run_length(decls);
|
||||
var first := count;
|
||||
for i in 0..count {
|
||||
if this.node_end(this.run_at(decls, i as u32)) >= same_before {
|
||||
first = i as u32;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var last := first;
|
||||
for i in first..count {
|
||||
break if this.start_of(this.run_at(decls, i as u32)) >= changed_end && i > first;
|
||||
last = i as u32;
|
||||
}
|
||||
|
||||
var from := same_before;
|
||||
var to := changed_end;
|
||||
if first < count {
|
||||
val start := this.start_of(this.run_at(decls, first));
|
||||
val end := this.node_end(this.run_at(decls, last)) + 1;
|
||||
from = start < from ? start : from;
|
||||
to = end > to ? end : to;
|
||||
}
|
||||
|
||||
# The notes of what is parsed again, and all of the lexer's, are made again.
|
||||
# They are put back in the order a whole parse makes them: the lexer's, then
|
||||
# the parser's along the text, those after the change moving with it.
|
||||
val from_byte := this.token_offsets[from];
|
||||
val to_byte := this.token_offsets[to < old_count - 1 ? to : old_count - 1];
|
||||
var before := vector::<note>::new();
|
||||
var after := vector::<note>::new();
|
||||
for &mut each in notes {
|
||||
continue if each.lexed;
|
||||
if each.offset < from_byte {
|
||||
before.append(move(each));
|
||||
continue;
|
||||
}
|
||||
|
||||
continue if each.offset < to_byte;
|
||||
each.offset = (each.offset as i64 + bytes) as u32;
|
||||
after.append(move(each));
|
||||
}
|
||||
|
||||
notes.clear();
|
||||
for &mut each in fresh_notes { notes.append(move(each)); }
|
||||
val kept := before.length + after.length;
|
||||
for &mut each in before { notes.append(move(each)); }
|
||||
|
||||
# The nodes after the change move with their tokens, and their ends with
|
||||
# them, which are held as how far past their tokens they are.
|
||||
for at in 1..this.node_count {
|
||||
if this.node_tokens[at] >= to {
|
||||
this.node_tokens[at] = (this.node_tokens[at] as i64 + shift) as u32;
|
||||
}
|
||||
}
|
||||
|
||||
var moved := vector::<u64>::new();
|
||||
for at in 0..this.starts.capacity {
|
||||
if this.starts.key_at(at) != keymap::free && this.starts.value_at(at) >= to {
|
||||
moved.append(this.starts.key_at(at));
|
||||
}
|
||||
}
|
||||
for key in moved {
|
||||
this.starts.set(key, (this.starts.get_or(&key, 0) as i64 + shift) as u32);
|
||||
}
|
||||
|
||||
this.take_tokens(fresh);
|
||||
|
||||
# The declarations around the change are parsed again, and their run takes
|
||||
# the place of theirs among the rest.
|
||||
val limit := (to as i64 + shift) as u32;
|
||||
var parsing := parser::new(this, names, notes, file);
|
||||
parsing.index = from;
|
||||
parsing.limit = limit;
|
||||
parsing.sink.count = kept as u32;
|
||||
parsing.sync();
|
||||
|
||||
val mark := parsing.mark;
|
||||
for i in 0..first { parsing.push(this.run_at(decls, i as u32)); }
|
||||
|
||||
# Where the parser reached as many notes as it makes, a whole parse would have
|
||||
# stopped somewhere else, so it is what tells which are made.
|
||||
val made := parsing.declarations(limit);
|
||||
if parsing.cut || parsing.sink.full {
|
||||
return this.parse_again(document, names, notes, file);
|
||||
}
|
||||
|
||||
for &mut each in after { notes.append(move(each)); }
|
||||
|
||||
for &node in this.run_view(made) { parsing.push(node); }
|
||||
for i in (last + 1)..count { parsing.push(this.run_at(decls, i as u32)); }
|
||||
|
||||
val declared := parsing.make_run(mark);
|
||||
this.node_lefts[document] = declared;
|
||||
this.set_node_end(document, this.token_count as u32 - 1);
|
||||
|
||||
if this.node_count > this.whole_nodes * 2 || this.extras.length > this.whole_extras * 2 {
|
||||
return this.parse_again(document, names, notes, file);
|
||||
}
|
||||
return document;
|
||||
}
|
||||
|
||||
#! Whether the old token and the fresh one are the same, the fresh one having
|
||||
#! moved `bytes` along the text.
|
||||
#! @param this The syntax tree.
|
||||
#! @param fresh The syntax lexed again.
|
||||
#! @param old The old token.
|
||||
#! @param now The fresh token.
|
||||
#! @param bytes How far the edit moved the fresh one, which is negative where
|
||||
#! text was taken out.
|
||||
#! @returns Whether they are.
|
||||
def syntax::same_token(&this, fresh: &syntax, old: u32, now: u32, bytes: i64): bool {
|
||||
val kind := this.token_kinds[old];
|
||||
return kind == fresh.token_kinds[now]
|
||||
&& this.token_names[old] == fresh.token_names[now]
|
||||
&& this.token_offsets[old] as i64 + bytes == fresh.token_offsets[now] as i64
|
||||
&& (kind == tk::name || kind.width != 0 || this.literal_length(old) == fresh.literal_length(now));
|
||||
}
|
||||
|
||||
#! Takes the tokens, documentation, and comments of `fresh` for its own.
|
||||
#! @param this The syntax tree.
|
||||
#! @param fresh The syntax lexed again, which is left without them.
|
||||
def syntax::take_tokens(&mut this, fresh: &mut syntax): void {
|
||||
this.token_kinds = move(&mut fresh.token_kinds);
|
||||
this.token_offsets = move(&mut fresh.token_offsets);
|
||||
this.token_names = move(&mut fresh.token_names);
|
||||
this.literal_tokens = move(&mut fresh.literal_tokens);
|
||||
this.literal_lengths = move(&mut fresh.literal_lengths);
|
||||
this.docs = move(&mut fresh.docs);
|
||||
this.comments = move(&mut fresh.comments);
|
||||
}
|
||||
|
||||
#! Parses the whole file again, from the tokens it already took.
|
||||
#! @param this The syntax tree.
|
||||
#! @param document The `document` node to parse into.
|
||||
#! @param names The names its tokens were lexed with.
|
||||
#! @param notes The file's notes, the lexer's first, which are kept, and
|
||||
#! then the parser's, which are made again.
|
||||
#! @param file The file's index.
|
||||
#! @returns The `document` node.
|
||||
def syntax::parse_again(
|
||||
&mut this,
|
||||
document: u32,
|
||||
names: &names,
|
||||
notes: &mut vector<note>,
|
||||
file: u32
|
||||
): u32 {
|
||||
this.reset();
|
||||
this.parses++;
|
||||
var lexed: u64 = 0;
|
||||
while lexed < notes.length && notes[lexed].lexed {
|
||||
lexed++;
|
||||
}
|
||||
while notes.length > lexed {
|
||||
notes.truncate();
|
||||
}
|
||||
|
||||
var parsing := parser::new(this, names, notes, file);
|
||||
return parsing.parse();
|
||||
}
|
||||
@@ -0,0 +1,389 @@
|
||||
#! @file statements.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the parsing of blocks and the statements within them: `if`, `while`,
|
||||
#! `for`, `match`, jumps, contracts, local functions, local variables, and the
|
||||
#! expressions and assignments written as statements.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! `{ statements }`.
|
||||
#! @param this The parser.
|
||||
#! @returns The `block` node.
|
||||
def parser::block(&mut this): u32 {
|
||||
if !this.check(tk::lbrace) {
|
||||
this.fail(ec::token, "Expected `{` to begin a block.");
|
||||
return this.error_here();
|
||||
}
|
||||
|
||||
val token := this.index;
|
||||
val angled := this.angled;
|
||||
this.angled = false;
|
||||
this.advance();
|
||||
|
||||
val mark := this.mark;
|
||||
while !this.check(tk::rbrace) && !this.check(tk::eof) {
|
||||
this.push(this.statement());
|
||||
if this.panic {
|
||||
this.synchronize(parser::scope::block);
|
||||
}
|
||||
}
|
||||
|
||||
val statements := this.make_run(mark);
|
||||
this.expect(tk::rbrace, "Expected `}` to close the block.");
|
||||
this.angled = angled;
|
||||
return this.make_node(nk::block, token, statements);
|
||||
}
|
||||
|
||||
def parser::statement(&mut this): u32 {
|
||||
return this.error_here() if !this.enter();
|
||||
|
||||
var made: u32 = 0;
|
||||
match this.kind {
|
||||
tk::kw_pub => {
|
||||
this.report(ec::token,
|
||||
"Only a declaration outside a function is `pub`, since nothing within one is reached by name from elsewhere.");
|
||||
this.advance();
|
||||
made = this.statement();
|
||||
}
|
||||
# The statement is kept with the attributes, within an `error` node.
|
||||
tk::attr => {
|
||||
val token := this.index;
|
||||
this.report(ec::token,
|
||||
"Attributes describe declarations outside a function, since nothing within one is reached by name from elsewhere.");
|
||||
val attrs := this.attributes();
|
||||
val held := this.panic ? 0 : this.statement();
|
||||
made = this.error(token, this.join_runs(attrs, this.run_of(held)));
|
||||
}
|
||||
tk::kw_val, tk::kw_var => {
|
||||
val lead := this.bare_lead();
|
||||
made = this.variable(&lead, parser::place::local);
|
||||
}
|
||||
tk::kw_const => {
|
||||
# A function within a block lives in its frame, which no constant can reach.
|
||||
if this.peek(1) == tk::kw_def {
|
||||
this.report(ec::token, "A `const def` is declared outside any function, where constants can call it.");
|
||||
this.advance();
|
||||
made = this.peek(1) == tk::name ? this.local_function() : this.simple();
|
||||
} else {
|
||||
val lead := this.bare_lead();
|
||||
made = this.variable(&lead, parser::place::local);
|
||||
}
|
||||
}
|
||||
tk::kw_if => made = this.if_stmt();
|
||||
tk::kw_when => made = this.when_block(false);
|
||||
tk::kw_while => made = this.while_stmt();
|
||||
tk::kw_for => made = this.for_stmt();
|
||||
tk::kw_match => made = this.match_expr(false);
|
||||
tk::kw_ret => made = this.jump(nk::return_stmt);
|
||||
tk::kw_break => made = this.jump(nk::break_stmt);
|
||||
tk::kw_cont => made = this.jump(nk::continue_stmt);
|
||||
tk::lbrace => made = this.block();
|
||||
tk::kw_def => made = this.peek(1) == tk::name ? this.local_function() : this.simple();
|
||||
else => made = this.contract_word() ? this.contract() : this.simple();
|
||||
}
|
||||
|
||||
this.leave();
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Whether the statement is a contract: `expects`, `asserts`, or `ensures`
|
||||
#! before `(`. Anywhere else, each is a name, as `t.expects(x)` calls a method.
|
||||
#! @param this The parser.
|
||||
#! @returns Whether it is.
|
||||
def parser::contract_word(&this): bool {
|
||||
return false if this.kind != tk::name || this.peek(1) != tk::lparen;
|
||||
val word := this.syntax.token_names[this.index];
|
||||
return word == names::expects_id || word == names::asserts_id || word == names::ensures_id;
|
||||
}
|
||||
|
||||
#! A contract, `expects(cond)`, `asserts(cond)`, or `ensures(cond)`, each of
|
||||
#! which may say what failed in place of the condition, as
|
||||
#! `expects(n != 0, "A count is not zero.")`, and an `ensures` may name what the
|
||||
#! function returns, as `ensures(r: r > 0)`, within which `old(e)` is what `e`
|
||||
#! was where it was reached.
|
||||
#! @param this The parser.
|
||||
#! @returns The `contract` node.
|
||||
def parser::contract(&mut this): u32 {
|
||||
val token := this.index;
|
||||
val ensures := this.syntax.token_names[token] == names::ensures_id;
|
||||
this.advance();
|
||||
this.advance();
|
||||
|
||||
var bound: u32 = 0;
|
||||
if ensures && this.check(tk::name) && this.peek(1) == tk::colon {
|
||||
bound = this.name_node("Expected the name of what the function returns.");
|
||||
this.advance();
|
||||
}
|
||||
val within := this.in_ensures;
|
||||
this.in_ensures = ensures;
|
||||
val condition := this.expression();
|
||||
this.in_ensures = within;
|
||||
var text: u32 = 0;
|
||||
if this.accept(tk::comma) {
|
||||
text = this.expression();
|
||||
}
|
||||
this.expect(tk::rparen, "Expected `)` to close the contract, which takes its condition and what it says failed.");
|
||||
this.expect(tk::semicolon, "Expected `;` after the statement.");
|
||||
return this.make_node(nk::contract, token, condition, this.make_record(bound, text));
|
||||
}
|
||||
|
||||
#! An expression followed by `;`.
|
||||
#! @param this The parser.
|
||||
#! @returns The `expr_stmt` node.
|
||||
def parser::simple(&mut this): u32 {
|
||||
val token := this.index;
|
||||
val made := this.expression();
|
||||
this.expect(tk::semicolon, "Expected `;` after the statement.");
|
||||
return this.make_node(nk::expr_stmt, token, made);
|
||||
}
|
||||
|
||||
#! `if cond { } else if cond { } else { }`, and `if const`.
|
||||
#! @param this The parser.
|
||||
#! @returns The `if_stmt` or `if_const` node.
|
||||
def parser::if_stmt(&mut this): u32 {
|
||||
return this.error_here() if !this.enter();
|
||||
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
|
||||
val constant := this.accept(tk::kw_const);
|
||||
val condition := this.condition_expr();
|
||||
val then := this.block();
|
||||
var other: u32 = 0;
|
||||
if this.accept(tk::kw_else) {
|
||||
other = this.check(tk::kw_if)
|
||||
? this.if_stmt()
|
||||
: this.block();
|
||||
}
|
||||
|
||||
this.leave();
|
||||
return this.branch(constant ? nk::if_const : nk::if_stmt, token, condition, then, other);
|
||||
}
|
||||
|
||||
def parser::while_stmt(&mut this): u32 {
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
|
||||
val condition := this.condition_expr();
|
||||
return this.make_node(nk::while_stmt, token, condition, this.block());
|
||||
}
|
||||
|
||||
#! `for x in xs`, `for i, x in xs`, `for &x in xs`, `for &mut x in xs`,
|
||||
#! `for i in a..b`, and `for const x in args`, which is unrolled over a pack
|
||||
#! while compiling.
|
||||
#! @param this The parser.
|
||||
#! @returns The `for_stmt` node.
|
||||
def parser::for_stmt(&mut this): u32 {
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
|
||||
val constant := this.accept(tk::kw_const);
|
||||
var index: u32 = 0;
|
||||
if this.check(tk::name) && this.peek(1) == tk::comma {
|
||||
index = this.name_node("Expected a name for the index.");
|
||||
this.advance();
|
||||
}
|
||||
|
||||
val reference := this.accept(tk::bitand);
|
||||
val mutable := reference && this.accept(tk::kw_mut);
|
||||
val variable := this.name_node("Expected a name for the loop variable.");
|
||||
this.expect(tk::kw_in, "Expected `in` after the loop variable.");
|
||||
|
||||
# A range is the loop's own, so `for i in 0..n` counts, rather than walking a
|
||||
# range made as a value.
|
||||
var iterated := this.condition_expr();
|
||||
var end: u32 = 0;
|
||||
var inclusive := false;
|
||||
if this.syntax.node_kinds[iterated] == nk::range {
|
||||
inclusive = this.syntax.token_kinds[this.syntax.node_tokens[iterated]] == tk::cascadeeq;
|
||||
end = this.syntax.node_rights[iterated];
|
||||
iterated = this.syntax.node_lefts[iterated];
|
||||
}
|
||||
val flags := parser::bit(constant, nf::constant)
|
||||
| parser::bit(reference, nf::reference)
|
||||
| parser::bit(mutable, nf::mutable)
|
||||
| parser::bit(inclusive, nf::inclusive);
|
||||
val record := this.make_record(index, variable, iterated, end, flags);
|
||||
return this.make_node(nk::for_stmt, token, record, this.block());
|
||||
}
|
||||
|
||||
#! A match statement, or a match used as a value, whose arms are separated by
|
||||
#! commas. `match const T` chooses by a type while compiling, its arms' patterns
|
||||
#! naming traits or types, and its node's token is the `const`.
|
||||
#! @param this The parser.
|
||||
#! @param value Whether it is used as a value.
|
||||
#! @returns The `match_stmt` or `match_value` node.
|
||||
def parser::match_expr(&mut this, value: bool): u32 {
|
||||
return this.error_here() if !this.enter();
|
||||
|
||||
val kind := value ? nk::match_value : nk::match_stmt;
|
||||
var token := this.index;
|
||||
this.advance();
|
||||
val chosen := this.check(tk::kw_const);
|
||||
if chosen {
|
||||
token = this.index;
|
||||
this.advance();
|
||||
}
|
||||
|
||||
val subject := this.condition_expr();
|
||||
if !this.accept(tk::lbrace) {
|
||||
this.fail(ec::token, "Expected `{` to begin the arms of the match.");
|
||||
this.leave();
|
||||
return this.make_node(kind, token, subject);
|
||||
}
|
||||
|
||||
# A match used as a value separates its arms with commas, and one as a
|
||||
# statement ends each with the block or statement it does.
|
||||
val arms := value
|
||||
? this.list(chosen ? parser::item::const_arm : parser::item::value_arm, tk::rbrace,
|
||||
"Expected `,` between the arms, or `}` to close the match.")
|
||||
: this.arms(chosen);
|
||||
this.leave();
|
||||
return this.make_node(kind, token, subject, arms);
|
||||
}
|
||||
|
||||
#! Whether a value written in place begins here, as `4`, `-1`, `'a'`, `"a"`, or
|
||||
#! `true`, which a `match const` choosing by a constant takes as a pattern.
|
||||
#! @param this The parser.
|
||||
#! @returns Whether one does.
|
||||
def parser::written_value(&this): bool {
|
||||
val kind := this.peek(0);
|
||||
if kind == tk::minus {
|
||||
val next := this.peek(1);
|
||||
return next == tk::signed || next == tk::unsigned || next == tk::float;
|
||||
}
|
||||
return kind == tk::kw_true || kind == tk::kw_false || kind == tk::signed || kind == tk::unsigned
|
||||
|| kind == tk::float || kind == tk::char || kind == tk::string;
|
||||
}
|
||||
|
||||
#! The arms of a match statement, through its `}`.
|
||||
#! @param this The parser.
|
||||
#! @param chosen Whether it is a `match const`, whose patterns are traits or
|
||||
#! types.
|
||||
#! @returns The run of `match_arm` nodes.
|
||||
def parser::arms(&mut this, chosen: bool): u32 {
|
||||
val mark := this.mark;
|
||||
while !this.check(tk::rbrace) && !this.check(tk::eof) {
|
||||
this.push(this.arm(false, chosen));
|
||||
if this.panic {
|
||||
this.synchronize(parser::scope::block);
|
||||
}
|
||||
}
|
||||
|
||||
val arms := this.make_run(mark);
|
||||
this.expect(tk::rbrace, "Expected `}` to close the match.");
|
||||
return arms;
|
||||
}
|
||||
|
||||
#! An arm of a match: its patterns, or `else`, then what it does, which is a
|
||||
#! block or a statement, or in a match used as a value, an expression. A pattern
|
||||
#! is an expression: a value compared with the subject, or the name of the field
|
||||
#! of a union the subject holds, as `error` or `error(code)`. A `match const`'s
|
||||
#! patterns are types instead, as `integer` or `&strview`, or values written in
|
||||
#! place, as `4` or `true`, where it chooses by a constant, and where it is a
|
||||
#! type, so is what each arm gives.
|
||||
#! @param this The parser.
|
||||
#! @param value Whether the match is used as a value.
|
||||
#! @param chosen Whether it is a `match const`.
|
||||
#! @param typed Whether it is a type, whose arms give types.
|
||||
#! @returns The `match_arm` node, or an `error` node.
|
||||
def parser::arm(&mut this, value: bool, chosen: bool = false, typed: bool = false): u32 {
|
||||
val token := this.index;
|
||||
var patterns: u32 = 0;
|
||||
if !this.accept(tk::kw_else) {
|
||||
if chosen {
|
||||
val mark := this.mark;
|
||||
while true {
|
||||
this.push(this.written_value() ? this.condition_expr() : this.type_expr());
|
||||
break if !this.accept(tk::comma);
|
||||
}
|
||||
patterns = this.make_run(mark);
|
||||
} else if this.check(tk::name) && this.peek(1) == tk::colon {
|
||||
this.fail(ec::token,
|
||||
"An arm names the field of a union it matches, as `field =>`, or `field(value) =>` to use its value.");
|
||||
} else {
|
||||
val mark := this.mark;
|
||||
while true {
|
||||
this.push(this.condition_expr());
|
||||
break if !this.accept(tk::comma);
|
||||
}
|
||||
patterns = this.make_run(mark);
|
||||
}
|
||||
}
|
||||
|
||||
this.expect(tk::arrow, "Expected `=>` followed by what the arm does.");
|
||||
var body: u32 = 0;
|
||||
if typed {
|
||||
body = this.type_expr();
|
||||
} else if value && this.check(tk::kw_ret) {
|
||||
this.fail(ec::token,
|
||||
"Each arm of a match used as a value is the expression it gives, as `2 => x,`, so leave out `return` and `;`.");
|
||||
} else if value {
|
||||
body = this.expression();
|
||||
} else if this.check(tk::lbrace) {
|
||||
body = this.block();
|
||||
} else {
|
||||
# A single statement is the body, as a block of its own.
|
||||
val first := this.index;
|
||||
body = this.make_node(nk::block, first, this.run_of(this.statement()));
|
||||
}
|
||||
|
||||
return this.make_node(nk::match_arm, token, patterns, body);
|
||||
}
|
||||
|
||||
#! `return`, `break`, or `continue`, each of which may be followed by `if`.
|
||||
#! `return value if cond;` is the `if` it reads as, built here so that nothing
|
||||
#! below the parser tells the two apart.
|
||||
#! @param this The parser.
|
||||
#! @param kind `return_stmt`, `break_stmt`, or `continue_stmt`.
|
||||
#! @returns The node, or the `if_stmt` holding it.
|
||||
def parser::jump(&mut this, kind: nk): u32 {
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
|
||||
var value: u32 = 0;
|
||||
if kind == nk::return_stmt && !this.check(tk::semicolon) && !this.check(tk::kw_if) {
|
||||
value = this.expression();
|
||||
}
|
||||
|
||||
var made := this.make_node(kind, token, value);
|
||||
if this.check(tk::kw_if) {
|
||||
val guard := this.index;
|
||||
this.advance();
|
||||
|
||||
val constant := this.accept(tk::kw_const);
|
||||
val condition := this.condition_expr();
|
||||
val then := this.make_node(nk::block, token, this.run_of(made));
|
||||
made = this.branch(constant ? nk::if_const : nk::if_stmt, guard, condition, then, 0);
|
||||
}
|
||||
|
||||
this.expect(tk::semicolon, "Expected `;` after the statement.");
|
||||
return made;
|
||||
}
|
||||
|
||||
#! A function declared within a block, `def name(params): type { body }`, which
|
||||
#! is a local holding the function as a `lambda`, named before its body so it
|
||||
#! can call itself. It gives the types of its parameters and its result, as any
|
||||
#! other named function does.
|
||||
#! @param this The parser.
|
||||
#! @returns The `variable` node, or an `error` node.
|
||||
def parser::local_function(&mut this): u32 {
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
|
||||
val name := this.index;
|
||||
this.advance();
|
||||
if this.check(tk::less) {
|
||||
this.fail(ec::token, "Functions declared within a block cannot be generic.");
|
||||
return this.error_here();
|
||||
}
|
||||
|
||||
val signature := this.params(false, false);
|
||||
val body := this.block();
|
||||
val lambda := this.make_node(nk::lambda, token, signature.params, this.make_record(signature.result, body));
|
||||
return this.make_node(nk::variable, name, this.make_record(0, 0, 0, lambda), nf::function as u32);
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
#! @file tables.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the tables the parser reads tokens by, built while compiling: the
|
||||
#! traits of each kind of token, as whether it begins a declaration or closes a
|
||||
#! list, and the binding power of each operator.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! What the parser asks of a kind of token, as bits of `tk::traits`. Each kind's
|
||||
#! answers are worked out once, while compiling, so asking is one load.
|
||||
enum tt : u32 {
|
||||
assigns = 0x0001, #! `=`, `+=`, and the rest.
|
||||
declares = 0x0002, #! Begins a declaration at the top level of a file.
|
||||
imports = 0x0004, #! `import` or `module`.
|
||||
states = 0x0008, #! Begins a statement a block resumes at after an error.
|
||||
fields = 0x0010, #! Begins a member a struct or union resumes at.
|
||||
members = 0x0020, #! Begins a member a trait resumes at.
|
||||
opens = 0x0040, #! Opens what a later token closes.
|
||||
closes = 0x0080, #! Closes what an earlier token opened.
|
||||
text = 0x0100, #! Its text says something its node's kind does not: a name, a literal, or an operator.
|
||||
piece = 0x0200, #! May be part of a type before its `<`, as `&mut a::b`.
|
||||
angle = 0x0400, #! Begins with a `>` that may close type arguments: `>`, `>>`, `>=`, or `>>=`.
|
||||
}
|
||||
|
||||
#! What the parser asks of each kind of token.
|
||||
const token_traits: u32[tk::count as u64] = make_token_traits();
|
||||
|
||||
#! How tightly each kind of token binds as an operator, `none` where it is not
|
||||
#! one.
|
||||
const token_powers: parser::bp[tk::count as u64] = make_token_powers();
|
||||
|
||||
#! The answers of a kind of token, as bits of `tt`.
|
||||
#! @param this The kind of token.
|
||||
#! @returns The bits.
|
||||
val tk::traits: u32 { get => token_traits[this as u64]; }
|
||||
|
||||
#! Whether a kind of token has the trait.
|
||||
#! @param this The kind of token.
|
||||
#! @param which The trait.
|
||||
#! @returns Whether it has.
|
||||
def tk::has(&this, which: tt): bool {
|
||||
return (this.traits & which as u32) != 0;
|
||||
}
|
||||
|
||||
const def make_token_traits(): u32[tk::count as u64] {
|
||||
var made: u32[tk::count as u64];
|
||||
for each in [
|
||||
tk::assign, tk::pluseq, tk::minuseq, tk::stareq, tk::slasheq, tk::moduloeq,
|
||||
tk::bitandeq, tk::bitoreq, tk::bitxoreq, tk::lshifteq, tk::rshifteq
|
||||
] { made[each as u64] |= tt::assigns as u32; }
|
||||
|
||||
for each in [
|
||||
tk::kw_pub, tk::attr, tk::kw_when, tk::kw_enum, tk::kw_def, tk::kw_const,
|
||||
tk::kw_struct, tk::kw_union, tk::kw_trait, tk::kw_type, tk::kw_val, tk::kw_var
|
||||
] { made[each as u64] |= tt::declares as u32; }
|
||||
|
||||
for each in [tk::kw_imp, tk::kw_mod, tk::kw_extern] { made[each as u64] |= tt::imports as u32; }
|
||||
|
||||
for each in [
|
||||
tk::kw_val, tk::kw_var, tk::kw_const, tk::kw_if, tk::kw_while, tk::kw_for,
|
||||
tk::kw_match, tk::kw_when, tk::kw_ret, tk::kw_break, tk::kw_cont
|
||||
] { made[each as u64] |= tt::states as u32; }
|
||||
|
||||
for each in [tk::kw_val, tk::kw_var, tk::attr, tk::kw_pub] { made[each as u64] |= tt::fields as u32; }
|
||||
for each in [tk::kw_def, tk::kw_const, tk::attr] { made[each as u64] |= tt::members as u32; }
|
||||
for each in [tk::lparen, tk::lbracket, tk::lbrace, tk::istring_beg] { made[each as u64] |= tt::opens as u32; }
|
||||
for each in [tk::rparen, tk::rbracket, tk::rbrace, tk::istring_end] { made[each as u64] |= tt::closes as u32; }
|
||||
|
||||
for each in [
|
||||
tk::name, tk::unknown, tk::signed, tk::unsigned, tk::float, tk::char, tk::string,
|
||||
tk::istring, tk::istring_beg, tk::istring_mid, tk::istring_end,
|
||||
tk::kw_true, tk::kw_false, tk::kw_null,
|
||||
tk::assign, tk::equals, tk::negate, tk::noteq, tk::less, tk::lesseq, tk::more,
|
||||
tk::moreeq, tk::plus, tk::minus, tk::star, tk::slash, tk::modulo, tk::logand,
|
||||
tk::logor, tk::bitand, tk::bitor, tk::bitxor, tk::bitnot, tk::lshift, tk::rshift,
|
||||
tk::pluseq, tk::minuseq, tk::stareq, tk::slasheq, tk::moduloeq, tk::bitandeq,
|
||||
tk::bitoreq, tk::bitxoreq, tk::lshifteq, tk::rshifteq, tk::increment, tk::decrement,
|
||||
tk::cascade, tk::cascadeeq
|
||||
] { made[each as u64] |= tt::text as u32; }
|
||||
|
||||
for each in [tk::name, tk::scope, tk::kw_mut, tk::bitand, tk::star, tk::kw_not] {
|
||||
made[each as u64] |= tt::piece as u32;
|
||||
}
|
||||
|
||||
for each in [tk::more, tk::rshift, tk::moreeq, tk::rshifteq] { made[each as u64] |= tt::angle as u32; }
|
||||
return made;
|
||||
}
|
||||
|
||||
const def make_token_powers(): parser::bp[tk::count as u64] {
|
||||
var made: parser::bp[tk::count as u64];
|
||||
for each in [
|
||||
tk::assign, tk::pluseq, tk::minuseq, tk::stareq, tk::slasheq, tk::moduloeq,
|
||||
tk::bitandeq, tk::bitoreq, tk::bitxoreq, tk::lshifteq, tk::rshifteq
|
||||
] { made[each as u64] = parser::bp::assign; }
|
||||
|
||||
made[tk::kw_else as u64] = parser::bp::fallback;
|
||||
made[tk::quest as u64] = parser::bp::ternary;
|
||||
made[tk::cascade as u64] = parser::bp::range;
|
||||
made[tk::cascadeeq as u64] = parser::bp::range;
|
||||
made[tk::logor as u64] = parser::bp::logor;
|
||||
made[tk::logand as u64] = parser::bp::logand;
|
||||
made[tk::bitor as u64] = parser::bp::bitor;
|
||||
made[tk::bitxor as u64] = parser::bp::bitxor;
|
||||
made[tk::bitand as u64] = parser::bp::bitand;
|
||||
for each in [tk::equals, tk::noteq] { made[each as u64] = parser::bp::equality; }
|
||||
for each in [tk::less, tk::lesseq, tk::more, tk::moreeq] { made[each as u64] = parser::bp::compare; }
|
||||
for each in [tk::lshift, tk::rshift] { made[each as u64] = parser::bp::shift; }
|
||||
for each in [tk::plus, tk::minus] { made[each as u64] = parser::bp::sum; }
|
||||
for each in [tk::star, tk::slash, tk::modulo] { made[each as u64] = parser::bp::product; }
|
||||
for each in [tk::kw_as, tk::kw_is] { made[each as u64] = parser::bp::cast; }
|
||||
for each in [tk::lparen, tk::lbracket, tk::dot, tk::increment, tk::decrement, tk::scope] {
|
||||
made[each as u64] = parser::bp::postfix;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
@@ -0,0 +1,627 @@
|
||||
#! @file types.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the parsing of types: names and paths with their type arguments,
|
||||
#! references, arrays, `type_of`, and the type parameters of declarations.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! What may follow the segments of a path as type arguments.
|
||||
enum parser::args {
|
||||
plain, #! Nothing, as in imports.
|
||||
types, #! Type arguments after `<`, as in `box<i64>`.
|
||||
values, #! Type arguments after `::<`, as in `box::<i64>`.
|
||||
}
|
||||
|
||||
#! A type, as `&mut T`, `*u8`, `T[4]`, `def(T): U`, or `std::list<T>`, which
|
||||
#! is also what follows `as` and `is`, so `x as i32 | y` remains a bitwise or.
|
||||
#! @param this The parser.
|
||||
#! @returns The type's node, or an `error` node.
|
||||
def parser::type_expr(&mut this): u32 {
|
||||
return this.error_here() if !this.enter();
|
||||
# What `as` and `is` take goes on with the expression's `?`, not a type's.
|
||||
val chooses := !this.plain_type;
|
||||
this.plain_type = false;
|
||||
val argument := this.type_argument;
|
||||
this.type_argument = false;
|
||||
|
||||
# `C ? A : B` chooses by a constant: a name or a path, which `!` may deny, a
|
||||
# call, or an expression in parentheses, as `(N > 4) ? A : B`.
|
||||
var condition: u32 = 0;
|
||||
var made: u32 = 0;
|
||||
if chooses && this.check(tk::negate) {
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
condition = this.make_node(nk::unary, token, this.condition_name(this.type_name()));
|
||||
if !this.check(tk::quest) {
|
||||
this.fail(ec::token, "Expected `?`: a type is chosen by a constant, as `!MUTABLE ? &T : &mut T`.");
|
||||
}
|
||||
made = condition;
|
||||
} else if chooses && this.check(tk::lparen) && this.chooses_type(0) {
|
||||
val angled := this.angled;
|
||||
this.angled = false;
|
||||
condition = this.pratt(parser::bp::ternary, true);
|
||||
this.angled = angled;
|
||||
made = condition;
|
||||
} else if chooses && this.check(tk::name) && this.call_chooses_type() {
|
||||
# A call is a condition too, as `small(N) ? A : B`.
|
||||
val angled := this.angled;
|
||||
this.angled = false;
|
||||
condition = this.pratt(parser::bp::ternary, true);
|
||||
this.angled = angled;
|
||||
made = condition;
|
||||
} else if this.check(tk::star) || this.check(tk::bitand) {
|
||||
val kind := this.check(tk::star) ? nk::type_ptr : nk::type_ref;
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
val mutable := this.accept(tk::kw_mut);
|
||||
# What `as` and `is` take stays plain within it, as `x as *T != null`.
|
||||
this.plain_type = !chooses;
|
||||
made = this.make_node(kind, token, this.type_expr(), parser::bit(mutable, nf::mutable));
|
||||
} else {
|
||||
made = this.type_postfix();
|
||||
if chooses && this.check(tk::quest) && this.syntax.node_kinds[made] == nk::type_name && parser::begins_type(this.peek(1)) {
|
||||
condition = this.condition_name(made);
|
||||
} else if chooses && this.syntax.node_kinds[made] == nk::type_name && parser::continues_condition(this.kind)
|
||||
&& (!argument || this.condition_chooses_type()) {
|
||||
# A test of a type is a condition too, as `T is reference ? T::target : T`,
|
||||
# and so is a name compared, as `N == 4`, or joined, as `M && N == 4`.
|
||||
condition = this.condition_after(this.condition_name(made));
|
||||
if !this.check(tk::quest) {
|
||||
this.fail(ec::token, "Expected `?`: a condition chooses a type, as `T is reference ? T::target : T`.");
|
||||
}
|
||||
made = condition;
|
||||
}
|
||||
}
|
||||
if condition != 0 && this.check(tk::quest) {
|
||||
made = this.type_cond(condition);
|
||||
}
|
||||
|
||||
this.leave();
|
||||
return made;
|
||||
}
|
||||
|
||||
#! `C ? A : B`, from the `?`: the type `A` where the constant `C` holds, and `B`
|
||||
#! where it does not, as `MUTABLE ? &mut T : &T`.
|
||||
#! @param this The parser.
|
||||
#! @param condition The condition's node.
|
||||
#! @returns The `type_cond` node.
|
||||
def parser::type_cond(&mut this, condition: u32): u32 {
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
val then := this.type_expr();
|
||||
this.expect(tk::colon, "Expected `:` between the types `?` chooses from, as `MUTABLE ? &mut T : &T`.");
|
||||
val other := this.type_expr();
|
||||
return this.make_node(nk::type_cond, token, condition, this.make_record(then, other));
|
||||
}
|
||||
|
||||
#! A name read as a type that is a type's condition instead, as `MUTABLE` of
|
||||
#! `MUTABLE ? &mut T : &T`: the name or path of a constant, as an expression.
|
||||
#! @param this The parser.
|
||||
#! @param made The `type_name` node.
|
||||
#! @returns The node, now a `name` or `path`.
|
||||
def parser::condition_name(&mut this, made: u32): u32 {
|
||||
if this.syntax.node_kinds[made] == nk::type_name {
|
||||
this.syntax.node_kinds[made] = this.syntax.node_lefts[made] == 0 ? nk::name : nk::path;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The rest of a condition a type is chosen by that begins with a name read as
|
||||
#! a type, as `T is pointer && T::target is integer` of `T is pointer &&
|
||||
#! T::target is integer ? T::target : u8`: what operators follow it, up to its
|
||||
#! `?`.
|
||||
#! @param this The parser.
|
||||
#! @param left The name's node, now a `name` or `path`.
|
||||
#! @returns The condition's node.
|
||||
def parser::condition_after(&mut this, left: u32): u32 {
|
||||
val angled := this.angled;
|
||||
this.angled = false;
|
||||
var made := left;
|
||||
while true {
|
||||
val power := this.power(made);
|
||||
break if power as u32 <= parser::bp::ternary as u32;
|
||||
made = this.infix(made, power, true);
|
||||
}
|
||||
this.angled = angled;
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Whether a token after a name read as a type goes on with a condition a
|
||||
#! type is chosen by, which nothing a type is goes on with: `is`, `==`, `!=`,
|
||||
#! `&&`, or `||`. A comparison by `<` or `>` is written in parentheses, as
|
||||
#! `(N > 4) ? A : B`, since those are a type's arguments.
|
||||
#! @param kind The token's kind.
|
||||
#! @returns Whether it does.
|
||||
def parser::continues_condition(kind: tk): bool {
|
||||
return kind == tk::kw_is || kind == tk::equals || kind == tk::noteq || kind == tk::logand || kind == tk::logor;
|
||||
}
|
||||
|
||||
#! Whether what follows a name read as a type among type arguments goes on
|
||||
#! with a condition a type is chosen by, up to a `?` a type follows, as `is
|
||||
#! reference ? T::target : T`, where `T is integer ? 4 : 8` is a constant. The
|
||||
#! condition compares by no `<` or `>`, so those are brackets.
|
||||
#! @param this The parser.
|
||||
#! @returns Whether it does.
|
||||
def parser::condition_chooses_type(&this): bool {
|
||||
var depth: u32 = 0;
|
||||
var ahead: u32 = 0;
|
||||
while ahead < 1024 {
|
||||
val kind := this.peek(ahead);
|
||||
return false if kind == tk::eof || kind == tk::semicolon || kind == tk::lbrace || kind == tk::rbrace;
|
||||
if kind == tk::lparen || kind == tk::lbracket || kind == tk::less {
|
||||
depth++;
|
||||
} else if kind == tk::rparen || kind == tk::rbracket || kind == tk::more {
|
||||
return false if depth == 0;
|
||||
depth--;
|
||||
} else if kind == tk::rshift {
|
||||
return false if depth < 2;
|
||||
depth -= 2;
|
||||
} else if depth == 0 && (kind == tk::comma || kind == tk::assign) {
|
||||
return false;
|
||||
} else if depth == 0 && kind == tk::quest {
|
||||
return parser::begins_type(this.peek(ahead + 1));
|
||||
}
|
||||
ahead++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Whether a token begins a type, so a `?` before it chooses between types.
|
||||
#! @param kind The token's kind.
|
||||
#! @returns Whether it does.
|
||||
def parser::begins_type(kind: tk): bool {
|
||||
return kind == tk::name || kind == tk::star || kind == tk::bitand || kind == tk::kw_def
|
||||
|| kind == tk::lparen || kind == tk::kw_match || kind == tk::negate;
|
||||
}
|
||||
|
||||
#! Whether what begins with the `(` a number of tokens ahead is the condition
|
||||
#! a type is chosen by: a `?` a type follows comes after it, outside any
|
||||
#! parentheses, before anything ends a type, as `(N > 4) ? A : B`, where `(N >
|
||||
#! 4) ? 8 : 4` is a value. The condition may go on past it, as `(A || B) && (C
|
||||
#! || D) ? X : Y`, or `small(N) && M ? A : B` from a call's `(`, but compares by
|
||||
#! `<` or `>` only within parentheses, since `>` ends type arguments.
|
||||
#! @param this The parser.
|
||||
#! @param start How many tokens ahead the `(` is.
|
||||
#! @returns Whether it does.
|
||||
def parser::chooses_type(&this, start: u32): bool {
|
||||
var depth: u32 = 0;
|
||||
var ahead := start;
|
||||
while ahead < start + 1024 {
|
||||
val kind := this.peek(ahead);
|
||||
return false if kind == tk::eof || kind == tk::semicolon || kind == tk::lbrace || kind == tk::rbrace;
|
||||
if kind == tk::lparen {
|
||||
depth++;
|
||||
} else if kind == tk::rparen {
|
||||
return false if depth == 0;
|
||||
depth--;
|
||||
} else if depth == 0 {
|
||||
return parser::begins_type(this.peek(ahead + 1)) if kind == tk::quest;
|
||||
return false if kind == tk::comma || kind == tk::assign || kind == tk::colon || kind == tk::more
|
||||
|| kind == tk::moreeq || kind == tk::rshift || kind == tk::arrow || kind == tk::rbracket;
|
||||
}
|
||||
ahead++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Whether the name at the current token begins a call that is the condition
|
||||
#! a type is chosen by, or begins it, as `small(N) ? u32 : u64`: a name or a
|
||||
#! path, which may give type arguments after `::<`, then its arguments in
|
||||
#! parentheses, as `chooses_type` reads them.
|
||||
#! @param this The parser.
|
||||
#! @returns Whether it does.
|
||||
def parser::call_chooses_type(&this): bool {
|
||||
var ahead: u32 = 1;
|
||||
while this.peek(ahead) == tk::scope {
|
||||
if this.peek(ahead + 1) == tk::name {
|
||||
ahead += 2;
|
||||
continue;
|
||||
}
|
||||
return false if this.peek(ahead + 1) != tk::less;
|
||||
ahead += 2;
|
||||
var depth: u32 = 1;
|
||||
while depth != 0 && ahead < 256 {
|
||||
val kind := this.peek(ahead);
|
||||
return false if kind == tk::eof || kind == tk::semicolon || kind == tk::lbrace || kind == tk::rbrace;
|
||||
if kind == tk::less {
|
||||
depth++;
|
||||
} else if kind == tk::more {
|
||||
depth--;
|
||||
} else if kind == tk::rshift {
|
||||
return false if depth < 2;
|
||||
depth -= 2;
|
||||
}
|
||||
ahead++;
|
||||
}
|
||||
}
|
||||
return this.peek(ahead) == tk::lparen && this.chooses_type(ahead);
|
||||
}
|
||||
|
||||
#! A type followed by the lengths of fixed arrays of it, as `T[4][2]`, or a
|
||||
#! pack followed by the index of one of its types, as `Ts...[0]`, a `type_fixed`
|
||||
#! node whose token is the `...`.
|
||||
#! @param this The parser.
|
||||
#! @returns The type's node, a `type_fixed` node where lengths follow.
|
||||
def parser::type_postfix(&mut this): u32 {
|
||||
var made := this.type_primary();
|
||||
while this.check(tk::lbracket) || (this.check(tk::spread) && this.peek(1) == tk::lbracket) {
|
||||
val token := this.index;
|
||||
if this.check(tk::spread) {
|
||||
this.advance();
|
||||
this.advance();
|
||||
val index := this.expression();
|
||||
this.expect(tk::rbracket, "Expected `]` after the index of the pack's type.");
|
||||
made = this.make_node(nk::type_fixed, token, made, index);
|
||||
continue;
|
||||
}
|
||||
this.advance();
|
||||
if this.check(tk::rbracket) {
|
||||
this.report(ec::token,
|
||||
"Expected the number of elements, as `T[4]`: a run of elements whose number is not known is a `std::list<T>`, and a view of one a `slice<T>`.");
|
||||
}
|
||||
|
||||
val length := this.expression();
|
||||
this.expect(tk::rbracket, "Expected `]` after the number of elements.");
|
||||
made = this.make_node(nk::type_fixed, token, made, length);
|
||||
}
|
||||
|
||||
return made;
|
||||
}
|
||||
|
||||
#! A type that `...` may follow: a function type's last parameter, which takes
|
||||
#! any number of arguments, as `def(i64...): i64`, or is a pack, as `def(Ts...):
|
||||
#! R`, and a pack passed on among type arguments, as `box<i64, Ts...>`.
|
||||
#! @param this The parser.
|
||||
#! @returns The type's node, or a `type_spread` of it.
|
||||
def parser::spread_type(&mut this): u32 {
|
||||
val made := this.type_expr();
|
||||
return made if !this.check(tk::spread);
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
return this.make_node(nk::type_spread, token, made);
|
||||
}
|
||||
|
||||
def parser::type_primary(&mut this): u32 {
|
||||
# A function type, as `def(int, str): bool`, whose result takes everything
|
||||
# after it.
|
||||
if this.check(tk::kw_def) {
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
this.expect(tk::lparen, "Expected `(` to begin the parameter types.");
|
||||
|
||||
val params := this.list(parser::item::param_type, tk::rparen, "Expected `)` to close the parameter types.");
|
||||
this.expect(tk::colon, "Expected `:` followed by the return type.");
|
||||
return this.make_node(nk::type_func, token, params, this.type_expr());
|
||||
}
|
||||
|
||||
if this.accept(tk::lparen) {
|
||||
val made := this.type_expr();
|
||||
this.expect(tk::rparen, "Expected `)` to close the type.");
|
||||
return made;
|
||||
}
|
||||
|
||||
# `match const T { pointer => T::target, else => T }` is the type its first arm
|
||||
# whose trait or type `T` fits gives.
|
||||
if this.check(tk::kw_match) {
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
this.expect(tk::kw_const, "Expected `const` after `match`: a type is chosen by `match const T { ... }`.");
|
||||
val subject := this.type_expr();
|
||||
if !this.accept(tk::lbrace) {
|
||||
this.fail(ec::token, "Expected `{` to begin the arms of the match.");
|
||||
return this.make_node(nk::type_match, token, subject);
|
||||
}
|
||||
val arms := this.list(parser::item::type_arm, tk::rbrace, "Expected `,` between the arms, or `}` to close the match.");
|
||||
return this.make_node(nk::type_match, token, subject, arms);
|
||||
}
|
||||
|
||||
# `type_of(d)` is the type of what a declaration known while compiling declares.
|
||||
if this.check_word(names::type_of_id) && this.peek(1) == tk::lparen {
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
this.advance();
|
||||
|
||||
val declaration := this.expression();
|
||||
this.expect(tk::rparen, "Expected `)` after the declaration whose type it is.");
|
||||
return this.make_node(nk::type_of, token, declaration);
|
||||
}
|
||||
|
||||
return this.type_name();
|
||||
}
|
||||
|
||||
#! A named type, as `std::list<T>`, made of its segments. One of a single
|
||||
#! segment without type arguments, as `u8`, holds no run: its token names it.
|
||||
#! @param this The parser.
|
||||
#! @returns The `type_name` node.
|
||||
def parser::type_name(&mut this): u32 {
|
||||
if !this.check(tk::name) {
|
||||
this.fail(ec::kind, "Expected a type name.");
|
||||
return this.error_here();
|
||||
}
|
||||
|
||||
val token := this.index;
|
||||
if this.peek(1) != tk::less && this.peek(1) != tk::scope {
|
||||
this.advance();
|
||||
return this.make_node(nk::type_name, token);
|
||||
}
|
||||
return this.make_node(nk::type_name, token, this.segments(parser::args::types));
|
||||
}
|
||||
|
||||
#! A path, as `std::io`, of segments without type arguments. One of a single
|
||||
#! segment holds no run: its token names it.
|
||||
#! @param this The parser.
|
||||
#! @returns The `path` node.
|
||||
def parser::path(&mut this): u32 {
|
||||
val token := this.index;
|
||||
if this.check(tk::name) && this.peek(1) != tk::scope {
|
||||
this.advance();
|
||||
return this.make_node(nk::path, token);
|
||||
}
|
||||
return this.make_node(nk::path, token, this.segments(parser::args::plain));
|
||||
}
|
||||
|
||||
#! The segments of a path, as `a::b::c`, each of which may take type arguments,
|
||||
#! as `res<i64>::ok` in a type, or `res::<i64>::ok` in an expression.
|
||||
#! @param this The parser.
|
||||
#! @param args How type arguments are written after each segment, if at all.
|
||||
#! @returns The run of `segment` nodes.
|
||||
def parser::segments(&mut this, args: parser::args): u32 {
|
||||
val mark := this.mark;
|
||||
while true {
|
||||
if !this.check(tk::name) {
|
||||
this.fail(ec::ident, "Expected an identifier in the path.");
|
||||
break;
|
||||
}
|
||||
|
||||
val token := this.index;
|
||||
var types: u32 = 0;
|
||||
this.advance();
|
||||
if args == parser::args::types && this.check(tk::less) {
|
||||
types = this.type_args();
|
||||
} else if args == parser::args::values && this.at_value_args() {
|
||||
this.advance();
|
||||
types = this.type_args();
|
||||
}
|
||||
|
||||
this.push(this.make_node(nk::segment, token, types));
|
||||
break if !this.accept(tk::scope);
|
||||
}
|
||||
|
||||
return this.make_run(mark);
|
||||
}
|
||||
|
||||
#! Whether type arguments follow a name in an expression, written `::<` so that
|
||||
#! a `<` alone always compares.
|
||||
#! @param this The parser.
|
||||
#! @returns Whether they do.
|
||||
def parser::at_value_args(&this): bool {
|
||||
return this.check(tk::scope) && this.peek(1) == tk::less;
|
||||
}
|
||||
|
||||
#! The type arguments `<T, U>`, from the current `<`. After `as` and `is`, a `<`
|
||||
#! always begins type arguments, so a comparison there needs parentheses.
|
||||
#! @param this The parser.
|
||||
#! @returns The run of types.
|
||||
def parser::type_args(&mut this): u32 {
|
||||
val open := this.index;
|
||||
val angled := this.angled;
|
||||
this.angled = false;
|
||||
this.advance();
|
||||
|
||||
val mark := this.mark;
|
||||
while true {
|
||||
this.push(this.type_arg());
|
||||
break if !this.accept(tk::comma);
|
||||
}
|
||||
this.angled = angled;
|
||||
|
||||
val types := this.make_run(mark);
|
||||
this.close_angle(this.follows_cast(open)
|
||||
? "Expected `>` to close the type arguments. A `<` after the type of `as` or `is` begins type arguments, so put a comparison in parentheses, as `(n as i64) < limit`."
|
||||
: "Expected `>` to close the type arguments.");
|
||||
return types;
|
||||
}
|
||||
|
||||
#! Whether the name at the current token begins what is given type arguments
|
||||
#! after `::<`, as `size_of::<T>()`, which no type is.
|
||||
#! @param this The parser.
|
||||
#! @returns Whether it does.
|
||||
def parser::names_generic_call(&this): bool {
|
||||
var ahead: u32 = 1;
|
||||
while this.peek(ahead) == tk::scope {
|
||||
return true if this.peek(ahead + 1) == tk::less;
|
||||
return false if this.peek(ahead + 1) != tk::name;
|
||||
ahead += 2;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! A type argument: a type, or the value a constant parameter is given, a
|
||||
#! literal, a name, or an expression of them, as `buffer<u8, 64>` or
|
||||
#! `grid<N * 2>`. A name alone is read as a type, which the checker takes as the
|
||||
#! constant it names where the parameter is one. `(` begins a value, unless a
|
||||
#! type that needs one does, and so does a name given type arguments after
|
||||
#! `::<`, as `buffer<u8, size_of::<T>()>`.
|
||||
#! @param this The parser.
|
||||
#! @returns The type's or the value's node.
|
||||
def parser::type_arg(&mut this): u32 {
|
||||
val next := this.peek(1);
|
||||
val value := match this.kind {
|
||||
tk::signed, tk::unsigned, tk::float, tk::char, tk::string, tk::istring, tk::istring_beg,
|
||||
tk::kw_true, tk::kw_false, tk::kw_null, tk::minus, tk::negate, tk::bitnot, tk::lbracket => true,
|
||||
tk::lparen => next != tk::star && next != tk::bitand && next != tk::kw_def && !this.chooses_type(0),
|
||||
tk::name => this.names_generic_call(),
|
||||
else => false
|
||||
};
|
||||
return this.constant_arg(0) if value;
|
||||
|
||||
this.type_argument = true;
|
||||
val made := this.spread_type();
|
||||
return made if this.syntax.node_kinds[made] != nk::type_name || !this.continues_value();
|
||||
|
||||
# The name is the expression's first operand: a name of one segment, or a path.
|
||||
this.syntax.node_kinds[made] = this.syntax.node_lefts[made] == 0 ? nk::name : nk::path;
|
||||
return this.constant_arg(made);
|
||||
}
|
||||
|
||||
#! Whether what follows a name read as a type goes on with an expression, so the
|
||||
#! name began a constant argument, as `N` does in `grid<N * 2>`. `>` closes the
|
||||
#! arguments, so a comparison is put in parentheses.
|
||||
#! @param this The parser.
|
||||
#! @returns Whether it does.
|
||||
def parser::continues_value(&this): bool {
|
||||
return match this.kind {
|
||||
tk::lparen, tk::lbrace, tk::dot, tk::quest, tk::kw_as, tk::kw_is => true,
|
||||
tk::more, tk::moreeq, tk::rshift, tk::rshifteq, tk::comma => false,
|
||||
else => token_powers[this.kind as u64] as u32 > parser::bp::assign as u32
|
||||
};
|
||||
}
|
||||
|
||||
#! A constant argument, which is an expression, going on from the name already
|
||||
#! read as a type where one was.
|
||||
#! @param this The parser.
|
||||
#! @param primed The `name` or `path` the expression begins with, or 0.
|
||||
#! @returns The expression's node.
|
||||
def parser::constant_arg(&mut this, primed: u32): u32 {
|
||||
val angled := this.angled;
|
||||
this.angled = true;
|
||||
var made: u32 = 0;
|
||||
if primed == 0 {
|
||||
made = this.expression();
|
||||
} else {
|
||||
made = primed;
|
||||
# A struct literal of the name, as `point { x = 1 }`.
|
||||
if this.check(tk::lbrace) {
|
||||
this.syntax.node_kinds[made] = nk::type_name;
|
||||
made = this.make_node(nk::construct, this.index, made, this.field_list());
|
||||
}
|
||||
while true {
|
||||
val power := this.power(made);
|
||||
break if power == parser::bp::none;
|
||||
made = this.infix(made, power, true);
|
||||
}
|
||||
}
|
||||
this.angled = angled;
|
||||
return made;
|
||||
}
|
||||
|
||||
#! A pattern parameter, `def(A, Ts...): R`, from the current `def`: a type
|
||||
#! parameter for each of the parameter types it names, the last of which may be
|
||||
#! a pack, and one for its result.
|
||||
#! @param this The parser.
|
||||
#! @returns The `type_pattern` node.
|
||||
def parser::type_pattern(&mut this): u32 {
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
this.expect(tk::lparen, "Expected `(` to begin the pattern's parameter types, as `def(Ts...): R`.");
|
||||
|
||||
val mark := this.mark;
|
||||
while !this.check(tk::rparen) && !this.check(tk::eof) {
|
||||
if !this.check(tk::name) {
|
||||
this.fail(ec::ident, "Expected the name of a type parameter the pattern's parameter type is, as `T` of `def(T): R`.");
|
||||
break;
|
||||
}
|
||||
val name := this.index;
|
||||
this.advance();
|
||||
val packed := this.accept(tk::spread);
|
||||
if packed && !this.check(tk::rparen) {
|
||||
this.report_at(name, ec::token, "A pack takes every parameter type after those before it, so it is the last.");
|
||||
}
|
||||
this.push(this.make_node(nk::type_param, name, 0, parser::bit(packed, nf::packed)));
|
||||
break if !this.accept(tk::comma);
|
||||
}
|
||||
val params := this.make_run(mark);
|
||||
this.expect(tk::rparen, "Expected `)` to close the pattern's parameter types.");
|
||||
this.expect(tk::colon, "Expected `:` followed by the name of the type parameter the result is, as `R` of `def(Ts...): R`.");
|
||||
|
||||
var result: u32 = 0;
|
||||
if this.check(tk::name) {
|
||||
result = this.make_node(nk::type_param, this.index, 0, 0);
|
||||
this.advance();
|
||||
} else {
|
||||
this.fail(ec::ident, "Expected the name of the type parameter the result is, as `R` of `def(Ts...): R`.");
|
||||
}
|
||||
return this.make_node(nk::type_pattern, token, params, result);
|
||||
}
|
||||
|
||||
#! A pattern parameter, `L[N]`, from the current `[` after the name of its
|
||||
#! element's type parameter: that, and a constant parameter for its length.
|
||||
#! @param this The parser.
|
||||
#! @param token The name of the element's type parameter.
|
||||
#! @returns The `array_pattern` node.
|
||||
def parser::array_pattern(&mut this, token: u32): u32 {
|
||||
val element := this.make_node(nk::type_param, token, 0, 0);
|
||||
this.advance();
|
||||
var length: u32 = 0;
|
||||
if this.check(tk::name) {
|
||||
length = this.make_node(nk::const_param, this.index, 0, 0);
|
||||
this.advance();
|
||||
} else {
|
||||
this.fail(ec::ident, "Expected the name of the constant parameter the length is, as `N` of `L[N]`.");
|
||||
}
|
||||
this.expect(tk::rbracket, "Expected `]` to close the pattern, as `L[N]`.");
|
||||
return this.make_node(nk::array_pattern, token, element, length);
|
||||
}
|
||||
|
||||
#! The type parameters `<T, Us...>` of a declaration, from the current `<`,
|
||||
#! whose bounds are given in a `where` clause after its signature, which gives
|
||||
#! them to the `type_param` nodes.
|
||||
#! @param this The parser.
|
||||
#! @returns The run of `type_param` nodes.
|
||||
def parser::type_params(&mut this): u32 {
|
||||
this.advance();
|
||||
|
||||
val mark := this.mark;
|
||||
while true {
|
||||
# `def(Ts...): R` is given a function type, and names its parameters' types
|
||||
# and its result's.
|
||||
if this.check(tk::kw_def) {
|
||||
this.push(this.type_pattern());
|
||||
break if !this.accept(tk::comma);
|
||||
continue;
|
||||
}
|
||||
|
||||
# `const N: u64` stands for a value each instance is given, of the type written.
|
||||
val valued := this.accept(tk::kw_const);
|
||||
if !this.check(tk::name) {
|
||||
this.fail(ec::ident, valued ? "Expected the name of a constant parameter, as `const N: u64`." : "Expected the name of a type parameter.");
|
||||
break;
|
||||
}
|
||||
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
# `L[N]` is given a fixed array, and names its element's type and its length.
|
||||
if !valued && this.check(tk::lbracket) {
|
||||
this.push(this.array_pattern(token));
|
||||
break if !this.accept(tk::comma);
|
||||
continue;
|
||||
}
|
||||
if valued {
|
||||
if this.accept(tk::spread) {
|
||||
this.report_at(token, ec::token, "A constant parameter stands for one value, so it is not a pack.");
|
||||
}
|
||||
this.expect(tk::colon, "Expected `:` followed by the type of the constant, as `const N: u64`.");
|
||||
this.push(this.make_node(nk::const_param, token, 0, this.type_expr()));
|
||||
break if !this.accept(tk::comma);
|
||||
continue;
|
||||
}
|
||||
|
||||
# `Ts...` is a pack, standing for any number of types, which comes last.
|
||||
val packed := this.accept(tk::spread);
|
||||
if packed && !this.at(tt::angle) {
|
||||
this.report_at(token, ec::token,
|
||||
"A pack takes every type argument after the type parameters before it, so it is the last.");
|
||||
}
|
||||
|
||||
this.push(this.make_node(nk::type_param, token, 0, parser::bit(packed, nf::packed)));
|
||||
break if !this.accept(tk::comma);
|
||||
}
|
||||
|
||||
val params := this.make_run(mark);
|
||||
this.close_angle("Expected `>` to close the type parameters.");
|
||||
return params;
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
#! @file when.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the parsing of `when` blocks, which choose the declarations or
|
||||
#! statements a program has while compiling, and of the conditions they test.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! `when condition { ... }`, holding declarations at the top level of a file and
|
||||
#! statements within a function, followed by `else when` and `else`.
|
||||
#! @param this The parser.
|
||||
#! @param declarations Whether it holds declarations, at the top level of a
|
||||
#! file, rather than statements.
|
||||
#! @returns The `when_block` node.
|
||||
def parser::when_block(&mut this, declarations: bool): u32 {
|
||||
return this.error_here() if !this.enter();
|
||||
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
|
||||
val condition := this.condition();
|
||||
val then := declarations ? this.decl_block() : this.block();
|
||||
var other: u32 = 0;
|
||||
if this.accept(tk::kw_else) {
|
||||
other = this.check(tk::kw_when)
|
||||
? this.when_block(declarations)
|
||||
: declarations ? this.decl_block() : this.block();
|
||||
}
|
||||
|
||||
this.leave();
|
||||
return this.branch(nk::when_block, token, condition, then, other);
|
||||
}
|
||||
|
||||
#! The declarations of a `when` at the top level, as a `block`, which cannot
|
||||
#! import or begin a module.
|
||||
#! @param this The parser.
|
||||
#! @returns The `block` node.
|
||||
def parser::decl_block(&mut this): u32 {
|
||||
if !this.check(tk::lbrace) {
|
||||
this.fail(ec::token, "Expected `{` to begin the declarations.");
|
||||
return this.error_here();
|
||||
}
|
||||
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
|
||||
val mark := this.mark;
|
||||
while !this.check(tk::rbrace) && !this.check(tk::eof) {
|
||||
if this.check(tk::kw_imp) || this.check(tk::kw_mod) || this.check(tk::kw_extern) {
|
||||
this.fail(ec::decl,
|
||||
"Imports and modules cannot be within `when`; a whole section of a file can be declared `module path when condition;`.");
|
||||
} else {
|
||||
this.push(this.declaration());
|
||||
}
|
||||
|
||||
if this.panic {
|
||||
this.synchronize(parser::scope::declarations);
|
||||
}
|
||||
}
|
||||
|
||||
val declarations := this.make_run(mark);
|
||||
this.expect(tk::rbrace, "Expected `}` to close the declarations.");
|
||||
return this.make_node(nk::block, token, declarations);
|
||||
}
|
||||
|
||||
#! A condition on the target, where `&&` binds tighter than `||`.
|
||||
#! @param this The parser.
|
||||
#! @returns The condition's node.
|
||||
def parser::condition(&mut this): u32 {
|
||||
var left := this.condition_and();
|
||||
while this.check(tk::logor) {
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
left = this.make_node(nk::binary, token, left, this.condition_and());
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
def parser::condition_and(&mut this): u32 {
|
||||
var left := this.condition_operand();
|
||||
while this.check(tk::logand) {
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
left = this.make_node(nk::binary, token, left, this.condition_operand());
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
#! A target key compared with a value, as `os == linux`, a negation, or a
|
||||
#! condition in parentheses.
|
||||
#! @param this The parser.
|
||||
#! @returns The operand's node, or an `error` node.
|
||||
def parser::condition_operand(&mut this): u32 {
|
||||
return this.error_here() if !this.enter();
|
||||
|
||||
val token := this.index;
|
||||
var made: u32 = 0;
|
||||
if this.accept(tk::negate) {
|
||||
made = this.make_node(nk::unary, token, this.condition_operand());
|
||||
} else if this.accept(tk::lparen) {
|
||||
made = this.condition();
|
||||
this.expect(tk::rparen, "Expected `)` to close the condition.");
|
||||
} else if !this.check(tk::name) {
|
||||
this.fail(ec::ident, "Expected a target key: `os`, `arch`, or `backend`.");
|
||||
made = this.error_here();
|
||||
} else {
|
||||
made = this.comparison();
|
||||
}
|
||||
|
||||
this.leave();
|
||||
return made;
|
||||
}
|
||||
|
||||
#! `key == value` or `key != value`, from the key.
|
||||
#! @param this The parser.
|
||||
#! @returns The `binary` node.
|
||||
def parser::comparison(&mut this): u32 {
|
||||
val key := this.name_node("Expected a target key: `os`, `arch`, or `backend`.");
|
||||
if !this.check(tk::equals) && !this.check(tk::noteq) {
|
||||
this.fail(ec::token, "Expected `==` or `!=` after the target key.");
|
||||
return this.error_here();
|
||||
}
|
||||
|
||||
val token := this.index;
|
||||
this.advance();
|
||||
val value := this.name_node("Expected the value the key is compared with, as `linux`.");
|
||||
return this.make_node(nk::binary, token, key, value);
|
||||
}
|
||||
Reference in new issue
Block a user