Class: CDDL

Inherits:
Object
  • Object
show all
Defined in:
lib/cddlc.rb,
lib/writer/cddl-writer.rb,
lib/processor/cddl-visitor.rb,
lib/processor/cddl-expander.rb,
lib/processor/cddl-constants.rb,
lib/processor/cddl-undefined.rb,
lib/processor/cddl-flattening.rb

Defined Under Namespace

Classes: MissingGenericError

Constant Summary collapse

DATA_DIR =
Pathname.new(__FILE__) + "../../data/"
CDDL_INCLUDE_PATH =

empty string is for CDDL::DATA_DIR

ENV["CDDL_INCLUDE_PATH"] || ".:"
SAFE_FN =
/\A[-._a-zA-Z0-9]+\z/
IMPINC =
/\A(?:import|include)\z/
IDENTIFIER_RE =
/\A[A-Za-z@_$]([-.]*[A-Za-z@_$0-9])*\z/
INT_RE =
/\A0|[-]?[1-9][0-9]*\z/
RULE_OP_TO_CHOICE =
{"/=" => ["tcho", "tadd"], "//=" => ["gcho", "gadd"]}
CHOICERULES =
RULE_OP_TO_CHOICE.values.flatten
RULE_OUTER_TO_CHOICE =
{"tadd" => ["tcho", "tadd"], "gadd" => ["gcho", "gadd"]}
MOGRIFIED_ID_RE =
/\A\$\.[A-Za-z@_$]([-.]*[A-Za-z@_$0-9])*\z/
@@parser =
CDDLGRAMMARParser.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ast_, directives_ = []) ⇒ CDDL

Returns a new instance of CDDL.



226
227
228
229
230
231
# File 'lib/cddlc.rb', line 226

def initialize(ast_, directives_ = [])
  @ast = ast_
  @tree = ast.ast
  @rules = nil                # only fill in if needed
  @directives = directives_
end

Instance Attribute Details

#astObject

Returns the value of attribute ast.



225
226
227
# File 'lib/cddlc.rb', line 225

def ast
  @ast
end

#directivesObject

Returns the value of attribute directives.



225
226
227
# File 'lib/cddlc.rb', line 225

def directives
  @directives
end

#treeObject

Returns the value of attribute tree.



225
226
227
# File 'lib/cddlc.rb', line 225

def tree
  @tree
end

Class Method Details

.cddl_include_pathObject



13
14
15
# File 'lib/cddlc.rb', line 13

def self.cddl_include_path
  CDDL_INCLUDE_PATH.split(":", -1).map {_1 == "" ? CDDL::DATA_DIR : Pathname.new(_1)}
end

.from_cddl(s) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cddlc.rb', line 61

