Module: SmarterJSON::Framer
- Includes:
- Bytes
- Defined in:
- lib/smarter_json/parser.rb
Constant Summary collapse
- CHUNK_SIZE =
16 * 1024
Constants included from Bytes
Bytes::BACKSLASH, Bytes::COLON, Bytes::COMMA, Bytes::CR, Bytes::DOLLAR, Bytes::DOT, Bytes::DQUOTE, Bytes::HASH, Bytes::LBRACE, Bytes::LBRACKET, Bytes::LF, Bytes::LOWER_E, Bytes::LOWER_F, Bytes::LOWER_N, Bytes::LOWER_T, Bytes::LOWER_U, Bytes::LOWER_X, Bytes::MINUS, Bytes::NINE, Bytes::PLUS, Bytes::RBRACE, Bytes::RBRACKET, Bytes::SLASH, Bytes::SPACE, Bytes::SQUOTE, Bytes::STAR, Bytes::TAB, Bytes::UNDERSCORE, Bytes::UPPER_E, Bytes::UPPER_F, Bytes::UPPER_I, Bytes::UPPER_N, Bytes::UPPER_T, Bytes::UPPER_X, Bytes::ZERO
Class Method Summary collapse
- .block_comment_start?(buffer, scan) ⇒ Boolean
-
.defer_for_split_marker?(buffer, scan, b, mode, doc_start) ⇒ Boolean
True when ‘b` is the lead byte of a multi-byte marker but the rest of that marker has not been read into the buffer yet, so we cannot decide what it is.
- .each_document(io) {|buffer| ... } ⇒ Object
- .line_comment_start?(buffer, scan) ⇒ Boolean
- .preceded_by_ws_or_start?(buffer, scan) ⇒ Boolean
- .read_chunk(io) ⇒ Object
- .scan_buffer(buffer, scan, doc_start, stack, mode) ⇒ Object
- .separators_only?(buffer) ⇒ Boolean
- .whitespace_byte?(b) ⇒ Boolean
Class Method Details
.block_comment_start?(buffer, scan) ⇒ Boolean
328 329 330 |
# File 'lib/smarter_json/parser.rb', line 328 def block_comment_start?(buffer, scan) buffer.getbyte(scan) == SLASH && buffer.getbyte(scan + 1) == STAR && preceded_by_ws_or_start?(buffer, scan) end |
.defer_for_split_marker?(buffer, scan, b, mode, doc_start) ⇒ Boolean
True when ‘b` is the lead byte of a multi-byte marker but the rest of that marker has not been read into the buffer yet, so we cannot decide what it is. `//` and `/*` need 2 bytes; `”’‘ (and a closing `”’‘) needs 3; a closing `*/` needs 2. Backslash escapes and single-byte delimiters never need this.
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/smarter_json/parser.rb', line 265 def defer_for_split_marker?(buffer, scan, b, mode, doc_start) avail = buffer.bytesize - scan case mode when :block_comment b == STAR && avail < 2 when :triple b == SQUOTE && avail < 3 when nil if doc_start.nil? b == SLASH && avail < 2 else (b == SLASH && avail < 2) || (b == SQUOTE && avail < 3) end else false end end |
.each_document(io) {|buffer| ... } ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/smarter_json/parser.rb', line 122 def each_document(io, &block) buffer = +"" scan = 0 doc_start = nil stack = [] mode = nil while (chunk = read_chunk(io)) buffer << chunk loop do emitted, buffer, scan, doc_start, stack, mode = scan_buffer(buffer, scan, doc_start, stack, mode) break unless emitted yield emitted end end yield buffer unless separators_only?(buffer) end |
.line_comment_start?(buffer, scan) ⇒ Boolean
321 322 323 324 325 326 |
# File 'lib/smarter_json/parser.rb', line 321 def line_comment_start?(buffer, scan) b = buffer.getbyte(scan) return preceded_by_ws_or_start?(buffer, scan) if b == HASH b == SLASH && buffer.getbyte(scan + 1) == SLASH && preceded_by_ws_or_start?(buffer, scan) end |
.preceded_by_ws_or_start?(buffer, scan) ⇒ Boolean
332 333 334 335 336 337 |
# File 'lib/smarter_json/parser.rb', line 332 def preceded_by_ws_or_start?(buffer, scan) return true if scan.zero? prev = buffer.getbyte(scan - 1) whitespace_byte?(prev) end |
.read_chunk(io) ⇒ Object
142 143 144 145 146 147 148 149 150 |
# File 'lib/smarter_json/parser.rb', line 142 def read_chunk(io) if io.respond_to?(:readpartial) io.readpartial(CHUNK_SIZE) else io.read(CHUNK_SIZE) end rescue EOFError nil end |
.scan_buffer(buffer, scan, doc_start, stack, mode) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/smarter_json/parser.rb', line 152 def scan_buffer(buffer, scan, doc_start, stack, mode) while scan < buffer.bytesize b = buffer.getbyte(scan) # A multi-byte marker (// /* ''' */) whose lead byte is here but whose # remaining bytes have not arrived yet must not be guessed at — advancing # past the lead byte would misread the brace/quote that follows it once the # next chunk lands. Stop and let each_document append more input, then resume # from this same position. At true EOF the leftover is parsed whole instead. break if defer_for_split_marker?(buffer, scan, b, mode, doc_start) if mode == :double if b == BACKSLASH scan += 2 elsif b == DQUOTE mode = nil scan += 1 else scan += 1 end elsif mode == :single if b == BACKSLASH scan += 2 elsif b == SQUOTE mode = nil scan += 1 else scan += 1 end elsif mode == :triple if buffer.byteslice(scan, 3) == "'''" mode = nil scan += 3 else scan += 1 end elsif mode == :line_comment if [LF, CR].include?(b) mode = nil else scan += 1 next end elsif mode == :block_comment if buffer.byteslice(scan, 2) == '*/' mode = nil scan += 2 else scan += 1 end elsif doc_start.nil? if whitespace_byte?(b) scan += 1 elsif line_comment_start?(buffer, scan) mode = :line_comment scan += buffer.getbyte(scan) == HASH ? 1 : 2 elsif block_comment_start?(buffer, scan) mode = :block_comment scan += 2 elsif [LBRACE, LBRACKET].include?(b) doc_start = scan stack << b scan += 1 else scan = buffer.bytesize end else if mode.nil? && line_comment_start?(buffer, scan) mode = :line_comment scan += buffer.getbyte(scan) == HASH ? 1 : 2 elsif mode.nil? && block_comment_start?(buffer, scan) mode = :block_comment scan += 2 elsif b == DQUOTE mode = :double scan += 1 elsif buffer.byteslice(scan, 3) == "'''" mode = :triple scan += 3 elsif b == SQUOTE mode = :single scan += 1 elsif [LBRACE, LBRACKET].include?(b) stack << b scan += 1 elsif b == RBRACE stack.pop if stack.last == LBRACE scan += 1 if stack.empty? doc = buffer.byteslice(doc_start, scan - doc_start) buffer = buffer.byteslice(scan..-1) || +"" return [doc, buffer, 0, nil, [], nil] end elsif b == RBRACKET stack.pop if stack.last == LBRACKET scan += 1 if stack.empty? doc = buffer.byteslice(doc_start, scan - doc_start) buffer = buffer.byteslice(scan..-1) || +"" return [doc, buffer, 0, nil, [], nil] end else scan += 1 end end end [nil, buffer, scan, doc_start, stack, mode] end |
.separators_only?(buffer) ⇒ Boolean
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/smarter_json/parser.rb', line 283 def separators_only?(buffer) scan = 0 mode = nil while scan < buffer.bytesize b = buffer.getbyte(scan) if mode == :line_comment if [LF, CR].include?(b) mode = nil else scan += 1 next end elsif mode == :block_comment if buffer.byteslice(scan, 2) == '*/' mode = nil scan += 2 else scan += 1 end elsif whitespace_byte?(b) scan += 1 elsif line_comment_start?(buffer, scan) mode = :line_comment scan += buffer.getbyte(scan) == HASH ? 1 : 2 elsif block_comment_start?(buffer, scan) mode = :block_comment scan += 2 else return false end end true end |