Module lcpp

lcpp - a C-PreProcessor for Lua 5.1 and LuaJIT ffi integration

Copyright (C) 2012-2014 Michael Schmoock michael@schmoock.net

Links

* GitHub page: http://github.com/willsteel/lcpp * Project page: http://lcpp.schmoock.net * Lua: http://www.lua.org * LuaJIT: http://luajit.org * Sponsored by: http://mmbbq.org

It can be used to pre-process LuaJIT ffi C header file input.

It can also be used to preprocess any other code (i.e. Lua itself)

git clone https://github.com/willsteel/lcpp.git

USAGE

-- load lcpp
local lcpp = require("lcpp")

-- use LuaJIT ffi and lcpp to parse cpp code
local ffi = require("ffi")
ffi.cdef("#include <your_header.h>")

-- use lcpp manually but add some predefines
local lcpp = require("lcpp");
local out = lcpp.compileFile("your_header.h", {UNICODE=1});
print(out);

-- compile some input manually
local out = lcpp.compile([[
    #include "myheader.h"
    #define MAXPATH 260
    typedef struct somestruct_t {
        void*          base;
        size_t         size;
        wchar_t        path[MAXPATH];
    } t_exe;
]])
-- the result should be like this
out == [[
    // <preprocessed content of file "myheader.h">
    typedef struct somestruct_t {
        void*          base;
        size_t         size;
        wchar_t        path[260];
    } t_exe;
]]

-- access lcpp defines dynamically (i.e. if used with ffi)
local ffi = require("ffi")
local lcpp = require("lcpp")
ffi.cdef("#include <your_header.h>")
=ffi.lcpp_defs.YOUR_DEFINE

This CPPs BNF:

RULES:
CODE              := {LINE}
LINE              := {STUFF NEWML} STUFF  NEWL
STUFF             := DIRECTIVE | IGNORED_CONTENT
DIRECTIVE         := OPTSPACES CMD OPTSPACES DIRECTIVE_NAME WHITESPACES DIRECTIVE_CONTENT WHITESPACES NEWL

LEAVES:
NEWL              := "\n"
NEWL_ESC          := "\\n"
WHITESPACES       := "[ \t]+"
OPTSPACES         := "[ \t]*"
COMMENT           := "//(.-)$"
MLCOMMENT         := "/[*](.-)[*]/"
IGNORED_CONTENT   := "[^#].*"
CMD               := "#"
DIRECTIVE_NAME    := "include"|"define"|"undef"|"if"|"else"|"elif"|"else if"|"endif"|"ifdef"|"ifndef"|"pragma"
DIRECTIVE_CONTENT := ".*?"

TODOs:

- lcpp.LCPP_LUA for: load, loadfile

License (MIT)


Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

MIT license: http://www.opensource.org/licenses/mit-license.php

Functions

init (input, predefines, macro_sources) initialies a lcpp state.
compile (code, predefines) the preprocessors main function.
compileFile (filename, predefines) preprocesses a file
disable () disable lcpp processing for ffi, loadstring and such
enable () (re)enable lcpp processing for ffi, loadstring and such


Functions

init (input, predefines, macro_sources)
initialies a lcpp state. not needed manually. handy for testing

Parameters:

  • input
  • predefines
  • macro_sources create sate var
compile (code, predefines)
the preprocessors main function. returns the preprocessed output as a string.

Parameters:

  • code data as string
  • predefines OPTIONAL a table of predefined variables

Usage:

  • lcpp.compile("#define bar 0x1337\nstatic const int foo = bar;")
  • lcpp.compile("#define bar 0x1337\nstatic const int foo = bar;", {["bar"] = "0x1338"})
compileFile (filename, predefines)
preprocesses a file

Parameters:

  • filename the file to read
  • predefines OPTIONAL a table of predefined variables

Usage:

    out, state = lcpp.compileFile("../odbg/plugin.h", {["MAX_PAH"]=260, ["UNICODE"]=true})
disable ()
disable lcpp processing for ffi, loadstring and such
enable ()
(re)enable lcpp processing for ffi, loadstring and such
generated by LDoc 1.4.6 Last updated 2018-11-23 01:24:02