thp/src/syntax/mod.rs

48 lines
1.2 KiB
Rust
Raw Normal View History

use crate::ast_types::Binding;
use crate::error_handling::SyntaxError;
2022-11-28 23:33:34 +00:00
use super::token::Token;
2022-11-21 21:58:51 +00:00
mod binding;
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
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
pub fn construct_ast<'a>(tokens: &'a Vec<Token>) -> Result<ModuleAST<'a>, SyntaxError> {
let _token_amount = tokens.len();
let mut current_pos = 0;
2023-01-17 13:04:11 +00:00
match next_construct(tokens, current_pos) {
SyntaxResult::Ok(module) => Ok(ModuleAST {
bindings: vec![module],
}),
SyntaxResult::None => Err(SyntaxError {
reason: String::from("D:"),
}),
SyntaxResult::Err(err) => Err(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
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"),
})
})
}