Skip to content

Commit

Permalink
feat: Add hexadecimal numerical notation (#3654)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
vanillajonathan and pre-commit-ci[bot] authored Oct 11, 2023
1 parent 27b70c5 commit a731d86
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
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");

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

0 comments on commit a731d86

Please sign in to comment.