From c9dffa7713e86059225060df050b30276dd47ff8 Mon Sep 17 00:00:00 2001 From: Teascade Date: Sun, 21 Jun 2020 01:45:09 +0300 Subject: [PATCH] Split parser.rs into two files --- src/{parser.rs => parser/mod.rs} | 106 +------------------------------ src/parser/parsed_reid.rs | 103 ++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 103 deletions(-) rename src/{parser.rs => parser/mod.rs} (66%) create mode 100644 src/parser/parsed_reid.rs diff --git a/src/parser.rs b/src/parser/mod.rs similarity index 66% rename from src/parser.rs rename to src/parser/mod.rs index 2b6c936..404f5aa 100644 --- a/src/parser.rs +++ b/src/parser/mod.rs @@ -1,10 +1,10 @@ -use super::errors::CompilerError; +mod parsed_reid; +use super::errors::CompilerError; +use parsed_reid::{Expression, ParsedReid}; use std::fmt; use std::fmt::Display; -type Ident = String; - const ALLOWED_IDENT_CHARS: [char; 38] = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_', '-', @@ -195,106 +195,6 @@ impl Parser { } } -#[derive(Debug)] -pub struct ParsedReid(Expression); - -#[derive(Debug)] -pub enum Expression { - BlockExpr(Position, Vec), - StatementExpr(Position, Statement), -} - -impl Expression { - pub fn parse(parser: &mut Parser) -> Result { - let begin_pos = parser.pos(); - - let expect = parser.expect("{"); - if let Some(_) = expect.get() { - let mut exp_list = Vec::new(); - while { - match Expression::parse(parser) { - Ok(exp) => { - exp_list.push(exp); - true - } - Err(_) => false, - } - } {} - if let Some(_) = parser.expect("}").get() { - Ok(Expression::BlockExpr(begin_pos, exp_list)) - } else { - Err(CompilerError::ExpectedToken(parser.pos(), '}')) - } - } else { - match Statement::parse(parser) { - Ok(statement) => Ok(Expression::StatementExpr(begin_pos, statement)), - Err(err) => Err(CompilerError::ExpectedExpression(begin_pos, Box::new(err))), - } - } - } -} - -#[derive(Debug)] -pub enum Statement { - LetStatement(Position, Ident, Box), - ValueRef(Position, Pattern), -} - -impl Statement { - pub fn parse(parser: &mut Parser) -> Result { - let pos = parser.pos(); - - if let Some(_) = parser.expect("let").get() { - let ident = parser - .expect_ident() - .get_or(CompilerError::ExpectedIdent(pos))?; - parser - .expect("=") - .get_or(CompilerError::ExpectedToken(pos, '='))?; - match Expression::parse(parser) { - Ok(expr) => { - parser - .expect(";") - .get_or(CompilerError::ExpectedToken(pos, ';'))?; - Ok(Statement::LetStatement(pos, ident, Box::new(expr))) - } - Err(err) => Err(CompilerError::ExpectedExpression(pos, Box::new(err))), - } - } else if let Ok(pattern) = Pattern::parse(parser) { - Ok(Statement::ValueRef(pos, pattern)) - } else { - Err(CompilerError::ExpectedStatement(pos)) - } - } -} - -#[derive(Debug)] -pub enum Pattern { - IdentPattern(Position, Ident), - LiteralPattern(Position, LiteralPattern), -} - -impl Pattern { - fn parse(parser: &mut Parser) -> Result { - let pos = parser.pos(); - if let Some(string) = parser.expect_string_lit().get() { - Ok(Pattern::LiteralPattern( - pos, - LiteralPattern::StringLit(string), - )) - } else if let Some(ident) = parser.expect_ident().get() { - Ok(Pattern::IdentPattern(pos, ident)) - } else { - Err(CompilerError::ExpectedPattern(pos)) - } - } -} - -#[derive(Debug)] -pub enum LiteralPattern { - StringLit(String), -} - pub struct Expect<'a> { text: Option, len: usize, diff --git a/src/parser/parsed_reid.rs b/src/parser/parsed_reid.rs new file mode 100644 index 0000000..53a2a8e --- /dev/null +++ b/src/parser/parsed_reid.rs @@ -0,0 +1,103 @@ +use super::{CompilerError, Parser, Position}; + +type Ident = String; + +#[derive(Debug)] +pub struct ParsedReid(pub Expression); + +#[derive(Debug)] +pub enum Expression { + BlockExpr(Position, Vec), + StatementExpr(Position, Statement), +} + +impl Expression { + pub fn parse(parser: &mut Parser) -> Result { + let begin_pos = parser.pos(); + + let expect = parser.expect("{"); + if let Some(_) = expect.get() { + let mut exp_list = Vec::new(); + while { + match Expression::parse(parser) { + Ok(exp) => { + exp_list.push(exp); + true + } + Err(_) => false, + } + } {} + if let Some(_) = parser.expect("}").get() { + Ok(Expression::BlockExpr(begin_pos, exp_list)) + } else { + Err(CompilerError::ExpectedToken(parser.pos(), '}')) + } + } else { + match Statement::parse(parser) { + Ok(statement) => Ok(Expression::StatementExpr(begin_pos, statement)), + Err(err) => Err(CompilerError::ExpectedExpression(begin_pos, Box::new(err))), + } + } + } +} + +#[derive(Debug)] +pub enum Statement { + LetStatement(Position, Ident, Box), + ValueRef(Position, Pattern), +} + +impl Statement { + pub fn parse(parser: &mut Parser) -> Result { + let pos = parser.pos(); + + if let Some(_) = parser.expect("let").get() { + let ident = parser + .expect_ident() + .get_or(CompilerError::ExpectedIdent(pos))?; + parser + .expect("=") + .get_or(CompilerError::ExpectedToken(pos, '='))?; + match Expression::parse(parser) { + Ok(expr) => { + parser + .expect(";") + .get_or(CompilerError::ExpectedToken(pos, ';'))?; + Ok(Statement::LetStatement(pos, ident, Box::new(expr))) + } + Err(err) => Err(CompilerError::ExpectedExpression(pos, Box::new(err))), + } + } else if let Ok(pattern) = Pattern::parse(parser) { + Ok(Statement::ValueRef(pos, pattern)) + } else { + Err(CompilerError::ExpectedStatement(pos)) + } + } +} + +#[derive(Debug)] +pub enum Pattern { + IdentPattern(Position, Ident), + LiteralPattern(Position, LiteralPattern), +} + +impl Pattern { + fn parse(parser: &mut Parser) -> Result { + let pos = parser.pos(); + if let Some(string) = parser.expect_string_lit().get() { + Ok(Pattern::LiteralPattern( + pos, + LiteralPattern::StringLit(string), + )) + } else if let Some(ident) = parser.expect_ident().get() { + Ok(Pattern::IdentPattern(pos, ident)) + } else { + Err(CompilerError::ExpectedPattern(pos)) + } + } +} + +#[derive(Debug)] +pub enum LiteralPattern { + StringLit(String), +}