Class: Muskox::Pure::Lexer
- Inherits:
-
StringScanner
- Object
- StringScanner
- Muskox::Pure::Lexer
- Defined in:
- lib/muskox/json_lexer.rb
Constant Summary collapse
- STRING =
/" ((?:[^\x0-\x1f"\\] | # escaped special characters: \\["\\\/bfnrt] | \\u[0-9a-fA-F]{4} | # match all but escaped special characters: \\[\x20-\x21\x23-\x2e\x30-\x5b\x5d-\x61\x63-\x65\x67-\x6d\x6f-\x71\x73\x75-\xff])*) "/nx
- INTEGER =
/(-?0|-?[1-9]\d*)/
- FLOAT =
/(-? (?:0|[1-9]\d*) (?: \.\d+(?i:e[+-]?\d+) | \.\d+ | (?i:e[+-]?\d+) ) )/x
- NAN =
/NaN/
- INFINITY =
/Infinity/
- MINUS_INFINITY =
/-Infinity/
- OBJECT_OPEN =
/\{/
- OBJECT_CLOSE =
/\}/
- ARRAY_OPEN =
/\[/
- ARRAY_CLOSE =
/\]/
- PAIR_DELIMITER =
/:/
- COLLECTION_DELIMITER =
/,/
- TRUE =
/true/
- FALSE =
/false/
- NULL =
/null/
- IGNORE =
%r( (?: //[^\n\r]*[\n\r]| # line comments /\* # c-style comments (?: [^*/]| # normal chars /[^*]| # slashes that do not start a nested comment \*[^/]| # asterisks that do not end this comment /(?=\*/) # single slash before this comment's end )* \*/ # the End of this comment |[ \t\r\n]+ # whitespaces: space, horicontal tab, lf, cr )+ )mx
- UNPARSED =
Object.new
Instance Method Summary collapse
-
#initialize(source, opts = {}) ⇒ Lexer
constructor
Creates a new JSON::Pure::Parser instance for the string source.
-
#lex(&block) ⇒ Object
Parses the current JSON string source and returns the complete data structure as a result.
- #quirks_mode? ⇒ Boolean
- #reset ⇒ Object
Constructor Details
#initialize(source, opts = {}) ⇒ Lexer
Creates a new JSON::Pure::Parser instance for the string source.
It will be configured by the opts hash. opts can have the following keys:
-
max_nesting: The maximum depth of nesting allowed in the parsed data structures. Disable depth checking with :max_nesting => false|nil|0, it defaults to 100.
-
allow_nan: If set to true, allow NaN, Infinity and -Infinity in defiance of RFC 4627 to be parsed by the Parser. This option defaults to false.
-
symbolize_names: If set to true, returns symbols for the names (keys) in a JSON object. Otherwise strings are returned, which is also the default.
-
quirks_mode: Enables quirks_mode for parser, that is for example parsing single JSON values instead of documents is possible.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/muskox/json_lexer.rb', line 69 def initialize(source, opts = {}) opts ||= {} unless @quirks_mode = opts[:quirks_mode] source = convert_encoding source end super source if !opts.key?(:max_nesting) # defaults to 100 @max_nesting = 100 elsif opts[:max_nesting] @max_nesting = opts[:max_nesting] else @max_nesting = 0 end @allow_nan = !!opts[:allow_nan] @symbolize_names = !!opts[:symbolize_names] @match_string = opts[:match_string] end |
Instance Method Details
#lex(&block) ⇒ Object
Parses the current JSON string source and returns the complete data structure as a result.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/muskox/json_lexer.rb', line 100 def lex &block @callback = block reset if @quirks_mode while !eos? && skip(IGNORE) end if eos? raise ParserError, "source did not contain any JSON!" else obj = lex_value obj == UNPARSED and raise ParserError, "source did not contain any JSON!" end else until eos? case when scan(OBJECT_OPEN) # obj and raise ParserError, "source '#{peek(20)}' not in JSON!" @current_nesting = 1 lex_object when scan(ARRAY_OPEN) # obj and raise ParserError, "source '#{peek(20)}' not in JSON!" @current_nesting = 1 lex_array when skip(IGNORE) ; else raise ParserError, "source '#{peek(20)}' not in JSON!" end end # obj or raise ParserError, "source did not contain any JSON!" end # obj end |
#quirks_mode? ⇒ Boolean
89 90 91 |
# File 'lib/muskox/json_lexer.rb', line 89 def quirks_mode? !!@quirks_mode end |
#reset ⇒ Object
93 94 95 96 |
# File 'lib/muskox/json_lexer.rb', line 93 def reset super @current_nesting = 0 end |