cddlc 0.1.5 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aa1feed69996a1e91c43877cfbe3e68c6b935039976ae95db312fef99a0e8bbd
4
- data.tar.gz: 746c4b175cb78c502ab731e43672841663cfb4a4159bcf387f8f69c9f34e347a
3
+ metadata.gz: 2d56a7033911531ad73966b0807a1c6b9d9c09b9b09638d43274337e067a02fa
4
+ data.tar.gz: 609ab555ddc49c64a1dde396b99e01b2c79fd9a4fb63b1efb19ccd0ba3a311c1
5
5
  SHA512:
6
- metadata.gz: c9b58614695995f0336496734a3684992ddd6f7a1be0dfcbc0451f45afde7e78eb2a2f89ce60e9c5e0c09a1c0e7e8856cc5e5658fe3fe78bc22bc08c4897c4d4
7
- data.tar.gz: d19ca545ca39eb87af227651438755c13475dd75bd29ef11a6a6327aac42e9c4a69fe0e5322a291e7819903d89c4de1296e1605934bce9b154dcb7ef6563c151
6
+ metadata.gz: c704b7fbc908dd1449a6c267936bea906f689852d54b54291fc8a2bc4ca62c39b2e947223da42069343e5d1b28e997907ffeb451d528c5436a7a96f82de17498
7
+ data.tar.gz: 8e9f965b91b31eae2986a5e4c621dc0048bb64308017ec917d1a7b90853b9c6322674eaf4ef034905c8f0c555791e5664112cb905b03255fe72b857f57830f67
data/bin/cddlc CHANGED
@@ -55,6 +55,9 @@ begin
55
55
  opts.on("-x", "--[no-]expand", "Expand generics") do |v|
56
56
  $options.expand_generics = v
57
57
  end
58
+ opts.on("-f", "--[no-]flattening", "Flattening") do |v|
59
+ $options.flattening = v
60
+ end
58
61
  opts.on("-u", "--[no-]undefined", "Note undefined names") do |v|
59
62
  $options.note_undefined = v
60
63
  end
@@ -82,6 +85,11 @@ rescue Exception => e
82
85
  exit 1
83
86
  end
84
87
 
88
+ if ($options.expand_generics || $options.flattening) && !$options.rules
89
+ warn "** can't expand or flatten in tree; adding -r flag"
90
+ $options.rules = true
91
+ end
92
+
85
93
  cddl_file = ""
86
94
  if $options.start
87
95
  cddl_file << "$.start.$ = #{$options.start}\n"
@@ -117,15 +125,20 @@ else
117
125
  cddl_file << ARGF.read
118
126
  end
119
127
 
128
+
120
129
  cddl = CDDL.from_cddl(cddl_file)
121
130
  result = if $options.rules
122
131
  if $options.expand_generics
123
132
  require_relative "../lib/processor/cddl-expander.rb"
124
133
  cddl.expand_generics
125
134
  end
135
+ if $options.flattening
136
+ require_relative "../lib/processor/cddl-flattening.rb"
137
+ cddl.flattening
138
+ warn "*** done flattening" if $options.verbose
139
+ end
126
140
  cddl.rules
127
141
  else
128
- warn "** can't expand in tree; use -r flag as well" if $options.expand_generics
129
142
  cddl.tree
130
143
  end
131
144
 
data/cddlc.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "cddlc"
3
- s.version = "0.1.5"
3
+ s.version = "0.1.7"
4
4
  s.summary = "CDDL (Concise Data Definition Language) converters and miscellaneous tools"
5
5
  s.description = %q{cddlc implements converters and miscellaneous tools for CDDL, RFC 8610}
6
6
  s.author = "Carsten Bormann"
data/lib/cddlc.rb CHANGED
@@ -98,9 +98,9 @@ class CDDL
98
98
  end
99
99
  end.compact
100
100
  end
101
- puts "FROM #{from.inspect}" if $options.verbose
102
- puts "PREFERRED_TAG #{preferred_tag.inspect}" if $options.verbose
103
- puts "DOCREF #{docref.inspect}" if $options.verbose
101
+ warn "FROM #{from.inspect}" if $options.verbose
102
+ warn "PREFERRED_TAG #{preferred_tag.inspect}" if $options.verbose
103
+ warn "DOCREF #{docref.inspect}" if $options.verbose
104
104
 
105
105
  fn = docref.downcase << ".cddl"
106
106
  io = CDDL.read_from_include_path(fn)
@@ -162,6 +162,7 @@ class CDDL
162
162
  end
163
163
  break unless got_more
164
164
  to_be_imported = cddl_undefined # XXX square...
165
+ warn "TO IMPORT #{to_be_imported.inspect}" if $options.verbose
165
166
  must_be_found = false
166
167
  end
167
168
  if preferred_tag
@@ -947,7 +947,7 @@ module CDDLGRAMMAR
947
947
  end
948
948
 
949
949
  module Type214
