1#!/usr/bin/env python
2
3#
4# Copyright 2015, NICTA
5#
6# This software may be distributed and modified according to the terms of
7# the BSD 2-Clause license. Note that NO WARRANTY is provided.
8# See "LICENSE_BSD2.txt" for details.
9#
10# @TAG(NICTA_BSD)
11#
12
13import argparse, os, sys
14
15MY_DIR = os.path.dirname(os.path.abspath(__file__))
16
17sys.path.append(os.path.join(MY_DIR, '../../../internal/misc/pysymbols'))
18import isasymbols
19
20def main(argv):
21    parser = argparse.ArgumentParser(
22        description='generate unicode-ascii translation tables')
23    parser.add_argument('--output', '-o', type=argparse.FileType('w'),
24        default=sys.stdout, help='output file')
25    options = parser.parse_args(argv[1:])
26
27    with open(os.path.join(MY_DIR, '../../../isabelle/etc/symbols')) as f:
28        t = isasymbols.Translator(f.read())
29
30    # Write an ASCII-to-unicode table.
31    options.output.write('static map<wstring, wchar_t> ascii_to_unicode = {\n')
32    for sym in t.symbols:
33        options.output.write('    { L"%s", L\'\\x%x\' },\n' %
34            (sym.ascii_text.replace('\\', '\\\\'), sym.code_point))
35    options.output.write('};\n\n')
36
37    # Write a unicode-to-ASCII table.
38    options.output.write('static map<wchar_t, wstring> unicode_to_ascii = {\n')
39    for s in t.symbols:
40        options.output.write('    { L\'\\x%x\', L"%s" },\n' %
41            (s.code_point, s.ascii_text.replace('\\', '\\\\')))
42    options.output.write('};\n\n')
43
44    # Work out the maximum length of an ASCII sequence.
45    ascii_seq_max = max(len(s.ascii_text) for s in t.symbols)
46    options.output.write('static const unsigned int ASCII_SEQ_MAX = %u;\n\n' %
47        ascii_seq_max)
48
49    return 0
50
51if __name__ == '__main__':
52    sys.exit(main(sys.argv))
53