class Asciidoctor::Table::ParserContext

finally, a new buffer is allocated to track the next cell.
instantiated, the row is closed if the cell satisifies the column count and,
is located, the previous cell is closed, an instance of Table::Cell is
moves through the lines of the table using tail recursion. When a cell boundary
class are primarily responsible for tracking the buffer of a cell as the parser
Public: Methods for managing the parsing of an AsciiDoc table. Instances of this

def activate_rowspan(rowspan, colspan)

returns nothing

determining the effective number of cells in the current row.
Internal: Activate a rowspan. The rowspan Array is consulted when
def activate_rowspan(rowspan, colspan)
  1.upto(rowspan - 1) {|i| @active_rowspans[i] = (@active_rowspans[i] || 0) + colspan }
  nil
end

def advance

the next line if the last cell had wrapped content).
Internal: Advance to the next line (which may come after the parser begins processing
def advance
  @linenum += 1
end

def buffer_has_unclosed_quotes? append = nil

isn't quoted data
returns true if the buffer has unclosed quotes, false if it doesn't or it

Public: Determines whether the buffer has unclosed quotes. Used for CSV data.
def buffer_has_unclosed_quotes? append = nil
  if (record = append ? (@buffer + append).strip : @buffer.strip) == '"'
    true
  elsif record.start_with? '"'
    if ((trailing_quote = record.end_with? '"') && (record.end_with? '""')) || (record.start_with? '""')
      ((record = record.gsub '""', '').start_with? '"') && !(record.end_with? '"')
    else
      !trailing_quote
    end
  else
    false
  end
end

def cell_closed?

returns true if the cell is marked as closed, false otherwise

Public: Checks whether the current cell has been marked as closed
def cell_closed?
  !@cell_open
end

def cell_open?

returns true if the cell is marked as open, false otherwise

Public: Checks whether the current cell is still open
def cell_open?
  @cell_open
end

def close_cell(eol = false)

returns nothing

row has been met, close the row and begin a new one.
the current row and, if the number of expected columns for the current
Public: Close the current cell, instantiate a new Table::Cell, add it to
def close_cell(eol = false)
  if @format == 'psv'
    cell_text = @buffer
    @buffer = ''
    if (cellspec = take_cellspec)
      repeat = cellspec.delete('repeatcol') || 1
    else
      logger.error message_with_context 'table missing leading separator; recovering automatically', source_location: Reader::Cursor.new(*@start_cursor_data)
      cellspec = {}
      repeat = 1
    end
  else
    cell_text = @buffer.strip
    @buffer = ''
    cellspec = nil
    repeat = 1
    if @format == 'csv' && !cell_text.empty? && cell_text.include?('"')
      # this may not be perfect logic, but it hits the 99%
      if cell_text.start_with?('"') && cell_text.end_with?('"')
        # unquote
        if (cell_text = cell_text.slice(1, cell_text.length - 2))
          # trim whitespace and collapse escaped quotes
          cell_text = cell_text.strip.squeeze('"')
        else
          logger.error message_with_context 'unclosed quote in CSV data; setting cell to empty', source_location: @reader.cursor_at_prev_line
          cell_text = ''
        end
      else
        # collapse escaped quotes
        cell_text = cell_text.squeeze('"')
      end
    end
  end
  1.upto(repeat) do |i|
    # TODO make column resolving an operation
    if @colcount == -1
      @table.columns << (column = Table::Column.new(@table, @table.columns.size + i - 1))
      if cellspec && (cellspec.key? 'colspan') && (extra_cols = cellspec['colspan'].to_i - 1) > 0
        offset = @table.columns.size
        extra_cols.times do |j|
          @table.columns << Table::Column.new(@table, offset + j)
        end
      end
    else
      # QUESTION is this right for cells that span columns?
      unless (column = @table.columns[@current_row.size])
        logger.error message_with_context 'dropping cell because it exceeds specified number of columns', source_location: @reader.cursor_before_mark
        return
      end
    end
    cell = Table::Cell.new(column, cell_text, cellspec, cursor: @reader.cursor_before_mark)
    @reader.mark
    unless !cell.rowspan || cell.rowspan == 1
      activate_rowspan(cell.rowspan, (cell.colspan || 1))
    end
    @column_visits += (cell.colspan || 1)
    @current_row << cell
    # don't close the row if we're on the first line and the column count has not been set explicitly
    # TODO perhaps the colcount/linenum logic should be in end_of_row? (or a should_end_row? method)
    close_row if end_of_row? && (@colcount != -1 || @linenum > 0 || (eol && i == repeat))
  end
  @cell_open = false
  nil
end

def close_open_cell(next_cellspec = {})

returns nothing

by the next cell.
cell spec captured from the end of this cell onto the stack for use
Public: If the current cell is open, close it. In additional, push the
def close_open_cell(next_cellspec = {})
  push_cellspec next_cellspec
  close_cell(true) if cell_open?
  advance
  nil
end

def close_row

returns nothing

Array and counter variables.
Internal: Close the row by adding it to the Table and resetting the row
def close_row
  @table.rows.body << @current_row
  # don't have to account for active rowspans here
  # since we know this is first row
  @colcount = @column_visits if @colcount == -1
  @column_visits = 0
  @current_row = []
  @active_rowspans.shift
  @active_rowspans[0] ||= 0
  nil
end

def effective_column_visits

cells plus any active rowspans.
Internal: Calculate the effective column visits, which consists of the number of
def effective_column_visits
  @column_visits + @active_rowspans[0]
end

def end_of_row?

Internal: Check whether we've met the number of effective columns for the current row.
def end_of_row?
  @colcount == -1 || effective_column_visits == @colcount
end

def initialize reader, table, attributes = {}

def initialize reader, table, attributes = {}
  @start_cursor_data = (@reader = reader).mark
  @table = table
  if attributes.key? 'format'
    if FORMATS.include?(xsv = attributes['format'])
      if xsv == 'tsv'
        # NOTE tsv is just an alias for csv with a tab separator
        @format = 'csv'
      elsif (@format = xsv) == 'psv' && table.document.nested?
        xsv = '!sv'
      end
    else
      logger.error message_with_context %(illegal table format: #{xsv}), source_location: reader.cursor_at_prev_line
      @format, xsv = 'psv', (table.document.nested? ? '!sv' : 'psv')
    end
  else
    @format, xsv = 'psv', (table.document.nested? ? '!sv' : 'psv')
  end
  if attributes.key? 'separator'
    if (sep = attributes['separator']).nil_or_empty?
      @delimiter, @delimiter_rx = DELIMITERS[xsv]
    # QUESTION should we support any other escape codes or multiple tabs?
    elsif sep == '\t'
      @delimiter, @delimiter_rx = DELIMITERS['tsv']
    else
      @delimiter, @delimiter_rx = sep, /#{::Regexp.escape sep}/
    end
  else
    @delimiter, @delimiter_rx = DELIMITERS[xsv]
  end
  @colcount = table.columns.empty? ? -1 : table.columns.size
  @buffer = ''
  @cellspecs = []
  @cell_open = false
  @active_rowspans = [0]
  @column_visits = 0
  @current_row = []
  @linenum = -1
end

def keep_cell_open

returns nothing

reached and the cell may contain additional text.
Public: Marks that the cell should be kept open. Used when the end of the line is
def keep_cell_open
  @cell_open = true
  nil
end

def mark_cell_closed

returns nothing

instance and add it to the current row.
Public: Marks the cell as closed so that the parser knows to instantiate a new cell
def mark_cell_closed
  @cell_open = false
  nil
end

def match_delimiter(line)

returns Regexp MatchData if the line contains the delimiter, false otherwise

used by this table.
Public: Checks whether the line provided contains the cell delimiter
def match_delimiter(line)
  @delimiter_rx.match(line)
end

def push_cellspec(cellspec = {})

returns nothing

stack is used to carry over the spec to the next cell.
Public: Puts a cell spec onto the stack. Cell specs precede the delimiter, so a
def push_cellspec(cellspec = {})
  # this shouldn't be nil, but we check anyway
  @cellspecs << (cellspec || {})
  nil
end

def skip_past_delimiter(pre)

Returns nothing

Public: Skip past the matched delimiter because it's inside quoted text.
def skip_past_delimiter(pre)
  @buffer = %(#{@buffer}#{pre}#{@delimiter})
  nil
end

def skip_past_escaped_delimiter(pre)

Returns nothing

Public: Skip past the matched delimiter because it's escaped.
def skip_past_escaped_delimiter(pre)
  @buffer = %(#{@buffer}#{pre.chop}#{@delimiter})
  nil
end

def starts_with_delimiter?(line)

returns true if the line starts with the delimiter, false otherwise

used by this table.
Public: Checks whether the line provided starts with the cell delimiter
def starts_with_delimiter?(line)
  line.start_with? @delimiter
end

def take_cellspec

returns The cell spec Hash captured from parsing the previous cell

when the cell is being closed.
stack is used to carry over the spec from the previous cell to the current cell
Public: Takes a cell spec from the stack. Cell specs precede the delimiter, so a
def take_cellspec
  @cellspecs.shift
end