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)


396
397
398
# File 'lib/smarter_json/parser.rb', line 396

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)


333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/smarter_json/parser.rb', line 333

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)


190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/smarter_json/parser.rb', line 190

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)


389
390
391
392
393
394
# File 'lib/smarter_json/parser.rb', line 389

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)


400
401
402
403
404
405
# File 'lib/smarter_json/parser.rb', line 400

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



210
211
212
213
214
215
216
217
218
# File 'lib/smarter_json/parser.rb', line 210

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



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
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
# File 'lib/smarter_json/parser.rb', line 220

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)


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
382
383
# File 'lib/smarter_json/parser.rb', line 351

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)


385
386
387
# File 'lib/smarter_json/parser.rb', line 385

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