Class: YARP::LexCompat::Heredoc::DedentingHeredoc
- Inherits:
-
Object
- Object
- YARP::LexCompat::Heredoc::DedentingHeredoc
- Defined in:
- lib/yarp/lex_compat.rb
Overview
Heredocs that are dedenting heredocs are a little more complicated. Ripper outputs on_ignored_sp tokens for the whitespace that is being removed from the output. YARP only modifies the node itself and keeps the token the same. This simplifies YARP, but makes comparing against Ripper much harder because there is a length mismatch.
Fortunately, we already have to pull out the heredoc tokens in order to insert them into the stream in the correct order. As such, we can do some extra manipulation on the tokens to make them match Ripper’s output by mirroring the dedent logic that Ripper uses.
Constant Summary collapse
- TAB_WIDTH =
8
Instance Attribute Summary collapse
-
#dedent ⇒ Object
readonly
Returns the value of attribute dedent.
-
#dedent_next ⇒ Object
readonly
Returns the value of attribute dedent_next.
-
#embexpr_balance ⇒ Object
readonly
Returns the value of attribute embexpr_balance.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Instance Method Summary collapse
-
#<<(token) ⇒ Object
As tokens are coming in, we track the minimum amount of common leading whitespace on plain string content tokens.
-
#initialize ⇒ DedentingHeredoc
constructor
A new instance of DedentingHeredoc.
- #to_a ⇒ Object
Constructor Details
#initialize ⇒ DedentingHeredoc
Returns a new instance of DedentingHeredoc.
346 347 348 349 350 351 |
# File 'lib/yarp/lex_compat.rb', line 346 def initialize @tokens = [] @dedent_next = true @dedent = nil @embexpr_balance = 0 end |
Instance Attribute Details
#dedent ⇒ Object (readonly)
Returns the value of attribute dedent.
344 345 346 |
# File 'lib/yarp/lex_compat.rb', line 344 def dedent @dedent end |
#dedent_next ⇒ Object (readonly)
Returns the value of attribute dedent_next.
344 345 346 |
# File 'lib/yarp/lex_compat.rb', line 344 def dedent_next @dedent_next end |
#embexpr_balance ⇒ Object (readonly)
Returns the value of attribute embexpr_balance.
344 345 346 |
# File 'lib/yarp/lex_compat.rb', line 344 def embexpr_balance @embexpr_balance end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
344 345 346 |
# File 'lib/yarp/lex_compat.rb', line 344 def tokens @tokens end |
Instance Method Details
#<<(token) ⇒ Object
As tokens are coming in, we track the minimum amount of common leading whitespace on plain string content tokens. This allows us to later remove that amount of whitespace from the beginning of each line.
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 |
# File 'lib/yarp/lex_compat.rb', line 356 def <<(token) case token.event when :on_embexpr_beg, :on_heredoc_beg @embexpr_balance += 1 when :on_embexpr_end, :on_heredoc_end @embexpr_balance -= 1 when :on_tstring_content if embexpr_balance == 0 token.value.split(/(?<=\n)/).each_with_index do |line, index| next if line.strip.empty? && line.end_with?("\n") next if !(dedent_next || index > 0) leading = line[/\A(\s*)\n?/, 1] next_dedent = 0 leading.each_char do |char| if char == "\t" next_dedent = next_dedent - (next_dedent % TAB_WIDTH) + TAB_WIDTH else next_dedent += 1 end end @dedent = [dedent, next_dedent].compact.min end end end @dedent_next = token.event == :on_tstring_content && embexpr_balance == 0 tokens << token end |
#to_a ⇒ Object
388 389 390 391 392 393 394 395 396 397 398 399 400 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 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/yarp/lex_compat.rb', line 388 def to_a # If every line in the heredoc is blank, we still need to split up the # string content token into multiple tokens. if dedent.nil? results = [] embexpr_balance = 0 tokens.each do |token| case token.event when :on_embexpr_beg, :on_heredoc_beg embexpr_balance += 1 results << token when :on_embexpr_end, :on_heredoc_end embexpr_balance -= 1 results << token when :on_tstring_content if embexpr_balance == 0 lineno = token[0][0] column = token[0][1] token.value.split(/(?<=\n)/).each_with_index do |value, index| column = 0 if index > 0 results << Token.new([[lineno, column], :on_tstring_content, value, token.state]) lineno += 1 end else results << token end else results << token end end return results end # Otherwise, we're going to run through each token in the list and # insert on_ignored_sp tokens for the amount of dedent that we need to # perform. We also need to remove the dedent from the beginning of # each line of plain string content tokens. results = [] dedent_next = true embexpr_balance = 0 tokens.each do |token| # Notice that the structure of this conditional largely matches the # whitespace calculation we performed above. This is because # checking if the subsequent token needs to be dedented is common to # both the dedent calculation and the ignored_sp insertion. case token.event when :on_embexpr_beg embexpr_balance += 1 results << token when :on_embexpr_end embexpr_balance -= 1 results << token when :on_tstring_content if embexpr_balance == 0 # Here we're going to split the string on newlines, but maintain # the newlines in the resulting array. We'll do that with a look # behind assertion. splits = token.value.split(/(?<=\n)/) index = 0 while index < splits.length line = splits[index] lineno = token[0][0] + index column = token[0][1] # Blank lines do not count toward common leading whitespace # calculation and do not need to be dedented. if dedent_next || index > 0 column = 0 end # If the dedent is 0 and we're not supposed to dedent the next # line or this line doesn't start with whitespace, then we # should concatenate the rest of the string to match ripper. if dedent == 0 && (!dedent_next || !line.start_with?(/\s/)) line = splits[index..].join index = splits.length end # If we are supposed to dedent this line or if this is not the # first line of the string and this line isn't entirely blank, # then we need to insert an on_ignored_sp token and remove the # dedent from the beginning of the line. if (dedent > 0) && (dedent_next || index > 0) deleting = 0 deleted_chars = [] # Gather up all of the characters that we're going to # delete, stopping when you hit a character that would put # you over the dedent amount. line.each_char.with_index do |char, i| case char when "\r" if line.chars[i + 1] == "\n" break end when "\n" break when "\t" deleting = deleting - (deleting % TAB_WIDTH) + TAB_WIDTH else deleting += 1 end break if deleting > dedent deleted_chars << char end # If we have something to delete, then delete it from the # string and insert an on_ignored_sp token. if deleted_chars.any? ignored = deleted_chars.join line.delete_prefix!(ignored) results << Token.new([[lineno, 0], :on_ignored_sp, ignored, token[3]]) column = ignored.length end end results << Token.new([[lineno, column], token[1], line, token[3]]) unless line.empty? index += 1 end else results << token end else results << token end dedent_next = ((token.event == :on_tstring_content) || (token.event == :on_heredoc_end)) && embexpr_balance == 0 end results end |