#! @file programs.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains what says which program a file belongs to. A file declaring `main` #! begins a program, which holds it and every file declaring none, so an #! editor whose workspace holds several, as a program and the program testing #! it, checks one of them at a time. module sakura::analyzer; #! Whether a parsed file declares `main` outside any module, beginning a #! program. A `main` in any branch of a `when` counts, whatever the target, and #! one after a `module` is that module's and does not. A file whose syntax has #! mistakes counts by what parsed. #! @param this The file's syntax. #! @param root Its `document` node. #! @param main The ID of the name `main`. #! @returns Whether it does. pub def syntax::declares_main(&this, root: u32, main: u32): bool { var found := false; this.main_within(this.node_lefts[root], main, &mut found); return found; } #! Whether a run of top-level declarations declares `main` before a `module`. #! @param this The file's syntax. #! @param run The run. #! @param main The ID of the name `main`. #! @param found Where whether one does is written. #! @returns Whether a `module` ended the run, so nothing after it counts. def syntax::main_within(&this, run: u32, main: u32, found: &mut bool): bool { for &node in this.run_view(run) { match this.node_kinds[node] { nk::module_decl, nk::extern_module => return true; nk::function => { if this.token_names[this.node_tokens[node]] == main && this.record(node, function_slot::receiver) == 0 { found = true; } } nk::when_block => { # Each branch is a block of declarations, or another `when`. var branch := this.record(node, if_slot::then); var other := this.record(node, if_slot::other); var steps := 0; while steps < 64 { steps++; if branch != 0 && this.node_kinds[branch] == nk::block { return true if this.main_within(this.node_lefts[branch], main, found); } break if other == 0; if this.node_kinds[other] == nk::when_block { branch = this.record(other, if_slot::then); other = this.record(other, if_slot::other); } else { branch = other; other = 0; } } } else => {} } } return false; } #! Whether any note made of a file is a mistake of its syntax, which the lexer #! or the parser made, so what the checker says of it would mislead. #! @param this The note. #! @returns Whether it is. pub val note::is_syntax: bool { get { return match this.code { ec::syntax, ec::token, ec::expr, ec::ident, ec::kind, ec::decl, ec::escape, ec::char, ec::block => true, else => false }; } }