diff --git a/ergotree-interpreter/src/eval.rs b/ergotree-interpreter/src/eval.rs index 1a5ebfcd5..dd9f03a9f 100644 --- a/ergotree-interpreter/src/eval.rs +++ b/ergotree-interpreter/src/eval.rs @@ -1,7 +1,7 @@ //! Interpreter use bounded_vec::BoundedVecOutOfBounds; use ergotree_ir::mir::constant::TryExtractInto; -use ergotree_ir::mir::expr::SourceSpan; +use ergotree_ir::mir::expr::Span; use ergotree_ir::sigma_protocol::sigma_boolean::SigmaProp; use sigma_ser::ScorexParsingError; use sigma_ser::ScorexSerializationError; @@ -170,13 +170,13 @@ pub enum EvalError { /// eval error error: Box, /// source span - source_span: SourceSpan, + source_span: Span, }, } impl EvalError { /// Wrap eval error with source span - pub fn wrap_with_span(self, source_span: SourceSpan) -> Self { + pub fn wrap_with_span(self, source_span: Span) -> Self { EvalError::WrappedWithSpan { error: Box::new(self), source_span, diff --git a/ergotree-interpreter/src/eval/expr.rs b/ergotree-interpreter/src/eval/expr.rs index 8dd436445..074ec6bda 100644 --- a/ergotree-interpreter/src/eval/expr.rs +++ b/ergotree-interpreter/src/eval/expr.rs @@ -1,5 +1,5 @@ use ergotree_ir::mir::expr::Expr; -use ergotree_ir::mir::expr::SourceSpan; +use ergotree_ir::mir::expr::Span; use ergotree_ir::mir::value::Value; use super::Env; @@ -86,11 +86,11 @@ impl Evaluable for Expr { } pub trait ExtResultEvalError { - fn with_span(self, span: &SourceSpan) -> Result; + fn with_span(self, span: &Span) -> Result; } impl ExtResultEvalError for Result { - fn with_span(self, span: &SourceSpan) -> Result { + fn with_span(self, span: &Span) -> Result { self.map_err(|e| e.wrap_with_span(span.clone())) } } diff --git a/ergotree-ir/src/lib.rs b/ergotree-ir/src/lib.rs index 9f81748ec..b2ac46230 100644 --- a/ergotree-ir/src/lib.rs +++ b/ergotree-ir/src/lib.rs @@ -28,6 +28,7 @@ pub mod ergo_tree; pub mod mir; pub mod serialization; pub mod sigma_protocol; +pub mod source_span; pub mod type_check; pub mod types; pub mod util; diff --git a/ergotree-ir/src/mir/expr.rs b/ergotree-ir/src/mir/expr.rs index 7b1943e65..f59b0cbb1 100644 --- a/ergotree-ir/src/mir/expr.rs +++ b/ergotree-ir/src/mir/expr.rs @@ -3,6 +3,7 @@ use std::convert::TryFrom; use std::convert::TryInto; +use crate::source_span::Spanned; use crate::types::stype::LiftIntoSType; use crate::types::stype::SType; @@ -80,14 +81,11 @@ use derive_more::From; use derive_more::TryInto; use thiserror::Error; -mod source_span; -pub use source_span::*; - #[derive(PartialEq, Eq, Debug, Clone, From, TryInto)] /// Expression in ErgoTree pub enum Expr { /// Append - Concatenation of two collections - Append(SourceSpanWrapper), + Append(Spanned), /// Constant value Const(Constant), /// Placeholder for a constant diff --git a/ergotree-ir/src/mir/expr/source_span.rs b/ergotree-ir/src/source_span.rs similarity index 55% rename from ergotree-ir/src/mir/expr/source_span.rs rename to ergotree-ir/src/source_span.rs index 07cac9179..6e1930a6b 100644 --- a/ergotree-ir/src/mir/expr/source_span.rs +++ b/ergotree-ir/src/source_span.rs @@ -1,33 +1,34 @@ -use crate::mir::coll_append::Append; +//! Source position for an IR node in the source code -use super::Expr; +use crate::mir::coll_append::Append; +use crate::mir::expr::Expr; /// Source position for the Expr #[derive(PartialEq, Eq, Debug, Clone)] -pub struct SourceSpan { +pub struct Span { /// Start position in the source code pub start: usize, /// End position in the source code pub end: usize, } -impl SourceSpan { +impl Span { /// Empty span pub fn empty() -> Self { - SourceSpan { start: 0, end: 0 } + Span { start: 0, end: 0 } } } /// Wrapper for Expr with source position #[derive(PartialEq, Eq, Debug, Clone)] -pub struct SourceSpanWrapper { +pub struct Spanned { /// Source position - pub source_span: SourceSpan, + pub source_span: Span, /// Wrapped value pub expr: T, } -impl SourceSpanWrapper { +impl Spanned { /// Expression pub fn expr(&self) -> &T { &self.expr @@ -37,21 +38,18 @@ impl SourceSpanWrapper { // TODO: can be a macros impl From for Expr { fn from(v: Append) -> Self { - Expr::Append(SourceSpanWrapper { - source_span: SourceSpan::empty(), + Expr::Append(Spanned { + source_span: Span::empty(), expr: v, }) } } -impl From for SourceSpanWrapper { +impl From for Spanned { fn from(v: T) -> Self { - SourceSpanWrapper { - source_span: SourceSpan::empty(), + Spanned { + source_span: Span::empty(), expr: v, } } } - -// TODO: draft pretty printer and how it's sets source span for every expr -// TODO: draft enriching eval errors with source span and hightlight it in the source code piece