Class: Solid::Parser::RubyParser

Inherits:
Solid::Parser show all
Defined in:
lib/solid/parser/ruby_parser.rb

Constant Summary

Constants inherited from Solid::Parser

BASE_PATH, KEYWORDS

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression) ⇒ RubyParser

Returns a new instance of RubyParser.



9
10
11
# File 'lib/solid/parser/ruby_parser.rb', line 9

def initialize(expression)
  @expression = expression
end

Class Method Details

.parse(string) ⇒ Object



5
6
7
# File 'lib/solid/parser/ruby_parser.rb', line 5

def self.parse(string)
  new(string).parse
end

Instance Method Details

#handle_and(left_expression, right_expression) ⇒ Object



80
81
82
# File 'lib/solid/parser/ruby_parser.rb', line 80

def handle_and(left_expression, right_expression)
  handle_call(left_expression, :'&&', right_expression)
end

#handle_array(*array_values) ⇒ Object



42
43
44
# File 'lib/solid/parser/ruby_parser.rb', line 42

def handle_array(*array_values)
  LiteralArray.new(array_values.map(&method(:parse_one)))
end

#handle_call(receiver, method_name, *arguments) ⇒ Object



74
75
76
77
78
# File 'lib/solid/parser/ruby_parser.rb', line 74

def handle_call(receiver, method_name, *arguments)
  return ContextVariable.new(method_name.to_s) if receiver.nil?

  MethodCall.new(parse_one(receiver), method_name.to_s, arguments.map(&method(:parse_one)))
end

#handle_const(const_name) ⇒ Object



70
71
72
# File 'lib/solid/parser/ruby_parser.rb', line 70

def handle_const(const_name)
  ContextVariable.new(const_name.to_s)
end

#handle_dot2(start_value, end_value) ⇒ Object



50
51
52
# File 'lib/solid/parser/ruby_parser.rb', line 50

def handle_dot2(start_value, end_value)
  LiteralRange.new(parse_one(start_value), parse_one(end_value), false)
end

#handle_dot3(start_value, end_value) ⇒ Object



54
55
56
# File 'lib/solid/parser/ruby_parser.rb', line 54

def handle_dot3(start_value, end_value)
  LiteralRange.new(parse_one(start_value), parse_one(end_value), true)
end

#handle_falseObject



62
63
64
# File 'lib/solid/parser/ruby_parser.rb', line 62

def handle_false
  KEYWORDS['false']
end

#handle_hash(*hash_keys_and_values) ⇒ Object



46
47
48
# File 'lib/solid/parser/ruby_parser.rb', line 46

def handle_hash(*hash_keys_and_values)
  LiteralHash.new(Hash[*hash_keys_and_values.map(&method(:parse_one))])
end

#handle_lit(literal) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/solid/parser/ruby_parser.rb', line 29

def handle_lit(literal)
  case literal
  when Range # see https://github.com/seattlerb/ruby_parser/issues/134
    LiteralRange.new(Literal.new(literal.first), Literal.new(literal.last), literal.exclude_end?)
  else
    Literal.new(literal)
  end
end

#handle_nilObject



66
67
68
# File 'lib/solid/parser/ruby_parser.rb', line 66

def handle_nil
  KEYWORDS['nil']
end

#handle_or(left_expression, right_expression) ⇒ Object



84
85
86
# File 'lib/solid/parser/ruby_parser.rb', line 84

def handle_or(left_expression, right_expression)
  handle_call(left_expression, :'||', right_expression)
end

#handle_str(literal) ⇒ Object



38
39
40
# File 'lib/solid/parser/ruby_parser.rb', line 38

def handle_str(literal)
  Literal.new(literal)
end

#handle_trueObject



58
59
60
# File 'lib/solid/parser/ruby_parser.rb', line 58

def handle_true
  KEYWORDS['true']
end

#parseObject



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

def parse
  begin
    @sexp = ::RubyParser.new.parse(@expression)
  rescue ::RubyParser::SyntaxError => exc
    raise Solid::SyntaxError.new(exc.message)
  end
  parse_one(@sexp)
end

#parse_one(expression) ⇒ Object

Raises:



22
23
24
25
26
27
# File 'lib/solid/parser/ruby_parser.rb', line 22

def parse_one(expression)
  type = expression.shift
  handler = "handle_#{type}"
  raise Solid::SyntaxError, "unknown expression type: #{type.inspect}" unless respond_to?(handler)
  public_send handler, *expression
end