Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): start support for hex numbers #553

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions harper-core/src/lexing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod hostname;
mod url;

use hostname::lex_hostname_token;
use ordered_float::OrderedFloat;
use url::lex_url;

use self::email_address::lex_email_address;
Expand All @@ -24,6 +25,7 @@ pub fn lex_token(source: &[char]) -> Option<FoundToken> {
lex_tabs,
lex_spaces,
lex_newlines,
lex_hex_number, // before lex_number, which would match the initial 0
lex_number,
lex_url,
lex_email_address,
Expand Down Expand Up @@ -89,6 +91,42 @@ pub fn lex_number(source: &[char]) -> Option<FoundToken> {
None
}

pub fn lex_hex_number(source: &[char]) -> Option<FoundToken> {
// < 3 to avoid accepting 0x alone
if source.len() < 3 || source[0] != '0' || source[1] != 'x' || !source[2].is_ascii_hexdigit() {
return None;
}

let mut i = 2;
let len = source.len();

while i < len {
let next = source[i];

if !next.is_ascii_hexdigit() {
if !next.is_alphanumeric() {
break;
} else {
return None;
}
}

i += 1;
}

let s: String = source[2..i].iter().collect();

// Should always succeed unless the logic above is broken
if let Ok(n) = u64::from_str_radix(&s, 16) {
return Some(FoundToken {
token: TokenKind::Number(OrderedFloat(n as f64), None),
next_index: s.len() + 2,
});
}

None
}

fn lex_newlines(source: &[char]) -> Option<FoundToken> {
let count = source.iter().take_while(|c| **c == '\n').count();

Expand Down Expand Up @@ -165,6 +203,7 @@ fn lex_catch(_source: &[char]) -> Option<FoundToken> {

#[cfg(test)]
mod tests {
use super::lex_hex_number;
use super::lex_token;
use super::lex_word;
use super::{FoundToken, TokenKind};
Expand All @@ -186,4 +225,37 @@ mod tests {
})
));
}

#[test]
fn lexes_good_hex() {
let cases = [
"0x0",
"0xa",
"0xF",
"0xaF",
"0x0123456789abcdef",
"0xAbCdEf9876543210",
];

for case in cases {
let source: Vec<_> = case.chars().collect();
assert!(matches!(
lex_hex_number(&source),
Some(FoundToken {
token: TokenKind::Number(_, None),
..
})
));
}
}

#[test]
fn lexes_bad_hex() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename this to does_not_lex_bad_hex. Even better would be to split these two tests into many smaller ones.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went with the "even better" option.

let cases = ["0x", "0xg", "0x123g", "0Xf00d"];

for &case in &cases {
let source: Vec<_> = case.chars().collect();
assert!(lex_hex_number(&source).is_none());
}
}
}