Class: HCL::Decoder
- Inherits:
-
ASTVisitor
- Object
- ASTVisitor
- HCL::Decoder
- Defined in:
- lib/hcl/decoder.rb
Constant Summary collapse
- SIMPLE_ESCAPES =
[ ["a", "\a"], ["b", "\b"], ["f", "\f"], ["n", "\n"], ["r", "\r"], ["t", "\t"], ["v", "\v"], ["\\", "\\"], ["'", "'"], ["\"", "\""] ].map { |a, b| [a.ord, b] }.to_h
Instance Method Summary collapse
- #common_nested_keys(a, b) ⇒ Object
- #conv_key(key) ⇒ Object
- #decode(ast) ⇒ Object
- #expand_objects(this, other, keys) ⇒ Object
- #hash_or_array(obj) ⇒ Object
-
#initialize ⇒ Decoder
constructor
A new instance of Decoder.
- #merge_object_lists(this, other, keys) ⇒ Object
- #merge_objects(this, other) ⇒ Object
- #newline?(b) ⇒ Boolean
- #objects_share_keys?(a, b, keys) ⇒ Boolean
- #recurse_objects(a, b, keys) ⇒ Object
- #set_object_nested(this, value, keys) ⇒ Object
- #undump(str) ⇒ Object
- #visit_boolean(ast) ⇒ Object
- #visit_comment(ast) ⇒ Object
- #visit_document(ast) ⇒ Object
- #visit_float(ast) ⇒ Object
- #visit_heredoc(ast) ⇒ Object
- #visit_integer(ast) ⇒ Object
- #visit_key_string(ast) ⇒ Object
- #visit_kv_key(ast) ⇒ Object
- #visit_list(ast) ⇒ Object
- #visit_object(ast) ⇒ Object
- #visit_string(ast) ⇒ Object
Methods inherited from ASTVisitor
Constructor Details
#initialize ⇒ Decoder
Returns a new instance of Decoder.
2 3 |
# File 'lib/hcl/decoder.rb', line 2 def initialize end |
Instance Method Details
#common_nested_keys(a, b) ⇒ Object
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 |
# File 'lib/hcl/decoder.rb', line 319 def common_nested_keys(a, b) a_child = a b_child = b keys = [] finished = false until finished break if Array === a_child or Array === b_child break if a_child.nil? or b_child.nil? break if a_child.length > 1 or b_child.length > 1 a_child.each do |k, v| finished = true if b_child[k].nil? finished = true if (not hash_or_array(a_child[k]) or not hash_or_array(b_child[k])) break if finished keys << k a_child = a_child[k] b_child = b_child[k] break end end return keys, a_child, b_child end |
#conv_key(key) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/hcl/decoder.rb', line 18 def conv_key(key) if Hash === key if key.key? :string key[:string] elsif Hash === key[:key] key[:key][:string] else key[:key] end else key end.to_s end |
#decode(ast) ⇒ Object
5 6 7 |
# File 'lib/hcl/decoder.rb', line 5 def decode(ast) visit(ast) end |
#expand_objects(this, other, keys) ⇒ Object
371 372 373 374 375 |
# File 'lib/hcl/decoder.rb', line 371 def (this, other, keys) this_child, other_child = recurse_objects(this, other, keys) set_object_nested(this, [this_child, other_child], keys) end |
#hash_or_array(obj) ⇒ Object
315 316 317 |
# File 'lib/hcl/decoder.rb', line 315 def hash_or_array(obj) Hash === obj or Array === obj end |
#merge_object_lists(this, other, keys) ⇒ Object
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/hcl/decoder.rb', line 377 def merge_object_lists(this, other, keys) this_child, other_child = recurse_objects(this, other, keys) if Hash === this_child && Array === other_child object = this_child list = other_child elsif Array === this_child && Hash === other_child object = other_child list = this_child else raise "not both lists" unless Array === this_child && Array == other_child end if list.nil? this_child.each_value do |v| other_child << v end set_object_nested(this, other_child, keys) else list << object set_object_nested(this, list, keys) end end |
#merge_objects(this, other) ⇒ Object
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/hcl/decoder.rb', line 402 def merge_objects(this, other) raise "merge_objects was called on the same object" if this == other raise "merge_objects was called with non-objects" unless Hash === this && Hash === other other.each do |k, v| tmp = this[k] if tmp != nil if Hash === tmp and Hash === v merge_objects(tmp, v) else this[k] = v end else this[k] = v end end end |
#newline?(b) ⇒ Boolean
47 48 49 |
# File 'lib/hcl/decoder.rb', line 47 def newline?(b) b == "\n".ord || b == "\r".ord end |
#objects_share_keys?(a, b, keys) ⇒ Boolean
346 347 348 349 350 351 352 |
# File 'lib/hcl/decoder.rb', line 346 def objects_share_keys?(a, b, keys) b.each_key do |k| return true if a.key? k end false end |
#recurse_objects(a, b, keys) ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/hcl/decoder.rb', line 303 def recurse_objects(a, b, keys) a_child = a.dup b_child = b.dup keys.each do |key| a_child = a_child[key] b_child = b_child[key] end return a_child, b_child end |
#set_object_nested(this, value, keys) ⇒ Object
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/hcl/decoder.rb', line 354 def set_object_nested(this, value, keys) raise "no keys" unless keys.size > 0 if keys.size == 1 this[keys.last] = value return end this_parent = this[keys.first] keys.drop(1).each do |key| this_parent = this_parent[key] end this_parent[keys.last] = value end |
#undump(str) ⇒ Object
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 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 200 201 202 |
# File 'lib/hcl/decoder.rb', line 64 def undump(str) chunks = nil chunk_start = 0 braces = 0 dollar = false hil = false bytes = str.bytes io = StringIO.new(str) b = io.getbyte until b.nil? if braces == 0 and dollar and b == "{".ord braces += 1 hil = true elsif braces > 0 and b == "{".ord braces += 1 end if braces > 0 and b == "}".ord braces -= 1 end dollar = false if braces == 0 and b == "$".ord dollar = true end if b == "\\".ord chunks = [] if chunks.nil? if chunk_start != io.pos-1 chunks << str[chunk_start..io.pos-2] end s = nil b = io.getbyte escape = SIMPLE_ESCAPES[b] if escape != nil b = io.getbyte s = escape elsif newline? b raise "string literal not terminated" if braces == 0 b = io.getbyte while newline? b s = "\n" elsif b == "x".ord c1 = nil c2 = nil b = io.getbyte || 0 c1 = b.chr.hex raise "invalid hexadecimal escape sequence" if c1.zero? b = io.getbyte || 0 c2 = b.chr.hex raise "invalid hexadecimal escape sequence" if c2.zero? b = io.getbyte s = (c1*16 + c2).to_s(16) elsif b == "u".ord || b == "U".ord size = if b == "U".ord then 8 else 4 end b = io.getbyte # Skip "u". codepoint = 0 hexdigits = 0 while hexdigits < size hex = b && b.chr.hex raise "UTF-8 escape sequence contained invalid character:(#{b.chr})" unless hex hexdigits += 1 codepoint = codepoint * 16 + hex raise "UTF-8 escape sequence too large" if codepoint > 0x10FFFF b = io.getbyte end s = codepoint.chr(Encoding::UTF_8) else cb = b && b.chr.to_i raise "invalid escape sequence" if cb.nil? b = io.getbyte if b != nil c2 = b.chr.to_i if b == "0".ord || c2 != 0 cb = 10 * cb + c2 b = io.getbyte if b != nil c3 = b.chr.to_i if b == "0".ord || c3 != 0 cb = 10 * cb + c3 raise "invalid decimal escape sequence" if cb > 255 b = io.getbyte end end end end s = cb.chr end chunks << s if s != nil chunk_start = if b.nil? then io.pos else io.pos - 1 end elsif b.nil? || (newline? b && braces == 0) raise "unfinished string" else b = io.getbyte end end raise "expected terminating brace" if b.nil? and braces != 0 if chunks != nil # Put last chunk into buffer. if chunk_start != io.pos chunks << str[chunk_start..io.pos-1] end chunks.join else # There were no escape sequences. str end end |
#visit_boolean(ast) ⇒ Object
224 225 226 227 228 229 230 231 232 |
# File 'lib/hcl/decoder.rb', line 224 def visit_boolean(ast) case ast[:boolean] when "true" then true when "false" false else raise "unknown boolean #{ast}" end end |
#visit_comment(ast) ⇒ Object
217 218 |
# File 'lib/hcl/decoder.rb', line 217 def visit_comment(ast) end |
#visit_document(ast) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/hcl/decoder.rb', line 9 def visit_document(ast) doc = ast[:document] if Array === doc visit({object: doc}) else doc end || {} end |
#visit_float(ast) ⇒ Object
234 235 236 |
# File 'lib/hcl/decoder.rb', line 234 def visit_float(ast) ast[:float].to_f end |
#visit_heredoc(ast) ⇒ Object
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 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/hcl/decoder.rb', line 238 def visit_heredoc(ast) doc = ast[:heredoc] tag = doc[:tag] content = doc[:doc].to_s.gsub(/#{tag}$/, "")[1..-1] case doc[:backticks].to_s when "<<" then content when "<<-" then # We need to unindent each line based on the indentation level of the marker lines = content.split("\n") indent = lines.last indented = true lines.each do |line| if indent.size > line.size indented = false break end prefix_found = line.delete_prefix(indent) != line unless prefix_found indented = false break end end # If all lines are not at least as indented as the terminating mark, return the # heredoc as is, but trim the leading space from the marker on the final line. unless indented return content.sub(/\s+\Z/, "") + "\n" end unindented_lines = [] lines.each do |line| unindented_lines << line.delete_prefix(indent) end unindented_lines.join("\n") else raise "unknown backticks #{backticks}" end end |
#visit_integer(ast) ⇒ Object
220 221 222 |
# File 'lib/hcl/decoder.rb', line 220 def visit_integer(ast) ast[:integer].to_i end |
#visit_key_string(ast) ⇒ Object
213 214 215 |
# File 'lib/hcl/decoder.rb', line 213 def visit_key_string(ast) ast[:key_string].to_s end |
#visit_kv_key(ast) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/hcl/decoder.rb', line 32 def visit_kv_key(ast) key = conv_key(ast[:kv_key]) value = visit(ast[:value]) extra_keys = ast[:keys] if extra_keys.nil? || extra_keys == [] { key => value } else rest = extra_keys.reverse.inject(value) do |h, k| { conv_key(k) => h } end { key => rest } end end |
#visit_list(ast) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/hcl/decoder.rb', line 281 def visit_list(ast) return [] if ast[:list].nil? list = if Hash === ast[:list] [ast[:list]] else ast[:list] end list.map do |a| value = a[:value] value = if Array === value it = value.reject { |i| i.key? :comment } raise "extra list value" unless it.size == 1 it.first else value end visit(value) end.reject(&:nil?) end |
#visit_object(ast) ⇒ Object
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 |
# File 'lib/hcl/decoder.rb', line 420 def visit_object(ast) object = ast[:object] return {} if object == "" pairs = ast[:object].map { |a| visit(a) }.reject(&:nil?) pairs.inject({}) do |result, object| first_key = object.sort.first[0] existing = result[first_key] value = object[first_key] = false if existing != nil if Array === existing existing << value else if Hash === existing if not Hash === existing = true else keys, a, b = common_nested_keys(existing, value) if Array === a or Array === b merge_object_lists(existing, value, keys) elsif objects_share_keys?(a, b, keys) if keys.size == 0 = true else (existing, value, keys) end else merge_objects(existing, value) result[first_key] = existing end end else = true end if result[first_key] = [existing, value] end end else result[first_key] = value end result end end |
#visit_string(ast) ⇒ Object
204 205 206 207 208 209 210 211 |
# File 'lib/hcl/decoder.rb', line 204 def visit_string(ast) string = ast[:string] if string == [] "" else undump(string.to_s) end end |