Advertisement
Guest User

Untitled

a guest
Jul 8th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.42 KB | None | 0 0
  1. local Statements = {}
  2.  
  3. function Statements:_add(name, stmtarr)
  4.   if self.values[name] then
  5.     return "Statement '", name, "' already exists."
  6.   end
  7.   local stmt = table.concat(stmtarr, "\n")
  8.   self.values[name] = stmt
  9. end
  10.  
  11. function Statements:_assert(lineN, ...)
  12.   if ... then
  13.     error("Error parsing "..self.curFile..". Line #"..lineN.. ". "..
  14.       table.concat({...}, ""))
  15.   else return lineN, ... end
  16. end
  17.  
  18. function Statements:_addFile(file)
  19.   self.curFile = file
  20.   local name, stmtarr = nil, {}
  21.   local first = true
  22.   local lineN = 0
  23.  
  24.   for line in io.lines(file) do
  25.     lineN = lineN + 1
  26.     if first and line:match("^%s*$") then goto continue end
  27.     local commentName = line:match("^--!%s*(.+)$")
  28.     if commentName then
  29.       if first then first = false
  30.       else self:_assert(lineN, self:_add(name, stmtarr)) end
  31.       name, stmtarr = commentName, {}
  32.     else
  33.       if first then self:_assert(lineN, "Meets unnamed SQL statement") end
  34.       table.insert(stmtarr, line)
  35.     end
  36.     ::continue::
  37.   end
  38.  
  39.   self:_assert(lineN, self:_add(name, stmtarr))
  40. end
  41.  
  42. function Statements:new(...)
  43.   local o = { values = {} }
  44.   setmetatable(o, self)
  45.   self.__index = self
  46.   if ... then o:addFiles(...) end
  47.   return o
  48. end
  49.  
  50. function Statements:addFiles(...)
  51.   local files = {...}
  52.   if #files == 0 then error('No files to load') end
  53.   for _,f in ipairs(files) do self:_addFile(f) end
  54. end
  55.  
  56. return Statements
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement