Class: Kwalify::Yaml::Parser

Inherits:
BaseParser show all
Defined in:
lib/kwalify/parser/yaml.rb

Overview

YAML parser with validator

ex.

schema = YAML.load_file('schema.yaml')
require 'kwalify'
validator = Kwalify::Validator.new(schema)
parser = Kwalify::Yaml::Parser.new(validator)  # validator is optional
#parser.preceding_alias = true  # optional
#parser.data_binding = true     # optional
ydoc = parser.parse_file('data.yaml')
errors = parser.errors
if errors && !errors.empty?
  errors.each do |e|
    puts "line=#{e.linenum}, path=#{e.path}, mesg=#{e.message}"
  end
end

Constant Summary collapse

MAPKEY_PATTERN =

:nodoc:

/([\w.][-\w.:]*\*?|".*?"|'.*?'|:\w+|=|<<)[ \t]*:\s+/
PRECEDING_ALIAS_PLACEHOLDER =

:nodoc:

Object.new

Constants inherited from BaseParser

BaseParser::CHAR_TABLE

Instance Attribute Summary collapse

Attributes inherited from BaseParser

#column, #filename, #linenum

Instance Method Summary collapse

Methods inherited from BaseParser

#_getch, #_set_column_and_linenum, #eos?, #group, #match?, #peep, #reset, #scan, #scan_string

Constructor Details

#initialize(validator = nil, properties = {}) ⇒ Parser

Returns a new instance of Parser.



46
47
48
49
50
51
52
# File 'lib/kwalify/parser/yaml.rb', line 46

def initialize(validator=nil, properties={})
  @validator = validator.is_a?(Hash) ? Kwalify::Validator.new(validator) : validator
  @data_binding    = properties[:data_binding]    # enable data binding or not
  @preceding_alias = properties[:preceding_alias] # allow preceding alias or not
  @sequence_class  = properties[:sequence_class] || Array
  @mapping_class   = properties[:mapping_class]  || Hash
end

Instance Attribute Details

#data_bindingObject

boolean



54
55
56
# File 'lib/kwalify/parser/yaml.rb', line 54

def data_binding
  @data_binding
end

#errorsObject (readonly)

Returns the value of attribute errors.



68
69
70
# File 'lib/kwalify/parser/yaml.rb', line 68

def errors
  @errors
end

#mapping_classObject

Class



57
58
59
# File 'lib/kwalify/parser/yaml.rb', line 57

def mapping_class
  @mapping_class
end

#preceding_aliasObject

boolean



55
56
57
# File 'lib/kwalify/parser/yaml.rb', line 55

def preceding_alias
  @preceding_alias
end

#sequence_classObject

Class



56
57
58
# File 'lib/kwalify/parser/yaml.rb', line 56

def sequence_class
  @sequence_class
end

#validatorObject

Validator



53
54
55
# File 'lib/kwalify/parser/yaml.rb', line 53

def validator
  @validator
end

Instance Method Details

#_parse_map_value(map, map_rule, path, level, key, is_merged, uniq_table, _linenum, _column, _linenum2, _column2) ⇒ Object



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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/kwalify/parser/yaml.rb', line 335

