Skip to content

Commit

Permalink
fix: Fix number parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mbaumgartl committed May 8, 2024
1 parent 022e5c5 commit a3d54e3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions sql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,8 @@ number
/ int_:int frac:frac __ { return parseFloat(int_ + frac); }
/ int_:int exp:exp __ { return parseFloat(int_ + exp); }
/ int_:int __ { return parseFloat(int_); }
/ frac:frac __ { return parseFloat(frac); }
/ op:("-" / "+" ) frac:frac __ { return parseFloat(op + frac); }

int
= digits
Expand Down
14 changes: 11 additions & 3 deletions test/sql2ast/literal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ describe('literals', () => {

describe('numbers', () => {
[
['should parse positive number', '+1', 1],
['should parse positive number', '1', 1],
['should parse (explicit) positive number', '+1', 1],
['should parse negative number', '-1', -1],
['should parse positive numbers', '+10', 10],
['should parse negative numbers', '-10', -10]
['should parse positive numbers', '10', 10],
['should parse (explicit) positive numbers', '+10', 10],
['should parse negative numbers', '-10', -10],
['should parse positive decimal numbers', '0.5', 0.5],
['should parse (explicit) positive decimal numbers', '+0.5', 0.5],
['should parse positive decimal numbers w/o integral part', '.5', 0.5],
['should parse (explicit) positive decimal numbers w/o integral part', '+.5', 0.5],
['should parse negative decimal numbers', '-0.5', -0.5],
['should parse negative decimal numbers w/o integral part', '-.5', -0.5]
].forEach(([label, expr, expectedValue]) => {
it(label, () => {
const ast = parser.parse(`SELECT ${expr}`);
Expand Down

0 comments on commit a3d54e3

Please sign in to comment.