Notes/src/services/search/parser.js

104 lines
3.0 KiB
JavaScript
Raw Normal View History

2020-05-19 00:00:35 +02:00
const AndExp = require('./expressions/and');
const OrExp = require('./expressions/or');
const NotExp = require('./expressions/not');
2020-05-20 00:03:33 +02:00
const AttributeExistsExp = require('./expressions/attribute_exists');
const FieldComparisonExp = require('./expressions/field_comparison');
2020-05-19 00:00:35 +02:00
const NoteCacheFulltextExp = require('./expressions/note_cache_fulltext');
const NoteContentFulltextExp = require('./expressions/note_content_fulltext');
2020-05-20 23:20:39 +02:00
const comparatorBuilder = require('./comparator_builder');
2020-05-19 00:00:35 +02:00
function getFulltext(tokens, includingNoteContent) {
2020-05-20 23:20:39 +02:00
if (tokens.length === 0) {
return null;
}
else if (includingNoteContent) {
return new OrExp([
new NoteCacheFulltextExp(tokens),
new NoteContentFulltextExp(tokens)
]);
2020-05-19 00:00:35 +02:00
}
else {
2020-05-20 23:20:39 +02:00
return new NoteCacheFulltextExp(tokens);
2020-05-19 00:00:35 +02:00
}
}
function isOperator(str) {
2020-05-20 23:20:39 +02:00
return str.match(/^[=<>*]+$/);
2020-05-19 00:00:35 +02:00
}
2020-05-20 23:20:39 +02:00
function getExpression(tokens) {
if (tokens.length === 0) {
return null;
}
2020-05-19 00:00:35 +02:00
const expressions = [];
let op = null;
for (let i = 0; i < tokens.length; i++) {
const token = tokens[i];
if (token === '#' || token === '@') {
continue;
}
if (Array.isArray(token)) {
2020-05-20 23:20:39 +02:00
expressions.push(getExpression(token));
2020-05-19 00:00:35 +02:00
}
else if (token.startsWith('#') || token.startsWith('@')) {
const type = token.startsWith('#') ? 'label' : 'relation';
if (i < tokens.length - 2 && isOperator(tokens[i + 1])) {
2020-05-20 23:20:39 +02:00
const operator = tokens[i + 1];
const comparedValue = tokens[i + 2];
const comparator = comparatorBuilder(operator, comparedValue);
if (!comparator) {
throw new Error(`Can't find operator '${operator}'`);
}
expressions.push(new FieldComparisonExp(type, token.substr(1), comparator));
2020-05-19 00:00:35 +02:00
i += 2;
}
else {
2020-05-20 00:03:33 +02:00
expressions.push(new AttributeExistsExp(type, token.substr(1)));
2020-05-19 00:00:35 +02:00
}
}
else if (['and', 'or'].includes(token.toLowerCase())) {
if (!op) {
op = token.toLowerCase();
}
else if (op !== token.toLowerCase()) {
throw new Error('Mixed usage of AND/OR - always use parenthesis to group AND/OR expressions.');
}
}
else if (isOperator(token)) {
throw new Error(`Misplaced or incomplete expression "${token}"`);
}
else {
throw new Error(`Unrecognized expression "${token}"`);
}
if (!op && expressions.length > 1) {
op = 'and';
}
}
2020-05-20 00:03:33 +02:00
2020-05-20 23:20:39 +02:00
if (op === null || op === 'and') {
return AndExp.of(expressions);
}
else if (op === 'or') {
return OrExp.of(expressions);
}
2020-05-19 00:00:35 +02:00
}
function parse(fulltextTokens, expressionTokens, includingNoteContent) {
return AndExp.of([
2020-05-20 23:20:39 +02:00
getFulltext(fulltextTokens, includingNoteContent),
getExpression(expressionTokens)
2020-05-19 00:00:35 +02:00
]);
}
2020-05-20 00:03:33 +02:00
module.exports = parse;