def _parse_map_value(map, map_rule, path, level, key, is_merged, uniq_table,
                     _linenum, _column, _linenum2, _column2)  #:nodoc:
  key = to_mapkey(key)
  path[-1] = key
  #if map.is_a?(Hash) && map.key?(key) && !is_merged
  if map.respond_to?('key?') && map.key?(key) && !is_merged
    rule = map_rule.mapping[key]
    unless rule && rule.default
      raise _syntax_error("mapping key is duplicated.", path)
    end
  end
  #
  if key == '='      # default
    val = level ? parse_block_value(level, nil, path, uniq_table, map) \
                : parse_flow_value(nil, path, uniq_table, map)
    map.default = val
  elsif key == '<<'  # merge
    classobj = nil
    if map_rule && map_rule.classname
      map_rule = map_rule.dup()
      classobj = map_rule.classobj
      map_rule.classname = nil
      map_rule.classobj = nil
    end
    val = level ? parse_block_value(level, map_rule, path, uniq_table, map) \
                : parse_flow_value(map_rule, path, uniq_table, map)
    if val.is_a?(Array)
      val.each_with_index do |v, i|
        unless v.is_a?(Hash) || (classobj && val.is_a?(classobj))
          raise _syntax_error("'<<': mapping required.", path + [i])
        end
      end
      values = val
    elsif val.is_a?(Hash) || (classobj && val.is_a?(classobj))
      values = [val]
    else
      raise _syntax_error("'<<': mapping (or sequence of mapping) required.", path)
    end
    #
    values.each do |hash|
      if !hash.is_a?(Hash)
        assert_error "hash=#{hash.inspect}" unless classobj && hash.is_a?(classobj)
        obj = hash
        hash = {}
        obj.instance_variables.each do |name|
          key = name[1..-1]  # '@foo' => 'foo'
          val = obj.instane_variable_get(name)
          hash[key] = val
        end
      end
      for key, val in hash
        path[-1] = key                                                   #*V
        rule = map_rule ? map_rule.mapping[key] : nil                    #*V
        utable = uniq_table ? uniq_table[key] : nil                      #*V
        _validate_map_value(map, map_rule, rule, path, utable,           #*V
                         key, val, _linenum, _column)                    #*V
        put_to_map(rule, map, key, val, _linenum2, _column2)
      end
    end
    is_merged = true
  else               # other
    rule = map_rule ? map_rule.mapping[key] : nil                        #*V
    utable = uniq_table ? uniq_table[key] : nil                          #*V
    val = level ? parse_block_value(level, rule, path, utable, map) \
                : parse_flow_value(rule, path, utable, map)
    _validate_map_value(map, map_rule, rule, path, utable, key, val,     #*V
                     _linenum, _column)                                  #*V
    put_to_map(rule, map, key, val, _linenum2, _column2)
  end
  return is_merged
end

#_set_error_info(linenum = @linenum, column = @column, &block) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/kwalify/parser/yaml.rb', line 90

def _set_error_info(linenum=@linenum, column=@column, &block)
  len = @errors.length
  yield
  n = @errors.length - len
  (1..n).each do |i|
    error = @errors[-i]
    error.linenum  ||= linenum
    error.column   ||= column
    error.filename ||= @filename
  end if n > 0
end

#_validate_map_value(map, map_rule, rule, path, uniq_table, key, val, _linenum, _column) ⇒ Object

*V



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/kwalify/parser/yaml.rb', line 408

def _validate_map_value(map, map_rule, rule, path, uniq_table, key, val, #*V
                     _linenum, _column)                                  #*V
  if map_rule && !rule                                                   #*V
    #_validate_error("unknown mapping key.", path)                       #*V
    _set_error_info(_linenum, _column) do                                #*V
      error = Kwalify::ErrorHelper.validate_error(:key_undefined,        #*V
                                    rule, path, map, ["#{key}:"])        #*V
      @errors << error                                                   #*V
      #error.linenum = _linenum                                          #*V
      #error.column  = _column                                           #*V
    end                                                                  #*V
  end                                                                    #*V
  _set_error_info(_linenum, _column) do                                  #*V
    @validator._validate(val, rule, path, @errors, @done, uniq_table, false)  #*V
  end if rule && !val.equal?(PRECEDING_ALIAS_PLACEHOLDER)                #*V
end

#document_start?Boolean

Returns:



112
113
114
# File 'lib/kwalify/parser/yaml.rb', line 112

def document_start?()
  return match?(/---\s/) && @column == 1
end

#has_next?Boolean

Returns:



122
123
124
# File 'lib/kwalify/parser/yaml.rb', line 122

def has_next?()
  return !(eos? || stream_end?)
end

#location(path) ⇒ Object



795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
# File 'lib/kwalify/parser/yaml.rb', line 795

