diff --git a/src/grammar.js b/src/grammar.js index 05196c2..802cd61 100644 --- a/src/grammar.js +++ b/src/grammar.js @@ -148,6 +148,7 @@ let ParserRules = [ {"name": "explicitReturn", "symbols": ["L_CURLY", "multiLine", "R_CURLY"], "postprocess": ([,statement]) => ({statement, returnValues: []})}, {"name": "explicitReturn", "symbols": ["L_CURLY", "singleLine", "R_CURLY"], "postprocess": ([,statement]) => ({statement, returnValues: []})}, {"name": "implicitReturn", "symbols": ["singleLine"], "postprocess": ([statement], _, reject) => statement.trim().startsWith('{') ? reject : ({returnValues: []})}, + {"name": "implicitReturn", "symbols": ["L_PAREN", "L_CURLY", "multiLine", "R_CURLY", "R_PAREN"], "postprocess": () => ({returnValues: []})}, {"name": "returnValues", "symbols": ["returnValue", "COMMA", "returnValues"], "postprocess": ([hit,, rest]) => [hit, ...rest]}, {"name": "returnValues", "symbols": ["returnValue"]}, {"name": "returnValue", "symbols": ["value", "AS", "token"], "postprocess": ([original,, name]) => ({...original, alias: name.value})}, diff --git a/src/grammar.ne b/src/grammar.ne index 4203db1..0d344c6 100644 --- a/src/grammar.ne +++ b/src/grammar.ne @@ -17,6 +17,7 @@ explicitReturn -> L_CURLY singleLine multiLine R_CURLY {% ([,statement1, stateme # ... implicitReturn -> singleLine {% ([statement], _, reject) => statement.trim().startsWith('{') ? reject : ({returnValues: []}) %} + | L_PAREN L_CURLY multiLine R_CURLY R_PAREN {% () => ({returnValues: []}) %} # RETURN hi AS foo, rand() AS bar returnValues -> returnValue COMMA returnValues {% ([hit,, rest]) => [hit, ...rest] %} diff --git a/src/grammar.test.js b/src/grammar.test.js index 70377c4..defde76 100644 --- a/src/grammar.test.js +++ b/src/grammar.test.js @@ -260,6 +260,45 @@ describe('lambda-parser', () => { } ]) }); + + test('Supports returning objects wrapped in parens', () => { + const query = `x => ({foo: 1})`; + + expect(parseLambda(query)).toEqual([ + { + type: 'lambda', + variant: 'implicit', + parameters: { + type: 'token', + value: 'x' + }, + body: { + returnValues: [] + } + } + ]) + }); + + test('Supports returning objects wrapped in parens even with new lines', () => { + const query = `x => ({ + foo: 1 + })`; + + expect(parseLambda(query)).toEqual([ + { + type: 'lambda', + variant: 'implicit', + parameters: { + type: 'token', + value: 'x' + }, + body: { + returnValues: [] + } + }, + expect.anything() + ]) + }); }); describe('explicit returns', () => {