From 703cf32a5dff2b08592306c535c313638b44d8b9 Mon Sep 17 00:00:00 2001 From: 289Adam289 Date: Tue, 13 Aug 2024 13:57:12 +0200 Subject: [PATCH 1/2] Add semicolon to grammr and impprove ast --- src/libs/SearchParser/searchParser.js | 17 +++++++++++++++-- src/libs/SearchParser/searchParser.peggy | 20 ++++++++++++++------ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/libs/SearchParser/searchParser.js b/src/libs/SearchParser/searchParser.js index 53d0ac82bec0..ac5e9f6765a4 100644 --- a/src/libs/SearchParser/searchParser.js +++ b/src/libs/SearchParser/searchParser.js @@ -167,6 +167,7 @@ peg$SyntaxError.buildMessage = function(expected, found) { }; function peg$parse(input, options) { + console.log(input) options = options !== undefined ? options : {}; var peg$FAILED = {}; @@ -203,7 +204,7 @@ function peg$parse(input, options) { var peg$r0 = /^[:=]/; var peg$r1 = /^[^"\r\n]/; - var peg$r2 = /^[A-Za-z0-9_@.\/#&+\-\\',]/; + var peg$r2 = /^[A-Za-z0-9_@.\/#&+\-\\',;]/; var peg$r3 = /^[ \t\r\n]/; var peg$e0 = peg$classExpectation([":", "="], false, false); @@ -233,7 +234,7 @@ function peg$parse(input, options) { var peg$e24 = peg$literalExpectation("sortOrder", false); var peg$e25 = peg$literalExpectation("\"", false); var peg$e26 = peg$classExpectation(["\"", "\r", "\n"], true, false); - var peg$e27 = peg$classExpectation([["A", "Z"], ["a", "z"], ["0", "9"], "_", "@", ".", "/", "#", "&", "+", "-", "\\", "'", ","], false, false); + var peg$e27 = peg$classExpectation([["A", "Z"], ["a", "z"], ["0", "9"], "_", "@", ".", "/", "#", "&", "+", "-", "\\", "'", ",", ";"], false, false); var peg$e28 = peg$otherExpectation("whitespace"); var peg$e29 = peg$classExpectation([" ", "\t", "\r", "\n"], false, false); @@ -243,6 +244,18 @@ function peg$parse(input, options) { if (!allFilters.length) { return null; } + const keywords = allFilters.filter((filter) => filter.left === "keyword" || filter.right?.left == "keyword") + const nonKeywords = allFilters.filter((filter) => filter.left !== "keyword" && filter.right?.left !== "keyword") + if(!nonKeywords.length){ + return keywords.reduce((result, filter) => buildFilter("or", result, filter)) + } + if(!keywords.length){ + return nonKeywords.reduce((result, filter) => buildFilter("and", result, filter)) + } + + return buildFilter("and", keywords.reduce((result, filter) => buildFilter("or", result, filter)), nonKeywords.reduce((result, filter) => buildFilter("and", result, filter))) + + return allFilters.reduce((result, filter) => buildFilter("and", result, filter)); }; var peg$f2 = function(field, op, value) { diff --git a/src/libs/SearchParser/searchParser.peggy b/src/libs/SearchParser/searchParser.peggy index 310e942bbb80..1577172b4b16 100644 --- a/src/libs/SearchParser/searchParser.peggy +++ b/src/libs/SearchParser/searchParser.peggy @@ -3,8 +3,7 @@ // // Here's a general grammar structure: // -// start: entry point for the parser. It calls the query rule and return its value. -// query: rule to process the values returned by the filterList rule. Takes filters as an argument and returns the final AST output. +// query: entry point for the parserand and rule to process the values returned by the filterList rule. Takes filters as an argument and returns the final AST output. // filterList: rule to process the array of filters returned by the filter rule. It takes head and tail as arguments, filters it for null values and builds the AST. // filter: rule to build the filter object. It takes field, operator and value as input and returns {operator, left: field, right: value} or null if the left value is a defaultValues // operator: rule to match pre-defined search syntax operators, e.g. !=, >, etc @@ -52,6 +51,18 @@ filterList if (!allFilters.length) { return null; } + const keywords = allFilters.filter((filter) => filter.left === "keyword" || filter.right?.left == "keyword") + const nonKeywords = allFilters.filter((filter) => filter.left !== "keyword" && filter.right?.left !== "keyword") + if(!nonKeywords.length){ + return keywords.reduce((result, filter) => buildFilter("or", result, filter)) + } + if(!keywords.length){ + return nonKeywords.reduce((result, filter) => buildFilter("and", result, filter)) + } + + return buildFilter("and", keywords.reduce((result, filter) => buildFilter("or", result, filter)), nonKeywords.reduce((result, filter) => buildFilter("and", result, filter))) + + return allFilters.reduce((result, filter) => buildFilter("and", result, filter)); } @@ -108,13 +119,10 @@ quotedString = '"' chars:[^"\r\n]* '"' { return chars.join(''); } alphanumeric - = chars:[A-Za-z0-9_@./#&+\-\\',]+ { return chars.join(''); } + = chars:[A-Za-z0-9_@./#&+\-\\',;]+ { return chars.join(''); } logicalAnd = _ { return "and"; } _ "whitespace" = [ \t\r\n]* - -start - = query From 39f5d26404f9ed9526902b26d7b0bf5bd58a567d Mon Sep 17 00:00:00 2001 From: 289Adam289 Date: Tue, 13 Aug 2024 16:04:25 +0200 Subject: [PATCH 2/2] fix typoes --- src/libs/SearchParser/searchParser.js | 3 +-- src/libs/SearchParser/searchParser.peggy | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/libs/SearchParser/searchParser.js b/src/libs/SearchParser/searchParser.js index ac5e9f6765a4..64d5334aa265 100644 --- a/src/libs/SearchParser/searchParser.js +++ b/src/libs/SearchParser/searchParser.js @@ -167,7 +167,6 @@ peg$SyntaxError.buildMessage = function(expected, found) { }; function peg$parse(input, options) { - console.log(input) options = options !== undefined ? options : {}; var peg$FAILED = {}; @@ -244,7 +243,7 @@ function peg$parse(input, options) { if (!allFilters.length) { return null; } - const keywords = allFilters.filter((filter) => filter.left === "keyword" || filter.right?.left == "keyword") + const keywords = allFilters.filter((filter) => filter.left === "keyword" || filter.right?.left === "keyword") const nonKeywords = allFilters.filter((filter) => filter.left !== "keyword" && filter.right?.left !== "keyword") if(!nonKeywords.length){ return keywords.reduce((result, filter) => buildFilter("or", result, filter)) diff --git a/src/libs/SearchParser/searchParser.peggy b/src/libs/SearchParser/searchParser.peggy index 1577172b4b16..d9ede101f7f8 100644 --- a/src/libs/SearchParser/searchParser.peggy +++ b/src/libs/SearchParser/searchParser.peggy @@ -3,7 +3,7 @@ // // Here's a general grammar structure: // -// query: entry point for the parserand and rule to process the values returned by the filterList rule. Takes filters as an argument and returns the final AST output. +// query: entry point for the parser and rule to process the values returned by the filterList rule. Takes filters as an argument and returns the final AST output. // filterList: rule to process the array of filters returned by the filter rule. It takes head and tail as arguments, filters it for null values and builds the AST. // filter: rule to build the filter object. It takes field, operator and value as input and returns {operator, left: field, right: value} or null if the left value is a defaultValues // operator: rule to match pre-defined search syntax operators, e.g. !=, >, etc @@ -51,7 +51,7 @@ filterList if (!allFilters.length) { return null; } - const keywords = allFilters.filter((filter) => filter.left === "keyword" || filter.right?.left == "keyword") + const keywords = allFilters.filter((filter) => filter.left === "keyword" || filter.right?.left === "keyword") const nonKeywords = allFilters.filter((filter) => filter.left !== "keyword" && filter.right?.left !== "keyword") if(!nonKeywords.length){ return keywords.reduce((result, filter) => buildFilter("or", result, filter))