| 1 |
-- tolua: package class |
| 2 |
-- Written by Waldemar Celes |
| 3 |
-- TeCGraf/PUC-Rio |
| 4 |
-- Jul 1998 |
| 5 |
-- $Id: $ |
| 6 |
|
| 7 |
-- This code is free software; you can redistribute it and/or modify it. |
| 8 |
-- The software provided hereunder is on an "as is" basis, and |
| 9 |
-- the author has no obligation to provide maintenance, support, updates, |
| 10 |
-- enhancements, or modifications. |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
-- Package class |
| 15 |
-- Represents the whole package being bound. |
| 16 |
-- The following fields are stored: |
| 17 |
-- {i} = list of objects in the package. |
| 18 |
classPackage = { |
| 19 |
classtype = 'package' |
| 20 |
} |
| 21 |
classPackage.__index = classPackage |
| 22 |
setmetatable(classPackage, classContainer) |
| 23 |
|
| 24 |
-- Print method |
| 25 |
function classPackage:print() |
| 26 |
print("Package: " .. self.name) |
| 27 |
local i = 1 |
| 28 |
while self[i] do |
| 29 |
self[i]:print("", "") |
| 30 |
i = i + 1 |
| 31 |
end |
| 32 |
end |
| 33 |
|
| 34 |
function classPackage:preprocess() |
| 35 |
-- avoid preprocessing embedded Lua code |
| 36 |
local L = {} |
| 37 |
self.code = gsub(self.code, "\n%s*%$%[", "\1") -- deal with embedded lua code |
| 38 |
self.code = gsub(self.code, "\n%s*%$%]", "\2") |
| 39 |
self.code = gsub(self.code, "(%b\1\2)", function(c) |
| 40 |
tinsert(L, c) |
| 41 |
return "\n#[" .. #L .. "]#" |
| 42 |
end) |
| 43 |
-- avoid preprocessing embedded C code |
| 44 |
local C = {} |
| 45 |
self.code = gsub(self.code, "\n%s*%$%<", "\3") -- deal with embedded C code |
| 46 |
self.code = gsub(self.code, "\n%s*%$%>", "\4") |
| 47 |
self.code = gsub(self.code, "(%b\3\4)", function(c) |
| 48 |
tinsert(C, c) |
| 49 |
return "\n#<" .. #C .. ">#" |
| 50 |
end) |
| 51 |
-- avoid preprocessing embedded C code |
| 52 |
self.code = gsub(self.code, "\n%s*%$%{", "\5") -- deal with embedded C code |
| 53 |
self.code = gsub(self.code, "\n%s*%$%}", "\6") |
| 54 |
self.code = gsub(self.code, "(%b\5\6)", function(c) |
| 55 |
tinsert(C, c) |
| 56 |
return "\n#<" .. #C .. ">#" |
| 57 |
end) |
| 58 |
|
| 59 |
--self.code = gsub(self.code,"\n%s*#[^d][^\n]*\n", "\n\n") -- eliminate preprocessor directives that don't start with 'd' |
| 60 |
self.code = gsub(self.code, "\n[ \t]*#[ \t]*[^d%<%[]", "\n//") -- eliminate preprocessor directives that don't start with 'd' |
| 61 |
|
| 62 |
-- avoid preprocessing verbatim lines |
| 63 |
local V = {} |
| 64 |
self.code = gsub(self.code, "\n(%s*%$[^%[%]][^\n]*)", function(v) |
| 65 |
tinsert(V, v) |
| 66 |
return "\n#" .. #V .. "#" |
| 67 |
end) |
| 68 |
|
| 69 |
-- perform global substitution |
| 70 |
|
| 71 |
self.code = gsub(self.code, "(//[^\n]*)", "") -- eliminate C++ comments |
| 72 |
self.code = gsub(self.code, "/%*", "\1") |
| 73 |
self.code = gsub(self.code, "%*/", "\2") |
| 74 |
self.code = gsub(self.code, "%b\1\2", "") |
| 75 |
self.code = gsub(self.code, "\1", "/%*") |
| 76 |
self.code = gsub(self.code, "\2", "%*/") |
| 77 |
self.code = gsub(self.code, "%s*@%s*", "@") -- eliminate spaces beside @ |
| 78 |
self.code = gsub(self.code, "%s?inline(%s)", "%1") -- eliminate 'inline' keyword |
| 79 |
--self.code = gsub(self.code,"%s?extern(%s)","%1") -- eliminate 'extern' keyword |
| 80 |
--self.code = gsub(self.code,"%s?virtual(%s)","%1") -- eliminate 'virtual' keyword |
| 81 |
--self.code = gsub(self.code,"public:","") -- eliminate 'public:' keyword |
| 82 |
self.code = gsub(self.code, "([^%w_])void%s*%*", "%1_userdata ") -- substitute 'void*' |
| 83 |
self.code = gsub(self.code, "([^%w_])void%s*%*", "%1_userdata ") -- substitute 'void*' |
| 84 |
self.code = gsub(self.code, "([^%w_])char%s*%*", "%1_cstring ") -- substitute 'char*' |
| 85 |
self.code = gsub(self.code, "([^%w_])lua_State%s*%*", "%1_lstate ") -- substitute 'lua_State*' |
| 86 |
|
| 87 |
-- restore embedded Lua code |
| 88 |
self.code = gsub(self.code, "%#%[(%d+)%]%#", function(n) |
| 89 |
return L[tonumber(n)] |
| 90 |
end) |
| 91 |
-- restore embedded C code |
| 92 |
self.code = gsub(self.code, "%#%<(%d+)%>%#", function(n) |
| 93 |
return C[tonumber(n)] |
| 94 |
end) |
| 95 |
-- restore verbatim lines |
| 96 |
self.code = gsub(self.code, "%#(%d+)%#", function(n) |
| 97 |
return V[tonumber(n)] |
| 98 |
end) |
| 99 |
|
| 100 |
self.code = string.gsub(self.code, "\n%s*%$([^\n]+)", function(l) |
| 101 |
Verbatim(l .. "\n") |
| 102 |
return "\n" |
| 103 |
end) |
| 104 |
end |
| 105 |
|
| 106 |
-- translate verbatim |
| 107 |
function classPackage:preamble() |
| 108 |
output('/*\n') |
| 109 |
output('** Lua binding: ' .. self.name .. '\n') |
| 110 |
output('** Generated automatically by ' .. TOLUA_VERSION .. '.\n') |
| 111 |
output('*/\n\n') |
| 112 |
|
| 113 |
output('#ifndef __cplusplus\n') |
| 114 |
output('#include "stdlib.h"\n') |
| 115 |
output('#endif\n') |
| 116 |
output('#include "string.h"\n\n') |
| 117 |
output('#include "tolua++.h"\n\n') |
| 118 |
|
| 119 |
if not flags.h then |
| 120 |
output('/* Exported function */') |
| 121 |
output('TOLUA_API int tolua_' .. self.name .. '_open (lua_State* tolua_S);') |
| 122 |
output('\n') |
| 123 |
end |
| 124 |
|
| 125 |
local i = 1 |
| 126 |
while self[i] do |
| 127 |
self[i]:preamble() |
| 128 |
i = i + 1 |
| 129 |
end |
| 130 |
|
| 131 |
if self:requirecollection(_collect) then |
| 132 |
output('\n') |
| 133 |
output('/* function to release collected object via destructor */') |
| 134 |
output('#ifdef __cplusplus\n') |
| 135 |
for i, v in op:ordered_pairs(_collect) do |
| 136 |
output('\nstatic int ' .. v .. ' (lua_State* tolua_S)') |
| 137 |
output('{') |
| 138 |
output(' ' .. i .. '* self = (' .. i .. '*) tolua_tousertype(tolua_S,1,0);') |
| 139 |
output(' Mtolua_delete(self);') |
| 140 |
output(' return 0;') |
| 141 |
output('}') |
| 142 |
end |
| 143 |
output('#endif\n\n') |
| 144 |
end |
| 145 |
|
| 146 |
output('\n') |
| 147 |
output('/* function to register type */') |
| 148 |
output('static void tolua_reg_types (lua_State* tolua_S)') |
| 149 |
output('{') |
| 150 |
|
| 151 |
if flags.t then |
| 152 |
output("#ifndef Mtolua_typeid\n#define Mtolua_typeid(L,TI,T)\n#endif\n") |
| 153 |
end |
| 154 |
for n, v in op:ordered_pairs(_usertype) do |
| 155 |
if (not _global_classes[v]) or _global_classes[v]:check_public_access() then |
| 156 |
output(' tolua_usertype(tolua_S,"', v, '");') |
| 157 |
if flags.t then |
| 158 |
output(' Mtolua_typeid(tolua_S,typeid(', v, '), "', v, '");') |
| 159 |
end |
| 160 |
end |
| 161 |
end |
| 162 |
output('}') |
| 163 |
output('\n') |
| 164 |
end |
| 165 |
|
| 166 |
-- register package |
| 167 |
-- write package open function |
| 168 |
function classPackage:register(pre) |
| 169 |
pre = pre or '' |
| 170 |
push(self) |
| 171 |
output(pre .. "/* Open function */") |
| 172 |
output(pre .. "TOLUA_API int tolua_" .. self.name .. "_open (lua_State* tolua_S)") |
| 173 |
output(pre .. "{") |
| 174 |
output(pre .. " tolua_open(tolua_S);") |
| 175 |
output(pre .. " tolua_reg_types(tolua_S);") |
| 176 |
output(pre .. " tolua_module(tolua_S,NULL,", self:hasvar(), ");") |
| 177 |
output(pre .. " tolua_beginmodule(tolua_S,NULL);") |
| 178 |
local i = 1 |
| 179 |
while self[i] do |
| 180 |
self[i]:register(pre .. " ") |
| 181 |
i = i + 1 |
| 182 |
end |
| 183 |
output(pre .. " tolua_endmodule(tolua_S);") |
| 184 |
output(pre .. " return 1;") |
| 185 |
output(pre .. "}") |
| 186 |
|
| 187 |
output("\n\n") |
| 188 |
output("#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501\n"); |
| 189 |
output(pre .. "TOLUA_API int luaopen_" .. self.name .. " (lua_State* tolua_S) {") |
| 190 |
output(pre .. " return tolua_" .. self.name .. "_open(tolua_S);") |
| 191 |
output(pre .. "}") |
| 192 |
output("#endif\n\n") |
| 193 |
|
| 194 |
pop() |
| 195 |
end |
| 196 |
|
| 197 |
-- write header file |
| 198 |
function classPackage:header() |
| 199 |
output('/*\n') |
| 200 |
output('** Lua binding: ' .. self.name .. '\n') |
| 201 |
output('** Generated automatically by ' .. TOLUA_VERSION .. ' on ' .. date() .. '.\n') |
| 202 |
output('*/\n\n') |
| 203 |
|
| 204 |
if not flags.h then |
| 205 |
output('/* Exported function */') |
| 206 |
output('TOLUA_API int tolua_' .. self.name .. '_open (lua_State* tolua_S);') |
| 207 |
output('\n') |
| 208 |
end |
| 209 |
end |
| 210 |
|
| 211 |
-- Internal constructor |
| 212 |
function _Package(self) |
| 213 |
setmetatable(self, classPackage) |
| 214 |
return self |
| 215 |
end |
| 216 |
|
| 217 |
-- Parse C header file with tolua directives |
| 218 |
-- *** Thanks to Ariel Manzur for fixing bugs in nested directives *** |
| 219 |
function extract_code(fn, s) |
| 220 |
local code = '\n$#include "' .. fn .. '"\n' |
| 221 |
s = "\n" .. s .. "\n" -- add blank lines as sentinels |
| 222 |
local _, e, c, t = strfind(s, "\n([^\n]-)[Tt][Oo][Ll][Uu][Aa]_([^%s]*)[^\n]*\n") |
| 223 |
while e do |
| 224 |
t = strlower(t) |
| 225 |
if t == "begin" then |
| 226 |
_, e, c = strfind(s, "(.-)\n[^\n]*[Tt][Oo][Ll][Uu][Aa]_[Ee][Nn][Dd][^\n]*\n", e) |
| 227 |
if not e then |
| 228 |
tolua_error("Unbalanced 'tolua_begin' directive in header file") |
| 229 |
end |
| 230 |
end |
| 231 |
code = code .. c .. "\n" |
| 232 |
_, e, c, t = strfind(s, "\n([^\n]-)[Tt][Oo][Ll][Uu][Aa]_([^%s]*)[^\n]*\n", e) |
| 233 |
end |
| 234 |
return code |
| 235 |
end |
| 236 |
|
| 237 |
-- Constructor |
| 238 |
-- Expects the package name, the file extension, and the file text. |
| 239 |
function Package(name, fn) |
| 240 |
local ext = "pkg" |
| 241 |
|
| 242 |
-- open input file, if any |
| 243 |
local st, msg |
| 244 |
if fn then |
| 245 |
st, msg = readfrom(flags.f) |
| 246 |
if not st then |
| 247 |
error('#' .. msg) |
| 248 |
end |
| 249 |
local _; _, _, ext = strfind(fn, ".*%.(.*)$") |
| 250 |
end |
| 251 |
local code |
| 252 |
if ext == 'pkg' then |
| 253 |
code = prep(st) |
| 254 |
else |
| 255 |
code = "\n" .. read('*a') |
| 256 |
if ext == 'h' or ext == 'hpp' then |
| 257 |
code = extract_code(fn, code) |
| 258 |
end |
| 259 |
end |
| 260 |
|
| 261 |
-- close file |
| 262 |
if fn then |
| 263 |
readfrom() |
| 264 |
end |
| 265 |
|
| 266 |
-- deal with include directive |
| 267 |
local nsubst |
| 268 |
repeat |
| 269 |
code, nsubst = gsub(code, '\n%s*%$(.)file%s*"(.-)"([^\n]*)\n', |
| 270 |
function(kind, fn, extra) |
| 271 |
local _, _, ext = strfind(fn, ".*%.(.*)$") |
| 272 |
local fp, msg = openfile(fn, 'r') |
| 273 |
if not fp then |
| 274 |
error('#' .. msg .. ': ' .. fn) |
| 275 |
end |
| 276 |
if kind == 'p' then |
| 277 |
local s = prep(fp) |
| 278 |
closefile(fp) |
| 279 |
return s |
| 280 |
end |
| 281 |
local s = read(fp, '*a') |
| 282 |
closefile(fp) |
| 283 |
if kind == 'c' or kind == 'h' then |
| 284 |
return extract_code(fn, s) |
| 285 |
elseif kind == 'l' then |
| 286 |
return "\n$[--##" .. fn .. "\n" .. s .. "\n$]\n" |
| 287 |
elseif kind == 'i' then |
| 288 |
local t = { code = s } |
| 289 |
extra = string.gsub(extra, "^%s*,%s*", "") |
| 290 |
local pars = split_c_tokens(extra, ",") |
| 291 |
include_file_hook(t, fn, table.unpack(pars)) |
| 292 |
return "\n\n" .. t.code |
| 293 |
else |
| 294 |
error('#Invalid include directive (use $cfile, $pfile, $lfile or $ifile)') |
| 295 |
end |
| 296 |
end) |
| 297 |
until nsubst == 0 |
| 298 |
|
| 299 |
-- deal with renaming directive |
| 300 |
repeat -- I don't know why this is necesary |
| 301 |
code, nsubst = gsub(code, '\n%s*%$renaming%s*(.-)%s*\n', function(r) |
| 302 |
appendrenaming(r) |
| 303 |
return "\n" |
| 304 |
end) |
| 305 |
until nsubst == 0 |
| 306 |
|
| 307 |
local t = _Package(_Container { name = name, code = code }) |
| 308 |
push(t) |
| 309 |
preprocess_hook(t) |
| 310 |
t:preprocess() |
| 311 |
preparse_hook(t) |
| 312 |
t:parse(t.code) |
| 313 |
pop() |
| 314 |
return t |
| 315 |
end |
| 316 |
|
| 317 |
setmetatable(_extra_parameters, { __index = _G }) |
| 318 |
|
| 319 |
function prep(file) |
| 320 |
local chunk = { 'local __ret = {"\\n"}\n' } |
| 321 |
for line in file:lines() do |
| 322 |
if string.find(line, "^##") then |
| 323 |
table.insert(chunk, string.sub(line, 3) .. "\n") |
| 324 |
else |
| 325 |
local last = 1 |
| 326 |
for text, expr, index in string.gmatch(line, "(.-)$(%b())()") do |
| 327 |
last = index |
| 328 |
if text ~= "" then |
| 329 |
table.insert(chunk, string.format('table.insert(__ret, %q )', text)) |
| 330 |
end |
| 331 |
table.insert(chunk, string.format('table.insert(__ret, %s )', expr)) |
| 332 |
end |
| 333 |
table.insert(chunk, string.format('table.insert(__ret, %q)\n', |
| 334 |
string.sub(line, last) .. "\n")) |
| 335 |
end |
| 336 |
end |
| 337 |
table.insert(chunk, '\nreturn table.concat(__ret)\n') |
| 338 |
local f, e = load(table.concat(chunk), nil, "t", _extra_parameters) |
| 339 |
if e then |
| 340 |
error("#" .. e) |
| 341 |
end |
| 342 |
return f() |
| 343 |
end |