Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search parser grammar update #47328

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/libs/SearchParser/searchParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,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);
Expand Down Expand Up @@ -233,7 +233,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);

Expand All @@ -243,6 +243,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) {
Expand Down
20 changes: 14 additions & 6 deletions src/libs/SearchParser/searchParser.peggy
Original file line number Diff line number Diff line change
Expand Up @@ -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 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
Expand Down Expand Up @@ -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)))

Comment on lines +54 to +64
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could implement this as part of a rule, instead of plain JS. Anyways, NAB for now.


return allFilters.reduce((result, filter) => buildFilter("and", result, filter));
}

Expand Down Expand Up @@ -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
Loading