Skip to content

Commit

Permalink
Fix labels that look like hex erroring
Browse files Browse the repository at this point in the history
  • Loading branch information
rozukke committed Sep 20, 2024
1 parent 0d82fe9 commit 8cc3bb3
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/lexer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt::Display;
use std::num::IntErrorKind;
use std::str::FromStr;
use std::{i16, u16};

Expand Down Expand Up @@ -199,13 +200,16 @@ impl Cursor<'_> {
let str_val = self.get_range(start..self.abs_pos());
let value = match u16::from_str_radix(str_val, 16) {
Ok(value) => value,
Err(e) => {
return Err(error::lex_invalid_lit(
(start - prefix..self.abs_pos()).into(),
self.src(),
e,
))
}
Err(e) => match e.kind() {
IntErrorKind::PosOverflow => {
return Err(error::lex_invalid_lit(
(start - prefix..self.abs_pos()).into(),
self.src(),
e,
));
}
_ => return Ok(self.ident()),
},
};

Ok(TokenKind::Lit(LiteralKind::Hex(value)))
Expand Down Expand Up @@ -388,6 +392,13 @@ mod test {
assert!(res.kind == TokenKind::Lit(LiteralKind::Hex(0x3000)))
}

#[test]
fn hex_not_num() {
let mut lex = Cursor::new("X_S");
let res = lex.advance_token().unwrap();
assert_eq!(res.kind, TokenKind::Label)
}

// DEC LIT TESTS

#[test]
Expand Down

0 comments on commit 8cc3bb3

Please sign in to comment.