diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-09-30 14:38:46 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-09-30 14:38:46 +0200 |
| commit | 3c3730425f0a9a4241c4f57cb7f4d00b71db201e (patch) | |
| tree | 8e63405574bc3909dddc2d3e8402a66bb2a8f059 /src/syntax | |
| parent | 7143e10afccc7beef22646f6c7355075f97afb2c (diff) | |
SpanWith trait ↔
Diffstat (limited to 'src/syntax')
| -rw-r--r-- | src/syntax/span.rs | 10 | ||||
| -rw-r--r-- | src/syntax/tree.rs | 8 |
2 files changed, 14 insertions, 4 deletions
diff --git a/src/syntax/span.rs b/src/syntax/span.rs index 6f5420d4..cda35ec0 100644 --- a/src/syntax/span.rs +++ b/src/syntax/span.rs @@ -17,6 +17,16 @@ pub trait Offset { fn offset(self, by: Pos) -> Self; } +/// Annotate a value with a span. +pub trait SpanWith: Sized { + /// Wraps `self` in a `Spanned` with the given span. + fn span_with(self, span: Span) -> Spanned<Self> { + Spanned::new(self, span) + } +} + +impl<T> SpanWith for T {} + /// A vector of spanned values of type `T`. pub type SpanVec<T> = Vec<Spanned<T>>; diff --git a/src/syntax/tree.rs b/src/syntax/tree.rs index 8c4270e2..5327bfa4 100644 --- a/src/syntax/tree.rs +++ b/src/syntax/tree.rs @@ -2,7 +2,7 @@ use std::fmt::{self, Debug, Formatter}; -use super::span::{SpanVec, Spanned}; +use super::span::{SpanVec, SpanWith, Spanned}; use super::Decoration; use crate::color::RgbaColor; use crate::compute::table::{SpannedEntry, Table}; @@ -203,7 +203,7 @@ impl TableExpr { for (key, entry) in self.iter() { let val = entry.val.v.eval(ctx, f).await; - let spanned = Spanned::new(val, entry.val.span); + let spanned = val.span_with(entry.val.span); let entry = SpannedEntry::new(entry.key, spanned); table.insert(key, entry); } @@ -230,12 +230,12 @@ impl CallExpr { if let Some(func) = ctx.scope.func(name) { let pass = func(span, args, ctx.clone()).await; f.extend(pass.feedback); - f.decorations.push(Spanned::new(Decoration::Resolved, span)); + f.decorations.push(Decoration::Resolved.span_with(span)); pass.output } else { if !name.is_empty() { error!(@f, span, "unknown function"); - f.decorations.push(Spanned::new(Decoration::Unresolved, span)); + f.decorations.push(Decoration::Unresolved.span_with(span)); } Value::Table(args) } |
