Module: Kwartz::PhpExpressionParser

Included in:
PhpHandler, PhpTranslator
Defined in:
lib/kwartz/binding/php.rb

Instance Method Summary collapse

Instance Method Details

#parse_expr_str(expr_str, linenum) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kwartz/binding/php.rb', line 20

def parse_expr_str(expr_str, linenum)
  case expr_str
  when /\A(\w+)\z/             # variable
    expr = '$' + $1
  when /\A(\w+)\.(\w+)\z/      # object.property
    expr = "$#{$1}->#{$2}"
  when /\A(\w+)\[('.*?'|".*?"|:\w+)\]\z/   # hash
    key = $2[0] == ?: ? "'#{$2[1..-1]}'" : $2
    expr = "$#{$1}[#{key}]"
  when /\A(\w+)\[(\w+)\]\z/    # array or hash
    begin
      expr = "$#{$1}[#{Integer($2)}]"
    rescue
      expr = "$#{$1}[$#{$2}]"
    end
  else
    raise convert_error("'#{expr_str}': invalid expression.", linenum)
  end
  return expr
end

#parse_expr_str!(expr_str) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/kwartz/binding/php.rb', line 42

def parse_expr_str!(expr_str)
  begin
    return parse_expr_str(expr_str, 0)
  rescue
    return expr_str
  end
end