Skip to content

Commit

Permalink
Rename Span to SourceSpan and switch it to offset+length based repres…
Browse files Browse the repository at this point in the history
…entation;
  • Loading branch information
greenhat committed Aug 8, 2023
1 parent f744c67 commit 3b16a07
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
12 changes: 6 additions & 6 deletions ergotree-interpreter/src/eval/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ergotree_ir::ergo_tree::ErgoTreeError;
use ergotree_ir::mir::constant::TryExtractFromError;
use ergotree_ir::serialization::SigmaParsingError;
use ergotree_ir::serialization::SigmaSerializationError;
use ergotree_ir::source_span::Span;
use ergotree_ir::source_span::SourceSpan;
use sigma_ser::ScorexParsingError;
use sigma_ser::ScorexSerializationError;
use thiserror::Error;
Expand Down Expand Up @@ -78,7 +78,7 @@ pub enum EvalError {
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct EvalErrorDetails {
/// source span
source_span: Span,
source_span: SourceSpan,
/// environment after evaluation
env: Env,
/// source code
Expand All @@ -87,7 +87,7 @@ pub struct EvalErrorDetails {

impl EvalError {
/// Wrap eval error with source span
pub fn wrap(self, source_span: Span, env: Env) -> Self {
pub fn wrap(self, source_span: SourceSpan, env: Env) -> Self {
EvalError::Wrapped {
error: Box::new(self),
details: EvalErrorDetails {
Expand All @@ -112,7 +112,7 @@ impl EvalError {
e => EvalError::Wrapped {
error: Box::new(e),
details: EvalErrorDetails {
source_span: Span::empty(),
source_span: SourceSpan::empty(),
env: Env::empty(),
source: Some(source),
},
Expand All @@ -122,11 +122,11 @@ impl EvalError {
}

pub trait ExtResultEvalError<T> {
fn enrich_err(self, span: Span, env: Env) -> Result<T, EvalError>;
fn enrich_err(self, span: SourceSpan, env: Env) -> Result<T, EvalError>;
}

impl<T> ExtResultEvalError<T> for Result<T, EvalError> {
fn enrich_err(self, span: Span, env: Env) -> Result<T, EvalError> {
fn enrich_err(self, span: SourceSpan, env: Env) -> Result<T, EvalError> {
self.map_err(|e| e.wrap(span, env))
}
}
20 changes: 10 additions & 10 deletions ergotree-ir/src/pretty_printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::mir::constant::Constant;
use crate::mir::expr::Expr;
use crate::mir::val_def::ValDef;
use crate::mir::val_use::ValUse;
use crate::source_span::Span;
use crate::source_span::SourceSpan;
use crate::source_span::Spanned;

/// Print error
Expand All @@ -29,7 +29,7 @@ pub trait Print {

impl Print for BlockValue {
fn print(&self, w: &mut dyn Printer) -> Result<Expr, PrintError> {
let start = w.current_pos();
let offset = w.current_pos();
writeln!(w, "{{")?;
w.inc_ident();
let indent = w.get_indent();
Expand All @@ -43,9 +43,9 @@ impl Print for BlockValue {
self.result.print(w)?;
w.dec_ident();
writeln!(w, "\n}}")?;
let end = w.current_pos();
let length = w.current_pos() - offset;
Ok(Spanned {
source_span: Span { start, end },
source_span: SourceSpan { offset, length },
expr: self.clone(),
}
.into())
Expand All @@ -54,12 +54,12 @@ impl Print for BlockValue {

impl Print for ValDef {
fn print(&self, w: &mut dyn Printer) -> Result<Expr, PrintError> {
let start = w.current_pos();
let offset = w.current_pos();
write!(w, "val v{} = ", self.id)?;
self.rhs.print(w)?;
let end = w.current_pos();
let length = w.current_pos() - offset;
Ok(Spanned {
source_span: Span { start, end },
source_span: SourceSpan { offset, length },
expr: self.clone(),
}
.into())
Expand All @@ -83,14 +83,14 @@ impl Print for ValUse {

impl Print for Append {
fn print(&self, w: &mut dyn Printer) -> Result<Expr, PrintError> {
let start = w.current_pos();
let offset = w.current_pos();
self.input.print(w)?;
write!(w, ".append(")?;
self.col_2.print(w)?;
write!(w, ")")?;
let end = w.current_pos();
let length = w.current_pos() - offset;
Ok(Spanned {
source_span: Span { start, end },
source_span: SourceSpan { offset, length },
expr: self.clone(),
}
.into())
Expand Down
28 changes: 16 additions & 12 deletions ergotree-ir/src/source_span.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
//! Source position for an IR node in the source code

use crate::mir::val_def::ValDef;
use crate::mir::block::BlockValue;
use crate::mir::coll_append::Append;
use crate::mir::expr::Expr;
use crate::mir::val_def::ValDef;

/// Source position for the Expr
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct Span {
/// Start position in the source code
pub start: usize,
/// End position in the source code
pub end: usize,
pub struct SourceSpan {
/// Start position in the span
pub offset: usize,
/// The length of the span
pub length: usize,
}

impl Span {
impl SourceSpan {
/// Empty span
pub fn empty() -> Self {
Span { start: 0, end: 0 }
SourceSpan {
offset: 0,
length: 0,
}
}
}


/// Wrapper for Expr with source position
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct Spanned<T> {
/// Source position
pub source_span: Span,
pub source_span: SourceSpan,
/// Wrapped value
pub expr: T,
}
Expand All @@ -42,7 +46,7 @@ macro_rules! into_expr {
impl From<$variant> for Expr {
fn from(v: $variant) -> Self {
Expr::$variant(Spanned {
source_span: Span::empty(),
source_span: SourceSpan::empty(),
expr: v,
})
}
Expand All @@ -57,7 +61,7 @@ into_expr!(ValDef);
impl<T> From<T> for Spanned<T> {
fn from(v: T) -> Self {
Spanned {
source_span: Span::empty(),
source_span: SourceSpan::empty(),
expr: v,
}
}
Expand All @@ -66,7 +70,7 @@ impl<T> From<T> for Spanned<T> {
impl Expr {
/// Source span for the Expr
#[allow(clippy::todo)]
pub fn span(&self) -> &Span {
pub fn span(&self) -> &SourceSpan {
match self {
Expr::Append(op) => &op.source_span,
Expr::Const(_) => todo!(),
Expand Down

0 comments on commit 3b16a07

Please sign in to comment.