Class: Rjq::Parser

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

Constant Summary collapse

DEFAULT_MAX_FILTER_DEPTH =
256
ASSIGNMENT_OPERATORS =
['=', '|=', '+=', '-=', '*=', '/=', '%=', '//='].freeze
COMPARISON_OPERATORS =
%w[== != < <= > >=].freeze
ADDITIVE_OPERATORS =
%w[+ -].freeze
MULTIPLICATIVE_OPERATORS =
%w[* / %].freeze
PRECEDENCE =
{
  '|' => 1,
  'as' => 2,
  ',' => 3,
  '=' => 4,
  '|=' => 4,
  '+=' => 4,
  '-=' => 4,
  '*=' => 4,
  '/=' => 4,
  '%=' => 4,
  '//=' => 4,
  'or' => 5,
  'and' => 6,
  '//' => 7,
  '==' => 8,
  '!=' => 8,
  '<' => 8,
  '<=' => 8,
  '>' => 8,
  '>=' => 8,
  '+' => 9,
  '-' => 9,
  '*' => 10,
  '/' => 10,
  '%' => 10
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(source, allow_comments: true, source_name: '<top-level>', initial_line: 1, initial_column: 1, start_offset: 0, max_filter_depth: DEFAULT_MAX_FILTER_DEPTH) ⇒ Parser

Returns a new instance of Parser.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rjq/parser.rb', line 38

def initialize(source, allow_comments: true, source_name: '<top-level>', initial_line: 1, initial_column: 1,
               start_offset: 0, max_filter_depth: DEFAULT_MAX_FILTER_DEPTH)
  unless max_filter_depth.is_a?(Integer) && max_filter_depth.positive?
    raise ArgumentError, 'max_filter_depth must be a positive Integer'
  end

  @tokens = if source.is_a?(Array)
              source
            else
              Lexer.new(source, allow_comments: allow_comments, source_name: source_name,
                                initial_line: initial_line, initial_column: initial_column,
                                start_offset: start_offset).tokenize
            end
  @index = 0
  @definitions = []
  @max_filter_depth = max_filter_depth
  @filter_depth = 0
end

Instance Method Details

#parseObject



57
58
59
60
61
62
63
# File 'lib/rjq/parser.rb', line 57

def parse
  directives = parse_directives
  parse_definitions
  body = eof? ? AST::Identity.new : parse_expression
  expect(:eof)
  AST::Program.new(body, @definitions, directives)
end