thp/src/syntax/mod.rs

128 lines
3.8 KiB
Rust
Raw Normal View History

use crate::error_handling::{MistiError, SyntaxError};
mod binding;
2023-09-21 00:53:46 +00:00
mod block;
mod expression;
2023-10-01 22:43:59 +00:00
mod functions;
2023-09-22 01:52:55 +00:00
mod params_list;
2023-09-09 01:17:46 +00:00
mod utils;
2023-09-08 01:50:51 +00:00
pub mod ast;
2023-09-08 01:46:11 +00:00
2023-09-17 22:58:56 +00:00
use crate::lexic::token::{Token, TokenType};
2023-09-09 01:28:53 +00:00
use ast::ModuleAST;
2023-01-05 23:20:58 +00:00
2023-09-17 22:58:56 +00:00
use self::ast::TopLevelDeclaration;
2023-01-08 23:09:06 +00:00
2023-09-21 00:53:46 +00:00
#[derive(Debug)]
pub enum ParseResult<A, B> {
2023-10-01 22:37:19 +00:00
/// The parsing was a success
2023-09-21 00:53:46 +00:00
Ok(A, usize),
2023-10-01 22:37:19 +00:00
/// The parsing failed past a point of no return.
2023-10-01 22:43:59 +00:00
///
2023-10-01 22:37:19 +00:00
/// For example, when parsing a function declaration
/// the `fun` token is found, but then no identifier
2023-09-21 00:53:46 +00:00
Err(SyntaxError),
2023-10-01 22:37:19 +00:00
/// A construct different from the one expected was found
2023-09-21 00:53:46 +00:00
Mismatch(B),
2023-10-01 22:37:19 +00:00
/// No construct was found
2023-09-21 00:53:46 +00:00
Unmatched,
}
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-09-17 22:58:56 +00:00
let mut top_level_declarations = Vec::new();
let token_amount = tokens.len();
let mut current_pos = 0;
// Minus one because the last token is always EOF
while current_pos < token_amount - 1 {
// Ignore newlines
if tokens[current_pos].token_type == TokenType::NewLine {
current_pos += 1;
continue;
}
match next_construct(tokens, current_pos) {
2023-09-22 01:34:51 +00:00
ParseResult::Ok(module, next_pos) => {
2023-09-17 22:58:56 +00:00
top_level_declarations.push(module);
current_pos = next_pos;
}
2023-09-22 01:34:51 +00:00
ParseResult::Err(err) => return Err(MistiError::Syntax(err)),
_ => {
2023-09-17 22:58:56 +00:00
return 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,
}));
}
}
2023-01-17 13:04:11 +00:00
}
2023-09-17 22:58:56 +00:00
Ok(ModuleAST {
declarations: top_level_declarations,
})
2022-11-21 21:58:51 +00:00
}
2023-01-05 23:20:58 +00:00
2023-09-22 01:34:51 +00:00
fn next_construct<'a>(
tokens: &'a Vec<Token>,
current_pos: usize,
) -> ParseResult<TopLevelDeclaration, ()> {
None.or_else(
2023-10-01 23:41:00 +00:00
|| match functions::function_declaration::try_parse(tokens, current_pos) {
2023-09-22 01:34:51 +00:00
ParseResult::Ok(declaration, next_pos) => Some(ParseResult::Ok(
TopLevelDeclaration::FunctionDeclaration(declaration),
next_pos,
)),
ParseResult::Err(err) => Some(ParseResult::Err(err)),
_ => None,
},
)
.unwrap_or_else(|| ParseResult::Unmatched)
}
2023-09-17 22:58:56 +00:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_parse_top_level_construct_with_trailing_newline() {
let input = String::from("fun f1(){}\n");
let tokens = crate::lexic::get_tokens(&input).unwrap();
let declarations = construct_ast(&tokens).unwrap().declarations;
assert_eq!(declarations.len(), 1);
match declarations.get(0).unwrap() {
TopLevelDeclaration::Binding(_) => panic!("Expected a function declaration"),
TopLevelDeclaration::FunctionDeclaration(_f) => {
assert!(true)
}
}
}
#[test]
fn should_parse_2_top_level_construct() {
let input = String::from("fun f1(){} fun f2() {}");
let tokens = crate::lexic::get_tokens(&input).unwrap();
let declarations = construct_ast(&tokens).unwrap().declarations;
assert_eq!(declarations.len(), 2);
match declarations.get(0).unwrap() {
TopLevelDeclaration::Binding(_) => panic!("Expected a function declaration"),
TopLevelDeclaration::FunctionDeclaration(_f) => {
assert!(true)
}
}
match declarations.get(1).unwrap() {
TopLevelDeclaration::Binding(_) => panic!("Expected a function declaration"),
TopLevelDeclaration::FunctionDeclaration(_f) => {
assert!(true)
}
}
}
}