Sat, 31 Mar 2012 22:13:32 +0100
parse.lua: Update for gsoc12.csv, and *hack* the code to work with the new format
local lpeg = require "lpeg" local setmetatable, tonumber = setmetatable, tonumber; local s_char = string.char; module("csv"); local delim = lpeg.P","; local char_escape = lpeg.R"az" + lpeg.S"\\\r\n" + delim; local numeric_escape = (lpeg.R"09"^1)^-3; local escape = (lpeg.P"\\" * (char_escape + numeric_escape)); local value = (escape + (1-delim))^0; local escape_map = setmetatable({ t = "\t", b = "\b", f = "\f"; n = "\n", r = "\r", v = "\v"; }, { __index = function (_, n) if tonumber(n) then print"n" return s_char(tonumber(n)); else return n; end end }); function read_record(line, value_callback) local fieldpos = 0; local callback = function (v) return value_callback(v:gsub("\\(.)", escape_map)); end; repeat fieldpos = lpeg.match(value / callback, line, fieldpos+1); until fieldpos >= #line; end return _M;