Class: StructuredSearch::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/structured_search/parser.rb

Overview

Parses a token stream, returning an array of StructuredSearch::Statement

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, providers) ⇒ Parser

Creates a new instance of the Parser, taking a Lexer and a hash of providers (An identifier and class that contains the search method) Params:

lexer

A StructuredSearch::Lexer object

providers

A Hash of provider names and their classes



14
15
16
17
18
19
20
# File 'lib/structured_search/parser.rb', line 14

def initialize(input, providers)
  @lexer = StructuredSearch::Lexer.new(input)
  @providers = Hash.new
  providers.each { |k,v| @providers[k.downcase] = v }
  
  @nodes, @statements = [], []
end

Instance Attribute Details

#providersObject (readonly)

stores all parse tree nodes



7
8
9
# File 'lib/structured_search/parser.rb', line 7

def providers
  @providers
end

#statementsObject (readonly)

stores all parse tree nodes



7
8
9
# File 'lib/structured_search/parser.rb', line 7

def statements
  @statements
end

Instance Method Details

#basic_optionsObject

Basic options given to BaseNode when creating a new instance of a node, including the line, column and type



59
60
61
# File 'lib/structured_search/parser.rb', line 59

def basic_options
  { line: @current_token.line, column: @current_token.column, type: @current_token.token }
end

#parseObject

Parses the next token in the token stream into an AST node Returns an AST node



46
47
48
49
50
51
52
53
54
55
# File 'lib/structured_search/parser.rb', line 46

def parse
  @current_token = read_token
  
  case @current_token.token
    when :SEMICOLON
      new_statement
    else
      send "new_#{@current_token.token.downcase}"
  end
end

#parse_to_endObject

Parses the token stream into statements until there are no more tokens left



34
35
36
37
38
39
40
41
42
# File 'lib/structured_search/parser.rb', line 34

def parse_to_end
  while peek_token
    new_node = parse
    @nodes << new_node if new_node
  end

  # flush token stream to a statement if no semicolon
  new_statement if @nodes.length > 0
end

#peek_tokenObject

Peeks at the next token in stream



28
29
30
# File 'lib/structured_search/parser.rb', line 28

def peek_token
  @lexer.scan(true)
end

#read_tokenObject

Reads the next token in the stream



23
24
25
# File 'lib/structured_search/parser.rb', line 23

def read_token
  @lexer.scan
end