Sha256: 5fae64fa77550aea63a9071ae9e0c145e583b62fb5bb95c76607d421d9a1e927

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

class SQLTree::Parser

  class UnexpectedToken < StandardError

    attr_reader :expected_token, :actual_token

    def initialize(actual_token, expected_token = nil)
      @expected_token, @actual_token = expected_token, actual_token
      message =  "Unexpected token: found #{actual_token.inspect}"
      message << ", but expected #{expected_token.inspect}" if expected_token
      message << '!'
      super(message)
    end
  end

  def self.parse(sql_string, options = {})
    self.new(sql_string, options).parse!
  end

  attr_reader :options

  def initialize(tokens, options)
    if tokens.kind_of?(String)
      @tokens = SQLTree::Tokenizer.new.tokenize(tokens)
    else
      @tokens = tokens
    end
    @options = options
  end

  def current
    @current_token
  end

  def next
    @current_token = @tokens.shift
  end

  def consume(*checks)
    checks.each do |check|
      raise UnexpectedToken.new(self.current, check) unless check == self.next
    end
  end

  def peek(distance = 1)
    @tokens[distance - 1]
  end

  def peek_tokens(amount)
    @tokens[0, amount]
  end

  def debug
    puts @tokens.inspect
  end

  def parse!
    case self.peek
    when SQLTree::Token::SELECT then SQLTree::Node::SelectQuery.parse(self)
    when SQLTree::Token::INSERT then SQLTree::Node::InsertQuery.parse(self)
    when SQLTree::Token::DELETE then SQLTree::Node::DeleteQuery.parse(self)
    when SQLTree::Token::UPDATE then SQLTree::Node::UpdateQuery.parse(self)
    else raise UnexpectedToken.new(self.peek)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sql_tree-0.1.0 lib/sql_tree/parser.rb