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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
#[cfg(feature="termion")] extern crate termion; use std::cmp::{PartialOrd, Ord, Ordering}; mod position; /// Lazy string buffer that fills up on demand. pub mod lazy; /// Source code formatter with span highlights and notes. /// /// Here are the kind of things you can produce with the [`Formatter`](fmt::Formatter): /// <pre><font color="#729FCF"><b>01 | </b></font>pub fn fibonacci(n: i32) -> u64 { /// <font color="#729FCF"><b>| ________ </b></font><font color="#EF2929"><b>^</b></font> /// <font color="#EF2929"><b> </b></font><font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>__________________________</b></font><font color="#729FCF"><b>|</b></font><font color="#EF2929"><b>________|</b></font> /// <font color="#EF2929"><b> </b></font><font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>| </b></font><font color="#729FCF"><b>|</b></font> /// <font color="#729FCF"><b> | </b></font><font color="#EF2929"><b>| </b></font><font color="#729FCF"><b>this is a pair of parenthesis</b></font> /// <font color="#729FCF"><b>02 | </b></font><font color="#EF2929"><b>| </b></font>if n < 0 { /// <font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>| __________________^</b></font> /// <font color="#729FCF"><b>03 | </b></font><font color="#EF2929"><b>| | </b></font>panic!("{} is negative!", n); /// <font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>| | </b></font><font color="#729FCF"><b>^</b></font><font color="#8AE234"><b>"</b></font><font color="#EF2929"><b>^^ </b></font><font color="#8AE234"><b>" </b></font><font color="#729FCF"><b>^ this is a pair of parenthesis</b></font> /// <font color="#729FCF"><b> | </b></font><font color="#EF2929"><b>| | </b></font><font color="#729FCF"><b>|</b></font><font color="#8AE234"><b>|_</b></font><font color="#EF2929"><b>|</b></font><font color="#8AE234"><b>_____________| </b></font><font color="#729FCF"><b>|</b></font> /// <font color="#729FCF"><b> | </b></font><font color="#EF2929"><b>| | </b></font><font color="#729FCF"><b>|__</b></font><font color="#EF2929"><b>|</b></font><font color="#729FCF"><b>_____________</b></font><font color="#8AE234"><b>|</b></font><font color="#729FCF"><b>___|</b></font> /// <font color="#729FCF"><b> | </b></font><font color="#EF2929"><b>| | | </b></font><font color="#8AE234"><b>|</b></font> /// <font color="#8AE234"><b> </b></font><font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>| | this is a pair of braces</b></font> /// <font color="#EF2929"><b> </b></font><font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>| | </b></font><font color="#8AE234"><b>|</b></font> /// <font color="#8AE234"><b> </b></font><font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>| | </b></font><font color="#8AE234"><b>this is a string</b></font> /// <font color="#729FCF"><b>04 | </b></font><font color="#EF2929"><b>| | </b></font>} else if n == 0 { /// <font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>| |_________^ ^</b></font> /// <font color="#EF2929"><b> </b></font><font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>| _________|________________|</b></font> /// <font color="#EF2929"><b> </b></font><font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>| | |</b></font> /// <font color="#EF2929"><b> </b></font><font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>| | this is a pair of braces</b></font> /// <font color="#729FCF"><b>05 | </b></font><font color="#EF2929"><b>| | </b></font>panic!("zero is not a right argument to fibonacci()!"); /// <font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>| | </b></font><font color="#729FCF"><b>^</b></font><font color="#8AE234"><b>" </b></font><font color="#729FCF"><b>__ </b></font><font color="#8AE234"><b>"</b></font><font color="#729FCF"><b>^ this is a pair of parenthesis</b></font> /// <font color="#729FCF"><b> | </b></font><font color="#EF2929"><b>| | </b></font><font color="#729FCF"><b>|</b></font><font color="#8AE234"><b>|__________________________________________</b></font><font color="#729FCF"><b>|</b></font><font color="#8AE234"><b>_|</b></font><font color="#729FCF"><b>|</b></font> /// <font color="#729FCF"><b> | </b></font><font color="#EF2929"><b>| | </b></font><font color="#729FCF"><b>|___________________________________________|_</b></font><font color="#8AE234"><b>|</b></font><font color="#729FCF"><b>|</b></font> /// <font color="#729FCF"><b> | </b></font><font color="#EF2929"><b>| | </b></font><font color="#729FCF"><b>| </b></font><font color="#8AE234"><b>|</b></font> /// <font color="#8AE234"><b> </b></font><font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>| | </b></font><font color="#729FCF"><b>this is a pair of parenthesis</b></font> /// <font color="#729FCF"><b> | </b></font><font color="#EF2929"><b>| | </b></font><font color="#8AE234"><b>|</b></font> /// <font color="#8AE234"><b> </b></font><font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>| | </b></font><font color="#8AE234"><b>this is a string</b></font> /// <font color="#729FCF"><b>06 | </b></font><font color="#EF2929"><b>| | </b></font>} else if n == 1 { /// <font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>| |_________^ ^</b></font> /// <font color="#EF2929"><b> </b></font><font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>| _________|________________|</b></font> /// <font color="#EF2929"><b> </b></font><font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>| | |</b></font> /// <font color="#EF2929"><b> </b></font><font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>| | this is a pair of braces</b></font> /// <font color="#729FCF"><b>07 | </b></font><font color="#EF2929"><b>| | </b></font>return 1; /// <font color="#729FCF"><b>08 | </b></font><font color="#EF2929"><b>| | </b></font>} /// <font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>| |_________^ this is a pair of braces</b></font> /// <font color="#729FCF"><b>09 | </b></font><font color="#EF2929"><b>| </b></font> /// <font color="#729FCF"><b>10 | </b></font><font color="#EF2929"><b>| </b></font>let mut sum = 0; /// <font color="#729FCF"><b>11 | </b></font><font color="#EF2929"><b>| </b></font>let mut last = 0; /// <font color="#729FCF"><b>12 | </b></font><font color="#EF2929"><b>| </b></font>let mut curr = 1; /// <font color="#729FCF"><b>13 | </b></font><font color="#EF2929"><b>| </b></font>for _i in 1..n { /// <font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>| ________________________^</b></font> /// <font color="#729FCF"><b>14 | </b></font><font color="#EF2929"><b>| | </b></font>sum = last + curr; /// <font color="#729FCF"><b>15 | </b></font><font color="#EF2929"><b>| | </b></font>last = curr; /// <font color="#729FCF"><b>16 | </b></font><font color="#EF2929"><b>| | </b></font>curr = sum; /// <font color="#729FCF"><b>17 | </b></font><font color="#EF2929"><b>| | </b></font>} /// <font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>| |_________^ this is a pair of braces</b></font> /// <font color="#729FCF"><b>18 | </b></font><font color="#EF2929"><b>| </b></font>sum /// <font color="#729FCF"><b>19 | </b></font><font color="#EF2929"><b>| </b></font>} /// <font color="#729FCF"><b>| </b></font><font color="#EF2929"><b>|___^ this is a pair of braces</b></font></pre> pub mod fmt; pub use position::Position; /// Span in a source file. /// /// A span points to a range of caracters between two cursor [`Position`]. /// /// ## Span construction with the `push*` methods /// /// A span can be directly created using the [`new`](Span::new) method, however in the context of parsing /// (or lexing) it might be useful to build spans incrementally. /// The `push*` methods family will help you do that. /// /// * [`push`](Span::push) will extend the span to include the given character located at the spans `end`. /// * [`push_column`](Span::push_column) will extend the span to include the next column. Note that this does not /// necessarily correspond to the next character (if it is a NL, or a full-width character for /// instance). /// * [`push_line`](Span::push_line) will extend the span to include the rest of the line. The end of the span will be /// placed at the begining of the next line. /// /// * The [`next`](Span::next) method can finally be used to create the span to `[end, end]` (when a token has /// been read entirely for instance) and start building the next span. The [`clear`](Span::clear) method /// does the same but *in place*. /// /// ## Example /// Here is a basic example computing the span of every word/token in a `char` stream. /// ```rust /// for c in chars { /// let c = c?; // report eventual I/O errors. /// if c.is_whitespace() { /// // save the current token. /// if !current.string.is_empty() { /// tokens.push(current.clone()); /// } /// /// // reset current token. /// current.string.clear(); /// current.span.clear(); // the span here is moved to the end of itself. /// } else { /// current.string.push(c); /// current.span.push(c); /// } /// } /// /// if !current.string.is_empty() { /// tokens.push(current); /// } /// ``` /// #[derive(Clone, Copy, PartialEq, Eq, Hash, Default)] pub struct Span { /// The position of the first character in the span. start: Position, /// The last position in the span. last: Position, /// The position of the character directly following the span. /// /// It is not included in the span. end: Position } impl PartialOrd for Span { fn partial_cmp(&self, other: &Span) -> Option<Ordering> { Some(self.cmp(other)) } } impl Ord for Span { fn cmp(&self, other: &Span) -> Ordering { self.end.cmp(&other.end) } } impl Span { /// Create a new span from three positions. /// /// If the `end` position is before the `start` position then the returned span will be /// `[start, start]`. /// If the `last` position is before `start` or after `end` it will panic. /// If the `last` position is equal to `end` while the span is not empty, it will panic. pub fn new(start: Position, last: Position, mut end: Position) -> Span { if end < start { end = start; } if last < start || (last >= end && end != start) { panic!("invalid span ({:?}, {:?}, {:?})", start, last, end); } Span { start: start, last: last, end: end } } /// Return the position of the first character in the span. pub fn start(&self) -> Position { self.start } /// Return the last position included in the span. pub fn last(&self) -> Position { self.last } /// Return the position of the character directly following the span. /// /// It is not included in the span. pub fn end(&self) -> Position { self.end } /// Checks if the span is empty. pub fn is_empty(&self) -> bool { self.start == self.end } /// Checks if two span overlaps. pub fn overlaps(&self, other: &Span) -> bool { (self.start <= other.start && self.end > other.start) || (other.start <= self.start && other.end > self.start) } /// The number of lines covered by the span. /// /// It is at least one, even if the span is empty. pub fn line_count(&self) -> usize { self.end.line - self.start.line + 1 } /// Checks if the span includes the given line. pub fn includes_line(&self, line: usize) -> bool { line >= self.start.line && line <= self.end.line } /// Extends the span to include the next column. /// /// Note that this does not necessarily correspond /// to the next character (if it is a NL, or a full-width character for instance). /// To do that you can use the [`push`](Span::push) method. pub fn push_column(&mut self) { self.last = self.end; self.end = self.end.next_column(); } /// Extends the span to include the rest of the line. /// /// The end of the span will be placed at the begining of the next line. pub fn push_line(&mut self) { self.last = self.end; self.end = self.end.next_line(); } /// Extend the span to include the given character located at the spans `end` position. pub fn push(&mut self, c: char) { self.last = self.end; self.end = self.end.next(c); } /// Compute the union of two spans. /// /// If the two spans do not overlap, all positions in between will be included in the /// resulting span. pub fn union(&self, other: Span) -> Span { if other.last > self.last && other.end > self.end { Span { start: std::cmp::min(self.start, other.start), last: other.last, end: other.end } } else { Span { start: std::cmp::min(self.start, other.start), last: self.last, end: self.end } } } /// Extend the span to the end of the given span. /// /// This is the *in-place* version of [`union`](Span::union), except that /// nothing happens if the input span finishes before the end of `self`. pub fn append(&mut self, other: Span) { if other.last > self.last && other.end > self.end { self.last = other.last; self.end = other.end; } } /// Return the next span (defined as `[end, end]`). pub fn next(&self) -> Span { Span { start: self.end, last: self.end, end: self.end } } /// Set the span to [`next`](Span::next) (`[end, end]`). pub fn clear(&mut self) { self.start = self.end; self.last = self.end; } } impl From<Position> for Span { fn from(pos: Position) -> Span { Span { start: pos, last: pos, end: pos } } } impl std::fmt::Display for Span { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "from {:?} to {:?}", self.start, self.end) } } impl std::fmt::Debug for Span { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "[{:?}, {:?}]", self.start, self.end) } }