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: underscores in numeric literals #48

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
abeba9b
feat: underscores in numeric literals
latonis Oct 19, 2023
d309978
rustfmt cleanup
latonis Oct 19, 2023
a273e76
feat: underscores for float literal
latonis Oct 19, 2023
7f431b2
clippy cleanup
latonis Oct 19, 2023
e65d38f
Merge branch 'VirusTotal:main' into numeric_underscores
latonis Oct 30, 2023
329a297
Merge branch 'VirusTotal:main' into numeric_underscores
latonis Nov 10, 2023
1bc0029
Merge branch 'VirusTotal:main' into numeric_underscores
latonis Nov 22, 2023
5af6663
Merge branch 'VirusTotal:main' into numeric_underscores
latonis Nov 25, 2023
3f94a13
Merge branch 'VirusTotal:main' into numeric_underscores
latonis Dec 2, 2023
c93cf0a
Merge branch 'VirusTotal:main' into numeric_underscores
latonis Dec 14, 2023
889e381
Merge branch 'VirusTotal:main' into numeric_underscores
latonis Dec 20, 2023
bf8cb82
Merge branch 'VirusTotal:main' into numeric_underscores
latonis Feb 4, 2024
8abdf0c
Merge branch 'main' into numeric_underscores
latonis Feb 21, 2024
aff1add
Merge branch 'VirusTotal:main' into numeric_underscores
latonis Feb 26, 2024
27d8b57
Merge branch 'VirusTotal:main' into numeric_underscores
latonis Feb 28, 2024
f0dcf6a
Merge branch 'VirusTotal:main' into numeric_underscores
latonis Mar 2, 2024
a240e1d
Merge branch 'VirusTotal:main' into numeric_underscores
latonis Mar 21, 2024
62d4d71
Merge branch 'VirusTotal:main' into numeric_underscores
latonis Apr 2, 2024
d4c2f4a
Merge branch 'VirusTotal:main' into numeric_underscores
latonis May 10, 2024
0a007ca
Merge branch 'VirusTotal:main' into numeric_underscores
latonis May 29, 2024
a0f82a7
Merge branch 'VirusTotal:main' into numeric_underscores
latonis Jun 18, 2024
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
8 changes: 7 additions & 1 deletion parser/src/parser/cst2ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1774,6 +1774,9 @@ where
let mut literal = integer_lit.as_str();
let mut multiplier = 1;

let without_underscore = literal.replace('_', "");
literal = without_underscore.as_str();

if let Some(without_suffix) = literal.strip_suffix("KB") {
literal = without_suffix;
multiplier = 1024;
Expand Down Expand Up @@ -1833,9 +1836,12 @@ fn float_lit_from_cst<'src>(
) -> Result<f64, Error> {
expect!(float_lit, GrammarRule::float_lit);

let literal = float_lit.as_str();
let mut literal = float_lit.as_str();
let span = ctx.span(&float_lit);

let without_underscore = literal.replace('_', "");
literal = without_underscore.as_str();

literal.parse::<f64>().map_err(|err| {
Error::from(ErrorInfo::invalid_float(
ctx.report_builder,
Expand Down
8 changes: 4 additions & 4 deletions parser/src/parser/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,13 @@ string_lit = @{
}

integer_lit = @{
"-"? ~ "0x" ~ ASCII_HEX_DIGIT+ |
"-"? ~ "0o" ~ ASCII_OCT_DIGIT+ |
"-"? ~ ASCII_DIGIT+ ~ ("KB" | "MB")?
"-"? ~ "0x" ~ ASCII_HEX_DIGIT+ ~ ("_" | ASCII_HEX_DIGIT)* |
"-"? ~ "0o" ~ ASCII_OCT_DIGIT+ ~ ("_" | ASCII_OCT_DIGIT)* |
"-"? ~ ASCII_DIGIT+ ~ ("_" | ASCII_DIGIT)* ~ ("KB" | "MB")?
}

float_lit = @{
"-"? ~ ASCII_DIGIT+ ~ DOT ~ ASCII_DIGIT+
"-"? ~ ASCII_DIGIT+ ~ ("_" | ASCII_DIGIT)* ~ DOT ~ ASCII_DIGIT+ ~ ("_" | ASCII_DIGIT)*
}

regexp = @{
Expand Down
103 changes: 103 additions & 0 deletions parser/src/parser/tests/cst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,109 @@ rule test : foo bar baz {
│ └─ ident "baz"
├─ LPAREN "("
└─ RPAREN ")"
"#,
),
////////////////////////////////////////////////////////////
(
line!(),
GrammarRule::term,
r#"#a in (1_000..200_)"#,
r##"
term
└─ primary_expr
├─ pattern_count "#a"
├─ k_IN "in"
└─ range
├─ LPAREN "("
├─ expr
│ └─ term
│ └─ primary_expr
│ └─ integer_lit "1_000"
├─ DOT_DOT ".."
├─ expr
│ └─ term
│ └─ primary_expr
│ └─ integer_lit "200_"
└─ RPAREN ")"
"##,
),
////////////////////////////////////////////////////////////
(
line!(),
GrammarRule::expr,
r#"0x0_2 | 0o00_1 & 0x03_"#,
r#"
expr
├─ term
│ └─ primary_expr
│ └─ integer_lit "0x0_2"
├─ BITWISE_OR "|"
├─ term
│ └─ primary_expr
│ └─ integer_lit "0o00_1"
├─ BITWISE_AND "&"
└─ term
└─ primary_expr
└─ integer_lit "0x03_"
"#,
),
////////////////////////////////////////////////////////////
(
line!(),
GrammarRule::expr,
r#"0_2_2 | 0x00000_11 & 0o05_5"#,
r#"
expr
├─ term
│ └─ primary_expr
│ └─ integer_lit "0_2_2"
├─ BITWISE_OR "|"
├─ term
│ └─ primary_expr
│ └─ integer_lit "0x00000_11"
├─ BITWISE_AND "&"
└─ term
└─ primary_expr
└─ integer_lit "0o05_5"
"#,
),
////////////////////////////////////////////////////////////
(
line!(),
GrammarRule::boolean_expr,
r#"2.5_5 * 2__3 * -1.0_1 == 5_55.05 + -(1_1)"#,
r#"
boolean_expr
└─ boolean_term
├─ expr
│ ├─ term
│ │ └─ primary_expr
│ │ └─ float_lit "2.5_5"
│ ├─ MUL "*"
│ ├─ term
│ │ └─ primary_expr
│ │ └─ integer_lit "2__3"
│ ├─ MUL "*"
│ └─ term
│ └─ primary_expr
│ └─ float_lit "-1.0_1"
├─ EQ "=="
└─ expr
├─ term
│ └─ primary_expr
│ └─ float_lit "5_55.05"
├─ ADD "+"
└─ term
└─ primary_expr
├─ MINUS "-"
└─ term
└─ primary_expr
├─ LPAREN "("
├─ expr
│ └─ term
│ └─ primary_expr
│ └─ integer_lit "1_1"
└─ RPAREN ")"
"#,
),
];
Expand Down
15 changes: 15 additions & 0 deletions parser/src/parser/tests/testdata/literals.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,19 @@
├─ 1MB
└─ 1024KB

###############################################################################

- rule: |
rule test {
condition:
1_000MB == 10_24KB
}
ast: |
root
└─ rule test
└─ condition
└─ eq
├─ 1_000MB
└─ 10_24KB

###############################################################################
Loading