1use std::fmt;
2
3#[derive(Clone, Copy, PartialEq, Eq)]
5pub struct Position {
6 pub line: usize,
7 pub col: usize,
8}
9
10impl Default for Position {
11 fn default() -> Self {
12 Self { line: 1, col: 0 }
13 }
14}
15
16impl fmt::Debug for Position {
17 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18 write!(f, "{}:{}", self.line, self.col)
19 }
20}
21
22impl fmt::Display for Position {
23 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24 write!(f, "{}:{}", self.line, self.col)
25 }
26}
27
28impl Position {
29 pub fn advance_line(&mut self) {
30 self.line += 1;
31 self.col = 0;
32 }
33
34 pub fn advance_col(&mut self) {
35 self.col += 1;
36 }
37
38 pub fn advance(&mut self, c: char) {
39 if c == '\n' {
40 self.advance_line()
41 } else {
42 self.advance_col()
43 }
44 }
45}
46
47#[derive(Clone, Copy, PartialEq, Eq)]
49pub struct Span {
50 pub start: Position,
51 pub end: Position,
52}
53
54impl Span {
55 pub fn extend(&self, other: &Self) -> Self {
56 Self {
57 start: self.start,
58 end: other.end,
59 }
60 }
61
62 pub fn on_line(line: usize, start_col: usize, end_col: usize) -> Self {
63 Self {
64 start: Position {
65 line,
66 col: start_col,
67 },
68 end: Position { line, col: end_col },
69 }
70 }
71}
72
73impl fmt::Debug for Span {
74 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75 write!(f, "{}-{}", self.start, self.end)
76 }
77}
78
79impl fmt::Display for Span {
80 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81 write!(f, "{}-{}", self.start, self.end)
82 }
83}
84
85#[derive(Clone, Debug)]
87pub struct Spanned<T> {
88 pub span: Span,
89 pub inner: T,
90}