source: proto/charsetcompiler/generate_UCD_property_functions.py @ 4424

Last change on this file since 4424 was 4424, checked in by cameron, 4 years ago

Restructure/bug fix for if-hierarchy generation

File size: 6.8 KB
Line 
1#
2# generate_UCD_property_functions.py -
3# generating Python pablo functions for various Unicode properties
4#
5# Robert D. Cameron
6# January 2, 2015
7#
8# Licensed under Open Software License 3.0.
9#
10#
11import re, string, os.path, UCD.cformat
12from UCD.unicode_set import *
13from UCD.UCD_parser import *
14from if_hierarchy import *
15from string import Template
16
17Unicode_property_fn_template = Template(r"""
18Property_${property}_${value} f${property}_${value};
19
20extern "C" {
21    BitBlock __get_${property}_${value} (Basis_bits & basis_bits) {
22        Struct_${property}_${value} output;
23        f${property}_${value} . do_block(basis_bits, output);
24        return output.cc;
25    }
26}
27""")
28
29Unicode_pablo_template = r"""
30
31#include "icgrep.h"
32
33#define LocalCarryDeclare(name, count)\
34CarryArray<count, 0> name;\
35
36
37@global
38
39
40"""
41
42Unicode_CC_struct = Template("class Struct_${property}_${value}:\n\tcc = 0\n\n")
43
44Unicode_CC_header = Template("def property_${property}_${value}(basis_bits, struct_${property}_${value}):\n")
45
46Unicode_dummy_main = "\n\ndef Main(basis_bits):\n    pass\n"
47
48Unicode_template_var = Template("struct_${property}_%s.cc")
49
50install_fn = r"""
51void install_property_%s_fn_ptrs(pablo::PabloCompiler & p) {
52  %s
53}
54"""
55
56class UCD_generator():
57    def __init__(self): 
58       pass
59 
60    def load_property_name_info(self):
61        (self.property_enum_name_list, self.full_name_map, self.property_lookup_map, self.property_kind_map) = parse_PropertyAlias_txt()
62
63    def load_property_value_info(self):
64        (self.property_value_list, self.property_value_enum_integer, self.property_value_full_name_map, self.property_value_lookup_map, self.missing_specs) = parse_PropertyValueAlias_txt(self.property_lookup_map)
65
66    def generate_enumerated_property_template(self, filename_root, property_code):
67        f = cformat.open_header_file_for_write(os.path.basename("ucd_precompiled_%s" % property_code), 'generate_UCD_property_functions.py')
68        cformat.write_imports(f, ['"pablo/pablo_compiler.h"'])
69        f.write(Unicode_pablo_template)
70        pc = property_code.lower()
71        vlist = self.property_value_list[property_code]
72        for v in vlist:
73            f.write(Unicode_property_fn_template.substitute({'property': pc, 'value': v}))
74        install_stmt = "  p.InstallExternalFunction(\"__get_%s_%%s\", (void *) & __get_%s_%%s);\n" % (pc, pc)
75        install_list = [install_stmt % (k, k) for k in vlist]
76        f.write(install_fn % (pc, "".join(install_list)))
77        cformat.close_header_file(f)
78       
79    def generate_enumerated_property_file(self, filename_root, property_code):
80        vlist = self.property_value_list[property_code]
81        canon_map = self.property_value_lookup_map[property_code]
82        (prop_values, value_map) = parse_UCD_enumerated_property_map(property_code, vlist, canon_map, filename_root + '.txt')
83        basename = os.path.basename(filename_root)
84        f = open(basename + ".pablo", "wb")
85        for k in vlist:
86            parms = {'property' : property_code.lower(), 'value' : k}
87            f.write(Unicode_CC_struct.substitute(parms))
88            f.write(Unicode_CC_header.substitute(parms))
89            template_var = Unicode_template_var.substitute(parms)
90            f.write(generateCharClassDefs(defaultIfRangeList, {k: uset_to_range_list(value_map[k])},  template_var))
91        f.write(Unicode_dummy_main)
92        f.close()
93
94    def generate_ScriptExtensions_pablo(self):
95        filename_root = 'ScriptExtensions'
96        property_code = 'scx'
97        vlist = self.property_value_list['sc']
98        (prop_values, value_map) = parse_ScriptExtensions_txt(vlist, self.property_value_lookup_map['sc'])
99        basename = os.path.basename(filename_root)
100        f = open(basename + ".pablo", "wb")
101        for k in vlist:
102            parms = {'property' : 'scx', 'value' : k}
103            f.write(Unicode_CC_struct.substitute(parms))
104            f.write(Unicode_CC_header.substitute(parms))
105            template_var = Unicode_template_var.substitute(parms)
106            f.write(generateCharClassDefs(defaultIfRangeList, {k: uset_to_range_list(value_map[k])},  template_var))
107        f.write(Unicode_dummy_main)
108        f.close()
109
110    def generate_binary_property_template(self, filename_root):
111        (props, prop_map) = parse_UCD_codepoint_name_map(filename_root + '.txt', self.property_lookup_map)
112        lprops = [p.lower() for p in props]
113        f = cformat.open_header_file_for_write(os.path.basename("ucd_precompiled_%s" % filename_root.lower()), 'generate_UCD_property_functions.py')
114        cformat.write_imports(f, ['"pablo/pablo_compiler.h"'])
115        f.write(Unicode_pablo_template)
116        for p in lprops:
117            f.write(Unicode_property_fn_template.substitute({'property': p, 'value': 'Y'}))
118        install_stmt = "  p.InstallExternalFunction(\"__get_%s_Y\", (void *) & __get_%s_Y);\n"
119        install_list = [install_stmt % (p, p) for p in lprops]
120        f.write(install_fn % (filename_root, "".join(install_list)))
121        cformat.close_header_file(f)
122       
123    def generate_binary_properties_file(self, filename_root):
124        (props, prop_map) = parse_UCD_codepoint_name_map(filename_root + '.txt', self.property_lookup_map)
125        basename = os.path.basename(filename_root)
126        f = open(basename + ".pablo", "wb")
127        for p in props:
128            parms = {'property' : p.lower(), 'value' : 'Y'}
129            f.write(Unicode_CC_struct.substitute(parms))
130            f.write(Unicode_CC_header.substitute(parms))
131            template_var = Unicode_template_var.substitute(parms)
132            f.write(generateCharClassDefs(defaultIfRangeList, {'Y': uset_to_range_list(prop_map[p])},  template_var))
133        f.write(Unicode_dummy_main)
134        f.close()
135
136
137def UCD_main():
138    ucd = UCD_generator()
139
140    # First parse all property names and their aliases
141    ucd.load_property_name_info()
142    #
143    # Next parse all property value names and their aliases.  Generate the data.
144    ucd.load_property_value_info()
145    #
146    # The Block property
147    ucd.generate_enumerated_property_template('Blocks', 'blk')
148    ucd.generate_enumerated_property_file('Blocks', 'blk')
149    #
150    # Scripts
151    ucd.generate_enumerated_property_template('Scripts', 'sc')
152    ucd.generate_enumerated_property_file('Scripts', 'sc')
153    #
154    # Script Extensions
155    ucd.generate_enumerated_property_template('ScriptExtensions', 'scx')
156    ucd.generate_ScriptExtensions_pablo()
157    #
158    # General Category
159    ucd.generate_enumerated_property_template('GeneralCategory', 'gc')
160    ucd.generate_enumerated_property_file('extracted/DerivedGeneralCategory', 'gc')
161    #
162    # Core Properties
163    ucd.generate_binary_property_template('DerivedCoreProperties')
164    ucd.generate_binary_properties_file('DerivedCoreProperties')
165    #
166    ucd.generate_binary_property_template('PropList')
167    ucd.generate_binary_properties_file('PropList')
168
169if __name__ == "__main__":
170    set_UCD_dir('UCD/7.0.0')
171    UCD_main()
Note: See TracBrowser for help on using the repository browser.