blob: 7249769f47af3d75f860e76c74466ce1e8268263 [file] [log] [blame]
Damien George26b512e2015-05-30 23:11:16 +01001"""
2Process raw qstr file and output qstr data with length, hash and data bytes.
3
4This script works with Python 2.6, 2.7, 3.3 and 3.4.
5"""
6
Dave Hylands0308f962014-03-10 00:07:35 -07007from __future__ import print_function
8
Damien George55baff42014-01-21 21:40:13 +00009import re
Damien Georgefdf0da52014-03-08 15:03:25 +000010import sys
Damien George1976bae2014-01-24 22:22:00 +000011
Damien George2243d682016-04-14 14:37:04 +010012# Python 2/3 compatibility:
13# - iterating through bytes is different
14# - codepoint2name lives in a different module
Damien George1976bae2014-01-24 22:22:00 +000015import platform
16if platform.python_version_tuple()[0] == '2':
Damien Georgef127bef2016-09-02 14:32:47 +100017 bytes_cons = lambda val, enc=None: bytearray(val)
Damien George1976bae2014-01-24 22:22:00 +000018 from htmlentitydefs import codepoint2name
19elif platform.python_version_tuple()[0] == '3':
Damien Georgef127bef2016-09-02 14:32:47 +100020 bytes_cons = bytes
Damien George1976bae2014-01-24 22:22:00 +000021 from html.entities import codepoint2name
Damien Georgef127bef2016-09-02 14:32:47 +100022# end compatibility code
23
Paul Sokolovsky73b70272014-04-13 05:28:46 +030024codepoint2name[ord('-')] = 'hyphen';
Damien George55baff42014-01-21 21:40:13 +000025
Damien Georgea71c83a2014-02-15 11:34:50 +000026# add some custom names to map characters that aren't in HTML
Damien George56e1f992015-01-11 14:16:24 +000027codepoint2name[ord(' ')] = 'space'
28codepoint2name[ord('\'')] = 'squot'
29codepoint2name[ord(',')] = 'comma'
Damien Georgea71c83a2014-02-15 11:34:50 +000030codepoint2name[ord('.')] = 'dot'
Paul Sokolovsky4aee1192014-02-18 00:06:37 +020031codepoint2name[ord(':')] = 'colon'
Damien Georgef30b6f02016-04-13 22:12:39 +010032codepoint2name[ord(';')] = 'semicolon'
Paul Sokolovsky4aee1192014-02-18 00:06:37 +020033codepoint2name[ord('/')] = 'slash'
Damien George58051112014-04-15 12:42:52 +010034codepoint2name[ord('%')] = 'percent'
Damien Georgeb013aea2014-04-15 12:50:21 +010035codepoint2name[ord('#')] = 'hash'
Damien George56e1f992015-01-11 14:16:24 +000036codepoint2name[ord('(')] = 'paren_open'
37codepoint2name[ord(')')] = 'paren_close'
38codepoint2name[ord('[')] = 'bracket_open'
39codepoint2name[ord(']')] = 'bracket_close'
Damien George897fe0c2014-04-15 22:03:55 +010040codepoint2name[ord('{')] = 'brace_open'
41codepoint2name[ord('}')] = 'brace_close'
Damien George708c0732014-04-27 19:23:46 +010042codepoint2name[ord('*')] = 'star'
Damien George56e1f992015-01-11 14:16:24 +000043codepoint2name[ord('!')] = 'bang'
Paul Sokolovskyf88eec02015-04-02 01:09:24 +030044codepoint2name[ord('\\')] = 'backslash'
Tony Abboud8d8fdcb2015-08-30 17:20:38 -040045codepoint2name[ord('+')] = 'plus'
Damien Georgef30b6f02016-04-13 22:12:39 +010046codepoint2name[ord('$')] = 'dollar'
47codepoint2name[ord('=')] = 'equals'
48codepoint2name[ord('?')] = 'question'
49codepoint2name[ord('@')] = 'at_sign'
50codepoint2name[ord('^')] = 'caret'
51codepoint2name[ord('|')] = 'pipe'
52codepoint2name[ord('~')] = 'tilde'
Damien Georgea71c83a2014-02-15 11:34:50 +000053
Damien George55baff42014-01-21 21:40:13 +000054# this must match the equivalent function in qstr.c
Damien Georgec3bd9412015-07-20 11:03:13 +000055def compute_hash(qstr, bytes_hash):
Damien George6e628c42014-03-25 15:27:15 +000056 hash = 5381
Damien Georgef127bef2016-09-02 14:32:47 +100057 for b in qstr:
58 hash = (hash * 33) ^ b
Chris Angelicode09caa2014-06-07 06:55:27 +100059 # Make sure that valid hash is never zero, zero means "hash not computed"
Damien Georgec3bd9412015-07-20 11:03:13 +000060 return (hash & ((1 << (8 * bytes_hash)) - 1)) or 1
Damien George55baff42014-01-21 21:40:13 +000061
Damien George594fa732016-01-31 12:59:59 +000062def qstr_escape(qst):
Damien Georgef30b6f02016-04-13 22:12:39 +010063 def esc_char(m):
64 c = ord(m.group(0))
65 try:
66 name = codepoint2name[c]
67 except KeyError:
68 name = '0x%02x' % c
69 return "_" + name + '_'
70 return re.sub(r'[^A-Za-z0-9_]', esc_char, qst)
Damien George594fa732016-01-31 12:59:59 +000071
72def parse_input_headers(infiles):
Damien George55baff42014-01-21 21:40:13 +000073 # read the qstrs in from the input files
Damien George6942f802015-01-11 17:52:45 +000074 qcfgs = {}
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020075 qstrs = {}
Damien George55baff42014-01-21 21:40:13 +000076 for infile in infiles:
77 with open(infile, 'rt') as f:
Damien George55baff42014-01-21 21:40:13 +000078 for line in f:
Damien George6942f802015-01-11 17:52:45 +000079 line = line.strip()
80
81 # is this a config line?
82 match = re.match(r'^QCFG\((.+), (.+)\)', line)
83 if match:
84 value = match.group(2)
85 if value[0] == '(' and value[-1] == ')':
86 # strip parenthesis from config value
87 value = value[1:-1]
88 qcfgs[match.group(1)] = value
89 continue
90
stijn1dc7f042014-05-02 21:10:47 +020091 # is this a QSTR line?
Damien George6942f802015-01-11 17:52:45 +000092 match = re.match(r'^Q\((.*)\)$', line)
stijn1dc7f042014-05-02 21:10:47 +020093 if not match:
Damien George5bb7d992014-04-13 13:16:51 +010094 continue
Damien George55baff42014-01-21 21:40:13 +000095
96 # get the qstr value
97 qstr = match.group(1)
Damien Georgea649d722016-04-14 15:22:36 +010098
99 # special case to specify control characters
100 if qstr == '\\n':
101 qstr = '\n'
102
103 # work out the corresponding qstr name
Damien George594fa732016-01-31 12:59:59 +0000104 ident = qstr_escape(qstr)
Damien George55baff42014-01-21 21:40:13 +0000105
106 # don't add duplicates
Paul Sokolovskyab5d0822014-01-24 00:22:00 +0200107 if ident in qstrs:
Damien George55baff42014-01-21 21:40:13 +0000108 continue
109
Damien George1976bae2014-01-24 22:22:00 +0000110 # add the qstr to the list, with order number to retain original order in file
Paul Sokolovsky6ea0e922014-04-11 20:36:08 +0300111 qstrs[ident] = (len(qstrs), ident, qstr)
Damien George55baff42014-01-21 21:40:13 +0000112
Paul Sokolovsky53ca6ae2015-10-11 11:09:57 +0300113 if not qcfgs:
114 sys.stderr.write("ERROR: Empty preprocessor output - check for errors above\n")
115 sys.exit(1)
116
Damien George594fa732016-01-31 12:59:59 +0000117 return qcfgs, qstrs
118
119def make_bytes(cfg_bytes_len, cfg_bytes_hash, qstr):
Damien Georgef127bef2016-09-02 14:32:47 +1000120 qbytes = bytes_cons(qstr, 'utf8')
121 qlen = len(qbytes)
122 qhash = compute_hash(qbytes, cfg_bytes_hash)
Damien George202d5ac2016-05-23 15:18:55 +0100123 if all(32 <= ord(c) <= 126 and c != '\\' and c != '"' for c in qstr):
Damien George49bb04e2016-04-14 14:20:25 +0100124 # qstr is all printable ASCII so render it as-is (for easier debugging)
Damien George49bb04e2016-04-14 14:20:25 +0100125 qdata = qstr
126 else:
127 # qstr contains non-printable codes so render entire thing as hex pairs
Damien Georgef127bef2016-09-02 14:32:47 +1000128 qdata = ''.join(('\\x%02x' % b) for b in qbytes)
Damien George594fa732016-01-31 12:59:59 +0000129 if qlen >= (1 << (8 * cfg_bytes_len)):
130 print('qstr is too long:', qstr)
131 assert False
132 qlen_str = ('\\x%02x' * cfg_bytes_len) % tuple(((qlen >> (8 * i)) & 0xff) for i in range(cfg_bytes_len))
133 qhash_str = ('\\x%02x' * cfg_bytes_hash) % tuple(((qhash >> (8 * i)) & 0xff) for i in range(cfg_bytes_hash))
134 return '(const byte*)"%s%s" "%s"' % (qhash_str, qlen_str, qdata)
135
136def print_qstr_data(qcfgs, qstrs):
Damien George95836f82015-01-11 22:27:30 +0000137 # get config variables
138 cfg_bytes_len = int(qcfgs['BYTES_IN_LEN'])
Damien Georgec3bd9412015-07-20 11:03:13 +0000139 cfg_bytes_hash = int(qcfgs['BYTES_IN_HASH'])
Damien George95836f82015-01-11 22:27:30 +0000140
Paul Sokolovsky3a2fb202015-07-31 14:57:36 +0300141 # print out the starter of the generated C header file
Damien George55baff42014-01-21 21:40:13 +0000142 print('// This file was automatically generated by makeqstrdata.py')
Dave Hylands7a996b12014-01-21 15:28:27 -0800143 print('')
Damien George95836f82015-01-11 22:27:30 +0000144
Damien George6942f802015-01-11 17:52:45 +0000145 # add NULL qstr with no hash or data
Damien Georgec3bd9412015-07-20 11:03:13 +0000146 print('QDEF(MP_QSTR_NULL, (const byte*)"%s%s" "")' % ('\\x00' * cfg_bytes_hash, '\\x00' * cfg_bytes_len))
Damien George95836f82015-01-11 22:27:30 +0000147
148 # go through each qstr and print it out
Paul Sokolovsky6ea0e922014-04-11 20:36:08 +0300149 for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]):
Damien George594fa732016-01-31 12:59:59 +0000150 qbytes = make_bytes(cfg_bytes_len, cfg_bytes_hash, qstr)
151 print('QDEF(MP_QSTR_%s, %s)' % (ident, qbytes))
152
153def do_work(infiles):
154 qcfgs, qstrs = parse_input_headers(infiles)
155 print_qstr_data(qcfgs, qstrs)
Damien George55baff42014-01-21 21:40:13 +0000156
Damien George55baff42014-01-21 21:40:13 +0000157if __name__ == "__main__":
Damien George26b512e2015-05-30 23:11:16 +0100158 do_work(sys.argv[1:])