blob: 3abf638e2bed49043fd680c4e404f6f1c56bbec4 [file] [log] [blame]
Dave Hylands0308f962014-03-10 00:07:35 -07001from __future__ import print_function
2
Damien George55baff42014-01-21 21:40:13 +00003import argparse
4import re
Damien Georgefdf0da52014-03-08 15:03:25 +00005import sys
Damien George1976bae2014-01-24 22:22:00 +00006
7# codepoint2name is different in Python 2 to Python 3
8import platform
9if platform.python_version_tuple()[0] == '2':
10 from htmlentitydefs import codepoint2name
11elif platform.python_version_tuple()[0] == '3':
12 from html.entities import codepoint2name
Paul Sokolovsky73b70272014-04-13 05:28:46 +030013codepoint2name[ord('-')] = 'hyphen';
Damien George55baff42014-01-21 21:40:13 +000014
Damien Georgea71c83a2014-02-15 11:34:50 +000015# add some custom names to map characters that aren't in HTML
Damien George56e1f992015-01-11 14:16:24 +000016codepoint2name[ord(' ')] = 'space'
17codepoint2name[ord('\'')] = 'squot'
18codepoint2name[ord(',')] = 'comma'
Damien Georgea71c83a2014-02-15 11:34:50 +000019codepoint2name[ord('.')] = 'dot'
Paul Sokolovsky4aee1192014-02-18 00:06:37 +020020codepoint2name[ord(':')] = 'colon'
21codepoint2name[ord('/')] = 'slash'
Damien George58051112014-04-15 12:42:52 +010022codepoint2name[ord('%')] = 'percent'
Damien Georgeb013aea2014-04-15 12:50:21 +010023codepoint2name[ord('#')] = 'hash'
Damien George56e1f992015-01-11 14:16:24 +000024codepoint2name[ord('(')] = 'paren_open'
25codepoint2name[ord(')')] = 'paren_close'
26codepoint2name[ord('[')] = 'bracket_open'
27codepoint2name[ord(']')] = 'bracket_close'
Damien George897fe0c2014-04-15 22:03:55 +010028codepoint2name[ord('{')] = 'brace_open'
29codepoint2name[ord('}')] = 'brace_close'
Damien George708c0732014-04-27 19:23:46 +010030codepoint2name[ord('*')] = 'star'
Damien George56e1f992015-01-11 14:16:24 +000031codepoint2name[ord('!')] = 'bang'
Paul Sokolovskyf88eec02015-04-02 01:09:24 +030032codepoint2name[ord('\\')] = 'backslash'
Damien Georgea71c83a2014-02-15 11:34:50 +000033
Damien George55baff42014-01-21 21:40:13 +000034# this must match the equivalent function in qstr.c
35def compute_hash(qstr):
Damien George6e628c42014-03-25 15:27:15 +000036 hash = 5381
Damien George55baff42014-01-21 21:40:13 +000037 for char in qstr:
Damien George6e628c42014-03-25 15:27:15 +000038 hash = (hash * 33) ^ ord(char)
Chris Angelicode09caa2014-06-07 06:55:27 +100039 # Make sure that valid hash is never zero, zero means "hash not computed"
40 return (hash & 0xffff) or 1
Damien George55baff42014-01-21 21:40:13 +000041
42def do_work(infiles):
43 # read the qstrs in from the input files
Damien George6942f802015-01-11 17:52:45 +000044 qcfgs = {}
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020045 qstrs = {}
Damien George55baff42014-01-21 21:40:13 +000046 for infile in infiles:
47 with open(infile, 'rt') as f:
Damien George55baff42014-01-21 21:40:13 +000048 for line in f:
Damien George6942f802015-01-11 17:52:45 +000049 line = line.strip()
50
51 # is this a config line?
52 match = re.match(r'^QCFG\((.+), (.+)\)', line)
53 if match:
54 value = match.group(2)
55 if value[0] == '(' and value[-1] == ')':
56 # strip parenthesis from config value
57 value = value[1:-1]
58 qcfgs[match.group(1)] = value
59 continue
60
stijn1dc7f042014-05-02 21:10:47 +020061 # is this a QSTR line?
Damien George6942f802015-01-11 17:52:45 +000062 match = re.match(r'^Q\((.*)\)$', line)
stijn1dc7f042014-05-02 21:10:47 +020063 if not match:
Damien George5bb7d992014-04-13 13:16:51 +010064 continue
Damien George55baff42014-01-21 21:40:13 +000065
66 # get the qstr value
67 qstr = match.group(1)
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020068 ident = re.sub(r'[^A-Za-z0-9_]', lambda s: "_" + codepoint2name[ord(s.group(0))] + "_", qstr)
Damien George55baff42014-01-21 21:40:13 +000069
70 # don't add duplicates
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020071 if ident in qstrs:
Damien George55baff42014-01-21 21:40:13 +000072 continue
73
Damien George1976bae2014-01-24 22:22:00 +000074 # add the qstr to the list, with order number to retain original order in file
Paul Sokolovsky6ea0e922014-04-11 20:36:08 +030075 qstrs[ident] = (len(qstrs), ident, qstr)
Damien George55baff42014-01-21 21:40:13 +000076
Damien George95836f82015-01-11 22:27:30 +000077 # get config variables
78 cfg_bytes_len = int(qcfgs['BYTES_IN_LEN'])
79 cfg_max_len = 1 << (8 * cfg_bytes_len)
80
81 # print out the starte of the generated C header file
Damien George55baff42014-01-21 21:40:13 +000082 print('// This file was automatically generated by makeqstrdata.py')
Dave Hylands7a996b12014-01-21 15:28:27 -080083 print('')
Damien George95836f82015-01-11 22:27:30 +000084
Damien George6942f802015-01-11 17:52:45 +000085 # add NULL qstr with no hash or data
Damien George95836f82015-01-11 22:27:30 +000086 print('QDEF(MP_QSTR_NULL, (const byte*)"\\x00\\x00%s" "")' % ('\\x00' * cfg_bytes_len))
87
88 # go through each qstr and print it out
Paul Sokolovsky6ea0e922014-04-11 20:36:08 +030089 for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]):
Damien George55baff42014-01-21 21:40:13 +000090 qhash = compute_hash(qstr)
Paul Sokolovskyf88eec02015-04-02 01:09:24 +030091 # Calculate len of str, taking escapes into account
92 qlen = len(qstr.replace("\\\\", "-").replace("\\", ""))
Damien George56e1f992015-01-11 14:16:24 +000093 qdata = qstr.replace('"', '\\"')
Damien George95836f82015-01-11 22:27:30 +000094 if qlen >= cfg_max_len:
95 print('qstr is too long:', qstr)
96 assert False
Damien George99ab64f2015-01-11 22:40:38 +000097 qlen_str = ('\\x%02x' * cfg_bytes_len) % tuple(((qlen >> (8 * i)) & 0xff) for i in range(cfg_bytes_len))
Damien George95836f82015-01-11 22:27:30 +000098 print('QDEF(MP_QSTR_%s, (const byte*)"\\x%02x\\x%02x%s" "%s")' % (ident, qhash & 0xff, (qhash >> 8) & 0xff, qlen_str, qdata))
Damien George55baff42014-01-21 21:40:13 +000099
100 return True
101
102def main():
103 arg_parser = argparse.ArgumentParser(description='Process raw qstr file and output qstr data with length, hash and data bytes')
104 arg_parser.add_argument('files', nargs='+', help='input file(s)')
105 args = arg_parser.parse_args()
106
107 result = do_work(args.files)
108 if not result:
Damien Georgefdf0da52014-03-08 15:03:25 +0000109 print('exiting with error code', file=sys.stderr)
Damien George55baff42014-01-21 21:40:13 +0000110 exit(1)
111
112if __name__ == "__main__":
113 main()