Class: CDDL::Parser

Inherits:
Object show all
Defined in:
lib/cddl.rb

Defined Under Namespace

Classes: BackTrack

Constant Summary collapse

FEATURE_REJECT_RE =
/\A\^/
REGEXP_FOR_STRING =

Memoize a bit here

Hash.new {|h, k|
  h[k] = Regexp.new("\\A(?:#{k})\\z")
}
ABNF_PARSER_FOR_STRING =
Hash.new {|h, k|
  grammar = "cddl-t0p--1eve1-f0r--abnf = " << k # XXX
  h[k] = ABNF.from_abnf(grammar)
}
ABNF_ENCODING_FOR_CONOP =
{
  abnf: Encoding::UTF_8,
  abnfb: Encoding::BINARY
}
VALUE_TYPE =
{text: String, bytes: String, int: Integer, float: Float}
SIMPLE_VALUE =
{
  [:prim, 7, 20] => [true, false, :bool],
  [:prim, 7, 21] => [true, true, :bool],
  [:prim, 7, 22] => [true, nil, :nil],
  [:prim, 7, 23] => [true, :undefined, :undefined],
}
OPERATORS =
{lt: :<, le: :<=, gt: :>, ge: :>=, eq: :==, ne: :!=}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_text) ⇒ Parser

Returns a new instance of Parser.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cddl.rb', line 45

