1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
// pest. The Elegant Parser // Copyright (C) 2017 Dragoș Tiselice // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. use std::cmp::Ordering; use std::fmt; use std::hash::{Hash, Hasher}; use std::rc::Rc; use super::input::Input; use super::position; /// A `struct` of a span over an `Input`. It is created from either /// [two `Position`s](struct.Position.html#method.span) or from a /// [`Pair`](../iterators/struct.Pair.html#method.span). pub struct Span<I: Input> { input: Rc<I>, start: usize, end: usize } #[inline] pub fn new<I: Input>(input: Rc<I>, start: usize, end: usize) -> Span<I> { Span { input, start, end } } impl<I: Input> Span<I> { /// Returns the `Span`'s start byte position as a `usize`. /// /// # Examples /// /// ``` /// # use std::rc::Rc; /// # use pest::inputs::{Position, StringInput}; /// let input = Rc::new(StringInput::new("ab".to_owned())); /// let start = Position::from_start(input); /// let end = start.clone().match_string("ab").unwrap(); /// let span = start.span(end); /// /// assert_eq!(span.start(), 0); /// ``` #[inline] pub fn start(&self) -> usize { self.start } /// Returns the `Span`'s end byte position as a `usize`. /// /// # Examples /// /// ``` /// # use std::rc::Rc; /// # use pest::inputs::{Position, StringInput}; /// let input = Rc::new(StringInput::new("ab".to_owned())); /// let start = Position::from_start(input); /// let end = start.clone().match_string("ab").unwrap(); /// let span = start.span(end); /// /// assert_eq!(span.end(), 2); /// ``` #[inline] pub fn end(&self) -> usize { self.end } /// Returns the `Span`'s start `Position`. /// /// # Examples /// /// ``` /// # use std::rc::Rc; /// # use pest::inputs::{Position, StringInput}; /// let input = Rc::new(StringInput::new("ab".to_owned())); /// let start = Position::from_start(input); /// let end = start.clone().match_string("ab").unwrap(); /// let span = start.clone().span(end); /// /// assert_eq!(span.start_pos(), start); /// ``` #[inline] pub fn start_pos(&self) -> position::Position<I> { // Span start position is a UTF-8 border and is safe. unsafe { position::new(self.input.clone(), self.start) } } /// Returns the `Span`'s end `Position`. /// /// # Examples /// /// ``` /// # use std::rc::Rc; /// # use pest::inputs::{Position, StringInput}; /// let input = Rc::new(StringInput::new("ab".to_owned())); /// let start = Position::from_start(input); /// let end = start.clone().match_string("ab").unwrap(); /// let span = start.span(end.clone()); /// /// assert_eq!(span.end_pos(), end); /// ``` #[inline] pub fn end_pos(&self) -> position::Position<I> { // Span end position is a UTF-8 border and is safe. unsafe { position::new(self.input.clone(), self.end) } } /// Splits the `Span` into a pair of `Position`s. /// /// # Examples /// /// ``` /// # use std::rc::Rc; /// # use pest::inputs::{Position, StringInput}; /// let input = Rc::new(StringInput::new("ab".to_owned())); /// let start = Position::from_start(input); /// let end = start.clone().match_string("ab").unwrap(); /// let span = start.clone().span(end.clone()); /// /// assert_eq!(span.split(), (start, end)); /// ``` #[inline] pub fn split(self) -> (position::Position<I>, position::Position<I>) { // Span start and end positions are UTF-8 borders and safe. let pos1 = unsafe { position::new(self.input.clone(), self.start) }; let pos2 = unsafe { position::new(self.input, self.end) }; (pos1, pos2) } /// Captures a `&str` slice from the `Input` defined by the `Span`. /// /// # Examples /// /// ``` /// # use std::rc::Rc; /// # use pest::inputs::{Position, StringInput}; /// let input = Rc::new(StringInput::new("abc".to_owned())); /// let start = Position::from_start(input).skip(1).unwrap(); /// let end = start.clone().match_string("b").unwrap(); /// let span = start.span(end); /// /// assert_eq!(span.as_str(), "b"); /// ``` #[inline] pub fn as_str(&self) -> &str { unsafe { self.input.slice(self.start, self.end) } } } // We don't want to enforce derivable traits on the Input which forces to implement them manually. impl<I: Input> fmt::Debug for Span<I> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Span {{ start: {}, end: {} }}", self.start, self.end) } } impl<I: Input> Clone for Span<I> { fn clone(&self) -> Span<I> { new(self.input.clone(), self.start, self.end) } } impl<I: Input> PartialEq for Span<I> { fn eq(&self, other: &Span<I>) -> bool { Rc::ptr_eq(&self.input, &other.input) && self.start == other.start && self.end == other.end } } impl<I: Input> Eq for Span<I> {} impl<I: Input> PartialOrd for Span<I> { fn partial_cmp(&self, other: &Span<I>) -> Option<Ordering> { if Rc::ptr_eq(&self.input, &other.input) { match self.start.partial_cmp(&other.start) { Some(Ordering::Equal) => self.end.partial_cmp(&other.end), ordering => ordering } } else { None } } } impl<I: Input> Ord for Span<I> { fn cmp(&self, other: &Span<I>) -> Ordering { self.partial_cmp(other).expect( "cannot compare spans from \ different inputs" ) } } impl<I: Input> Hash for Span<I> { fn hash<H: Hasher>(&self, state: &mut H) { (&*self.input as *const I).hash(state); self.start.hash(state); self.end.hash(state); } }