You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

15 lines
688 B

  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