diff options
-rw-r--r-- | src/locators/source.rs | 13 | ||||
-rw-r--r-- | src/tokeniser.rs | 16 |
2 files changed, 16 insertions, 13 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; diff --git a/src/tokeniser.rs b/src/tokeniser.rs index 4ff3d0b..8e3cb15 100644 --- a/src/tokeniser.rs +++ b/src/tokeniser.rs @@ -14,11 +14,11 @@ pub struct Tokeniser { /// Line where the embedded source file begins. pub embedded_first_line: usize, /// Position of the next character to be consumed. - pub position: Position, + pub position: SourcePosition, /// Position of the most recently consumed character. - pub prev_position: Position, + pub prev_position: SourcePosition, /// Position of the first character of the current token. - pub start_position: Position, + pub start_position: SourcePosition, /// The source characters consumed for the current token. pub consumed: String, /// List of characters that start a new token. @@ -35,9 +35,9 @@ impl Tokeniser { source_path: path.map(|p| p.into()), embedded_path: None, embedded_first_line: 0, - position: Position::ZERO, - prev_position: Position::ZERO, - start_position: Position::ZERO, + position: SourcePosition::ZERO, + prev_position: SourcePosition::ZERO, + start_position: SourcePosition::ZERO, consumed: String::new(), delimiters: Vec::new(), terminators: Vec::new(), @@ -145,11 +145,11 @@ impl Tokeniser { Some( SourceLocation { path: Some(embedded_path.to_owned()), - start: Position { + start: SourcePosition { line: in_merged.start.line.saturating_sub(offset), column: in_merged.start.column, }, - end: Position { + end: SourcePosition { line: in_merged.end.line.saturating_sub(offset), column: in_merged.end.column, } |