Toluapp Class Access Labels |
|
public, protected and private labels in classes (or any container, including namespaces, modules, and even the main packages).
access flag, which determines the object's access inside its container. Container objects also have a member called curr_member_access, which determines the access of each child object at the moment of its addition to the container.
An object is considered public when the value of access is nil, false, or 0. Anything else means non-public, and tolua++ doesn't care. The default access value for all objects is nil, so this new feature doesn't affect current users.
-L option. Usually a parser function will search the beginning of the string for a token, and if it finds anything, return the string without that token (after doing whatever it has to do with the token). The parser_hook can safely return nil.
This example implements the 'parser_hook' to look for labels, and set the curr_member_access to the current access. It also sets curr_member_access to private by default (based on the fact that the default value for curr_member_access is nil). The labels will still work on other containers (modules, namespaces and the main package), but those will still be public by default.
-- access_hook.lua
local access = {public = 0, protected = 1, private = 2}
function preparse_hook(p)
-- we need to make all structs 'public' by default
p.code = string.gsub(p.code, "(struct[^;]*{)", "%1\npublic:\n")
end
function parser_hook(s)
local container = classContainer.curr -- get the current container
if not container.curr_member_access and container.classtype == 'class' then
-- default access for classes is private
container.curr_member_access = access.private
end
-- try labels (public, private, etc)
do
local b,e,label = string.find(s, "^%s*(%w*)%s*:[^:]") -- we need to check for [^:], otherwise it would match 'namespace::type'
if b then
-- found a label, get the new access value from the global 'access' table
if access[label] then
container.curr_member_access = access[label]
end -- else ?
return strsub(s, e) -- normally we would use 'e+1', but we need to preserve the [^:]
end
end
end
Usage
$ tolua++ -L access-hook.lua package.pkg