1
0

search_query_parser.rb 688 B

123456789101112131415
  1. # frozen_string_literal: true
  2. class SearchQueryParser < Parslet::Parser
  3. rule(:term) { match('[^\s":]').repeat(1).as(:term) }
  4. rule(:quote) { str('"') }
  5. rule(:colon) { str(':') }
  6. rule(:space) { match('\s').repeat(1) }
  7. rule(:operator) { (str('+') | str('-')).as(:operator) }
  8. rule(:prefix) { (term >> colon).as(:prefix) }
  9. rule(:shortcode) { (colon >> term >> colon.maybe).as(:shortcode) }
  10. rule(:phrase) { (quote >> (term >> space.maybe).repeat >> quote).as(:phrase) }
  11. rule(:clause) { (prefix.maybe >> operator.maybe >> (phrase | term | shortcode)).as(:clause) }
  12. rule(:query) { (clause >> space.maybe).repeat.as(:query) }
  13. root(:query)
  14. end