| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
module Prism |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
class Source |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
def self.for(source, start_line = 1, offsets = []) |
| 14 |
if source.ascii_only? |
| 15 |
ASCIISource.new(source, start_line, offsets) |
| 16 |
elsif source.encoding == Encoding::BINARY |
| 17 |
source.force_encoding(Encoding::UTF_8) |
| 18 |
|
| 19 |
if source.valid_encoding? |
| 20 |
new(source, start_line, offsets) |
| 21 |
else |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
source.force_encoding(Encoding::BINARY) |
| 29 |
ASCIISource.new(source, start_line, offsets) |
| 30 |
end |
| 31 |
else |
| 32 |
new(source, start_line, offsets) |
| 33 |
end |
| 34 |
end |
| 35 |
|
| 36 |
|
| 37 |
attr_reader :source |
| 38 |
|
| 39 |
|
| 40 |
attr_reader :start_line |
| 41 |
|
| 42 |
|
| 43 |
attr_reader :offsets |
| 44 |
|
| 45 |
|
| 46 |
def initialize(source, start_line = 1, offsets = []) |
| 47 |
@source = source |
| 48 |
@start_line = start_line |
| 49 |
@offsets = offsets |
| 50 |
end |
| 51 |
|
| 52 |
|
| 53 |
def replace_start_line(start_line) |
| 54 |
@start_line = start_line |
| 55 |
end |
| 56 |
|
| 57 |
|
| 58 |
def replace_offsets(offsets) |
| 59 |
@offsets.replace(offsets) |
| 60 |
end |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
def encoding |
| 65 |
source.encoding |
| 66 |
end |
| 67 |
|
| 68 |
|
| 69 |
def lines |
| 70 |
source.lines |
| 71 |
end |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
def slice(byte_offset, length) |
| 76 |
source.byteslice(byte_offset, length) or raise |
| 77 |
end |
| 78 |
|
| 79 |
|
| 80 |
def byte_offset(line, column) |
| 81 |
normal = line - @start_line |
| 82 |
raise IndexError if normal < 0 |
| 83 |
offsets.fetch(normal) + column |
| 84 |
rescue IndexError |
| 85 |
raise ArgumentError, "line #{line} is out of range" |
| 86 |
end |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
def line(byte_offset) |
| 91 |
start_line + find_line(byte_offset) |
| 92 |
end |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
def line_start(byte_offset) |
| 97 |
offsets[find_line(byte_offset)] |
| 98 |
end |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
def line_end(byte_offset) |
| 103 |
offsets[find_line(byte_offset) + 1] || source.bytesize |
| 104 |
end |
| 105 |
|
| 106 |
|
| 107 |
def column(byte_offset) |
| 108 |
byte_offset - line_start(byte_offset) |
| 109 |
end |
| 110 |
|
| 111 |
|
| 112 |
def character_offset(byte_offset) |
| 113 |
(source.byteslice(0, byte_offset) or raise).length |
| 114 |
end |
| 115 |
|
| 116 |
|
| 117 |
def character_column(byte_offset) |
| 118 |
character_offset(byte_offset) - character_offset(line_start(byte_offset)) |
| 119 |
end |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
def code_units_offset(byte_offset, encoding) |
| 134 |
byteslice = (source.byteslice(0, byte_offset) or raise).encode(encoding, invalid: :replace, undef: :replace) |
| 135 |
|
| 136 |
if encoding == Encoding::UTF_16LE || encoding == Encoding::UTF_16BE |
| 137 |
byteslice.bytesize / 2 |
| 138 |
else |
| 139 |
byteslice.length |
| 140 |
end |
| 141 |
end |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
def code_units_cache(encoding) |
| 146 |
CodeUnitsCache.new(source, encoding) |
| 147 |
end |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
def code_units_column(byte_offset, encoding) |
| 152 |
code_units_offset(byte_offset, encoding) - code_units_offset(line_start(byte_offset), encoding) |
| 153 |
end |
| 154 |
|
| 155 |
|
| 156 |
def deep_freeze |
| 157 |
source.freeze |
| 158 |
offsets.freeze |
| 159 |
freeze |
| 160 |
end |
| 161 |
|
| 162 |
private |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
def find_line(byte_offset) |
| 167 |
index = offsets.bsearch_index { |offset| offset > byte_offset } || offsets.length |
| 168 |
index - 1 |
| 169 |
end |
| 170 |
end |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
class CodeUnitsCache |
| 187 |
class UTF16Counter |
| 188 |
def initialize(source, encoding) |
| 189 |
@source = source |
| 190 |
@encoding = encoding |
| 191 |
end |
| 192 |
|
| 193 |
def count(byte_offset, byte_length) |
| 194 |
@source.byteslice(byte_offset, byte_length).encode(@encoding, invalid: :replace, undef: :replace).bytesize / 2 |
| 195 |
end |
| 196 |
end |
| 197 |
|
| 198 |
class LengthCounter |
| 199 |
def initialize(source, encoding) |
| 200 |
@source = source |
| 201 |
@encoding = encoding |
| 202 |
end |
| 203 |
|
| 204 |
def count(byte_offset, byte_length) |
| 205 |
@source.byteslice(byte_offset, byte_length).encode(@encoding, invalid: :replace, undef: :replace).length |
| 206 |
end |
| 207 |
end |
| 208 |
|
| 209 |
private_constant :UTF16Counter, :LengthCounter |
| 210 |
|
| 211 |
|
| 212 |
def initialize(source, encoding) |
| 213 |
@source = source |
| 214 |
@counter = |
| 215 |
if encoding == Encoding::UTF_16LE || encoding == Encoding::UTF_16BE |
| 216 |
UTF16Counter.new(source, encoding) |
| 217 |
else |
| 218 |
LengthCounter.new(source, encoding) |
| 219 |
end |
| 220 |
|
| 221 |
@cache = {} |
| 222 |
@offsets = [] |
| 223 |
end |
| 224 |
|
| 225 |
|
| 226 |
def [](byte_offset) |
| 227 |
@cache[byte_offset] ||= |
| 228 |
if (index = @offsets.bsearch_index { |offset| offset > byte_offset }).nil? |
| 229 |
@offsets << byte_offset |
| 230 |
@counter.count(0, byte_offset) |
| 231 |
elsif index == 0 |
| 232 |
@offsets.unshift(byte_offset) |
| 233 |
@counter.count(0, byte_offset) |
| 234 |
else |
| 235 |
@offsets.insert(index, byte_offset) |
| 236 |
offset = @offsets[index - 1] |
| 237 |
@cache[offset] + @counter.count(offset, byte_offset - offset) |
| 238 |
end |
| 239 |
end |
| 240 |
end |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
class ASCIISource < Source |
| 251 |
|
| 252 |
def character_offset(byte_offset) |
| 253 |
byte_offset |
| 254 |
end |
| 255 |
|
| 256 |
|
| 257 |
def character_column(byte_offset) |
| 258 |
byte_offset - line_start(byte_offset) |
| 259 |
end |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
def code_units_offset(byte_offset, encoding) |
| 268 |
byte_offset |
| 269 |
end |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
def code_units_cache(encoding) |
| 275 |
->(byte_offset) { byte_offset } |
| 276 |
end |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
def code_units_column(byte_offset, encoding) |
| 282 |
byte_offset - line_start(byte_offset) |
| 283 |
end |
| 284 |
end |
| 285 |
|
| 286 |
|
| 287 |
class Location |
| 288 |
|
| 289 |
|
| 290 |
attr_reader :source |
| 291 |
protected :source |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
attr_reader :start_offset |
| 296 |
|
| 297 |
|
| 298 |
attr_reader :length |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
def initialize(source, start_offset, length) |
| 303 |
@source = source |
| 304 |
@start_offset = start_offset |
| 305 |
@length = length |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
@leading_comments = nil |
| 311 |
@trailing_comments = nil |
| 312 |
end |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
def leading_comments |
| 317 |
@leading_comments ||= [] |
| 318 |
end |
| 319 |
|
| 320 |
|
| 321 |
def leading_comment(comment) |
| 322 |
leading_comments << comment |
| 323 |
end |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
def trailing_comments |
| 328 |
@trailing_comments ||= [] |
| 329 |
end |
| 330 |
|
| 331 |
|
| 332 |
def trailing_comment(comment) |
| 333 |
trailing_comments << comment |
| 334 |
end |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
def comments |
| 339 |
[*@leading_comments, *@trailing_comments] |
| 340 |
end |
| 341 |
|
| 342 |
|
| 343 |
def copy(source: self.source, start_offset: self.start_offset, length: self.length) |
| 344 |
Location.new(source, start_offset, length) |
| 345 |
end |
| 346 |
|
| 347 |
|
| 348 |
def chop |
| 349 |
copy(length: length == 0 ? length : length - 1) |
| 350 |
end |
| 351 |
|
| 352 |
|
| 353 |
def inspect |
| 354 |
"#<Prism::Location @start_offset=#{@start_offset} @length=#{@length} start_line=#{start_line}>" |
| 355 |
end |
| 356 |
|
| 357 |
|
| 358 |
def source_lines |
| 359 |
source.lines |
| 360 |
end |
| 361 |
|
| 362 |
|
| 363 |
def slice |
| 364 |
source.slice(start_offset, length) |
| 365 |
end |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
def slice_lines |
| 371 |
line_start = source.line_start(start_offset) |
| 372 |
line_end = source.line_end(end_offset) |
| 373 |
source.slice(line_start, line_end - line_start) |
| 374 |
end |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
def start_character_offset |
| 379 |
source.character_offset(start_offset) |
| 380 |
end |
| 381 |
|
| 382 |
|
| 383 |
def start_code_units_offset(encoding = Encoding::UTF_16LE) |
| 384 |
source.code_units_offset(start_offset, encoding) |
| 385 |
end |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
def cached_start_code_units_offset(cache) |
| 390 |
cache[start_offset] |
| 391 |
end |
| 392 |
|
| 393 |
|
| 394 |
def end_offset |
| 395 |
start_offset + length |
| 396 |
end |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
def end_character_offset |
| 401 |
source.character_offset(end_offset) |
| 402 |
end |
| 403 |
|
| 404 |
|
| 405 |
def end_code_units_offset(encoding = Encoding::UTF_16LE) |
| 406 |
source.code_units_offset(end_offset, encoding) |
| 407 |
end |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
def cached_end_code_units_offset(cache) |
| 412 |
cache[end_offset] |
| 413 |
end |
| 414 |
|
| 415 |
|
| 416 |
def start_line |
| 417 |
source.line(start_offset) |
| 418 |
end |
| 419 |
|
| 420 |
|
| 421 |
def start_line_slice |
| 422 |
offset = source.line_start(start_offset) |
| 423 |
source.slice(offset, start_offset - offset) |
| 424 |
end |
| 425 |
|
| 426 |
|
| 427 |
def end_line |
| 428 |
source.line(end_offset) |
| 429 |
end |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
def start_column |
| 434 |
source.column(start_offset) |
| 435 |
end |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
def start_character_column |
| 440 |
source.character_column(start_offset) |
| 441 |
end |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
def start_code_units_column(encoding = Encoding::UTF_16LE) |
| 446 |
source.code_units_column(start_offset, encoding) |
| 447 |
end |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
def cached_start_code_units_column(cache) |
| 452 |
cache[start_offset] - cache[source.line_start(start_offset)] |
| 453 |
end |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
def end_column |
| 458 |
source.column(end_offset) |
| 459 |
end |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
def end_character_column |
| 464 |
source.character_column(end_offset) |
| 465 |
end |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
def end_code_units_column(encoding = Encoding::UTF_16LE) |
| 470 |
source.code_units_column(end_offset, encoding) |
| 471 |
end |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
def cached_end_code_units_column(cache) |
| 476 |
cache[end_offset] - cache[source.line_start(end_offset)] |
| 477 |
end |
| 478 |
|
| 479 |
|
| 480 |
def deconstruct_keys(keys) |
| 481 |
{ start_offset: start_offset, end_offset: end_offset } |
| 482 |
end |
| 483 |
|
| 484 |
|
| 485 |
def pretty_print(q) |
| 486 |
q.text("(#{start_line},#{start_column})-(#{end_line},#{end_column})") |
| 487 |
end |
| 488 |
|
| 489 |
|
| 490 |
def ==(other) |
| 491 |
Location === other && |
| 492 |
other.start_offset == start_offset && |
| 493 |
other.end_offset == end_offset |
| 494 |
end |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
def join(other) |
| 500 |
raise "Incompatible sources" if source != other.source |
| 501 |
raise "Incompatible locations" if start_offset > other.start_offset |
| 502 |
|
| 503 |
Location.new(source, start_offset, other.end_offset - start_offset) |
| 504 |
end |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
def adjoin(string) |
| 510 |
line_suffix = source.slice(end_offset, source.line_end(end_offset) - end_offset) |
| 511 |
|
| 512 |
line_suffix_index = line_suffix.byteindex(string) |
| 513 |
raise "Could not find #{string}" if line_suffix_index.nil? |
| 514 |
|
| 515 |
Location.new(source, start_offset, length + line_suffix_index + string.bytesize) |
| 516 |
end |
| 517 |
end |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
class Comment |
| 522 |
|
| 523 |
attr_reader :location |
| 524 |
|
| 525 |
|
| 526 |
def initialize(location) |
| 527 |
@location = location |
| 528 |
end |
| 529 |
|
| 530 |
|
| 531 |
def deconstruct_keys(keys) |
| 532 |
{ location: location } |
| 533 |
end |
| 534 |
|
| 535 |
|
| 536 |
def slice |
| 537 |
location.slice |
| 538 |
end |
| 539 |
end |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
class InlineComment < Comment |
| 544 |
|
| 545 |
|
| 546 |
def trailing? |
| 547 |
!location.start_line_slice.strip.empty? |
| 548 |
end |
| 549 |
|
| 550 |
|
| 551 |
def inspect |
| 552 |
"#<Prism::InlineComment @location=#{location.inspect}>" |
| 553 |
end |
| 554 |
end |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
class EmbDocComment < Comment |
| 559 |
|
| 560 |
def trailing? |
| 561 |
false |
| 562 |
end |
| 563 |
|
| 564 |
|
| 565 |
def inspect |
| 566 |
"#<Prism::EmbDocComment @location=#{location.inspect}>" |
| 567 |
end |
| 568 |
end |
| 569 |
|
| 570 |
|
| 571 |
class MagicComment |
| 572 |
|
| 573 |
attr_reader :key_loc |
| 574 |
|
| 575 |
|
| 576 |
attr_reader :value_loc |
| 577 |
|
| 578 |
|
| 579 |
def initialize(key_loc, value_loc) |
| 580 |
@key_loc = key_loc |
| 581 |
@value_loc = value_loc |
| 582 |
end |
| 583 |
|
| 584 |
|
| 585 |
def key |
| 586 |
key_loc.slice |
| 587 |
end |
| 588 |
|
| 589 |
|
| 590 |
def value |
| 591 |
value_loc.slice |
| 592 |
end |
| 593 |
|
| 594 |
|
| 595 |
def deconstruct_keys(keys) |
| 596 |
{ key_loc: key_loc, value_loc: value_loc } |
| 597 |
end |
| 598 |
|
| 599 |
|
| 600 |
def inspect |
| 601 |
"#<Prism::MagicComment @key=#{key.inspect} @value=#{value.inspect}>" |
| 602 |
end |
| 603 |
end |
| 604 |
|
| 605 |
|
| 606 |
class ParseError |
| 607 |
|
| 608 |
|
| 609 |
attr_reader :type |
| 610 |
|
| 611 |
|
| 612 |
attr_reader :message |
| 613 |
|
| 614 |
|
| 615 |
attr_reader :location |
| 616 |
|
| 617 |
|
| 618 |
attr_reader :level |
| 619 |
|
| 620 |
|
| 621 |
def initialize(type, message, location, level) |
| 622 |
@type = type |
| 623 |
@message = message |
| 624 |
@location = location |
| 625 |
@level = level |
| 626 |
end |
| 627 |
|
| 628 |
|
| 629 |
def deconstruct_keys(keys) |
| 630 |
{ type: type, message: message, location: location, level: level } |
| 631 |
end |
| 632 |
|
| 633 |
|
| 634 |
def inspect |
| 635 |
"#<Prism::ParseError @type=#{@type.inspect} @message=#{@message.inspect} @location=#{@location.inspect} @level=#{@level.inspect}>" |
| 636 |
end |
| 637 |
end |
| 638 |
|
| 639 |
|
| 640 |
class ParseWarning |
| 641 |
|
| 642 |
|
| 643 |
attr_reader :type |
| 644 |
|
| 645 |
|
| 646 |
attr_reader :message |
| 647 |
|
| 648 |
|
| 649 |
attr_reader :location |
| 650 |
|
| 651 |
|
| 652 |
attr_reader :level |
| 653 |
|
| 654 |
|
| 655 |
def initialize(type, message, location, level) |
| 656 |
@type = type |
| 657 |
@message = message |
| 658 |
@location = location |
| 659 |
@level = level |
| 660 |
end |
| 661 |
|
| 662 |
|
| 663 |
def deconstruct_keys(keys) |
| 664 |
{ type: type, message: message, location: location, level: level } |
| 665 |
end |
| 666 |
|
| 667 |
|
| 668 |
def inspect |
| 669 |
"#<Prism::ParseWarning @type=#{@type.inspect} @message=#{@message.inspect} @location=#{@location.inspect} @level=#{@level.inspect}>" |
| 670 |
end |
| 671 |
end |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
class Result |
| 677 |
|
| 678 |
attr_reader :comments |
| 679 |
|
| 680 |
|
| 681 |
attr_reader :magic_comments |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
attr_reader :data_loc |
| 687 |
|
| 688 |
|
| 689 |
attr_reader :errors |
| 690 |
|
| 691 |
|
| 692 |
attr_reader :warnings |
| 693 |
|
| 694 |
|
| 695 |
attr_reader :source |
| 696 |
|
| 697 |
|
| 698 |
def initialize(comments, magic_comments, data_loc, errors, warnings, source) |
| 699 |
@comments = comments |
| 700 |
@magic_comments = magic_comments |
| 701 |
@data_loc = data_loc |
| 702 |
@errors = errors |
| 703 |
@warnings = warnings |
| 704 |
@source = source |
| 705 |
end |
| 706 |
|
| 707 |
|
| 708 |
def deconstruct_keys(keys) |
| 709 |
{ comments: comments, magic_comments: magic_comments, data_loc: data_loc, errors: errors, warnings: warnings } |
| 710 |
end |
| 711 |
|
| 712 |
|
| 713 |
def encoding |
| 714 |
source.encoding |
| 715 |
end |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
def success? |
| 720 |
errors.empty? |
| 721 |
end |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
def failure? |
| 726 |
!success? |
| 727 |
end |
| 728 |
|
| 729 |
|
| 730 |
def code_units_cache(encoding) |
| 731 |
source.code_units_cache(encoding) |
| 732 |
end |
| 733 |
end |
| 734 |
|
| 735 |
|
| 736 |
class ParseResult < Result |
| 737 |
autoload :Comments, "prism/parse_result/comments" |
| 738 |
autoload :Errors, "prism/parse_result/errors" |
| 739 |
autoload :Newlines, "prism/parse_result/newlines" |
| 740 |
|
| 741 |
private_constant :Comments |
| 742 |
private_constant :Errors |
| 743 |
private_constant :Newlines |
| 744 |
|
| 745 |
|
| 746 |
attr_reader :value |
| 747 |
|
| 748 |
|
| 749 |
def initialize(value, comments, magic_comments, data_loc, errors, warnings, source) |
| 750 |
@value = value |
| 751 |
super(comments, magic_comments, data_loc, errors, warnings, source) |
| 752 |
end |
| 753 |
|
| 754 |
|
| 755 |
def deconstruct_keys(keys) |
| 756 |
super.merge!(value: value) |
| 757 |
end |
| 758 |
|
| 759 |
|
| 760 |
def attach_comments! |
| 761 |
Comments.new(self).attach! |
| 762 |
end |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
def mark_newlines! |
| 767 |
value.accept(Newlines.new(source.offsets.size)) |
| 768 |
end |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
def errors_format |
| 773 |
Errors.new(self).format |
| 774 |
end |
| 775 |
end |
| 776 |
|
| 777 |
|
| 778 |
class LexResult < Result |
| 779 |
|
| 780 |
attr_reader :value |
| 781 |
|
| 782 |
|
| 783 |
def initialize(value, comments, magic_comments, data_loc, errors, warnings, source) |
| 784 |
@value = value |
| 785 |
super(comments, magic_comments, data_loc, errors, warnings, source) |
| 786 |
end |
| 787 |
|
| 788 |
|
| 789 |
def deconstruct_keys(keys) |
| 790 |
super.merge!(value: value) |
| 791 |
end |
| 792 |
end |
| 793 |
|
| 794 |
|
| 795 |
class ParseLexResult < Result |
| 796 |
|
| 797 |
|
| 798 |
attr_reader :value |
| 799 |
|
| 800 |
|
| 801 |
def initialize(value, comments, magic_comments, data_loc, errors, warnings, source) |
| 802 |
@value = value |
| 803 |
super(comments, magic_comments, data_loc, errors, warnings, source) |
| 804 |
end |
| 805 |
|
| 806 |
|
| 807 |
def deconstruct_keys(keys) |
| 808 |
super.merge!(value: value) |
| 809 |
end |
| 810 |
end |
| 811 |
|
| 812 |
|
| 813 |
class Token |
| 814 |
|
| 815 |
attr_reader :source |
| 816 |
private :source |
| 817 |
|
| 818 |
|
| 819 |
attr_reader :type |
| 820 |
|
| 821 |
|
| 822 |
attr_reader :value |
| 823 |
|
| 824 |
|
| 825 |
def initialize(source, type, value, location) |
| 826 |
@source = source |
| 827 |
@type = type |
| 828 |
@value = value |
| 829 |
@location = location |
| 830 |
end |
| 831 |
|
| 832 |
|
| 833 |
def deconstruct_keys(keys) |
| 834 |
{ type: type, value: value, location: location } |
| 835 |
end |
| 836 |
|
| 837 |
|
| 838 |
def location |
| 839 |
location = @location |
| 840 |
return location if location.is_a?(Location) |
| 841 |
@location = Location.new(source, location >> 32, location & 0xFFFFFFFF) |
| 842 |
end |
| 843 |
|
| 844 |
|
| 845 |
def pretty_print(q) |
| 846 |
q.group do |
| 847 |
q.text(type.to_s) |
| 848 |
self.location.pretty_print(q) |
| 849 |
q.text("(") |
| 850 |
q.nest(2) do |
| 851 |
q.breakable("") |
| 852 |
q.pp(value) |
| 853 |
end |
| 854 |
q.breakable("") |
| 855 |
q.text(")") |
| 856 |
end |
| 857 |
end |
| 858 |
|
| 859 |
|
| 860 |
def ==(other) |
| 861 |
Token === other && |
| 862 |
other.type == type && |
| 863 |
other.value == value |
| 864 |
end |
| 865 |
|
| 866 |
|
| 867 |
def inspect |
| 868 |
location |
| 869 |
super |
| 870 |
end |
| 871 |
|
| 872 |
|
| 873 |
def deep_freeze |
| 874 |
value.freeze |
| 875 |
location.freeze |
| 876 |
freeze |
| 877 |
end |
| 878 |
end |
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
class Scope |
| 885 |
|
| 886 |
|
| 887 |
attr_reader :locals |
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
attr_reader :forwarding |
| 893 |
|
| 894 |
|
| 895 |
def initialize(locals, forwarding) |
| 896 |
@locals = locals |
| 897 |
@forwarding = forwarding |
| 898 |
end |
| 899 |
end |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
def self.scope(locals: [], forwarding: []) |
| 905 |
Scope.new(locals, forwarding) |
| 906 |
end |
| 907 |
end |