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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
#!/usr/bin/env python
###############################################################################
# Top contributors (to current version):
# Makai Mann, Mudathir Mohamed, Aina Niemetz
#
# This file is part of the cvc5 project.
#
# Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
# in the top-level source directory and their institutional affiliations.
# All rights reserved. See the file COPYING in the top-level source
# directory for licensing information.
# #############################################################################
##
"""
This script reads cvc5/src/api/cpp/cvc5_kind.h and generates
.pxd and .pxi files which declare all the cvc5 kinds and
implement a Python wrapper for kinds, respectively. The
default names are kinds.pxd / kinds.pxi, but the name is
configurable from the command line with --kinds-file-prefix.
The script is aware of the '#if 0' pattern and will ignore
kinds declared between '#if 0' and '#endif'. It can also
handle nested '#if 0' pairs.
"""
import argparse
import os
import sys
# get access to cvc5/src/api/parsekinds.py
sys.path.insert(0, os.path.abspath('${CMAKE_SOURCE_DIR}/src/api'))
from parsekinds import *
#################### Default Filenames ################
DEFAULT_PREFIX = 'kinds'
################ Comments and Macro Tokens ############
PYCOMMENT = '#'
KINDS_PXD_TOP = \
r"""cdef extern from "api/cpp/cvc5_kind.h" namespace "cvc5::api":
enum Kind:
"""
KINDS_PXI_TOP = \
r'''# distutils: language = c++
# distutils: extra_compile_args = -std=c++11
from cvc5kinds cimport Kind as c_Kind
from enum import Enum
class Kind(Enum):
"""The Kinds enum"""
def __new__(cls, value, doc):
self = object.__new__(cls)
self._value_ = value
self.__doc__ = doc
return self
'''
KINDS_ATTR_TEMPLATE = r''' {name}=c_Kind.{kind}, """{doc}"""
'''
def gen_pxd(parser: KindsParser, filename):
with open(filename, "w") as f:
# include the format_name docstring in the generated file
# could be helpful for users to see the formatting rules
for line in parser.format_name.__doc__.split(NL):
f.write(PYCOMMENT)
if not line.isspace():
f.write(line)
f.write(NL)
f.write(KINDS_PXD_TOP)
for kind in parser.kinds:
f.write(' {},\n'.format(kind))
def gen_pxi(parser : KindsParser, filename):
with open(filename, "w") as f:
f.write(KINDS_PXI_TOP)
for kind, name in parser.kinds.items():
doc = parser.kinds_doc.get(name, '')
f.write(KINDS_ATTR_TEMPLATE.format(name=name, kind=kind, doc=doc))
if __name__ == "__main__":
parser = argparse.ArgumentParser('Read a kinds header file and generate a '
'corresponding pxd file, with simplified kind names.')
parser.add_argument('--kinds-header', metavar='<KINDS_HEADER>',
help='The header file to read kinds from')
parser.add_argument('--kinds-file-prefix', metavar='<KIND_FILE_PREFIX>',
help='The prefix for the .pxd and .pxi files to write '
'the Cython declarations to.',
default=DEFAULT_PREFIX)
args = parser.parse_args()
kinds_header = args.kinds_header
kinds_file_prefix = args.kinds_file_prefix
kp = KindsParser()
kp.parse(kinds_header)
gen_pxd(kp, kinds_file_prefix + ".pxd")
gen_pxi(kp, kinds_file_prefix + ".pxi")
|