[1650] | 1 | -- tolua: container abstract 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 | -- table to store namespaced typedefs/enums in global scope |
---|
| 13 | global_typedefs = {} |
---|
| 14 | global_enums = {} |
---|
| 15 | |
---|
| 16 | -- Container class |
---|
| 17 | -- Represents a container of features to be bound |
---|
| 18 | -- to lua. |
---|
| 19 | classContainer = |
---|
| 20 | { |
---|
[2509] | 21 | curr = nil, |
---|
[1650] | 22 | } |
---|
| 23 | classContainer.__index = classContainer |
---|
| 24 | setmetatable(classContainer,classFeature) |
---|
| 25 | |
---|
| 26 | -- output tags |
---|
| 27 | function classContainer:decltype () |
---|
[2509] | 28 | push(self) |
---|
| 29 | local i=1 |
---|
| 30 | while self[i] do |
---|
| 31 | self[i]:decltype() |
---|
| 32 | i = i+1 |
---|
| 33 | end |
---|
| 34 | pop() |
---|
[1650] | 35 | end |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | -- write support code |
---|
| 39 | function classContainer:supcode () |
---|
| 40 | |
---|
[2509] | 41 | if not self:check_public_access() then |
---|
| 42 | return |
---|
| 43 | end |
---|
[1650] | 44 | |
---|
[2509] | 45 | push(self) |
---|
| 46 | local i=1 |
---|
| 47 | while self[i] do |
---|
| 48 | if self[i]:check_public_access() then |
---|
| 49 | self[i]:supcode() |
---|
| 50 | end |
---|
| 51 | i = i+1 |
---|
| 52 | end |
---|
| 53 | pop() |
---|
[1650] | 54 | end |
---|
| 55 | |
---|
| 56 | function classContainer:hasvar () |
---|
[2509] | 57 | local i=1 |
---|
| 58 | while self[i] do |
---|
| 59 | if self[i]:isvariable() then |
---|
| 60 | return 1 |
---|
| 61 | end |
---|
| 62 | i = i+1 |
---|
| 63 | end |
---|
| 64 | return 0 |
---|
[1650] | 65 | end |
---|
| 66 | |
---|
| 67 | -- Internal container constructor |
---|
| 68 | function _Container (self) |
---|
[2509] | 69 | setmetatable(self,classContainer) |
---|
| 70 | self.n = 0 |
---|
| 71 | self.typedefs = {tolua_n=0} |
---|
| 72 | self.usertypes = {} |
---|
| 73 | self.enums = {tolua_n=0} |
---|
| 74 | self.lnames = {} |
---|
| 75 | return self |
---|
[1650] | 76 | end |
---|
| 77 | |
---|
| 78 | -- push container |
---|
| 79 | function push (t) |
---|
[2509] | 80 | t.prox = classContainer.curr |
---|
| 81 | classContainer.curr = t |
---|
[1650] | 82 | end |
---|
| 83 | |
---|
| 84 | -- pop container |
---|
| 85 | function pop () |
---|
[2509] | 86 | --print("name",classContainer.curr.name) |
---|
| 87 | --foreach(classContainer.curr.usertypes,print) |
---|
| 88 | --print("______________") |
---|
| 89 | classContainer.curr = classContainer.curr.prox |
---|
[1650] | 90 | end |
---|
| 91 | |
---|
| 92 | -- get current namespace |
---|
| 93 | function getcurrnamespace () |
---|
[2509] | 94 | return getnamespace(classContainer.curr) |
---|
[1650] | 95 | end |
---|
| 96 | |
---|
| 97 | -- append to current container |
---|
| 98 | function append (t) |
---|
[2509] | 99 | return classContainer.curr:append(t) |
---|
[1650] | 100 | end |
---|
| 101 | |
---|
| 102 | -- append typedef to current container |
---|
| 103 | function appendtypedef (t) |
---|
[2509] | 104 | return classContainer.curr:appendtypedef(t) |
---|
[1650] | 105 | end |
---|
| 106 | |
---|
| 107 | -- append usertype to current container |
---|
| 108 | function appendusertype (t) |
---|
[2509] | 109 | return classContainer.curr:appendusertype(t) |
---|
[1650] | 110 | end |
---|
| 111 | |
---|
| 112 | -- append enum to current container |
---|
| 113 | function appendenum (t) |
---|
[2509] | 114 | return classContainer.curr:appendenum(t) |
---|
[1650] | 115 | end |
---|
| 116 | |
---|
| 117 | -- substitute typedef |
---|
| 118 | function applytypedef (mod,type) |
---|
[2509] | 119 | return classContainer.curr:applytypedef(mod,type) |
---|
[1650] | 120 | end |
---|
| 121 | |
---|
| 122 | -- check if is type |
---|
| 123 | function findtype (type) |
---|
[2509] | 124 | local t = classContainer.curr:findtype(type) |
---|
| 125 | return t |
---|
[1650] | 126 | end |
---|
| 127 | |
---|
| 128 | -- check if is typedef |
---|
| 129 | function istypedef (type) |
---|
[2509] | 130 | return classContainer.curr:istypedef(type) |
---|
[1650] | 131 | end |
---|
| 132 | |
---|
| 133 | -- get fulltype (with namespace) |
---|
| 134 | function fulltype (t) |
---|
[2509] | 135 | local curr = classContainer.curr |
---|
| 136 | while curr do |
---|
| 137 | if curr then |
---|
| 138 | if curr.typedefs and curr.typedefs[t] then |
---|
| 139 | return curr.typedefs[t] |
---|
| 140 | elseif curr.usertypes and curr.usertypes[t] then |
---|
| 141 | return curr.usertypes[t] |
---|
| 142 | end |
---|
| 143 | end |
---|
| 144 | curr = curr.prox |
---|
| 145 | end |
---|
| 146 | return t |
---|
[1650] | 147 | end |
---|
| 148 | |
---|
| 149 | -- checks if it requires collection |
---|
| 150 | function classContainer:requirecollection (t) |
---|
[2509] | 151 | push(self) |
---|
| 152 | local i=1 |
---|
| 153 | local r = false |
---|
| 154 | while self[i] do |
---|
| 155 | r = self[i]:requirecollection(t) or r |
---|
| 156 | i = i+1 |
---|
| 157 | end |
---|
| 158 | pop() |
---|
| 159 | return r |
---|
[1650] | 160 | end |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | -- get namesapce |
---|
| 164 | function getnamespace (curr) |
---|
[2509] | 165 | local namespace = '' |
---|
| 166 | while curr do |
---|
| 167 | if curr and |
---|
| 168 | ( curr.classtype == 'class' or curr.classtype == 'namespace') |
---|
| 169 | then |
---|
| 170 | namespace = (curr.original_name or curr.name) .. '::' .. namespace |
---|
| 171 | --namespace = curr.name .. '::' .. namespace |
---|
| 172 | end |
---|
| 173 | curr = curr.prox |
---|
| 174 | end |
---|
| 175 | return namespace |
---|
[1650] | 176 | end |
---|
| 177 | |
---|
| 178 | -- get namespace (only namespace) |
---|
| 179 | function getonlynamespace () |
---|
[2509] | 180 | local curr = classContainer.curr |
---|
| 181 | local namespace = '' |
---|
| 182 | while curr do |
---|
| 183 | if curr.classtype == 'class' then |
---|
| 184 | return namespace |
---|
| 185 | elseif curr.classtype == 'namespace' then |
---|
| 186 | namespace = curr.name .. '::' .. namespace |
---|
| 187 | end |
---|
| 188 | curr = curr.prox |
---|
| 189 | end |
---|
| 190 | return namespace |
---|
[1650] | 191 | end |
---|
| 192 | |
---|
| 193 | -- check if is enum |
---|
| 194 | function isenum (type) |
---|
[2509] | 195 | return classContainer.curr:isenum(type) |
---|
[1650] | 196 | end |
---|
| 197 | |
---|
| 198 | -- append feature to container |
---|
| 199 | function classContainer:append (t) |
---|
[2509] | 200 | self.n = self.n + 1 |
---|
| 201 | self[self.n] = t |
---|
| 202 | t.parent = self |
---|
[1650] | 203 | end |
---|
| 204 | |
---|
| 205 | -- append typedef |
---|
| 206 | function classContainer:appendtypedef (t) |
---|
[2509] | 207 | local namespace = getnamespace(classContainer.curr) |
---|
| 208 | self.typedefs.tolua_n = self.typedefs.tolua_n + 1 |
---|
| 209 | self.typedefs[self.typedefs.tolua_n] = t |
---|
| 210 | self.typedefs[t.utype] = namespace .. t.utype |
---|
| 211 | global_typedefs[namespace..t.utype] = t |
---|
| 212 | t.ftype = findtype(t.type) or t.type |
---|
| 213 | --print("appending typedef "..t.utype.." as "..namespace..t.utype.." with ftype "..t.ftype) |
---|
| 214 | append_global_type(namespace..t.utype) |
---|
| 215 | if t.ftype and isenum(t.ftype) then |
---|
| 216 | global_enums[namespace..t.utype] = true |
---|
| 217 | end |
---|
[1650] | 218 | end |
---|
| 219 | |
---|
| 220 | -- append usertype: return full type |
---|
| 221 | function classContainer:appendusertype (t) |
---|
[2509] | 222 | local container |
---|
| 223 | if t == (self.original_name or self.name) then |
---|
| 224 | container = self.prox |
---|
| 225 | else |
---|
| 226 | container = self |
---|
| 227 | end |
---|
| 228 | local ft = getnamespace(container) .. t |
---|
| 229 | container.usertypes[t] = ft |
---|
| 230 | _usertype[ft] = ft |
---|
| 231 | return ft |
---|
[1650] | 232 | end |
---|
| 233 | |
---|
| 234 | -- append enum |
---|
| 235 | function classContainer:appendenum (t) |
---|
[2509] | 236 | local namespace = getnamespace(classContainer.curr) |
---|
| 237 | self.enums.tolua_n = self.enums.tolua_n + 1 |
---|
| 238 | self.enums[self.enums.tolua_n] = t |
---|
| 239 | global_enums[namespace..t.name] = t |
---|
[1650] | 240 | end |
---|
| 241 | |
---|
| 242 | -- determine lua function name overload |
---|
| 243 | function classContainer:overload (lname) |
---|
[2509] | 244 | if not self.lnames[lname] then |
---|
| 245 | self.lnames[lname] = 0 |
---|
| 246 | else |
---|
| 247 | self.lnames[lname] = self.lnames[lname] + 1 |
---|
| 248 | end |
---|
| 249 | return format("%02d",self.lnames[lname]) |
---|
[1650] | 250 | end |
---|
| 251 | |
---|
| 252 | -- applies typedef: returns the 'the facto' modifier and type |
---|
| 253 | function classContainer:applytypedef (mod,type) |
---|
[2509] | 254 | if global_typedefs[type] then |
---|
| 255 | --print("found typedef "..global_typedefs[type].type) |
---|
| 256 | local mod1, type1 = global_typedefs[type].mod, global_typedefs[type].ftype |
---|
| 257 | local mod2, type2 = applytypedef(mod.." "..mod1, type1) |
---|
| 258 | --return mod2 .. ' ' .. mod1, type2 |
---|
| 259 | return mod2, type2 |
---|
| 260 | end |
---|
| 261 | do return mod,type end |
---|
[1650] | 262 | end |
---|
| 263 | |
---|
| 264 | -- check if it is a typedef |
---|
| 265 | function classContainer:istypedef (type) |
---|
[2509] | 266 | local env = self |
---|
| 267 | while env do |
---|
| 268 | if env.typedefs then |
---|
| 269 | local i=1 |
---|
| 270 | while env.typedefs[i] do |
---|
| 271 | if env.typedefs[i].utype == type then |
---|
| 272 | return type |
---|
| 273 | end |
---|
| 274 | i = i+1 |
---|
| 275 | end |
---|
[1650] | 276 | end |
---|
[2509] | 277 | env = env.parent |
---|
| 278 | end |
---|
| 279 | return nil |
---|
[1650] | 280 | end |
---|
| 281 | |
---|
| 282 | function find_enum_var(var) |
---|
| 283 | |
---|
[2509] | 284 | if tonumber(var) then return var end |
---|
[1650] | 285 | |
---|
[2509] | 286 | local c = classContainer.curr |
---|
| 287 | while c do |
---|
| 288 | local ns = getnamespace(c) |
---|
| 289 | for k,v in pairs(_global_enums) do |
---|
| 290 | if match_type(var, v, ns) then |
---|
| 291 | return v |
---|
| 292 | end |
---|
| 293 | end |
---|
| 294 | if c.base and c.base ~= '' then |
---|
| 295 | c = _global_classes[c:findtype(c.base)] |
---|
| 296 | else |
---|
| 297 | c = nil |
---|
| 298 | end |
---|
| 299 | end |
---|
[1650] | 300 | |
---|
[2509] | 301 | return var |
---|
[1650] | 302 | end |
---|
| 303 | |
---|
| 304 | -- check if is a registered type: return full type or nil |
---|
| 305 | function classContainer:findtype (t) |
---|
| 306 | |
---|
[2509] | 307 | t = string.gsub(t, "=.*", "") |
---|
| 308 | if _basic[t] then |
---|
| 309 | return t |
---|
| 310 | end |
---|
[1650] | 311 | |
---|
[2509] | 312 | local _,_,em = string.find(t, "([&%*])%s*$") |
---|
| 313 | t = string.gsub(t, "%s*([&%*])%s*$", "") |
---|
| 314 | p = self |
---|
| 315 | while p and type(p)=='table' do |
---|
| 316 | local st = getnamespace(p) |
---|
[1650] | 317 | |
---|
[2509] | 318 | for i=_global_types.n,1,-1 do -- in reverse order |
---|
[1650] | 319 | |
---|
[2509] | 320 | if match_type(t, _global_types[i], st) then |
---|
| 321 | return _global_types[i]..(em or "") |
---|
| 322 | end |
---|
| 323 | end |
---|
| 324 | if p.base and p.base ~= '' and p.base ~= t then |
---|
| 325 | --print("type is "..t..", p is "..p.base.." self.type is "..self.type.." self.name is "..self.name) |
---|
| 326 | p = _global_classes[p:findtype(p.base)] |
---|
| 327 | else |
---|
| 328 | p = nil |
---|
| 329 | end |
---|
| 330 | end |
---|
[1650] | 331 | |
---|
[2509] | 332 | return nil |
---|
[1650] | 333 | end |
---|
| 334 | |
---|
| 335 | function append_global_type(t, class) |
---|
[2509] | 336 | _global_types.n = _global_types.n +1 |
---|
| 337 | _global_types[_global_types.n] = t |
---|
| 338 | _global_types_hash[t] = 1 |
---|
| 339 | if class then append_class_type(t, class) end |
---|
[1650] | 340 | end |
---|
| 341 | |
---|
| 342 | function append_class_type(t,class) |
---|
[2509] | 343 | if _global_classes[t] then |
---|
| 344 | class.flags = _global_classes[t].flags |
---|
| 345 | class.lnames = _global_classes[t].lnames |
---|
| 346 | if _global_classes[t].base and (_global_classes[t].base ~= '') then |
---|
| 347 | class.base = _global_classes[t].base or class.base |
---|
| 348 | end |
---|
| 349 | end |
---|
| 350 | _global_classes[t] = class |
---|
| 351 | class.flags = class.flags or {} |
---|
[1650] | 352 | end |
---|
| 353 | |
---|
| 354 | function match_type(childtype, regtype, st) |
---|
| 355 | --print("findtype "..childtype..", "..regtype..", "..st) |
---|
[2509] | 356 | local b,e = string.find(regtype, childtype, -string.len(childtype), true) |
---|
| 357 | if b then |
---|
[1650] | 358 | |
---|
[2509] | 359 | if e == string.len(regtype) and |
---|
| 360 | (b == 1 or (string.sub(regtype, b-1, b-1) == ':' and |
---|
| 361 | string.sub(regtype, 1, b-1) == string.sub(st, 1, b-1))) then |
---|
| 362 | return true |
---|
| 363 | end |
---|
| 364 | end |
---|
[1650] | 365 | |
---|
[2509] | 366 | return false |
---|
[1650] | 367 | end |
---|
| 368 | |
---|
| 369 | function findtype_on_childs(self, t) |
---|
| 370 | |
---|
[2509] | 371 | local tchild |
---|
| 372 | if self.classtype == 'class' or self.classtype == 'namespace' then |
---|
| 373 | for k,v in ipairs(self) do |
---|
| 374 | if v.classtype == 'class' or v.classtype == 'namespace' then |
---|
| 375 | if v.typedefs and v.typedefs[t] then |
---|
| 376 | return v.typedefs[t] |
---|
| 377 | elseif v.usertypes and v.usertypes[t] then |
---|
| 378 | return v.usertypes[t] |
---|
| 379 | end |
---|
| 380 | tchild = findtype_on_childs(v, t) |
---|
| 381 | if tchild then return tchild end |
---|
| 382 | end |
---|
| 383 | end |
---|
| 384 | end |
---|
| 385 | return nil |
---|
[1650] | 386 | |
---|
| 387 | end |
---|
| 388 | |
---|
| 389 | function classContainer:isenum (type) |
---|
[2509] | 390 | if global_enums[type] then |
---|
| 391 | return type |
---|
| 392 | else |
---|
| 393 | return false |
---|
| 394 | end |
---|
[1650] | 395 | |
---|
[2509] | 396 | local basetype = gsub(type,"^.*::","") |
---|
| 397 | local env = self |
---|
| 398 | while env do |
---|
| 399 | if env.enums then |
---|
| 400 | local i=1 |
---|
| 401 | while env.enums[i] do |
---|
| 402 | if env.enums[i].name == basetype then |
---|
| 403 | return true |
---|
| 404 | end |
---|
| 405 | i = i+1 |
---|
| 406 | end |
---|
[1650] | 407 | end |
---|
[2509] | 408 | env = env.parent |
---|
| 409 | end |
---|
| 410 | return false |
---|
[1650] | 411 | end |
---|
| 412 | |
---|
| 413 | methodisvirtual = false -- a global |
---|
| 414 | |
---|
| 415 | -- parse chunk |
---|
| 416 | function classContainer:doparse (s) |
---|
[2509] | 417 | --print ("parse "..s) |
---|
[1650] | 418 | |
---|
[2509] | 419 | -- try the parser hook |
---|
| 420 | do |
---|
| 421 | local sub = parser_hook(s) |
---|
| 422 | if sub then |
---|
| 423 | return sub |
---|
| 424 | end |
---|
| 425 | end |
---|
[1650] | 426 | |
---|
[2509] | 427 | -- try the null statement |
---|
| 428 | do |
---|
| 429 | local b,e,code = string.find(s, "^%s*;") |
---|
| 430 | if b then |
---|
| 431 | return strsub(s,e+1) |
---|
| 432 | end |
---|
| 433 | end |
---|
[1650] | 434 | |
---|
[2509] | 435 | -- try empty verbatim line |
---|
| 436 | do |
---|
| 437 | local b,e,code = string.find(s, "^%s*$\n") |
---|
| 438 | if b then |
---|
| 439 | return strsub(s,e+1) |
---|
| 440 | end |
---|
| 441 | end |
---|
[1650] | 442 | |
---|
[2509] | 443 | -- try Lua code |
---|
| 444 | do |
---|
| 445 | local b,e,code = strfind(s,"^%s*(%b\1\2)") |
---|
| 446 | if b then |
---|
| 447 | Code(strsub(code,2,-2)) |
---|
| 448 | return strsub(s,e+1) |
---|
| 449 | end |
---|
| 450 | end |
---|
[1650] | 451 | |
---|
[2509] | 452 | -- try C code |
---|
| 453 | do |
---|
| 454 | local b,e,code = strfind(s,"^%s*(%b\3\4)") |
---|
| 455 | if b then |
---|
| 456 | code = '{'..strsub(code,2,-2)..'\n}\n' |
---|
| 457 | Verbatim(code,'r') -- verbatim code for 'r'egister fragment |
---|
| 458 | return strsub(s,e+1) |
---|
| 459 | end |
---|
| 460 | end |
---|
[1650] | 461 | |
---|
[2509] | 462 | -- try C code for preamble section |
---|
| 463 | do |
---|
| 464 | local b,e,code = string.find(s, "^%s*(%b\5\6)") |
---|
| 465 | if b then |
---|
| 466 | code = string.sub(code, 2, -2).."\n" |
---|
| 467 | Verbatim(code, '') |
---|
| 468 | return string.sub(s, e+1) |
---|
| 469 | end |
---|
| 470 | end |
---|
[1650] | 471 | |
---|
[2509] | 472 | -- try default_property directive |
---|
| 473 | do |
---|
| 474 | local b,e,ptype = strfind(s, "^%s*TOLUA_PROPERTY_TYPE%s*%(+%s*([^%)%s]*)%s*%)+%s*;?") |
---|
| 475 | if b then |
---|
| 476 | if not ptype or ptype == "" then |
---|
| 477 | ptype = "default" |
---|
| 478 | end |
---|
| 479 | self:set_property_type(ptype) |
---|
| 480 | return strsub(s, e+1) |
---|
| 481 | end |
---|
| 482 | end |
---|
[1650] | 483 | |
---|
[2509] | 484 | -- try protected_destructor directive |
---|
| 485 | do |
---|
| 486 | local b,e = string.find(s, "^%s*TOLUA_PROTECTED_DESTRUCTOR%s*;?") |
---|
| 487 | if b then |
---|
| 488 | if self.set_protected_destructor then |
---|
| 489 | self:set_protected_destructor(true) |
---|
| 490 | end |
---|
| 491 | return strsub(s, e+1) |
---|
| 492 | end |
---|
| 493 | end |
---|
[1650] | 494 | |
---|
[2509] | 495 | -- try 'extern' keyword |
---|
| 496 | do |
---|
| 497 | local b,e = string.find(s, "^%s*extern%s+") |
---|
| 498 | if b then |
---|
| 499 | -- do nothing |
---|
| 500 | return strsub(s, e+1) |
---|
| 501 | end |
---|
| 502 | end |
---|
[1650] | 503 | |
---|
[2509] | 504 | -- try 'virtual' keyworkd |
---|
| 505 | do |
---|
| 506 | local b,e = string.find(s, "^%s*virtual%s+") |
---|
| 507 | if b then |
---|
| 508 | methodisvirtual = true |
---|
| 509 | return strsub(s, e+1) |
---|
| 510 | end |
---|
| 511 | end |
---|
[1650] | 512 | |
---|
[2509] | 513 | -- try labels (public, private, etc) |
---|
| 514 | do |
---|
| 515 | local b,e = string.find(s, "^%s*%w*%s*:[^:]") |
---|
| 516 | if b then |
---|
| 517 | return strsub(s, e) -- preserve the [^:] |
---|
| 518 | end |
---|
| 519 | end |
---|
[1650] | 520 | |
---|
[2509] | 521 | -- try module |
---|
| 522 | do |
---|
| 523 | local b,e,name,body = strfind(s,"^%s*module%s%s*([_%w][_%w]*)%s*(%b{})%s*") |
---|
| 524 | if b then |
---|
| 525 | _curr_code = strsub(s,b,e) |
---|
| 526 | Module(name,body) |
---|
| 527 | return strsub(s,e+1) |
---|
| 528 | end |
---|
| 529 | end |
---|
[1650] | 530 | |
---|
[2509] | 531 | -- try namesapce |
---|
| 532 | do |
---|
| 533 | local b,e,name,body = strfind(s,"^%s*namespace%s%s*([_%w][_%w]*)%s*(%b{})%s*;?") |
---|
| 534 | if b then |
---|
| 535 | _curr_code = strsub(s,b,e) |
---|
| 536 | Namespace(name,body) |
---|
| 537 | return strsub(s,e+1) |
---|
| 538 | end |
---|
| 539 | end |
---|
[1650] | 540 | |
---|
[2509] | 541 | -- try define |
---|
| 542 | do |
---|
| 543 | local b,e,name = strfind(s,"^%s*#define%s%s*([^%s]*)[^\n]*\n%s*") |
---|
| 544 | if b then |
---|
| 545 | _curr_code = strsub(s,b,e) |
---|
| 546 | Define(name) |
---|
| 547 | return strsub(s,e+1) |
---|
| 548 | end |
---|
| 549 | end |
---|
[1650] | 550 | |
---|
[2509] | 551 | -- try enumerates |
---|
[1650] | 552 | |
---|
[2509] | 553 | do |
---|
| 554 | local b,e,name,body,varname = strfind(s,"^%s*enum%s+(%S*)%s*(%b{})%s*([^%s;]*)%s*;?%s*") |
---|
| 555 | if b then |
---|
| 556 | --error("#Sorry, declaration of enums and variables on the same statement is not supported.\nDeclare your variable separately (example: '"..name.." "..varname..";')") |
---|
| 557 | _curr_code = strsub(s,b,e) |
---|
| 558 | Enumerate(name,body,varname) |
---|
| 559 | return strsub(s,e+1) |
---|
| 560 | end |
---|
| 561 | end |
---|
[1650] | 562 | |
---|
[2509] | 563 | -- do |
---|
| 564 | -- local b,e,name,body = strfind(s,"^%s*enum%s+(%S*)%s*(%b{})%s*;?%s*") |
---|
| 565 | -- if b then |
---|
| 566 | -- _curr_code = strsub(s,b,e) |
---|
| 567 | -- Enumerate(name,body) |
---|
| 568 | -- return strsub(s,e+1) |
---|
| 569 | -- end |
---|
| 570 | -- end |
---|
[1650] | 571 | |
---|
[2509] | 572 | do |
---|
| 573 | local b,e,body,name = strfind(s,"^%s*typedef%s+enum[^{]*(%b{})%s*([%w_][^%s]*)%s*;%s*") |
---|
| 574 | if b then |
---|
| 575 | _curr_code = strsub(s,b,e) |
---|
| 576 | Enumerate(name,body) |
---|
| 577 | return strsub(s,e+1) |
---|
| 578 | end |
---|
| 579 | end |
---|
[1650] | 580 | |
---|
[2509] | 581 | -- try operator |
---|
| 582 | do |
---|
| 583 | local b,e,decl,kind,arg,const = strfind(s,"^%s*([_%w][_%w%s%*&:<>,]-%s+operator)%s*([^%s][^%s]*)%s*(%b())%s*(c?o?n?s?t?)%s*;%s*") |
---|
| 584 | if not b then |
---|
| 585 | -- try inline |
---|
| 586 | b,e,decl,kind,arg,const = strfind(s,"^%s*([_%w][_%w%s%*&:<>,]-%s+operator)%s*([^%s][^%s]*)%s*(%b())%s*(c?o?n?s?t?)[%s\n]*%b{}%s*;?%s*") |
---|
| 587 | end |
---|
| 588 | if not b then |
---|
| 589 | -- try cast operator |
---|
| 590 | b,e,decl,kind,arg,const = strfind(s, "^%s*(operator)%s+([%w_:%d<>%*%&%s]+)%s*(%b())%s*(c?o?n?s?t?)"); |
---|
| 591 | if b then |
---|
| 592 | local _,ie = string.find(s, "^%s*%b{}", e+1) |
---|
| 593 | if ie then |
---|
| 594 | e = ie |
---|
| 595 | end |
---|
| 596 | end |
---|
| 597 | end |
---|
| 598 | if b then |
---|
| 599 | _curr_code = strsub(s,b,e) |
---|
| 600 | Operator(decl,kind,arg,const) |
---|
| 601 | return strsub(s,e+1) |
---|
| 602 | end |
---|
| 603 | end |
---|
[1650] | 604 | |
---|
[2509] | 605 | -- try function |
---|
| 606 | do |
---|
| 607 | --local b,e,decl,arg,const = strfind(s,"^%s*([~_%w][_@%w%s%*&:<>]*[_%w])%s*(%b())%s*(c?o?n?s?t?)%s*=?%s*0?%s*;%s*") |
---|
| 608 | local b,e,decl,arg,const,virt = strfind(s,"^%s*([^%(\n]+)%s*(%b())%s*(c?o?n?s?t?)%s*(=?%s*0?)%s*;%s*") |
---|
| 609 | if not b then |
---|
| 610 | -- try function with template |
---|
| 611 | b,e,decl,arg,const = strfind(s,"^%s*([~_%w][_@%w%s%*&:<>]*[_%w]%b<>)%s*(%b())%s*(c?o?n?s?t?)%s*=?%s*0?%s*;%s*") |
---|
| 612 | end |
---|
| 613 | if not b then |
---|
| 614 | -- try a single letter function name |
---|
| 615 | b,e,decl,arg,const = strfind(s,"^%s*([_%w])%s*(%b())%s*(c?o?n?s?t?)%s*;%s*") |
---|
| 616 | end |
---|
| 617 | if b then |
---|
| 618 | if virt and string.find(virt, "[=0]") then |
---|
| 619 | if self.flags then |
---|
| 620 | self.flags.pure_virtual = true |
---|
| 621 | end |
---|
| 622 | end |
---|
| 623 | _curr_code = strsub(s,b,e) |
---|
| 624 | Function(decl,arg,const) |
---|
| 625 | return strsub(s,e+1) |
---|
| 626 | end |
---|
| 627 | end |
---|
[1650] | 628 | |
---|
[2509] | 629 | -- try inline function |
---|
| 630 | do |
---|
| 631 | local b,e,decl,arg,const = strfind(s,"^%s*([^%(\n]+)%s*(%b())%s*(c?o?n?s?t?)[^;{]*%b{}%s*;?%s*") |
---|
| 632 | --local b,e,decl,arg,const = strfind(s,"^%s*([~_%w][_@%w%s%*&:<>]*[_%w>])%s*(%b())%s*(c?o?n?s?t?)[^;]*%b{}%s*;?%s*") |
---|
| 633 | if not b then |
---|
| 634 | -- try a single letter function name |
---|
| 635 | b,e,decl,arg,const = strfind(s,"^%s*([_%w])%s*(%b())%s*(c?o?n?s?t?).-%b{}%s*;?%s*") |
---|
| 636 | end |
---|
| 637 | if b then |
---|
| 638 | _curr_code = strsub(s,b,e) |
---|
| 639 | Function(decl,arg,const) |
---|
| 640 | return strsub(s,e+1) |
---|
| 641 | end |
---|
| 642 | end |
---|
[1650] | 643 | |
---|
[2509] | 644 | -- try class |
---|
| 645 | do |
---|
| 646 | local b,e,name,base,body |
---|
| 647 | base = '' body = '' |
---|
| 648 | b,e,name = strfind(s,"^%s*class%s*([_%w][_%w@]*)%s*;") -- dummy class |
---|
| 649 | if not b then |
---|
| 650 | b,e,name = strfind(s,"^%s*struct%s*([_%w][_%w@]*)%s*;") -- dummy struct |
---|
| 651 | if not b then |
---|
| 652 | b,e,name,base,body = strfind(s,"^%s*class%s*([_%w][_%w@]*)%s*(.-)%s*(%b{})%s*;%s*") |
---|
| 653 | if not b then |
---|
| 654 | b,e,name,base,body = strfind(s,"^%s*struct%s*([_%w][_%w@]*)%s*(.-)%s*(%b{})%s*;%s*") |
---|
| 655 | if not b then |
---|
| 656 | b,e,name,base,body = strfind(s,"^%s*union%s*([_%w][_%w@]*)%s*(.-)%s*(%b{})%s*;%s*") |
---|
| 657 | if not b then |
---|
| 658 | base = '' |
---|
| 659 | b,e,body,name = strfind(s,"^%s*typedef%s%s*struct%s%s*[_%w]*%s*(%b{})%s*([_%w][_%w@]*)%s*;%s*") |
---|
| 660 | end |
---|
| 661 | end |
---|
| 662 | end |
---|
| 663 | end |
---|
| 664 | end |
---|
| 665 | if b then |
---|
| 666 | if base ~= '' then |
---|
| 667 | base = string.gsub(base, "^%s*:%s*", "") |
---|
| 668 | base = string.gsub(base, "%s*public%s*", "") |
---|
| 669 | base = split(base, ",") |
---|
| 670 | --local b,e |
---|
| 671 | --b,e,base = strfind(base,".-([_%w][_%w<>,:]*)$") |
---|
| 672 | else |
---|
| 673 | base = {} |
---|
| 674 | end |
---|
| 675 | _curr_code = strsub(s,b,e) |
---|
| 676 | Class(name,base,body) |
---|
| 677 | return strsub(s,e+1) |
---|
| 678 | end |
---|
| 679 | end |
---|
[1650] | 680 | |
---|
[2509] | 681 | -- try typedef |
---|
| 682 | do |
---|
| 683 | local b,e,types = strfind(s,"^%s*typedef%s%s*(.-)%s*;%s*") |
---|
| 684 | if b then |
---|
| 685 | _curr_code = strsub(s,b,e) |
---|
| 686 | Typedef(types) |
---|
| 687 | return strsub(s,e+1) |
---|
| 688 | end |
---|
| 689 | end |
---|
[1650] | 690 | |
---|
[2509] | 691 | -- try variable |
---|
| 692 | do |
---|
| 693 | local b,e,decl = strfind(s,"^%s*([_%w][_@%s%w%d%*&:<>,]*[_%w%d])%s*;%s*") |
---|
| 694 | if b then |
---|
| 695 | _curr_code = strsub(s,b,e) |
---|
[1650] | 696 | |
---|
[2509] | 697 | local list = split_c_tokens(decl, ",") |
---|
| 698 | Variable(list[1]) |
---|
| 699 | if list.n > 1 then |
---|
| 700 | local _,_,type = strfind(list[1], "(.-)%s+([^%s]*)$"); |
---|
[1650] | 701 | |
---|
[2509] | 702 | local i =2; |
---|
| 703 | while list[i] do |
---|
| 704 | Variable(type.." "..list[i]) |
---|
| 705 | i=i+1 |
---|
| 706 | end |
---|
| 707 | end |
---|
| 708 | --Variable(decl) |
---|
| 709 | return strsub(s,e+1) |
---|
| 710 | end |
---|
| 711 | end |
---|
[1650] | 712 | |
---|
[2509] | 713 | -- try string |
---|
| 714 | do |
---|
| 715 | local b,e,decl = strfind(s,"^%s*([_%w]?[_%s%w%d]-char%s+[_@%w%d]*%s*%[%s*%S+%s*%])%s*;%s*") |
---|
| 716 | if b then |
---|
| 717 | _curr_code = strsub(s,b,e) |
---|
| 718 | Variable(decl) |
---|
| 719 | return strsub(s,e+1) |
---|
| 720 | end |
---|
| 721 | end |
---|
[1650] | 722 | |
---|
[2509] | 723 | -- try array |
---|
| 724 | do |
---|
| 725 | local b,e,decl = strfind(s,"^%s*([_%w][][_@%s%w%d%*&:]*[]_%w%d])%s*;%s*") |
---|
| 726 | if b then |
---|
| 727 | _curr_code = strsub(s,b,e) |
---|
| 728 | Array(decl) |
---|
| 729 | return strsub(s,e+1) |
---|
| 730 | end |
---|
| 731 | end |
---|
[1650] | 732 | |
---|
[2509] | 733 | -- no matching |
---|
| 734 | if gsub(s,"%s%s*","") ~= "" then |
---|
| 735 | _curr_code = s |
---|
| 736 | error("#parse error") |
---|
| 737 | else |
---|
| 738 | return "" |
---|
| 739 | end |
---|
| 740 | |
---|
[1650] | 741 | end |
---|
| 742 | |
---|
| 743 | function classContainer:parse (s) |
---|
| 744 | |
---|
[2509] | 745 | self.curr_member_access = nil |
---|
[1650] | 746 | |
---|
[2509] | 747 | while s ~= '' do |
---|
| 748 | s = self:doparse(s) |
---|
| 749 | methodisvirtual = false |
---|
| 750 | end |
---|
[1650] | 751 | end |
---|
| 752 | |
---|
| 753 | |
---|
| 754 | -- property types |
---|
| 755 | |
---|
| 756 | function get_property_type() |
---|
| 757 | |
---|
[2509] | 758 | return classContainer.curr:get_property_type() |
---|
[1650] | 759 | end |
---|
| 760 | |
---|
| 761 | function classContainer:set_property_type(ptype) |
---|
[2509] | 762 | ptype = string.gsub(ptype, "^%s*", "") |
---|
| 763 | ptype = string.gsub(ptype, "%s*$", "") |
---|
[1650] | 764 | |
---|
[2509] | 765 | self.property_type = ptype |
---|
[1650] | 766 | end |
---|
| 767 | |
---|
| 768 | function classContainer:get_property_type() |
---|
[2509] | 769 | return self.property_type or (self.parent and self.parent:get_property_type()) or "default" |
---|
[1650] | 770 | end |
---|