summaryrefslogtreecommitdiff
path: root/script/LuaJIT/c-parser/cdriver.lua
blob: c979f6ebe888f7728f156bda7121286205e07fb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
local cdriver = {}

local cpp = require("LuaJIT.c-parser.cpp")
local c99 = require("LuaJIT.c-parser.c99")
local ctypes = require("LuaJIT.c-parser.ctypes")
local cdefines = require("LuaJIT.c-parser.cdefines")

function cdriver.process_file(filename)
    local ctx, err = cpp.parse_file(filename)
    if not ctx then
        return nil, "failed preprocessing '"..filename.."': " .. err
    end

    local srccode = table.concat(ctx.output, "\n").." $EOF$"

    local res, err, line, col, fragment = c99.match_language_grammar(srccode)
    if not res then
        return nil, ("failed parsing: %s:%d:%d: %s\n%s"):format(filename, line, col, err, fragment)
    end

    local ffi_types, err = ctypes.register_types(res)
    if not ffi_types then
        return nil, err
    end

    cdefines.register_defines(ffi_types, ctx.defines)

    return ffi_types
end

function cdriver.process_context(context)
    local ctx, err = cpp.parse_context(context)
    if not ctx then
        return nil, "failed preprocessing '"..context.."': " .. err
    end

    local srccode = table.concat(ctx.output, "\n").." $EOF$"

    local res, err, line, col, fragment = c99.match_language_grammar(srccode)
    if not res then
        return nil, ("failed parsing: %s:%d:%d: %s\n%s"):format(context, line, col, err, fragment)
    end

    local ffi_types, err = ctypes.register_types(res)
    if not ffi_types then
        return nil, err
    end

    cdefines.register_defines(ffi_types, ctx.defines)

    return ffi_types
end

return cdriver