From 0e4f4c5af67b4075174ace5a1c7800dae60a9d11 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 11 Nov 2019 10:52:57 +0000 Subject: Move Span definition to its own file And prepare for more variants --- dhall_syntax/src/core/span.rs | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 dhall_syntax/src/core/span.rs (limited to 'dhall_syntax/src/core/span.rs') diff --git a/dhall_syntax/src/core/span.rs b/dhall_syntax/src/core/span.rs new file mode 100644 index 0000000..ad5a3a9 --- /dev/null +++ b/dhall_syntax/src/core/span.rs @@ -0,0 +1,44 @@ +use std::rc::Rc; + +/// A location in the source text +#[derive(Debug, Clone)] +pub struct ParsedSpan { + input: Rc, + /// # Safety + /// + /// Must be a valid character boundary index into `input`. + start: usize, + /// # Safety + /// + /// Must be a valid character boundary index into `input`. + end: usize, +} + +/// A location in the source text +#[derive(Debug, Clone)] +pub enum Span { + Parsed(ParsedSpan), +} + +impl Span { + pub(crate) fn make(input: Rc, sp: pest::Span) -> Self { + Span::Parsed(ParsedSpan { + input, + start: sp.start(), + end: sp.end(), + }) + } + /// 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. + pub fn union(&self, other: &Span) -> Self { + use std::cmp::{max, min}; + use Span::*; + match (self, other) { + (Parsed(x), Parsed(y)) => Span::Parsed(ParsedSpan { + input: x.input.clone(), + start: min(x.start, y.start), + end: max(x.start, y.start), + }), + } + } +} -- cgit v1.2.3