junoser 0.7.6 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +73 -0
- data/Gemfile.lock +1 -1
- data/README.md +2 -1
- data/Rakefile +6 -9
- data/exe/junoser +1 -2
- data/exe/junoser-squash +1 -2
- data/lib/junoser/js_ruler.rb +56 -16
- data/lib/junoser/parser.rb +78897 -68013
- data/lib/junoser/ruler.rb +46 -43
- data/lib/junoser/version.rb +1 -1
- data/lib/junoser/xsd/base.rb +2 -2
- data/lib/junoser/xsd/choice.rb +25 -2
- data/lib/junoser/xsd/parsable.rb +2 -1
- data/lib/junoser/xsd/sequence.rb +3 -1
- metadata +1 -1
data/lib/junoser/ruler.rb
CHANGED
|
@@ -30,25 +30,25 @@ module Junoser
|
|
|
30
30
|
def process_line(str)
|
|
31
31
|
return str if str =~ /^(.* do|end)$/
|
|
32
32
|
|
|
33
|
-
str.gsub!(/("[^"]+")/) { "
|
|
33
|
+
str.gsub!(/("[^"]+")/) { "k(#{$1})" } # "foo" -> k("foo")
|
|
34
34
|
|
|
35
35
|
str.gsub!(/^(\s*)arg(\.as\(:\S+\))? \($/) { "#{$1}b(arg#{$2}," } # arg ( -> b(arg,
|
|
36
|
-
#
|
|
37
|
-
str.gsub!(/^(\s*)(
|
|
36
|
+
# k("foo") bar -> a(k("foo"), bar)
|
|
37
|
+
str.gsub!(/^(\s*)(k\(\S+\)) ([^ \t\n\r\f(|,]+)(\.as\(:\S+\))?(,?)$/) do
|
|
38
38
|
"#{$1}a(#{$2}, #{$3})#{$4}#{$5}"
|
|
39
39
|
end
|
|
40
|
-
#
|
|
41
|
-
str.gsub!(/^(\s*)(
|
|
40
|
+
# k("foo") (a | b) -> a(k("foo"), a | b)
|
|
41
|
+
str.gsub!(/^(\s*)(k\(\S+\)) (enum)?\((.*)\)(,?)$/) do
|
|
42
42
|
"#{$1}a(#{$2}, #{$3}#{$4})#{$5}"
|
|
43
43
|
end
|
|
44
|
-
str.gsub!(/^(\s*)(
|
|
44
|
+
str.gsub!(/^(\s*)(k\(\S+\)) \($/) { "#{$1}b(#{$2}," } # k("foo") ( -> b(k("foo"),
|
|
45
45
|
str.gsub!(/^(\s*)(enum)?(\(.*\))(\.as\(:\S\))? \($/) { "#{$1}b(#{$2}#{$3}#{$4}," } # (a | b) ( -> b((a | b),
|
|
46
|
-
#
|
|
47
|
-
str.gsub!(/^(\s*)(
|
|
46
|
+
# k("foo") bar ( -> b(a(k("foo"), bar),
|
|
47
|
+
str.gsub!(/^(\s*)(k\(\S+\)) ([^ \t\n\r\f(|,]+) \($/) do
|
|
48
48
|
"#{$1}b(a(#{$2}, #{$3}),"
|
|
49
49
|
end
|
|
50
|
-
#
|
|
51
|
-
str.gsub!(/^(\s*)(
|
|
50
|
+
# k("foo") (a | b) ( -> a(k("foo"), a | b,
|
|
51
|
+
str.gsub!(/^(\s*)(k\(\S+\)) (enum)?\((.*)\) \($/) do
|
|
52
52
|
"#{$1}a(#{$2}, #{$3}#{$4},"
|
|
53
53
|
end
|
|
54
54
|
str
|
|
@@ -164,51 +164,31 @@ module Junoser
|
|
|
164
164
|
' a(arg, arg)'], '')
|
|
165
165
|
end
|
|
166
166
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
# Fix overkill
|
|
167
|
+
# Fix overkill
|
|
170
168
|
str.gsub!(/^(\s*)"priority" \(\s*ca\(\s*arg\s*\)/) do
|
|
171
169
|
fmt(['"priority" (',
|
|
172
170
|
' a(arg, arg)', $1])
|
|
173
171
|
end
|
|
174
172
|
|
|
175
|
-
#
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
#
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
# "inet6"
|
|
185
|
-
str.gsub!(/"cspf"(.*\s*.*)"cspf-link"/) { %("cspf-link"#{$1}"cspf") }
|
|
186
|
-
str.gsub!(/"http"(.*\s*.*)"https"/) { %("https"#{$1}"http") }
|
|
187
|
-
str.gsub!(/"inet"(.*\s*.*)"inet6"/) { %("inet6"#{$1}"inet") }
|
|
188
|
-
str.gsub!(/"icmp"(.*\s*.*)"icmp6"/) { %("icmp6"#{$1}"icmp") }
|
|
189
|
-
str.gsub!(/"icmp"(.*\s*.*)"icmpv6"/) { %("icmpv6"#{$1}"icmp") }
|
|
190
|
-
str.gsub!(/"snmp"(.*\s*.*)"snmptrap"/) { %("snmptrap"#{$1}"snmp") }
|
|
191
|
-
str.gsub!(/"ospf"(.*\s*.*)"ospf3"/) { %("ospf3"#{$1}"ospf") }
|
|
192
|
-
str.gsub!(/"deny"(.*\s*.*)"deny-password"/) { %("deny-password"#{$1}"deny") }
|
|
193
|
-
str.gsub!(/"no-redirects"(.*\s*.*)"no-redirects-ipv6"/) { %("no-redirects-ipv6"#{$1}"no-redirects") }
|
|
194
|
-
str.gsub!(/"chassis"([^()]*)"chassis-ha-reswatch"/m) { %("chassis-ha-reswatch"#{$1}"chassis") }
|
|
195
|
-
str.gsub! '"tls1" | "tls11" | "tls12"', '"tls11" | "tls12" | "tls1"'
|
|
196
|
-
str.gsub!(/("group1" \| "group2" \| "group5") \| ([^)]+)/) { "#{$2} | #{$1}" }
|
|
197
|
-
|
|
198
|
-
%w[ccc ethernet-over-atm tcc vpls bridge].each do |encap|
|
|
199
|
-
str.gsub!(/"ethernet"(.*)"ethernet-#{encap}"/) { %("ethernet-#{encap}"#{$1}"ethernet") }
|
|
200
|
-
end
|
|
201
|
-
|
|
202
|
-
str.gsub!(/^(\s*)"path" arg \(\s*c\(\s*sc\(\s*"abstract",\s*c\(\s*"loose",\s*"loose-link",\s*"strict"\s*\)\s*\)\.as\(:oneline\)/) do
|
|
173
|
+
# 26.2 puts "exclude" in front of the oneline part of "path". Keep it and rewrite the oneline part only.
|
|
174
|
+
exclude = /((?:\s*"exclude" \(\s*c\(\s*arg\s*\)\s*\),)?)/
|
|
175
|
+
oneline = /sc\(\s*"abstract",\s*c\(\s*"loose",\s*"loose-link",\s*"strict"\s*\)\s*\)\.as\(:oneline\)/
|
|
176
|
+
str.gsub!(/^(\s*)"path" arg \(\s*c\(#{exclude}\s*#{oneline}/) do
|
|
177
|
+
members = if $2.empty?
|
|
178
|
+
[]
|
|
179
|
+
else
|
|
180
|
+
[' "exclude" (', ' c(', ' arg', ' )', ' ),']
|
|
181
|
+
end
|
|
203
182
|
fmt(['"path" arg (',
|
|
204
183
|
' c(',
|
|
184
|
+
*members,
|
|
205
185
|
' b(',
|
|
206
186
|
' ipaddr,',
|
|
207
187
|
' c(',
|
|
208
188
|
' "abstract",',
|
|
209
189
|
' c(',
|
|
210
|
-
' "loose-link",',
|
|
211
190
|
' "loose",',
|
|
191
|
+
' "loose-link",',
|
|
212
192
|
' "strict"',
|
|
213
193
|
' )',
|
|
214
194
|
' ).as(:oneline)',
|
|
@@ -219,8 +199,8 @@ module Junoser
|
|
|
219
199
|
# Fix .xsd: Elements without "nokeyword" flag
|
|
220
200
|
#
|
|
221
201
|
str.gsub!(/\((.*) \| "name"\)/) { "(#{$1} | arg)" }
|
|
202
|
+
str.gsub!(/\("name" \| (.*?)\)/) { "(#{$1} | arg)" }
|
|
222
203
|
str.gsub! '"vlan" ("all" | "vlan-name")', '"vlan" ("all" | arg)'
|
|
223
|
-
str.gsub!(/\((.*) \| "vlan-id"\)/) { "(#{$1} | arg)" }
|
|
224
204
|
|
|
225
205
|
%w[filename].each do |key|
|
|
226
206
|
str.gsub! %("#{key}" arg), 'arg'
|
|
@@ -297,6 +277,12 @@ module Junoser
|
|
|
297
277
|
str.gsub!(/^(\s*)\)(,\s*"fpc" enum\(\("all" \| "fpc-slot-number"\)\) \(\s*)c\((.*"lldp-priority"\s*\)\s*\))/m) do
|
|
298
278
|
"#{$1}).as(:oneline)#{$2}sc(#{$3}.as(:oneline)"
|
|
299
279
|
end
|
|
280
|
+
str.gsub! '"fpc" enum(("all" | "fpc-slot-number"))', '"fpc" ("all" | arg)'
|
|
281
|
+
|
|
282
|
+
# Fix .xsd: slot numbers in "set chassis alarm fru-absence ignore fpc xxx pem xxx"
|
|
283
|
+
# these must come after the "poe" section above, which matches "fpc" enum(("all" | "fpc-slot-number"))
|
|
284
|
+
str.gsub! '("all" | "fpc-slot-number")', '("all" | arg)'
|
|
285
|
+
str.gsub! '("all" | "psu-slot-number")', '("all" | arg)'
|
|
300
286
|
|
|
301
287
|
# Fix .xsd: support "set protocols iccp peer xxx liveness-detection single-hop"
|
|
302
288
|
str.gsub!(/(^rule\(:peer_group\) do.*?\n(\s*)"detection-time" \(\s*c\(\s*"threshold" arg\s*\)\s*\))/m) do
|
|
@@ -372,6 +358,23 @@ module Junoser
|
|
|
372
358
|
end
|
|
373
359
|
end
|
|
374
360
|
|
|
361
|
+
# A keyword fails when more characters follow it. Otherwise a bare keyword in a choice also matches the
|
|
362
|
+
# head of a longer sibling ("immediate-leave" vs "immediate-leave-on-host-interface") and parslet never
|
|
363
|
+
# tries the longer one. One atom is cheaper than str(word) >> lookahead.
|
|
364
|
+
class Keyword < Parslet::Atoms::Str
|
|
365
|
+
def try(source, context, consume_all)
|
|
366
|
+
result = super
|
|
367
|
+
# fail when the keyword goes on, like "-on-host-interface" after "immediate-leave"
|
|
368
|
+
return result unless result.first && source.matches?(/[^\\s#]/)
|
|
369
|
+
|
|
370
|
+
context.err(self, source, "Keyword \#{str.inspect} is followed by more characters")
|
|
371
|
+
end
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
def k(word)
|
|
375
|
+
Keyword.new(word)
|
|
376
|
+
end
|
|
377
|
+
|
|
375
378
|
# block with children maybe
|
|
376
379
|
def b(object, *children)
|
|
377
380
|
children.inject(object) {|rule, child| rule.as(:label) >> (space >> child.as(:child) | eos) }
|
data/lib/junoser/version.rb
CHANGED
data/lib/junoser/xsd/base.rb
CHANGED
|
@@ -40,13 +40,13 @@ module Junoser
|
|
|
40
40
|
@oneliner ||= !xml.xpath('./xsd:annotation/xsd:appinfo/*[local-name()="flag" and text()="oneliner"]').empty?
|
|
41
41
|
end
|
|
42
42
|
|
|
43
|
-
private
|
|
44
|
-
|
|
45
43
|
def nokeyword?
|
|
46
44
|
# don't use "/flag" as xsd manipulation automatically add "xsd:" prefix
|
|
47
45
|
@nokeyword ||= !xml.xpath('./xsd:annotation/xsd:appinfo/*[local-name()="flag" and text()="nokeyword"]').empty?
|
|
48
46
|
end
|
|
49
47
|
|
|
48
|
+
private
|
|
49
|
+
|
|
50
50
|
def has_single_child_of?(klass) # rubocop:disable Naming/PredicatePrefix
|
|
51
51
|
config.size == 1 && config.first.is_a?(klass)
|
|
52
52
|
end
|
data/lib/junoser/xsd/choice.rb
CHANGED
|
@@ -25,15 +25,38 @@ module Junoser
|
|
|
25
25
|
if config.empty?
|
|
26
26
|
''
|
|
27
27
|
elsif has_single_child_of?(Junoser::Xsd::Choice)
|
|
28
|
-
|
|
28
|
+
child = config.first
|
|
29
|
+
str = join(child.config)
|
|
30
|
+
return '' if str.empty?
|
|
31
|
+
|
|
32
|
+
child.sequential_arguments? ? fmt('s(', str, ')') : fmt('c(', str, ')')
|
|
33
|
+
elsif sequential_arguments?
|
|
34
|
+
fmt('s(', join(config), ')')
|
|
29
35
|
else
|
|
30
|
-
fmt('c(',
|
|
36
|
+
fmt('c(', join(config), ')')
|
|
31
37
|
end
|
|
32
38
|
end
|
|
33
39
|
|
|
34
40
|
def unbounded?
|
|
35
41
|
xml['maxOccurs'] == 'unbounded'
|
|
36
42
|
end
|
|
43
|
+
|
|
44
|
+
# xsd describes a sequence of positional arguments as an unbounded choice of
|
|
45
|
+
# "nokeyword" elements, eg. "priority <setup-priority> <reservation-priority>".
|
|
46
|
+
def sequential_arguments?
|
|
47
|
+
# a bounded choice means one of the arguments, which is a single argument
|
|
48
|
+
unbounded? && config.size > 1 &&
|
|
49
|
+
# Strictly, a sequence should cover the arguments only, eg. c("foo", s(arg, arg)).
|
|
50
|
+
# In .xsd, however, such a choice always has arguments only, so all? is enough.
|
|
51
|
+
config.all? { |c| c.is_a?(Junoser::Xsd::Element) && c.nokeyword? }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
# drop a child that renders as an empty string, because an empty operand breaks the generated parser
|
|
57
|
+
def join(children)
|
|
58
|
+
children.map(&:to_s).reject(&:empty?).join(",\n")
|
|
59
|
+
end
|
|
37
60
|
end
|
|
38
61
|
end
|
|
39
62
|
end
|
data/lib/junoser/xsd/parsable.rb
CHANGED
|
@@ -24,7 +24,8 @@ module Junoser
|
|
|
24
24
|
|
|
25
25
|
def remove_unused
|
|
26
26
|
xpath('/xsd:schema/*[self::xsd:import]').remove
|
|
27
|
-
|
|
27
|
+
# not "//": with a predicate libxml2 collects every node first and hits its nodeset limit on large schemas
|
|
28
|
+
xpath('descendant::xsd:element[@ref="undocumented" or @ref="junos:comment" or @name="apply-advanced"]').remove
|
|
28
29
|
self
|
|
29
30
|
end
|
|
30
31
|
end
|
data/lib/junoser/xsd/sequence.rb
CHANGED
|
@@ -35,7 +35,9 @@ module Junoser
|
|
|
35
35
|
return if str.empty?
|
|
36
36
|
|
|
37
37
|
# Assuming that <xsd:sequence> always has <xsd:complexType> as the parent
|
|
38
|
-
if
|
|
38
|
+
if child.sequential_arguments?
|
|
39
|
+
fmt('s(', str, ')')
|
|
40
|
+
elsif parent.parent&.oneliner? && child.unbounded?
|
|
39
41
|
fmt('sc(', str, ')')
|
|
40
42
|
else
|
|
41
43
|
fmt('c(', str, ')')
|