Skip to content

Commit

Permalink
Added support for single character strings (#91)
Browse files Browse the repository at this point in the history
ref #20

I -believe- this works all right. It's at the end of the rules so it should allow for matching on just about anything else practical, then allow us a chance at matching a single character string, before throwing a Lexical Error.

I've added negation for all special chars except for + and - as you ought to be able to do 'post:a+other' and 'post:a-something'.
  • Loading branch information
9larsons authored Oct 17, 2024
1 parent ca80ddf commit 07cc8c1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
6 changes: 4 additions & 2 deletions packages/nql-lang/dist/parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/nql-lang/src/nql.l
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ now(?=[-+]\d+[dwMyhms](?:{final}|$)) { this.pushState('re
'~^' return 'STARTSWITH';
'~$' return 'ENDSWITH';
'~' return 'CONTAINS';
([a-zA-Z])(?![a-zA-Z'"\,\(\)\>\<=\[\]\~]) return 'LITERAL';


%%
Expand Down
14 changes: 11 additions & 3 deletions packages/nql-lang/test/lexer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ describe('Lexer', function () {
(function () {
lex('my&valu\'e!');
}).should.throw(lexicalError);
lex('a').should.eql([
{token: 'LITERAL', matched: 'a'}
]);
lex('a-b').should.eql([
{token: 'LITERAL', matched: 'a-b'}
]);
lex('a+bc').should.eql([
{token: 'LITERAL', matched: 'a'},
{token: 'AND', matched: '+'},
{token: 'LITERAL', matched: 'bc'}
]);
});

it('should separate NOT at beginning of literal', function () {
Expand All @@ -180,9 +191,6 @@ describe('Lexer', function () {
});

it('should NOT permit special chars inside a literal', function () {
(function () {
lex('t+st');
}).should.throw(lexicalError);
(function () {
lex('t,st');
}).should.throw(lexicalError);
Expand Down
6 changes: 6 additions & 0 deletions packages/nql-lang/test/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,5 +627,11 @@ describe('Parser', function () {
sinon.assert.calledTwice(relDateToAbsoluteSpy);
});
});

describe('Single character literals', function () {
it('can handle single character literals', function () {
parse('name:a').should.eql({name: 'a'});
});
});
});
});

0 comments on commit 07cc8c1

Please sign in to comment.