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.



41
42
43
44
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
# File 'lib/cddl.rb', line 41

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.



1070
1071
1072
# File 'lib/cddl.rb', line 1070

def ast
  @ast
end

Instance Method Details

#aprObject

for debugging



77
78
79
# File 'lib/cddl.rb', line 77

def apr                     # for debugging
  @abnf.parse_results
end

#ast_debugObject



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

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

#cname(s) ⇒ Object



116
117
118
# File 'lib/cddl.rb', line 116

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

#defines(prefix) ⇒ Object

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



121
122
123
124
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
# File 'lib/cddl.rb', line 121

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_array(t) ⇒ Object



648
649
650
651
652
653
654
655
656
# File 'lib/cddl.rb', line 648

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



658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
# File 'lib/cddl.rb', line 658

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



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

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



285
286
287
288
# File 'lib/cddl.rb', line 285

def generate
  @recursion = 0
  generate1(rules)
end

#generate1(where, inmap = false) ⇒ Object



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

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
      CBOR::Tagged.new(where[2], 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
      32.times do
        content = generate1(target)
        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
      unless target == [:prim, 2]
        fail "Don't know yet how to generate #{where}"
      end
      content = CBOR::encode(generate1(control))
      if conop == :cborseq
        # 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 "Generated .cborseq sequence for #{where} not an array"
            end
        content[0...n] = ''
      end
      content
    when :within, :and
      32.times do
        content = generate1(target)
        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



256
257
258
259
260
261
# File 'lib/cddl.rb', line 256

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



760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
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
832
833
834
835
836
837
838
839
840
841
842
843
844
# File 'lib/cddl.rb', line 760

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



263
264
265
266
267
# File 'lib/cddl.rb', line 263

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



180
181
182
183
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
# File 'lib/cddl.rb', line 180

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



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

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

#validate(d, warn = true) ⇒ Object



681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# File 'lib/cddl.rb', line 681

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



859
860
861
862
863
864
865
866
867
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
953
954
955
956
957
958
959
960
961
962
963
964
965
966
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
# File 'lib/cddl.rb', line 859

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 :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 && 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



846
847
848
849
850
851
852
853
854
855
# File 'lib/cddl.rb', line 846

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

#validate_diagObject



677
678
679
# File 'lib/cddl.rb', line 677

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

#validate_forward(d, start, where) ⇒ Object



708
709
710
711
712
713
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
742
743
744
745
# File 'lib/cddl.rb', line 708

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



748
749
750
751
752
753
754
755
756
757
758
# File 'lib/cddl.rb', line 748

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



701
702
703
704
705
706
# File 'lib/cddl.rb', line 701

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

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cddl.rb', line 91

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