2023-09-22 01:34:51 +00:00
|
|
|
use std::f32::consts::E;
|
|
|
|
|
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-09-21 00:53:46 +00:00
|
|
|
mod block;
|
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
|
|
|
|
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-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-17 22:58:56 +00:00
|
|
|
Ok(TopLevelDeclaration, usize),
|
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),
|
|
|
|
}
|
|
|
|
|
2023-09-21 00:53:46 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ParseResult<A, B> {
|
|
|
|
Ok(A, usize),
|
|
|
|
Err(SyntaxError),
|
|
|
|
Mismatch(B),
|
|
|
|
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(
|
|
|
|
|| match function_declaration::try_parse(tokens, current_pos) {
|
|
|
|
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-03-14 21:10:43 +00:00
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|