This commit is contained in:
Araozu 2023-10-01 18:41:00 -05:00
parent 971b9d9516
commit 9af450eaa0
6 changed files with 18 additions and 64 deletions

View File

@ -35,7 +35,12 @@ pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> ParseResult<Function
}; };
current_pos = next_pos; current_pos = next_pos;
ParseResult::Ok(FunctionCall { identifier: Box::new(identifier.value.clone()) }, current_pos) ParseResult::Ok(
FunctionCall {
identifier: Box::new(identifier.value.clone()),
},
current_pos,
)
} }
#[cfg(test)] #[cfg(test)]

View File

@ -4,7 +4,7 @@ use crate::{
utils::Result3, utils::Result3,
}; };
use super::{ use super::super::{
ast::FunctionDeclaration, ast::FunctionDeclaration,
block::parse_block, block::parse_block,
params_list::parse_params_list, params_list::parse_params_list,

View File

@ -1,2 +1,3 @@
pub mod arguments_list; pub mod arguments_list;
pub mod function_call; pub mod function_call;
pub mod function_declaration;

View File

@ -38,7 +38,14 @@ return type = ;
### Block ### Block
```ebnf ```ebnf
block = "{", "}" block = "{", (statement, (new line, statement)*)?, "}"
```
### Statement
```ebnf
statement = function call
``` ```

View File

@ -3,7 +3,6 @@ use crate::error_handling::{MistiError, SyntaxError};
mod binding; mod binding;
mod block; mod block;
mod expression; mod expression;
mod function_declaration;
mod functions; mod functions;
mod params_list; mod params_list;
mod utils; mod utils;
@ -15,19 +14,6 @@ use ast::ModuleAST;
use self::ast::TopLevelDeclaration; use self::ast::TopLevelDeclaration;
#[derive(Debug)]
pub enum SyntaxResult {
///
/// A construct has been found
Ok(TopLevelDeclaration, usize),
///
/// No construct was found
None,
///
/// A construct was found, but there was an error parsing it
Err(SyntaxError),
}
#[derive(Debug)] #[derive(Debug)]
pub enum ParseResult<A, B> { pub enum ParseResult<A, B> {
/// The parsing was a success /// The parsing was a success
@ -84,7 +70,7 @@ fn next_construct<'a>(
current_pos: usize, current_pos: usize,
) -> ParseResult<TopLevelDeclaration, ()> { ) -> ParseResult<TopLevelDeclaration, ()> {
None.or_else( None.or_else(
|| match function_declaration::try_parse(tokens, current_pos) { || match functions::function_declaration::try_parse(tokens, current_pos) {
ParseResult::Ok(declaration, next_pos) => Some(ParseResult::Ok( ParseResult::Ok(declaration, next_pos) => Some(ParseResult::Ok(
TopLevelDeclaration::FunctionDeclaration(declaration), TopLevelDeclaration::FunctionDeclaration(declaration),
next_pos, next_pos,

View File

@ -4,7 +4,7 @@ use crate::{
utils::Result3, utils::Result3,
}; };
use super::{ParseResult, SyntaxResult}; use super::ParseResult;
/// Expects the token at `pos` to be of type `token_type` /// Expects the token at `pos` to be of type `token_type`
pub fn try_token_type(tokens: &Vec<Token>, pos: usize, token_type: TokenType) -> Result3<&Token> { pub fn try_token_type(tokens: &Vec<Token>, pos: usize, token_type: TokenType) -> Result3<&Token> {
@ -58,48 +58,3 @@ pub fn try_operator(tokens: &Vec<Token>, pos: usize, operator: String) -> Result
None => Result3::None, None => Result3::None,
} }
} }
pub fn _try_operator_w<'a>(
tokens: &'a Vec<Token>,
pos: usize,
operator: String,
error_message: String,
prev_token: &Token,
) -> Result<(&'a Token, usize), Option<SyntaxResult>> {
let mut current_pos = pos;
// Ignore all whitespace and newlines
while let Some(t) = tokens.get(current_pos) {
if t.token_type == TokenType::INDENT
|| t.token_type == TokenType::DEDENT
|| t.token_type == TokenType::NewLine
{
current_pos += 1;
} else {
break;
}
}
match tokens.get(current_pos) {
Some(t) if t.token_type == TokenType::Operator && t.value == operator => {
Ok((t, current_pos + 1))
}
Some(t) if t.token_type == TokenType::NewLine || t.token_type == TokenType::EOF => {
Err(Some(SyntaxResult::Err(SyntaxError {
reason: error_message,
error_start: prev_token.position,
error_end: prev_token.get_end_position(),
})))
}
Some(t) => Err(Some(SyntaxResult::Err(SyntaxError {
reason: error_message,
error_start: t.position,
error_end: t.get_end_position(),
}))),
None => Err(Some(SyntaxResult::Err(SyntaxError {
reason: error_message,
error_start: prev_token.position,
error_end: prev_token.get_end_position(),
}))),
}
}