class Parser

Constants

BINARY_OPERATORS
COMMANDS

Attributes

tokens[R]

Public Class Methods

new(tokens) click to toggle source
# File bin/twitter-algebra, line 283
def initialize(tokens)
  @tokens = tokens
end

Public Instance Methods

parse() click to toggle source
# File bin/twitter-algebra, line 287
def parse
  expr = expression
  expect_token(:END)
  expr
end

Private Instance Methods

binary_expression() click to toggle source
# File bin/twitter-algebra, line 321
def binary_expression
  left = primary_expression
  while klass = BINARY_OPERATORS[peek_token.type]
    next_token
    left = klass.new(left, primary_expression)
  end
  left
end
complement_expression() click to toggle source
# File bin/twitter-algebra, line 345
def complement_expression
  expect_token(:COMPLEMENT)
  AST::Complement.new(primary_expression)
end
expect_token(type) click to toggle source
# File bin/twitter-algebra, line 304
def expect_token(type)
  token = next_token
  if token.type != type
    raise SyntaxError, "Expected #{type}, saw #{token.value.inspect}"
  end
  token
end
expression() click to toggle source
# File bin/twitter-algebra, line 312
def expression
  binary_expression
end
handle_command_expression(handle) click to toggle source
# File bin/twitter-algebra, line 385
def handle_command_expression(handle)
  expect_token(:DOT)
  command = expect_token(:BAREWORD).value
  if klass = COMMANDS[command]
    klass.new(handle)
  else
    raise SyntaxError, "Unknown command #{command.inspect}"
  end
end
handle_expression() click to toggle source
# File bin/twitter-algebra, line 374
def handle_expression
  handle = expect_token(:BAREWORD).value
  if peek_token.type == :DOT
    handle_command_expression(handle)
  elsif peek_token.type == :SLASH
    list_expression(handle)
  else
    raise SyntaxError, "Unexpected #{peek_token.value.inspect}"
  end
end
handle_set_expression() click to toggle source
# File bin/twitter-algebra, line 362
def handle_set_expression
  expect_token(:OPEN_BRACE)
  handles = []
  while peek_token.type != :CLOSE_BRACE
    handles << expect_token(:BAREWORD).value
    break if peek_token.type == :CLOSE_BRACE
    expect_token(:COMMA)
  end
  expect_token(:CLOSE_BRACE)
  AST::HandleSet.new(handles)
end
list_expression(handle) click to toggle source
# File bin/twitter-algebra, line 395
def list_expression(handle)
  expect_token(:SLASH)
  list_name = expect_token(:BAREWORD).value
  AST::ListMembers.new(handle, list_name)
end
next_token() click to toggle source
# File bin/twitter-algebra, line 300
def next_token
  tokens.shift
end
parenthesized_expression() click to toggle source
# File bin/twitter-algebra, line 350
def parenthesized_expression
  expect_token(:OPEN_PAREN)
  expr = expression
  expect_token(:CLOSE_PAREN)
  expr
end
peek_token() click to toggle source
# File bin/twitter-algebra, line 296
def peek_token
  tokens.first
end
primary_expression() click to toggle source
# File bin/twitter-algebra, line 330
def primary_expression
  case peek_token.type
  when :COMPLEMENT
    complement_expression
  when :OPEN_PAREN
    parenthesized_expression
  when :OPEN_BRACE
    handle_set_expression
  when :BAREWORD
    handle_expression
  else
    raise SyntaxError, "Unexpected #{peek_token.value.inspect}"
  end
end