blob: 8100842b4c13eb4494b6f65fcbcf9814b30df8e4 [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
12# codepoint2name is different in Python 2 to Python 3
13import platform
14if platform.python_version_tuple()[0] == '2':
15 from htmlentitydefs import codepoint2name
16elif platform.python_version_tuple()[0] == '3':
17 from html.entities import codepoint2name
Paul Sokolovsky73b70272014-04-13 05:28:46 +030018codepoint2name[ord('-')] = 'hyphen';
Damien George55baff42014-01-21 21:40:13 +000019
Damien Georgea71c83a2014-02-15 11:34:50 +000020# add some custom names to map characters that aren't in HTML
Damien George56e1f992015-01-11 14:16:24 +000021codepoint2name[ord(' ')] = 'space'
22codepoint2name[ord('\'')] = 'squot'
23codepoint2name[ord(',')] = 'comma'
Damien Georgea71c83a2014-02-15 11:34:50 +000024codepoint2name[ord('.')] = 'dot'
Paul Sokolovsky4aee1192014-02-18 00:06:37 +020025codepoint2name[ord(':')] = 'colon'
26codepoint2name[ord('/')] = 'slash'
Damien George58051112014-04-15 12:42:52 +010027codepoint2name[ord('%')] = 'percent'
Damien Georgeb013aea2014-04-15 12:50:21 +010028codepoint2name[ord('#')] = 'hash'
Damien George56e1f992015-01-11 14:16:24 +000029codepoint2name[ord('(')] = 'paren_open'
30codepoint2name[ord(')')] = 'paren_close'
31codepoint2name[ord('[')] = 'bracket_open'
32codepoint2name[ord(']')] = 'bracket_close'
Damien George897fe0c2014-04-15 22:03:55 +010033codepoint2name[ord('{')] = 'brace_open'
34codepoint2name[ord('}')] = 'brace_close'
Damien George708c0732014-04-27 19:23:46 +010035codepoint2name[ord('*')] = 'star'
Damien George56e1f992015-01-11 14:16:24 +000036codepoint2name[ord('!')] = 'bang'
Paul Sokolovskyf88eec02015-04-02 01:09:24 +030037codepoint2name[ord('\\')] = 'backslash'
Damien Georgea71c83a2014-02-15 11:34:50 +000038
Damien George55baff42014-01-21 21:40:13 +000039# this must match the equivalent function in qstr.c
Damien Georgec3bd9412015-07-20 11:03:13 +000040def compute_hash(qstr, bytes_hash):
Damien George6e628c42014-03-25 15:27:15 +000041 hash = 5381
Damien George55baff42014-01-21 21:40:13 +000042 for char in qstr:
Damien George6e628c42014-03-25 15:27:15 +000043 hash = (hash * 33) ^ ord(char)
Chris Angelicode09caa2014-06-07 06:55:27 +100044 # Make sure that valid hash is never zero, zero means "hash not computed"
Damien Georgec3bd9412015-07-20 11:03:13 +000045 return (hash & ((1 << (8 * bytes_hash)) - 1)) or 1
Damien George55baff42014-01-21 21:40:13 +000046
47def do_work(infiles):
48 # read the qstrs in from the input files
Damien George6942f802015-01-11 17:52:45 +000049 qcfgs = {}
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020050 qstrs = {}
Damien George55baff42014-01-21 21:40:13 +000051 for infile in infiles:
52 with open(infile, 'rt') as f:
Damien George55baff42014-01-21 21:40:13 +000053 for line in f:
Damien George6942f802015-01-11 17:52:45 +000054 line = line.strip()
55
56 # is this a config line?
57 match = re.match(r'^QCFG\((.+), (.+)\)', line)
58 if match:
59 value = match.group(2)
60 if value[0] == '(' and value[-1] == ')':
61 # strip parenthesis from config value
62 value = value[1:-1]
63 qcfgs[match.group(1)] = value
64 continue
65
stijn1dc7f042014-05-02 21:10:47 +020066 # is this a QSTR line?
Damien George6942f802015-01-11 17:52:45 +000067 match = re.match(r'^Q\((.*)\)$', line)
stijn1dc7f042014-05-02 21:10:47 +020068 if not match:
Damien George5bb7d992014-04-13 13:16:51 +010069 continue
Damien George55baff42014-01-21 21:40:13 +000070
71 # get the qstr value
72 qstr = match.group(1)
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020073 ident = re.sub(r'[^A-Za-z0-9_]', lambda s: "_" + codepoint2name[ord(s.group(0))] + "_", qstr)
Damien George55baff42014-01-21 21:40:13 +000074
75 # don't add duplicates
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020076 if ident in qstrs:
Damien George55baff42014-01-21 21:40:13 +000077 continue
78
Damien George1976bae2014-01-24 22:22:00 +000079 # add the qstr to the list, with order number to retain original order in file
Paul Sokolovsky6ea0e922014-04-11 20:36:08 +030080 qstrs[ident] = (len(qstrs), ident, qstr)
Damien George55baff42014-01-21 21:40:13 +000081
Damien George95836f82015-01-11 22:27:30 +000082 # get config variables
83 cfg_bytes_len = int(qcfgs['BYTES_IN_LEN'])
Damien Georgec3bd9412015-07-20 11:03:13 +000084 cfg_bytes_hash = int(qcfgs['BYTES_IN_HASH'])
Damien George95836f82015-01-11 22:27:30 +000085 cfg_max_len = 1 << (8 * cfg_bytes_len)
86
Paul Sokolovsky3a2fb202015-07-31 14:57:36 +030087 # print out the starter of the generated C header file
Damien George55baff42014-01-21 21:40:13 +000088 print('// This file was automatically generated by makeqstrdata.py')
Dave Hylands7a996b12014-01-21 15:28:27 -080089 print('')
Damien George95836f82015-01-11 22:27:30 +000090
Damien George6942f802015-01-11 17:52:45 +000091 # add NULL qstr with no hash or data
Damien Georgec3bd9412015-07-20 11:03:13 +000092 print('QDEF(MP_QSTR_NULL, (const byte*)"%s%s" "")' % ('\\x00' * cfg_bytes_hash, '\\x00' * cfg_bytes_len))
Damien George95836f82015-01-11 22:27:30 +000093
94 # go through each qstr and print it out
Paul Sokolovsky6ea0e922014-04-11 20:36:08 +030095 for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]):
Damien Georgec3bd9412015-07-20 11:03:13 +000096 qhash = compute_hash(qstr, cfg_bytes_hash)
Paul Sokolovskyf88eec02015-04-02 01:09:24 +030097 # Calculate len of str, taking escapes into account
98 qlen = len(qstr.replace("\\\\", "-").replace("\\", ""))
Damien George56e1f992015-01-11 14:16:24 +000099 qdata = qstr.replace('"', '\\"')
Damien George95836f82015-01-11 22:27:30 +0000100 if qlen >= cfg_max_len:
101 print('qstr is too long:', qstr)
102 assert False
Damien George99ab64f2015-01-11 22:40:38 +0000103 qlen_str = ('\\x%02x' * cfg_bytes_len) % tuple(((qlen >> (8 * i)) & 0xff) for i in range(cfg_bytes_len))
Damien Georgec3bd9412015-07-20 11:03:13 +0000104 qhash_str = ('\\x%02x' * cfg_bytes_hash) % tuple(((qhash >> (8 * i)) & 0xff) for i in range(cfg_bytes_hash))
105 print('QDEF(MP_QSTR_%s, (const byte*)"%s%s" "%s")' % (ident, qhash_str, qlen_str, qdata))
Damien George55baff42014-01-21 21:40:13 +0000106
Damien George55baff42014-01-21 21:40:13 +0000107if __name__ == "__main__":
Damien George26b512e2015-05-30 23:11:16 +0100108 do_work(sys.argv[1:])