From a3d54e37e79b7c9ab520a4b734fee78dca70defd Mon Sep 17 00:00:00 2001 From: Marco Baumgartl Date: Fri, 19 Apr 2024 12:46:36 +0200 Subject: [PATCH] fix: Fix number parsing --- sql.pegjs | 2 ++ test/sql2ast/literal.spec.js | 14 +++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/sql.pegjs b/sql.pegjs index 03b8f23..0e72ff5 100644 --- a/sql.pegjs +++ b/sql.pegjs @@ -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 diff --git a/test/sql2ast/literal.spec.js b/test/sql2ast/literal.spec.js index f81de64..212089a 100644 --- a/test/sql2ast/literal.spec.js +++ b/test/sql2ast/literal.spec.js @@ -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}`);