forked from katzen-cafe/iowo
107 lines
2.8 KiB
Rust
107 lines
2.8 KiB
Rust
|
use enumset::enum_set;
|
||
|
|
||
|
use crate::lst_parser::{
|
||
|
error::SyntaxError,
|
||
|
grammar::expression::expression,
|
||
|
syntax_kind::{SyntaxKind::*, TokenSet},
|
||
|
CompletedMarker, Parser,
|
||
|
};
|
||
|
|
||
|
const TOP_LEVEL_ITEM_START: TokenSet = enum_set!(DEF_KW | MOD_KW | USE_KW);
|
||
|
|
||
|
pub fn mod_body(p: &mut Parser) {
|
||
|
loop {
|
||
|
if top_level_item(p).is_none() {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn mod_decl(p: &mut Parser) -> Option<CompletedMarker> {
|
||
|
let mod_start = p.start("module");
|
||
|
if !p.eat(MOD_KW) {
|
||
|
return None;
|
||
|
}
|
||
|
|
||
|
let mod_name = p.start("module_name");
|
||
|
if p.eat(IDENT) {
|
||
|
mod_name.complete(p, MODULE_NAME);
|
||
|
} else {
|
||
|
mod_name.error(p, SyntaxError::Expected(vec![IDENT]));
|
||
|
}
|
||
|
|
||
|
let mod_body_marker = p.start("mod_body");
|
||
|
if p.eat(SEMICOLON) {
|
||
|
mod_body_marker.abandon(p);
|
||
|
Some(mod_start.complete(p, MODULE))
|
||
|
} else if p.eat(L_BRACE) {
|
||
|
mod_body(p);
|
||
|
if !p.eat(R_BRACE) {
|
||
|
mod_body_marker
|
||
|
.complete(p, MODULE_BODY)
|
||
|
.precede(p, "unclosed_mod_body_err")
|
||
|
.error(p, SyntaxError::UnclosedModuleBody);
|
||
|
} else {
|
||
|
mod_body_marker.complete(p, MODULE_BODY);
|
||
|
}
|
||
|
Some(mod_start.complete(p, MODULE))
|
||
|
} else {
|
||
|
Some(mod_start.error(p, SyntaxError::Expected(vec![MODULE_BODY])))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn top_level_item(p: &mut Parser) -> Option<CompletedMarker> {
|
||
|
if !TOP_LEVEL_ITEM_START.contains(p.current()) {
|
||
|
return None;
|
||
|
}
|
||
|
def(p).or_else(|| mod_decl(p))
|
||
|
}
|
||
|
|
||
|
fn def(p: &mut Parser) -> Option<CompletedMarker> {
|
||
|
let def_start = p.start("top_level_def");
|
||
|
if !p.eat(DEF_KW) {
|
||
|
def_start.abandon(p);
|
||
|
return None;
|
||
|
}
|
||
|
|
||
|
let def_name = p.start("def_name");
|
||
|
if p.eat(IDENT) {
|
||
|
def_name.complete(p, DEF_NAME);
|
||
|
} else {
|
||
|
def_name.error(p, SyntaxError::Expected(vec![IDENT]));
|
||
|
}
|
||
|
|
||
|
let maybe_expected_eq = p.start("maybe_expect_eq");
|
||
|
if !p.eat(EQ) {
|
||
|
maybe_expected_eq.error(p, SyntaxError::Expected(vec![EQ]));
|
||
|
} else {
|
||
|
maybe_expected_eq.abandon(p);
|
||
|
}
|
||
|
|
||
|
let body = p.start("def_body");
|
||
|
if expression(p, false).is_some() {
|
||
|
body.complete(p, DEF_BODY);
|
||
|
} else {
|
||
|
body.error(p, SyntaxError::Expected(vec![DEF_BODY]));
|
||
|
}
|
||
|
|
||
|
let def = def_start.complete(p, DEF);
|
||
|
Some(if p.eat(SEMICOLON) {
|
||
|
def
|
||
|
} else if TOP_LEVEL_ITEM_START.contains(p.current()) || p.at(EOF) {
|
||
|
def.precede(p, "unterminated_tl_item")
|
||
|
.error(p, SyntaxError::UnterminatedTopLevelItem)
|
||
|
} else {
|
||
|
def.precede(p, "err_unexpected")
|
||
|
.error(p, SyntaxError::Expected(vec![SEMICOLON]))
|
||
|
})
|
||
|
}
|
||
|
|
||
|
fn r#use(p: &mut Parser) -> Option<CompletedMarker> {
|
||
|
if !p.eat(USE_KW) {
|
||
|
return None;
|
||
|
}
|
||
|
|
||
|
todo!()
|
||
|
}
|