def location(path)
  if path.empty? || path == '/'
    return @location_table[-1]    # return value is [linenum, column]
  end
  if path.is_a?(Array)
    items = path.collect { |item| to_scalar(item) }
  elsif path.is_a?(String)
    items = path.split('/').collect { |item| to_scalar(item) }
    items.shift if path[0] == ?/  # delete empty string on head
  else
    raise ArgumentError.new("path should be Array or String.")
  end
  last_item = items.pop()
  c = @doc                        # collection
  items.each do |item|
    if c.is_a?(Array)
      c = c[item.to_i]
    elsif c.is_a?(Hash)
      c = c[item]
    elsif (table = @location_table[c.__id__]) && table[:classobj]
      if c.respond_to?(item)
        c = c.__send__(item)
      elsif c.respond_to?("[]=")
        c = c[item]
      else
        assert false
      end
    else
      #assert false
      raise ArgumentError.new("#{path.inspect}: invalid path.")
    end
  end
  collection = @location_table[c.__id__]
  return nil if collection.nil?
  index = c.is_a?(Array) ? last_item.to_i : last_item
  return collection[index]  # return value is [linenum, column]
end

#parse(input = nil, opts = {}) ⇒ Object



127
128
129
130
# File 'lib/kwalify/parser/yaml.rb', line 127

def parse(input=nil, opts={})
  reset_scanner(input, opts[:filename], opts[:untabify]) if input
  return parse_next()
end

#parse_alias(rule, path, uniq_table, container) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/kwalify/parser/yaml.rb', line 198

def parse_alias(rule, path, uniq_table, container)
  name = group(1)
  if @anchors.key?(name)
    val = @anchors[name]
  elsif @preceding_alias
    @preceding_aliases << [name, rule, path.dup, container,
                           @linenum, @column - name.length - 1]
    val = PRECEDING_ALIAS_PLACEHOLDER
  else
    raise _syntax_error("*#{name}: anchor not found.", path,
                        @linenum, @column - name.length - 1)
  end
  skip_spaces_and_comments()
  return val
end

#parse_anchor(rule, path, uniq_table, container) ⇒ Object



187
188
189
190
191
192
193
194
195
# File 'lib/kwalify/parser/yaml.rb', line 187

def parse_anchor(rule, path, uniq_table, container)
  name = group(1)
  if @anchors.key?(name)
    raise _syntax_error("&#{name}: anchor duplicated.", path,
                        @linenum, @column - name.length)
  end
  skip_spaces_and_comments()
  return name
end

#parse_block_map(map, map_rule, path, uniq_table) ⇒ Object



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/kwalify/parser/yaml.rb', line 426

def parse_block_map(map, map_rule, path, uniq_table)
  _start_linenum = @linenum                                              #*V
  _start_column  = @column                                               #*V
  level = @column
  path.push(nil)
  is_merged = false
  while true
    _linenum = @linenum                                                  #*V
    _column  = @column                                                   #*V
    break unless level == @column && scan(MAPKEY_PATTERN)
    key = group(1)
    skip_spaces_and_comments()                                           #*V
    _linenum2 = @linenum                                                 #*V
    _column2  = @column                                                  #*V
    is_merged = _parse_map_value(map, map_rule, path, level, key, is_merged,
                                 uniq_table, _linenum, _column, _linenum2, _column2)
    #skip_spaces_and_comments()
  end
  path.pop()
  _set_error_info(_start_linenum, _start_column) do                      #*V
    @validator._validate_mapping_required_keys(map, map_rule,            #*V
                                               path, @errors)            #*V
  end if map_rule                                                        #*V
  return map
end

#parse_block_scalar(rule, path, uniq_table) ⇒ Object



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/kwalify/parser/yaml.rb', line 463

