From ed80c0abcdfc62d3d2f83ba53682693cb014c83d Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Sun, 9 Feb 2025 10:20:32 +1300 Subject: Rename Position type to SourcePosition This better matches the naming convention of the other source locator types, and better differentiates a SourcePosition from other kinds of position. --- src/locators/source.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'src/locators/source.rs') 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, /// 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; -- cgit v1.2.3-70-g09d2