class Hjson::AST::Parser

Attributes

buffer[R]
options[R]
payload[R]
source[R]

Public Class Methods

declare(var, val = nil, &block) click to toggle source
# File lib/hjson/ast/parser.rb, line 26
def self.declare(var, val = nil, &block)
  declared_vars[var] = block_given? ? block : val
end
declared_vars() click to toggle source
# File lib/hjson/ast/parser.rb, line 22
def self.declared_vars
  @declared_vars ||= {}
end
new(buffer, **options) click to toggle source
# File lib/hjson/ast/parser.rb, line 41
def initialize(buffer, **options)
  @buffer  = buffer
  @payload = nil
  @options = options
  assign_declared_vars!
end
parser(&parser) click to toggle source
# File lib/hjson/ast/parser.rb, line 18
def self.parser(&parser)
  parsers << parser
end
parsers() click to toggle source
# File lib/hjson/ast/parser.rb, line 14
def self.parsers
  @parsers ||= []
end
rule(name, &block) click to toggle source
# 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

Public Instance Methods

parse() click to toggle source
# File lib/hjson/ast/parser.rb, line 37
def parse
  catch(:halt) { parsers.reduce(nil) { |_, parser| instance_eval(&parser) } }
end

Private Instance Methods

assign_declared_vars!() click to toggle source
# File lib/hjson/ast/parser.rb, line 50
def assign_declared_vars!
  self.class.declared_vars.each do |var, val|
    val =
      case val
      when Proc   then instance_eval(&val)
      when Symbol then send(val)
      when Integer then val
      else val.dup
      end
    instance_variable_set(:"@#{var}", val)
  end
end
char()
Alias for: current_char
char?() click to toggle source
# File lib/hjson/ast/parser.rb, line 97
def char?
  !!char
end
current_char() click to toggle source
# File lib/hjson/ast/parser.rb, line 92
def current_char
  string[charpos]
end
Also aliased as: char
halt(data = nil) click to toggle source
# File lib/hjson/ast/parser.rb, line 69
def halt(data = nil)
  throw :halt, data
end
parsers() click to toggle source
# File lib/hjson/ast/parser.rb, line 73
def parsers
  self.class.parsers
end
peek(offset = 0) click to toggle source
# File lib/hjson/ast/parser.rb, line 88
def peek(offset = 0)
  string[charpos + offset]
end
read_while(**options, &block) click to toggle source
# File lib/hjson/ast/parser.rb, line 77
def read_while(**options, &block)
  if options[:if] && respond_to?(options[:if], true)
    while char && send(options[:if])
      instance_eval(&block)
      read
    end
  else
    read while char && instance_exec(char, &block)
  end
end
validate(expected) click to toggle source
# File lib/hjson/ast/parser.rb, line 63
def validate(expected)
  current = current_char
  fail SyntaxError,
    "Expected %p instead of %p" % [expected, current] if expected && expected != current
end