class RbJSON5::Parser

@api private JSON5 parser implementation using [Parslet](github.com/kschiess/parslet)

Parsing process:

  1. parses a JSON5 string into the intermediate structure by using Parser.parser

  2. convert the intermediate structure into Ruby objects by using Parser.transform

Constants

IDENTIFIER_PART_PATTERNS

list of patterns for part character of identifier

IDENTIFIER_START_PATTERNS

list of patterns for start character of identifier

INVALID_CHARACTER_FOR_IDENTIFIER

call back block called when character unescaped from Unicode escape sequence is not allowed for identifier

ObjectMember

@api private structure to keep a name/value pair for JSON5Object

WHITE_SPACE_CODES

list of character codes of white space

Public Class Methods

new(root = nil) click to toggle source

@param root [Symbol]

specifies the root rule of the Parser for testing parpose
# File lib/rb_json5/parser.rb, line 46
def initialize(root = nil)
  @root = root
end
parser() click to toggle source

returns the class to parse a JSON5 string

# File lib/rb_json5/parser.rb, line 17
def parser
  @parser ||= Class.new(Parslet::Parser)
end
transform() click to toggle source

returns the class to convert the intermediate structure into Ruby objects

# File lib/rb_json5/parser.rb, line 22
def transform
  @transform ||= Class.new(Parslet::Transform)
end

Private Class Methods

parse_helper(helper_name, &body) click to toggle source

defines a helper method within Parser.parser

# File lib/rb_json5/parser.rb, line 34
def parse_helper(helper_name, &body)
  parser.class_eval { define_method(helper_name, &body) }
end
parse_rule(rule_name, &body) click to toggle source

defines a rule how to parse the given JSON5 string

# File lib/rb_json5/parser.rb, line 29
def parse_rule(rule_name, &body)
  parser.class_eval { rule(rule_name, &body) }
end
transform_rule(expression, &body) click to toggle source

defines a rule hot to convert tht intermediate strucure into Ruby objects

# File lib/rb_json5/parser.rb, line 39
def transform_rule(expression, &body)
  transform.class_eval { rule(expression, &body) }
end

Public Instance Methods

parse(string_or_io, symbolize_names = false) click to toggle source

parses a JSON5 string into the Ruby

@param (see RbJSON5.parse) @return (see RbJSON5.parse)

@see RbJSON5.parse @see RbJSON5.load_file

# File lib/rb_json5/parser.rb, line 57
def parse(string_or_io, symbolize_names = false)
  tree = parser.parse(read_json5(string_or_io), reporter: error_reporter)
  transform.apply(tree, symbolize_names: symbolize_names)
rescue Parslet::ParseFailed => e
  raise ParseError.new(e.message, e.parse_failure_cause)
end

Private Instance Methods

error_reporter() click to toggle source
# File lib/rb_json5/parser.rb, line 76
def error_reporter
  Parslet::ErrorReporter::Contextual.new
end
parser() click to toggle source
# File lib/rb_json5/parser.rb, line 66
def parser
  parser = self.class.parser.new
  @root && parser.__send__(@root) || parser
end
read_json5(string_or_io) click to toggle source
# File lib/rb_json5/parser.rb, line 71
def read_json5(string_or_io)
  string_or_io.respond_to?(:read) &&
    string_or_io.read || string_or_io
end
transform() click to toggle source
# File lib/rb_json5/parser.rb, line 80
def transform
  self.class.transform.new
end