diff --git a/src/common/loc.rs b/src/common/loc.rs index 26bea1b..3747b06 100644 --- a/src/common/loc.rs +++ b/src/common/loc.rs @@ -1,6 +1,4 @@ -use std::fmt::Display; - - +use std::fmt::{Debug, Display}; #[derive(Debug, Clone, PartialEq, PartialOrd, Ord, Eq)] pub struct Loc { @@ -9,6 +7,7 @@ pub struct Loc { col: usize, } +#[allow(dead_code)] impl Loc { pub fn new(s: impl ToString, line: usize, col: usize) -> Self { Self { @@ -27,6 +26,28 @@ impl Loc { } } + +#[derive(Debug, Clone)] +pub struct LocBox { + inner: T, + loc: Loc +} + +impl LocBox { + pub fn new(loc: &Loc, inner: T) -> Self { + Self { loc: loc.clone(), inner } + } + pub fn inner(&self) -> &T { + &self.inner + } + pub fn inner_mut(&mut self) -> &mut T { + &mut self.inner + } + pub fn loc(&self) -> &Loc { + &self.loc + } +} + impl Display for Loc { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}:{}:{}", self.file, self.line, self.col) diff --git a/src/parser/ast/expr.rs b/src/parser/ast/expr.rs index 2fd743e..600551e 100644 --- a/src/parser/ast/expr.rs +++ b/src/parser/ast/expr.rs @@ -1,76 +1,55 @@ use std::collections::HashMap; -use crate::{common::Loc, tokeniser::tokentype::*}; +use crate::{common::loc::LocBox, tokeniser::tokentype::*}; -use super::{typ::{Type, TypeBox}, Ast}; - -#[derive(Debug, Clone)] -pub struct ExprBox { - inner: Expr, - loc: Loc -} - -impl ExprBox { - pub fn new(loc: &Loc, expr: Expr) -> Self { - Self { loc: loc.clone(), inner: expr } - } - pub fn inner(&self) -> &Expr { - &self.inner - } - pub fn inner_mut(&mut self) -> &mut Expr { - &mut self.inner - } - pub fn loc(&self) -> &Loc { - &self.loc - } -} +use super::{typ::Type, Ast}; #[derive(Debug, Clone)] pub enum Expr { // Comment(Comment), - Group(Box), + Group(Box>), UnOp { typ: Punctuation, - right: Box, + right: Box>, }, BinOp { typ: Punctuation, - left: Box, - right: Box, + left: Box>, + right: Box>, }, Literal(super::literal::Literal), ArrayIndex { - name: Box, - index: Box, + name: Box>, + index: Box>, }, Path(Path), Call { - path: Box, - params: CallParams, // ExprBox ~ (, Expr)* + path: Box>, + params: CallParams, // LocBox ~ (, Expr)* }, //MethodCall { - // var_name: Box, + // var_name: Box>, // method_name: Ident, // params: CallParams, //}, /// the left side only exists on the /.|->/ chain FieldAccess { - left: Box>, - right: Box, + left: Box>>, + right: Box>, }, PtrFieldAccess { - left: Box>, - right: Box, + left: Box>>, + right: Box>, }, ForLoop { init: Box, - test: Box, - on_loop: Box, + test: Box>, + on_loop: Box>, body: Block, }, WhileLoop { - test: Box, + test: Box>, body: Block, }, InfLoop { @@ -79,20 +58,20 @@ pub enum Expr { If(IfExpr), Struct { path: Path, - fields: HashMap, + fields: HashMap>, }, - Return(Box>), + Return(Box>>), Break, Continue, Cast { - left: Box, - right: Box + left: Box>, + right: Box> }, } -impl ExprBox { +impl Expr { pub fn unwrap_path(&self) -> Path { - let Expr::Path(p) = self.inner.clone() else {panic!("Unwrapping")}; + let Expr::Path(p) = self else {panic!("Unwrapping")}; p.clone() } } @@ -100,7 +79,7 @@ impl ExprBox { #[derive(Debug, Clone)] -pub struct CallParams(pub Vec); +pub struct CallParams(pub Vec>); #[derive(Debug, Clone)] pub struct Block(pub Vec); @@ -111,7 +90,7 @@ pub struct Path(pub Vec); #[derive(Debug, Clone)] pub struct IfExpr { - pub test: Box, + pub test: Box>, pub body: Block, pub else_if: Option } diff --git a/src/parser/ast/literal.rs b/src/parser/ast/literal.rs index eed9a73..670196f 100644 --- a/src/parser/ast/literal.rs +++ b/src/parser/ast/literal.rs @@ -1,8 +1,8 @@ use std::collections::HashMap; -use crate::tokeniser::tokentype::*; +use crate::{common::loc::LocBox, tokeniser::tokentype::*}; -use super::{expr::ExprBox, typ::TypeBox, Ast}; +use super::{expr::Expr, typ::Type, Ast}; #[derive(Debug, Clone)] pub enum Literal { @@ -10,10 +10,10 @@ pub enum Literal { Ident(Ident), String(TString), Char(Char), - Array(Vec), + Array(Vec>), ArrayRepeat { - typ: Box, - count: Box, + typ: Box>, + count: Box>, }, Struct { name: Ident, diff --git a/src/parser/ast/mod.rs b/src/parser/ast/mod.rs index d801106..868c967 100644 --- a/src/parser/ast/mod.rs +++ b/src/parser/ast/mod.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use statement::{Enum, Function, Struct, TypeAlias}; +use crate::common::loc::LocBox; pub use crate::tokeniser::tokentype::*; pub mod expr; @@ -21,8 +22,8 @@ pub struct Program { #[derive(Debug, Clone)] pub enum Ast { - Expr(expr::ExprBox), - Statement(statement::StatementBox), + Expr(LocBox), + Statement(LocBox), } diff --git a/src/parser/ast/statement.rs b/src/parser/ast/statement.rs index 127b32e..69f0513 100644 --- a/src/parser/ast/statement.rs +++ b/src/parser/ast/statement.rs @@ -1,28 +1,7 @@ -use crate::common::Loc; +use crate::common::{loc::LocBox, Loc}; -use super::{expr::{Block, ExprBox}, typ::{Type, TypeBox}, Ident, TString}; - -#[derive(Debug, Clone)] -pub struct StatementBox { - inner: Statement, - loc: Loc -} - -impl StatementBox { - pub fn new(loc: &Loc, stat: Statement) -> Self { - Self { loc: loc.clone(), inner: stat } - } - pub fn inner(&self) -> &Statement { - &self.inner - } - pub fn inner_mut(&mut self) -> &mut Statement { - &mut self.inner - } - pub fn loc(&self) -> &Loc { - &self.loc - } -} +use super::{expr::{Block, Expr}, typ::Type, Ident, TString}; #[derive(Debug, Clone)] pub enum Statement { @@ -32,31 +11,31 @@ pub enum Statement { Enum(Enum), ConstVar { name: Ident, - typ: TypeBox, - val: ExprBox + typ: LocBox, + val: LocBox }, StaticVar { name: Ident, - typ: TypeBox, - val: ExprBox, + typ: LocBox, + val: LocBox, }, Let { name: Ident, - typ: Option, - val: Option, + typ: Option>, + val: Option>, }, } #[derive(Debug, Clone)] pub struct TypeAlias { pub name: Ident, - pub typ: TypeBox, + pub typ: LocBox, } #[derive(Debug, Clone)] pub struct Struct { pub name: Ident, - pub fields: Vec<(Ident, TypeBox)>, + pub fields: Vec<(Ident, LocBox)>, } #[derive(Debug, Clone)] @@ -69,8 +48,8 @@ pub struct Enum { pub struct Function { pub struct_name: Option, pub name: Ident, - pub params: Vec<(Ident, TypeBox)>, - pub ret_type: Option, + pub params: Vec<(Ident, LocBox)>, + pub ret_type: Option>, pub qual_const: bool, pub qual_extern: Option, // abi pub body: Option, // If None then its a type declaration diff --git a/src/parser/ast/typ.rs b/src/parser/ast/typ.rs index c5c5b96..ce45660 100644 --- a/src/parser/ast/typ.rs +++ b/src/parser/ast/typ.rs @@ -1,27 +1,6 @@ -use crate::common::Loc; +use crate::common::{loc::LocBox, Loc}; -use super::{expr::ExprBox, Ident}; - -#[derive(Debug, Clone)] -pub struct TypeBox { - inner: Type, - loc: Loc -} - -impl TypeBox { - pub fn new(loc: &Loc, stat: Type) -> Self { - Self { loc: loc.clone(), inner: stat } - } - pub fn inner(&self) -> &Type { - &self.inner - } - pub fn inner_mut(&mut self) -> &mut Type { - &mut self.inner - } - pub fn loc(&self) -> &Loc { - &self.loc - } -} +use super::{expr::Expr, Ident}; #[derive(Debug, Clone)] pub enum Type { @@ -34,7 +13,7 @@ pub enum Type { }, ArrayRepeat { inner: Box, - count: ExprBox, + count: LocBox, }, Owned(Ident), } diff --git a/src/parser/expr.rs b/src/parser/expr.rs index 9eb8442..aa013db 100644 --- a/src/parser/expr.rs +++ b/src/parser/expr.rs @@ -2,9 +2,9 @@ use std::collections::HashMap; use anyhow::{bail, Result}; -use crate::{debug, error, lerror, parser::{typ::parse_type, Punctuation}, tokeniser::Token}; +use crate::{common::loc::LocBox, debug, error, lerror, parser::{typ::parse_type, Punctuation}, tokeniser::Token}; -use super::{ast::{expr::{Block, CallParams, Expr, ExprBox, IfBranchExpr, IfExpr, Path}, literal::Literal, TokenType}, parse_item, utils, Delimiter, Keyword}; +use super::{ast::{expr::{Block, CallParams, Expr, IfBranchExpr, IfExpr, Path}, literal::Literal, TokenType}, parse_item, utils, Delimiter, Keyword}; const BINOP_LIST: &[TokenType] = &[ TokenType::Punct(Punctuation::Plus), @@ -37,14 +37,14 @@ const BINOP_LIST: &[TokenType] = &[ TokenType::Punct(Punctuation::Ge), ]; -pub fn parse_expr(tokens: &mut Vec, precedence: usize, consume_semi: bool) -> Result> { +pub fn parse_expr(tokens: &mut Vec, precedence: usize, consume_semi: bool) -> Result>> { let res = if let Some(_) = utils::check(tokens, TokenType::Delim(Delimiter::ParenL)) { Some(parse_group(tokens)?) } else if let Some(_) = utils::check(tokens, TokenType::ident("")) { let p = parse_path(tokens)?; if let Some(_) = utils::check(tokens, TokenType::Delim(Delimiter::CurlyL)) { - Some(parse_struct_literal(tokens, p.unwrap_path())?) + Some(parse_struct_literal(tokens, p.inner().unwrap_path())?) } else { Some(p) } @@ -74,11 +74,11 @@ pub fn parse_expr(tokens: &mut Vec, precedence: usize, consume_semi: bool } else if let Some(_) = utils::check(tokens, TokenType::Keyword(Keyword::Return)) { return Ok(Some(parse_return(tokens)?)); } else if let Some(kw) = utils::check_consume(tokens, TokenType::Keyword(Keyword::Break)) { - return Ok(Some(ExprBox::new(kw.loc(), Expr::Break))); + return Ok(Some(LocBox::new(kw.loc(), Expr::Break))); } else if let Some(kw) = utils::check_consume(tokens, TokenType::Keyword(Keyword::Continue)) { - return Ok(Some(ExprBox::new(kw.loc(), Expr::Continue))); + return Ok(Some(LocBox::new(kw.loc(), Expr::Continue))); } else if let Some(kw) = utils::check(tokens, TokenType::Keyword(Keyword::If)) { - return Ok(Some(ExprBox::new(&kw.loc().clone(), Expr::If(parse_if(tokens)?)))); + return Ok(Some(LocBox::new(&kw.loc().clone(), Expr::If(parse_if(tokens)?)))); } else { None }; @@ -113,16 +113,16 @@ pub fn parse_expr(tokens: &mut Vec, precedence: usize, consume_semi: bool Ok(res) } -fn parse_return(tokens: &mut Vec) -> Result { +fn parse_return(tokens: &mut Vec) -> Result> { let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Return), "")?; let item = parse_expr(tokens, 0, true)?; - Ok(ExprBox::new(kw.loc(), Expr::Return(Box::new(item)))) + Ok(LocBox::new(kw.loc(), Expr::Return(Box::new(item)))) } -fn parse_cast(tokens: &mut Vec, left: ExprBox) -> Result { +fn parse_cast(tokens: &mut Vec, left: LocBox) -> Result> { let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::As), "")?; let typ = parse_type(tokens)?; - Ok(ExprBox::new(kw.loc(), Expr::Cast { + Ok(LocBox::new(kw.loc(), Expr::Cast { left: Box::new(left), right: Box::new(typ) })) @@ -168,19 +168,19 @@ fn parse_if(tokens: &mut Vec) -> Result { }) } } -fn parse_while_loop(tokens: &mut Vec) -> Result { +fn parse_while_loop(tokens: &mut Vec) -> Result> { let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::While), "")?; let Some(test) = parse_expr(tokens, 0, false)? else { lerror!(kw.loc(), "Expected test comparrison for while loop, got nothing"); bail!("") }; let block = parse_block(tokens)?; - Ok(ExprBox::new(kw.loc(), Expr::WhileLoop { + Ok(LocBox::new(kw.loc(), Expr::WhileLoop { test: Box::new(test), body: block })) } -fn parse_for_loop(tokens: &mut Vec) -> Result { +fn parse_for_loop(tokens: &mut Vec) -> Result> { let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::For), "")?; let Some(pre) = parse_item(tokens)? else { lerror!(kw.loc(), "Expected init stat for a for loop, got nothing"); @@ -198,19 +198,19 @@ fn parse_for_loop(tokens: &mut Vec) -> Result { }; let block = parse_block(tokens)?; - Ok(ExprBox::new(kw.loc(), Expr::ForLoop { + Ok(LocBox::new(kw.loc(), Expr::ForLoop { init: Box::new(pre), test: Box::new(test), on_loop: Box::new(post), body: block })) } -fn parse_inf_loop(tokens: &mut Vec) -> Result { +fn parse_inf_loop(tokens: &mut Vec) -> Result> { let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Loop), "")?; let block = parse_block(tokens)?; - Ok(ExprBox::new(kw.loc(), Expr::InfLoop { body: block })) + Ok(LocBox::new(kw.loc(), Expr::InfLoop { body: block })) } -fn parse_fn_call(tokens: &mut Vec, left: ExprBox) -> Result { +fn parse_fn_call(tokens: &mut Vec, left: LocBox) -> Result> { let start = utils::check_consume_or_err(tokens, TokenType::Delim(Delimiter::ParenL), "")?; let mut params = Vec::new(); @@ -228,22 +228,22 @@ fn parse_fn_call(tokens: &mut Vec, left: ExprBox) -> Result { } } _ = utils::check_consume_or_err(tokens, TokenType::Delim(Delimiter::ParenR), ""); - Ok(ExprBox::new(start.loc(), Expr::Call { path: Box::new(left), params: CallParams(params) })) + Ok(LocBox::new(start.loc(), Expr::Call { path: Box::new(left), params: CallParams(params) })) } -fn parse_array_index(tokens: &mut Vec, left: ExprBox) -> Result { +fn parse_array_index(tokens: &mut Vec, left: LocBox) -> Result> { let start = utils::check_consume_or_err(tokens, TokenType::Delim(Delimiter::SquareL), "")?; let Some(idx) = parse_expr(tokens, 0, false)? else { lerror!(start.loc(), "Expected index for in array index but found nothing."); bail!("") }; _ = utils::check_consume_or_err(tokens, TokenType::Delim(Delimiter::SquareR), ""); - Ok(ExprBox::new(start.loc(), Expr::ArrayIndex { + Ok(LocBox::new(start.loc(), Expr::ArrayIndex { name: Box::new(left), index: Box::new(idx) })) } -fn parse_field_access(tokens: &mut Vec, left: ExprBox) -> Result { +fn parse_field_access(tokens: &mut Vec, left: LocBox) -> Result> { let start = utils::check_consume_or_err(tokens, TokenType::Punct(Punctuation::Fieldaccess), "unreachable")?; let right = if let Some(_) = utils::check_2_last(tokens, TokenType::Punct(Punctuation::Arrow)) { @@ -255,13 +255,13 @@ fn parse_field_access(tokens: &mut Vec, left: ExprBox) -> Result } else { parse_path(tokens)? }; - Ok(ExprBox::new(start.loc(), Expr::FieldAccess { + Ok(LocBox::new(start.loc(), Expr::FieldAccess { left: Box::new(Some(left)), right: Box::new(right) })) } -fn parse_ptr_field_access(tokens: &mut Vec, left: ExprBox) -> Result { +fn parse_ptr_field_access(tokens: &mut Vec, left: LocBox) -> Result> { let start = utils::check_consume_or_err(tokens, TokenType::Punct(Punctuation::Arrow), "unreachable")?; let right = if let Some(_) = utils::check_2_last(tokens, TokenType::Punct(Punctuation::Arrow)) { let right = parse_path(tokens)?; @@ -272,28 +272,28 @@ fn parse_ptr_field_access(tokens: &mut Vec, left: ExprBox) -> Result) -> Result { +fn parse_literal(tokens: &mut Vec) -> Result> { if let Some(tkn) = utils::check_consume(tokens, TokenType::string("", false)) { let TokenType::String(str) = tkn.tt() else {unreachable!()}; - return Ok(ExprBox::new(tkn.loc(), Expr::Literal(Literal::String(str.clone())))); + return Ok(LocBox::new(tkn.loc(), Expr::Literal(Literal::String(str.clone())))); } else if let Some(tkn) = utils::check_consume(tokens, TokenType::number(0, 0, false)) { let TokenType::Number(val) = tkn.tt() else {unreachable!()}; - return Ok(ExprBox::new(tkn.loc(), Expr::Literal(Literal::Number(val.clone())))); + return Ok(LocBox::new(tkn.loc(), Expr::Literal(Literal::Number(val.clone())))); } else if let Some(tkn) = utils::check_consume(tokens, TokenType::char('\0')) { let TokenType::Char(val) = tkn.tt() else {unreachable!()}; - return Ok(ExprBox::new(tkn.loc(), Expr::Literal(Literal::Char(val.clone())))); + return Ok(LocBox::new(tkn.loc(), Expr::Literal(Literal::Char(val.clone())))); } else if let Some(start) = utils::check_consume(tokens, TokenType::Delim(Delimiter::SquareL)) { if let Some(_) = utils::check_consume(tokens, TokenType::Delim(Delimiter::SquareR)) { - return Ok(ExprBox::new(start.loc(), Expr::Literal(Literal::Array(Vec::new())))); + return Ok(LocBox::new(start.loc(), Expr::Literal(Literal::Array(Vec::new())))); } if *tokens[tokens.len()-2].tt() == TokenType::Punct(Punctuation::Comma) { let first = parse_expr(tokens, 0, false)?; @@ -310,12 +310,12 @@ fn parse_literal(tokens: &mut Vec) -> Result { } } utils::check_consume_or_err(tokens, TokenType::Delim(Delimiter::SquareR), "")?; - return Ok(ExprBox::new(start.loc(), Expr::Literal(Literal::Array(values)))); + return Ok(LocBox::new(start.loc(), Expr::Literal(Literal::Array(values)))); } else if *tokens[tokens.len()-2].tt() == TokenType::Punct(Punctuation::Semi) { let typ = parse_type(tokens)?; let count = parse_expr(tokens, 0, false)?.unwrap(); utils::check_consume_or_err(tokens, TokenType::Delim(Delimiter::SquareR), "")?; - return Ok(ExprBox::new(start.loc(), Expr::Literal(Literal::ArrayRepeat { + return Ok(LocBox::new(start.loc(), Expr::Literal(Literal::ArrayRepeat { typ: Box::new(typ), count: Box::new(count) }))); @@ -331,7 +331,7 @@ fn parse_literal(tokens: &mut Vec) -> Result { unreachable!() } -fn parse_struct_literal(tokens: &mut Vec, name: Path) -> Result { +fn parse_struct_literal(tokens: &mut Vec, name: Path) -> Result> { let start = utils::check_consume_or_err(tokens, TokenType::Delim(Delimiter::CurlyL), "")?; let mut fields = HashMap::new(); while !tokens.is_empty() { @@ -348,10 +348,10 @@ fn parse_struct_literal(tokens: &mut Vec, name: Path) -> Result break; } } - Ok(ExprBox::new(start.loc(), Expr::Struct { path: name, fields })) + Ok(LocBox::new(start.loc(), Expr::Struct { path: name, fields })) } -fn parse_group(tokens: &mut Vec) -> Result { +fn parse_group(tokens: &mut Vec) -> Result> { let start = utils::check_consume_or_err(tokens, TokenType::Delim(Delimiter::ParenL), "")?; let Some(expr) = parse_expr(tokens, 0, false)? else { lerror!(start.loc(), "Expected expr found nothing"); @@ -359,10 +359,10 @@ fn parse_group(tokens: &mut Vec) -> Result { }; utils::check_consume_or_err(tokens, TokenType::Delim(Delimiter::ParenR), "")?; - Ok(ExprBox::new(start.loc(), Expr::Group(Box::new(expr)))) + Ok(LocBox::new(start.loc(), Expr::Group(Box::new(expr)))) } -fn parse_path(tokens: &mut Vec) -> Result { +fn parse_path(tokens: &mut Vec) -> Result> { let mut buf = Vec::new(); let part = utils::check_consume(tokens, TokenType::ident("")).unwrap(); @@ -375,10 +375,10 @@ fn parse_path(tokens: &mut Vec) -> Result { buf.push(part.tt().unwrap_ident()); } - Ok(ExprBox::new(part.loc(), Expr::Path(Path(buf)))) + Ok(LocBox::new(part.loc(), Expr::Path(Path(buf)))) } -fn parse_unop(tokens: &mut Vec) -> Result { +fn parse_unop(tokens: &mut Vec) -> Result> { let typ = utils::check_consume_or_err_from_many(tokens, &[ TokenType::Punct(Punctuation::Not), TokenType::Punct(Punctuation::Plus), @@ -393,13 +393,13 @@ fn parse_unop(tokens: &mut Vec) -> Result { lerror!(&loc, "Expected expression after unary token, found nothing"); bail!("") }; - Ok(ExprBox::new(&loc, Expr::UnOp { + Ok(LocBox::new(&loc, Expr::UnOp { typ, right: Box::new(right) })) } -fn parse_binop(tokens: &mut Vec, mut lhs: ExprBox, precedence: usize) -> Result { +fn parse_binop(tokens: &mut Vec, mut lhs: LocBox, precedence: usize) -> Result> { // TODO: https://en.wikipedia.org/wiki/Operator-precedence_parser#Pseudocode loop { @@ -431,7 +431,7 @@ fn parse_binop(tokens: &mut Vec, mut lhs: ExprBox, precedence: usize) -> _ = tokens.pop(); let Some(rhs) = parse_expr(tokens, rp, false)? else {break;}; - lhs = ExprBox::new(&op_loc, Expr::BinOp { + lhs = LocBox::new(&op_loc, Expr::BinOp { typ: op, left: Box::new(lhs), right: Box::new(rhs) diff --git a/src/parser/stat.rs b/src/parser/stat.rs index acf7259..55f4c96 100644 --- a/src/parser/stat.rs +++ b/src/parser/stat.rs @@ -1,19 +1,20 @@ use anyhow::bail; +use crate::common::loc::LocBox; use crate::lerror; use crate::parser::ast::TokenType; use crate::parser::expr::parse_expr; use crate::parser::{Delimiter, Ident, Keyword, Punctuation}; use crate::tokeniser::Token; -use super::ast::typ::{Type, TypeBox}; +use super::ast::typ::Type; use super::expr::parse_block; use super::typ::parse_type; use super::utils; -use super::ast::statement::{Enum, Function, Statement, StatementBox, Struct, TypeAlias}; +use super::ast::statement::{Enum, Function, Statement, Struct, TypeAlias}; type Result = anyhow::Result; -pub fn parse_statement(tokens: &mut Vec) -> Result> { +pub fn parse_statement(tokens: &mut Vec) -> Result>> { if let Some(_) = utils::check(tokens, TokenType::Keyword(Keyword::Fn)) { Ok(Some(parse_fn(tokens)?)) } else @@ -39,7 +40,7 @@ pub fn parse_statement(tokens: &mut Vec) -> Result> } } -fn parse_enum(tokens: &mut Vec) -> Result { +fn parse_enum(tokens: &mut Vec) -> Result> { let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Enum), "")?; let name = utils::check_consume_or_err(tokens, TokenType::ident(""), "")?.tt().unwrap_ident(); _ = utils::check_consume(tokens, TokenType::Delim(Delimiter::CurlyL)); @@ -60,10 +61,10 @@ fn parse_enum(tokens: &mut Vec) -> Result { fields.push(field_name); } - Ok(StatementBox::new(kw.loc(), Statement::Enum(Enum { name, fields }))) + Ok(LocBox::new(kw.loc(), Statement::Enum(Enum { name, fields }))) } -fn parse_struct(tokens: &mut Vec) -> Result { +fn parse_struct(tokens: &mut Vec) -> Result> { let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Struct), "")?; let name = utils::check_consume_or_err(tokens, TokenType::ident(""), "")?.tt().unwrap_ident(); _ = utils::check_consume(tokens, TokenType::Delim(Delimiter::CurlyL)); @@ -86,10 +87,10 @@ fn parse_struct(tokens: &mut Vec) -> Result { fields.push((field_name, typ)); } - Ok(StatementBox::new(kw.loc(), Statement::Struct(Struct { name, fields }))) + Ok(LocBox::new(kw.loc(), Statement::Struct(Struct { name, fields }))) } -fn parse_static(tokens: &mut Vec) -> Result { +fn parse_static(tokens: &mut Vec) -> Result> { let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Static), "")?; let name = utils::check_consume_or_err(tokens, TokenType::ident(""), "")?.tt().unwrap_ident(); @@ -101,10 +102,10 @@ fn parse_static(tokens: &mut Vec) -> Result { bail!("") }; _ = utils::check_consume_or_err(tokens, TokenType::Punct(Punctuation::Semi), "")?; - Ok(StatementBox::new(kw.loc(), Statement::StaticVar { name, typ, val })) + Ok(LocBox::new(kw.loc(), Statement::StaticVar { name, typ, val })) } -fn parse_let(tokens: &mut Vec) -> Result { +fn parse_let(tokens: &mut Vec) -> Result> { let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Let), "")?; let name = utils::check_consume_or_err(tokens, TokenType::ident(""), "")?.tt().unwrap_ident(); let mut typ = None; @@ -120,9 +121,9 @@ fn parse_let(tokens: &mut Vec) -> Result { val = Some(_val); } _ = utils::check_consume_or_err(tokens, TokenType::Punct(Punctuation::Semi), "")?; - Ok(StatementBox::new(kw.loc(), Statement::Let { name, typ, val })) + Ok(LocBox::new(kw.loc(), Statement::Let { name, typ, val })) } -fn parse_constant(tokens: &mut Vec) -> Result { +fn parse_constant(tokens: &mut Vec) -> Result> { let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Const), "")?; if let Some(_) = utils::check(tokens, TokenType::Keyword(Keyword::Fn)) { @@ -137,20 +138,20 @@ fn parse_constant(tokens: &mut Vec) -> Result { bail!("") }; _ = utils::check_consume_or_err(tokens, TokenType::Punct(Punctuation::Semi), "")?; - Ok(StatementBox::new(kw.loc(), Statement::ConstVar { name, typ, val })) + Ok(LocBox::new(kw.loc(), Statement::ConstVar { name, typ, val })) } -fn parse_type_alias(tokens: &mut Vec) -> Result { +fn parse_type_alias(tokens: &mut Vec) -> Result> { let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Type), "")?; let name = utils::check_consume_or_err(tokens, TokenType::ident(""), "")?.tt().unwrap_ident(); _ = utils::check_consume_or_err(tokens, TokenType::Punct(Punctuation::Eq), "")?; let typ = parse_type(tokens)?; _ = utils::check_consume_or_err(tokens, TokenType::Punct(Punctuation::Semi), "")?; - Ok(StatementBox::new(kw.loc(), Statement::TypeAlias(TypeAlias { name, typ }))) + Ok(LocBox::new(kw.loc(), Statement::TypeAlias(TypeAlias { name, typ }))) } -fn parse_fn(tokens: &mut Vec) -> Result { +fn parse_fn(tokens: &mut Vec) -> Result> { // Just remove the kw since we checked it before let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Fn), "")?; @@ -177,7 +178,7 @@ fn parse_fn(tokens: &mut Vec) -> Result { body = None; } - Ok(StatementBox::new(kw.loc(), Statement::Fn(Function{ + Ok(LocBox::new(kw.loc(), Statement::Fn(Function{ struct_name, name, params, @@ -190,7 +191,7 @@ fn parse_fn(tokens: &mut Vec) -> Result { -fn parse_fn_params(tokens: &mut Vec) -> Result> { +fn parse_fn_params(tokens: &mut Vec) -> Result)>> { let mut args = Vec::new(); utils::check_consume_or_err(tokens, TokenType::Delim(Delimiter::ParenL), "")?; while !tokens.is_empty() { diff --git a/src/parser/typ.rs b/src/parser/typ.rs index 6e8da23..10ead5a 100644 --- a/src/parser/typ.rs +++ b/src/parser/typ.rs @@ -1,10 +1,10 @@ use anyhow::Result; -use crate::{parser::Delimiter, tokeniser::Token}; +use crate::{common::loc::LocBox, parser::Delimiter, tokeniser::Token}; -use super::{ast::{typ::{Type, TypeBox}, TokenType}, expr::parse_expr, utils, Keyword, Punctuation}; +use super::{ast::{typ::Type, TokenType}, expr::parse_expr, utils, Keyword, Punctuation}; -pub fn parse_type(tokens: &mut Vec) -> Result { +pub fn parse_type(tokens: &mut Vec) -> Result> { let mut ref_cnt = Vec::new(); let mut loc = None; while let Some(tok) = utils::check_consume(tokens, TokenType::Punct(Punctuation::Ampersand)) { @@ -60,5 +60,5 @@ pub fn parse_type(tokens: &mut Vec) -> Result { _ => unreachable!() } } - Ok(TypeBox::new(&loc.unwrap(), typ)) + Ok(LocBox::new(&loc.unwrap(), typ)) } diff --git a/tests/parser/enumerations.exp b/tests/parser/enumerations.exp index b48ee12..d543055 100644 --- a/tests/parser/enumerations.exp +++ b/tests/parser/enumerations.exp @@ -2,7 +2,7 @@ Program { ast: Block( [ Statement( - StatementBox { + LocBox { inner: Enum( Enum { name: Ident( @@ -19,7 +19,7 @@ Program { }, ), Statement( - StatementBox { + LocBox { inner: Enum( Enum { name: Ident( diff --git a/tests/parser/expressions.exp b/tests/parser/expressions.exp index e6a06a5..b0c9f70 100644 --- a/tests/parser/expressions.exp +++ b/tests/parser/expressions.exp @@ -2,20 +2,20 @@ Program { ast: Block( [ Statement( - StatementBox { + LocBox { inner: Let { name: Ident( "a", ), typ: None, val: Some( - ExprBox { + LocBox { inner: BinOp { typ: EqEq, - left: ExprBox { + left: LocBox { inner: BinOp { typ: Star, - left: ExprBox { + left: LocBox { inner: Literal( Number( Number { @@ -31,7 +31,7 @@ Program { col: 10, }, }, - right: ExprBox { + right: LocBox { inner: Literal( Number( Number { @@ -54,7 +54,7 @@ Program { col: 12, }, }, - right: ExprBox { + right: LocBox { inner: Literal( Number( Number { @@ -87,20 +87,20 @@ Program { }, ), Statement( - StatementBox { + LocBox { inner: Let { name: Ident( "b", ), typ: None, val: Some( - ExprBox { + LocBox { inner: BinOp { typ: EqEq, - left: ExprBox { + left: LocBox { inner: BinOp { typ: Div, - left: ExprBox { + left: LocBox { inner: Literal( Number( Number { @@ -116,7 +116,7 @@ Program { col: 10, }, }, - right: ExprBox { + right: LocBox { inner: Literal( Number( Number { @@ -139,10 +139,10 @@ Program { col: 11, }, }, - right: ExprBox { + right: LocBox { inner: UnOp { typ: Star, - right: ExprBox { + right: LocBox { inner: Path( Path( [ @@ -182,22 +182,22 @@ Program { }, ), Statement( - StatementBox { + LocBox { inner: Let { name: Ident( "c", ), typ: None, val: Some( - ExprBox { + LocBox { inner: BinOp { typ: Div, - left: ExprBox { + left: LocBox { inner: Group( - ExprBox { + LocBox { inner: PtrFieldAccess { left: Some( - ExprBox { + LocBox { inner: Path( Path( [ @@ -214,10 +214,10 @@ Program { }, }, ), - right: ExprBox { + right: LocBox { inner: FieldAccess { left: Some( - ExprBox { + LocBox { inner: Path( Path( [ @@ -234,10 +234,10 @@ Program { }, }, ), - right: ExprBox { + right: LocBox { inner: PtrFieldAccess { left: Some( - ExprBox { + LocBox { inner: Path( Path( [ @@ -254,7 +254,7 @@ Program { }, }, ), - right: ExprBox { + right: LocBox { inner: Path( Path( [ @@ -298,7 +298,7 @@ Program { col: 10, }, }, - right: ExprBox { + right: LocBox { inner: Literal( Number( Number { @@ -331,17 +331,17 @@ Program { }, ), Statement( - StatementBox { + LocBox { inner: Let { name: Ident( "d", ), typ: None, val: Some( - ExprBox { + LocBox { inner: BinOp { typ: Div, - left: ExprBox { + left: LocBox { inner: Literal( Number( Number { @@ -357,10 +357,10 @@ Program { col: 10, }, }, - right: ExprBox { + right: LocBox { inner: PtrFieldAccess { left: Some( - ExprBox { + LocBox { inner: Path( Path( [ @@ -377,10 +377,10 @@ Program { }, }, ), - right: ExprBox { + right: LocBox { inner: FieldAccess { left: Some( - ExprBox { + LocBox { inner: Path( Path( [ @@ -397,10 +397,10 @@ Program { }, }, ), - right: ExprBox { + right: LocBox { inner: PtrFieldAccess { left: Some( - ExprBox { + LocBox { inner: Path( Path( [ @@ -417,7 +417,7 @@ Program { }, }, ), - right: ExprBox { + right: LocBox { inner: Path( Path( [ @@ -471,20 +471,20 @@ Program { }, ), Statement( - StatementBox { + LocBox { inner: Let { name: Ident( "e", ), typ: None, val: Some( - ExprBox { + LocBox { inner: BinOp { typ: Div, - left: ExprBox { + left: LocBox { inner: PtrFieldAccess { left: Some( - ExprBox { + LocBox { inner: Path( Path( [ @@ -501,10 +501,10 @@ Program { }, }, ), - right: ExprBox { + right: LocBox { inner: FieldAccess { left: Some( - ExprBox { + LocBox { inner: Path( Path( [ @@ -521,10 +521,10 @@ Program { }, }, ), - right: ExprBox { + right: LocBox { inner: PtrFieldAccess { left: Some( - ExprBox { + LocBox { inner: Path( Path( [ @@ -541,7 +541,7 @@ Program { }, }, ), - right: ExprBox { + right: LocBox { inner: Path( Path( [ @@ -578,7 +578,7 @@ Program { col: 11, }, }, - right: ExprBox { + right: LocBox { inner: Literal( Number( Number { @@ -611,20 +611,20 @@ Program { }, ), Statement( - StatementBox { + LocBox { inner: Let { name: Ident( "f", ), typ: None, val: Some( - ExprBox { + LocBox { inner: BinOp { typ: Div, - left: ExprBox { + left: LocBox { inner: FieldAccess { left: Some( - ExprBox { + LocBox { inner: Path( Path( [ @@ -641,10 +641,10 @@ Program { }, }, ), - right: ExprBox { + right: LocBox { inner: FieldAccess { left: Some( - ExprBox { + LocBox { inner: Path( Path( [ @@ -661,10 +661,10 @@ Program { }, }, ), - right: ExprBox { + right: LocBox { inner: FieldAccess { left: Some( - ExprBox { + LocBox { inner: Path( Path( [ @@ -681,7 +681,7 @@ Program { }, }, ), - right: ExprBox { + right: LocBox { inner: Path( Path( [ @@ -718,7 +718,7 @@ Program { col: 11, }, }, - right: ExprBox { + right: LocBox { inner: Literal( Number( Number { @@ -751,22 +751,22 @@ Program { }, ), Statement( - StatementBox { + LocBox { inner: Let { name: Ident( "g", ), typ: None, val: Some( - ExprBox { + LocBox { inner: BinOp { typ: Star, - left: ExprBox { + left: LocBox { inner: ArrayIndex { - name: ExprBox { + name: LocBox { inner: FieldAccess { left: Some( - ExprBox { + LocBox { inner: Path( Path( [ @@ -783,7 +783,7 @@ Program { }, }, ), - right: ExprBox { + right: LocBox { inner: Path( Path( [ @@ -806,13 +806,13 @@ Program { col: 11, }, }, - index: ExprBox { + index: LocBox { inner: UnOp { typ: Star, - right: ExprBox { + right: LocBox { inner: FieldAccess { left: Some( - ExprBox { + LocBox { inner: Path( Path( [ @@ -829,7 +829,7 @@ Program { }, }, ), - right: ExprBox { + right: LocBox { inner: Path( Path( [ @@ -866,7 +866,7 @@ Program { col: 13, }, }, - right: ExprBox { + right: LocBox { inner: Literal( Number( Number { diff --git a/tests/parser/functions.exp b/tests/parser/functions.exp index bc3aaed..f85d93f 100644 --- a/tests/parser/functions.exp +++ b/tests/parser/functions.exp @@ -2,7 +2,7 @@ Program { ast: Block( [ Statement( - StatementBox { + LocBox { inner: Fn( Function { struct_name: None, @@ -14,7 +14,7 @@ Program { Ident( "argc", ), - TypeBox { + LocBox { inner: Owned( Ident( "i32", @@ -31,7 +31,7 @@ Program { Ident( "argv", ), - TypeBox { + LocBox { inner: Ref { inner: Array { inner: Owned( @@ -51,7 +51,7 @@ Program { ), ], ret_type: Some( - TypeBox { + LocBox { inner: Owned( Ident( "i32", @@ -70,10 +70,10 @@ Program { Block( [ Expr( - ExprBox { + LocBox { inner: Return( Some( - ExprBox { + LocBox { inner: Literal( Number( Number { @@ -111,7 +111,7 @@ Program { }, ), Statement( - StatementBox { + LocBox { inner: Fn( Function { struct_name: Some( @@ -127,7 +127,7 @@ Program { Ident( "self", ), - TypeBox { + LocBox { inner: Ref { inner: Owned( Ident( @@ -147,7 +147,7 @@ Program { Ident( "a", ), - TypeBox { + LocBox { inner: Ref { inner: Owned( Ident( @@ -167,7 +167,7 @@ Program { Ident( "b", ), - TypeBox { + LocBox { inner: Ref { inner: Owned( Ident( @@ -185,7 +185,7 @@ Program { ), ], ret_type: Some( - TypeBox { + LocBox { inner: Ref { inner: Owned( Ident( @@ -214,7 +214,7 @@ Program { }, ), Statement( - StatementBox { + LocBox { inner: Fn( Function { struct_name: Some( @@ -230,7 +230,7 @@ Program { Ident( "a", ), - TypeBox { + LocBox { inner: Ref { inner: Owned( Ident( @@ -250,7 +250,7 @@ Program { Ident( "b", ), - TypeBox { + LocBox { inner: Ref { inner: Owned( Ident( @@ -268,7 +268,7 @@ Program { ), ], ret_type: Some( - TypeBox { + LocBox { inner: Ref { inner: Owned( Ident( diff --git a/tests/parser/if-statements.exp b/tests/parser/if-statements.exp index b61050e..d0dd1b3 100644 --- a/tests/parser/if-statements.exp +++ b/tests/parser/if-statements.exp @@ -2,13 +2,13 @@ Program { ast: Block( [ Expr( - ExprBox { + LocBox { inner: If( IfExpr { - test: ExprBox { + test: LocBox { inner: BinOp { typ: Gt, - left: ExprBox { + left: LocBox { inner: Path( Path( [ @@ -24,7 +24,7 @@ Program { col: 5, }, }, - right: ExprBox { + right: LocBox { inner: Literal( Number( Number { diff --git a/tests/parser/loops.exp b/tests/parser/loops.exp index 4158a85..e20480c 100644 --- a/tests/parser/loops.exp +++ b/tests/parser/loops.exp @@ -2,17 +2,17 @@ Program { ast: Block( [ Expr( - ExprBox { + LocBox { inner: ForLoop { init: Statement( - StatementBox { + LocBox { inner: Let { name: Ident( "i", ), typ: None, val: Some( - ExprBox { + LocBox { inner: Literal( Number( Number { @@ -37,10 +37,10 @@ Program { }, }, ), - test: ExprBox { + test: LocBox { inner: BinOp { typ: Lt, - left: ExprBox { + left: LocBox { inner: Path( Path( [ @@ -56,7 +56,7 @@ Program { col: 17, }, }, - right: ExprBox { + right: LocBox { inner: Literal( Number( Number { @@ -79,10 +79,10 @@ Program { col: 19, }, }, - on_loop: ExprBox { + on_loop: LocBox { inner: BinOp { typ: AddEq, - left: ExprBox { + left: LocBox { inner: Path( Path( [ @@ -98,7 +98,7 @@ Program { col: 25, }, }, - right: ExprBox { + right: LocBox { inner: Literal( Number( Number { @@ -133,12 +133,12 @@ Program { }, ), Expr( - ExprBox { + LocBox { inner: WhileLoop { - test: ExprBox { + test: LocBox { inner: BinOp { typ: Gt, - left: ExprBox { + left: LocBox { inner: Path( Path( [ @@ -154,7 +154,7 @@ Program { col: 8, }, }, - right: ExprBox { + right: LocBox { inner: Literal( Number( Number { @@ -189,7 +189,7 @@ Program { }, ), Expr( - ExprBox { + LocBox { inner: InfLoop { body: Block( [], diff --git a/tests/parser/structs.exp b/tests/parser/structs.exp index 7778913..259db54 100644 --- a/tests/parser/structs.exp +++ b/tests/parser/structs.exp @@ -2,7 +2,7 @@ Program { ast: Block( [ Statement( - StatementBox { + LocBox { inner: Struct( Struct { name: Ident( @@ -19,7 +19,7 @@ Program { }, ), Statement( - StatementBox { + LocBox { inner: Struct( Struct { name: Ident( @@ -30,7 +30,7 @@ Program { Ident( "a", ), - TypeBox { + LocBox { inner: Ref { inner: Owned( Ident(