def initialize(source_text)
  @abnf = Peggy::ABNF.new
  _cresult = @abnf.compile! ABNF_SPEC, ignore: :s
  presult = @abnf.parse? :cddl, (source_text + PRELUDE)
  expected_length = source_text.length + PRELUDE.length
  if expected_length != presult
    upto = @abnf.parse_results.keys.max
    puts "UPTO: #{upto}" if $advanced
    pp @abnf.parse_results[upto] if $advanced
    pp @abnf.parse_results[presult] if $advanced
    puts "SO FAR: #{presult}"  if $advanced
    puts @abnf.ast? if $advanced
    presult ||= 0
    part1 = source_text[[presult - 100, 0].max...presult]
    part3 = source_text[upto...[upto + 100, source_text.length].min]
    if upto - presult < 100
      part2 = source_text[presult...upto]
    else
      part2 = source_text[presult, 50] + "......." + source_text[upto-50, 50]
    end
    warn "*** Look for syntax problems around the #{
           "%%%".colorize(background: :light_yellow)} markers:\n#{
           part1}#{"%%%".colorize(color: :green, background: :light_yellow)}#{
           part2}#{"%%%".colorize(color: :red, background: :light_yellow)}#{
           part3}"
    raise ParseError, "*** Parse error at #{presult} upto #{upto} of #{
                      source_text.length} (#{expected_length})."
  end
  puts @abnf.ast? if $debug_ast
  @ast = @abnf.ast?
  # our little argument stack for rule processing
  @insides = []
  # collect error information
  @last_message = ""
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



1265
1266
1267
# File 'lib/cddl.rb', line 1265

def ast
  @ast
end

Instance Method Details

#aprObject

for debugging



81
82
83
# File 'lib/cddl.rb', line 81

def apr                     # for debugging
  @abnf.parse_results
end

#ast_debugObject



85
86
87
# File 'lib/cddl.rb', line 85

def ast_debug
  ast.to_s[/.*comment/m]    # stop at first comment -- prelude
end

#cname(s) ⇒ Object



120
121
122
# File 'lib/cddl.rb', line 120

def cname(s)
  s.to_s.gsub(/-/, "_")
end

#defines(prefix) ⇒ Object

Generate some simple #define lines from the value-only rules



125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/cddl.rb', line 125

def defines(prefix)
  prefix ||= "CDDL"
  if prefix =~ /%\d*\$?s/
    format = prefix
    format += "\n" unless format[-1] == "\n"
  else
    format = "#define #{prefix}_%s %s\n"
  end
  s = {}                    # keys form crude set of defines
  add = proc { |*a| s[format % a] = true }
  r = rules
  ast.each :rule do |rule|
    if rulename = rule.typename
      t = rule.type.children(:type1)
      if t.size == 1
        if (t2 = t.first.children(:type2)) && t2.size == 1 && (v = t2.first.value)
          add.(cname(rulename), v.to_s)
        end
      end
    end
  end
  # CBOR::PP.pp r
  walk(r) do |subtree, anno|
    if subtree[0] == :type1 && subtree[1..-1].all? {|x| x[0] == :int}
      if enumname = subtree.cbor_annotations rescue nil
        enumname = cname(enumname.first)
        subtree[1..-1].each do |x|
          if memname = x.cbor_annotations
            memname = "#{enumname}_#{cname(memname.first)}"
            add.(memname, x[1].to_s)
          end
        end
      end
    end
    if subtree[0] == :array
      if (arrayname = subtree.cbor_annotations rescue nil) || anno
        arrayname = cname(arrayname ? arrayname.first : anno)
        subtree[1..-1].each_with_index do |x, i|
          if x[0] == :member
            if Array === x[3] && x[3][0] == :text
              memname = x[3][1] # preferably use key string
            elsif memname = x[4].cbor_annotations
              memname = memname.first # use value annotation otherwise
            end
            if memname
              memname = "#{arrayname}_#{cname(memname)}_index"
              add.(memname, i.to_s)
            end
          end
          if x[0] == :member && (x[1] != 1 || x[2] != 1)
            break           # can't give numbers if we have optionals etc.
          end
        end
      end
    end
  end
  s.keys.join
end

#extract_arg0(t) ⇒ Object



754
755
756
757
758
759
760
761
762
763
764
# File 'lib/cddl.rb', line 754

def extract_arg0(t)
  return [false] unless t[0] == :array
  [true,
   (el = t[1]
    return [false] unless el[0..3] == [:member, 1, 1, nil]
    ok, v, vt = extract_value(el[4])
    return [false] unless ok
    [v, vt]
   ),
   *t[2..-1]]
end

#extract_array(t) ⇒ Object



743
744
745
746
747
748
749
750
751
# File 'lib/cddl.rb', line 743

def extract_array(t)
  return [false] unless t[0] == :array
  [true, *t[1..-1].map { |el|
     return [false] unless el[0..3] == [:member, 1, 1, nil]
     ok, v, vt = extract_value(el[4])
     return [false] unless ok
     [v, vt]
   }]
end

#extract_feature(control, d) ⇒ Object



766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
# File 'lib/cddl.rb', line 766

def extract_feature(control, d)
  ok, v, vt = extract_value(control)
  if ok
    nm = v
    det = d
    warn "*** feature controller should be a string: #{control.inspect}" unless String == vt
  else
    ok, *v = extract_array(control)
    if ok && v.size == 2
      nm = v[0][0]
      det = v[1][0]
      warn "*** first element of feature controller should be a string: #{control.inspect}" unless String === nm
    else
      warn "*** feature controller not implemented: #{control.inspect}"
    end
  end
  [nm, det]
end

#extract_value(t) ⇒ Object



714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
# File 'lib/cddl.rb', line 714

def extract_value(t)        # []
  if vt = VALUE_TYPE[t[0]]
    [true, t[1], vt]
  elsif v = SIMPLE_VALUE[t]
    v
  elsif t[0] == :anno
    _, conop, target, control = t
    # warn ["EXV0", conop, target, control].inspect
    if conop == :cat || conop == :plus || conop == :det
      ok1, v1, vt1 = extract_value(target)
      ok2, v2, vt2 = extract_value(control)
      # warn ["EXV", ok1, v1, vt1, ok2, v2, vt2].inspect
      if ok1 && ok2
        if vt1 == Integer
          [true, v1 + Integer(v2), Integer] if vt2 == Integer || vt2 == Float
        elsif vt1 == Float
          [true, v1 + v2, vt1] if vt2 == Integer || vt2 == Float
        else
          if conop == :det
            v1 = remove_indentation(v1)
            v2 = remove_indentation(v2)
          end
          [true, v1 + v2, vt1] if vt1 == vt2
        end
      end rescue nil
    end
  end || [false]
end

#generateObject



304
305
306
307
# File 'lib/cddl.rb', line 304

def generate
  @recursion = 0
  generate1(rules)
end

#generate1(where, inmap = false) ⇒ Object



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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
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
553
554
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
595
596
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
632
633
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
# File 'lib/cddl.rb', line 309

def generate1(where, inmap = false)
  case where[0]
  when :type1
    fail BackTrack.new("Can't generate from empty type choice socket yet") unless where.size > 1
    begin
      chosen = where[rand(where.size-1)+1]
      generate1(chosen)
    rescue BackTrack
      tries = where[1..-1].sample(where.size) - [chosen]
      r = begin
            if tries.empty?
              BackTrack.new("No suitable alternative in type choice")
            else
              generate1(tries.pop)
            end
          rescue BackTrack
            retry
          end
      fail r if BackTrack === r
      r
    end
  when :grpchoice
    fail BackTrack.new("Can't generate from empty group choice socket yet") unless where.size > 1
    begin
      chosen = where[rand(where.size-1)+1]
      chosen.flat_map {|m| generate1(m, inmap)}
    rescue BackTrack
      tries = where[1..-1].sample(where.size) - [chosen]
      r = begin
            if tries.empty?
              BackTrack.new("No suitable alternative in group choice")
            else
              tries.pop.flat_map {|m| generate1(m, inmap)}
            end
          rescue BackTrack
            retry
          end
      fail r if BackTrack === r
      r
    end
  when :map
    Hash[where[1..-1].flat_map {|m| generate1(m, true)}]
  when :recurse_grpent
    name = where[1]
    rule = lookup_recurse_grpent(name)
    if @recursion < MAX_RECURSE
      @recursion += 1
#p ["recurse_grpent", *rule]
#r = generate1(rule)
      r = rule[1..-1].flat_map {|m| generate1(m, inmap)}
      @recursion -= 1
      r
    else
      fail "Deep recursion into #{name}: #{@stage1[name]}, not yet implemented"
    end
  when :array, :grpent
    r = where[1..-1].flat_map {|m| generate1(m).map{|e| e[1]}}
        .flat_map {|e| Array === e && e[0] == :grpent ? e[1..-1] : [e]}
                       # nested grpents need to be "unpacked"
    if where[0] == :grpent
      [:grpent, *r]
    else
      r
    end
  when :member
    st = where[1]
    fudge = 4 * (1 - (@recursion / MAX_RECURSE.to_f))**3 # get less generate-happy with more recursion
    en = [where[2], [st, fudge].max].min # truncate to fudge unless must be more
    st += rand(en + 1 - st) if en != st
    kr = where[3]
    vr = where[4]
    if inmap
      unless kr
        case vr[0]
        when :grpent
          # fail "grpent in map #{vr.inspect}" unless vr.size == 2
          g = Array.new(st) {
            vr[1..-1].map{ |vrx|
              generate1(vrx, true)
            }.flatten(1)
          }.flatten(1)
          # warn "GGG #{g.inspect}"
          return g
        when :grpchoice
          g = Array.new(st) { generate1(vr, true) }.flatten(1)
          # warn "GGG #{g.inspect}"
          return g
        else
          fail "member key not given in map for #{where}"  # || vr == [:grpchoice]
        end
      end
    end
    begin
      Array.new(st) { [ (generate1(kr) if kr), # XXX: need error in map context
                        generate1(vr)
                      ]}
    rescue BackTrack
      fail BackTrack.new("Need #{where[1]}..#{where[2]} of these: #{[kr, vr].inspect}") unless where[1] == 0
      []
    end
  when :text, :int, :float, :bytes
    where[1]
  when :range
    rand(where[1])
  when :prim
    case where[1]
    when nil
      gen_word              # XXX: maybe always returning a string is confusing
    when 0
      rand(4711)
    when 1
      ~rand(815)
    when 2
      gen_word.force_encoding(Encoding::BINARY)
    when 3
      gen_word
    when 6
      tn = Integer === where[2] ? where[2] : generate1(where[2])
      unless Integer === tn && tn >= 0
        fail "Can't generate a tag number out of #{where[2]}"
      end
      CBOR::Tagged.new(tn, generate1(where[3]))
    when 7
      case where[2]
      when nil
        Math::PI
      when 20
        false
      when 21
        true
      when 22
        nil
      when 23
        :undefined
      when 25, 26, 27
        rand()
      end
    else
      fail "Can't generate prim #{where[1]}"
    end
  when :anno
    target = where[2]
    control = where[3]
    case conop = where[1]
    when :size
      should_be_int = generate1(control)
      unless (Array === target && target[0] == :prim && [0, 2, 3].include?(target[1])) && Integer === should_be_int && should_be_int >= 0
        fail "Don't know yet how to generate #{where}"
      end
      s = Random.new.bytes(should_be_int)
      case target[1]
      when 0
        # silently restrict to what we can put into a uint
        s[0...8].bytes.inject(0) {|a, b| a << 8 | b }
      when 2
        s
      when 3
        Base64.urlsafe_encode64(s)[0...should_be_int]
        # XXX generate word a la w = gen_word
      end
    when :bits
      set_of_bits = Array.new(10) { generate1(control) } # XXX: ten?
      # p set_of_bits
      unless (target == [:prim, 0] || target == [:prim, 2]) &&
             set_of_bits.all? {|x| Integer === x && x >= 0 }
        fail "Don't know yet how to generate #{where}"
      end
      if target == [:prim, 2]
        set_of_bits.inject(String.new) do |s, i|
          n = i >> 3
          bit = 1 << (i & 7)
          if v = s.getbyte(n)
            s.setbyte(n, v | bit); s
          else
            s << "\0" * (n - s.size) << bit.chr(Encoding::BINARY)
          end
        end
      else                  # target == [:prim, 0]
        set_of_bits.inject(0) do |a, v|
          a |= (1 << v)
        end
      end
    when :default
      # Hmm.
      unless $default_warned
        warn "*** Ignoring .default for now."
        $default_warned = true
      end
      generate1(target, inmap)
    when :feature
      feature, = extract_feature(control, nil)
      # warn "@@@ GENFEAT #{feature.inspect} #{CDDL_FEATURE_REJECT[feature].inspect}"
      if CDDL_FEATURE_REJECT[feature]
        fail BackTrack.new("Feature '#{feature}' rejected")
      end
      generate1(target, inmap)
    when :cat, :det
      lhs = generate1(target, inmap)
      rhs = generate1(control)
      if conop == :det
        lhs = remove_indentation(lhs)
        rhs = remove_indentation(rhs)
      end
      begin
        lhs + rhs
      rescue Exception => e
        fail "Can't #{lhs.inspect} .cat #{rhs.inspect}: #{e.message}"
      end
    when :plus
      lhs = generate1(target, inmap)
      rhs = generate1(control)
      begin
        case lhs
        when Integer
          lhs + Integer(rhs)
        when Numeric
          lhs + rhs
        else
          fail "#{lhs.inspect}: Not a number"
        end
      rescue Exception => e
        fail "Can't #{lhs.inspect} .plus #{rhs.inspect}: #{e.message}"
      end
    when :eq
      content = generate1(control)
      if validate1(content, where)
        return content
      end
      fail "Not smart enough to generate #{where}"
    when :lt, :le, :gt, :ge, :ne
      if Array === target && target[0] == :prim
        content = generate1(control)
        try = if Numeric === content
                content = Integer(content)
                case target[1]
                when 0
                  case conop
                  when :lt
                    rand(0...content)
                  when :le
                    rand(0..content)
                  end
                end
              end
        if validate1(try, where)
          return try
        else
          warn "HUH gen #{where.inspect} #{try.inspect}" unless try.nil?
        end
      end
      try_generate(target) do |content|
        if validate1(content, where)
          return content
        end
      end
      fail "Not smart enough to generate #{where}"
    when :regexp
      regexp = generate1(control)
      unless target == [:prim, 3] && String === regexp
        fail "Don't know yet how to generate #{where}"
      end
      REGEXP_FOR_STRING[regexp].random_example(max_repeater_variance: 5)
    when :abnf, :abnfb
      grammar = generate1(control)
      bytes = true if target == [:prim, 2]
      bytes = false if target == [:prim, 3]
      unless !bytes.nil? && String === grammar
        fail "Don't know yet how to generate #{where}"
      end
      grammar << "\n" unless grammar =~ /\n\z/
      out = ABNF_PARSER_FOR_STRING[grammar].generate
      if conop == :abnfb
        out = out.codepoints.pack("C*")
      end
      enc = bytes ? Encoding::BINARY : Encoding::UTF_8
      out.force_encoding(enc)
    when :cbor, :cborseq, :cbordet, :cborseqdet
      unless target == [:prim, 2]
        fail "Don't know yet how to generate #{where}"
      end
      input = generate1(control)
      if conop == :cbordet || conop == :cborseqdet
        input = input.cbor_prepare_deterministic
      end
      content = CBOR::encode(input)
      if conop == :cborseq || conop == :cborseqdet
        # remove the first head
        content = unpack_array_to_sequence(content, where)
      end
      content
    when :json
      unless target == [:prim, 3]
        fail "Don't know yet how to generate #{where}"
      end
      content = JSON::dump(generate1(control))
      content
    when :decimal
      unless target == [:prim, 3]
        fail "Don't know yet how to generate #{where}"
      end
      content = Integer(generate1(control)).to_s
      content
    when :join
      try_generate(control) do |content|
        if Array === content &&
           content.all? {|x| String === x} &&
           Set[content.map {|x| x.encoding}].size == 1
          content = content.join
          if validate1(content, target)
            return content
          end
        end
      end
      fail "Don't know yet how to generate #{where}"
    when :b64u, :"b64u-sloppy", :b64c, :"b64c-sloppy",
         :b45, :b32, :h32, :hex, :hexlc, :hexuc
      try_generate(control) do |content|
        if String === content
          content = case conop
                when :b64u, :"b64u-sloppy"
                  Base64.urlsafe_encode64(content, padding: false)
                when :b64c, :"b64c-sloppy"
                  Base64.strict_encode64(content)
                when :b45
                  Base45Lite.encode(content)
                when :b32
                  Base32.table = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
                  Base32.encode(content).gsub("=", "")
                when :h32
                  Base32.table = "0123456789ABCDEFGHIJKLMNOPQRSTUV"
                  Base32.encode(content).gsub("=", "")
                when :hex, :hexuc
                  content.unpack("H*")[0].upcase
                when :hexlc
                  content.unpack("H*")[0]
                else fail "Cannot happen"
                end
          if validate1(content, target)
            return content
          end
        end
      end
      fail "Not smart enough to generate #{where}"
    when :printf
      try_generate(control) do |content|
        if Array === content && content.size >= 1
          fmt, *data = content
          if String === fmt
            begin
              content = fmt % data
              if validate1(content, target)
                return content
              end
            rescue ArgumentError => e
              # be verbose about mismatches here
              @last_message << "\n** #{fmt.inspect} ArgumentError #{e}"
            end
          end
        end
      end
      fail "Not smart enough to generate #{where}#{@last_message}"
    when :within, :and
      try_generate(target) do |content|
        if validate1(content, control)
          return content
        elsif conop == :within
          warn "*** #{content.inspect} meets #{target.inspect} but not #{control.inspect}"
        end
      end
      fail "Not smart enough to generate #{where}"
    else
      fail "Don't know yet how to generate from #{where}"
    end
  when :recurse
    name = where[1]
    rule = @stage1[name]
    if @recursion < MAX_RECURSE
      @recursion += 1
#p ["recurse", *rule]
      r = generate1(rule)
      @recursion -= 1
      r
    else
      fail "Deep recursion into #{name}: #{@stage1[name]}, not yet implemented"
    end
  else
    fail "Don't know how to generate from #{where[0]} in #{where.inspect}"
  end
end

#lookup_recurse_grpent(name) ⇒ Object



260
261
262
263
264
265
# File 'lib/cddl.rb', line 260

def lookup_recurse_grpent(name)
  rule = @stage1[name]
  # pp rule
  fail unless rule.size == 2
  [rule[0], *rule[1]]
end

#map_check(d, d_check, members) ⇒ Object



868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
# File 'lib/cddl.rb', line 868

def map_check(d, d_check, members)
  anno = []
  anno if members.all? { |r|
    puts "ALL SUBRULE: #{r.inspect}"         if ENV["CDDL_TRACE"]
    t, s, e, k, v = r
    case t
    when :recurse_grpent
      rule = lookup_recurse_grpent(s)
      if ann2 = map_check(d, d_check, rule[1..-1])
        anno.concat(ann2)
      end
    when :grpchoice
      r[1..-1].any? {|cand|
        puts "CHOICE SUBRULE: #{cand.inspect}"         if ENV["CDDL_TRACE"]
        cand_d_check = d_check.dup
        if ann2 = map_check(d, cand_d_check, cand)
          puts "CHOICE SUBRULE SUCCESS: #{cand.inspect}"         if ENV["CDDL_TRACE"]
          d_check.replace(cand_d_check)
          anno.concat(ann2)
        end
      }
    when :member
      unless k
        case v[0]
        when :grpent
          entries = v[1..-1]
        when :grpchoice
          entries = [v]
        else
          fail "member name not known for group entry #{r} in map"
        end
        d_check1 = d_check.dup
        occ = 0
        ann2 = []
        while occ < e && (ann3 = map_check(d, d_check1, entries)) && ann3 != []
          occ += 1
          ann2.concat(ann3)
        end
        if occ >= s
          d_check.replace(d_check1)
          anno.concat(ann2)
          puts "OCC SATISFIED: #{occ.inspect} >= #{s.inspect}" if ENV["CDDL_TRACE"]
          anno
        else
          # leave some diagnostic breadcrumbs?
          puts "OCC UNSATISFIED: #{occ.inspect} < #{s.inspect}" if ENV["CDDL_TRACE"]
          false
        end
      else
      # this is mostly quadratic; let's do the linear thing if possible
      simple, simpleval = extract_value(k)
      if simple
                      puts "SIMPLE: #{d_check.inspect} #{simpleval}"         if ENV["CDDL_TRACE"]
        # add occurrence check; check that val is present in the first place
        actual = d.fetch(simpleval, :not_found)
        if actual == :not_found
          s == 0          # minimum occurrence must be 0 then
        else
          if (ann2 = validate1a(actual, v)) &&
             d_check.delete(simpleval) {:not_found} != :not_found
            anno.concat(ann2)
          end
        end
      else
                      puts "COMPLEX: #{k.inspect} #{simple.inspect} #{simpleval.inspect}"         if ENV["CDDL_TRACE"]
        keys = d_check.keys
        ta, keys = keys.partition{ |key| validate1(key, k)}
        count = 0
        catch :enough do
          ta.all? { |val|
            if (ann2 = validate1a(d[val], v)) && # XXX check cut or not!
               d_check.delete(val) {:not_found} != :not_found
              anno.concat(ann2)
              throw :enough, true if (count += 1) == e
              true
            end
          }
        end and validate_result(count >= s) { "not enough #{ta.inspect} for #{r.inspect}" }
      end
      end
    else
      fail "Cannot validate #{t} in maps yet #{r}" # MMM
    end
  }
end

#remove_indentation(s) ⇒ Object



267
268
269
270
271
# File 'lib/cddl.rb', line 267

def remove_indentation(s)
  l = s.lines
  indent = l.grep(/\S/).map {|l| l[/^\s*/].size}.min
  l.map {|l| l.sub(/^ {0,#{indent}}/, "")}.join
end

#rulesObject



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
# File 'lib/cddl.rb', line 184

def rules
  @rules = {}
  @generics = {}
  @bindings = [{}]
  ast.each :rule do |rule|
    rule_ast =
      if rulename = rule.groupname
        [:grpent, rule.grpent]
      elsif rulename = rule.typename
        [:type1, *rule.type.children(:type1)]
      else
        fail "Huh?"
      end
    n = rulename.to_s
    asg = rule.assign.to_s
    if g = rule.genericparm
      if asg != "="
        fail "Augment #{asg.inspect} not implemented for generics"
      end
      ids = g.children(:id).map(&:to_s)
      # puts ["ids", ids].inspect
      if b = @generics[n]
        fail "Duplicate generics definition #{n} as #{rule_ast} (was #{b})"
      end
      @generics[n] = [rule_ast, ids]
    else
      case asg
      when "="
        if @rules[n]
          a = strip_nodes(rule_ast).inspect
          b = strip_nodes(@rules[n]).inspect
          if a == b
            warn "*** Warning: Identical redefinition of #{n} as #{a}"
          else
            fail "Duplicate rule definition #{n} as #{b} (was #{a})"
          end
        end
        @rules[n] = rule_ast
      when "/="
        @rules[n] ||= [:type1]
        fail "Can't add #{rule_ast} to #{n}" unless rule_ast[0] == :type1
        # XXX need to check existing rule as well
        @rules[n].concat rule_ast[1..-1]
        # puts "#{@rules[n].inspect} /="
      when "//="
        @rules[n] ||= [:grpchoice]
        fail "Can't add #{rule_ast} to #{n}" unless rule_ast[0] == :grpent
        # XXX need to check existing rule as well
        if @rules[n][0] == :grpent # widen
          @rules[n] = [:grpchoice, @rules[n]]
        end
        @rules[n] << rule_ast
        # puts "#{@rules[n].inspect} //="
      else
        fail "Unknown assign #{asg.inspect}"
      end
    end
  end
  # pp @generics
  @rootrule = @rules.keys.first # DRAFT: generics are ignored here.
  # now process the rules...
  @stage1 = {}
  result = r_process(@rootrule, @rules[@rootrule])
  r_process("used_in_cddl_prelude", @rules["used_in_cddl_prelude"])
  @rules.each do |n, r|
  #   r_process(n, r)     # debug only loop
    warn "*** Unused rule #{n}" unless @stage1[n]
  end
  if result[0] == :grpent
    warn "Group at top -- first rule must be a type!"
  end
  # end
  # @stage1
  result
end

#strip_nodes(n) ⇒ Object



89
90
91
92
93
# File 'lib/cddl.rb', line 89

def strip_nodes(n)
  [n[0], *n[1..-1].map {|e|
    e._strip
  }]
end

#try_generate(target) ⇒ Object



699
700
701
702
703
704
# File 'lib/cddl.rb', line 699

def try_generate(target)
  32.times do
    content = generate1(target)
    yield content           # should return if success
  end
end

#unpack_array_to_sequence(content, where) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/cddl.rb', line 273

def unpack_array_to_sequence(content, where)
  # remove the first head
  n = case content.getbyte(0) - (4 << 5)
      when 0..23; 1
      when 24; 2
      when 25; 3
      when 26; 5
      when 27; 9      # unlikely :-)
      else fail ".cborseq sequence for #{where} not an array"
      end
  content[0...n] = ''
  content
end

#validate(d, warn = true) ⇒ Object



789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
# File 'lib/cddl.rb', line 789

def validate(d, warn=true)
  @recursion = 0
  $features = Hash.new {|h, k| h[k] = {}}
  result = validate1a(d, rules)
  if warn
    if result
      if $features != {}
        features = $features.reject {|k, v| CDDL_FEATURE_OK[k.to_s] }
        # warn "@@@ FEAT #{CDDL_FEATURE_OK.inspect} #{CDDL_FEATURE_REJECT.inspect}"
        warn "** Features potentially used (#$fn): #{features.map {|k, v| "#{k}: #{v.keys}"}.join(", ")}" if features != {}
      end
    else
      warn "CDDL validation failure (#{result.inspect} for #{d.inspect}):"
      PP::pp(validate_diag, STDERR)
      puts(validate_diag.cbor_diagnostic)
    end
  end
  result
end

#validate1(d, where) ⇒ Object



967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
# File 'lib/cddl.rb', line 967

def validate1(d, where)
  if ENV["CDDL_TRACE"]
    puts "DATA: #{d.inspect}"
    puts "RULE: #{where.inspect}"
  end
  # warn ["val1", d, where].inspect
  @last_data = d
  @last_rule = where
  ann = nil
  case where[0]
  when :type1
    if where[1..-1].any? {|r| ann = validate1a(d, r)}
      ann
    end
  when :map
    if Hash === d
      d_check = d.dup
      if (ann = map_check(d, d_check, where[1..-1])) && d_check == {}
        ann
      else
        if ENV["CDDL_TRACE"]
          puts "MAP RESIDUAL: #{d_check.inspect} for #{where[1..-1]} and #{d.inspect}"
        end
      end
    end
  when :array
    # warn ["valarr", d, where].inspect
    if Array === d
      # validate1 against the record
      idx, ann = validate_forward(d, 0, where)
      ann if validate_result(idx == d.size) { "#{validate_diag.inspect} -- cannot complete (#{idx}, #{d.size}) array #{d} for #{where}" }
    end
  when :int, :float
    _, v = extract_value(where)
    [] if d == v
  when :text, :bytes
    _, v = extract_value(where)
    [] if d == v && ((d.encoding == Encoding::BINARY) == (v.encoding == Encoding::BINARY))
  when :range
    [] if where[2] === d && where[1].include?(d)
  when :anno
    _, conop, target, control = where
    if conop == :cat || conop == :plus || conop == :det
      ok1, v1, vt1 = extract_value(target)
      ok2, v2, vt2 = extract_value(control)
      # warn ["ANNO0", ok1, v1, vt1, ok2, v2, vt2, d].inspect
      if ok1 && ok2
        v2 = Integer(v2) if vt1 == Integer
        if conop == :det
          v1 = remove_indentation(v1)
          v2 = remove_indentation(v2)
        end
        # warn ["ANNO", ok1, v1, vt1, ok2, v2, vt2, d].inspect
        [] if d == v1 + v2  # XXX Focus ArgumentError
      end
    elsif ann = validate1a(d, target)
      case conop
      when :size
        case d
        when Integer
          ok, v, vt = extract_value(control)
          if ok && vt == Integer
            ann if (d >> (8*v)) == 0
          end
        when String
          ann if validate1(d.bytesize, control)
        end
      when :bits
        if String === d
          d.each_byte.with_index.all? { |b, i|
            bit = i << 3
            ann if 8.times.all? { |nb|
              b[nb] == 0 || validate1(bit+nb, control)
            }
          }
        elsif Integer === d
          if d >= 0
            ok = true
            i = 0
            while ok && d > 0
              if d.odd?
                ok &&= validate1(i, control)
              end
              d >>= 1; i += 1
            end
            ann if ok
          end
        end
      when :default
        # Hmm.
        unless $default_warned
          warn "*** Ignoring .default for now."
          $default_warned = true
        end
        ann
      when :feature
        nm, det = extract_feature(control, d)
        $features[nm][det] = true
        ann if !CDDL_FEATURE_REJECT[nm]
      when :lt, :le, :gt, :ge, :eq, :ne
        op = OPERATORS[conop]
        ok, v, _vt = extract_value(control)
        if ok
          ann if d.send(op, v) rescue nil # XXX Focus ArgumentError
        end
      when :regexp
        ann if (
        if String === d
          ok, v, vt = extract_value(control)
          if ok && vt == String
            re = REGEXP_FOR_STRING[v]
            # pp re
            d.match(re)
          end
        end
        )
      when :abnf, :abnfb
        ann if (
        if String === d
          ok, v, vt = extract_value(control)
          if ok && vt == String
            begin
              ABNF_PARSER_FOR_STRING[v].validate(
                d.dup.force_encoding(ABNF_ENCODING_FOR_CONOP[conop]).codepoints.pack("U*")
              )
              true
            rescue => e
              # warn "*** #{e}" # XXX
              @last_message = e.to_s.force_encoding(Encoding::UTF_8)
              nil
            end
          end
        end
        )
      when :cbor
        ann if validate1((CBOR::decode(d) rescue :BAD_CBOR), control)
      when :cborseq
        ann if validate1((CBOR::decode("\x9f".b << d << "\xff".b) rescue :BAD_CBOR), control)
      when :cbordet
        decoded = CBOR::decode(d) rescue :BAD_CBOR
        if d != decoded.to_deterministic_cbor
          @last_message = "CBOR #{d.inspect} not deterministically encoded"
          nil
        else
          ann if validate1(decoded, control)
        end
      when :cborseqdet
        decoded = CBOR::decode("\x9f".b << d << "\xff".b) rescue :BAD_CBOR
        if d != unpack_array_to_sequence(decoded.to_deterministic_cbor, d.inspect)
          @last_message = "CBOR Sequence #{d.inspect} not deterministically encoded"
          nil
        else
          ann if validate1(decoded, control)
        end
      when :json
        ann if validate1((JSON::load(d) rescue :BAD_JSON), control)
      when :decimal
        ann if (
            if /\A0|-?[1-9][0-9]*\z/ === d
              validate1((d.to_i rescue :BAD_JSON), control)
            end
          )
      when :join
        ok, *v = extract_array(control)
        # warn "@@@JJJ@@@ #{ok.inspect} #{v.inspect}"
        if ok
          expected = v.map {|ve|
            fail "Not a string in #{where}" unless String === ve[0]
            ve[0]
          }.join
          ann if d == expected
        else
          fail "Don't know yet how to validate against #{where}"
        end
      when :b64u, :"b64u-sloppy", :b64c, :"b64c-sloppy", :b45, :b32, :h32, :hex, :hexlc, :hexuc
        ann if (
            String === d && (
            decoded = case conop
                when :b64u
                  /=/ !~ d &&
                  Base64.urlsafe_decode64(d)
                when :"b64u-sloppy"
                  /[-_=]/ !~ d &&
                  Base64.decode64(d.tr("+/", "-_"))
                when :b64c
                  Base64.strict_decode64(d)
                when :"b64c-sloppy"
                  Base64.decode64(d)
                when :b32
                  /=/ !~ d &&
                    (Base32.table = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567") &&
                    Base32.decode(d)
                when :b45
                  Base45Lite.decode(d)
                when :h32
                  /=/ !~ d &&
                    (Base32.table = "0123456789ABCDEFGHIJKLMNOPQRSTUV") &&
                    Base32.decode(d)
                when :hex
                  /\A[0-9a-fA-F]*\z/ =~ d && [d].pack("H*")
                when :hexuc
                  /\A[0-9A-F]*\z/ =~ d && [d].pack("H*")
                when :hexlc
                  /\A[0-9a-f]*\z/ =~ d && [d].pack("H*")
                else fail "Cannot happen"
                end
            ) && validate1(decoded.b, control)
          )
      when :printf
        ann if String === d && (
                 ok, fmt, *v = extract_arg0(control)
                 if ok && String == fmt[1]
                   fmt = fmt[0]
                   # warn "** ok #{ok.inspect} fmt #{fmt.inspect} v #{v.inspect}"
                   decoded = d.scanf(fmt) # this is a bit too lenient, so let's do:
                   encode_again = fmt % decoded
                   if encode_again != d
                     warn "** fmt #{fmt.inspect} d #{d.inspect} decoded #{decoded.inspect} encode_again #{encode_again.inspect}"
                   else
                     validate1(decoded, [:array, *v])
                   end
                 end
               )
      when :within
        if validate1(d, control)
          ann
        else
          warn "*** #{d.inspect} meets #{target} but not #{control}"
          nil
        end
      when :and
        ann if validate1(d, control)
      else
        fail "Don't know yet how to validate against #{where}"
      end
    end
  when :prim
    # warn "validate prim WI #{where.inspect} #{d.inspect}"
    case where[1]
    when nil
      true
    when 0
      Integer === d && d >= 0 && d <= 0xffffffffffffffff
    when 1
      Integer === d && d < 0 && d >= -0x10000000000000000
    when 2
      String === d && d.encoding == Encoding::BINARY
    when 3
      String === d && d.encoding != Encoding::BINARY # cheat
    when 6
      if Integer === d
        t = 2               # assuming only 2/3 match an Integer
        if d < 0
          d = ~d
          t = 3
        end
        d = CBOR::Tagged.new(t, d == 0 ? "".b : d.digits(256).reverse!.pack("C*"))
      end
      CBOR::Tagged === d && (
        Integer === where[2] ? d.tag == where[2] : validate1a(d.tag, where[2])
      ) && validate1a(d.data, where[3])
    when 7
      t, v = extract_value(where)
      if t
        v.eql? d
      else
        case where[2]
        when nil
          # XXX
          fail
        when 25, 26, 27
          Float === d
        else
          fail
        end
      end
    else
      fail "Can't validate prim #{where[1]} yet"
    end
  when :recurse
    name = where[1]
    rule = @stage1[name]
    if @recursion < MAX_RECURSE
      @recursion += 1
      r = validate1a(d, rule)
      @recursion -= 1
      r
    else
      fail "Deep recursion into #{name}: #{rule}, not yet implemented"
    end
  else
    @last_message = "Don't know how to validate #{where}"
    false
    # fail where
  end
end

#validate1a(d, where) ⇒ Object



954
955
956
957
958
959
960
961
962
963
# File 'lib/cddl.rb', line 954

def validate1a(d, where)
  if ann = validate1(d, where)
    here = [d, where]
    if Array === ann
      [here, *ann]
    else
      [here]
    end
  end
end

#validate_diagObject



785
786
787
# File 'lib/cddl.rb', line 785

def validate_diag
  [@last_data, @last_rule, @last_message]
end

#validate_forward(d, start, where) ⇒ Object



816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
# File 'lib/cddl.rb', line 816

def validate_forward(d, start, where)
  # warn ["valforw", d, start, where].inspect
  i = 0
  ann = []
  where[1..-1].each { |r|
    t, s, e, _k, v = r # XXX
    if t == :recurse_grpent
      rule = lookup_recurse_grpent(s)
      n, ann2 = validate_linear(d, start+i, rule)
      return [false, ann] unless n
      i += n
      ann.concat(ann2)
    elsif t == :grpchoice
      return [false, ann] unless r[1..-1].any? {|cand|
        n, ann2 = validate_forward(d, start+i, [:foo, *cand])
        if n
          i += n
          ann.concat(ann2)
        end}
    else
      fail r.inspect unless t == :member
      occ = 0
      while ((occ < e) && i != d.size && ((n, ann2 = validate_linear(d, start+i, v)); n))
        i += n
        occ += 1
        ann.concat(ann2)
      end
      if occ < s
        # warn "*** lme #{@last_message.encoding} #{@last_message}"
        # warn "*** #{"\noccur #{occ} < #{s}, not reached at #{i} in array #{d} for #{where}".encoding}"
        @last_message << "\noccur #{occ} < #{s}, not reached at #{i} in array #{d} for #{where}"
        return [false, ann]
      end
    end
  }
  # warn ["valforw>", i].inspect
  [i, ann]
end

#validate_linear(d, start, where) ⇒ Object

returns number of matches or false for breakage



856
857
858
859
860
861
862
863
864
865
866
# File 'lib/cddl.rb', line 856

def validate_linear(d, start, where)
  # warn ["vallin", d, start, where].inspect
  fail unless Array === d
  case where[0]
  when :grpent
    # must be inside an array with nested occurrences
    validate_forward(d, start, where)
  else
    (ann = validate1a(d[start], where)) ? [1, ann] : [false, ann]
  end
end

#validate_result(check) ⇒ Object



809
810
811
812
813
814
# File 'lib/cddl.rb', line 809

def validate_result(check)
  check || (
    @last_message << yield
    false
  )
end

#walk(rule, anno = nil, &block) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cddl.rb', line 95

def walk(rule, anno = nil, &block)
  r = []
  case rule
  when Array
    r.concat(Array(yield rule, anno))
    case rule[0]
    when :type1, :array, :map
      a = (rule.cbor_annotations rescue nil) if rule.size == 2
      a = a.first if a
      r.concat(rule[1..-1].map{|x| walk(x, a, &block)})
    when :grpchoice
      r.concat(rule[1..-1].map{|x| x.flat_map {|y| walk(y, &block)}})
    when :member
      r << walk(rule[3], &block)
      r << walk(rule[4], &block)
    when :anno
      r << walk(rule[2], &block)
      r << walk(rule[3], &block)
    else
      # p ["LEAF", rule[0]]
    end
  end
  r.compact
end