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
450 451 452 |
# File 'lib/smarter_json/parser.rb', line 450 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.
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/smarter_json/parser.rb', line 387 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
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/smarter_json/parser.rb', line 244 def each_document(io) 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
443 444 445 446 447 448 |
# File 'lib/smarter_json/parser.rb', line 443 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
454 455 456 457 458 459 |
# File 'lib/smarter_json/parser.rb', line 454 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
264 265 266 267 268 269 270 271 272 |
# File 'lib/smarter_json/parser.rb', line 264 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
274 275 276 277 278 279 280 281 282 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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/smarter_json/parser.rb', line 274 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
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
# File 'lib/smarter_json/parser.rb', line 405 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 |