Add offset to Location type

This commit is contained in:
Greg Shuflin 2021-11-12 01:14:03 -08:00
parent f9349edf77
commit b7f796322b
2 changed files with 7 additions and 3 deletions

View File

@ -226,7 +226,7 @@ struct TokenHandler {
impl TokenHandler {
fn new(tokens: Vec<Token>) -> TokenHandler {
let end_of_file = match tokens.last() {
None => Location { line_num: 0, char_num: 0 },
None => Location { line_num: 0, char_num: 0, offset: 0 },
Some(t) => t.location,
};
TokenHandler { idx: 0, tokens, end_of_file }

View File

@ -17,6 +17,7 @@ use itertools::Itertools;
pub struct Location {
pub(crate) line_num: u32,
pub(crate) char_num: u16,
pub(crate) offset: usize,
}
impl fmt::Display for Location {
@ -221,8 +222,11 @@ pub fn tokenize(input: &str) -> Vec<Token> {
c if is_operator(&c) => handle_operator(c, &mut input),
unknown => Error(format!("Unexpected character: {}", unknown)),
};
let location =
Location { line_num: line_num.try_into().unwrap(), char_num: char_num.try_into().unwrap() };
let location = Location {
offset: 0,
line_num: line_num.try_into().unwrap(),
char_num: char_num.try_into().unwrap(),
};
tokens.push(Token { kind: cur_tok_kind, location });
}
tokens