module CsvParser

Autogenerated from a Treetop grammar. Edits may be lost.

Constants

VERSION

Public Class Methods

error(type, line, column, msg = nil) click to toggle source
# File lib/csv_parser.rb, line 43
def self.error(type, line, column, msg = nil)
  klass, msg =
    case type
    when :missing_quote
      [MissingQuoteError, "no ending quote found for quote on line #{line}, column #{column}"]
    when :stray_quote
      [StrayQuoteError, "invalid quote found on line #{line}, column #{column}"]
    when :missing_fields
      [MissingFieldsError, "record on line #{line} had too few fields"]
    when :extra_fields
      [ExtraFieldsError, "record on line #{line} had too many fields"]
    else
      Error
    end

  klass.new(msg, line, column)
end
parse(data, options = {}) click to toggle source
# File lib/csv_parser.rb, line 26
def self.parse(data, options = {})
  parser = ::CsvParser::CsvParser.new
  options.each_pair do |key, value|
    parser.send("#{key}=", value)
  end
  result = parser.parse(data)
  if result
    warnings = parser.warnings.collect do |(desc, line, col)|
      error(desc, line, col)
    end
    Result.new(result.value, warnings)
  else
    raise error(parser.failure_type, parser.failure_line,
                parser.failure_column, parser.failure_reason)
  end
end