Sha256: 35a03299b07c0ee458fa00d1c8621563e9e5973592b58c4364cbec49424a6260

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module Pursuit
  # Parser for a list of terms.
  #
  class TermParser < Parslet::Parser
    # Whitespace

    rule(:space)  { match('\s').repeat(1) }
    rule(:space?) { match('\s').repeat(0) }

    # Character Types

    rule(:escaped_character) do
      str('\\') >> match('.')
    end

    # String Types

    rule(:string_double_quotes) do
      str('"') >> (escaped_character | match('[^"]')).repeat(0).as(:string_double_quotes) >> str('"')
    end

    rule(:string_single_quotes) do
      str("'") >> (escaped_character | match("[^']")).repeat(0).as(:string_single_quotes) >> str("'")
    end

    rule(:string_no_quotes) do
      match('[^\s]').repeat(1).as(:string_no_quotes)
    end

    rule(:string) do
      string_double_quotes | string_single_quotes | string_no_quotes
    end

    # Terms

    rule(:term)      { string.as(:term) }
    rule(:term_pair) { term.as(:left) >> space >> term_node.as(:right) }
    rule(:term_node) { term_pair | term }
    rule(:terms)     { space? >> term_node >> space? }
    root(:terms)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pursuit-1.1.0 lib/pursuit/term_parser.rb
pursuit-1.0.1 lib/pursuit/term_parser.rb