diff options
author | Damien George <damien.p.george@gmail.com> | 2014-01-24 14:14:44 -0800 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-01-24 14:14:44 -0800 |
commit | 60aca4810f68811b6fc0b69d02ccb05ddf2e555f (patch) | |
tree | 290586fcfd6428980ae5c04383f4a4e2458f409b /py/makeqstrdata.py | |
parent | 2b2cb7b7f4f467b67082f79053118df78f48e66e (diff) | |
parent | ab5d08280bda4e8c96a0729ae99400e1a8bf08a5 (diff) | |
download | micropython-60aca4810f68811b6fc0b69d02ccb05ddf2e555f.tar.gz micropython-60aca4810f68811b6fc0b69d02ccb05ddf2e555f.zip |
Merge pull request #215 from pfalcon/qstr-special-chars
Allow qstr's with non-ident chars, construct good identifier for them.
Diffstat (limited to 'py/makeqstrdata.py')
-rw-r--r-- | py/makeqstrdata.py | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/py/makeqstrdata.py b/py/makeqstrdata.py index c4b7be42e7..dbafd47d52 100644 --- a/py/makeqstrdata.py +++ b/py/makeqstrdata.py @@ -1,5 +1,6 @@ import argparse import re +from htmlentitydefs import codepoint2name # this must match the equivalent function in qstr.c def compute_hash(qstr): @@ -10,7 +11,7 @@ def compute_hash(qstr): def do_work(infiles): # read the qstrs in from the input files - qstrs = [] + qstrs = {} for infile in infiles: with open(infile, 'rt') as f: line_number = 0 @@ -23,28 +24,29 @@ def do_work(infiles): continue # verify line is of the correct form - match = re.match(r'Q\(([0-9A-Za-z_]+)\)$', line) + match = re.match(r'Q\((.+)\)$', line) if not match: print('({}:{}) bad qstr format, got {}'.format(infile, line_number, line)) return False # get the qstr value qstr = match.group(1) + ident = re.sub(r'[^A-Za-z0-9_]', lambda s: "_" + codepoint2name[ord(s.group(0))] + "_", qstr) # don't add duplicates - if qstr in qstrs: + if ident in qstrs: continue # add the qstr to the list - qstrs.append(qstr) + qstrs[ident] = qstr # process the qstrs, printing out the generated C header file print('// This file was automatically generated by makeqstrdata.py') print('') - for qstr in qstrs: + for ident, qstr in qstrs.items(): qhash = compute_hash(qstr) qlen = len(qstr) - print('Q({}, (const byte*)"\\x{:02x}\\x{:02x}\\x{:02x}\\x{:02x}" "{}")'.format(qstr, qhash & 0xff, (qhash >> 8) & 0xff, qlen & 0xff, (qlen >> 8) & 0xff, qstr)) + 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)) return True |