Skip to content

Commit

Permalink
Move source_span module to the ergotree-ir crate root;
Browse files Browse the repository at this point in the history
rename SourceSpan to Span and wrapper to Spanned;
  • Loading branch information
greenhat committed Jul 24, 2023
1 parent 801660d commit af665a1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 26 deletions.
6 changes: 3 additions & 3 deletions ergotree-interpreter/src/eval.rs
Original file line number Diff line number Diff line change
@@ -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;

Check failure on line 4 in ergotree-interpreter/src/eval.rs

View workflow job for this annotation

GitHub Actions / clippy

unresolved import `ergotree_ir::mir::expr::Span`

error[E0432]: unresolved import `ergotree_ir::mir::expr::Span` --> ergotree-interpreter/src/eval.rs:4:5 | 4 | use ergotree_ir::mir::expr::Span; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `Span` in `mir::expr` | help: consider importing this struct instead | 4 | use ergotree_ir::source_span::Span; | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Check failure on line 4 in ergotree-interpreter/src/eval.rs

View workflow job for this annotation

GitHub Actions / Build without default features

unresolved import `ergotree_ir::mir::expr::Span`

Check failure on line 4 in ergotree-interpreter/src/eval.rs

View workflow job for this annotation

GitHub Actions / Check intra-documentation links

unresolved import `ergotree_ir::mir::expr::Span`

Check failure on line 4 in ergotree-interpreter/src/eval.rs

View workflow job for this annotation

GitHub Actions / Tests on ubuntu-latest

unresolved import `ergotree_ir::mir::expr::Span`
use ergotree_ir::sigma_protocol::sigma_boolean::SigmaProp;
use sigma_ser::ScorexParsingError;
use sigma_ser::ScorexSerializationError;
Expand Down Expand Up @@ -170,13 +170,13 @@ pub enum EvalError {
/// eval error
error: Box<EvalError>,
/// 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,
Expand Down
6 changes: 3 additions & 3 deletions ergotree-interpreter/src/eval/expr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ergotree_ir::mir::expr::Expr;
use ergotree_ir::mir::expr::SourceSpan;
use ergotree_ir::mir::expr::Span;

Check failure on line 2 in ergotree-interpreter/src/eval/expr.rs

View workflow job for this annotation

GitHub Actions / clippy

unresolved import `ergotree_ir::mir::expr::Span`

error[E0432]: unresolved import `ergotree_ir::mir::expr::Span` --> ergotree-interpreter/src/eval/expr.rs:2:5 | 2 | use ergotree_ir::mir::expr::Span; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `Span` in `mir::expr` | help: consider importing one of these items instead | 2 | use crate::eval::Span; | ~~~~~~~~~~~~~~~~~~ 2 | use ergotree_ir::source_span::Span; | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Check failure on line 2 in ergotree-interpreter/src/eval/expr.rs

View workflow job for this annotation

GitHub Actions / Build without default features

unresolved import `ergotree_ir::mir::expr::Span`

Check failure on line 2 in ergotree-interpreter/src/eval/expr.rs

View workflow job for this annotation

GitHub Actions / Check intra-documentation links

unresolved import `ergotree_ir::mir::expr::Span`

Check failure on line 2 in ergotree-interpreter/src/eval/expr.rs

View workflow job for this annotation

GitHub Actions / Tests on ubuntu-latest

unresolved import `ergotree_ir::mir::expr::Span`
use ergotree_ir::mir::value::Value;

use super::Env;
Expand Down Expand Up @@ -86,11 +86,11 @@ impl Evaluable for Expr {
}

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

impl<T> ExtResultEvalError<T> for Result<T, EvalError> {
fn with_span(self, span: &SourceSpan) -> Result<T, EvalError> {
fn with_span(self, span: &Span) -> Result<T, EvalError> {
self.map_err(|e| e.wrap_with_span(span.clone()))
}
}
1 change: 1 addition & 0 deletions ergotree-ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 2 additions & 4 deletions ergotree-ir/src/mir/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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>),
Append(Spanned<Append>),
/// Constant value
Const(Constant),
/// Placeholder for a constant
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T> {
pub struct Spanned<T> {
/// Source position
pub source_span: SourceSpan,
pub source_span: Span,
/// Wrapped value
pub expr: T,
}

impl<T> SourceSpanWrapper<T> {
impl<T> Spanned<T> {
/// Expression
pub fn expr(&self) -> &T {
&self.expr
Expand All @@ -37,21 +38,18 @@ impl<T> SourceSpanWrapper<T> {
// TODO: can be a macros
impl From<Append> for Expr {
fn from(v: Append) -> Self {
Expr::Append(SourceSpanWrapper {
source_span: SourceSpan::empty(),
Expr::Append(Spanned {
source_span: Span::empty(),
expr: v,
})
}
}

impl<T> From<T> for SourceSpanWrapper<T> {
impl<T> From<T> for Spanned<T> {
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

0 comments on commit af665a1

Please sign in to comment.