Skip to content

Commit

Permalink
fix: handle whitespace at the strat of the query
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Oct 2, 2022
1 parent 5bb19a8 commit 32620de
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/grammar.ne
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@preprocessor typescript

main -> logical_expression {% id %}
main -> _ logical_expression {% (data) => data[1] %}

# Whitespace: `_` is optional, `__` is mandatory.
_ -> whitespace_character:* {% (data) => data[0].length %}
Expand Down
2 changes: 1 addition & 1 deletion src/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ interface Grammar {
const grammar: Grammar = {
Lexer: undefined,
ParserRules: [
{"name": "main", "symbols": ["logical_expression"], "postprocess": id},
{"name": "main", "symbols": ["_", "logical_expression"], "postprocess": (data) => data[1]},
{"name": "_$ebnf$1", "symbols": []},
{"name": "_$ebnf$1", "symbols": ["_$ebnf$1", "whitespace_character"], "postprocess": (d) => d[0].concat([d[1]])},
{"name": "_", "symbols": ["_$ebnf$1"], "postprocess": (data) => data[0].length},
Expand Down
44 changes: 44 additions & 0 deletions test/liqe/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,50 @@ test('foo', testQuery, {
type: 'Tag',
});

test('foo with whitespace at the start', (t) => {
t.deepEqual(parse(' foo'), {
expression: {
location: {
end: 4,
start: 1,
},
quoted: false,
type: 'LiteralExpression',
value: 'foo',
},
field: {
type: 'ImplicitField',
},
location: {
end: 4,
start: 1,
},
type: 'Tag',
});
});

test('foo with whitespace at the end', (t) => {
t.deepEqual(parse('foo'), {
expression: {
location: {
end: 3,
start: 0,
},
quoted: false,
type: 'LiteralExpression',
value: 'foo',
},
field: {
type: 'ImplicitField',
},
location: {
end: 3,
start: 0,
},
type: 'Tag',
});
});

test('(foo)', testQuery, {
expression: {
expression: {
Expand Down

0 comments on commit 32620de

Please sign in to comment.