Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/tolua/lua/basic.lua @ 3129

Last change on this file since 3129 was 3129, checked in by rgrieder, 15 years ago

Fixed tolua update:

  • Too many changes in package.lua (somehow glitched..)
  • Lua has a problem with the variadic function used.
  • Property svn:eol-style set to native
File size: 9.8 KB
Line 
1-- tolua: basic utility functions
2-- Written by Waldemar Celes
3-- TeCGraf/PUC-Rio
4-- Jul 1998
5-- Last update: Apr 2003
6-- $Id: $
7
8-- This code is free software; you can redistribute it and/or modify it.
9-- The software provided hereunder is on an "as is" basis, and
10-- the author has no obligation to provide maintenance, support, updates,
11-- enhancements, or modifications.
12
13
14-- Basic C types and their corresponding Lua types
15-- All occurrences of "char*" will be replaced by "_cstring",
16-- and all occurrences of "void*" will be replaced by "_userdata"
17_basic = {
18    ['void'] = '',
19    ['char'] = 'number',
20    ['int'] = 'number',
21    ['short'] = 'number',
22    ['long'] = 'number',
23    ['unsigned'] = 'number',
24    ['float'] = 'number',
25    ['double'] = 'number',
26    ['_cstring'] = 'string',
27    ['_userdata'] = 'userdata',
28    ['char*'] = 'string',
29    ['void*'] = 'userdata',
30    ['bool'] = 'boolean',
31    ['lua_Object'] = 'value',
32    ['LUA_VALUE'] = 'value',    -- for compatibility with tolua 4.0
33    ['lua_State*'] = 'state',
34    ['_lstate'] = 'state',
35    ['lua_Function'] = 'value',
36}
37
38_basic_ctype = {
39    number = "lua_Number",
40    string = "const char*",
41    userdata = "void*",
42    boolean = "bool",
43    value = "int",
44    state = "lua_State*",
45}
46
47-- functions the are used to do a 'raw push' of basic types
48_basic_raw_push = {}
49
50-- List of user defined types
51-- Each type corresponds to a variable name that stores its tag value.
52_usertype = {}
53
54-- List of types that have to be collected
55_collect = {}
56
57-- List of types
58_global_types = {n=0}
59_global_types_hash = {}
60
61-- list of classes
62_global_classes = {}
63
64-- List of enum constants
65_global_enums = {}
66
67-- List of auto renaming
68_renaming = {}
69function appendrenaming (s)
70    local b,e,old,new = strfind(s,"%s*(.-)%s*@%s*(.-)%s*$")
71    if not b then
72        error("#Invalid renaming syntax; it should be of the form: pattern@pattern")
73    end
74    tinsert(_renaming,{old=old, new=new})
75end
76
77function applyrenaming (s)
78    for i=1,getn(_renaming) do
79        local m,n = gsub(s,_renaming[i].old,_renaming[i].new)
80        if n ~= 0 then
81            return m
82        end
83    end
84    return nil
85end
86
87-- Error handler
88function tolua_error (s,f)
89    if _curr_code then
90        print("***curr code for error is "..tostring(_curr_code))
91        print(debug.traceback())
92    end
93    local out = _OUTPUT
94    _OUTPUT = _STDERR
95    if strsub(s,1,1) == '#' then
96        write("\n** tolua: "..strsub(s,2)..".\n\n")
97        if _curr_code then
98            local _,_,s = strfind(_curr_code,"^%s*(.-\n)") -- extract first line
99            if s==nil then s = _curr_code end
100            s = gsub(s,"_userdata","void*") -- return with 'void*'
101            s = gsub(s,"_cstring","char*")  -- return with 'char*'
102            s = gsub(s,"_lstate","lua_State*")  -- return with 'lua_State*'
103            write("Code being processed:\n"..s.."\n")
104        end
105    else
106        if not f then f = "(f is nil)" end
107        print("\n** tolua internal error: "..f..s..".\n\n")
108        return
109    end
110    _OUTPUT = out
111end
112
113function warning (msg)
114    if flags.q then
115        return
116    end
117    local out = _OUTPUT
118    _OUTPUT = _STDERR
119    write("\n** tolua warning: "..msg..".\n\n")
120    _OUTPUT = out
121end
122
123-- register an user defined type: returns full type
124function regtype (t)
125    --if isbasic(t) then
126    --    return t
127    --end
128    local ft = findtype(t)
129
130    if not _usertype[ft] then
131        return appendusertype(t)
132    end
133    return ft
134end
135
136-- return type name: returns full type
137function typevar(type)
138    if type == '' or type == 'void' then
139        return type
140    else
141        local ft = findtype(type)
142        if ft then
143            return ft
144        end
145        _usertype[type] = type
146        return type
147    end
148end
149
150-- check if basic type
151function isbasic (type)
152    local t = gsub(type,'const ','')
153    local m,t = applytypedef('', t)
154    local b = _basic[t]
155    if b then
156        return b,_basic_ctype[b]
157    end
158    return nil
159end
160
161-- split string using a token
162function split (s,t)
163    local l = {n=0}
164    local f = function (s)
165        l.n = l.n + 1
166        l[l.n] = s
167        return ""
168    end
169    local p = "%s*(.-)%s*"..t.."%s*"
170    s = gsub(s,"^%s+","")
171    s = gsub(s,"%s+$","")
172    s = gsub(s,p,f)
173    l.n = l.n + 1
174    l[l.n] = gsub(s,"(%s%s*)$","")
175    return l
176end
177
178-- splits a string using a pattern, considering the spacial cases of C code (templates, function parameters, etc)
179-- pattern can't contain the '^' (as used to identify the begining of the line)
180-- also strips whitespace
181function split_c_tokens(s, pat)
182
183    s = string.gsub(s, "^%s*", "")
184    s = string.gsub(s, "%s*$", "")
185
186    local token_begin = 1
187    local token_end = 1
188    local ofs = 1
189    local ret = {n=0}
190
191    function add_token(ofs)
192
193        local t = string.sub(s, token_begin, ofs)
194        t = string.gsub(t, "^%s*", "")
195        t = string.gsub(t, "%s*$", "")
196        ret.n = ret.n + 1
197        ret[ret.n] = t
198    end
199
200    while ofs <= string.len(s) do
201
202        local sub = string.sub(s, ofs, -1)
203        local b,e = string.find(sub, "^"..pat)
204        if b then
205            add_token(ofs-1)
206            ofs = ofs+e
207            token_begin = ofs
208        else
209            local char = string.sub(s, ofs, ofs)
210            if char == "(" or char == "<" then
211
212                local block
213                if char == "(" then block = "^%b()" end
214                if char == "<" then block = "^%b<>" end
215
216                b,e = string.find(sub, block)
217                if not b then
218                    -- unterminated block?
219                    ofs = ofs+1
220                else
221                    ofs = ofs + e
222                end
223
224            else
225                ofs = ofs+1
226            end
227        end
228
229    end
230    add_token(ofs)
231    --if ret.n == 0 then
232
233    --    ret.n=1
234    --    ret[1] = ""
235    --end
236
237    return ret
238
239end
240
241-- concatenate strings of a table
242function concat (t,f,l,jstr)
243    jstr = jstr or " "
244    local s = ''
245    local i=f
246    while i<=l do
247        s = s..t[i]
248        i = i+1
249        if i <= l then s = s..jstr end
250    end
251    return s
252end
253
254-- concatenate all parameters, following output rules
255function concatparam (line, ...)
256    local i=1
257    while i<=arg.n do
258        if _cont and not strfind(_cont,'[%(,"]') and
259            strfind(arg[i],"^[%a_~]") then
260            line = line .. ' '
261        end
262        line = line .. arg[i]
263        if arg[i] ~= '' then
264            _cont = strsub(arg[i],-1,-1)
265        end
266        i = i+1
267    end
268    if strfind(arg[arg.n],"[%/%)%;%{%}]$") then
269        _cont=nil line = line .. '\n'
270    end
271    return line
272end
273
274-- output line
275function output (...)
276    local i=1
277    while i<=arg.n do
278        if _cont and not strfind(_cont,'[%(,"]') and
279            strfind(arg[i],"^[%a_~]") then
280            write(' ')
281        end
282        write(arg[i])
283        if arg[i] ~= '' then
284            _cont = strsub(arg[i],-1,-1)
285        end
286        i = i+1
287    end
288    if strfind(arg[arg.n],"[%/%)%;%{%}]$") then
289        _cont=nil write('\n')
290    end
291end
292
293function get_property_methods(ptype, name)
294
295    if get_property_methods_hook and get_property_methods_hook(ptype,name) then
296        return get_property_methods_hook(ptype, name)
297    end
298
299    if ptype == "default" then -- get_name, set_name
300        return "get_"..name, "set_"..name
301    end
302
303    if ptype == "qt" then -- name, setName
304        return name, "set"..string.upper(string.sub(name, 1, 1))..string.sub(name, 2, -1)
305    end
306
307    if ptype == "overload" then -- name, name
308        return name,name
309    end
310
311    return nil
312end
313
314-------------- the hooks
315
316-- called right after processing the $[ichl]file directives,
317-- right before processing anything else
318-- takes the package object as the parameter
319function preprocess_hook(p)
320    -- p.code has all the input code from the pkg
321end
322
323
324-- called for every $ifile directive
325-- takes a table with a string called 'code' inside, the filename, and any extra arguments
326-- passed to $ifile. no return value
327function include_file_hook(t, filename, ...)
328
329end
330
331-- called after processing anything that's not code (like '$renaming', comments, etc)
332-- and right before parsing the actual code.
333-- takes the Package object with all the code on the 'code' key. no return value
334function preparse_hook(package)
335
336end
337
338-- called before starting output
339function pre_output_hook(package)
340
341end
342
343-- called after writing all the output.
344-- takes the Package object
345function post_output_hook(package)
346
347end
348
349
350-- called from 'get_property_methods' to get the methods to retrieve a property
351-- according to its type
352function get_property_methods_hook(property_type, name)
353
354end
355
356-- called from ClassContainer:doparse with the string being parsed
357-- return nil, or a substring
358function parser_hook(s)
359
360    return nil
361end
362
363-- called from classFunction:supcode, before the call to the function is output
364function pre_call_hook(f)
365
366end
367
368-- called from classFunction:supcode, after the call to the function is output
369function post_call_hook(f)
370
371end
372
373-- called before the register code is output
374function pre_register_hook(package)
375
376end
377
378-- called to output an error message
379function output_error_hook(str, arg)
380    return string.format(str, arg)
381end
382
383-- custom pushers
384
385_push_functions = {}
386_is_functions = {}
387_to_functions = {}
388
389_base_push_functions = {}
390_base_is_functions = {}
391_base_to_functions = {}
392
393local function search_base(t, funcs)
394    local class = _global_classes[t]
395
396    while class do
397        if funcs[class.type] then
398            return funcs[class.type]
399        end
400        class = _global_classes[class.btype]
401    end
402    return nil
403end
404
405function get_push_function(t)
406    return _push_functions[t] or search_base(t, _base_push_functions) or "tolua_pushusertype"
407end
408
409function get_to_function(t)
410    return _to_functions[t] or search_base(t, _base_to_functions) or "tolua_tousertype"
411end
412 
413function get_is_function(t)
414    return _is_functions[t] or search_base(t, _base_is_functions) or "tolua_isusertype"
415end
416
Note: See TracBrowser for help on using the repository browser.