From 207d10c4b7d838d712b135dca56bc31f3fe5b648 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 11 Nov 2019 13:42:31 +0000 Subject: Clarify Span::union and add Span::merge --- dhall_syntax/src/core/span.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'dhall_syntax') diff --git a/dhall_syntax/src/core/span.rs b/dhall_syntax/src/core/span.rs index d10a53d..4012cfb 100644 --- a/dhall_syntax/src/core/span.rs +++ b/dhall_syntax/src/core/span.rs @@ -33,8 +33,9 @@ impl Span { }) } - /// Takes the union of the two spans. Assumes that the spans come from the same input. - /// This will also capture any input between the spans. + /// Takes the union of the two spans, i.e. the range of input covered by the two spans plus any + /// input between them. Assumes that the spans come from the same input. Fails if one of the + /// spans does not point to an input location. pub fn union(&self, other: &Span) -> Self { use std::cmp::{max, min}; use Span::*; @@ -51,6 +52,17 @@ impl Span { } } + /// Merges two spans assumed to point to a similar thing. If only one of them points to an + /// input location, use that one. + pub fn merge(&self, other: &Span) -> Self { + use Span::*; + match (self, other) { + (Parsed(x), _) | (_, Parsed(x)) => Parsed(x.clone()), + (Artificial, _) | (_, Artificial) => Artificial, + (Decoded, Decoded) => Decoded, + } + } + pub fn error(&self, message: impl Into) -> String { use pest::error::{Error, ErrorVariant}; use pest::Span; -- cgit v1.2.3