Dave Hylands | 0308f96 | 2014-03-10 00:07:35 -0700 | [diff] [blame] | 1 | from __future__ import print_function |
| 2 | |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 3 | import argparse |
| 4 | import re |
Damien George | fdf0da5 | 2014-03-08 15:03:25 +0000 | [diff] [blame] | 5 | import sys |
Damien George | 1976bae | 2014-01-24 22:22:00 +0000 | [diff] [blame] | 6 | |
| 7 | # codepoint2name is different in Python 2 to Python 3 |
| 8 | import platform |
| 9 | if platform.python_version_tuple()[0] == '2': |
| 10 | from htmlentitydefs import codepoint2name |
| 11 | elif platform.python_version_tuple()[0] == '3': |
| 12 | from html.entities import codepoint2name |
Paul Sokolovsky | 73b7027 | 2014-04-13 05:28:46 +0300 | [diff] [blame] | 13 | codepoint2name[ord('-')] = 'hyphen'; |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 14 | |
Damien George | a71c83a | 2014-02-15 11:34:50 +0000 | [diff] [blame] | 15 | # add some custom names to map characters that aren't in HTML |
| 16 | codepoint2name[ord('.')] = 'dot' |
Paul Sokolovsky | 4aee119 | 2014-02-18 00:06:37 +0200 | [diff] [blame] | 17 | codepoint2name[ord(':')] = 'colon' |
| 18 | codepoint2name[ord('/')] = 'slash' |
Damien George | 5805111 | 2014-04-15 12:42:52 +0100 | [diff] [blame] | 19 | codepoint2name[ord('%')] = 'percent' |
Damien George | b013aea | 2014-04-15 12:50:21 +0100 | [diff] [blame] | 20 | codepoint2name[ord('#')] = 'hash' |
Damien George | 897fe0c | 2014-04-15 22:03:55 +0100 | [diff] [blame^] | 21 | codepoint2name[ord('{')] = 'brace_open' |
| 22 | codepoint2name[ord('}')] = 'brace_close' |
Damien George | a71c83a | 2014-02-15 11:34:50 +0000 | [diff] [blame] | 23 | |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 24 | # this must match the equivalent function in qstr.c |
| 25 | def compute_hash(qstr): |
Damien George | 6e628c4 | 2014-03-25 15:27:15 +0000 | [diff] [blame] | 26 | hash = 5381 |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 27 | for char in qstr: |
Damien George | 6e628c4 | 2014-03-25 15:27:15 +0000 | [diff] [blame] | 28 | hash = (hash * 33) ^ ord(char) |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 29 | return hash & 0xffff |
| 30 | |
Damien George | 5bb7d99 | 2014-04-13 13:16:51 +0100 | [diff] [blame] | 31 | # given a list of (name,regex) pairs, find the first one that matches the given line |
Damien George | 3683789 | 2014-04-14 23:38:37 +0100 | [diff] [blame] | 32 | def re_match_first(regexs, line): |
Damien George | 5bb7d99 | 2014-04-13 13:16:51 +0100 | [diff] [blame] | 33 | for name, regex in regexs: |
| 34 | match = re.match(regex, line) |
| 35 | if match: |
| 36 | return name, match |
| 37 | return None, None |
| 38 | |
Damien George | 3683789 | 2014-04-14 23:38:37 +0100 | [diff] [blame] | 39 | # regexs to recognise lines that the CPP emits |
| 40 | # use a list so that matching order is honoured |
| 41 | cpp_regexs = [ |
| 42 | ('qstr', r'Q\((.+)\)$'), |
| 43 | ('cdecl', r'(typedef|extern) [A-Za-z0-9_* ]+;$') |
| 44 | ] |
| 45 | |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 46 | def do_work(infiles): |
| 47 | # read the qstrs in from the input files |
Paul Sokolovsky | ab5d082 | 2014-01-24 00:22:00 +0200 | [diff] [blame] | 48 | qstrs = {} |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 49 | for infile in infiles: |
| 50 | with open(infile, 'rt') as f: |
| 51 | line_number = 0 |
| 52 | for line in f: |
| 53 | line_number += 1 |
| 54 | line = line.strip() |
| 55 | |
Damien George | 5bb7d99 | 2014-04-13 13:16:51 +0100 | [diff] [blame] | 56 | # ignore blank lines, comments and preprocessor directives |
| 57 | if len(line) == 0 or line.startswith('//') or line.startswith('#'): |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 58 | continue |
| 59 | |
Damien George | 5bb7d99 | 2014-04-13 13:16:51 +0100 | [diff] [blame] | 60 | # work out what kind of line it is |
Damien George | 3683789 | 2014-04-14 23:38:37 +0100 | [diff] [blame] | 61 | match_kind, match = re_match_first(cpp_regexs, line) |
Damien George | 5bb7d99 | 2014-04-13 13:16:51 +0100 | [diff] [blame] | 62 | if match_kind is None: |
| 63 | # unknown line format |
Damien George | fdf0da5 | 2014-03-08 15:03:25 +0000 | [diff] [blame] | 64 | print('({}:{}) bad qstr format, got {}'.format(infile, line_number, line), file=sys.stderr) |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 65 | return False |
Damien George | 5bb7d99 | 2014-04-13 13:16:51 +0100 | [diff] [blame] | 66 | elif match_kind != 'qstr': |
| 67 | # not a line with a qstr |
| 68 | continue |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 69 | |
| 70 | # get the qstr value |
| 71 | qstr = match.group(1) |
Paul Sokolovsky | ab5d082 | 2014-01-24 00:22:00 +0200 | [diff] [blame] | 72 | ident = re.sub(r'[^A-Za-z0-9_]', lambda s: "_" + codepoint2name[ord(s.group(0))] + "_", qstr) |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 73 | |
| 74 | # don't add duplicates |
Paul Sokolovsky | ab5d082 | 2014-01-24 00:22:00 +0200 | [diff] [blame] | 75 | if ident in qstrs: |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 76 | continue |
| 77 | |
Damien George | 1976bae | 2014-01-24 22:22:00 +0000 | [diff] [blame] | 78 | # add the qstr to the list, with order number to retain original order in file |
Paul Sokolovsky | 6ea0e92 | 2014-04-11 20:36:08 +0300 | [diff] [blame] | 79 | qstrs[ident] = (len(qstrs), ident, qstr) |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 80 | |
| 81 | # process the qstrs, printing out the generated C header file |
| 82 | print('// This file was automatically generated by makeqstrdata.py') |
Dave Hylands | 7a996b1 | 2014-01-21 15:28:27 -0800 | [diff] [blame] | 83 | print('') |
Paul Sokolovsky | 6ea0e92 | 2014-04-11 20:36:08 +0300 | [diff] [blame] | 84 | for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]): |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 85 | qhash = compute_hash(qstr) |
| 86 | qlen = len(qstr) |
Paul Sokolovsky | ab5d082 | 2014-01-24 00:22:00 +0200 | [diff] [blame] | 87 | print('Q({}, (const byte*)"\\x{:02x}\\x{:02x}\\x{:02x}\\x{:02x}" "{}")'.format(ident, qhash & 0xff, (qhash >> 8) & 0xff, qlen & 0xff, (qlen >> 8) & 0xff, qstr)) |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 88 | |
| 89 | return True |
| 90 | |
| 91 | def main(): |
| 92 | arg_parser = argparse.ArgumentParser(description='Process raw qstr file and output qstr data with length, hash and data bytes') |
| 93 | arg_parser.add_argument('files', nargs='+', help='input file(s)') |
| 94 | args = arg_parser.parse_args() |
| 95 | |
| 96 | result = do_work(args.files) |
| 97 | if not result: |
Damien George | fdf0da5 | 2014-03-08 15:03:25 +0000 | [diff] [blame] | 98 | print('exiting with error code', file=sys.stderr) |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 99 | exit(1) |
| 100 | |
| 101 | if __name__ == "__main__": |
| 102 | main() |