Skip to content

Commit

Permalink
refactor: simplify Spanned<T> struct to Pos<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestrew committed Dec 27, 2023
1 parent 9bddfc0 commit 859fc80
Show file tree
Hide file tree
Showing 99 changed files with 426 additions and 460 deletions.
40 changes: 20 additions & 20 deletions src/ast/expressions.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::fmt::Debug;

use super::*;
use crate::diagnostics::SpannedError;
use crate::diagnostics::PosError;
use crate::lexer::{Token, TokenKind};

pub type ExprResult<'source> = Result<Expression<'source>, SpannedError>;
pub type ExprResult<'source> = Result<Expression<'source>, PosError>;

#[derive(PartialEq)]
pub enum Expression<'source> {
Expand Down Expand Up @@ -58,7 +58,7 @@ macro_rules! match_methods {
}

impl<'source> NodeError for Expression<'source> {
fn errors(&self) -> Vec<SpannedError> {
fn errors(&self) -> Vec<PosError> {
match_methods!(self, errors)
}
}
Expand Down Expand Up @@ -108,7 +108,7 @@ impl<'source> Identifier<'source> {
}

impl<'source> NodeError for Identifier<'source> {
fn errors(&self) -> Vec<SpannedError> {
fn errors(&self) -> Vec<PosError> {
vec![]
}
}
Expand Down Expand Up @@ -149,7 +149,7 @@ impl<'source> From<Token<'source>> for Primative<'source> {
}

impl<'source> NodeError for Primative<'source> {
fn errors(&self) -> Vec<SpannedError> {
fn errors(&self) -> Vec<PosError> {
vec![]
}
}
Expand All @@ -171,7 +171,7 @@ impl<'source> From<Token<'source>> for StringLiteral<'source> {
}

impl<'source> NodeError for StringLiteral<'source> {
fn errors(&self) -> Vec<SpannedError> {
fn errors(&self) -> Vec<PosError> {
vec![]
}
}
Expand Down Expand Up @@ -255,7 +255,7 @@ impl<'source> Prefix<'source> {
}

impl<'source> NodeError for Prefix<'source> {
fn errors(&self) -> Vec<SpannedError> {
fn errors(&self) -> Vec<PosError> {
self.right.errors()
}
}
Expand Down Expand Up @@ -293,7 +293,7 @@ impl<'source> Infix<'source> {
}

impl<'source> NodeError for Infix<'source> {
fn errors(&self) -> Vec<SpannedError> {
fn errors(&self) -> Vec<PosError> {
let mut errors = Vec::new();
errors.extend(self.left.errors());
errors.extend(self.right.errors());
Expand All @@ -304,9 +304,9 @@ impl<'source> NodeError for Infix<'source> {
#[derive(Debug, PartialEq)]
pub struct If<'source> {
token: Token<'source>,
pub condition: Result<Box<Expression<'source>>, SpannedError>,
pub condition: Result<Box<Expression<'source>>, PosError>,
pub consequence: BlockResult<'source>,
pub alternative: Result<Option<Block<'source>>, SpannedError>,
pub alternative: Result<Option<Block<'source>>, PosError>,
range: Range,
}

Expand All @@ -315,7 +315,7 @@ impl<'source> If<'source> {
token: Token<'source>,
condition: ExprResult<'source>,
consequence: BlockResult<'source>,
alternative: Result<Option<Block<'source>>, SpannedError>,
alternative: Result<Option<Block<'source>>, PosError>,
end: Position,
) -> Self {
let condition = match condition {
Expand All @@ -340,7 +340,7 @@ impl<'source> If<'source> {
}

impl<'source> NodeError for If<'source> {
fn errors(&self) -> Vec<SpannedError> {
fn errors(&self) -> Vec<PosError> {
let mut errors = Vec::new();
if let Err(ref err) = self.condition {
errors.push(err.clone())
Expand All @@ -361,7 +361,7 @@ impl<'source> NodeError for If<'source> {
}
}

pub type VecExprResult<'source> = Result<Vec<ExprResult<'source>>, SpannedError>;
pub type VecExprResult<'source> = Result<Vec<ExprResult<'source>>, PosError>;

#[derive(Debug, PartialEq)]
pub struct Function<'source> {
Expand Down Expand Up @@ -393,7 +393,7 @@ impl<'source> Function<'source> {
}

impl<'source> NodeError for Function<'source> {
fn errors(&self) -> Vec<SpannedError> {
fn errors(&self) -> Vec<PosError> {
let mut errors = Vec::new();

match &self.params {
Expand Down Expand Up @@ -447,7 +447,7 @@ impl<'source> Call<'source> {
}

impl<'source> NodeError for Call<'source> {
fn errors(&self) -> Vec<SpannedError> {
fn errors(&self) -> Vec<PosError> {
let mut errors = Vec::new();

match &self.args {
Expand Down Expand Up @@ -489,7 +489,7 @@ impl<'source> Array<'source> {
}

impl<'source> NodeError for Array<'source> {
fn errors(&self) -> Vec<SpannedError> {
fn errors(&self) -> Vec<PosError> {
let mut errors = Vec::new();

match &self.elems {
Expand All @@ -513,14 +513,14 @@ pub type ExprPairs<'source> = (Expression<'source>, Expression<'source>);
#[derive(Debug, PartialEq)]
pub struct Hash<'source> {
token: Token<'source>,
pub kv_pairs: Result<Vec<Result<ExprPairs<'source>, SpannedError>>, SpannedError>,
pub kv_pairs: Result<Vec<Result<ExprPairs<'source>, PosError>>, PosError>,
range: Range,
}

impl<'source> Hash<'source> {
pub fn new(
token: Token<'source>,
kv_pairs: Result<Vec<Result<ExprPairs<'source>, SpannedError>>, SpannedError>,
kv_pairs: Result<Vec<Result<ExprPairs<'source>, PosError>>, PosError>,
end: Position,
) -> Self {
let range = Range::new(token.start, end);
Expand All @@ -537,7 +537,7 @@ impl<'source> Hash<'source> {
}

impl<'source> NodeError for Hash<'source> {
fn errors(&self) -> Vec<SpannedError> {
fn errors(&self) -> Vec<PosError> {
let mut errors = Vec::new();

match &self.kv_pairs {
Expand Down Expand Up @@ -590,7 +590,7 @@ impl<'source> Index<'source> {
}

impl<'source> NodeError for Index<'source> {
fn errors(&self) -> Vec<SpannedError> {
fn errors(&self) -> Vec<PosError> {
let mut errors = Vec::new();
errors.extend(self.object.errors());

Expand Down
8 changes: 4 additions & 4 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use expressions::*;
pub use statements::*;
use tower_lsp::lsp_types::{Position, Range};

use crate::diagnostics::SpannedError;
use crate::diagnostics::PosError;
use crate::lexer::Token;

pub struct Program<'source> {
Expand All @@ -33,7 +33,7 @@ impl<'source> Debug for Program<'source> {
}

pub trait NodeError {
fn errors(&self) -> Vec<SpannedError>;
fn errors(&self) -> Vec<PosError>;
}

pub trait Nodes {
Expand All @@ -44,7 +44,7 @@ pub trait Nodes {
#[derive(PartialEq)]
pub enum Node<'source> {
Statement(Statement<'source>),
Error(SpannedError),
Error(PosError),
}

impl<'source> Debug for Node<'source> {
Expand All @@ -57,7 +57,7 @@ impl<'source> Debug for Node<'source> {
}

impl<'source> NodeError for Node<'source> {
fn errors(&self) -> Vec<SpannedError> {
fn errors(&self) -> Vec<PosError> {
match &self {
Node::Statement(stmt) => stmt.errors(),
Node::Error(err) => vec![err.clone()],
Expand Down
14 changes: 7 additions & 7 deletions src/ast/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use std::fmt::Debug;
use tower_lsp::lsp_types::Range;

use super::*;
use crate::diagnostics::SpannedError;
use crate::diagnostics::PosError;
use crate::lexer::Token;

pub type StmtResult<'source> = Result<Statement<'source>, SpannedError>;
pub type BlockResult<'source> = Result<Block<'source>, SpannedError>;
pub type StmtResult<'source> = Result<Statement<'source>, PosError>;
pub type BlockResult<'source> = Result<Block<'source>, PosError>;

#[derive(PartialEq)]
pub enum Statement<'source> {
Expand Down Expand Up @@ -40,7 +40,7 @@ macro_rules! match_methods {
}

impl<'source> NodeError for Statement<'source> {
fn errors(&self) -> Vec<SpannedError> {
fn errors(&self) -> Vec<PosError> {
match_methods!(self, errors)
}
}
Expand Down Expand Up @@ -93,7 +93,7 @@ impl<'source> Let<'source> {
}

impl<'source> NodeError for Let<'source> {
fn errors(&self) -> Vec<SpannedError> {
fn errors(&self) -> Vec<PosError> {
self.value.errors()
}
}
Expand Down Expand Up @@ -121,7 +121,7 @@ impl<'source> Return<'source> {
}

impl<'source> NodeError for Return<'source> {
fn errors(&self) -> Vec<SpannedError> {
fn errors(&self) -> Vec<PosError> {
match &self.value {
Some(Ok(expr)) => expr.errors(),
Some(Err(err)) => vec![err.clone()],
Expand Down Expand Up @@ -153,7 +153,7 @@ impl<'source> Block<'source> {
}

impl<'source> NodeError for Block<'source> {
fn errors(&self) -> Vec<SpannedError> {
fn errors(&self) -> Vec<PosError> {
let mut errors = Vec::new();
for node in &self.statements {
match node {
Expand Down
28 changes: 14 additions & 14 deletions src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use tower_lsp::lsp_types::DiagnosticSeverity;

use crate::ast::Operator;
use crate::eval::Object;
use crate::spanned::Spanned;
use crate::pos::Pos;

pub type SpannedDiagnostic = Spanned<Diagnostics>;
pub type SpannedError = Spanned<MonkeyError>;
pub type SpannedWarning = Spanned<MonkeyWarning>;
pub type PosDiagnostic = Pos<Diagnostics>;
pub type PosError = Pos<MonkeyError>;
pub type PosWarning = Pos<MonkeyWarning>;

#[derive(Debug, Clone, PartialEq)]
pub enum Diagnostics {
Expand Down Expand Up @@ -35,7 +35,7 @@ impl std::fmt::Display for Diagnostics {
}
}

impl std::fmt::Debug for SpannedDiagnostic {
impl std::fmt::Debug for PosDiagnostic {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let msg = match &**self {
Diagnostics::Error(err) => format!("{:?}", err),
Expand Down Expand Up @@ -86,20 +86,20 @@ pub enum MonkeyError {

impl MonkeyError {
pub fn new_unknown_op<T>(
span: &Spanned<T>,
pos: &Pos<T>,
left_obj: &Object,
right_obj: &Object,
op: Operator,
) -> SpannedError {
span.map(Self::UnknownOperator(
) -> PosError {
pos.map(Self::UnknownOperator(
left_obj.typename(),
right_obj.typename(),
op.as_str(),
))
}
}

impl std::fmt::Debug for SpannedError {
impl std::fmt::Debug for PosError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let err = match &**self {
MonkeyError::UnexpectedToken(_) => format!("UnexpectedToken(\"{}\")", **self),
Expand Down Expand Up @@ -127,8 +127,8 @@ impl From<MonkeyError> for Diagnostics {
}
}

impl From<SpannedError> for SpannedDiagnostic {
fn from(value: SpannedError) -> Self {
impl From<PosError> for PosDiagnostic {
fn from(value: PosError) -> Self {
value.transform()
}
}
Expand All @@ -142,7 +142,7 @@ pub enum MonkeyWarning {
UnreachableCode,
}

impl std::fmt::Debug for SpannedWarning {
impl std::fmt::Debug for PosWarning {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let err = match &**self {
MonkeyWarning::UnusedExpression => format!("UnusedExpression(\"{}\")", **self),
Expand All @@ -158,8 +158,8 @@ impl From<MonkeyWarning> for Diagnostics {
}
}

impl From<SpannedWarning> for SpannedDiagnostic {
fn from(value: SpannedWarning) -> Self {
impl From<PosWarning> for PosDiagnostic {
fn from(value: PosWarning) -> Self {
value.transform()
}
}
Loading

0 comments on commit 859fc80

Please sign in to comment.