164 lines
5.3 KiB
Plaintext
164 lines
5.3 KiB
Plaintext
#! @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, parser::keys_expected);
|
|
made = this.error_here();
|
|
} else {
|
|
made = this.comparison();
|
|
}
|
|
|
|
this.leave();
|
|
return made;
|
|
}
|
|
|
|
#! What a note says where a condition has no target key.
|
|
const parser::keys_expected: strview =
|
|
"Expected a target key: `os`, `arch`, `backend`, or `compiler`.";
|
|
|
|
#! `key == value` or `key != value`, from the key, or an order, `<`, `<=`, `>`,
|
|
#! or `>=`, which `compiler` is compared by. The value is a name, as `linux`,
|
|
#! or a version, as `1.2.0`.
|
|
#! @param this The parser.
|
|
#! @returns The `binary` node.
|
|
def parser::comparison(&mut this): u32 {
|
|
val key := this.name_node(parser::keys_expected);
|
|
if !this.at_comparison() {
|
|
this.fail(ec::token, "Expected `==`, `!=`, `<`, `<=`, `>`, or `>=` after the target key.");
|
|
return this.error_here();
|
|
}
|
|
|
|
val token := this.index;
|
|
this.advance();
|
|
return this.make_node(nk::binary, token, key, this.version_node())
|
|
if this.check(tk::signed) || this.check(tk::float);
|
|
val value := this.name_node("Expected the value the key is compared with, as `linux` or `1.2.0`.");
|
|
return this.make_node(nk::binary, token, key, value);
|
|
}
|
|
|
|
#! Whether the current token is one a condition compares by.
|
|
#! @param this The parser.
|
|
#! @returns Whether it is.
|
|
def parser::at_comparison(&this): bool {
|
|
return this.check(tk::equals) || this.check(tk::noteq)
|
|
|| this.check(tk::less) || this.check(tk::lesseq)
|
|
|| this.check(tk::more) || this.check(tk::moreeq);
|
|
}
|
|
|
|
#! A version, as `1.2.0`, which the lexer makes numbers and `.`s of: one
|
|
#! `number` node whose tokens are all of them, which the checker reads as it is
|
|
#! written.
|
|
#! @param this The parser.
|
|
#! @returns The `number` node.
|
|
def parser::version_node(&mut this): u32 {
|
|
val token := this.index;
|
|
while this.check(tk::signed) || this.check(tk::float) || this.check(tk::dot) {
|
|
this.advance();
|
|
}
|
|
return this.make_node(nk::number, token);
|
|
}
|