From 995c5382f686fd2756fb4b718310929234132fea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gunnlaugur=20=C3=9E=C3=B3r=20Briem?= Date: Tue, 27 Feb 2024 10:35:07 +0000 Subject: [PATCH] Address test failures Change some test expectations to expect a unary minus expression. Change the parsing of ->>-9 and of SET TIMEZONE -9 to convert the unary minus expression to a negative integer. --- src/lexer.spec.ts | 9 ++++++--- src/syntax/expr.ne | 8 +++++++- src/syntax/expr.spec.ts | 29 ++++++++++++++++++++++++----- src/syntax/simple-statements.ne | 1 + 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/lexer.spec.ts b/src/lexer.spec.ts index 73452e3..03e58f6 100644 --- a/src/lexer.spec.ts +++ b/src/lexer.spec.ts @@ -168,15 +168,18 @@ describe('Lexer', () => { next({ type: 'comma' }); next({ type: 'float', value: '.1' }); next({ type: 'comma' }); - next({ type: 'float', value: '-.1' }); + next({ type: 'op_minus' }); + next({ type: 'float', value: '.1' }); next({ type: 'comma' }); - next({ type: 'float', value: '-0.1' }); + next({ type: 'op_minus' }); + next({ type: 'float', value: '0.1' }); next({ type: 'comma' }); next({ type: 'float', value: '0.1' }); next({ type: 'comma' }); next({ type: 'float', value: '10.' }); next({ type: 'comma' }); - next({ type: 'float', value: '-10.' }); + next({ type: 'op_minus' }); + next({ type: 'float', value: '10.' }); }) it('tokenizes ->', () => { diff --git a/src/syntax/expr.ne b/src/syntax/expr.ne index aff8c67..a7ce40f 100644 --- a/src/syntax/expr.ne +++ b/src/syntax/expr.ne @@ -107,7 +107,13 @@ expr_array_index | expr_member {% unwrap %} expr_member - -> (expr_member | expr_paren) ops_member (string | int) {% x => track(x, { + -> (expr_member | expr_paren) ops_member %op_minus int {% x => track(x, { + type: 'member', + operand: unwrap(x[0]), + op: x[1], + member: -unwrap(x[3]) + }) %} + | (expr_member | expr_paren) ops_member (string | int) {% x => track(x, { type: 'member', operand: unwrap(x[0]), op: x[1], diff --git a/src/syntax/expr.spec.ts b/src/syntax/expr.spec.ts index 8d75707..b1e4107 100644 --- a/src/syntax/expr.spec.ts +++ b/src/syntax/expr.spec.ts @@ -46,18 +46,37 @@ describe('Expressions', () => { }); checkTreeExpr(['-0.5', '-.5'], { - type: 'numeric', - value: -0.5, + type: 'unary', + op: '-', + operand: { + type: 'numeric', + value: 0.5, + } }); checkTreeExpr(['-42.', '-42.0'], { - type: 'numeric', - value: -42, + type: 'unary', + op: '-', + operand: { + type: 'numeric', + value: 42, + } }); checkInvalidExpr('42. 51'); - checkInvalidExpr('42.-51'); + checkTreeExpr(['42.-51'], { + type: 'binary', + op: '-', + left: { + type: 'numeric', + value: 42, + }, + right: { + type: 'integer', + value: 51, + }, + }); checkTreeExprLoc(['null'], { _location: { start: 0, end: 4 }, diff --git a/src/syntax/simple-statements.ne b/src/syntax/simple-statements.ne index 7677bbf..bc989be 100644 --- a/src/syntax/simple-statements.ne +++ b/src/syntax/simple-statements.ne @@ -40,6 +40,7 @@ simplestatements_set_timezone -> kw_time kw_zone simplestatements_set_timezone_v simplestatements_set_timezone_val -> (string | int) {% x => track(x, { type: 'value', value: unwrap(x[0]) }) %} + | %op_minus int {% x => track(x, { type: 'value', value: -unwrap(x[1]) }) %} | kw_local {% x => track(x, { type: 'local'}) %} | %kw_default {% x => track(x, { type: 'default'}) %} | kw_interval string kw_hour %kw_to kw_minute {% x => track(x, { type: 'interval', value: unbox(x[1]) }) %}