garden_lang_parser/
position.rs

1use std::{path::PathBuf, rc::Rc};
2
3use serde::{Deserialize, Serialize};
4
5use crate::lex::Token;
6
7/// A position is a range in source code. It is a span between
8/// `start_offset` and `end_offset` in `path`.
9#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Ord)]
10pub struct Position {
11    /// The start of this position, relative to the start of the
12    /// file. Measured in bytes.
13    pub start_offset: usize,
14    /// The end of this position, relative to the start of the
15    /// file. Measured in bytes.
16    pub end_offset: usize,
17    // TODO: Use LineNumber instead, finding a way to serialize it.
18    pub line_number: usize,
19    pub end_line_number: usize,
20    /// The column of the start of this position. Zero indexed.
21    pub column: usize,
22    pub end_column: usize,
23    pub path: Rc<PathBuf>,
24}
25
26impl std::fmt::Debug for Position {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        if std::env::var("VERBOSE").is_ok() {
29            f.debug_struct("Position")
30                .field("start_offset", &self.start_offset)
31                .field("end_offset", &self.end_offset)
32                .field("line_number", &self.line_number)
33                .field("end_line_number", &self.end_line_number)
34                .field("path", &self.path)
35                .finish()
36        } else {
37            f.write_str("Position { ... }")
38        }
39    }
40}
41
42impl Position {
43    pub fn todo(path: Rc<PathBuf>) -> Self {
44        Self {
45            start_offset: 0,
46            end_offset: 0,
47            line_number: 0,
48            column: 0,
49            end_column: 0,
50            end_line_number: 0,
51            path,
52        }
53    }
54
55    /// Return the merged position of `first` and `second`.
56    pub fn merge(first: &Self, second: &Self) -> Self {
57        Self {
58            start_offset: first.start_offset,
59            end_offset: std::cmp::max(first.end_offset, second.end_offset),
60            line_number: first.line_number,
61            end_line_number: std::cmp::max(first.end_line_number, second.end_line_number),
62            column: first.column,
63            end_column: if first.end_offset > second.end_offset {
64                first.end_column
65            } else {
66                second.end_column
67            },
68            path: first.path.clone(),
69        }
70    }
71
72    /// Merge the position of this token (including doc comments) with
73    /// the second position.
74    pub fn merge_token(first: &Token, second: &Self) -> Self {
75        let mut first_pos = first.position.clone();
76        if let Some((comment_pos, _)) = first.preceding_comments.first() {
77            first_pos = comment_pos.clone();
78        }
79
80        Self::merge(&first_pos, second)
81    }
82
83    /// Format this position as a human-friendly string, e.g. "foo.gdn:123".
84    pub fn as_ide_string(&self) -> String {
85        format!(
86            "{}:{}",
87            self.path.display(),
88            // 1-indexed line number
89            self.line_number + 1,
90        )
91    }
92
93    pub fn contains_offset(&self, offset: usize) -> bool {
94        self.start_offset <= offset && offset <= self.end_offset
95    }
96}