Skip to content

Instantly share code, notes, and snippets.

@creationix
Created February 2, 2012 22:42
Show Gist options
  • Save creationix/1726260 to your computer and use it in GitHub Desktop.
Save creationix/1726260 to your computer and use it in GitHub Desktop.
local Path = require('path')
local FS = require('fs')
local function search(code, pattern)
local data = {}
for pos, k, v in code:gmatch(pattern) do
local sub = code:sub(1, pos)
local comment = sub:match("\n-- ([^\n]+)\n$")
if not comment then
local long = sub:match("\n--%[(%b[])%]\n$")
if long then
comment = long:sub(3, #long-2)
end
end
data[k] = {v, comment}
p(k, data[k])
end
return data
end
local function parse(file)
local code = FS.read_file_sync(file .. ".lua")
local name = code:match("\nreturn%s+([_%a][_%w]*)")
if not name then error("Can't find exports variable") end
if not (name == Path.basename(file)) then error("exports variable must match filename") end
local variables = search(code, "()\nlocal ([_%a][_%w]*) = ([^\n]*)\n")
local functions = search(code, "()\nfunction ([_%a][_%w]*[.:][_%a][_%w]*)%(([^)]*)%)\n")
local props = search(code, "()\n([_%a][_%w]*[.:][_%a][_%w]*) = ([^\n]*)\n")
p{
name = name,
variables = variables,
functions = functions,
props = props
}
end
parse(process.argv[1] or "shapes")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment