added not() expression

This commit is contained in:
zadam 2020-05-27 00:09:19 +02:00
parent dc2d5a0a79
commit 55b210d7c5
4 changed files with 32 additions and 1 deletions

View File

@ -63,4 +63,9 @@ describe("Lexer expression", () => {
expect(lexer(`# ~author.title = 'Hugh Howey' AND note.'book title' = 'Silo'`).expressionTokens) expect(lexer(`# ~author.title = 'Hugh Howey' AND note.'book title' = 'Silo'`).expressionTokens)
.toEqual(["#", "~author", ".", "title", "=", "hugh howey", "and", "note", ".", "book title", "=", "silo"]); .toEqual(["#", "~author", ".", "title", "=", "hugh howey", "and", "note", ".", "book title", "=", "silo"]);
}); });
it("negation of sub-expression", () => {
expect(lexer(`# not(#capital) and note.noteId != "root"`).expressionTokens)
.toEqual(["#", "not", "(", "#capital", ")", "and", "note", ".", "noteid", "!=", "root"]);
});
}); });

View File

@ -46,7 +46,7 @@ describe("Parser", () => {
it("simple label AND", () => { it("simple label AND", () => {
const rootExp = parser({ const rootExp = parser({
fulltextTokens: [], fulltextTokens: [],
expressionTokens: ["#first", "=", "text", "AND", "#second", "=", "text"], expressionTokens: ["#first", "=", "text", "and", "#second", "=", "text"],
parsingContext: new ParsingContext(true) parsingContext: new ParsingContext(true)
}); });

View File

@ -512,5 +512,21 @@ describe("Search", () => {
expect(searchResults.length).toEqual(4); expect(searchResults.length).toEqual(4);
}); });
it("test not(...)", async () => {
const italy = note("Italy").label("capital", "Rome");
const slovakia = note("Slovakia").label("capital", "Bratislava");
rootNote
.child(note("Europe")
.child(slovakia)
.child(italy));
const parsingContext = new ParsingContext();
let searchResults = await searchService.findNotesWithQuery('# not(#capital) and note.noteId != root', parsingContext);
expect(searchResults.length).toEqual(1);
expect(noteCache.notes[searchResults[0].noteId].title).toEqual("Europe");
});
// FIXME: test what happens when we order without any filter criteria // FIXME: test what happens when we order without any filter criteria
}); });

View File

@ -255,6 +255,16 @@ function getExpression(tokens, parsingContext, level = 0) {
return exp; return exp;
} }
else if (token === 'not') {
i += 1;
if (!Array.isArray(tokens[i])) {
parsingContext.addError(`not keyword should be followed by sub-expression in parenthesis, got ${tokens[i]} instead`);
continue;
}
expressions.push(new NotExp(getExpression(tokens[i], parsingContext, level++)));
}
else if (token === 'note') { else if (token === 'note') {
i++; i++;