# HG changeset patch # User Matthew Wild # Date 1257006511 0 # Node ID fc93ff84350dfc9277392160dd517627b7bf436a Initial commit diff -r 000000000000 -r fc93ff84350d tsv.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tsv.lua Sat Oct 31 16:28:31 2009 +0000 @@ -0,0 +1,40 @@ + +local lpeg = require "lpeg" +local setmetatable, tonumber = + setmetatable, tonumber; +local s_char = string.char; + +module("tsv"); + +local delim = lpeg.P"\t"; + +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;