Class: ABNF
- Inherits:
-
Object
- Object
- ABNF
- Defined in:
- lib/abnftt/abnf-util.rb,
lib/abnftt/abnf-writer.rb,
lib/abnftt/abnf-visitor.rb,
lib/abnftt/abnf-squasher.rb,
lib/abnftt/abnf-flattener.rb,
lib/abnftt.rb
Constant Summary collapse
- UNESCAPED_SQSTR_RANGES =
[[0xA, 0xA], [0x20, 0x26], # "'" # [0x28, 0x5b], [0x5d, 0x7e], [0xa0, 0xd7ff], -- but JSON allows 7F-9F [0x28, 0x5b], [0x5d, 0xd7ff], # \ [0xe000, 0x10ffff]].map {|l, r| [l.chr(Encoding::UTF_8), r.chr(Encoding::UTF_8)] }
- ESCAPED_SQSTR_MAPPINGS =
[ ["\x08", "b"], ["\x09", "t"], ["\x0A", "n"], ["\x0C", "f"], ["\x0D", "r"], ["\x27", "'"], ["\x2F", "/"], ["\x5C", "\\"]]
- FIXUP_NAMES =
Hash.new {|h, k| k}
- @@parser =
ABNFGrammarParser.new
- @@gensym =
0
Instance Attribute Summary collapse
-
#ast ⇒ Object
Returns the value of attribute ast.
-
#parser ⇒ Object
Returns the value of attribute parser.
-
#rules ⇒ Object
Returns the value of attribute rules.
-
#tree ⇒ Object
Returns the value of attribute tree.
Class Method Summary collapse
Instance Method Summary collapse
-
#alt_ranges(l, r, step = 4, ndig = false) ⇒ Object
Support legacy JSON u/uu and u… hex unicode.
- #alt_ranges_legacy(l, r) ⇒ Object
- #alt_ranges_modern(l, r, step = 4) ⇒ Object
-
#breaker(s, col = 69) ⇒ Object
primitively break down lines so they fit on a teletype.
- #char_range_to_string ⇒ Object
- #char_range_to_string1(prod) ⇒ Object
- #ci_compat(prod) ⇒ Object
- #ci_cs_merge(prod) ⇒ Object
-
#count_alt(counter, prod) ⇒ Object
sharing.
- #detect_ci(prod) ⇒ Object
-
#do_range(l, r, step = 4) ⇒ Object
split range into passages that have the property needed for hex_ranges.
- #do_ranges_outside(cr, l, r) ⇒ Object
- #expand_op_into(s, op, out = [op]) ⇒ Object
-
#expand_range_into(s, op, out = [op]) ⇒ Object
Cleanup operations.
-
#expand_string(s, case_fold = false) ⇒ Object
flatten_strings: reduce all strings to char-range/seq/alt.
- #flatten_ops ⇒ Object
- #flatten_ops_1(prod) ⇒ Object
- #flatten_strings ⇒ Object
- #flatten_strings_1(prod) ⇒ Object
- #generate ⇒ Object
- #generate1(what) ⇒ Object
-
#hex_ranges(l, r, ndig = false) ⇒ Object
This assumes l and r are preprocessed to have single or full ranges except in one place.
-
#hexdig_range(l, r) ⇒ Object
Utilities for creating hexadecimal rules from unsigned integers.
-
#initialize(ast_ = nil, rules_ = {}) ⇒ ABNF
constructor
A new instance of ABNF.
- #merge_strings_1(prod) ⇒ Object
- #overlap(cr, l, r) ⇒ Object
-
#prec_check(inner, targetprec, prec) ⇒ Object
precedence: 1: / alt -> (type1) 2: »« seq 4: atomic.
- #seq_rep(prod) ⇒ Object
- #share_alt(prefix) ⇒ Object
- #share_hex(_prefix) ⇒ Object
- #share_hex_1(prod, rules) ⇒ Object
- #squash_edn_levels(**options) ⇒ Object
- #squash_edn_levels_1(prod, **options) ⇒ Object
-
#stringify(s) ⇒ Object
return [precedence ((2 if seq needed)), string].
- #to_s ⇒ Object
- #to_treetop(modname) ⇒ Object
- #to_treetop0(k, v) ⇒ Object
- #to_treetop1(ast) ⇒ Object
- #validate(s) ⇒ Object
- #visit(prod, &block) ⇒ Object
- #visit_all(prod_array, &block) ⇒ Object
- #wrap(head, all) ⇒ Object
- #wrap_flat(head, all) ⇒ Object
- #write_lhs(k) ⇒ Object
- #write_rhs(v, targetprec = 0) ⇒ Object
- #write_rule(k, v) ⇒ Object
Constructor Details
#initialize(ast_ = nil, rules_ = {}) ⇒ ABNF
Returns a new instance of ABNF.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/abnftt.rb', line 61 def initialize(ast_ = nil, rules_ = {}) if ast_ @ast = ast_ @tree = ast.ast end @rules = rules_ @tree.each do |x| op, name, val, rest = x fail rest if rest @rules[name] = if old = @rules[name] fail "duplicate rule for name #{name}" if op == "=" if Array === old && old[0] == "alt" old.dup << val else ["alt", old, val] end else val end end if @tree # warn "** rules #{rules.inspect}" end |
Instance Attribute Details
#ast ⇒ Object
Returns the value of attribute ast.
60 61 62 |
# File 'lib/abnftt.rb', line 60 def ast @ast end |
#parser ⇒ Object
Returns the value of attribute parser.
206 207 208 |
# File 'lib/abnftt.rb', line 206 def parser @parser end |
#rules ⇒ Object
Returns the value of attribute rules.
60 61 62 |
# File 'lib/abnftt.rb', line 60 def rules @rules end |
#tree ⇒ Object
Returns the value of attribute tree.
60 61 62 |
# File 'lib/abnftt.rb', line 60 def tree @tree end |
Class Method Details
.from_abnf(s) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/abnftt.rb', line 48 def self.from_abnf(s) ast = @@parser.parse s if !ast fail self.reason(@@parser, s) end ABNF.new(ast) end |
.from_rules(r) ⇒ Object
56 57 58 |
# File 'lib/abnftt.rb', line 56 def self.from_rules(r) ABNF.new(nil, r) end |
.reason(parser, s) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/abnftt.rb', line 37 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
#alt_ranges(l, r, step = 4, ndig = false) ⇒ Object
Support legacy JSON u/uu and u… hex unicode
105 106 107 108 109 110 111 |
# File 'lib/abnftt/abnf-util.rb', line 105 def alt_ranges(l, r, step = 4, ndig = false) alt = [] do_range(l.ord, r.ord, step) do |l, r, column| alt << hex_ranges(l, r, ndig) end wrap("alt", alt.reverse) # work around prioritized choice end |
#alt_ranges_legacy(l, r) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/abnftt/abnf-util.rb', line 113 def alt_ranges_legacy(l, r) alt = [] if l < 0x10000 alt << ["alt", alt_ranges(l, [r, 0xFFFF].min, 4, 4)] end if r >= 0x10000 l1 = [l, 0x10000].max - 0x10000 r1 = r - 0x10000 do_range(l1, r1, 10) do |l2, r2, column| alt << ["seq", alt_ranges((l2 >> 10) + 0xD800, (r2 >> 10) + 0xD800, 4, 4), ("\\u"), alt_ranges((l2 & 0x3FF) + 0xDC00, (r2 & 0x3FF) + 0xDC00, 4, 4)] end end wrap_flat("alt", alt) end |
#alt_ranges_modern(l, r, step = 4) ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/abnftt/abnf-util.rb', line 131 def alt_ranges_modern(l, r, step = 4) ["seq", ("{"), ["rep", 0, true, ["cs","0"]], alt_ranges(l, r, 4, false), ("}")] end |
#breaker(s, col = 69) ⇒ Object
primitively break down lines so they fit on a teletype
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/abnftt/abnf-writer.rb', line 90 def breaker(s, col = 69) ret = "" s.each_line do |*l| while l[-1].size > col breakpoint = l[-1][0...col].rindex(' ') break unless breakpoint && breakpoint > 4 while (partial = l[-1][0...breakpoint]).count('"').odd? break1 = partial.rindex('"') breakpoint = l[-1][0...break1].rindex(' ') break unless breakpoint && breakpoint > 4 end l[-1..-1] = [ l[-1][0...breakpoint], " " << l[-1][breakpoint+1..-1] ] end ret << l.join("\n") end ret end |
#char_range_to_string ⇒ Object
245 246 247 248 249 |
# File 'lib/abnftt/abnf-util.rb', line 245 def char_range_to_string rules.each do |name, prod| rules[name] = ci_cs_merge(detect_ci(char_range_to_string1(prod))) end end |
#char_range_to_string1(prod) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/abnftt/abnf-util.rb', line 225 def char_range_to_string1(prod) visit(prod) do |here| case here in ["seq", *rest] rest = (rest, "seq") i = rest.size while i > 1 if (rest[i-1] in ["cs", s2]) && (rest[i-2] in ["cs", s1]) rest[i-2..i-1] = [["cs", s1 + s2]] end i -= 1 end [true, rest] in ["char-range", chr, ^chr] if chr.between?(" ", "!") || chr.between?("#", "~") [true, ["cs", chr]] else false end end end |
#ci_compat(prod) ⇒ Object
276 277 278 279 280 281 282 283 284 285 |
# File 'lib/abnftt/abnf-util.rb', line 276 def ci_compat(prod) case prod in ["ci", s] s in ["cs", s] if s =~ /\A[^A-Za-z]*\z/ s else nil end end |
#ci_cs_merge(prod) ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/abnftt/abnf-util.rb', line 286 def ci_cs_merge(prod) visit(prod) do |here| case here in ["seq", *rest] rest = rest.map{|x| ci_cs_merge(x)} i = rest.size while i > 1 if (s2 = ci_compat(rest[i-1])) && (s1 = ci_compat(rest[i-2])) rest[i-2..i-1] = [["ci", s1 + s2]] end i -= 1 end [true, wrap_flat("seq", rest)] else false end end end |
#count_alt(counter, prod) ⇒ Object
sharing
332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/abnftt/abnf-util.rb', line 332 def count_alt(counter, prod) visit(prod) do |here| case here in ["alt", *rest] rest.each {|pr| count_alt(counter, pr)} counter[here] += 1 else false end end end |
#detect_ci(prod) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/abnftt/abnf-util.rb', line 251 def detect_ci(prod) visit(prod) do |here| case here in ["alt", ["cs", c1], ["cs", c2]] if c1.downcase == c2 && c2.upcase == c1 [true, ["ci", c1]] in [*rest1, ["cs", c1], ["cs", c2], *rest2] if c1.downcase == c2 && c2.upcase == c1 # warn [:PEEP, rest1, c1, c2, rest2].inspect if rest1[0] == "alt" [true, detect_ci([*rest1, ["ci", c1], *rest2])] else false end # This isn't complete... in [*rest1, ["cs", c0], ["cs", c1], ["cs", c2], *rest2] if c1.downcase == c2 && c2.upcase == c1 # warn [:PEEP2, rest1, c0, c1, c2, rest2].inspect if rest1[0] == "alt" [true, detect_ci([*rest1, ["cs", c0], ["ci", c1], *rest2])] else false end else false end end end |
#do_range(l, r, step = 4) ⇒ Object
split range into passages that have the property needed for hex_ranges
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/abnftt/abnf-util.rb', line 79 def do_range(l, r, step = 4) column = 0 while l <= r mask = (1 << step * (column + 1)) - 1 new_r = l | mask if new_r > r # right hand side: come down from mountain while column >= 0 mask >>= step new_r = (r + 1) & ~mask yield l, new_r - 1, column + 1 if l != new_r l = new_r column -= 1 end return else column += 1 if (l & mask) != 0 yield l, new_r, column l = new_r + 1 end end end end |
#do_ranges_outside(cr, l, r) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/abnftt/abnf-util.rb', line 35 def do_ranges_outside(cr, l, r) if cr[2] < l || cr[1] > r # outside yield cr[1].ord, cr[2].ord else if cr[1] < l yield cr[1].ord, l.ord - 1 end if cr[2] > r yield r.ord + 1, cr[2].ord end end end |
#expand_op_into(s, op, out = [op]) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/abnftt/abnf-flattener.rb', line 4 def (s, op, out = [op]) s.each do |el| case el in [^op, *inner] (inner, op, out) else out << flatten_ops_1(el) end end out end |
#expand_range_into(s, op, out = [op]) ⇒ Object
Cleanup operations
214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/abnftt/abnf-util.rb', line 214 def (s, op, out = [op]) s.each do |el| case el in [^op, *inner] (inner, op, out) else out << char_range_to_string1(el) end end out end |
#expand_string(s, case_fold = false) ⇒ Object
flatten_strings: reduce all strings to char-range/seq/alt
141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/abnftt/abnf-util.rb', line 141 def (s, case_fold = false) wrap("seq", s.chars.map do |ch| if case_fold && (u = ch.upcase; d = ch.downcase; u != d) ["alt", (u), (d)] else ["char-range", ch, ch] end end) end |
#flatten_ops ⇒ Object
27 28 29 30 31 |
# File 'lib/abnftt/abnf-flattener.rb', line 27 def flatten_ops rules.each do |name, prod| rules[name] = flatten_ops_1(prod) end end |
#flatten_ops_1(prod) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/abnftt/abnf-flattener.rb', line 15 def flatten_ops_1(prod) visit(prod) do |here| case here in ["seq", *rest] [true, (rest, "seq")] in ["alt", *rest] [true, (rest, "alt")] else false end end end |
#flatten_strings ⇒ Object
206 207 208 209 210 |
# File 'lib/abnftt/abnf-util.rb', line 206 def flatten_strings rules.each do |name, prod| rules[name] = flatten_strings_1(prod) end end |
#flatten_strings_1(prod) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/abnftt/abnf-util.rb', line 153 def flatten_strings_1(prod) f1 = visit(prod) do |here| case here in ["cs", string] [true, (string, false)] in ["ci", string] [true, (string, true)] else false end end merge_strings_1(flatten_ops_1(f1)) end |
#generate ⇒ Object
85 86 87 |
# File 'lib/abnftt.rb', line 85 def generate generate1(rules.first.first) end |
#generate1(what) ⇒ Object
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 |
# File 'lib/abnftt.rb', line 89 def generate1(what) case what when String expansion = rules[what] fail "can't find rules #{what}" unless expansion generate1(expansion) when Array op, *args = what case op when "seq" args.map {|arg| generate1(arg)}.join when "alt" generate1(args.sample) when "rep" l, h, x, rest = args fail rest if rest h = l+3 if h == true n = rand(h-l+1)+l (0...n).map { generate1(x) }.join when "ci" s, rest = args fail rest if rest s.chars.map{|x|[x.upcase, x.downcase].sample}.join when "cs" s, rest = args fail rest if rest s when "char-range" l, r = args fail rest if rest (rand(r.ord-l.ord+1)+l.ord).chr(Encoding::UTF_8) when "prose" # ["prose", text] fail "prose not implemented #{what.inspect}" when "im" warn "abnftt-style inline module ignored #{what.inspect}" '' else fail [op, args].inspect end else fail end end |
#hex_ranges(l, r, ndig = false) ⇒ Object
This assumes l and r are preprocessed to have single or full ranges except in one place
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/abnftt/abnf-util.rb', line 67 def hex_ranges(l, r, ndig = false) ld = l.digits(16) rd = r.digits(16) ndig ||= rd.size seq = [] (0...ndig).each do |dig| seq << hexdig_range(ld[dig] || 0, rd[dig] || 0) end wrap("seq", seq.reverse) end |
#hexdig_range(l, r) ⇒ Object
Utilities for creating hexadecimal rules from unsigned integers
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/abnftt/abnf-util.rb', line 50 def hexdig_range(l, r) alt = [] if l < 10 alt << ["char-range", (l+0x30).chr(Encoding::UTF_8), ([r, 9].min+0x30).chr(Encoding::UTF_8)] end if r >= 10 alt << ["char-range", ([l, 10].max+0x41-0xA).chr(Encoding::UTF_8), (r+0x41-0xA).chr(Encoding::UTF_8)] alt << ["char-range", ([l, 10].max+0x61-0xA).chr(Encoding::UTF_8), (r+0x61-0xA).chr(Encoding::UTF_8)] end wrap("alt", alt) end |
#merge_strings_1(prod) ⇒ Object
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 |
# File 'lib/abnftt/abnf-util.rb', line 168 def merge_strings_1(prod) visit(prod) do |here| case here in ["alt", *rest] ranges = [] i = 0 while i < rest.size case rest[i] in ["char-range", _ic1, _ic2] j = i while j+1 < rest.size && (rest[j+1] in ["char-range", _jc1, _jc2]) j += 1 end ranges << [i, j] if i != j # inclusive right i = j else here[i+1] = merge_strings_1(rest[i]) # XXX could be part of a range end i += 1 end ranges.reverse.each do |i, j| sorted = here[i+1..j+1].sort l = sorted.length while l > 1 l -= 1 # index to last item if sorted[l][1].ord == sorted[l-1][2].ord+1 # merge: sorted[l-1..l] = [["char-range", sorted[l-1][1], sorted[l][2]]] end end here[i+1..j+1] = sorted end [true, here] else false end end end |
#overlap(cr, l, r) ⇒ Object
29 30 31 32 33 |
# File 'lib/abnftt/abnf-util.rb', line 29 def overlap(cr, l, r) if cr[2] >= l && cr[1] <= r ["char-range", [cr[1], l].max, [cr[2], r].min] end end |
#prec_check(inner, targetprec, prec) ⇒ Object
precedence: 1: / alt -> (type1) 2: »« seq 4: atomic
19 20 21 22 23 24 25 |
# File 'lib/abnftt/abnf-writer.rb', line 19 def prec_check(inner, targetprec, prec) if targetprec >= prec "(#{inner})" else inner end end |
#seq_rep(prod) ⇒ Object
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 |
# File 'lib/abnftt/abnf-util.rb', line 305 def seq_rep(prod) visit(prod) do |here| case here in ["seq", *rest] rest = rest.map{|x| seq_rep(x)} i = rest.size # behind last element while i > 1 j = i - 1 # end of range s_end = rest[j] k = j # start of range while k > 0 && rest[k-1] == s_end k -= 1 end if k != j n = j - k + 1 rest[k..j] = [["rep", n, n, s_end]] end i = k # skip element k end [true, wrap_flat("seq", rest)] else false end end end |
#share_alt(prefix) ⇒ Object
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 |
# File 'lib/abnftt/abnf-util.rb', line 344 def share_alt(prefix) counter = Hash.new(0) rules.each do |name, prod| count_alt(counter, prod) end subs = {} counter.to_a.select{|k, v| v > 2}.sort_by{|k, v| -v}.each_with_index do |(el, _count), i| name = "#{prefix}-a#{i}" rules[name] = el subs[el] = name end rules.each do |name, prod| count_alt(counter, prod) end rules.replace(Hash[rules.map do |k, v| [k, seq_rep(visit(v) do |prod| if (s = subs[prod]) && k != s [true, s] end end)] end]) replacements = Hash[ rules.map{|k, v| [v, k] if String === v && /^sq-a\d+$/ === v }.compact] # warn [:REPLA, replacements].inspect used = {} used[rules.first.first] = true rules.each do |k, v| visit(v) do |here| if String === here if r = replacements[here] used[r] = true else used[here] = true end end end end # TODO: Should not do a h-x09 # warn [:USED, used].inspect rules.replace(Hash[rules.map {|k, v| unless replacements[k] || !used[k] if r = replacements[v] v = rules[v] end v = visit(v) do |here| if String === here && (r = replacements[here]) # warn [:R, v, r].inspect [true, r] end end [k, v] end }.compact]) end |
#share_hex(_prefix) ⇒ Object
454 455 456 457 458 459 460 |
# File 'lib/abnftt/abnf-util.rb', line 454 def share_hex(_prefix) newrules = {} rules.each do |name, prod| rules[name] = share_hex_1(prod, newrules) end rules.merge!(Hash[newrules.sort]) end |
#share_hex_1(prod, rules) ⇒ Object
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 |
# File 'lib/abnftt/abnf-util.rb', line 401 def share_hex_1(prod, rules) visit(prod) do |here| case here in ["alt", ["char-range", c3l, "9"], ["char-range", "A", c4r], ["char-range", "a", c6r]] if c4r == c6r.upcase && c3l >= "0" && c6r <= "f" name = "x#{c3l}#{c6r}" rules[name] ||= here [true, name] in ["alt", ["char-range", c4l, c4r], ["char-range", c6l, c6r]] if c4r == c6r.upcase && c4l == c6l.upcase && c6l.between?("a", "f") && c6r.between?("a", "f") name = "x#{c6l}#{c6r}" rules[name] ||= here [true, name] # in ["char-range", l, r] if l >= "0" && r <= "9" # name = "x#{l}#{r}" # rules[name] ||= here # [true, name] in ["seq", ["cs", "\\u"], *rest] suff = "0" rest = rest.map {|r| share_hex_1(r, rules) } case rest in [["alt", [/^c./, hex], *], *] name = "u-#{hex}" while rules[name] && rules[name] != here name = "u-#{hex}-#{suff.succ!}" end in [["alt", ["seq", [/^c./, hex], *], *], *] name = "u-#{hex}x" while rules[name] && rules[name] != here name = "u-#{hex}x-#{suff.succ!}" end else # require 'neatjson' # warn ::JSON.neat_generate(here) name = "u-#{suff.succ!}" while rules[name] && rules[name] != here name = "u-#{suff.succ!}" end end rules[name] ||= here [true, name] else false end end end |
#squash_edn_levels(**options) ⇒ Object
59 60 61 62 63 |
# File 'lib/abnftt/abnf-squasher.rb', line 59 def squash_edn_levels(**) rules.each do |name, prod| rules[name] = squash_edn_levels_1(prod, **) end end |
#squash_edn_levels_1(prod, **options) ⇒ Object
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 |
# File 'lib/abnftt/abnf-squasher.rb', line 27 def squash_edn_levels_1(prod, **) f1 = visit(prod) do |here| case here in ["char-range", c1, c2] lit = UNESCAPED_SQSTR_RANGES.map { |u1, u2| overlap(here, u1, u2) }.compact esc = ESCAPED_SQSTR_MAPPINGS.map {|cv, ev| if cv >= c1 && cv <= c2 ["seq", ["char-range", "\\", "\\"], ["char-range", ev, ev]] end }.compact u_escapes = [] if [:ascii] do_ranges_outside(here, " ", "~") do |l, r| u_escapes << alt_ranges_legacy(l, r) # old u_escapes << alt_ranges_modern(l, r) # new end else u_escapes << alt_ranges_legacy(c1.ord, c2.ord) # old u_escapes << alt_ranges_modern(c1.ord, c2.ord) # new end oldnew = ["seq", ["cs", "\\u"], wrap_flat("alt", u_escapes) ] if u_escapes != [] [true, wrap_flat("alt", [*lit, *esc, oldnew].compact.sort)] else false end end flatten_ops_1(f1) end |
#stringify(s) ⇒ Object
return [precedence ((2 if seq needed)), string]
5 6 7 8 |
# File 'lib/abnftt/abnf-writer.rb', line 5 def stringify(s) fail "Can't stringify #{s.inspect} yet" unless s =~ /\A[ !#-~]*\z/ %{"#{s}"} end |
#to_s ⇒ Object
85 86 87 |
# File 'lib/abnftt/abnf-writer.rb', line 85 def to_s rules.map {|k, v| write_rule(k, v) }.join("\n").sub(/.\z/) {$& << "\n"} end |
#to_treetop(modname) ⇒ Object
134 135 136 137 138 139 140 141 |
# File 'lib/abnftt.rb', line 134 def to_treetop(modname) <<~EOS # Encoding: UTF-8 grammar #{modname} #{rules.map {|k, v| to_treetop0(k, v)}.join} end EOS end |
#to_treetop0(k, v) ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/abnftt.rb', line 142 def to_treetop0(k, v) <<~EOS rule #{to_treetop1(k)} #{to_treetop1(v)} end EOS end |
#to_treetop1(ast) ⇒ Object
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 200 201 202 |
# File 'lib/abnftt.rb', line 154 def to_treetop1(ast) case ast when String FIXUP_NAMES[ast].gsub("-", "_") when Array case ast[0] when "alt" # ["alt", *a] "(#{ast[1..-1].map {|x| to_treetop1(x)}.join(" / ")})" when "seq" # ["seq", *a] "(#{ast[1..-1].map {|x| to_treetop1(x)}.join(" ")})" when "rep" # ["rep", s, e, a] t = to_treetop1(ast[3]) || "@@@" if ast[3][0] == "rep" t = "(#{t})" end case [ast[1], ast[2]] when [0, 1] t + "?" when [0, true] t + "*" when [1, true] t + "+" else t + " #{ast[1]}..#{ast[2] == true ? '' : ast[2]}" end when "prose" # ["prose", text] fail "prose not implemented #{ast.inspect}" when "ci" # ["ci", text] s = ast[1] if s =~ /\A[^A-Za-z]*\z/ s.inspect else s.inspect << "i" # could do this always, but reduce noise end when "cs" # ["cs", text] ast[1].inspect when "char-range" # ["char-range", c1, c2] c1 = Regexp.quote(ast[1]) c2 = Regexp.quote(ast[2]) "[#{c1}-#{c2}]" # XXX does that always work when "im" # ["im", a, text] to_treetop1(ast[1]) + " " + ast[2] else fail "to_treetop(#{ast.inspect})" end else fail "to_treetop(#{ast.inspect})" end end |
#validate(s) ⇒ Object
207 208 209 210 211 212 213 |
# File 'lib/abnftt.rb', line 207 def validate(s) @parser ||= Treetop.load_from_string(to_treetop("ABNF_Mod" << (@@gensym += 1).to_s)) parser_instance ||= @parser.new unless result1 = parser_instance.parse(s) fail self.class.reason(parser_instance, s) end end |
#visit(prod, &block) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/abnftt/abnf-visitor.rb', line 7 def visit(prod, &block) done, ret = block.call(prod, &block) if done return ret end case prod in ["alt", *prods] ["alt", *visit_all(prods, &block)] in ["tadd", *prods] ["tadd", *visit_all(prods, &block)] in ["seq", *prods] ["seq", *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/abnftt/abnf-visitor.rb', line 4 def visit_all(prod_array, &block) prod_array.map {|prod| visit(prod, &block)} end |
#wrap(head, all) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/abnftt/abnf-util.rb', line 6 def wrap(head, all) if all.size == 1 all.first else [head, *all] end end |
#wrap_flat(head, all) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/abnftt/abnf-util.rb', line 14 def wrap_flat(head, all) if all.size == 1 all.first else [head, *all.collect_concat {|el| case el in [^head, *rest] rest else [el] end }] end end |
#write_lhs(k) ⇒ Object
10 11 12 |
# File 'lib/abnftt/abnf-writer.rb', line 10 def write_lhs(k) k end |
#write_rhs(v, targetprec = 0) ⇒ Object
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/abnftt/abnf-writer.rb', line 27 def write_rhs(v, targetprec = 0) prec, ret = case v in String # this should really be ["name", id] [4, v] in ["name", id] [4, id] in ["alt" | "tadd", *types] [1, types.map{write_rhs(_1, 1)}.join(" / ")] in ["seq", *groups] case groups.size when 0; [4, ""] # XXX else [2, "#{groups.map{write_rhs(_1, 2)}.join(" ")}"] end in ["ci", s] [4, stringify(s)] in ["cs", s] if s =~ /\A[^A-Za-z]*\z/ [4, stringify(s)] else [4, "%s" << stringify(s)] # reduce noise if no alphabetics end in ["char-range", c1, c2] nc1 = "%02x" % c1.ord nc2 = "%02x" % c2.ord nc2add = "-#{nc2}" if nc2 != nc1 [4, "%x#{nc1}#{nc2add}"] in ["rep", s, e, group] if s == 0 && e == 1 [4, "[#{write_rhs(group)}]"] else occur = case [s, e] in [1, 1]; "" in [0, true]; "*" in [n, ^n]; n.to_s else "#{s}*#{e != true ? e : ""}" end [4, "#{occur}#{write_rhs(group, 4)}"] end else fail [:WRITE_NOMATCH, v].inspect end prec_check(ret, targetprec, prec) end |
#write_rule(k, v) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/abnftt/abnf-writer.rb', line 75 def write_rule(k, v) case v in ["tadd", *_rest] assign = "=/" else assign = "=" end "#{write_lhs(k)} #{assign} #{write_rhs(v, 0)}" end |