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 binary numerical notation #3661

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.
Jump to
Jump to file
Failed to load files.
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 binary numerical notation. Example
`filter status == 0b1111000011110000`. (@vanillajonathan, #3661)
- Add support for hexadecimal numerical notation. Example
`filter status == 0xff`. (@vanillajonathan, #3654)

Expand Down
34 changes: 32 additions & 2 deletions crates/prql-parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,22 @@ 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")
let binary_notation = just("0b")
.then_ignore(just("_").or_not())
.ignore_then(
filter(|c: &char| *c == '0' || *c == '1')
.repeated()
.at_least(1)
.at_most(32)
.collect::<String>()
.try_map(|digits, _| {
Ok(Literal::Integer(i64::from_str_radix(&digits, 2).unwrap()))
}),
)
.labelled("number");

let hexadecimal_notation = just("0x")
.then_ignore(just("_").or_not())
.ignore_then(
filter(|c: &char| c.is_ascii_hexdigit())
.repeated()
Expand Down Expand Up @@ -299,7 +314,8 @@ fn literal() -> impl Parser<char, Literal, Error = Cheap<char>> {
.map(Literal::Timestamp);

choice((
hex_notation,
binary_notation,
hexadecimal_notation,
string,
raw_string,
value_and_unit,
Expand Down Expand Up @@ -497,8 +513,22 @@ fn test_line_wrap() {

#[test]
fn numbers() {
// Binary notation
assert_eq!(
literal().parse("0b1111000011110000").unwrap(),
Literal::Integer(61680)
);
assert_eq!(
literal().parse("0b_1111000011110000").unwrap(),
Literal::Integer(61680)
);

// Hexadecimal notation
assert_eq!(literal().parse("0xff").unwrap(), Literal::Integer(255));
assert_eq!(
literal().parse("0x_deadbeef").unwrap(),
Literal::Integer(3735928559)
);
}

#[test]
Expand Down
5 changes: 5 additions & 0 deletions web/book/src/reference/syntax/literals.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ where the number after `e` is the exponent in 10-base.
Underscores are ignored, so they can be placed at arbitrary positions, but it is
advised to use them as thousand separators.

Integers can, alternatively, be expressed using hexadecimal, or binary notation
using these prefixes respectively: `0x` or `0b`.

```prql
from numbers
select {
small = 1.000_000_1,
big = 5_000_000,
huge = 5e9,
binary = 0x0011,
hex = 0x80,
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ expression: "from numbers\nselect {\n small = 1.000_000_1,\n big = 5_000_0
SELECT
1.0000001 AS small,
5000000 AS big,
5000000000.0 AS huge
5000000000.0 AS huge,
17 AS binary,
128 AS hex
FROM
numbers