2023-03-14 21:10:43 +00:00
|
|
|
use crate::ast_types::Binding;
|
2023-04-14 15:17:03 +00:00
|
|
|
use crate::error_handling::{SyntaxError, MistiError};
|
2023-03-14 21:10:43 +00:00
|
|
|
|
2022-11-28 23:33:34 +00:00
|
|
|
use super::token::Token;
|
2022-11-21 21:58:51 +00:00
|
|
|
|
2023-02-15 21:17:50 +00:00
|
|
|
mod binding;
|
2023-03-14 21:10:43 +00:00
|
|
|
mod expression;
|
2023-01-23 12:58:53 +00:00
|
|
|
use super::ast_types;
|
2023-01-05 23:20:58 +00:00
|
|
|
|
2023-01-23 12:58:53 +00:00
|
|
|
use ast_types::ModuleAST;
|
2023-01-08 23:09:06 +00:00
|
|
|
|
2023-03-14 21:10:43 +00:00
|
|
|
pub enum SyntaxResult<'a> {
|
|
|
|
///
|
|
|
|
/// A construct has been found
|
|
|
|
Ok(Binding<'a>),
|
|
|
|
///
|
|
|
|
/// 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-04-14 15:17:03 +00:00
|
|
|
pub fn construct_ast<'a>(tokens: &'a Vec<Token>) -> Result<ModuleAST<'a>, MistiError> {
|
2023-03-14 21:10:43 +00:00
|
|
|
let _token_amount = tokens.len();
|
|
|
|
let mut 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 {
|
|
|
|
None.or_else(|| binding::try_parse(tokens, 0))
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
SyntaxResult::Err(SyntaxError {
|
|
|
|
reason: String::from("Unrecognized token"),
|
2023-03-15 21:33:00 +00:00
|
|
|
// FIXME: This should get the position of the _token_ that current_pos points to
|
|
|
|
error_start: current_pos,
|
|
|
|
error_end: current_pos,
|
2023-03-14 21:10:43 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|