Class: RbJSON5::Parser Private

Inherits:
Object
  • Object
show all
Extended by:
Parslet
Defined in:
lib/rb_json5/parser.rb,
lib/rb_json5/parser/misc.rb,
lib/rb_json5/parser/null.rb,
lib/rb_json5/parser/array.rb,
lib/rb_json5/parser/space.rb,
lib/rb_json5/parser/number.rb,
lib/rb_json5/parser/object.rb,
lib/rb_json5/parser/string.rb,
lib/rb_json5/parser/boolean.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

JSON5 parser implementation using 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

Defined Under Namespace

Classes: ObjectMember

Constant Summary collapse

WHITE_SPACE_CODES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

list of character codes of white space

[
  "\u0009", "\u000b", "\u000c", "\u0020", "\u00a0", "\ufeff"
].freeze
IDENTIFIER_START_PATTERNS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

list of patterns for start character of identifier

[
  /[$_]/, /\p{Letter}/, /\p{Letter_Number}/
].freeze
IDENTIFIER_PART_PATTERNS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

list of patterns for part character of identifier

[
  /[\u200c\u200d]/,
  /\p{Nonspacing_Mark}/, /\p{Spacing_Mark}/,
  /\p{Decimal_Number}/, /\p{Connector_Punctuation}/,
  *IDENTIFIER_START_PATTERNS
].freeze
INVALID_CHARACTER_FOR_IDENTIFIER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

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

lambda do |character, sequence|
  Parslet::Cause.format(
    sequence.line_cache, sequence.position.bytepos,
    "#{character.inspect} cannot be used for identifier"
  ).raise
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root = nil) ⇒ Parser

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Parser.

Parameters:

  • root (Symbol) (defaults to: nil)

    specifies the root rule of the Parser for testing parpose



46
47
48
# File 'lib/rb_json5/parser.rb', line 46

def initialize(root = nil)
  @root = root
end

Class Method Details

.parserObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

returns the class to parse a JSON5 string



17
18
19
# File 'lib/rb_json5/parser.rb', line 17

def parser
  @parser ||= Class.new(Parslet::Parser)
end

.transformObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

returns the class to convert the intermediate structure into Ruby objects



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

def transform
  @transform ||= Class.new(Parslet::Transform)
end

Instance Method Details

#parse(string_or_io, symbolize_names = false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

parses a JSON5 string into the Ruby

Parameters:

  • string_or_io (String, #read)

    JSON5 string itself or object like IO containing JSON5 string

  • symbolize_names (Boolean) (defaults to: false)

    If set to true, converts names (keys) in a JSON5 object into Symbol

Returns:

  • (Object)

    Ruby data structure represented by the input

See Also:



57
58
59
60
61
62
# 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