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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
#!/usr/bin/python
"""Utility to generate the header files for BOOST_METAPARSE_STRING"""
# Copyright Abel Sinkovics (abel@sinkovics.hu) 2016.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
import argparse
import math
import os
import sys
VERSION = 1
class Namespace(object):
"""Generate namespace definition"""
def __init__(self, out_f, names):
self.out_f = out_f
self.names = names
def begin(self):
"""Generate the beginning part"""
self.out_f.write('\n')
for depth, name in enumerate(self.names):
self.out_f.write(
'{0}namespace {1}\n{0}{{\n'.format(self.prefix(depth), name)
)
def end(self):
"""Generate the closing part"""
for depth in xrange(len(self.names) - 1, -1, -1):
self.out_f.write('{0}}}\n'.format(self.prefix(depth)))
def prefix(self, depth=None):
"""Returns the prefix of a given depth. Returns the prefix code inside
the namespace should use when depth is None."""
if depth is None:
depth = len(self.names)
return ' ' * depth
def __enter__(self):
self.begin()
return self
def __exit__(self, typ, value, traceback):
self.end()
def write_autogen_info(out_f):
"""Write the comment about the file being autogenerated"""
out_f.write(
'\n'
'// This is an automatically generated header file.\n'
'// Generated with the tools/string_headers.py utility of\n'
'// Boost.Metaparse\n'
)
class IncludeGuard(object):
"""Generate include guards"""
def __init__(self, out_f):
self.out_f = out_f
def begin(self):
"""Generate the beginning part"""
name = 'BOOST_METAPARSE_V1_CPP11_IMPL_STRING_HPP'
self.out_f.write('#ifndef {0}\n#define {0}\n'.format(name))
write_autogen_info(self.out_f)
def end(self):
"""Generate the closing part"""
self.out_f.write('\n#endif\n')
def __enter__(self):
self.begin()
return self
def __exit__(self, typ, value, traceback):
self.end()
def macro_name(name):
"""Generate the full macro name"""
return 'BOOST_METAPARSE_V{0}_{1}'.format(VERSION, name)
def define_macro(out_f, (name, args, body), undefine=False, check=True):
"""Generate a macro definition or undefinition"""
if undefine:
out_f.write(
'#undef {0}\n'
.format(macro_name(name))
)
else:
if args:
arg_list = '({0})'.format(', '.join(args))
else:
arg_list = ''
if check:
out_f.write(
'#ifdef {0}\n'
'# error {0} already defined.\n'
'#endif\n'
.format(macro_name(name))
)
out_f.write(
'#define {0}{1} {2}\n'.format(macro_name(name), arg_list, body)
)
def filename(out_dir, name, undefine=False):
"""Generate the filename"""
if undefine:
prefix = 'undef_'
else:
prefix = ''
return os.path.join(out_dir, '{0}{1}.hpp'.format(prefix, name.lower()))
def length_limits(max_length_limit, length_limit_step):
"""Generates the length limits"""
string_len = len(str(max_length_limit))
return [
str(i).zfill(string_len) for i in
xrange(
length_limit_step,
max_length_limit + length_limit_step - 1,
length_limit_step
)
]
def unique_names(count):
"""Generate count unique variable name"""
return ('C{0}'.format(i) for i in xrange(0, count))
def generate_take(out_f, steps, line_prefix):
"""Generate the take function"""
out_f.write(
'{0}constexpr inline int take(int n_)\n'
'{0}{{\n'
'{0} return {1} 0 {2};\n'
'{0}}}\n'
'\n'.format(
line_prefix,
''.join('n_ >= {0} ? {0} : ('.format(s) for s in steps),
')' * len(steps)
)
)
def generate_make_string(out_f, max_step):
"""Generate the make_string template"""
steps = [2 ** n for n in xrange(int(math.log(max_step, 2)), -1, -1)]
with Namespace(
out_f,
['boost', 'metaparse', 'v{0}'.format(VERSION), 'impl']
) as nsp:
generate_take(out_f, steps, nsp.prefix())
out_f.write(
'{0}template <int LenNow, int LenRemaining, char... Cs>\n'
'{0}struct make_string;\n'
'\n'
'{0}template <char... Cs>'
' struct make_string<0, 0, Cs...> : string<> {{}};\n'
.format(nsp.prefix())
)
disable_sun = False
for i in reversed(steps):
if i > 64 and not disable_sun:
out_f.write('#ifndef __SUNPRO_CC\n')
disable_sun = True
out_f.write(
'{0}template <int LenRemaining,{1}char... Cs>'
' struct make_string<{2},LenRemaining,{3}Cs...> :'
' concat<string<{4}>,'
' typename make_string<take(LenRemaining),'
'LenRemaining-take(LenRemaining),Cs...>::type> {{}};\n'
.format(
nsp.prefix(),
''.join('char {0},'.format(n) for n in unique_names(i)),
i,
''.join('{0},'.format(n) for n in unique_names(i)),
','.join(unique_names(i))
)
)
if disable_sun:
out_f.write('#endif\n')
def generate_string(out_dir, limits):
"""Generate string.hpp"""
max_limit = max((int(v) for v in limits))
with open(filename(out_dir, 'string'), 'wb') as out_f:
with IncludeGuard(out_f):
out_f.write(
'\n'
'#include <boost/metaparse/v{0}/cpp11/impl/concat.hpp>\n'
'#include <boost/preprocessor/cat.hpp>\n'
.format(VERSION)
)
generate_make_string(out_f, 512)
out_f.write(
'\n'
'#ifndef BOOST_METAPARSE_LIMIT_STRING_SIZE\n'
'# error BOOST_METAPARSE_LIMIT_STRING_SIZE not defined\n'
'#endif\n'
'\n'
'#if BOOST_METAPARSE_LIMIT_STRING_SIZE > {0}\n'
'# error BOOST_METAPARSE_LIMIT_STRING_SIZE is greater than'
' {0}. To increase the limit run tools/string_headers.py of'
' Boost.Metaparse against your Boost headers.\n'
'#endif\n'
'\n'
.format(max_limit)
)
define_macro(out_f, (
'STRING',
['s'],
'{0}::make_string< '
'{0}::take(sizeof(s)-1), sizeof(s)-1-{0}::take(sizeof(s)-1),'
'BOOST_PP_CAT({1}, BOOST_METAPARSE_LIMIT_STRING_SIZE)(s)'
'>::type'
.format(
'::boost::metaparse::v{0}::impl'.format(VERSION),
macro_name('I')
)
))
out_f.write('\n')
for limit in xrange(0, max_limit + 1):
out_f.write(
'#define {0} {1}\n'
.format(
macro_name('I{0}'.format(limit)),
macro_name('INDEX_STR{0}'.format(
min(int(l) for l in limits if int(l) >= limit)
))
)
)
out_f.write('\n')
prev_macro = None
prev_limit = 0
for length_limit in (int(l) for l in limits):
this_macro = macro_name('INDEX_STR{0}'.format(length_limit))
out_f.write(
'#define {0}(s) {1}{2}\n'
.format(
this_macro,
'{0}(s),'.format(prev_macro) if prev_macro else '',
','.join(
'{0}((s), {1})'
.format(macro_name('STRING_AT'), i)
for i in xrange(prev_limit, length_limit)
)
)
)
prev_macro = this_macro
prev_limit = length_limit
def positive_integer(value):
"""Throws when the argument is not a positive integer"""
val = int(value)
if val > 0:
return val
else:
raise argparse.ArgumentTypeError("A positive number is expected")
def existing_path(value):
"""Throws when the path does not exist"""
if os.path.exists(value):
return value
else:
raise argparse.ArgumentTypeError("Path {0} not found".format(value))
def main():
"""The main function of the script"""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'--boost_dir',
required=False,
type=existing_path,
help='The path to the include/boost directory of Metaparse'
)
parser.add_argument(
'--max_length_limit',
required=False,
default=2048,
type=positive_integer,
help='The maximum supported length limit'
)
parser.add_argument(
'--length_limit_step',
required=False,
default=128,
type=positive_integer,
help='The longest step at which headers are generated'
)
args = parser.parse_args()
if args.boost_dir is None:
tools_path = os.path.dirname(os.path.abspath(__file__))
boost_dir = os.path.join(
os.path.dirname(tools_path),
'include',
'boost'
)
else:
boost_dir = args.boost_dir
if args.max_length_limit < 1:
sys.stderr.write('Invalid maximum length limit')
sys.exit(-1)
generate_string(
os.path.join(
boost_dir,
'metaparse',
'v{0}'.format(VERSION),
'cpp11',
'impl'
),
length_limits(args.max_length_limit, args.length_limit_step)
)
if __name__ == '__main__':
main()
|