class Mhc::Query::Context

Constants

TOKENS
TOKEN_REGEXP

Public Class Methods

new(string) click to toggle source
# File lib/mhc/query.rb, line 144
def initialize(string)
  @tokens = tokenize(string)
end

Public Instance Methods

debug_dump() click to toggle source
# File lib/mhc/query.rb, line 162
def debug_dump
  @tokens.map{|token| "#{token.type} => #{token.value}"}.join(", ")
end
eat_if(*expected_types) click to toggle source
# File lib/mhc/query.rb, line 148
def eat_if(*expected_types)
  expected_types.each do |expected_type|
    if @tokens.first and @tokens.first.type == expected_type
      return @tokens.shift
    end
  end
  return nil
end
expect(*expected_types) click to toggle source
# File lib/mhc/query.rb, line 157
def expect(*expected_types)
  token = eat_if(*expected_types) and return token
  raise ParseError, "#{expected_types.map(&:upcase).join(' or ')} expected before #{@tokens.first.value rescue 'END'}"
end

Private Instance Methods

get_token(string) click to toggle source
# File lib/mhc/query.rb, line 181
def get_token(string)
  if match = TOKEN_REGEXP.match(string)
    name   = match.names.find{|name| match[name]}
    value  = match[name]
    remain = match.post_match.strip
    return [Token.new(name, value), remain]
  end
  return [nil, string]
end
tokenize(string) click to toggle source
# File lib/mhc/query.rb, line 168
def tokenize(string)
  tokens = []

  loop do
    token, string = get_token(string)
    break if token.nil?
    tokens << token
  end

  raise ParseError, "can not tokenize '#{string}'" unless string.length == 0
  return tokens
end