thp/src/syntax/mod.rs

52 lines
1.5 KiB
Rust
Raw Normal View History

use crate::error_handling::{MistiError, SyntaxError};
mod binding;
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 {
///
/// A construct has been found
2023-09-09 01:17:46 +00:00
Ok(TopLevelConstruct),
///
/// 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> {
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
match next_construct(tokens, current_pos) {
SyntaxResult::Ok(module) => Ok(ModuleAST {
bindings: vec![module],
}),
SyntaxResult::None => Err(MistiError::Syntax(SyntaxError {
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,
})),
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
fn next_construct<'a>(tokens: &'a Vec<Token>, current_pos: usize) -> SyntaxResult {
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)
}