Changeset 3127 for code/branches/pch/src/tolua/lua/package.lua
- Timestamp:
- Jun 9, 2009, 4:29:42 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/pch/src/tolua/lua/package.lua
r2710 r3127 42 42 return "\n#["..getn(L).."]#" 43 43 end 44 ) 45 -- avoid preprocessing embedded C code 44 ) -- avoid preprocessing embedded C code 46 45 local C = {} 47 46 self.code = gsub(self.code,"\n%s*%$%<","\3") -- deal with embedded C code … … 51 50 return "\n#<"..getn(C)..">#" 52 51 end 53 ) 54 -- avoid preprocessing embedded C code 52 ) -- avoid preprocessing embedded C code 55 53 self.code = gsub(self.code,"\n%s*%$%{","\5") -- deal with embedded C code 56 54 self.code = gsub(self.code,"\n%s*%$%}","\6") … … 60 58 end 61 59 ) 62 63 60 --self.code = gsub(self.code,"\n%s*#[^d][^\n]*\n", "\n\n") -- eliminate preprocessor directives that don't start with 'd' 64 61 self.code = gsub(self.code,"\n[ \t]*#[ \t]*[^d%<%[]", "\n//") -- eliminate preprocessor directives that don't start with 'd' … … 71 68 end 72 69 ) 73 74 70 -- perform global substitution 75 71 … … 94 90 return L[tonumber(n)] 95 91 end 96 ) 97 -- restore embedded C code 92 ) -- restore embedded C code 98 93 self.code = gsub(self.code,"%#%<(%d+)%>%#", function (n) 99 94 return C[tonumber(n)] 100 95 end 101 ) 102 -- restore verbatim lines 96 ) -- restore verbatim lines 103 97 self.code = gsub(self.code,"%#(%d+)%#", function (n) 104 98 return V[tonumber(n)] 105 99 end 106 100 ) 107 108 101 self.code = string.gsub(self.code, "\n%s*%$([^\n]+)", function (l) 109 102 Verbatim(l.."\n") 110 103 return "\n" 111 104 end 112 ) 113 end 114 105 )end 115 106 -- translate verbatim 116 107 function classPackage:preamble () … … 146 137 output('{') 147 138 output(' '..i..'* self = ('..i..'*) tolua_tousertype(tolua_S,1,0);') 148 output(' delete self;')139 output(' Mtolua_delete(self);') 149 140 output(' return 0;') 150 141 output('}') … … 157 148 output('static void tolua_reg_types (lua_State* tolua_S)') 158 149 output('{') 159 foreach(_usertype,function(n,v) output(' tolua_usertype(tolua_S,"',v,'");') end) 150 160 151 if flags.t then 161 152 output("#ifndef Mtolua_typeid\n#define Mtolua_typeid(L,TI,T)\n#endif\n") 162 foreach(_usertype,function(n,v) output(' Mtolua_typeid(tolua_S,typeid(',v,'), "',v,'");') end) 163 end 153 end 154 foreach(_usertype,function(n,v) 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) 164 162 output('}') 165 163 output('\n') … … 191 189 output(pre.."int luaopen_"..self.name.." (lua_State* tolua_S) {") 192 190 output(pre.." return tolua_"..self.name.."_open(tolua_S);") 193 output(pre.."} ;")191 output(pre.."}") 194 192 output("#endif\n\n") 195 193 … … 204 202 205 203 if flags.H then 206 local package_lower = string.lower(self.name) 207 output('#include "'..package_lower..'/'..self.name..'Prereqs.h"\n') 208 output('/* Exported function */') 204 local package_lower = string.lower(self.name) output('#include "'..package_lower..'/'..self.name..'Prereqs.h"\n') output('/* Exported function */') 209 205 output('_'..self.name..'Export') 210 output('int tolua_'..self.name..'_open (lua_State* tolua_S);') 211 output('\n') 206 output('int tolua_'..self.name..'_open (lua_State* tolua_S);') output('\n') 212 207 end 213 208 end … … 224 219 local code = '\n$#include "'..string.lower(flags.n)..'/'..fn..'"\n' 225 220 s= "\n" .. s .. "\n" -- add blank lines as sentinels 226 227 -- eliminate export macro problems in class declarations 228 s = gsub(s, ' _%w*Export ', ' ') 229 230 local _,e,c,t = strfind(s, "\n([^\n]-)[Tt][Oo][Ll][Uu][Aa]_([^%s]*)[^\n]*\n") 221 -- eliminate export macro problems in class declarations s = gsub(s, ' _%w*Export ', ' ') local _,e,c,t = strfind(s, "\n([^\n]-)[Tt][Oo][Ll][Uu][Aa]_([^%s]*)[^\n]*\n") 231 222 while e do 232 223 t = strlower(t) … … 249 240 250 241 -- open input file, if any 242 local st,msg 251 243 if fn then 252 244 local file 253 if flags.f then 254 if string.sub(flags.f, 1, 1) == '/' or string.sub(flags.f, 1, 1) == '\\' or (string.len(flags.f) > 1 and string.sub(flags.f, 2, 2) == ':') then 255 file = flags.f 256 else 257 file = flags.w..'/'..flags.f 258 end 259 else 260 file = flags.f 261 end 262 local st, msg = readfrom(file) 263 if not st then 245 if flags.f then if string.sub(flags.f, 1, 1) == '/' or string.sub(flags.f, 1, 1) == '\\' or (string.len(flags.f) > 1 and string.sub(flags.f, 2, 2) == ':') then file = flags.f else file = flags.w..'/'..flags.f end else file = flags.f end st, msg = readfrom(flags.f) if not st then 264 246 error('#'..msg..' path: '..flags.f) 265 247 end 266 248 local _; _, _, ext = strfind(fn,".*%.(.*)$") 267 249 end 268 local code = "\n" .. read('*a') 269 if ext == 'h' or ext == 'hpp' then 270 code = extract_code(fn,code) 250 local code 251 if ext == 'pkg' then 252 code = prep(st) 253 else 254 code = "\n" .. read('*a') 255 if ext == 'h' or ext == 'hpp' then 256 code = extract_code(fn,code) 257 end 271 258 end 272 259 … … 276 263 end 277 264 278 -- prepare working directory 279 local current_path 280 if not flags.w and flags.f then 281 current_path = gsub(flags.f, '(/)[^/]*%.?[^/]*$', '%1') 282 elseif flags.w then 283 if not (string.sub(flags.w, string.len(flags.w)) == '/') then 284 current_path = flags.w..'/' 285 else 286 current_path = flags.w 287 end 288 else 289 current_path = '' 290 end 291 292 -- deal with include directive 265 -- prepare working directory local current_path if not flags.w and flags.f then current_path = gsub(flags.f, '(/)[^/]*%.?[^/]*$', '%1') elseif flags.w then if not (string.sub(flags.w, string.len(flags.w)) == '/') then current_path = flags.w..'/' else current_path = flags.w end else current_path = '' end -- deal with include directive 293 266 local nsubst 294 267 repeat … … 300 273 error('#'..msg..': '..fn) 301 274 end 275 if kind == 'p' then 276 local s = prep(fp) 277 closefile(fp) 278 return s 279 end 302 280 local s = read(fp,'*a') 303 281 closefile(fp) 304 282 if kind == 'c' or kind == 'h' then 305 283 return extract_code(fn,s) 306 elseif kind == 'p' then307 return "\n\n" .. s308 284 elseif kind == 'l' then 309 285 return "\n$[--##"..fn.."\n" .. s .. "\n$]\n" … … 318 294 end 319 295 end 320 ) 321 until nsubst==0 296 ) until nsubst==0 322 297 323 298 -- deal with renaming directive … … 337 312 338 313 314 setmetatable(_extra_parameters, { __index = _G }) 315 316 function prep(file) 317 318 local chunk = {'local __ret = {"\\n"}\n'} 319 for line in file:lines() do 320 if string.find(line, "^##") then 321 table.insert(chunk, string.sub(line, 3) .. "\n") 322 else 323 local last = 1 324 for text, expr, index in string.gfind(line, "(.-)$(%b())()") do 325 last = index 326 if text ~= "" then 327 table.insert(chunk, string.format('table.insert(__ret, %q )', text)) 328 end 329 table.insert(chunk, string.format('table.insert(__ret, %s )', expr)) 330 end 331 table.insert(chunk, string.format('table.insert(__ret, %q)\n', 332 string.sub(line, last).."\n")) 333 end 334 end 335 table.insert(chunk, '\nreturn table.concat(__ret)\n') 336 local f,e = loadstring(table.concat(chunk)) 337 if e then 338 error("#"..e) 339 end 340 setfenv(f, _extra_parameters) 341 return f() 342 end
Note: See TracChangeset
for help on using the changeset viewer.