class Parser::Lexer::Literal

def nest_and_try_closing(delimiter, ts, te)

def nest_and_try_closing(delimiter, ts, te)
  delimiter = coerce_encoding(delimiter)
  if @start_delim && @start_delim == delimiter
    @nesting += 1
  elsif delimiter?(delimiter)
    @nesting -= 1
  end
  # Finalize if last matching delimiter is closed.
  if @nesting == 0
    if words?
      extend_space(ts, ts)
    end
    # Emit the string as a single token if it's applicable.
    if @monolithic
      emit(MONOLITHIC[@start_tok], @buffer, @str_s, te)
    else
      # If this is a heredoc, @buffer contains the sentinel now.
      # Just throw it out. Lexer flushes the heredoc after each
      # non-heredoc-terminating \n anyway, so no data will be lost.
      flush_string unless heredoc?
      emit(:tSTRING_END, @end_delim, ts, te)
    end
  end
end