summaryrefslogtreecommitdiff
path: root/src/locators
diff options
context:
space:
mode:
Diffstat (limited to 'src/locators')
-rw-r--r--src/locators/source.rs13
1 files changed, 8 insertions, 5 deletions
diff --git a/src/locators/source.rs b/src/locators/source.rs
index 2cf1ef9..6837133 100644
--- a/src/locators/source.rs
+++ b/src/locators/source.rs
@@ -23,9 +23,9 @@ pub struct SourceLocation {
/// File path the source was loaded from.
pub path: Option<PathBuf>,
/// Position of the first character of the string.
- pub start: Position,
+ pub start: SourcePosition,
/// Position of the final character of the string.
- pub end: Position,
+ pub end: SourcePosition,
}
impl std::fmt::Display for SourceLocation {
@@ -41,25 +41,28 @@ impl std::fmt::Display for SourceLocation {
#[derive(Clone, Copy)]
-pub struct Position {
+pub struct SourcePosition {
/// The number of lines that precede this line in the source file.
pub line: usize,
/// The number of characters that precede this character in the line.
pub column: usize,
}
-impl Position {
+impl SourcePosition {
pub const ZERO: Self = Self { line: 0, column: 0 };
+ // Advance to the next character in the row.
pub fn to_next_char(&mut self) {
self.column += 1;
}
+ // Advance to the start of the next line.
pub fn to_next_line(&mut self) {
self.line += 1;
self.column = 0;
}
+ // Advance to the next character, wrapping on line breaks.
pub fn advance(&mut self, c: char) {
match c {
'\n' => self.to_next_line(),
@@ -68,7 +71,7 @@ impl Position {
}
}
-impl std::fmt::Display for Position {
+impl std::fmt::Display for SourcePosition {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
let y = self.line + 1;
let x = self.column + 1;