Skip to content

Commit

Permalink
Merge pull request Expensify#47328 from software-mansion-labs/289Adam…
Browse files Browse the repository at this point in the history
…289/47095-search-parser-grammar-update

Search parser grammar update
  • Loading branch information
luacmartins authored Aug 20, 2024
2 parents d519981 + 6939837 commit ccec331
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
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)))


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

0 comments on commit ccec331

Please sign in to comment.