SearchQueryParser
1.0.0
SearchQueryParser
是一个简单的类似 Google 的 Swift 搜索引擎查询解析器(和标记)。它需要一个搜索字符串并构建其抽象语法树。该软件包适用于与 Apple 的 Search Kit 一起使用,以突出显示找到的结果。
巴科斯-诺尔形式 (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 )
https://github.com/SerhiyButz/SearchQueryParser.git
点击“下一步”
确保“规则”字段设置为如下所示:“版本:直到下一个主要版本:1.3.0”
单击“下一步”完成
欲了解更多信息,请查看此处。
将以下内容添加到您的 Podfile 中:
platform :osx , '10.12'
target 'YOUR-TARGET' do
use_frameworks!
pod 'SearchQueryParser' , :git => 'https://github.com/SerhiyButz/SearchQueryParser.git'
end
然后运行pod install
。
该项目已获得 MIT 许可。