def self.from_cddl(s)
  ast = @@parser.parse s
  if !ast
    fail self.reason(@@parser, s)
  end
  if $options.cddl2
    directives = s.lines.grep(/^;# /).map(&:chomp).map{|l| l.sub(/^;#\s+/, '').split(/\s+/)}
    # puts directives.to_yaml
  end

  ret = CDDL.new(ast, directives)

  if $options.cddl2
    ret.perform_directives
  end
  ret
end

.read_from_include_path(fn) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cddlc.rb', line 43

def self.read_from_include_path(fn)
  io = nil
  CDDL::cddl_include_path.each do |path|
    begin
      io = (path + fn).open
      break
    rescue Errno::ENOENT
      next
    end
  end
  io
end

.reason(parser, s) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/cddlc.rb', line 17

def self.reason(parser, s)
  reason = [parser.failure_reason]
  parser.failure_reason =~ /^(Expected .+) after/m
  reason << "#{$1.gsub("\n", '<<<NEWLINE>>>')}:" if $1
  if line = s.lines.to_a[parser.failure_line - 1]
    reason << line
    reason << "#{'~' * (parser.failure_column - 1)}^"
  end
  reason.join("\n")
end

Instance Method Details

#cddl_add_used_by(prod, used) ⇒ Object



21
22
23
24
25
# File 'lib/processor/cddl-undefined.rb', line 21

def cddl_add_used_by(prod, used)
  cddl_extract_names(prod) do |name|
    used[name] = true
  end
end

#cddl_extract_names(prod, &name_used) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/processor/cddl-undefined.rb', line 6

def cddl_extract_names(prod, &name_used)
  visit(prod) do |p|
    case p
      in ["gen", name, *_gen_args]
        name_used.call(name)
        false
      in ["name", name]
        name_used.call(name)
        false
      else
        false
    end
  end
end

#cddl_undefinedObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/processor/cddl-undefined.rb', line 27

def cddl_undefined
  # currently only works on expanded...
  cddl2 = self.deep_clone                # needs deep-clone
  begin
    cddl2.expand_generics
  rescue CDDL::MissingGenericError => e
    return [e.name] # make this expandable first by pulling missing generic
  end
  used = {}
  cddl2.rules.each do |k, v|
    fail unless String === k
    cddl2.cddl_add_used_by(v, used)
  end
  used.keys.reject {|name| cddl2.rules[name] || prelude.rules[name]}
end

#check_socket(name, nv, right, wrong, wrong_plain, wrong_kind, kind) ⇒ Object



307
308
309
310
311
312
313
314
315
316
# File 'lib/cddlc.rb', line 307

def check_socket(name, nv, right, wrong, wrong_plain, wrong_kind, kind)
  unless nv[0] == right
    if nv[0] == wrong
      flaw = "#{wrong_kind} choice #{write_rhs([wrong_plain, *nv[1..-1]], 2.1)} in"
    else
      flaw = "plain assignment of #{write_rhs(nv, 2.1)} to"
    end
    warn "** warning: #{flaw} #{kind} socket #{name}"
  end
end

#constant_evaluate(rhs) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/processor/cddl-constants.rb', line 5

def constant_evaluate(rhs)
  case rhs
  in ["number", n]
    [true, eval(n)]
  in ["text", n]
    [true, n]
  in ["name", n]
    if nv = rules[n]
      constant_evaluate(nv)
    end
  else
    [false]
  end
end

#deep_cloneObject



233
234
235
# File 'lib/cddlc.rb', line 233

def deep_clone
  Marshal.load(Marshal.dump(self))
end

#expand_genericsObject



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

def expand_generics
  @gen = {}
  rules.each do |name, prod|
    case prod
    in ["parm", parmnames, type]
      if prod[0] == "parm"
        @gen[name] = [parmnames, type]
      end
    else
    end
  end
  warn "@gen = #{@gen.inspect}" if $options.verbose
  @gen.each do |k, v|
    parmnames = v[0]
    fail unless rules[k] == ["parm", parmnames, v[1]]
    rules.delete(k)
  end
  @new_rules = {}
  rules.each do |name, prod|
    fail if Array === name
    @new_rules[name] = expand_prod(prod)
  end
  warn "@new_rules = #{@new_rules.inspect}" if $options.verbose
  @rules = @new_rules
end

#expand_prod(prod) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/processor/cddl-expander.rb', line 36

def expand_prod(prod)
  visit(prod) do |p, &block|
    case p
    in ["gen", name, *gen_args]
      [true, gen_apply(name, gen_args, &block)]
    else
      [false]
    end
  end
end

#extract_constantsObject



19
20
21
22
23
24
25
26
# File 'lib/processor/cddl-constants.rb', line 19

def extract_constants
  nr = {}
  rules.each do |k, v|
    isconstant, value = constant_evaluate(v)
    nr[k] = value if isconstant
  end
  nr
end

#flatteningObject



95
96
97
98
99
# File 'lib/processor/cddl-flattening.rb', line 95

def flattening
  symtab = flattening_occurrences
  PP.pp(["*** SYMTAB", symtab], STDERR) if $options.verbose
  rules.replace(flattening_replace(symtab))
end

#flattening_key_name(key, value, env = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/processor/cddl-flattening.rb', line 6

def flattening_key_name(key, value, env = nil)
  case key
  in ["enum", ["mem", _cut, ["text", IDENTIFIER_RE => text], _]]
    [false, text]
  in ["text", IDENTIFIER_RE => text]
    [false, text]
  in ["number", INT_RE => intval] if env
    [true, "$.#{env}$#{intval}"]
  else
    [false]
  end
end

#flattening_mogrify(name, prod, symtab, alias_rules) ⇒ Object



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
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/processor/cddl-flattening.rb', line 44

def flattening_mogrify(name, prod, symtab, alias_rules)
  step1 = visit(prod) do |here|
      case here
      in ["mem", cut, key, value]
        ### mogrify
        labeled, keyname = flattening_key_name(key, value, name)
        if keyname
          new_name =
            unless labeled
              syment = symtab[keyname]
              fail keyname unless Array === syment
              if syment.size == 1
                "$.#{keyname}"
              else
                "$.#{name}$#{keyname}"
              end
            else
              keyname
            end
          new_value2 = flattening_mogrify(new_name, value, symtab, alias_rules)
          new_name2 = new_name
          n = 0
          while ar = alias_rules[new_name2] and ar != new_value2
            n += 1
            new_name2 = new_name << "@" << n.to_s
          end
          alias_rules[new_name2] = new_value2
          [true, ["mem", cut, key, ["name", new_name2]]]
        end
      else
        false
      end
    end
  step2 = visit(step1) do |here|
    case here
    in ["enum", ["mem", _cut, ["text", IDENTIFIER_RE], ["name", MOGRIFIED_ID_RE => new_name2]]]
      [true, ["name", new_name2]]
    else
      false
    end
  end
  step2
end

#flattening_occurrencesObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/processor/cddl-flattening.rb', line 18

def flattening_occurrences
  symtab = Hash.new { |h, k| h[k] = [] }
  rules.each do |name, prod|
    visit(prod) do |here|
      case here
      in ["mem", _cut, key, value]
        _labeled, keyname = flattening_key_name(key, value, false)
        if keyname
          symtab[keyname] << [name, value]
          false
        end
      else
        false
      end
    end
  end
  symtab_replacements = Hash[symtab.map do |k, v|
    s = Set[*v.map{_2}]
    # warn "** #{k} #{s.inspect}"
    if s.size == 1
      [k, [[v.map{|k, v| k}.join("|"), s.first]]]
    end
  end.compact]
  # warn "** symtab_replacements: #{symtab_replacements.inspect}" if $options.verbose
  symtab.merge(symtab_replacements)
end

#flattening_replace(symtab) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/processor/cddl-flattening.rb', line 87

def flattening_replace(symtab)
  alias_rules = {}
  new_rules = Hash[rules.map do |name, prod|
                     [name,
                      flattening_mogrify(name, prod, symtab, alias_rules)]
                   end]
  new_rules.merge(alias_rules)
end

#gen_apply(gen_name, gen_args, &block) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/processor/cddl-expander.rb', line 29

def gen_apply(gen_name, gen_args, &block)
  gen_parms, gen_prod = @gen[gen_name]
  unless gen_parms
    fail MissingGenericError.new(gen_name, gen_args)
  end
  substitute(gen_prod, gen_parms, gen_args, &block)
end

#merge_to_rule(name, val, cho) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/cddlc.rb', line 237

def merge_to_rule(name, val, cho)
      @rules[name] = nv =
        if (old = @rules[name]) && old != val
          fail "overwriting #{write_rule(name, old)} ...with... #{write_rule(name, val)}" unless cho
          if Array === old && cho.include?(old[0])
            if cho.include?(val[0])
              old.dup.append(*val[1..-1])
            else
              old.dup << val
            end
          else
            #  can't put an old "g/tadd" into a new "t/gcho"
            fail "can't add #{write_rule(name, [cho[1], val])} ...to... #{write_rule(name, old)}" if CHOICERULES.include?(old[0])
            [cho[0], old, val] # old might need to be packaged for gcho
          end
        else
          if cho
            if val[0] == cho[0]
              [cho[1], *val[1..-1]]
            else
              [cho[1], val]
            end
          else
            val
          end
        end
      if name[0..1] == "$$"
        check_socket(name, nv, "gadd", "tadd", "tcho", "type", "group")
      elsif name[0] == "$"
        check_socket(name, nv, "tadd", "gadd", "gcho", "group", "type")
      end
end

#perform_directivesObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
162
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
# File 'lib/cddlc.rb', line 79

def perform_directives
  directives.each do |di|
    preferred_tag = nil
    from = nil
    case di
    in [IMPINC => dir, SAFE_FN => docref, "as", SAFE_FN => preferred_tag]
    in [IMPINC => dir, SAFE_FN => docref]
    in [IMPINC => dir, *from, "from", SAFE_FN => docref, "as", SAFE_FN => preferred_tag]
    in [IMPINC => dir, *from, "from", SAFE_FN => docref]
    else
      warn "** Can't parse directive »#{di.join(" ")}«"
      next
    end
    if from
      from = from.map do |name|
        name.chomp!(",")
        if IDENTIFIER_RE === name
          name
        else
          warn "*** invalid identifier #{name.inspect} ignored in »#{di.join(' ')}«"
          nil
        end
      end.compact
    end
    warn "FROM #{from.inspect}" if $options.verbose
    warn "PREFERRED_TAG #{preferred_tag.inspect}" if $options.verbose
    warn "DOCREF #{docref.inspect}" if $options.verbose

    fn = docref.downcase << ".cddl"
    io = CDDL.read_from_include_path(fn)
    unless io
      warn "** #{dir} file #{fn} not found in #{CDDL::cddl_include_path.map(&:to_s)}"
      next
    end

    include_file = io.read
    included_cddl = CDDL.from_cddl(include_file)
    if preferred_tag
      included_cddl = included_cddl.deep_clone # needed?
      renamed_names = included_cddl.rules.keys
      name_rename = Hash[
        renamed_names.map { |o|
          n = "#{preferred_tag}.#{o}"
          warn "** Warning: renamed name #{n} already in #{fn}" if included_cddl.rules[n]
          [o, n]}]
      included_cddl.rename(name_rename)
    end

    case dir
    in "import"
      warn "** IMPORTING #{from && from.join(", ") << " from "}#{fn}" if $options.verbose

      must_be_found = false
      to_be_imported = if from
                         if preferred_tag
                           from.map{ |k|
                             if k =~ /\A#{preferred_tag}[.]/
                               k
                             elsif nn = name_rename[k]
                               if old = store_rule(k, v = ["name", nn])
                                 warn "*** autogenerated rule #{k} = #{v} would overwrite #{old}"
                               end
                               nn
                             else
                               warn_error "*** imported rule #{k.inspect} not in #{fn}"
                             end
                           }.compact
                         else
                           must_be_found = true
                           from
                         end
                       else
                         cddl_undefined
                       end
      loop do
        # p to_be_imported
        got_more = false
        to_be_imported.each do |name|
          if rule = included_cddl.rules[name]
            rules[name] = rule
            warn "IMPORTED #{name} from #{fn}" if $options.verbose
            got_more = true
          elsif must_be_found
            warn_error "*** imported rule #{name.inspect} not in #{fn}"
          end
        end
        break unless got_more
        recently_imported = to_be_imported
        to_be_imported = cddl_undefined # XXX square...
        warn "TO IMPORT #{to_be_imported.inspect}" if $options.verbose
        if to_be_imported == recently_imported
          fail "No progress importing #{to_be_imported.inspect} from #{fn} (generics issue?)"
        end
        must_be_found = false
      end
      if preferred_tag
        to_be_imported.each do |name|
          warn "** Warning: undefined reference #{name} without namespace prefix is defined in namespaced imported module #{fn}" if name_rename[name]
          ### instead just add rule...
        end
      end
    in "include"
      if from
        from = Set[*from]
      end
      warn "** INCLUDING #{from && from.join(", ") << " from "}#{fn}" if $options.verbose
      included_cddl.rules.each do |k, v|
        if from
          next unless from.include?(k)
          from.delete(k)
        end
        if old = store_rule(k, v)
          warn "*** included rule #{k} = #{v} would overwrite #{old}"
        end
      end
      if from && !from.empty?
        warn "*** could not find #{from.join(", ")} in #{fn}"
      end
    end
  end
end

#prec_check(inner, targetprec, prec, indent_s) ⇒ Object

precedence: 0: // gcho 1: , seq 2: / tcho -> (type1) 3: (type1) .xxx op -> type2 4: type2



20
21
22
23
24
25
26
# File 'lib/writer/cddl-writer.rb', line 20

def prec_check(inner, targetprec, prec, indent_s)
  if targetprec > prec
    "(#{inner.gsub("\n", "\n" << indent_s)})" # XXX embedded byte strings
  else
    inner
  end
end

#preludeObject



318
319
320
321
322
323
# File 'lib/cddlc.rb', line 318

def prelude
  if @prelude.nil?
    @prelude = CDDL.from_cddl(File.read(DATA_DIR + "prelude.cddl"))
  end
  @prelude
end

#rename(rename_map) ⇒ Object

(keeps only renamed rules)



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cddlc.rb', line 29

def rename(rename_map)
  rules.replace(
    Hash[rename_map.map do |o, n|
           [n, visit(rules[o]) do |prod|
              case prod
              in ["name", *] | ["gen", *]
                prod[1] = rename_map[prod[1]] || prod[1]
              else
              end
              false
            end]
         end])
end

#rulesObject



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

def rules
  if @rules.nil?              # memoize
    @rules = {}
    fail unless @tree.first == "cddl"
    @tree[1..-1].each do |x|
      op, name, val, rest = x
      cho = RULE_OP_TO_CHOICE[op]
      fail rest if rest
      fail name unless Array === name
      case name[0]
      when "name"
        fail unless name.size == 2
        name = name[1]
      when "gen"
        parmnames = name[2..-1]
        name = name[1]        # XXX update val with parm/arg
        val = ["parm", parmnames,
               visit(val) do |p|
                 case p
                 in ["name", nm]
                   if ix = parmnames.index(nm)
                     [true, ["arg", ix]]
                   end
                 else
                   false
                 end
               end]
      else
        fail name
      end
      merge_to_rule(name, val, cho)
    end
    # warn "** rules #{rules.inspect}"
  end
  @rules
end

#store_rule(k, v) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/cddlc.rb', line 205

def store_rule(k, v)
  if old = rules[k]
    if old != v
      if cho = RULE_OUTER_TO_CHOICE[v[0]]
        merge_to_rule(k, v, cho)
      else
        return old              # error
      end
    end
  else
    rules[k] = v
  end
  nil                         # OK
end

#substitute(prod, parms, subs, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/processor/cddl-expander.rb', line 15

def substitute(prod, parms, subs, &block)
  visit(prod) do |p, &block1|
    case p
    in ["gen", name, *gen_args] # XXX
      [true, gen_apply(name, gen_args, &block1)]
    in ["arg", num]
      if replacement = subs[num]
        [true, visit(expand_prod(replacement), &block)]
      end
    else
      false
    end
  end
end

#to_sObject



128
129
130
# File 'lib/writer/cddl-writer.rb', line 128

def to_s
  rules.map {|k, v| write_rule(k, v) }.join("\n")
end

#visit(prod, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/processor/cddl-visitor.rb', line 7

def visit(prod, &block)
  done, ret = block.call(prod, &block)
  if done
    return ret
  end

  case prod
  in ["parm", parmnames, prod]
    ["parm", parmnames, visit(prod, &block)]
  in ["gen", name, *types]
    ["gen", name, *visit_all(types, &block)]
  in ["op", op, *prods]
    ["op", op, *visit_all(prods, &block)]
  in ["map", prod]
    ["map", visit(prod, &block)]
  in ["ary", prod]
    ["ary", visit(prod, &block)]
  in ["gcho", *prods]
    ["gcho", *visit_all(prods, &block)]
  in ["tcho", *prods]
    ["tcho", *visit_all(prods, &block)]
  in ["gadd", *prods]
    ["gadd", *visit_all(prods, &block)]
  in ["tadd", *prods]
    ["tadd", *visit_all(prods, &block)]
  in ["seq", *prods]
    ["seq", *visit_all(prods, &block)]
  in ["enum", prod]
    ["enum", visit(prod, &block)]
  in ["unwrap", prod]
    ["unwrap", visit(prod, &block)] # XXX, this may need to be bottled in a rule
  in ["prim", prod, *prods]
    ["prim", visit(prod, &block), *visit_all(prods, &block)]
  in ["mem", cut, *prods]
    ["mem", cut, *visit_all(prods, &block)]
  in ["rep", s, e, prod]
    ["rep", s, e, visit(prod, &block)]
  else
    prod
  end
end

#visit_all(prod_array, &block) ⇒ Object



4
5
6
# File 'lib/processor/cddl-visitor.rb', line 4

def visit_all(prod_array, &block)
  prod_array.map {|prod| visit(prod, &block)}
end

#warn_error(s) ⇒ Object



220
221
222
223
# File 'lib/cddlc.rb', line 220

def warn_error(s)
  warn s
  @error = true
end

#write_lhs(k, parmnames) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/writer/cddl-writer.rb', line 5

def write_lhs(k, parmnames)
  if parmnames
    "#{k}<#{parmnames.join(", ")}>"
  else
    k
  end
end

#write_rhs(v, targetprec = 0, indent = 0, pn = []) ⇒ Object



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/writer/cddl-writer.rb', line 28

def write_rhs(v, targetprec = 0, indent = 0, pn = [])
  # pn = parmnames
#    warn "** #{v.inspect}"
  indent_p = "  " * indent
  indent += 1
  indent_s = "  " * indent
  prec, ret =
  case v
  in ["parm", parmnames, type]
    [4, write_rhs(type, 2, indent, parmnames)]
  in ["arg", Integer => num]
    [4, pn[num] || "id$#{num}"]
  in ["name", id]
    [4, id]
  in ["gen", id, *parms]  # oops -- namep vs. namea; ouch
    [4, "#{id}<#{parms.map{write_rhs(_1, 2, indent, pn)}.join(", ")}>"]
  in ["tcho" | "tadd", *types]
    [2.1, types.map{write_rhs(_1, 3, indent, pn)}.join(" / ")]
  in ["gcho" | "gadd", *groups]
    [0, groups.map{write_rhs(_1, 2, indent, pn)}.join(" // ")]
  in ["op", op, l, r]
    [3, "#{write_rhs(l, 4, indent, pn)} #{op} #{write_rhs(r, 4, indent, pn)}"]
    # 3->4: work around cddl tool limitation
  in ["map", group]
    [3, "{#{write_rhs(group, 0, indent, pn)}}"] # 4->3: work around cddl tool limitation
  in ["ary", group]
    [3, "[#{write_rhs(group, 0, indent, pn)}]"] # 4->3: work around cddl tool limitation
  in ["unwrap", namep]
    [4, "~#{write_rhs(namep, 4, indent, pn)}"]
  in ["enum", ["name", _name] => namep]
    [4, "&#{write_rhs(namep, 4, indent, pn)}"]
  in ["enum", ["gen", _name, *types] => namep]
    [4, "&#{write_rhs(namep, 4, indent, pn)})"]
  in ["enum", group]
    [4, "&(#{write_rhs(group, 0, indent, pn)})"]
  in ["prim"]
    [4, "#"]
  in ["prim", maj]
    [4, "##{maj}"]
  in ["prim", maj, Integer => min]
    [4, "##{maj}.#{min}"]
  in ["prim", 7, Array => min]
    [4, "##{maj}.<#{write_rhs(min, 0, indent, pn)}>"]
  in ["prim", 6, Integer => tag, type]
    [4, "#6.#{tag}(#{write_rhs(type, 0, indent, pn)})"]
  in ["prim", 6, Array => tag, type]
    [4, "#6.<#{write_rhs(tag, 0, indent, pn)}>(#{write_rhs(type, 0, indent, pn)})"]
  in ["prim", 6, nil, type]
    [4, "#6(#{write_rhs(type, 0, indent, pn)})"]
# prim: extension for #6.<i>(t)
  in ["seq", *groups]
    case groups.size
    when 0; [4, ""]
    # when 1; "#{write_rhs(g[0], targetprec, indent, pn)}"
    else
      [1, "\n#{indent_p}#{groups.map{write_rhs(_1, 1, indent, pn)}.join(",\n#{indent_p}")},\n"]
    end
  in ["rep", s, e, group]
    occur = case [s, e]
            in [1, 1];     ""
            in [0, 1];     "? "
            in [0, false]; "* "
            in [1, false]; "+ "
            else
              "#{s}*#{e || ""}"
            end
    [1, "#{occur}#{write_rhs(group, 2, indent, pn)}"]
  in ["mem", false, nil, t2]
    [2, write_rhs(t2, 2, indent, pn)]
  in ["mem", true, ["text", IDENTIFIER_RE => bareword], t2]
    [2, "#{bareword}: #{write_rhs(t2, 2, indent, pn)}"]
  in ["mem", true, ["number", INT_RE => bareword], t2]
    [2, "#{bareword}: #{write_rhs(t2, 2, indent, pn)}"]
  in ["mem", cut, t1, t2]
    [2, "#{write_rhs(t1, 3, indent, pn)} #{cut ? "^" : ""}=> #{write_rhs(t2, 2, indent, pn)}"]
    # 2->3: work around cddl tool limitation
  in ["bytes", t]
    [4, t]                    # XXX not very clean
  in ["text", t]
    [4, "\"#{t}\""]           # XXX escape
  in ["number", t]
    [4, t.to_s]
  end
  prec_check(ret, targetprec, prec, indent_s)
end

#write_rule(k, v) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/writer/cddl-writer.rb', line 114

def write_rule(k, v)
    parmnames = false
    assign = "="
    case v
    in ["tadd", *rest]
      assign = "/="
    in ["gadd", *rest]
      assign = "//="
    in ["parm", parmnames, _type]
    else
    end
    "#{write_lhs(k, parmnames)} #{assign} #{write_rhs(v, 2.1)}" # 2: parenthesize groups
end