def parse_block_scalar(rule, path, uniq_table)
  _linenum = @linenum                                                    #*V
  _column  = @column                                                     #*V
  ch = peep(1)
  if ch == '"' || ch == "'"
    val = scan_string()
    scan(/[ \t]*(?:\#.*)?$/)
  else
    scan(/(.*?)[ \t]*(?:\#.*)?$/)
    #str.rstrip!
    val = to_scalar(group(1))
  end
  val = create_scalar(rule, val, _linenum, _column)                      #*V
  #_set_error_info(_linenum, _column) do                                 #*V
  #  @validator._validate_unique(val, rule, path, @errors, uniq_table)   #*V
  #end if uniq_table                                                     #*V
  skip_spaces_and_comments()
  return val
end

#parse_block_seq(seq, seq_rule, path, uniq_table) ⇒ Object



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
# File 'lib/kwalify/parser/yaml.rb', line 307

def parse_block_seq(seq, seq_rule, path, uniq_table)
  level = @column
  rule = seq_rule ? seq_rule.sequence[0] : nil
  path.push(nil)
  i = 0
  _linenum = @linenum                                                    #*V
  _column  = @column                                                     #*V
  uniq_table = rule ? rule._uniqueness_check_table() : nil               #*V
  while level == @column && scan(/-\s+/)
    path[-1] = i
    skip_spaces_and_comments()                                           #*V
    _linenum2 = @linenum
    _column2  = @column
    val = parse_block_value(level, rule, path, uniq_table, seq)
    add_to_seq(rule, seq, val, _linenum2, _column2)    # seq << val
    _set_error_info(_linenum, _column) do                                #*V
      @validator._validate(val, rule, path, @errors, @done, uniq_table, false) #*V
    end if rule && !val.equal?(PRECEDING_ALIAS_PLACEHOLDER)              #*V
    skip_spaces_and_comments()
    i += 1
    _linenum = @linenum                                                  #*V
    _column  = @column                                                   #*V
  end
  path.pop()
  return seq
end

#parse_block_text(column, rule, path, uniq_table) ⇒ Object



484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
# File 'lib/kwalify/parser/yaml.rb', line 484

def parse_block_text(column, rule, path, uniq_table)
  _linenum = @linenum                                                    #*V
  _column  = @column                                                     #*V
  indicator = scan(/[|>]/)
  chomping = scan(/[-+]/)
  num = scan(/\d+/)
  indent = num ? column + num.to_i - 1 : nil
  unless scan(/[ \t]*(.*?)(\#.*)?\r?\n/)   # /[ \t]*(\#.*)?\r?\n/
    raise _syntax_error("Syntax Error (line break or comment are expected)", path)
  end
  s = group(1)
  is_folded = false
  while match?(/( *)(.*?)(\r?\n)/)
    spaces = group(1)
    text   = group(2)
    nl     = group(3)
    if indent.nil?
      if spaces.length >= column
        indent = spaces.length
      elsif text.empty?
        s << nl
        scan(/.*?\n/)
        next
      else
        @diagnostic = 'text indent in block text may be shorter than that of first line or specified column.'
        break
      end
    else
      if spaces.length < indent && !text.empty?
        @diagnostic = 'text indent in block text may be shorter than that of first line or specified column.'
        break
      end
    end
    scan(/.*?\n/)
    if indicator == '|'
      s << spaces[indent..-1] if spaces.length >= indent
      s << text << nl
    else  # indicator == '>'
      if !text.empty? && spaces.length == indent
        if s.sub!(/\r?\n((\r?\n)+)\z/, '\1')
          nil
        elsif is_folded
          s.sub!(/\r?\n\z/, ' ')
        end
        #s.sub!(/\r?\n\z/, '') if !s.sub!(/\r?\n(\r?\n)+\z/, '\1') && is_folded
        is_folded = true
      else
        is_folded = false
        s << spaces[indent..-1] if spaces.length > indent
      end
      s << text << nl
    end
  end
  ## chomping
  if chomping == '+'
    nil
  elsif chomping == '-'
    s.sub!(/(\r?\n)+\z/, '')
  else
    s.sub!(/(\r?\n)(\r?\n)+\z/, '\1')
  end
  #
  skip_spaces_and_comments()
  val = s
  #_set_error_info(_linenum, _column) do                                  #*V
  #  @validator._validate_unique(val, rule, path, @errors, uniq_table)    #*V
  #end if uniq_table                                                      #*V
  return val
end

#parse_block_value(level, rule, path, uniq_table, container) ⇒ Object



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
# File 'lib/kwalify/parser/yaml.rb', line 235

def parse_block_value(level, rule, path, uniq_table, container)
  skip_spaces_and_comments()
  ## nil
  return nil if @column <= level || eos?
  ## anchor and alias
  name = nil
  if scan(/\&([-\w]+)/)
    name = parse_anchor(rule, path, uniq_table, container)
  elsif scan(/\*([-\w]+)/)
    return parse_alias(rule, path, uniq_table, container)
  end
  ## type
  if scan(/!!?\w+/)
    skip_spaces_and_comments()
  end
  ## sequence
  if match?(/-\s+/)
    if rule && !rule.sequence
      #_validate_error("sequence is not expected.", path)
      rule = nil
    end
    seq = create_sequence(rule, @linenum, @column)
    @anchors[name] = seq if name
    parse_block_seq(seq, rule, path, uniq_table)
    return seq
  end
  ## mapping
  if match?(MAPKEY_PATTERN)
    if rule && !rule.mapping
      #_validate_error("mapping is not expected.", path)
      rule = nil
    end
    map = create_mapping(rule, @linenum, @column)
    @anchors[name] = map if name
    parse_block_map(map, rule, path, uniq_table)
    return map
  end
  ## sequence (flow-style)
  if match?(/\[/)
    if rule && !rule.sequence
      #_validate_error("sequence is not expected.", path)
      rule = nil
    end
    seq = create_sequence(rule, @linenum, @column)
    @anchors[name] = seq if name
    parse_flow_seq(seq, rule, path, uniq_table)
    return seq
  end
  ## mapping (flow-style)
  if match?(/\{/)
    if rule && !rule.mapping
      #_validate_error("mapping is not expected.", path)
      rule = nil
    end
    map = create_mapping(rule, @linenum, @column)
    @anchors[name] = map if name
    parse_flow_map(map, rule, path, uniq_table)
    return map
  end
  ## block text
  if match?(/[|>]/)
    text = parse_block_text(level, rule, path, uniq_table)
    @anchors[name] = text if name
    return text
  end
  ## scalar
  scalar = parse_block_scalar(rule, path, uniq_table)
  @anchors[name] = scalar if name
  return scalar
end

#parse_file(filename, opts = {}) ⇒ Object



133
134
135
136
# File 'lib/kwalify/parser/yaml.rb', line 133

def parse_file(filename, opts={})
  opts[:filename] = filename
  return parse(File.read(filename), opts)
end

#parse_flow_map(map, map_rule, path, uniq_table) ⇒ Object



634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# File 'lib/kwalify/parser/yaml.rb', line 634

def parse_flow_map(map, map_rule, path, uniq_table)
  #scan(/\{\s*/)  # not work?
  _start_linenum = @linenum                                              #*V
  _start_column  = @column                                               #*V
  scan(/\{/)
  skip_spaces_and_comments()
  if scan(/\}/)
    nil
  else
    path.push(nil)
    is_merged = false
    while true
      _linenum = @linenum                                                #*V
      _column  = @column                                                 #*V
      unless scan(MAPKEY_PATTERN)
        raise _syntax_error("mapping key is expected.", path)
      end
      key = group(1)
      skip_spaces_and_comments()
      _linenum2 = @linenum                                               #*V
      _column2  = @column                                                #*V
      is_merged = _parse_map_value(map, map_rule, path, nil, key, is_merged,
                         uniq_table, _linenum, _column, _linenum2, _column2)
      #skip_spaces_and_comments()
      break unless scan(/,\s+/)
    end
    path.pop()
    unless scan(/\}/)
      raise _syntax_error("flow mapping is not closed by '}'.", path)
    end
  end
  skip_spaces_and_comments()
  _set_error_info(_start_linenum, _start_column) do                      #*V
    @validator._validate_mapping_required_keys(map, map_rule, path, @errors)  #*V
  end if map_rule                                                        #*V
  return map
end

#parse_flow_scalar(rule, path, uniq_table) ⇒ Object



673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
# File 'lib/kwalify/parser/yaml.rb', line 673

def parse_flow_scalar(rule, path, uniq_table)
  ch = peep(1)
  _linenum = @linenum                                                    #*V
  _column  = @column                                                     #*V
  if ch == '"' || ch == "'"
    val = scan_string()
  else
    str = scan(/[^,\]\}\#]*/)
    if match?(/,\S/)
      while match?(/,\S/)
        str << scan(/./)
        str << scan(/[^,\]\}\#]*/)
      end
    end
    str.rstrip!
    val = to_scalar(str)
  end
  val = create_scalar(rule, val, _linenum, _column)                      #*V
  #_set_error_info(_linenum, _column) do                                  #*V
  #  @validator._validate_unique(val, rule, path, @errors, uniq_table)    #*V
  #end if uniq_table                                                      #*V
  skip_spaces_and_comments()
  return val
end

#parse_flow_seq(seq, seq_rule, path, uniq_table) ⇒ Object



597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# File 'lib/kwalify/parser/yaml.rb', line 597

def parse_flow_seq(seq, seq_rule, path, uniq_table)
  #scan(/\[\s*/)
  scan(/\[/)
  skip_spaces_and_comments()
  if scan(/\]/)
    nil
  else
    rule = seq_rule ? seq_rule.sequence[0] : nil                         #*V
    uniq_table = rule ? rule._uniqueness_check_table() : nil             #*V
    path.push(nil)
    i = 0
    while true
      path[-1] = i
      _linenum = @linenum                                                #*V
      _column  = @column                                                 #*V
      val = parse_flow_value(rule, path, uniq_table, seq)
      add_to_seq(rule, seq, val, _linenum, _column)  # seq << val
      _set_error_info(_linenum, _column) do                              #*V
        @validator._validate(val, rule, path, @errors, @done, uniq_table, false)  #*V
      end if rule && !val.equal?(PRECEDING_ALIAS_PLACEHOLDER)            #*V
      skip_spaces_and_comments()
      break unless scan(/,\s+/)
      i += 1
      if match?(/\]/)
        raise _syntax_error("sequence item required (or last comma is extra).", path)
      end
    end
    path.pop()
    unless scan(/\]/)
      raise _syntax_error("flow sequence is not closed by ']'.", path)
    end
  end
  skip_spaces_and_comments()
  return seq
end

#parse_flow_value(rule, path, uniq_table, container) ⇒ Object



555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# File 'lib/kwalify/parser/yaml.rb', line 555

def parse_flow_value(rule, path, uniq_table, container)
  skip_spaces_and_comments()
  ## anchor and alias
  name = nil
  if scan(/\&([-\w]+)/)
    name = parse_anchor(rule, path, uniq_table, container)
  elsif scan(/\*([-\w]+)/)
    return parse_alias(rule, path, uniq_table, container)
  end
  ## type
  if scan(/!!?\w+/)
    skip_spaces_and_comments()
  end
  ## sequence
  if match?(/\[/)
    if rule && !rule.sequence                                            #*V
      #_validate_error("sequence is not expected.", path)                #*V
      rule = nil                                                         #*V
    end                                                                  #*V
    seq = create_sequence(rule, @linenum, @column)
    @anchors[name] = seq if name
    parse_flow_seq(seq, rule, path, uniq_table)
    return seq
  end
  ## mapping
  if match?(/\{/)
    if rule && !rule.mapping                                             #*V
      #_validate_error("mapping is not expected.", path)                 #*V
      rule = nil                                                         #*V
    end                                                                  #*V
    map = create_mapping(rule, @linenum, @column)
    @anchors[name] = map if name
    parse_flow_map(map, rule, path, uniq_table)
    return map
  end
  ## scalar
  scalar = parse_flow_scalar(rule, path, uniq_table)
  @anchors[name] = scalar if name
  return scalar
end

#parse_nextObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/kwalify/parser/yaml.rb', line 139

def parse_next()
  reset_parser()
  path = []
  skip_spaces_and_comments()
  if document_start?()
    scan(/.*\n/)
    skip_spaces_and_comments()
  end
  _linenum = @linenum                                                    #*V
  _column = @column                                                      #*V
  rule = @validator ? @validator.rule : nil                              #*V
  uniq_table = nil                                                       #*V
  parent = nil                                                           #*V
  val = parse_block_value(0, rule, path, uniq_table, parent)
  _set_error_info(_linenum, _column) do                                  #*V
    @validator._validate(val, rule, [], @errors, @done, uniq_table, false)  #*V
  end if rule                                                            #*V
  resolve_preceding_aliases(val) if @preceding_alias
  unless eos? || document_start?() || stream_end?()
    raise _syntax_error("document end expected (maybe invalid tab char found).", path)
  end
  @doc = val
  @location_table[-1] = [_linenum, _column]
  return val
end

#parse_stream(input, opts = {}, &block) ⇒ Object Also known as: parse_documents



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/kwalify/parser/yaml.rb', line 166

def parse_stream(input, opts={}, &block)
  reset_scanner(input, opts[:filename], opts[:untabify])
  ydocs = block_given? ? nil : []
  while true
    ydoc = parse_next()
    ydocs ? (ydocs << ydoc) : (yield ydoc)
    break if eos? || stream_end?()
    document_start?() or raise "** internal error"
    scan(/.*\n/)
  end
  return ydocs
end

#reset_parserObject



60
61
62
63
64
65
66
67
# File 'lib/kwalify/parser/yaml.rb', line 60

def reset_parser()
  @anchors = {}
  @errors = []
  @done = {}
  @preceding_aliases = []
  @location_table = {}    # object_id -> sequence or mapping
  @doc = nil
end

#resolve_preceding_aliases(val) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/kwalify/parser/yaml.rb', line 215

def resolve_preceding_aliases(val)
  @preceding_aliases.each do |name, rule, path, container, _linenum, _column|
    unless @anchors.key?(name)
      raise _syntax_error("*#{name}: anchor not found.", path, _linenum, _column)
    end
    key = path[-1]
    val = @anchors[name]
    raise unless !container.respond_to?('[]') || container[key].equal?(PRECEDING_ALIAS_PLACEHOLDER)
    if container.is_a?(Array)
      container[key] = val
    else
      put_to_map(rule, container, key, val, _linenum, _column)
    end
    _set_error_info(_linenum, _column) do                                #*V
      @validator._validate(val, rule, path, @errors, @done, false)       #*V
    end if rule                                                          #*V
  end
end

#set_errors_linenum(errors) ⇒ Object



834
835
836
837
838
# File 'lib/kwalify/parser/yaml.rb', line 834

def set_errors_linenum(errors)
  errors.each do |error|
    error.linenum, error.column = location(error.path)
  end
end

#skip_spaces_and_commentsObject



103
104
105
106
107
108
109
# File 'lib/kwalify/parser/yaml.rb', line 103

def skip_spaces_and_comments()
  scan(/\s+/)
  while match?(/\#/)
    scan(/.*?\n/)
    scan(/\s+/)
  end
end

#stream_end?Boolean

Returns:



117
118
119
# File 'lib/kwalify/parser/yaml.rb', line 117

def stream_end?()
  return match?(/\.\.\.\s/) && @column == 1
end

#to_scalar(str) ⇒ Object



702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# File 'lib/kwalify/parser/yaml.rb', line 702

def to_scalar(str)
  case str
  when nil                ;  val = nil
  when /\A-?\d+\.\d+\z/   ;  val = str.to_f
  when /\A-?\d+\z/        ;  val = str.to_i
  when /\A(true|yes)\z/   ;  val = true
  when /\A(false|no)\z/   ;  val = false
  when /\A(null|~)\z/     ;  val = nil
  when /\A"(.*)"\z/       ;  val = $1
  when /\A'(.*)'\z/       ;  val = $1
  when /\A:(\w+)\z/       ;  val = $1.intern
  when /\A(\d\d\d\d)-(\d\d)-(\d\d)(?: (\d\d):(\d\d):(\d\d))?\z/
    year, month, day, hour, min, sec = $1, $2, $3, $4, $5, $6
    if hour
      val = Time.mktime(year, month, day, hour, min, sec)
    else
      val = Date.new(year.to_i, month.to_i, day.to_i)
    end
    ## or
    #params = [$1, $2, $3, $4, $5, $6]
    #val = Time.mktime(*params)
  else
    val = str.empty? ? nil : str
  end
  skip_spaces_and_comments()
  return val
end