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

Class Method Details

.block_comment_start?(buffer, scan) ⇒ Boolean

Returns:

  • (Boolean)


415
416
417
# File 'lib/smarter_json/parser.rb', line 415

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.

Returns:

  • (Boolean)


352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/smarter_json/parser.rb', line 352

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

Yields:

  • (buffer)


209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/smarter_json/parser.rb', line 209

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

Returns:

  • (Boolean)


408
409
410
411
412
413
# File 'lib/smarter_json/parser.rb', line 408

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

Returns:

  • (Boolean)


419
420
421
422
423
424
# File 'lib/smarter_json/parser.rb', line 419

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



229
230
231
232
233
234
235
236
237
# File 'lib/smarter_json/parser.rb', line 229

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



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
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
# File 'lib/smarter_json/parser.rb', line 239

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

Returns:

  • (Boolean)


370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/smarter_json/parser.rb', line 370

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

.whitespace_byte?(b) ⇒ Boolean

Returns:

  • (Boolean)


404
405
406
# File 'lib/smarter_json/parser.rb', line 404

def whitespace_byte?(b)
  b == SPACE || (b && b >= TAB && b <= CR)
end