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: Add hexadecimal numerical notation #3654

Merged
merged 5 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
(@vanillajonathan, #3568)
- Add support for long Unicode escape sequences. Example `"Hello \u{01F422}"`.
(@vanillajonathan, #3569)
- Add support for hexadecimal numerical notation. Example
`filter status == 0xff`. (@vanillajonathan, #3654)

**Fixes**:

Expand Down
20 changes: 20 additions & 0 deletions crates/prql-parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ pub fn ident_part() -> impl Parser<char, String, Error = Cheap<char>> + Clone {
}

fn literal() -> impl Parser<char, Literal, Error = Cheap<char>> {
let hex_notation = just("0x")
.ignore_then(
filter(|c: &char| c.is_ascii_hexdigit())
.repeated()
.at_least(1)
.at_most(12)
.collect::<String>()
.try_map(|digits, _| {
Ok(Literal::Integer(i64::from_str_radix(&digits, 16).unwrap()))
}),
)
.labelled("number");
vanillajonathan marked this conversation as resolved.
Show resolved Hide resolved
vanillajonathan marked this conversation as resolved.
Show resolved Hide resolved

let exp = one_of("eE").chain(one_of("+-").or_not().chain::<char, _, _>(text::digits(10)));

let integer = filter(|c: &char| c.is_ascii_digit() && *c != '0')
Expand Down Expand Up @@ -286,6 +299,7 @@ fn literal() -> impl Parser<char, Literal, Error = Cheap<char>> {
.map(Literal::Timestamp);

choice((
hex_notation,
string,
raw_string,
value_and_unit,
Expand Down Expand Up @@ -481,6 +495,12 @@ fn test_line_wrap() {
"###);
}

#[test]
fn numbers() {
// Hexadecimal notation
assert_eq!(literal().parse("0xff").unwrap(), Literal::Integer(255));
}

#[test]
fn quotes() {
use insta::assert_snapshot;
Expand Down