forked from intelxed/xed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metaenum.py
executable file
·155 lines (140 loc) · 5.2 KB
/
metaenum.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
# -*- python -*-
# Mark Charney <[email protected]>
# Enumeration support
#BEGIN_LEGAL
#
#Copyright (c) 2019 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#END_LEGAL
# subprocess requires python 2.4 (replaces all os.popen() ) or later
import sys
import re
import enumer
###################################################################################
class metaenum_t(object):
"""This class is for reading in prefab enumeration files and
generating the corresponding enumeration by calling the enumer.py
module."""
comment_pattern = re.compile(r'[#].*$')
doxygen_comment_pattern = re.compile(r'//[/!]<.*')
def __init__(self, enum_fn, gendir='.'):
"""The inputs are an enumeration specification file and an
output directory."""
self.cplusplus=False
self.enum_fn = enum_fn # input file
self.gendir = gendir
self.tuples = None # list [enumer.enumer_value_t]
self.cfn = None
self.hfn = None
self.density = ''
self.namespace = None
self.type_name = None
self.prefix = None
self.stream_ifdef = None
self.proto_prefix=''
self.extra_header=None # might be a list
self.read_file()
def read_file(self):
"""Read in an existing enumeration file name, and build our
internal enumer structure. Return a tuple with the consumed data."""
stream_ifdef = ''
lines = open(self.enum_fn,'r').readlines()
simple_tuples = []
density = 'automatic'
namespace = None
proto_prefix = ''
extra_header = []
cplusplus = False
for line in lines:
nline = metaenum_t.comment_pattern.sub('',line).strip()
if len(nline) == 0:
continue
wrds = nline.split()
if wrds[0] == 'cplusplus':
cplusplus = True
elif wrds[0] == 'namespace':
namespace = wrds[1]
elif wrds[0] == 'hfn':
hfn = wrds[1]
elif wrds[0] == 'cfn':
cfn = wrds[1]
elif wrds[0] == 'density':
density = wrds[1]
elif wrds[0] == 'prefix':
prefix = wrds[1]
elif wrds[0] == 'typename':
typename = wrds[1]
elif wrds[0] == 'stream_ifdef':
stream_ifdef = wrds[1]
elif wrds[0] == 'proto_prefix':
proto_prefix = wrds[1]
elif wrds[0] == 'extra_header':
extra_header.append(wrds[1])
else:
token = wrds[0]
comment = None
value = None
if len(wrds) > 1:
if metaenum_t.doxygen_comment_pattern.match(wrds[1]):
comment = ' '.join(wrds[1:])
else:
value = wrds[1]
if len(wrds) > 2:
comment = ' '.join(wrds[2:])
simple_tuples.append( (token, value, comment) )
self.tuples = []
for token,value,comment in simple_tuples:
self.tuples.append(enumer.enumer_value_t(token,value,comment))
self.cfn = cfn
self.hfn = hfn
self.density = density
self.namespace = namespace
self.type_name = typename
self.prefix = prefix
self.stream_ifdef = stream_ifdef
self.proto_prefix= proto_prefix
self.extra_header= extra_header
self.cplusplus = cplusplus
def run_enumer(self):
e = enumer.enumer_t(self.type_name, self.prefix, self.tuples,
self.cfn, self.hfn,
self.gendir,
self.namespace,
self.stream_ifdef,
cplusplus = self.cplusplus,
proto_prefix = self.proto_prefix,
extra_header=self.extra_header,
density=self.density)
e.emit()
self.src_full_file_name = e.cf.full_file_name
self.hdr_full_file_name = e.hf.full_file_name
def _test_meta_enum():
m = metaenum_t("datafiles/xed-machine-modes-enum.txt", "obj")
m.run_enumer()
if __name__ == '__main__':
args = len(sys.argv)
if args == 1:
sys.stderr.write("TESTING %s\n" % sys.argv[0])
_test_meta_enum()
elif args == 3:
odir = sys.argv[1]
enum_file = sys.argv[2]
m = metaenum_t(enum_file, odir)
m.run_enumer()
sys.exit(0)
else:
sys.stderr.write("Usage: %s odir enumfile\n" % sys.argv[0])
sys.exit(1)