Split parser.rs into two files

This commit is contained in:
Sofia 2020-06-21 01:45:09 +03:00
parent 9daefab1e0
commit c9dffa7713
2 changed files with 106 additions and 103 deletions

View File

@ -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<Expression>),
StatementExpr(Position, Statement),
}
impl Expression {
pub fn parse(parser: &mut Parser) -> Result<Expression, CompilerError> {
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<Expression>),
ValueRef(Position, Pattern),
}
impl Statement {
pub fn parse(parser: &mut Parser) -> Result<Statement, CompilerError> {
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<Pattern, CompilerError> {
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<String>,
len: usize,

103
src/parser/parsed_reid.rs Normal file
View File

@ -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<Expression>),
StatementExpr(Position, Statement),
}
impl Expression {
pub fn parse(parser: &mut Parser) -> Result<Expression, CompilerError> {
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<Expression>),
ValueRef(Position, Pattern),
}
impl Statement {
pub fn parse(parser: &mut Parser) -> Result<Statement, CompilerError> {
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<Pattern, CompilerError> {
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),
}