class RbJSON5::Parser
@api private JSON5 parser implementation using [Parslet](github.com/kschiess/parslet)
Parsing process:
-
parses a JSON5 string into the intermediate structure by using
Parser.parser
-
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
@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
returns the class to parse a JSON5 string
# File lib/rb_json5/parser.rb, line 17 def parser @parser ||= Class.new(Parslet::Parser) end
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
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
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
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
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
# File lib/rb_json5/parser.rb, line 76 def error_reporter Parslet::ErrorReporter::Contextual.new end
# File lib/rb_json5/parser.rb, line 66 def parser parser = self.class.parser.new @root && parser.__send__(@root) || parser end
# 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
# File lib/rb_json5/parser.rb, line 80 def transform self.class.transform.new end