130 lines
5.4 KiB
Plaintext
130 lines
5.4 KiB
Plaintext
#! @file parser.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains the parser, which builds the syntax tree of a file from its tokens,
|
|
#! and `parse`, where it begins. What it parses is split across the files of the
|
|
#! `parser` directory, one for each part of the language.
|
|
|
|
import std::containers;
|
|
module sakura::analyzer;
|
|
|
|
|
|
#! The sakura language parser structure.
|
|
#!
|
|
#! This is responsible for taking a freshly lexed input syntax and to structure
|
|
#! it into an abstract syntax tree. It will assure that any syntax issues are
|
|
#! noted and reported to the next stage. The syntax tree produced is checked by
|
|
#! the next compiler step for semantic analysis.
|
|
#!
|
|
#! The parsing functions are split by what they parse across `parser/`: each
|
|
#! returns the index of the node it made, or of an `error` node where it found
|
|
#! none, and 0 where what it parses may be left out and was.
|
|
pub struct parser {
|
|
var syntax: &mut syntax; #! Reads tokens and appends nodes and extras.
|
|
var names: &names; #! The names the tokens were lexed with, which give a name's length.
|
|
var scratch: stack<u32>; #! A scratchpad when the parser needs to hold onto things.
|
|
var depth: u32; #! Cleanly stops heavily nested constructs.
|
|
var file: u32; #! Current file index.
|
|
var index: u32; #! Current token index.
|
|
var kind: tk; #! The kind of the current token, or `eof` at the limit.
|
|
var limit: u32; #! Where the tokens seem to end, short of their end while giving up on a declaration.
|
|
var panic: bool; #! Silences notes until the parser resynchronizes.
|
|
var halted: bool; #! Gave up on the declaration it is within, so notes nothing more of it.
|
|
var begun: u32; #! The token the top-level declaration being parsed begins at.
|
|
var cut: bool; #! Ran into the limit with something left open, while reparsing.
|
|
# Whether a constant argument is being parsed between `<` and `>`, outside any
|
|
# bracket it opens, where `>` closes the arguments rather than comparing.
|
|
var angled: bool;
|
|
# Whether a trait's members are being parsed, and the trait's type parameters,
|
|
# which a member's `where` may bound beside its own, as `self` may be.
|
|
var in_members: bool;
|
|
var member_params: u32;
|
|
# Whether an `ensures`'s condition is being parsed, where `old(e)` is what `e`
|
|
# was where it was reached.
|
|
var in_ensures: bool;
|
|
# Whether the type about to be parsed is what `as` or `is` takes, where a `?`
|
|
# after it goes on with the expression's ternary rather than a type's.
|
|
var plain_type: bool;
|
|
var type_argument: bool; #! Whether the type `type_expr` reads next is a type argument, where a condition is one only where a type follows its `?`, as `T is integer ? 4 : 8` is a constant.
|
|
|
|
# Where the parser puts its notes, which `max_notes` bounds: kept with the
|
|
# lexer's, or heard as each is made, as where another thread reports them.
|
|
pub var sink: note_sink; #! Where the parser puts its notes.
|
|
}
|
|
|
|
#! The deepest the parser nests before it gives up on the declaration.
|
|
pub const parser::max_depth: u32 = 100;
|
|
|
|
#! The most notes the parser makes for one file.
|
|
pub const parser::max_notes: u32 = 100;
|
|
|
|
#! Creates a new parser from the provided information.
|
|
#! @param syntax Where the parser will store the nodes and extras that it
|
|
#! creates.
|
|
#! @param names The names the tokens were lexed with.
|
|
#! @param notes Where the parser will store the notes.
|
|
#! @param file The index of the file that it's parsing.
|
|
#! @returns The instantiated parser.
|
|
pub def parser::new(
|
|
syntax: &mut syntax,
|
|
names: &names,
|
|
notes: &mut vector<note>,
|
|
file: u32
|
|
): parser {
|
|
return parser {
|
|
syntax = syntax,
|
|
names = names,
|
|
sink = note_sink::new(notes, parser::max_notes),
|
|
scratch = stack::<u32>::new(),
|
|
file = file
|
|
};
|
|
}
|
|
|
|
#! Parses the token stream.
|
|
#!
|
|
#! This generates the abstract syntax tree within the `syntax` variable, such
|
|
#! that when the parser ends, the syntax can be evaluated by a checker. The
|
|
#! token stream must end with `tk::eof`.
|
|
#! @param this The parser.
|
|
#! @returns The index of the `document` node, the first the parser makes.
|
|
pub def parser::parse(&mut this): u32 {
|
|
this.syntax.reserve_for_tokens();
|
|
this.limit = this.syntax.token_count as u32;
|
|
this.sync();
|
|
|
|
# The run is made before it is stored: making it appends nodes, which may move
|
|
# the vector a reference into it was taken from.
|
|
val document := this.make_node(nk::document, this.index);
|
|
val declared := this.declarations(this.limit);
|
|
this.syntax.node_lefts[document] = declared;
|
|
this.syntax.set_node_end(document, this.limit - 1);
|
|
this.syntax.whole_nodes = this.syntax.node_count;
|
|
this.syntax.whole_extras = this.syntax.extras.length;
|
|
this.syntax.fit_nodes();
|
|
return document;
|
|
}
|
|
|
|
#! The declarations from the current token through the limit, each recovered
|
|
#! from on its own.
|
|
#! @param this The parser.
|
|
#! @param limit The last token to parse.
|
|
#! @returns Their run.
|
|
def parser::declarations(&mut this, limit: u32): u32 {
|
|
val mark := this.mark;
|
|
while !this.check(tk::eof) {
|
|
this.begun = this.index;
|
|
this.push(this.declaration());
|
|
if this.halted {
|
|
this.resume(limit);
|
|
}
|
|
|
|
if this.panic {
|
|
this.synchronize(parser::scope::document);
|
|
}
|
|
}
|
|
|
|
return this.make_run(mark);
|
|
}
|