| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# qmi-mkenums is ported from glib-mkenums. |
| 4 |
# Same license applies. |
| 5 |
|
| 6 |
# If the code below looks horrible and unpythonic, do not panic. |
| 7 |
# |
| 8 |
# It is. |
| 9 |
# |
| 10 |
# This is a manual conversion from the original Perl script to |
| 11 |
# Python. Improvements are welcome. |
| 12 |
# |
| 13 |
from __future__ import print_function, unicode_literals |
| 14 |
|
| 15 |
import argparse |
| 16 |
import os |
| 17 |
import re |
| 18 |
import sys |
| 19 |
import tempfile |
| 20 |
import io |
| 21 |
import errno |
| 22 |
import codecs |
| 23 |
import locale |
| 24 |
|
| 25 |
VERSION_STR = '''glib-mkenums version 2.64.2 |
| 26 |
glib-mkenums comes with ABSOLUTELY NO WARRANTY. |
| 27 |
You may redistribute copies of glib-mkenums under the terms of |
| 28 |
the GNU General Public License which can be found in the |
| 29 |
GLib source package. Sources, examples and contact |
| 30 |
information are available at http://www.gtk.org''' |
| 31 |
|
| 32 |
# pylint: disable=too-few-public-methods |
| 33 |
class Color: |
| 34 |
'''ANSI Terminal colors''' |
| 35 |
GREEN = '\033[1;32m' |
| 36 |
BLUE = '\033[1;34m' |
| 37 |
YELLOW = '\033[1;33m' |
| 38 |
RED = '\033[1;31m' |
| 39 |
END = '\033[0m' |
| 40 |
|
| 41 |
|
| 42 |
def print_color(msg, color=Color.END, prefix='MESSAGE'): |
| 43 |
'''Print a string with a color prefix''' |
| 44 |
if os.isatty(sys.stderr.fileno()): |
| 45 |
real_prefix = '{start}{prefix}{end}'.format(start=color, prefix=prefix, end=Color.END) |
| 46 |
else: |
| 47 |
real_prefix = prefix |
| 48 |
print('{prefix}: {msg}'.format(prefix=real_prefix, msg=msg), file=sys.stderr) |
| 49 |
|
| 50 |
|
| 51 |
def print_error(msg): |
| 52 |
'''Print an error, and terminate''' |
| 53 |
print_color(msg, color=Color.RED, prefix='ERROR') |
| 54 |
sys.exit(1) |
| 55 |
|
| 56 |
|
| 57 |
def print_warning(msg, fatal=False): |
| 58 |
'''Print a warning, and optionally terminate''' |
| 59 |
if fatal: |
| 60 |
color = Color.RED |
| 61 |
prefix = 'ERROR' |
| 62 |
else: |
| 63 |
color = Color.YELLOW |
| 64 |
prefix = 'WARNING' |
| 65 |
print_color(msg, color, prefix) |
| 66 |
if fatal: |
| 67 |
sys.exit(1) |
| 68 |
|
| 69 |
|
| 70 |
def print_info(msg): |
| 71 |
'''Print a message''' |
| 72 |
print_color(msg, color=Color.GREEN, prefix='INFO') |
| 73 |
|
| 74 |
|
| 75 |
def get_rspfile_args(rspfile): |
| 76 |
''' |
| 77 |
Response files are useful on Windows where there is a command-line character |
| 78 |
limit of 8191 because when passing sources as arguments to glib-mkenums this |
| 79 |
limit can be exceeded in large codebases. |
| 80 |
|
| 81 |
There is no specification for response files and each tool that supports it |
| 82 |
generally writes them out in slightly different ways, but some sources are: |
| 83 |
https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-response-files |
| 84 |
https://docs.microsoft.com/en-us/windows/desktop/midl/the-response-file-command |
| 85 |
''' |
| 86 |
import shlex |
| 87 |
if not os.path.isfile(rspfile): |
| 88 |
sys.exit('Response file {!r} does not exist'.format(rspfile)) |
| 89 |
try: |
| 90 |
with open(rspfile, 'r') as f: |
| 91 |
cmdline = f.read() |
| 92 |
except OSError as e: |
| 93 |
sys.exit('Response file {!r} could not be read: {}' |
| 94 |
.format(rspfile, e.strerror)) |
| 95 |
return shlex.split(cmdline) |
| 96 |
|
| 97 |
|
| 98 |
def write_output(output): |
| 99 |
global output_stream |
| 100 |
print(output, file=output_stream) |
| 101 |
|
| 102 |
|
| 103 |
# Python 2 defaults to ASCII in case stdout is redirected. |
| 104 |
# This should make it match Python 3, which uses the locale encoding. |
| 105 |
if sys.stdout.encoding is None: |
| 106 |
output_stream = codecs.getwriter( |
| 107 |
locale.getpreferredencoding())(sys.stdout) |
| 108 |
else: |
| 109 |
output_stream = sys.stdout |
| 110 |
|
| 111 |
|
| 112 |
# Some source files aren't UTF-8 and the old perl version didn't care. |
| 113 |
# Replace invalid data with a replacement character to keep things working. |
| 114 |
# https://bugzilla.gnome.org/show_bug.cgi?id=785113#c20 |
| 115 |
def replace_and_warn(err): |
| 116 |
# 7 characters of context either side of the offending character |
| 117 |
print_warning('UnicodeWarning: {} at {} ({})'.format( |
| 118 |
err.reason, err.start, |
| 119 |
err.object[err.start - 7:err.end + 7])) |
| 120 |
return ('?', err.end) |
| 121 |
|
| 122 |
codecs.register_error('replace_and_warn', replace_and_warn) |
| 123 |
|
| 124 |
|
| 125 |
# glib-mkenums.py |
| 126 |
# Information about the current enumeration |
| 127 |
flags = None # Is enumeration a bitmask? |
| 128 |
option_underscore_name = '' # Overridden underscore variant of the enum name |
| 129 |
# for example to fix the cases we don't get the |
| 130 |
# mixed-case -> underscorized transform right. |
| 131 |
option_lowercase_name = '' # DEPRECATED. A lower case name to use as part |
| 132 |
# of the *_get_type() function, instead of the |
| 133 |
# one that we guess. For instance, when an enum |
| 134 |
# uses abnormal capitalization and we can not |
| 135 |
# guess where to put the underscores. |
| 136 |
option_since = '' # User provided version info for the enum. |
| 137 |
seenbitshift = 0 # Have we seen bitshift operators? |
| 138 |
enum_prefix = None # Prefix for this enumeration |
| 139 |
enumname = '' # Name for this enumeration |
| 140 |
enumshort = '' # $enumname without prefix |
| 141 |
enumname_prefix = '' # prefix of $enumname |
| 142 |
enumindex = 0 # Global enum counter |
| 143 |
firstenum = 1 # Is this the first enumeration per file? |
| 144 |
entries = [] # [ name, val ] for each entry |
| 145 |
sandbox = None # sandbox for safe evaluation of expressions |
| 146 |
|
| 147 |
output = '' # Filename to write result into |
| 148 |
|
| 149 |
def parse_trigraph(opts): |
| 150 |
result = {} |
| 151 |
|
| 152 |
for opt in re.split(r'\s*,\s*', opts): |
| 153 |
opt = re.sub(r'^\s*', '', opt) |
| 154 |
opt = re.sub(r'\s*$', '', opt) |
| 155 |
m = re.search(r'(\w+)(?:=(.+))?', opt) |
| 156 |
assert m is not None |
| 157 |
groups = m.groups() |
| 158 |
key = groups[0] |
| 159 |
if len(groups) > 1: |
| 160 |
val = groups[1] |
| 161 |
else: |
| 162 |
val = 1 |
| 163 |
result[key] = val |
| 164 |
return result |
| 165 |
|
| 166 |
def parse_entries(file, file_name): |
| 167 |
global entries, enumindex, enumname, seenbitshift, flags |
| 168 |
looking_for_name = False |
| 169 |
|
| 170 |
while True: |
| 171 |
line = file.readline() |
| 172 |
if not line: |
| 173 |
break |
| 174 |
|
| 175 |
line = line.strip() |
| 176 |
|
| 177 |
# read lines until we have no open comments |
| 178 |
while re.search(r'/\*([^*]|\*(?!/))*$', line): |
| 179 |
line += file.readline() |
| 180 |
|
| 181 |
# strip comments w/o options |
| 182 |
line = re.sub(r'''/\*(?!<) |
| 183 |
([^*]+|\*(?!/))* |
| 184 |
\*/''', '', line, flags=re.X) |
| 185 |
|
| 186 |
line = line.rstrip() |
| 187 |
|
| 188 |
# skip empty lines |
| 189 |
if len(line.strip()) == 0: |
| 190 |
continue |
| 191 |
|
| 192 |
if looking_for_name: |
| 193 |
m = re.match(r'\s*(\w+)', line) |
| 194 |
if m: |
| 195 |
enumname = m.group(1) |
| 196 |
return True |
| 197 |
|
| 198 |
# Handle include files |
| 199 |
m = re.match(r'\#include\s*<([^>]*)>', line) |
| 200 |
if m: |
| 201 |
newfilename = os.path.join("..", m.group(1)) |
| 202 |
newfile = io.open(newfilename, encoding="utf-8", |
| 203 |
errors="replace_and_warn") |
| 204 |
|
| 205 |
if not parse_entries(newfile, newfilename): |
| 206 |
return False |
| 207 |
else: |
| 208 |
continue |
| 209 |
|
| 210 |
m = re.match(r'\s*\}\s*(\w+)', line) |
| 211 |
if m: |
| 212 |
enumname = m.group(1) |
| 213 |
enumindex += 1 |
| 214 |
return 1 |
| 215 |
|
| 216 |
m = re.match(r'\s*\}', line) |
| 217 |
if m: |
| 218 |
enumindex += 1 |
| 219 |
looking_for_name = True |
| 220 |
continue |
| 221 |
|
| 222 |
m = re.match(r'''\s* |
| 223 |
(\w+)\s* # name |
| 224 |
(?:=( # value |
| 225 |
\s*\w+\s*\(.*\)\s* # macro with multiple args |
| 226 |
| # OR |
| 227 |
(?:[^,/]|/(?!\*))* # anything but a comma or comment |
| 228 |
))?,?\s* |
| 229 |
(?:/\*< # options |
| 230 |
(([^*]|\*(?!/))*) |
| 231 |
>\s*\*/)?,? |
| 232 |
\s*$''', line, flags=re.X) |
| 233 |
if m: |
| 234 |
groups = m.groups() |
| 235 |
name = groups[0] |
| 236 |
value = None |
| 237 |
options = None |
| 238 |
if len(groups) > 1: |
| 239 |
value = groups[1] |
| 240 |
if len(groups) > 2: |
| 241 |
options = groups[2] |
| 242 |
if flags is None and value is not None and '<<' in value: |
| 243 |
seenbitshift = 1 |
| 244 |
|
| 245 |
if options is not None: |
| 246 |
options = parse_trigraph(options) |
| 247 |
if 'skip' not in options: |
| 248 |
entries.append((name, value, options.get('nick'))) |
| 249 |
else: |
| 250 |
entries.append((name, value)) |
| 251 |
elif re.match(r's*\#', line): |
| 252 |
pass |
| 253 |
else: |
| 254 |
print_warning('Failed to parse "{}" in {}'.format(line, file_name)) |
| 255 |
return False |
| 256 |
|
| 257 |
help_epilog = '''Production text substitutions: |
| 258 |
\u0040EnumName\u0040 PrefixTheXEnum |
| 259 |
\u0040enum_name\u0040 prefix_the_xenum |
| 260 |
\u0040ENUMNAME\u0040 PREFIX_THE_XENUM |
| 261 |
\u0040ENUMSHORT\u0040 THE_XENUM |
| 262 |
\u0040ENUMPREFIX\u0040 PREFIX |
| 263 |
\u0040enumsince\u0040 the user-provided since value given (qmi-mkenums only) |
| 264 |
\u0040VALUENAME\u0040 PREFIX_THE_XVALUE |
| 265 |
\u0040valuenick\u0040 the-xvalue |
| 266 |
\u0040valuenum\u0040 the integer value (limited support, Since: 2.26) |
| 267 |
\u0040type\u0040 either enum or flags |
| 268 |
\u0040Type\u0040 either Enum or Flags |
| 269 |
\u0040TYPE\u0040 either ENUM or FLAGS |
| 270 |
\u0040filename\u0040 name of current input file |
| 271 |
\u0040basename\u0040 base name of the current input file (Since: 2.22) |
| 272 |
''' |
| 273 |
|
| 274 |
|
| 275 |
# production variables: |
| 276 |
idprefix = "" # "G", "Gtk", etc |
| 277 |
symprefix = "" # "g", "gtk", etc, if not just lc($idprefix) |
| 278 |
fhead = "" # output file header |
| 279 |
fprod = "" # per input file production |
| 280 |
ftail = "" # output file trailer |
| 281 |
eprod = "" # per enum text (produced prior to value itarations) |
| 282 |
vhead = "" # value header, produced before iterating over enum values |
| 283 |
vprod = "" # value text, produced for each enum value |
| 284 |
vtail = "" # value tail, produced after iterating over enum values |
| 285 |
comment_tmpl = "" # comment template |
| 286 |
|
| 287 |
def read_template_file(file): |
| 288 |
global idprefix, symprefix, fhead, fprod, ftail, eprod, vhead, vprod, vtail, comment_tmpl |
| 289 |
tmpl = {'file-header': fhead, |
| 290 |
'file-production': fprod, |
| 291 |
'file-tail': ftail, |
| 292 |
'enumeration-production': eprod, |
| 293 |
'value-header': vhead, |
| 294 |
'value-production': vprod, |
| 295 |
'value-tail': vtail, |
| 296 |
'comment': comment_tmpl, |
| 297 |
} |
| 298 |
in_ = 'junk' |
| 299 |
|
| 300 |
ifile = io.open(file, encoding="utf-8", errors="replace_and_warn") |
| 301 |
for line in ifile: |
| 302 |
m = re.match(r'\/\*\*\*\s+(BEGIN|END)\s+([\w-]+)\s+\*\*\*\/', line) |
| 303 |
if m: |
| 304 |
if in_ == 'junk' and m.group(1) == 'BEGIN' and m.group(2) in tmpl: |
| 305 |
in_ = m.group(2) |
| 306 |
continue |
| 307 |
elif in_ == m.group(2) and m.group(1) == 'END' and m.group(2) in tmpl: |
| 308 |
in_ = 'junk' |
| 309 |
continue |
| 310 |
else: |
| 311 |
sys.exit("Malformed template file " + file) |
| 312 |
|
| 313 |
if in_ != 'junk': |
| 314 |
tmpl[in_] += line |
| 315 |
|
| 316 |
if in_ != 'junk': |
| 317 |
sys.exit("Malformed template file " + file) |
| 318 |
|
| 319 |
fhead = tmpl['file-header'] |
| 320 |
fprod = tmpl['file-production'] |
| 321 |
ftail = tmpl['file-tail'] |
| 322 |
eprod = tmpl['enumeration-production'] |
| 323 |
vhead = tmpl['value-header'] |
| 324 |
vprod = tmpl['value-production'] |
| 325 |
vtail = tmpl['value-tail'] |
| 326 |
comment_tmpl = tmpl['comment'] |
| 327 |
|
| 328 |
parser = argparse.ArgumentParser(epilog=help_epilog, |
| 329 |
formatter_class=argparse.RawDescriptionHelpFormatter) |
| 330 |
|
| 331 |
parser.add_argument('--identifier-prefix', default='', dest='idprefix', |
| 332 |
help='Identifier prefix') |
| 333 |
parser.add_argument('--symbol-prefix', default='', dest='symprefix', |
| 334 |
help='Symbol prefix') |
| 335 |
parser.add_argument('--fhead', default=[], dest='fhead', action='append', |
| 336 |
help='Output file header') |
| 337 |
parser.add_argument('--ftail', default=[], dest='ftail', action='append', |
| 338 |
help='Output file footer') |
| 339 |
parser.add_argument('--fprod', default=[], dest='fprod', action='append', |
| 340 |
help='Put out TEXT every time a new input file is being processed.') |
| 341 |
parser.add_argument('--eprod', default=[], dest='eprod', action='append', |
| 342 |
help='Per enum text, produced prior to value iterations') |
| 343 |
parser.add_argument('--vhead', default=[], dest='vhead', action='append', |
| 344 |
help='Value header, produced before iterating over enum values') |
| 345 |
parser.add_argument('--vprod', default=[], dest='vprod', action='append', |
| 346 |
help='Value text, produced for each enum value.') |
| 347 |
parser.add_argument('--vtail', default=[], dest='vtail', action='append', |
| 348 |
help='Value tail, produced after iterating over enum values') |
| 349 |
parser.add_argument('--comments', default='', dest='comment_tmpl', |
| 350 |
help='Comment structure') |
| 351 |
parser.add_argument('--enums-only', default=False, action='store_true', dest='enumsonly', |
| 352 |
help='Only process enums, not flags') |
| 353 |
parser.add_argument('--flags-only', default=False, action='store_true', dest='flagsonly', |
| 354 |
help='Only process flags, not enums') |
| 355 |
parser.add_argument('--template', default='', dest='template', |
| 356 |
help='Template file') |
| 357 |
parser.add_argument('--output', default=None, dest='output') |
| 358 |
parser.add_argument('--version', '-v', default=False, action='store_true', dest='version', |
| 359 |
help='Print version information') |
| 360 |
parser.add_argument('args', nargs='*', |
| 361 |
help='One or more input files, or a single argument @rspfile_path ' |
| 362 |
'pointing to a file that contains the actual arguments') |
| 363 |
|
| 364 |
# Support reading an rspfile of the form @filename which contains the args |
| 365 |
# to be parsed |
| 366 |
if len(sys.argv) == 2 and sys.argv[1].startswith('@'): |
| 367 |
args = get_rspfile_args(sys.argv[1][1:]) |
| 368 |
else: |
| 369 |
args = sys.argv[1:] |
| 370 |
|
| 371 |
options = parser.parse_args(args) |
| 372 |
|
| 373 |
if options.version: |
| 374 |
print(VERSION_STR) |
| 375 |
sys.exit(0) |
| 376 |
|
| 377 |
def unescape_cmdline_args(arg): |
| 378 |
arg = arg.replace('\\n', '\n') |
| 379 |
arg = arg.replace('\\r', '\r') |
| 380 |
return arg.replace('\\t', '\t') |
| 381 |
|
| 382 |
if options.template != '': |
| 383 |
read_template_file(options.template) |
| 384 |
|
| 385 |
idprefix += options.idprefix |
| 386 |
symprefix += options.symprefix |
| 387 |
|
| 388 |
enumsonly = options.enumsonly |
| 389 |
flagsonly = options.flagsonly |
| 390 |
|
| 391 |
# This is a hack to maintain some semblance of backward compatibility with |
| 392 |
# the old, Perl-based glib-mkenums. The old tool had an implicit ordering |
| 393 |
# on the arguments and templates; each argument was parsed in order, and |
| 394 |
# all the strings appended. This allowed developers to write: |
| 395 |
# |
| 396 |
# glib-mkenums \ |
| 397 |
# --fhead ... \ |
| 398 |
# --template a-template-file.c.in \ |
| 399 |
# --ftail ... |
| 400 |
# |
| 401 |
# And have the fhead be prepended to the file-head stanza in the template, |
| 402 |
# as well as the ftail be appended to the file-tail stanza in the template. |
| 403 |
# Short of throwing away ArgumentParser and going over sys.argv[] element |
| 404 |
# by element, we can simulate that behaviour by ensuring some ordering in |
| 405 |
# how we build the template strings: |
| 406 |
# |
| 407 |
# - the head stanzas are always prepended to the template |
| 408 |
# - the prod stanzas are always appended to the template |
| 409 |
# - the tail stanzas are always appended to the template |
| 410 |
# |
| 411 |
# Within each instance of the command line argument, we append each value |
| 412 |
# to the array in the order in which it appears on the command line. |
| 413 |
fhead = ''.join([unescape_cmdline_args(x) for x in options.fhead]) + fhead |
| 414 |
vhead = ''.join([unescape_cmdline_args(x) for x in options.vhead]) + vhead |
| 415 |
|
| 416 |
fprod += ''.join([unescape_cmdline_args(x) for x in options.fprod]) |
| 417 |
eprod += ''.join([unescape_cmdline_args(x) for x in options.eprod]) |
| 418 |
vprod += ''.join([unescape_cmdline_args(x) for x in options.vprod]) |
| 419 |
|
| 420 |
ftail = ftail + ''.join([unescape_cmdline_args(x) for x in options.ftail]) |
| 421 |
vtail = vtail + ''.join([unescape_cmdline_args(x) for x in options.vtail]) |
| 422 |
|
| 423 |
if options.comment_tmpl != '': |
| 424 |
comment_tmpl = unescape_cmdline_args(options.comment_tmpl) |
| 425 |
elif comment_tmpl == "": |
| 426 |
# default to C-style comments |
| 427 |
comment_tmpl = "/* \u0040comment\u0040 */" |
| 428 |
|
| 429 |
output = options.output |
| 430 |
|
| 431 |
if output is not None: |
| 432 |
(out_dir, out_fn) = os.path.split(options.output) |
| 433 |
out_suffix = '_' + os.path.splitext(out_fn)[1] |
| 434 |
if out_dir == '': |
| 435 |
out_dir = '.' |
| 436 |
fd, filename = tempfile.mkstemp(dir=out_dir) |
| 437 |
os.close(fd) |
| 438 |
tmpfile = io.open(filename, "w", encoding="utf-8") |
| 439 |
output_stream = tmpfile |
| 440 |
else: |
| 441 |
tmpfile = None |
| 442 |
|
| 443 |
# put auto-generation comment |
| 444 |
comment = comment_tmpl.replace('\u0040comment\u0040', |
| 445 |
'This file is generated by glib-mkenums, do ' |
| 446 |
'not modify it. This code is licensed under ' |
| 447 |
'the same license as the containing project. ' |
| 448 |
'Note that it links to GLib, so must comply ' |
| 449 |
'with the LGPL linking clauses.') |
| 450 |
write_output("\n" + comment + '\n') |
| 451 |
|
| 452 |
def replace_specials(prod): |
| 453 |
prod = prod.replace(r'\\a', r'\a') |
| 454 |
prod = prod.replace(r'\\b', r'\b') |
| 455 |
prod = prod.replace(r'\\t', r'\t') |
| 456 |
prod = prod.replace(r'\\n', r'\n') |
| 457 |
prod = prod.replace(r'\\f', r'\f') |
| 458 |
prod = prod.replace(r'\\r', r'\r') |
| 459 |
prod = prod.rstrip() |
| 460 |
return prod |
| 461 |
|
| 462 |
|
| 463 |
def warn_if_filename_basename_used(section, prod): |
| 464 |
for substitution in ('\u0040filename\u0040', |
| 465 |
'\u0040basename\u0040'): |
| 466 |
if substitution in prod: |
| 467 |
print_warning('{} used in {} section.'.format(substitution, |
| 468 |
section)) |
| 469 |
|
| 470 |
if len(fhead) > 0: |
| 471 |
prod = fhead |
| 472 |
warn_if_filename_basename_used('file-header', prod) |
| 473 |
prod = replace_specials(prod) |
| 474 |
write_output(prod) |
| 475 |
|
| 476 |
def process_file(curfilename): |
| 477 |
global entries, flags, seenbitshift, enum_prefix |
| 478 |
firstenum = True |
| 479 |
|
| 480 |
try: |
| 481 |
curfile = io.open(curfilename, encoding="utf-8", |
| 482 |
errors="replace_and_warn") |
| 483 |
except IOError as e: |
| 484 |
if e.errno == errno.ENOENT: |
| 485 |
print_warning('No file "{}" found.'.format(curfilename)) |
| 486 |
return |
| 487 |
raise |
| 488 |
|
| 489 |
while True: |
| 490 |
line = curfile.readline() |
| 491 |
if not line: |
| 492 |
break |
| 493 |
|
| 494 |
line = line.strip() |
| 495 |
|
| 496 |
# read lines until we have no open comments |
| 497 |
while re.search(r'/\*([^*]|\*(?!/))*$', line): |
| 498 |
line += curfile.readline() |
| 499 |
|
| 500 |
# strip comments w/o options |
| 501 |
line = re.sub(r'''/\*(?!<) |
| 502 |
([^*]+|\*(?!/))* |
| 503 |
\*/''', '', line) |
| 504 |
|
| 505 |
# ignore forward declarations |
| 506 |
if re.match(r'\s*typedef\s+enum.*;', line): |
| 507 |
continue |
| 508 |
|
| 509 |
m = re.match(r'''\s*typedef\s+enum\s*[_A-Za-z]*[_A-Za-z0-9]*\s* |
| 510 |
({)?\s* |
| 511 |
(?:/\*< |
| 512 |
(([^*]|\*(?!/))*) |
| 513 |
>\s*\*/)? |
| 514 |
\s*({)?''', line, flags=re.X) |
| 515 |
if m: |
| 516 |
groups = m.groups() |
| 517 |
if len(groups) >= 2 and groups[1] is not None: |
| 518 |
options = parse_trigraph(groups[1]) |
| 519 |
if 'skip' in options: |
| 520 |
continue |
| 521 |
enum_prefix = options.get('prefix', None) |
| 522 |
flags = options.get('flags', None) |
| 523 |
if 'flags' in options: |
| 524 |
if flags is None: |
| 525 |
flags = 1 |
| 526 |
else: |
| 527 |
flags = int(flags) |
| 528 |
option_lowercase_name = options.get('lowercase_name', None) |
| 529 |
option_underscore_name = options.get('underscore_name', None) |
| 530 |
option_since = options.get('since', None) |
| 531 |
else: |
| 532 |
enum_prefix = None |
| 533 |
flags = None |
| 534 |
option_lowercase_name = None |
| 535 |
option_underscore_name = None |
| 536 |
option_since = None |
| 537 |
|
| 538 |
if option_lowercase_name is not None: |
| 539 |
if option_underscore_name is not None: |
| 540 |
print_warning("lowercase_name overridden with underscore_name") |
| 541 |
option_lowercase_name = None |
| 542 |
else: |
| 543 |
print_warning("lowercase_name is deprecated, use underscore_name") |
| 544 |
|
| 545 |
# Didn't have trailing '{' look on next lines |
| 546 |
if groups[0] is None and (len(groups) < 4 or groups[3] is None): |
| 547 |
while True: |
| 548 |
line = curfile.readline() |
| 549 |
if not line: |
| 550 |
print_error("Syntax error when looking for opening { in enum") |
| 551 |
if re.match(r'\s*\{', line): |
| 552 |
break |
| 553 |
|
| 554 |
seenbitshift = 0 |
| 555 |
entries = [] |
| 556 |
|
| 557 |
# Now parse the entries |
| 558 |
parse_entries(curfile, curfilename) |
| 559 |
|
| 560 |
# figure out if this was a flags or enums enumeration |
| 561 |
if flags is None: |
| 562 |
flags = seenbitshift |
| 563 |
|
| 564 |
if flags and enumsonly: |
| 565 |
continue |
| 566 |
elif not flags and flagsonly: |
| 567 |
continue |
| 568 |
|
| 569 |
# Autogenerate a prefix |
| 570 |
if enum_prefix is None: |
| 571 |
for entry in entries: |
| 572 |
if len(entry) < 3 or entry[2] is None: |
| 573 |
name = entry[0] |
| 574 |
if enum_prefix is not None: |
| 575 |
enum_prefix = os.path.commonprefix([name, enum_prefix]) |
| 576 |
else: |
| 577 |
enum_prefix = name |
| 578 |
if enum_prefix is None: |
| 579 |
enum_prefix = "" |
| 580 |
else: |
| 581 |
# Trim so that it ends in an underscore |
| 582 |
enum_prefix = re.sub(r'_[^_]*$', '_', enum_prefix) |
| 583 |
else: |
| 584 |
# canonicalize user defined prefixes |
| 585 |
enum_prefix = enum_prefix.upper() |
| 586 |
enum_prefix = enum_prefix.replace('-', '_') |
| 587 |
enum_prefix = re.sub(r'(.*)([^_])$', r'\1\2_', enum_prefix) |
| 588 |
|
| 589 |
fixed_entries = [] |
| 590 |
for e in entries: |
| 591 |
name = e[0] |
| 592 |
num = e[1] |
| 593 |
if len(e) < 3 or e[2] is None: |
| 594 |
nick = re.sub(r'^' + enum_prefix, '', name) |
| 595 |
nick = nick.replace('_', '-').lower() |
| 596 |
e = (name, num, nick) |
| 597 |
fixed_entries.append(e) |
| 598 |
entries = fixed_entries |
| 599 |
|
| 600 |
# Spit out the output |
| 601 |
if option_underscore_name is not None: |
| 602 |
enumlong = option_underscore_name.upper() |
| 603 |
enumsym = option_underscore_name.lower() |
| 604 |
enumshort = re.sub(r'^[A-Z][A-Z0-9]*_', '', enumlong) |
| 605 |
|
| 606 |
enumname_prefix = re.sub('_' + enumshort + '$', '', enumlong) |
| 607 |
elif symprefix == '' and idprefix == '': |
| 608 |
# enumname is e.g. GMatchType |
| 609 |
enspace = re.sub(r'^([A-Z][a-z]*).*$', r'\1', enumname) |
| 610 |
|
| 611 |
enumshort = re.sub(r'^[A-Z][a-z]*', '', enumname) |
| 612 |
enumshort = re.sub(r'([^A-Z])([A-Z])', r'\1_\2', enumshort) |
| 613 |
enumshort = re.sub(r'([A-Z][A-Z])([A-Z][0-9a-z])', r'\1_\2', enumshort) |
| 614 |
enumshort = enumshort.upper() |
| 615 |
|
| 616 |
enumname_prefix = re.sub(r'^([A-Z][a-z]*).*$', r'\1', enumname).upper() |
| 617 |
|
| 618 |
enumlong = enspace.upper() + "_" + enumshort |
| 619 |
enumsym = enspace.lower() + "_" + enumshort.lower() |
| 620 |
|
| 621 |
if option_lowercase_name is not None: |
| 622 |
enumsym = option_lowercase_name |
| 623 |
else: |
| 624 |
enumshort = enumname |
| 625 |
if idprefix: |
| 626 |
enumshort = re.sub(r'^' + idprefix, '', enumshort) |
| 627 |
else: |
| 628 |
enumshort = re.sub(r'/^[A-Z][a-z]*', '', enumshort) |
| 629 |
|
| 630 |
enumshort = re.sub(r'([^A-Z])([A-Z])', r'\1_\2', enumshort) |
| 631 |
enumshort = re.sub(r'([A-Z][A-Z])([A-Z][0-9a-z])', r'\1_\2', enumshort) |
| 632 |
enumshort = enumshort.upper() |
| 633 |
|
| 634 |
if symprefix: |
| 635 |
enumname_prefix = symprefix.upper() |
| 636 |
else: |
| 637 |
enumname_prefix = idprefix.upper() |
| 638 |
|
| 639 |
enumlong = enumname_prefix + "_" + enumshort |
| 640 |
enumsym = enumlong.lower() |
| 641 |
|
| 642 |
if option_since is not None: |
| 643 |
enumsince = option_since |
| 644 |
else: |
| 645 |
enumsince = "" |
| 646 |
|
| 647 |
if firstenum: |
| 648 |
firstenum = False |
| 649 |
|
| 650 |
if len(fprod) > 0: |
| 651 |
prod = fprod |
| 652 |
base = os.path.basename(curfilename) |
| 653 |
|
| 654 |
prod = prod.replace('\u0040filename\u0040', curfilename) |
| 655 |
prod = prod.replace('\u0040basename\u0040', base) |
| 656 |
prod = replace_specials(prod) |
| 657 |
|
| 658 |
write_output(prod) |
| 659 |
|
| 660 |
if len(eprod) > 0: |
| 661 |
prod = eprod |
| 662 |
|
| 663 |
prod = prod.replace('\u0040enum_name\u0040', enumsym) |
| 664 |
prod = prod.replace('\u0040EnumName\u0040', enumname) |
| 665 |
prod = prod.replace('\u0040ENUMSHORT\u0040', enumshort) |
| 666 |
prod = prod.replace('\u0040ENUMNAME\u0040', enumlong) |
| 667 |
prod = prod.replace('\u0040ENUMPREFIX\u0040', enumname_prefix) |
| 668 |
prod = prod.replace('\u0040enumsince\u0040', enumsince) |
| 669 |
if flags: |
| 670 |
prod = prod.replace('\u0040type\u0040', 'flags') |
| 671 |
else: |
| 672 |
prod = prod.replace('\u0040type\u0040', 'enum') |
| 673 |
if flags: |
| 674 |
prod = prod.replace('\u0040Type\u0040', 'Flags') |
| 675 |
else: |
| 676 |
prod = prod.replace('\u0040Type\u0040', 'Enum') |
| 677 |
if flags: |
| 678 |
prod = prod.replace('\u0040TYPE\u0040', 'FLAGS') |
| 679 |
else: |
| 680 |
prod = prod.replace('\u0040TYPE\u0040', 'ENUM') |
| 681 |
prod = replace_specials(prod) |
| 682 |
write_output(prod) |
| 683 |
|
| 684 |
if len(vhead) > 0: |
| 685 |
prod = vhead |
| 686 |
prod = prod.replace('\u0040enum_name\u0040', enumsym) |
| 687 |
prod = prod.replace('\u0040EnumName\u0040', enumname) |
| 688 |
prod = prod.replace('\u0040ENUMSHORT\u0040', enumshort) |
| 689 |
prod = prod.replace('\u0040ENUMNAME\u0040', enumlong) |
| 690 |
prod = prod.replace('\u0040ENUMPREFIX\u0040', enumname_prefix) |
| 691 |
prod = prod.replace('\u0040enumsince\u0040', enumsince) |
| 692 |
if flags: |
| 693 |
prod = prod.replace('\u0040type\u0040', 'flags') |
| 694 |
else: |
| 695 |
prod = prod.replace('\u0040type\u0040', 'enum') |
| 696 |
if flags: |
| 697 |
prod = prod.replace('\u0040Type\u0040', 'Flags') |
| 698 |
else: |
| 699 |
prod = prod.replace('\u0040Type\u0040', 'Enum') |
| 700 |
if flags: |
| 701 |
prod = prod.replace('\u0040TYPE\u0040', 'FLAGS') |
| 702 |
else: |
| 703 |
prod = prod.replace('\u0040TYPE\u0040', 'ENUM') |
| 704 |
prod = replace_specials(prod) |
| 705 |
write_output(prod) |
| 706 |
|
| 707 |
if len(vprod) > 0: |
| 708 |
prod = vprod |
| 709 |
next_num = 0 |
| 710 |
|
| 711 |
prod = replace_specials(prod) |
| 712 |
for name, num, nick in entries: |
| 713 |
tmp_prod = prod |
| 714 |
|
| 715 |
if '\u0040valuenum\u0040' in prod: |
| 716 |
# only attempt to eval the value if it is requested |
| 717 |
# this prevents us from throwing errors otherwise |
| 718 |
if num is not None: |
| 719 |
# use sandboxed evaluation as a reasonable |
| 720 |
# approximation to C constant folding |
| 721 |
inum = eval(num, {}, {}) |
| 722 |
|
| 723 |
# make sure it parsed to an integer |
| 724 |
if not isinstance(inum, int): |
| 725 |
sys.exit("Unable to parse enum value '%s'" % num) |
| 726 |
num = inum |
| 727 |
else: |
| 728 |
num = next_num |
| 729 |
|
| 730 |
tmp_prod = tmp_prod.replace('\u0040valuenum\u0040', str(num)) |
| 731 |
next_num = int(num) + 1 |
| 732 |
|
| 733 |
tmp_prod = tmp_prod.replace('\u0040VALUENAME\u0040', name) |
| 734 |
tmp_prod = tmp_prod.replace('\u0040valuenick\u0040', nick) |
| 735 |
if flags: |
| 736 |
tmp_prod = tmp_prod.replace('\u0040type\u0040', 'flags') |
| 737 |
else: |
| 738 |
tmp_prod = tmp_prod.replace('\u0040type\u0040', 'enum') |
| 739 |
if flags: |
| 740 |
tmp_prod = tmp_prod.replace('\u0040Type\u0040', 'Flags') |
| 741 |
else: |
| 742 |
tmp_prod = tmp_prod.replace('\u0040Type\u0040', 'Enum') |
| 743 |
if flags: |
| 744 |
tmp_prod = tmp_prod.replace('\u0040TYPE\u0040', 'FLAGS') |
| 745 |
else: |
| 746 |
tmp_prod = tmp_prod.replace('\u0040TYPE\u0040', 'ENUM') |
| 747 |
tmp_prod = tmp_prod.rstrip() |
| 748 |
|
| 749 |
write_output(tmp_prod) |
| 750 |
|
| 751 |
if len(vtail) > 0: |
| 752 |
prod = vtail |
| 753 |
prod = prod.replace('\u0040enum_name\u0040', enumsym) |
| 754 |
prod = prod.replace('\u0040EnumName\u0040', enumname) |
| 755 |
prod = prod.replace('\u0040ENUMSHORT\u0040', enumshort) |
| 756 |
prod = prod.replace('\u0040ENUMNAME\u0040', enumlong) |
| 757 |
prod = prod.replace('\u0040ENUMPREFIX\u0040', enumname_prefix) |
| 758 |
prod = prod.replace('\u0040enumsince\u0040', enumsince) |
| 759 |
if flags: |
| 760 |
prod = prod.replace('\u0040type\u0040', 'flags') |
| 761 |
else: |
| 762 |
prod = prod.replace('\u0040type\u0040', 'enum') |
| 763 |
if flags: |
| 764 |
prod = prod.replace('\u0040Type\u0040', 'Flags') |
| 765 |
else: |
| 766 |
prod = prod.replace('\u0040Type\u0040', 'Enum') |
| 767 |
if flags: |
| 768 |
prod = prod.replace('\u0040TYPE\u0040', 'FLAGS') |
| 769 |
else: |
| 770 |
prod = prod.replace('\u0040TYPE\u0040', 'ENUM') |
| 771 |
prod = replace_specials(prod) |
| 772 |
write_output(prod) |
| 773 |
|
| 774 |
for fname in sorted(options.args): |
| 775 |
process_file(fname) |
| 776 |
|
| 777 |
if len(ftail) > 0: |
| 778 |
prod = ftail |
| 779 |
warn_if_filename_basename_used('file-tail', prod) |
| 780 |
prod = replace_specials(prod) |
| 781 |
write_output(prod) |
| 782 |
|
| 783 |
# put auto-generation comment |
| 784 |
comment = comment_tmpl |
| 785 |
comment = comment.replace('\u0040comment\u0040', 'Generated data ends here') |
| 786 |
write_output("\n" + comment + "\n") |
| 787 |
|
| 788 |
if tmpfile is not None: |
| 789 |
tmpfilename = tmpfile.name |
| 790 |
tmpfile.close() |
| 791 |
|
| 792 |
try: |
| 793 |
os.unlink(options.output) |
| 794 |
except OSError as error: |
| 795 |
if error.errno != errno.ENOENT: |
| 796 |
raise error |
| 797 |
|
| 798 |
os.rename(tmpfilename, options.output) |