Skip to content

Commit

Permalink
feat: add location to field
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Sep 29, 2022
1 parent 0de9158 commit a3179b8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/grammar.ne
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const field = d => {
path: d[0].name.split('.').filter(Boolean),
quoted: d[0].quoted,
quotes: d[0].quotes,
location: d[0].location,
},
...d[3]
}
Expand Down Expand Up @@ -98,9 +99,9 @@ side ->
| query {% d => ({field: {name: '<implicit>'}, ...d[0]}) %}

field ->
[_a-zA-Z$] [a-zA-Z\d_$.]:* {% d => ({name: d[0] + d[1].join(''), quoted: false}) %}
| sqstring {% d => ({name: d[0], quoted: true, quotes: 'single'}) %}
| dqstring {% d => ({name: d[0], quoted: true, quotes: 'double'}) %}
[_a-zA-Z$] [a-zA-Z\d_$.]:* {% (data, location) => ({name: data[0] + data[1].join(''), quoted: false, location}) %}
| sqstring {% (data, location) => ({name: data[0], quoted: true, quotes: 'single', location}) %}
| dqstring {% (data, location) => ({name: data[0], quoted: true, quotes: 'double', location}) %}

query ->
relational_operator _ decimal {% d => ({quoted: false, query: d[2], relationalOperator: d[0][0]}) %}
Expand Down
7 changes: 4 additions & 3 deletions src/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const field = d => {
path: d[0].name.split('.').filter(Boolean),
quoted: d[0].quoted,
quotes: d[0].quotes,
location: d[0].location,
},
...d[3]
}
Expand Down Expand Up @@ -231,9 +232,9 @@ const grammar: Grammar = {
{"name": "side", "symbols": ["query"], "postprocess": d => ({field: {name: '<implicit>'}, ...d[0]})},
{"name": "field$ebnf$1", "symbols": []},
{"name": "field$ebnf$1", "symbols": ["field$ebnf$1", /[a-zA-Z\d_$.]/], "postprocess": (d) => d[0].concat([d[1]])},
{"name": "field", "symbols": [/[_a-zA-Z$]/, "field$ebnf$1"], "postprocess": d => ({name: d[0] + d[1].join(''), quoted: false})},
{"name": "field", "symbols": ["sqstring"], "postprocess": d => ({name: d[0], quoted: true, quotes: 'single'})},
{"name": "field", "symbols": ["dqstring"], "postprocess": d => ({name: d[0], quoted: true, quotes: 'double'})},
{"name": "field", "symbols": [/[_a-zA-Z$]/, "field$ebnf$1"], "postprocess": (data, location) => ({name: data[0] + data[1].join(''), quoted: false, location})},
{"name": "field", "symbols": ["sqstring"], "postprocess": (data, location) => ({name: data[0], quoted: true, quotes: 'single', location})},
{"name": "field", "symbols": ["dqstring"], "postprocess": (data, location) => ({name: data[0], quoted: true, quotes: 'double', location})},
{"name": "query", "symbols": ["relational_operator", "_", "decimal"], "postprocess": d => ({quoted: false, query: d[2], relationalOperator: d[0][0]})},
{"name": "query", "symbols": ["decimal"], "postprocess": d => ({quoted: false, query: d.join('')})},
{"name": "query", "symbols": ["regex"], "postprocess": d => ({quoted: false, regex: true, query: d.join('')})},
Expand Down
5 changes: 5 additions & 0 deletions test/liqe/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ test('foo:null', testQuery, {

test('foo.bar:baz', testQuery, {
field: {
location: 0,
name: 'foo.bar',
path: [
'foo',
Expand All @@ -206,20 +207,23 @@ test('foo.bar:baz', testQuery, {

test('foo_bar:baz', testQuery, {
field: {
location: 0,
name: 'foo_bar',
},
query: 'baz',
});

test('$foo:baz', testQuery, {
field: {
location: 0,
name: '$foo',
},
query: 'baz',
});

test('"foo bar":baz', testQuery, {
field: {
location: 0,
name: 'foo bar',
quoted: true,
quotes: 'double',
Expand All @@ -229,6 +233,7 @@ test('"foo bar":baz', testQuery, {

test('\'foo bar\':baz', testQuery, {
field: {
location: 0,
name: 'foo bar',
quoted: true,
quotes: 'single',
Expand Down

0 comments on commit a3179b8

Please sign in to comment.