class RDoc::Markup::Parser

def build_list(margin)

def build_list(margin)
  p :list_start => margin if @debug
  list = RDoc::Markup::List.new
  label = nil
  until @tokens.empty? do
    type, data, column, = get
    case type
    when *LIST_TOKENS then
      if column < margin || (list.type && list.type != type) then
        unget
        break
      end
      list.type = type
      peek_type, _, column, = peek_token
      case type
      when :NOTE, :LABEL then
        label = [] unless label
        if peek_type == :NEWLINE then
          # description not on the same line as LABEL/NOTE
          # skip the trailing newline & any blank lines below
          while peek_type == :NEWLINE
            get
            peek_type, _, column, = peek_token
          end
          # we may be:
          #   - at end of stream
          #   - at a column < margin:
          #         [text]
          #       blah blah blah
          #   - at the same column, but with a different type of list item
          #       [text]
          #       * blah blah
          #   - at the same column, with the same type of list item
          #       [one]
          #       [two]
          # In all cases, we have an empty description.
          # In the last case only, we continue.
          if peek_type.nil? || column < margin then
            empty = true
          elsif column == margin then
            case peek_type
            when type
              empty = :continue
            when *LIST_TOKENS
              empty = true
            else
              empty = false
            end
          else
            empty = false
          end
          if empty then
            label << data
            next if empty == :continue
            break
          end
        end
      else
        data = nil
      end
      if label then
        data = label << data
        label = nil
      end
      list_item = RDoc::Markup::ListItem.new data
      parse list_item, column
      list << list_item
    else
      unget
      break
    end
  end
  p :list_end => margin if @debug
  if list.empty? then
    return nil unless label
    return nil unless [:LABEL, :NOTE].include? list.type
    list_item = RDoc::Markup::ListItem.new label, RDoc::Markup::BlankLine.new
    list << list_item
  end
  list
end