Repository navigation
Expand file tree
/
Copy pathpreprocessor_example.py
More file actions
70 lines (53 loc) · 1.72 KB
/
Copy pathpreprocessor_example.py
File metadata and controls
70 lines (53 loc) · 1.72 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""Example: #if / #elif / #else and macros.
Shows how
- ``#define N 5`` sets the size of arrays and bounds of sequences,
- ``#if`` / ``#elif`` / ``#else`` with ``defined(X)``, ``!``, ``&&``,
``||`` and comparisons select the definitions that are read,
- ``IDLParser(defines=...)`` defines macros from outside, like ``-D`` of
a C preprocessor, so that one IDL gives several variants.
Run::
python examples/preprocessor_example.py
"""
import _path # noqa: F401 (use the idl_parser of this checkout)
from idl_parser import parser
IDL = '''
#ifndef SENSOR_IDL
#define SENSOR_IDL
#ifndef MAX_POINTS
#define MAX_POINTS 16
#endif
#define VERSION 2
module sensor {
#if VERSION >= 2 && !defined(LEGACY)
struct Point { double x; double y; double z; };
#else
struct Point { double x; double y; };
#endif
struct Scan {
Point points[MAX_POINTS];
sequence<double, MAX_POINTS> intensities;
};
#if defined(USE_TIMESTAMP) || defined(DEBUG)
struct StampedScan { unsigned long long stamp; Scan scan; };
#elif VERSION == 1
struct OldScan { Scan scan; };
#endif
};
#endif // SENSOR_IDL
'''
def describe(defines):
global_module = parser.IDLParser(defines=defines).load(IDL)
sensor = global_module.module_by_name('sensor')
print('defines=%r' % (defines,))
for struct in sensor.structs:
members = ', '.join('%s %s' % (m.type.name, m.name) for m in struct.members)
print(' struct %s { %s }' % (struct.name, members))
bound = sensor.struct_by_name('Scan').member_by_name('intensities').type.bound
print(' intensities bound: %s' % bound)
print()
def main():
describe({})
describe({'LEGACY': ''})
describe({'USE_TIMESTAMP': '1', 'MAX_POINTS': '4'})
if __name__ == '__main__':
main()