Class: CDDL::Parser

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

Defined Under Namespace

Classes: BackTrack

Constant Summary collapse

REGEXP_FOR_STRING =

Memoize a bit here

Hash.new {|h, k|
  h[k] = Regexp.new("\\A#{k}\\z")
}
VALUE_TYPE =
{text: 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.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cddl.rb', line 26

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 = []
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



873
874
875
# File 'lib/cddl.rb', line 873

def ast
  @ast
end

Instance Method Details

#aprObject

for debugging



60
61
62
# File 'lib/cddl.rb', line 60

def apr                     # for debugging
  @abnf.parse_results
end

#ast_debugObject



64
65
66
# File 'lib/cddl.rb', line 64

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

#cname(s) ⇒ Object



99
100
101
# File 'lib/cddl.rb', line 99

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

#defines(prefix) ⇒ Object

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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
# File 'lib/cddl.rb', line 104

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



529
530
531
532
533
534
535
536
537
# File 'lib/cddl.rb', line 529

def extract_value(t)
  if vt = VALUE_TYPE[t[0]]
    [true, t[1], vt]
  elsif v = SIMPLE_VALUE[t]
    v
  else
    [false]
  end
end

#generateObject



252
253
254
255
# File 'lib/cddl.rb', line 252

def generate
  @recursion = 0
  generate1(rules)
end

#generate1(where, inmap = false) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
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
# File 'lib/cddl.rb', line 257

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]
    en = [where[2], [st, 4].max].min # truncate to 4 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) { generate1(vr[1], true) }.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 where[1]
    when :size
      should_be_int = generate1(control)
      unless (target == [:prim, 2] || target == [:prim, 0]) && 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)
      if target == [:prim, 0]
        # silently restrict to what we can put into a uint
        s[0...8].bytes.inject(0) {|a, b| a << 8 | b }
      else
        s
      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 :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 where[1]
                  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 :cbor, :cborseq
      unless target == [:prim, 2]
        fail "Don't know yet how to generate #{where}"
      end
      content = CBOR::encode(generate1(control))
      if where[1] == :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 where[1] == :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



239
240
241
242
243
244
# File 'lib/cddl.rb', line 239

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



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

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}"
        # 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}"
        keys = d_check.keys
        ta, keys = keys.partition{ |key| validate1(key, k)}
        # XXX check ta.size against s/e
        ta.all? { |val|
          if (ann2 = validate1a(d[val], v)) && 
             d_check.delete(val) {:not_found} != :not_found
            anno.concat(ann2)
          end
        }
      end
      end
    else
      fail "Cannot validate #{t} in maps yet #{r}" # MMM
    end
  }
end

#rulesObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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
# File 'lib/cddl.rb', line 163

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



68
69
70
71
72
# File 'lib/cddl.rb', line 68

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

#validate(d, warn = true) ⇒ Object



543
544
545
546
547
548
549
550
551
552
553
# File 'lib/cddl.rb', line 543

def validate(d, warn=true)
  @recursion = 0
  result = validate1a(d, rules)
  unless result
    if warn
      warn "CDDL validation failure (#{result.inspect} for #{d.inspect}):"
      PP::pp(validate_diag, STDERR)
    end
  end
  result
end

#validate1(d, where) ⇒ Object



707
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
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
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
# File 'lib/cddl.rb', line 707

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 :text, :int, :float, :bytes
    _, v = extract_value(where)
    [] if d == v
  when :range
    [] if where[2] === d && where[1].include?(d)
  when :anno
    target = where[2]
    if ann = validate1a(d, target)
      control = where[3]
      case where[1]
      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 :lt, :le, :gt, :ge, :eq, :ne
        op = OPERATORS[where[1]]
        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 :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
      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



694
695
696
697
698
699
700
701
702
703
# File 'lib/cddl.rb', line 694

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

#validate_diagObject



539
540
541
# File 'lib/cddl.rb', line 539

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

#validate_forward(d, start, where) ⇒ Object



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

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
        @last_message = "occur not reached 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



600
601
602
603
604
605
606
607
608
609
610
# File 'lib/cddl.rb', line 600

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



555
556
557
558
559
560
# File 'lib/cddl.rb', line 555

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

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cddl.rb', line 74

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