Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions debug.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import parseLambda from './parse-lambda';
import {parseLambda} from './src';

const something = `[{x}] => { CALL db.labels() YIELD label

Expand All @@ -9,10 +9,10 @@ foo

RETURN 1
}`;
const result1 = parseLambda(something)
const result2 = parseLambda('x => {asdsd RETURN rand() }')
const result3 = parseLambda('[{rand():x}] => { RETURN rand() AS bar }')
const result4 = parseLambda('x1 => "foo"')
// const result1 = parseLambda(something)
const result2 = parseLambda('x => { \n \n \n asdsd RETURN rand() }')
// const result3 = parseLambda('[{rand():x}] => { RETURN rand() AS bar }')
// const result4 = parseLambda('x1 => "foo"')

console.log(JSON.stringify(result1, null, 2));
console.log(`Ambiguity ${result1.length}`)
console.log(JSON.stringify(result2, null, 2));
console.log(`Ambiguity ${result2.length}`)
10 changes: 5 additions & 5 deletions src/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,10 @@ let ParserRules = [
},
{"name": "lambda", "symbols": ["explicitParameters", "FAT_ARROW", "explicitReturn"], "postprocess": ([parameters,,body]) => ({type: 'lambda', variant: 'explicit', parameters, body})},
{"name": "lambda", "symbols": ["implicitParameters", "FAT_ARROW", "implicitReturn"], "postprocess": ([parameters,,body]) => ({type: 'lambda', variant: 'implicit', parameters, body})},
{"name": "explicitReturn", "symbols": ["L_CURLY", "singleLine", "multiLine", "__", "RETURN", "returnValues", "R_CURLY"], "postprocess": ([,statement1, statement2,,, returnValues]) => ({statement: [statement1, statement2].join('\n').trim(), returnValues})},
{"name": "explicitReturn", "symbols": ["L_CURLY", "multiLine", "__", "RETURN", "returnValues", "R_CURLY"], "postprocess": ([,statement,,, returnValues]) => ({statement, returnValues})},
{"name": "explicitReturn", "symbols": ["L_CURLY", "singleLine", "__", "RETURN", "returnValues", "R_CURLY"], "postprocess": ([,statement,,, returnValues]) => ({statement, returnValues})},
{"name": "explicitReturn", "symbols": ["L_CURLY", "_", "RETURN", "returnValues", "R_CURLY"], "postprocess": ([,,, returnValues]) => ({statement: '', returnValues})},
{"name": "implicitReturn", "symbols": ["returnValue"], "postprocess": ([returnValue]) => ({returnValues: [returnValue]})},
{"name": "explicitReturn", "symbols": ["L_CURLY", "singleLine", "multiLine", "R_CURLY"], "postprocess": ([,statement1, statement2]) => ({statement: [statement1, statement2].join('\n').trim(), returnValues: []})},
{"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": "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})},
Expand Down Expand Up @@ -197,6 +196,7 @@ let ParserRules = [
{"name": "chars", "symbols": [/[a-zA-Z]/, "chars$ebnf$1"], "postprocess": ([value, rest]) => `${value}${rest.join('')}`},
{"name": "multiLine", "symbols": ["newLine", "singleLine", "multiLine"], "postprocess": ([, hit, rest], _ , reject) => rest ? [hit, rest].join('\n').trim() : reject},
{"name": "multiLine", "symbols": ["newLine", "multiLine"], "postprocess": ([hit, rest]) => [hit, rest].join('\n').trim()},
{"name": "multiLine", "symbols": ["newLine", "singleLine"], "postprocess": ([hit, rest]) => [hit, rest].join('\n').trim()},
{"name": "multiLine", "symbols": ["newLine"], "postprocess": id},
{"name": "singleLine$ebnf$1", "symbols": [/[^\n]/]},
{"name": "singleLine$ebnf$1", "symbols": ["singleLine$ebnf$1", /[^\n]/], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
Expand Down
10 changes: 5 additions & 5 deletions src/grammar.ne
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ lambda -> explicitParameters FAT_ARROW explicitReturn {% ([parameters,,body]) =>
| implicitParameters FAT_ARROW implicitReturn {% ([parameters,,body]) => ({type: 'lambda', variant: 'implicit', parameters, body}) %}

# { RETURN ... }
explicitReturn -> L_CURLY singleLine multiLine __ RETURN returnValues R_CURLY {% ([,statement1, statement2,,, returnValues]) => ({statement: [statement1, statement2].join('\n').trim(), returnValues}) %}
| L_CURLY multiLine __ RETURN returnValues R_CURLY {% ([,statement,,, returnValues]) => ({statement, returnValues}) %}
| L_CURLY singleLine __ RETURN returnValues R_CURLY {% ([,statement,,, returnValues]) => ({statement, returnValues}) %}
| L_CURLY _ RETURN returnValues R_CURLY {% ([,,, returnValues]) => ({statement: '', returnValues}) %}
explicitReturn -> L_CURLY singleLine multiLine R_CURLY {% ([,statement1, statement2]) => ({statement: [statement1, statement2].join('\n').trim(), returnValues: []}) %}
| L_CURLY multiLine R_CURLY {% ([,statement]) => ({statement, returnValues: []}) %}
| L_CURLY singleLine R_CURLY {% ([,statement]) => ({statement, returnValues: []}) %}

# ...
implicitReturn -> returnValue {% ([returnValue]) => ({returnValues: [returnValue]}) %}
implicitReturn -> singleLine {% ([statement], _, reject) => statement.trim().startsWith('{') ? reject : ({returnValues: []}) %}

# RETURN hi AS foo, rand() AS bar
returnValues -> returnValue COMMA returnValues {% ([hit,, rest]) => [hit, ...rest] %}
Expand Down Expand Up @@ -105,6 +104,7 @@ chars -> [a-zA-Z] [a-zA-Z0-9]:* {% ([value, rest]) => `${value}${rest.join('')}`

multiLine -> newLine singleLine multiLine {% ([, hit, rest], _ , reject) => rest ? [hit, rest].join('\n').trim() : reject %}
| newLine multiLine {% ([hit, rest]) => [hit, rest].join('\n').trim() %} # щ(゚Д゚щ)
| newLine singleLine {% ([hit, rest]) => [hit, rest].join('\n').trim() %}
| newLine {% id %}

singleLine -> [^\n]:+ {% ([hit], _, reject) => hit.join('').trim() %}
Expand Down
Loading