Example code for an external emitter to generate Python bindings. More...
#include "../fbdoc_options.bi"
Go to the source code of this file.
Macros | |
#define | NEW_ENTRY /* Code(NL & NL & "# " & MID(.Fnam, INSTRREV(.Fnam, SLASH) + 1) & ": " & .LineNo) */ |
Macro to place a comment in to the output (file name and line number) More... | |
Functions | |
FUNCTION_AS_STRING | genNLiteral (BYREF_AS_STRING T) |
Transform number literals. More... | |
FUNCTION_AS_STRING | genCType (BYVAL_AS_Parser_PTR P) |
Transform a FB type in to a ctype. More... | |
SUB_CDECL | py_CreateIni (BYVAL_AS_Parser_PTR P) |
Export the initializer(s) for a variable. More... | |
SUB_CDECL | py_entryListPara (BYVAL_AS_Parser_PTR P) |
Emitter to extract the types of a parameter list. More... | |
SUB_CDECL | py_function (BYVAL_AS_Parser_PTR P) |
Emitter called when the Parser is on top of a function body. More... | |
SUB_CDECL | py_emitEnumNames (BYVAL_AS_Parser_PTR P) |
Emitter to generate a line for a ENUM block entry. More... | |
SUB_CDECL | py_emitBlockNames (BYVAL_AS_Parser_PTR P) |
Emitter to generate a line for a TYPE / UNION block entry. More... | |
SUB_CDECL | py_declare (BYVAL_AS_Parser_PTR P) |
Emitter called when the Parser is at a variable declaration. More... | |
SUB_CDECL | py_enum (BYVAL_AS_Parser_PTR P) |
Emitter called when the Parser is at the start of a ENUM block. More... | |
SUB_CDECL | py_union (BYVAL_AS_Parser_PTR P) |
Emitter called when the Parser is at the start of a UNION block. More... | |
SUB_CDECL | py_class (BYVAL_AS_Parser_PTR P) |
Emitter called when the Parser is at the start of a TYPE block. More... | |
SUB_CDECL | py_define (BYVAL_AS_Parser_PTR P) |
Emitter called when the Parser is at a #DEFINE line or at the start of a #MACRO More... | |
SUB_CDECL | py_include (BYVAL_AS_Parser_PTR P) |
Emitter called when the Parser is at an #INCLUDE line. More... | |
SUB_CDECL | py_init (BYVAL_AS_Parser_PTR P) |
Emitter called when the Parser is at the start of a file. More... | |
SUB_CDECL | py_exit (BYVAL_AS_Parser_PTR P) |
Emitter called when the Parser is at the end of a input file. More... | |
SUB_CDECL | py_CTOR (BYVAL_AS_Options_PTR O) |
Constructor called before the first input gets parsed. More... | |
SUB_CDECL | EmitterInit (BYVAL_AS_EmitterIF_PTR Emi, BYREF_AS_STRING Par) |
Initialize the EmitterIF and evaluate parameters. More... | |
Variables | |
LONG | ENUM_COUNT |
A counter for ENUM blocks. More... | |
STRING | T1 |
The fields type block. More... | |
STRING | T2 |
The list of type names (, separated) More... | |
STRING | CLASSES |
The list of class names (!"\n" separated) More... | |
STRING | LIBRARY |
The name of the binary to build. More... | |
STRING | IMPORTS |
The list of Python imports. More... | |
STRING | HEADER |
The header for the output. More... | |
Example code for an external emitter to generate Python bindings.
This file contains example source code for an external emitter. The emitter outputs Python declarations from the FB source code, in order to generate Python bindings based on ctypes for libraries compiled in FB.
The author is neither keen on Python syntax, nor on ctypes specials. Rather than a perfect example, this code is more like a quick and dirty solution to generate a Phyton binding for the library libpruio. But interrested users may find some inspiration in the code.
Example:
In order to compute a ctypes binding for libpruio, execute in the folder src/bas/plugins
the following command:
-e "py_ctypes"
: choose external emitter named py_ctypes-t
: follow source tree-pylib=libpruio
: binary name to include for python code (special plugin option, unknown for fbdoc)../libpruio/src/pruio/pruio.bi
: the file to start at (expects that libpruio source is installed at the same directory level as fbdoc project source)The output comes at STOUT (in the shell). In order to write it to a file just append > libpruio.py
to the command, like
\ToDo load RepData from file in CTOR
Definition in file py_ctypes.bas.
#define NEW_ENTRY /* Code(NL & NL & "# " & MID(.Fnam, INSTRREV(.Fnam, SLASH) + 1) & ": " & .LineNo) */ |
Macro to place a comment in to the output (file name and line number)
Definition at line 49 of file py_ctypes.bas.
FUNCTION_AS_STRING genNLiteral | ( | BYREF_AS_STRING | T | ) |
Transform number literals.
T | The string to work on |
Helper procedure that checks a string for numerical literals and transforms the FB notation to Python equivalents.
Definition at line 72 of file py_ctypes.bas.
FUNCTION_AS_STRING genCType | ( | BYVAL_AS_Parser_PTR | P | ) |
Transform a FB type in to a ctype.
P | The parser calling this emitter |
Helper function that transforms a FB type in to the ctypes equivalent in case of a standard type. Otherwise the original type symbol gets returned. Pointers get enclosed by POINTER(...)
, but ZSTRING PTR
gets transformed to c_char_p
.
Definition at line 100 of file py_ctypes.bas.
SUB_CDECL py_CreateIni | ( | BYVAL_AS_Parser_PTR | P | ) |
Export the initializer(s) for a variable.
P | the parser calling this emitter |
This sub reads the initializer from the input buffer and emits them unchanged. This may be a bunch of text in case of an array initializer.
Definition at line 136 of file py_ctypes.bas.
SUB_CDECL py_entryListPara | ( | BYVAL_AS_Parser_PTR | P | ) |
Emitter to extract the types of a parameter list.
P | The parser calling this emitter |
Emitter that collects the types of the parameter list in global variable T2
, to be used in py_function().
Definition at line 188 of file py_ctypes.bas.
SUB_CDECL py_function | ( | BYVAL_AS_Parser_PTR | P | ) |
Emitter called when the Parser is on top of a function body.
P | The parser calling this emitter |
FIXME
Definition at line 203 of file py_ctypes.bas.
SUB_CDECL py_emitEnumNames | ( | BYVAL_AS_Parser_PTR | P | ) |
Emitter to generate a line for a ENUM block entry.
P | The parser calling this emitter |
Emitter that gets called when the parser is in a block (ENUM
). It generates a line for each member and writes it (them) to the output.
Definition at line 241 of file py_ctypes.bas.
SUB_CDECL py_emitBlockNames | ( | BYVAL_AS_Parser_PTR | P | ) |
Emitter to generate a line for a TYPE / UNION block entry.
P | The parser calling this emitter |
This emitter gets called when the parser is in a block (TYPE UNION
). It generates a line for each member and writes it (them) to the output.
Definition at line 272 of file py_ctypes.bas.
SUB_CDECL py_declare | ( | BYVAL_AS_Parser_PTR | P | ) |
Emitter called when the Parser is at a variable declaration.
P | The parser calling this emitter |
Emitter that generates a variable (or function) declaration and writes it (them) to the output.
Definition at line 339 of file py_ctypes.bas.
SUB_CDECL py_enum | ( | BYVAL_AS_Parser_PTR | P | ) |
Emitter called when the Parser is at the start of a ENUM block.
P | The parser calling this emitter |
FIXME
Definition at line 367 of file py_ctypes.bas.
SUB_CDECL py_union | ( | BYVAL_AS_Parser_PTR | P | ) |
Emitter called when the Parser is at the start of a UNION block.
P | The parser calling this emitter |
FIXME
Definition at line 383 of file py_ctypes.bas.
SUB_CDECL py_class | ( | BYVAL_AS_Parser_PTR | P | ) |
Emitter called when the Parser is at the start of a TYPE block.
P | The parser calling this emitter |
FIXME
Definition at line 398 of file py_ctypes.bas.
SUB_CDECL py_define | ( | BYVAL_AS_Parser_PTR | P | ) |
Emitter called when the Parser is at a #DEFINE
line or at the start of a #MACRO
P | The parser calling this emitter |
FIXME
Definition at line 424 of file py_ctypes.bas.
SUB_CDECL py_include | ( | BYVAL_AS_Parser_PTR | P | ) |
Emitter called when the Parser is at an #INCLUDE
line.
P | The parser calling this emitter |
FIXME
Definition at line 447 of file py_ctypes.bas.
SUB_CDECL py_init | ( | BYVAL_AS_Parser_PTR | P | ) |
Emitter called when the Parser is at the start of a file.
P | The parser calling this emitter |
This emitter sends the HEADER information to the output once.
Definition at line 461 of file py_ctypes.bas.
SUB_CDECL py_exit | ( | BYVAL_AS_Parser_PTR | P | ) |
Emitter called when the Parser is at the end of a input file.
P | The parser calling this emitter |
This emitter just adds a new line character to the output.
Definition at line 473 of file py_ctypes.bas.
SUB_CDECL py_CTOR | ( | BYVAL_AS_Options_PTR | O | ) |
Constructor called before the first input gets parsed.
O | The Options UDT calling this constructor |
FIXME
Definition at line 485 of file py_ctypes.bas.
SUB_CDECL EmitterInit | ( | BYVAL_AS_EmitterIF_PTR | Emi, |
BYREF_AS_STRING | Par | ||
) |
Initialize the EmitterIF and evaluate parameters.
Emi | The newly created EmitterIF to fill with our callbacks |
Par | Additional command line parameters, not parsed by fbdoc |
FIXME
Definition at line 516 of file py_ctypes.bas.
LONG ENUM_COUNT |
A counter for ENUM blocks.
Definition at line 51 of file py_ctypes.bas.
STRING T1 |
The fields type block.
Definition at line 54 of file py_ctypes.bas.
STRING T2 |
The list of type names (,
separated)
Definition at line 55 of file py_ctypes.bas.
STRING CLASSES |
The list of class names (!"\n"
separated)
Definition at line 56 of file py_ctypes.bas.
STRING LIBRARY |
The name of the binary to build.
Definition at line 57 of file py_ctypes.bas.
STRING IMPORTS |
The list of Python imports.
Definition at line 58 of file py_ctypes.bas.
STRING HEADER |
The header for the output.
Definition at line 59 of file py_ctypes.bas.