2023-04-15 22:17:27 +00:00
|
|
|
use crate::error_handling::{MistiError, SyntaxError};
|
2023-03-14 21:10:43 +00:00
|
|
|
|
2023-02-15 21:17:50 +00:00
|
|
|
mod binding;
|
2023-03-14 21:10:43 +00:00
|
|
|
mod expression;
|
2023-09-09 01:17:46 +00:00
|
|
|
mod function_declaration;
|
|
|
|
mod utils;
|
|
|
|
|
2023-09-08 01:50:51 +00:00
|
|
|
pub mod ast;
|
2023-09-08 01:46:11 +00:00
|
|
|
|
|
|
|
use crate::lexic::token::Token;
|
2023-09-09 01:28:53 +00:00
|
|
|
use ast::ModuleAST;
|
2023-01-05 23:20:58 +00:00
|
|
|
|
2023-09-09 01:17:46 +00:00
|
|
|
use self::ast::TopLevelConstruct;
|
2023-01-08 23:09:06 +00:00
|
|
|
|
2023-09-09 01:17:46 +00:00
|
|
|
#[derive(Debug)]
|
2023-09-08 01:32:59 +00:00
|
|
|
pub enum SyntaxResult {
|
2023-03-14 21:10:43 +00:00
|
|
|
///
|
|
|
|
/// A construct has been found
|
2023-09-09 01:17:46 +00:00
|
|
|
Ok(TopLevelConstruct),
|
2023-03-14 21:10:43 +00:00
|
|
|
///
|
|
|
|
/// No construct was found
|
|
|
|
None,
|
|
|
|
///
|
|
|
|
/// A construct was found, but there was an error parsing it
|
|
|
|
Err(SyntaxError),
|
|
|
|
}
|
|
|
|
|
2022-11-28 23:33:34 +00:00
|
|
|
/// Constructs the Misti AST from a vector of tokens
|
2023-09-08 01:32:59 +00:00
|
|
|
pub fn construct_ast<'a>(tokens: &'a Vec<Token>) -> Result<ModuleAST, MistiError> {
|
2023-03-14 21:10:43 +00:00
|
|
|
let _token_amount = tokens.len();
|
2023-09-08 01:50:51 +00:00
|
|
|
let current_pos = 0;
|
2023-01-17 13:04:11 +00:00
|
|
|
|
2023-03-14 21:10:43 +00:00
|
|
|
match next_construct(tokens, current_pos) {
|
|
|
|
SyntaxResult::Ok(module) => Ok(ModuleAST {
|
|
|
|
bindings: vec![module],
|
|
|
|
}),
|
2023-04-14 15:17:03 +00:00
|
|
|
SyntaxResult::None => Err(MistiError::Syntax(SyntaxError {
|
2023-03-15 21:33:00 +00:00
|
|
|
reason: String::from("PARSER couldn't parse any construction"),
|
|
|
|
// FIXME: This should get the position of the _token_ that current_pos points to
|
|
|
|
error_start: current_pos,
|
|
|
|
error_end: current_pos,
|
2023-04-14 15:17:03 +00:00
|
|
|
})),
|
|
|
|
SyntaxResult::Err(err) => Err(MistiError::Syntax(err)),
|
2023-01-17 13:04:11 +00:00
|
|
|
}
|
2022-11-21 21:58:51 +00:00
|
|
|
}
|
2023-01-05 23:20:58 +00:00
|
|
|
|
2023-03-14 21:10:43 +00:00
|
|
|
fn next_construct<'a>(tokens: &'a Vec<Token>, current_pos: usize) -> SyntaxResult {
|
2023-04-15 22:17:27 +00:00
|
|
|
None.or_else(|| binding::try_parse(tokens, current_pos))
|
2023-09-09 01:28:53 +00:00
|
|
|
.or_else(|| function_declaration::try_parse(tokens, current_pos))
|
2023-04-16 23:17:28 +00:00
|
|
|
.unwrap_or_else(|| SyntaxResult::None)
|
2023-03-14 21:10:43 +00:00
|
|
|
}
|