950
- def uint
950
+ def tag_number
951
951
  elements[1]
952
952
  end
953
953
  end
@@ -970,7 +970,7 @@ module CDDLGRAMMAR
970
970
  module Type216
971
971
  def ast
972
972
  el = elements[2].elements
973
- ["prim", 6, el && el[1].text_value.to_i, elements[5].ast]
973
+ ["prim", 6, el && el[1].ast, elements[5].ast]
974
974
  end
975
975
  end
976
976
 
@@ -1330,7 +1330,7 @@ module CDDLGRAMMAR
1330
1330
  end
1331
1331
  s48 << r49
1332
1332
  if r49
1333
- r50 = _nt_uint
1333
+ r50 = _nt_tag_number
1334
1334
  s48 << r50
1335
1335
  end
1336
1336
  if s48.last
@@ -1476,6 +1476,85 @@ module CDDLGRAMMAR
1476
1476
  r0
1477
1477
  end
1478
1478
 
1479
+ module TagNumber0
1480
+ def ast() text_value.to_i end
1481
+ end
1482
+
1483
+ module TagNumber1
1484
+ def type
1485
+ elements[1]
1486
+ end
1487
+
1488
+ end
1489
+
1490
+ module TagNumber2
1491
+ def ast() elements[1].ast end
1492
+ end
1493
+
1494
+ def _nt_tag_number
1495
+ start_index = index
1496
+ if node_cache[:tag_number].has_key?(index)
1497
+ cached = node_cache[:tag_number][index]
1498
+ if cached
1499
+ node_cache[:tag_number][index] = cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
1500
+ @index = cached.interval.end
1501
+ end
1502
+ return cached
1503
+ end
1504
+
1505
+ i0 = index
1506
+ r1 = _nt_uint
1507
+ r1.extend(TagNumber0)
1508
+ r1.extend(TagNumber0)
1509
+ if r1
1510
+ r1 = SyntaxNode.new(input, (index-1)...index) if r1 == true
1511
+ r0 = r1
1512
+ else
1513
+ i2, s2 = index, []
1514
+ if (match_len = has_terminal?("<", false, index))
1515
+ r3 = true
1516
+ @index += match_len
1517
+ else
1518
+ terminal_parse_failure('"<"')
1519
+ r3 = nil
1520
+ end
1521
+ s2 << r3
1522
+ if r3
1523
+ r4 = _nt_type
1524
+ s2 << r4
1525
+ if r4
1526
+ if (match_len = has_terminal?(">", false, index))
1527
+ r5 = true
1528
+ @index += match_len
1529
+ else
1530
+ terminal_parse_failure('">"')
1531
+ r5 = nil
1532
+ end
1533
+ s2 << r5
1534
+ end
1535
+ end
1536
+ if s2.last
1537
+ r2 = instantiate_node(SyntaxNode,input, i2...index, s2)
1538
+ r2.extend(TagNumber1)
1539
+ r2.extend(TagNumber2)
1540
+ else
1541
+ @index = i2
1542
+ r2 = nil
1543
+ end
1544
+ if r2
1545
+ r2 = SyntaxNode.new(input, (index-1)...index) if r2 == true
1546
+ r0 = r2
1547
+ else
1548
+ @index = i0
1549
+ r0 = nil
1550
+ end
1551
+ end
1552
+
1553
+ node_cache[:tag_number][start_index] = r0
1554
+
1555
+ r0
1556
+ end
1557
+
1479
1558
  def _nt_rangeop
1480
1559
  start_index = index
1481
1560
  if node_cache[:rangeop].has_key?(index)
@@ -42,7 +42,7 @@ class CDDL
42
42
  else
43
43
  end
44
44
  end
45
- p @gen if $options.verbose
45
+ warn "@gen = #{@gen.inspect}" if $options.verbose
46
46
  @gen.each do |k, v|
47
47
  parmnames = v[0]
48
48
  fail unless rules[k] == ["parm", parmnames, v[1]]
@@ -53,7 +53,7 @@ class CDDL
53
53
  fail if Array === name
54
54
  @new_rules[name] = expand_prod(prod)
55
55
  end
56
- p @new_rules if $options.verbose
56
+ warn "@new_rules = #{@new_rules.inspect}" if $options.verbose
57
57
  @rules = @new_rules
58
58
  end
59
59
  end
