SearchQueryParser
is a simple Google-like search-engine-query parser (and marker) for Swift. It takes a search string and builds its abstract syntax tree. This package is applicable for use with Apple's Search Kit for highlighting of found results.
- Scannerless (lexerless)
- Boolean-like syntax support (AND ("&"), OR ("|"), NOT ("!") logical operators)
- Prefix and suffix wildcards ("*")
- Phrase searches
- Tolerant to search query syntax errors
- Provides facilities to locate search terms within an input text
Backus–Naur form (BNF):
Query = Sweeping-Mute-And-Term, ⧚’?, EOF ;
Sweeping-Mute-And-Term = Mute-And-Term, { ⧚’?, Mute-And-Term }* ;
Mute-And-Term = Or-Term, Or-Term* ;
Or-Term = And-Term, {⧚?, (‘|’ | “OR”), And-Term }* ;
And-Term = Not-Term, {⧚?, (‘&’ | “AND”), Not-Term }* ;
Not-Term = ⧚?, (’!’ | “NOT”), Not-Term | Primary-Term ;
Primary-Term = ⧚?, ’(‘ ⧚?, Mute-And-Term, ⧚? ‘)’ |
⧚?, “‘“ ⧚?, Phrase, ⧚? “‘“ |
⧚?, Prefix-Wildcard-Search-Term |
⧚?, Suffix-Wildcard-Search-Term |
⧚?, Search-Term ;
Phrase = Search-Term, { ⧚, Search-Term }* ;
Prefix-Wildcard-Search-Term = ‘*’, Search-Term ;
Suffix-Wildcard-Search-Term = Search-Term, ‘*’ ;
Search-Term = Letter | Letter, Search-Term ;
Letter = Alpha | Digit | ‘_’ ;
⧚’ = ⧚-(“*” | “!” | “&” | “(“ | “)” | “\””)
⧚ = { ? any-character ?-Letter }+ ;
Alpha = ? alpha-character ? ;
Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
import SearchQueryParser
func printQueryAST(_ query: String) {
guard let parser = SearchQueryParser(query), let ast = parser.astRoot
else {
print("Couldn't parse")
return
}
print("AST: \(ast)")
}
printQueryAST("foo bar")
import SearchQueryParser
func highlightSearchQueryMatches(query: String, in text: NSMutableAttributedString) {
guard let queryTerms = SearchQueryParser(query)
else {
print("No matches found")
return
}
let tokensProvider = SearchTextScanner(text.string)
guard let applicator = SearchQueryApplicator(searchQueryTerms: queryTerms,
textTokens: tokensProvider)
else {
print("Nothing was marked")
return
}
applicator.markedRanges.forEach { range in
text.addAttribute(.backgroundColor, value: NSColor.yellow, range: range)
}
}
let text = NSMutableAttributedString(string: "Lorem ipsum ...")
highlightSearchQueryMatches(query: "foo & bar", in: text)
- Go to "File" -> "Swift Packages" -> "Add Package Dependency"
- Paste Search Query Parser repository URL into the search field:
https://github.com/SerhiyButz/SearchQueryParser.git
-
Click "Next"
-
Ensure that the "Rules" field is set to something like this: "Version: Up To Next Major: 1.3.0"
-
Click "Next" to finish
For more info, check out here.
Add the following to your Podfile:
platform :osx, '10.12'
target 'YOUR-TARGET' do
use_frameworks!
pod 'SearchQueryParser', :git => 'https://github.com/SerhiyButz/SearchQueryParser.git'
end
Then run pod install
.
This project is licensed under the MIT license.