Class: JSE::Parser

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Parser

Returns a new instance of Parser.



12
13
14
# File 'lib/jse/ast/parser.rb', line 12

def initialize(env)
  @env = env
end

Class Method Details

.symbol?(s) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
6
# File 'lib/jse/ast/parser.rb', line 3

def self.symbol?(s)
  return false if s == "$*"
  s.is_a?(String) && s.start_with?("$") && !s.start_with?("$$")
end

.unescape(s) ⇒ Object



8
9
10
# File 'lib/jse/ast/parser.rb', line 8

def self.unescape(s)
  s.is_a?(String) && s.start_with?("$$") ? s[1..] : s
end

Instance Method Details

#parse(expr) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jse/ast/parser.rb', line 16

def parse(expr)
  case expr
  when nil, true, false, Integer, Float
    Ast::LiteralNode.new(expr, @env)
  when String
    if self.class.symbol?(expr)
      Ast::SymbolNode.new(expr, @env)
    else
      Ast::LiteralNode.new(self.class.unescape(expr), @env)
    end
  when Array
    parse_list(expr)
  when Hash
    parse_dict(expr)
  else
    Ast::LiteralNode.new(expr, @env)
  end
end