@@ -0,0 +1,99 @@
1
+ require_relative "../cddlc.rb"
2
+ require_relative "./cddl-visitor.rb"
3
+
4
+ class CDDL
5
+ ID_RE = /\A[A-Za-z@_$]([-.]*[A-Za-z@_$0-9])*\z/
6
+ MOGRIFIED_ID_RE = /\A\$\.[A-Za-z@_$]([-.]*[A-Za-z@_$0-9])*\z/
7
+ def flattening_key_name(key, value, env = nil)
8
+ case key
9
+ in ["enum", ["mem", ["text", ID_RE => text], _]]
10
+ [false, text]
11
+ in ["text", ID_RE => text]
12
+ [false, text]
13
+ in ["number", /\A0|[-]?[1-9][0-9]*\z/ => intval] if env
14
+ [true, "$.#{env}$#{intval}"]
15
+ else
16
+ [false]
17
+ end
18
+ end
19
+ def flattening_occurrences
20
+ symtab = Hash.new { |h, k| h[k] = [] }
21
+ rules.each do |name, prod|
22
+ visit(prod) do |here|
23
+ case here
24
+ in ["mem", key, value]
25
+ _labeled, keyname = flattening_key_name(key, value, false)
26
+ if keyname
27
+ symtab[keyname] << [name, value]
28
+ false
29
+ end
30
+ else
31
+ false
32
+ end
33
+ end
34
+ end
35
+ symtab_replacements = Hash[symtab.map do |k, v|
36
+ s = Set[*v.map{_2}]
37
+ # warn "** #{k} #{s.inspect}"
38
+ if s.size == 1
39
+ [k, [[v.map{|k, v| k}.join("|"), s.first]]]
40
+ end
41
+ end.compact]
42
+ # warn "** symtab_replacements: #{symtab_replacements.inspect}" if $options.verbose
43
+ symtab.merge(symtab_replacements)
44
+ end
45
+ def flattening_mogrify(name, prod, symtab, alias_rules)
46
+ step1 = visit(prod) do |here|
47
+ case here
48
+ in ["mem", key, value]
49
+ ### mogrify
50
+ labeled, keyname = flattening_key_name(key, value, name)
51
+ if keyname
52
+ new_name =
53
+ unless labeled
54
+ syment = symtab[keyname]
55
+ fail keyname unless Array === syment
56
+ if syment.size == 1
57
+ "$.#{keyname}"
58
+ else
59
+ "$.#{name}$#{keyname}"
60
+ end
61
+ else
62
+ keyname
63
+ end
64
+ new_value2 = flattening_mogrify(new_name, value, symtab, alias_rules)
65
+ if ar = alias_rules[new_name]
66
+ fail [:ALIAS_RULES, ar, new_value].inspect if ar != new_value2
67
+ else
68
+ alias_rules[new_name] = new_value2
69
+ end
70
+ [true, ["mem", key, ["name", new_name]]]
71
+ end
72
+ else
73
+ false
74
+ end
75
+ end
76
+ step2 = visit(step1) do |here|
77
+ case here
78
+ in ["enum", ["mem", ["text", ID_RE], ["name", MOGRIFIED_ID_RE => new_name]]]
79
+ [true, ["name", new_name]]
80
+ else
81
+ false
82
+ end
83
+ end
84
+ step2
85
+ end
86
+ def flattening_replace(symtab)
87
+ alias_rules = {}
88
+ new_rules = Hash[rules.map do |name, prod|
89
+ [name,
90
+ flattening_mogrify(name, prod, symtab, alias_rules)]
91
+ end]
92
+ new_rules.merge(alias_rules)
93
+ end
94
+ def flattening
95
+ symtab = flattening_occurrences
96
+ PP.pp(["*** SYMTAB", symtab], STDERR) if $options.verbose
97
+ rules.replace(flattening_replace(symtab))
98
+ end
99
+ end
@@ -31,7 +31,7 @@ class CDDL
31
31
  ["enum", visit(prod, &block)]
32
32
  in ["unwrap", prod]
33
33
  ["unwrap", visit(prod, &block)] # XXX, this may need to be bottled in a rule
34
- in ["prim", Array => prod, *prods]
34
+ in ["prim", prod, *prods]
35
35
  ["prim", visit(prod, &block), *visit_all(prods, &block)]
36
36
  in ["mem", *prods]
37
37
  ["mem", *visit_all(prods, &block)]
@@ -68,6 +68,8 @@ class CDDL
68
68
  [4, "##{maj}.#{min}"]
69
69
  in ["prim", 6, Integer => tag, type]
70
70
  [4, "#6.#{tag}(#{write_rhs(type, 0, indent, pn)})"]
71
+ in ["prim", 6, Array => tag, type]
72
+ [4, "#6.<#{write_rhs(tag, 0, indent, pn)}>(#{write_rhs(type, 0, indent, pn)})"]
71
73
  in ["prim", 6, nil, type]
72
74
  [4, "#6(#{write_rhs(type, 0, indent, pn)})"]
73
75
  # prim: extension for #6.<i>(t)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cddlc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carsten Bormann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-05 00:00:00.000000000 Z
11
+ date: 2023-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -101,6 +101,7 @@ files:
101
101
  - lib/parser/cddl-util.rb
102
102
  - lib/parser/cddlgrammar.rb
103
103
  - lib/processor/cddl-expander.rb
104
+ - lib/processor/cddl-flattening.rb
104
105
  - lib/processor/cddl-undefined.rb
105
106
  - lib/processor/cddl-visitor.rb
106
107
  - lib/writer/cddl-writer.rb