Skip to content

Commit

Permalink
Make the lexer accept scientific notation (#1456)
Browse files Browse the repository at this point in the history
* Make the lexer accept scientific notation

* Mention scientific notation on doc/manual/syntax.md
  • Loading branch information
vkleen authored Jul 17, 2023
1 parent 3d47bac commit da07e3e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion core/src/parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub enum NormalToken<'input> {
// regex for checking identifiers at ../lsp/nls/src/requests/completion.rs
#[regex("_?[a-zA-Z][_a-zA-Z0-9-']*")]
Identifier(&'input str),
#[regex("[0-9]*\\.?[0-9]+", |lex| parse_number(lex.slice()))]
#[regex("[0-9]*\\.?[0-9]+([eE][+\\-]?[0-9]+)?", |lex| parse_number(lex.slice()))]
NumLiteral(Number),

// **IMPORTANT**
Expand Down
10 changes: 10 additions & 0 deletions core/tests/integration/pass/core/numbers.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# test.type = 'pass'
let {check, ..} = import "../lib/assert.ncl" in

[
5 + 5 == 10,
5 * 5 == 25,
1e6 == 1000000,
1e+3 / 2e-3 == 0.5e6,
]
|> check
6 changes: 4 additions & 2 deletions doc/manual/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ fit exactly into a 64-bit signed integer or a 64-bit unsigned integer. They
are serialized as a 64-bit float otherwise. The latter conversion might lose
precision as well, for example when serializing `1/3`.

Here are some examples of number literals in Nickel:
Here are some examples of number literals in Nickel; scientific notation for
decimal is supported:

```nickel
1
0.543
42
1.7e217
-3e-3
-1000000
-6.8
```
Expand Down

0 comments on commit da07e3e

Please sign in to comment.