refactor: organize files for parsing expressions
This commit is contained in:
parent
0393995a49
commit
7332a2e13f
@ -1,19 +0,0 @@
|
|||||||
use super::{ast::Expression, ParsingResult};
|
|
||||||
use crate::lexic::token::Token;
|
|
||||||
|
|
||||||
mod comparison;
|
|
||||||
mod equality;
|
|
||||||
mod factor;
|
|
||||||
pub mod function_call_expr;
|
|
||||||
mod primary;
|
|
||||||
mod term;
|
|
||||||
mod unary;
|
|
||||||
|
|
||||||
/// Expression is defined in the grammar.
|
|
||||||
pub fn try_parse(tokens: &Vec<Token>, pos: usize) -> ParsingResult<Expression> {
|
|
||||||
// TODO: This must be newline/indentation aware
|
|
||||||
equality::try_parse(tokens, pos)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {}
|
|
@ -3,6 +3,7 @@ use crate::{
|
|||||||
lexic::token::{Token, TokenType},
|
lexic::token::{Token, TokenType},
|
||||||
syntax::{
|
syntax::{
|
||||||
ast::{functions::ArgumentsList, Expression},
|
ast::{functions::ArgumentsList, Expression},
|
||||||
|
parseable::Parseable,
|
||||||
utils::parse_token_type,
|
utils::parse_token_type,
|
||||||
ParsingError, ParsingResult,
|
ParsingError, ParsingResult,
|
||||||
},
|
},
|
||||||
@ -22,15 +23,14 @@ pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> ParsingResult<Argume
|
|||||||
|
|
||||||
let mut arguments = Vec::<Expression>::new();
|
let mut arguments = Vec::<Expression>::new();
|
||||||
loop {
|
loop {
|
||||||
let (next_expression, next_pos) =
|
let (next_expression, next_pos) = match Expression::try_parse(tokens, current_pos) {
|
||||||
match super::super::expression::try_parse(tokens, current_pos) {
|
Ok((expression, next_pos)) => (expression, next_pos),
|
||||||
Ok((expression, next_pos)) => (expression, next_pos),
|
Err(ParsingError::Err(error)) => {
|
||||||
Err(ParsingError::Err(error)) => {
|
// TODO: Write a more detailed error
|
||||||
// TODO: Write a more detailed error
|
return Err(ParsingError::Err(error));
|
||||||
return Err(ParsingError::Err(error));
|
}
|
||||||
}
|
_ => break,
|
||||||
_ => break,
|
};
|
||||||
};
|
|
||||||
current_pos = next_pos;
|
current_pos = next_pos;
|
||||||
|
|
||||||
arguments.push(next_expression);
|
arguments.push(next_expression);
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use crate::error_handling::MistiError;
|
use crate::error_handling::MistiError;
|
||||||
|
|
||||||
mod expression;
|
|
||||||
mod functions;
|
mod functions;
|
||||||
mod parseable;
|
mod parseable;
|
||||||
mod parsers;
|
mod parsers;
|
||||||
|
@ -96,7 +96,7 @@ mod tests {
|
|||||||
fn should_parse_empty_block() {
|
fn should_parse_empty_block() {
|
||||||
let tokens = get_tokens(&String::from("{}")).unwrap();
|
let tokens = get_tokens(&String::from("{}")).unwrap();
|
||||||
let (block, next_pos) = Block::try_parse(&tokens, 0).unwrap();
|
let (block, next_pos) = Block::try_parse(&tokens, 0).unwrap();
|
||||||
|
|
||||||
assert_eq!(2, next_pos);
|
assert_eq!(2, next_pos);
|
||||||
assert_eq!(0, block.members.len())
|
assert_eq!(0, block.members.len())
|
||||||
}
|
}
|
||||||
@ -114,7 +114,7 @@ mod tests {
|
|||||||
BlockMember::Stmt(Statement::FnDecl(f)) => {
|
BlockMember::Stmt(Statement::FnDecl(f)) => {
|
||||||
assert_eq!(f.identifier.value, "f");
|
assert_eq!(f.identifier.value, "f");
|
||||||
}
|
}
|
||||||
_ => panic!("Expected a function declaration, got {:?}", member)
|
_ => panic!("Expected a function declaration, got {:?}", member),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,7 +131,7 @@ mod tests {
|
|||||||
BlockMember::Stmt(Statement::FnDecl(f)) => {
|
BlockMember::Stmt(Statement::FnDecl(f)) => {
|
||||||
assert_eq!(f.identifier.value, "f");
|
assert_eq!(f.identifier.value, "f");
|
||||||
}
|
}
|
||||||
_ => panic!("Expected a function declaration, got {:?}", member)
|
_ => panic!("Expected a function declaration, got {:?}", member),
|
||||||
}
|
}
|
||||||
|
|
||||||
let member = &block.members[1];
|
let member = &block.members[1];
|
||||||
@ -139,7 +139,7 @@ mod tests {
|
|||||||
BlockMember::Stmt(Statement::FnDecl(f)) => {
|
BlockMember::Stmt(Statement::FnDecl(f)) => {
|
||||||
assert_eq!(f.identifier.value, "g");
|
assert_eq!(f.identifier.value, "g");
|
||||||
}
|
}
|
||||||
_ => panic!("Expected a function declaration, got {:?}", member)
|
_ => panic!("Expected a function declaration, got {:?}", member),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
use crate::{
|
|
||||||
lexic::token::Token,
|
|
||||||
syntax::{
|
|
||||||
ast::Expression,
|
|
||||||
expression,
|
|
||||||
parseable::{Parseable, ParsingResult},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
impl<'a> Parseable<'a> for Expression<'a> {
|
|
||||||
type Item = Expression<'a>;
|
|
||||||
|
|
||||||
fn try_parse(tokens: &'a Vec<Token>, current_pos: usize) -> ParsingResult<'a, Self::Item> {
|
|
||||||
expression::try_parse(tokens, current_pos)
|
|
||||||
}
|
|
||||||
}
|
|
22
src/syntax/parsers/expression/mod.rs
Normal file
22
src/syntax/parsers/expression/mod.rs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
use super::super::{ast::Expression, ParsingResult};
|
||||||
|
use crate::{lexic::token::Token, syntax::parseable::Parseable};
|
||||||
|
|
||||||
|
mod comparison;
|
||||||
|
mod equality;
|
||||||
|
mod factor;
|
||||||
|
pub mod function_call_expr;
|
||||||
|
mod primary;
|
||||||
|
mod term;
|
||||||
|
mod unary;
|
||||||
|
|
||||||
|
impl<'a> Parseable<'a> for Expression<'a> {
|
||||||
|
type Item = Expression<'a>;
|
||||||
|
|
||||||
|
fn try_parse(tokens: &'a Vec<Token>, current_pos: usize) -> ParsingResult<'a, Self::Item> {
|
||||||
|
// TODO: This must be newline/indentation aware
|
||||||
|
equality::try_parse(tokens, current_pos)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {}
|
@ -1,7 +1,8 @@
|
|||||||
use super::super::utils::Tokenizer;
|
|
||||||
use crate::{
|
use crate::{
|
||||||
lexic::token::{Token, TokenType},
|
lexic::token::{Token, TokenType},
|
||||||
syntax::{ast::Expression, ParsingError, ParsingResult},
|
syntax::{
|
||||||
|
ast::Expression, parseable::Parseable, utils::Tokenizer, ParsingError, ParsingResult,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// This grammar may not be up to date. Refer to the spec for the latest grammar.
|
/// This grammar may not be up to date. Refer to the spec for the latest grammar.
|
||||||
@ -27,7 +28,7 @@ pub fn try_parse(tokens: &Vec<Token>, pos: usize) -> ParsingResult<Expression> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn parse_parenthesized_expression(tokens: &Vec<Token>, pos: usize) -> ParsingResult<Expression> {
|
fn parse_parenthesized_expression(tokens: &Vec<Token>, pos: usize) -> ParsingResult<Expression> {
|
||||||
let expression = super::try_parse(tokens, pos + 1);
|
let expression = Expression::try_parse(tokens, pos + 1);
|
||||||
match expression {
|
match expression {
|
||||||
Ok((expression, next_pos)) => match tokens.get(next_pos) {
|
Ok((expression, next_pos)) => match tokens.get(next_pos) {
|
||||||
Some(token) => match token.token_type {
|
Some(token) => match token.token_type {
|
@ -1,6 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
lexic::token::Token,
|
lexic::token::Token,
|
||||||
syntax::{ast::Expression, ParsingError, ParsingResult},
|
syntax::{ast::Expression, parseable::Parseable, ParsingError, ParsingResult},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::function_call_expr;
|
use super::function_call_expr;
|
||||||
@ -14,7 +14,7 @@ use super::function_call_expr;
|
|||||||
pub fn try_parse(tokens: &Vec<Token>, pos: usize) -> ParsingResult<Expression> {
|
pub fn try_parse(tokens: &Vec<Token>, pos: usize) -> ParsingResult<Expression> {
|
||||||
match tokens.get(pos) {
|
match tokens.get(pos) {
|
||||||
Some(token) if token.value == "!" || token.value == "-" => {
|
Some(token) if token.value == "!" || token.value == "-" => {
|
||||||
match super::try_parse(tokens, pos + 1) {
|
match Expression::try_parse(tokens, pos + 1) {
|
||||||
Ok((expression, next_pos)) => Ok((
|
Ok((expression, next_pos)) => Ok((
|
||||||
Expression::UnaryOperator(&token.value, Box::new(expression)),
|
Expression::UnaryOperator(&token.value, Box::new(expression)),
|
||||||
next_pos,
|
next_pos,
|
Loading…
Reference in New Issue
Block a user