137 lines
7.7 KiB
Plaintext
137 lines
7.7 KiB
Plaintext
#! @file node.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains the node of a syntax tree, as gathered from a `syntax`, with `nk`,
|
|
#! the kinds of node, and `nf`, the flags a node may hold.
|
|
module sakura::analyzer;
|
|
|
|
#! A node of a syntax tree: its kind, the token it says what it is by, its two
|
|
#! slots, and the last token of what it was parsed from.
|
|
#!
|
|
#! A syntax keeps each field of its nodes in a vector of its own, and this is
|
|
#! the value `syntax::node` gathers from them. What the slots hold is what the
|
|
#! kind's shape says. Node 0 stands for no node, wherever an index may be
|
|
#! absent.
|
|
pub struct node {
|
|
val kind: nk; #! Its kind.
|
|
val token: u32; #! The token it says what it is by.
|
|
val left: u32; #! Its first slot, which its shape says the meaning of.
|
|
val right: u32; #! Its second slot.
|
|
val end: u32; #! The last token of what it was parsed from.
|
|
}
|
|
|
|
#! Defines the different types of nodes.
|
|
#!
|
|
#! What the `left` and `right` of each kind hold is described by its entry in
|
|
#! `shapes`, which lists the kinds in this order. `count` is not a kind: it is
|
|
#! how many there are, which sizes that table.
|
|
pub enum nk : u8 {
|
|
# Structure
|
|
error, #! Where the parser made no sense of the source, holding what it did parse. Also node 0, which stands for "no node" wherever an index may be absent.
|
|
document, #! A file, whose run is its declarations.
|
|
block, #! A block, whose run is its statements.
|
|
|
|
# Declarations
|
|
import_decl, #! `import`, of the module's `path`.
|
|
module_decl, #! `module`, of its `path` and any `when` condition.
|
|
extern_import, #! `extern import`, of the library's `string` and any `when` condition.
|
|
extern_module, #! `extern module`, of its `path` and any `when` condition: a section whose functions without a body, structs, and unions are C's.
|
|
when_block, #! At the top level, of declarations, or within a function, of statements.
|
|
function, #! A function, with its record and flags.
|
|
param, #! A parameter: its type, and a record of its default and flags.
|
|
variable, #! A variable or constant, with its record and flags.
|
|
field, #! A field of a struct or union, with its record and flags.
|
|
spread, #! A struct spread into another, as `...pair`, with its renames.
|
|
rename, #! A field a spread renames: its token the old name, and the node the new.
|
|
property, #! A property, with its record and its accessors.
|
|
accessor, #! A property's `get` or `set`: its parameters, and a record of its body and flags.
|
|
struct_decl, #! A struct, with its record and fields.
|
|
union_decl, #! A union, with its record and fields.
|
|
trait_decl, #! A trait, with its record and members.
|
|
assoc_type, #! A trait's associated type, with its record and default.
|
|
enumeration, #! An enum, with its record and members.
|
|
enumerator, #! A member of an enum: its attributes and its value.
|
|
alias, #! A type alias, with its record and the type it aliases.
|
|
attribute, #! An attribute: its `type_name` and its field inits.
|
|
type_param, #! A type parameter: its bounds, and whether it is a pack.
|
|
const_param, #! A constant parameter, `const N: u64`: its bounds, which it may not have, and its type.
|
|
type_pattern, #! A pattern parameter, `def(Ts...): R`: the type parameters its parameter types name, and the one its result names.
|
|
array_pattern, #! A pattern parameter, `L[N]`: the type parameter its element is, and the constant parameter its length is.
|
|
|
|
# Types
|
|
segment, #! A segment of a type's name, with its type arguments or parameters.
|
|
type_name, #! A type's name, as its segments.
|
|
type_ref, #! A reference type: what it refers to, and whether it writes.
|
|
type_ptr, #! A pointer type: what it points at, and whether it writes.
|
|
type_fixed, #! A fixed array type: its element, and its length.
|
|
type_func, #! A function type: its parameter types, and its result.
|
|
type_spread, #! `T...`: a function type's variadic last parameter, or a pack passed on among type arguments.
|
|
type_of, #! `type_of(d)`, the type a declaration of reflection names.
|
|
|
|
# Statements
|
|
if_stmt, #! `if`: its condition, and a record of its branches.
|
|
if_const, #! `if const`: its condition, and a record of its branches.
|
|
while_stmt, #! `while`: its condition and body.
|
|
for_stmt, #! `for`, with its record and body.
|
|
match_stmt, #! A `match` statement: its subject and arms.
|
|
match_arm, #! An arm of a match: its patterns, which are none for `else`, and its body.
|
|
return_stmt, #! `return`, with the value it gives.
|
|
break_stmt, #! `break`.
|
|
continue_stmt, #! `continue`.
|
|
expr_stmt, #! An expression as a statement.
|
|
contract, #! `expects`, `asserts`, or `ensures`: its condition, and a record of the name it gives what is returned, its text, and which it is.
|
|
|
|
# Expressions
|
|
name, #! A name, which its token is.
|
|
path, #! A path of segments, as `std::io`.
|
|
number, #! A number, which its token is.
|
|
character, #! A character, which its token is.
|
|
string, #! Text, which its token is.
|
|
istring, #! Interpolated text: its pieces of text and its holes.
|
|
boolean, #! `true` or `false`.
|
|
null_lit, #! `null`.
|
|
unary, #! A prefix operator, its token, on its operand: also `*p` and `...xs`.
|
|
postfix, #! `++` or `--` after their operand.
|
|
reference, #! `&` or `&mut` before their operand.
|
|
binary, #! A binary operator, its token, on its left and right.
|
|
assign, #! An assignment, plain or compound: its target and value.
|
|
ternary, #! `c ? a : b`: its condition, and a record of its branches.
|
|
fallback, #! `x else y`: its value, and a record of its error's name and the fallback.
|
|
call, #! A call: its callee and arguments.
|
|
type_args, #! A value given type arguments after it, as `value_of(d)::<i64>`.
|
|
index, #! `x[i]`: its object and index.
|
|
slice, #! `x[a:b]`: its object, and a record of its start and count.
|
|
member, #! `x.name`, with any type arguments.
|
|
member_of, #! `x.(d)`: its object and the declaration of reflection.
|
|
cast, #! `x as T`: its value and type.
|
|
is_test, #! `x is T`, which is a bound test where `x` names a type, as `T is copyable`.
|
|
construct, #! A struct literal: its `type_name` and field inits.
|
|
field_init, #! A field given in a struct literal, whose token is the field where it is named.
|
|
array, #! An array literal, as its elements.
|
|
lambda, #! An anonymous function: its parameters, and a record of its result and body.
|
|
match_value, #! A `match` used as a value: its subject and arms.
|
|
range, #! `a..b`, or `a..=b` where its token is `..=`: its start and end.
|
|
old_value, #! `old(e)` within an `ensures`: what `e` was where the `ensures` was reached.
|
|
undefined, #! `undefined` as a variable's value: its memory left as it is, which nothing zeroes.
|
|
|
|
count, #! How many kinds there are, which sizes `shapes`; not a kind itself.
|
|
}
|
|
|
|
#! The bits a node's `flags` slot holds, where its kind has one.
|
|
pub enum nf : u32 {
|
|
exported = 0x0001, #! `pub`.
|
|
mutable = 0x0002, #! `var`, `&mut`, `*mut`, `&mut this`, and `for &mut x`.
|
|
constant = 0x0004, #! `const`, `const def`, and `for const`.
|
|
reference = 0x0008, #! `for &x`.
|
|
variadic = 0x0010, #! A parameter `name: T...`.
|
|
packed = 0x0020, #! A type parameter `Ts...`.
|
|
negated = 0x0040, #! `is not`.
|
|
computed = 0x0080, #! `x is (d)`.
|
|
function = 0x0100, #! A local declared with `def`, whose value is a `lambda`.
|
|
named = 0x0200, #! A `field_init` naming its field, as `x = 1`.
|
|
inclusive = 0x0400, #! A `for` loop's range written `a..=b`, which includes its end.
|
|
implied = 0x0800, #! A parameter of an accessor the syntax leaves out: `this` of a `get` or `set`, and a `set`'s `value`.
|
|
}
|