OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (C) 2013 Google Inc. All rights reserved. | 2 # Copyright (C) 2013 Google Inc. All rights reserved. |
3 # | 3 # |
4 # Redistribution and use in source and binary forms, with or without | 4 # Redistribution and use in source and binary forms, with or without |
5 # modification, are permitted provided that the following conditions are | 5 # modification, are permitted provided that the following conditions are |
6 # met: | 6 # met: |
7 # | 7 # |
8 # * Redistributions of source code must retain the above copyright | 8 # * Redistributions of source code must retain the above copyright |
9 # notice, this list of conditions and the following disclaimer. | 9 # notice, this list of conditions and the following disclaimer. |
10 # * Redistributions in binary form must reproduce the above | 10 # * Redistributions in binary form must reproduce the above |
11 # copyright notice, this list of conditions and the following disclaimer | 11 # copyright notice, this list of conditions and the following disclaimer |
12 # in the documentation and/or other materials provided with the | 12 # in the documentation and/or other materials provided with the |
13 # distribution. | 13 # distribution. |
14 # * Neither the name of Google Inc. nor the names of its | 14 # * Neither the name of Google Inc. nor the names of its |
15 # contributors may be used to endorse or promote products derived from | 15 # contributors may be used to endorse or promote products derived from |
16 # this software without specific prior written permission. | 16 # this software without specific prior written permission. |
17 # | 17 # |
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | 29 |
30 from collections import OrderedDict | |
30 from itertools import groupby, islice | 31 from itertools import groupby, islice |
31 import sys | 32 import sys |
32 | 33 |
33 import in_generator | 34 import in_generator |
34 import template_expander | 35 import template_expander |
35 | 36 |
36 PARAMETER_NAME = 'data' | 37 PARAMETER_NAME = 'data' |
37 | 38 |
38 | 39 |
39 def _trie(tags, index): | 40 def _trie(str_to_return_value_dict, index): |
40 """Make a trie from list of tags, starting at index. | 41 """Make a trie from list of tags, starting at index. |
41 | 42 |
42 Resulting trie is partly space-optimized (semi-radix tree): once have only | 43 Resulting trie is partly space-optimized (semi-radix tree): once have only |
43 one string left, compact the entire branch to one leaf node. | 44 one string left, compact the entire branch to one leaf node. |
44 However, does not compact branch nodes with a single child. (FIXME) | 45 However, does not compact branch nodes with a single child. (FIXME) |
45 | 46 |
46 Returns: | 47 Returns: |
47 (char, subtrie, tag, conditions): (char, trie, str, list) | 48 dict(char, subtrie, string, value, conditions): |
Timothy Loh
2016/05/03 07:40:25
Can we return something structured like (e.g.)
{
meade_UTC10
2016/05/04 07:24:54
Done.
| |
48 code generation differs between branch nodes and leaf nodes, | 49 char, list, string, string, string. Code generation differs between |
49 hence need different data for each. | 50 branch nodes and leaf nodes, hence need different data for each. |
50 | 51 |
51 Arguments: | 52 Arguments: |
52 tags: sorted list | 53 str_to_return_value_dict: A ordered dictionary of strings to the return |
53 (sorted needed by groupby, list needed by len) | 54 value strings. Ordered by length, then alphabetically. |
54 index: index at which to branch | 55 index: index at which to branch |
55 (assumes prior to this index strings have a common prefix) | |
56 """ | 56 """ |
57 def trie_node(char, subtags_iter): | 57 def trie_node(char, subtags_iter): |
58 # Pass in |char| so we can include in same tuple without unpacking | 58 # Pass in |char| so we can include in same tuple without unpacking |
59 subtags = list(subtags_iter) # need list for len | 59 subtags = list(subtags_iter) # need list for len |
60 if len(subtags) == 1: # terminal node, no subtrie | 60 if len(subtags) == 1: # terminal node, no subtrie |
61 subtrie = None | |
62 tag = subtags[0] | 61 tag = subtags[0] |
63 conditions = _conditions(tag, index + 1) | 62 return { |
63 'char': char, | |
64 'subtrie': None, | |
65 'string': tag, | |
66 'value': subtags_iter[tag], | |
67 'conditions': _conditions(tag, index + 1) | |
68 } | |
64 else: | 69 else: |
65 subtrie = _trie(subtags, index + 1) | 70 return { |
66 tag = None | 71 'char': char, |
67 conditions = None | 72 'subtrie': _trie(subtags_iter, index + 1), |
68 return char, subtrie, tag, conditions | 73 'string': None, |
74 'value': None, | |
75 'conditions': None | |
76 } | |
69 | 77 |
70 # Group by char at index | 78 # Group by char at index |
71 def char_at_index(tag): | 79 def char_at_index(string): |
72 return tag[index].lower() | 80 return string[index].lower() |
73 | 81 |
74 char_subtags = ((k, g) for k, g in groupby(tags, char_at_index)) | 82 char_subtags = [] |
83 for k, g in groupby(str_to_return_value_dict, char_at_index): | |
Timothy Loh
2016/05/03 07:40:25
same comment as other place about groupby/OrderedD
meade_UTC10
2016/05/04 07:24:54
Done.
| |
84 sub_dict = OrderedDict( | |
85 [(inner_key, str_to_return_value_dict[inner_key]) for inner_key in g ]) | |
86 char_subtags.append((k, sub_dict)) | |
75 | 87 |
76 # FIXME: if all subtags have a common prefix, merge with child | 88 return [trie_node(char, subtags) for char, subtags in char_subtags] |
77 # and skip the switch in the generated code | |
78 | |
79 return (trie_node(char, subtags) for char, subtags in char_subtags) | |
80 | 89 |
81 | 90 |
82 def _conditions(tag, index): | 91 def _conditions(tag, index): |
Timothy Loh
2016/05/03 07:40:25
as discussed, let's move all of the logic for this
meade_UTC10
2016/05/04 07:24:54
Done.
| |
83 # boolean conditions to check suffix; corresponds to compacting branch | 92 # boolean conditions to check suffix; corresponds to compacting branch |
84 # with a single leaf | 93 # with a single leaf |
85 return ["%s[%d] == '%c'" % (PARAMETER_NAME, i, c.lower()) | 94 return ["%s[%d] == '%c'" % (PARAMETER_NAME, i, c.lower()) |
86 for i, c in islice(enumerate(tag), index, None)] | 95 for i, c in islice(enumerate(tag), index, None)] |
87 | 96 |
88 | 97 |
98 def _trie_list(str_to_return_value_dict): | |
99 ordered_dict = OrderedDict( | |
Timothy Loh
2016/05/03 07:40:25
OrderedDict+groupby seems weird here, probably bet
meade_UTC10
2016/05/04 07:24:54
Done.
| |
100 sorted(str_to_return_value_dict.items(), key=lambda pair: (len(pair[0]), pair[0]))) | |
101 | |
102 dicts = [] | |
103 for k, g in groupby(ordered_dict, len): | |
104 sub_dict = OrderedDict( | |
105 [(inner_key, str_to_return_value_dict[inner_key]) for inner_key in g ]) | |
106 dicts.append((k, sub_dict)) | |
107 | |
108 return [(length, _trie(d, 0)) for length, d in dicts] | |
109 | |
110 | |
89 class ElementLookupTrieWriter(in_generator.Writer): | 111 class ElementLookupTrieWriter(in_generator.Writer): |
90 # FIXME: Inherit all these from somewhere. | 112 # FIXME: Inherit all these from somewhere. |
91 defaults = { | 113 defaults = { |
92 'JSInterfaceName': None, | 114 'JSInterfaceName': None, |
93 'constructorNeedsCreatedByParser': None, | 115 'constructorNeedsCreatedByParser': None, |
94 'constructorNeedsFormElement': None, | 116 'constructorNeedsFormElement': None, |
95 'interfaceName': None, | 117 'interfaceName': None, |
96 'noConstructor': None, | 118 'noConstructor': None, |
97 'runtimeEnabled': None, | 119 'runtimeEnabled': None, |
98 } | 120 } |
99 default_parameters = { | 121 default_parameters = { |
100 'attrsNullNamespace': None, | 122 'attrsNullNamespace': None, |
101 'export': '', | 123 'export': '', |
102 'fallbackInterfaceName': '', | 124 'fallbackInterfaceName': '', |
103 'fallbackJSInterfaceName': '', | 125 'fallbackJSInterfaceName': '', |
104 'namespace': '', | 126 'namespace': '', |
105 'namespacePrefix': '', | 127 'namespacePrefix': '', |
106 'namespaceURI': '', | 128 'namespaceURI': '', |
107 } | 129 } |
108 | 130 |
109 def __init__(self, in_file_paths): | 131 def __init__(self, in_file_paths): |
110 super(ElementLookupTrieWriter, self).__init__(in_file_paths) | 132 super(ElementLookupTrieWriter, self).__init__(in_file_paths) |
111 self._tags = [entry['name'] for entry in self.in_file.name_dictionaries] | 133 self._tags = {} |
134 for entry in self.in_file.name_dictionaries: | |
135 self._tags[entry['name']] = entry['name'] | |
112 self._namespace = self.in_file.parameters['namespace'].strip('"') | 136 self._namespace = self.in_file.parameters['namespace'].strip('"') |
113 self._outputs = { | 137 self._outputs = { |
114 (self._namespace + 'ElementLookupTrie.h'): self.generate_header, | 138 (self._namespace + 'ElementLookupTrie.h'): self.generate_header, |
115 (self._namespace + 'ElementLookupTrie.cpp'): self.generate_implement ation, | 139 (self._namespace + 'ElementLookupTrie.cpp'): self.generate_implement ation, |
116 } | 140 } |
117 | 141 |
118 @template_expander.use_jinja('ElementLookupTrie.h.tmpl') | 142 @template_expander.use_jinja('ElementLookupTrie.h.tmpl') |
119 def generate_header(self): | 143 def generate_header(self): |
120 return { | 144 return { |
121 'namespace': self._namespace, | 145 'namespace': self._namespace, |
122 } | 146 } |
123 | 147 |
124 @template_expander.use_jinja('ElementLookupTrie.cpp.tmpl') | 148 @template_expander.use_jinja('ElementLookupTrie.cpp.tmpl') |
125 def generate_implementation(self): | 149 def generate_implementation(self): |
126 # First sort, so groupby works | |
127 self._tags.sort(key=lambda tag: (len(tag), tag)) | |
128 # Group tags by length | |
129 length_tags = ((k, g) for k, g in groupby(self._tags, len)) | |
130 | |
131 return { | 150 return { |
132 'namespace': self._namespace, | 151 'namespace': self._namespace, |
133 'length_tries': ((length, _trie(tags, 0)) | 152 'length_tries': _trie_list(self._tags) |
134 for length, tags in length_tags), | |
135 } | 153 } |
136 | 154 |
137 | 155 |
138 if __name__ == '__main__': | 156 if __name__ == '__main__': |
139 in_generator.Maker(ElementLookupTrieWriter).main(sys.argv) | 157 in_generator.Maker(ElementLookupTrieWriter).main(sys.argv) |
OLD | NEW |