Class: Hjson::AST::Parser

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hjson/ast/parser.rb

Direct Known Subclasses

NumberParser

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer, **options) ⇒ Parser

Returns a new instance of Parser.



41
42
43
44
45
46
# File 'lib/hjson/ast/parser.rb', line 41

def initialize(buffer, **options)
  @buffer  = buffer
  @payload = nil
  @options = options
  assign_declared_vars!
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



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

def buffer
  @buffer
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#payloadObject (readonly)

Returns the value of attribute payload.



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

def payload
  @payload
end

#sourceObject (readonly)

Returns the value of attribute source.



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

def source
  @source
end

Class Method Details

.declare(var, val = nil, &block) ⇒ Object



26
27
28
# File 'lib/hjson/ast/parser.rb', line 26

def self.declare(var, val = nil, &block)
  declared_vars[var] = block_given? ? block : val
end

.declared_varsObject



22
23
24
# File 'lib/hjson/ast/parser.rb', line 22

def self.declared_vars
  @declared_vars ||= {}
end

.parser(&parser) ⇒ Object



18
19
20
# File 'lib/hjson/ast/parser.rb', line 18

def self.parser(&parser)
  parsers << parser
end

.parsersObject



14
15
16
# File 'lib/hjson/ast/parser.rb', line 14

def self.parsers
  @parsers ||= []
end

.rule(name, &block) ⇒ Object



30
31
32
33
34
35
# File 'lib/hjson/ast/parser.rb', line 30

def self.rule(name, &block)
  define_method(:"#{name}?") do |c = nil|
    c = char unless c
    instance_exec(c, &block)
  end
end

Instance Method Details

#parseObject



37
38
39
# File 'lib/hjson/ast/parser.rb', line 37

def parse
  catch(:halt) { parsers.reduce(nil) { |_, parser| instance_eval(&parser) } }
end