Skip to content

Commit

Permalink
Fix syntax error position at end of file (#69)
Browse files Browse the repository at this point in the history
* Aesthetic changes.

* Return correct row and column at EOF.
  • Loading branch information
hdwalters authored Nov 16, 2024
1 parent 35a65e6 commit e9e6d99
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/compiling/failing/position_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,24 @@ pub struct PositionInfo {
impl PositionInfo {
/// Create a new erorr from scratch
pub fn new(meta: &impl Metadata, position: Position, len: usize) -> Self {
PositionInfo {
let info = PositionInfo {
position,
path: meta.get_path(),
len,
data: None
}.updated_pos(meta)
};
info.updated_pos(meta)
}

/// Create a new erorr at the end of file
pub fn at_eof(meta: &impl Metadata) -> Self {
PositionInfo {
let info = PositionInfo {
path: meta.get_path(),
position: Position::EOF,
len: 0,
data: None
}.updated_pos(meta)
};
info.updated_pos(meta)
}

/// Create a new erorr at given position
Expand Down Expand Up @@ -133,7 +135,9 @@ impl PositionInfo {
Err(_) => (0, 0)
}
}
else { (0, 0) }
else {
(0, 0)
}
}
}
}
Expand All @@ -150,17 +154,17 @@ impl PositionInfo {
pub fn get_pos_by_code(&self, code: impl AsRef<str>) -> (usize, usize) {
let code = code.as_ref();
match self.position {
Position::Pos(row, col) => (row, col),
Position::Pos(row, col) => {
(row, col)
}
Position::EOF => {
let mut col = 1;
let mut row = 1;
// Count letters in column
col += code.split_whitespace().count();
// Coint letters in row
if let Some(last) = code.split_whitespace().last() {
row += last.len();
// Add one to `row` because `enumerate()` counts from zero.
// Add one to `col` because `len()` counts from zero.
if let Some((row, line)) = code.lines().enumerate().last() {
(row + 1, line.len() + 1)
} else {
(0, 0)
}
(row, col)
}
}
}
Expand Down

0 comments on commit e9e6d99

Please sign in to comment.