OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 import os.path | |
4 import re | |
5 import subprocess | 3 import subprocess |
6 import sys | 4 import sys |
7 | 5 |
8 from in_file import InFile | 6 import css_properties |
9 import in_generator | 7 import in_generator |
10 import license | 8 import license |
11 | 9 |
12 | 10 |
13 HEADER_TEMPLATE = """ | 11 HEADER_TEMPLATE = """ |
14 %(license)s | 12 %(license)s |
15 | 13 |
16 #ifndef %(class_name)s_h | 14 #ifndef %(class_name)s_h |
17 #define %(class_name)s_h | 15 #define %(class_name)s_h |
18 | 16 |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 return true; | 169 return true; |
172 default: | 170 default: |
173 return false; | 171 return false; |
174 } | 172 } |
175 } | 173 } |
176 | 174 |
177 } // namespace WebCore | 175 } // namespace WebCore |
178 """ | 176 """ |
179 | 177 |
180 | 178 |
181 class CSSPropertiesWriter(in_generator.Writer): | 179 class CSSPropertyNamesWriter(css_properties.CSSProperties): |
182 class_name = "CSSPropertyNames" | 180 class_name = "CSSPropertyNames" |
183 defaults = { | |
184 'alias_for': None, | |
185 'is_internal': False, | |
186 } | |
187 | 181 |
188 def __init__(self, file_paths): | 182 def __init__(self, in_file_path): |
189 in_generator.Writer.__init__(self, file_paths) | 183 super(CSSPropertyNamesWriter, self).__init__(in_file_path) |
190 self._outputs = {(self.class_name + ".h"): self.generate_header, | 184 self._outputs = {(self.class_name + ".h"): self.generate_header, |
191 (self.class_name + ".cpp"): self.generate_implementatio
n, | 185 (self.class_name + ".cpp"): self.generate_implementatio
n, |
192 } | 186 } |
193 | 187 |
194 self._aliases = filter(lambda property: property['alias_for'], self.in_f
ile.name_dictionaries) | |
195 for offset, property in enumerate(self._aliases): | |
196 # Aliases use the enum_name that they are an alias for. | |
197 property['enum_name'] = self._enum_name_from_property_name(property[
'alias_for']) | |
198 # Aliases do not get an enum_value. | |
199 | |
200 self._properties = filter(lambda property: not property['alias_for'], se
lf.in_file.name_dictionaries) | |
201 if len(self._properties) > 1024: | |
202 print "ERROR : There is more than 1024 CSS Properties, you need to u
pdate CSSProperty.h/StylePropertyMetadata m_propertyID accordingly." | |
203 exit(1) | |
204 self._first_property_id = 1 # We start after CSSPropertyInvalid. | |
205 property_id = self._first_property_id | |
206 for offset, property in enumerate(self._properties): | |
207 property['enum_name'] = self._enum_name_from_property_name(property[
'name']) | |
208 property['enum_value'] = self._first_property_id + offset | |
209 if property['name'].startswith('-internal-'): | |
210 property['is_internal'] = True | |
211 | |
212 def _enum_name_from_property_name(self, property_name): | |
213 return "CSSProperty" + re.sub(r'(^[^-])|-(.)', lambda match: (match.grou
p(1) or match.group(2)).upper(), property_name) | |
214 | |
215 def _enum_declaration(self, property): | 188 def _enum_declaration(self, property): |
216 return " %(enum_name)s = %(enum_value)s," % property | 189 return " %(property_id)s = %(enum_value)s," % property |
217 | 190 |
218 def generate_header(self): | 191 def generate_header(self): |
219 return HEADER_TEMPLATE % { | 192 return HEADER_TEMPLATE % { |
220 'license': license.license_for_generated_cpp(), | 193 'license': license.license_for_generated_cpp(), |
221 'class_name': self.class_name, | 194 'class_name': self.class_name, |
222 'property_enums': "\n".join(map(self._enum_declaration, self._proper
ties)), | 195 'property_enums': "\n".join(map(self._enum_declaration, self._proper
ties_list)), |
223 'first_property_id': self._first_property_id, | 196 'first_property_id': self._first_enum_value, |
224 'properties_count': len(self._properties), | 197 'properties_count': len(self._properties), |
225 'last_property_id': self._first_property_id + len(self._properties)
- 1, | 198 'last_property_id': self._first_enum_value + len(self._properties) -
1, |
226 'max_name_length': reduce(max, map(len, map(lambda property: propert
y['name'], self._properties))), | 199 'max_name_length': max(map(len, self._properties)), |
227 } | 200 } |
228 | 201 |
229 def _case_properties(self, property): | |
230 return "case %(enum_name)s:" % property | |
231 | |
232 def generate_implementation(self): | 202 def generate_implementation(self): |
233 property_offsets = [] | 203 property_offsets = [] |
234 current_offset = 0 | 204 current_offset = 0 |
235 for property in self._properties: | 205 for property in self._properties_list: |
236 property_offsets.append(current_offset) | 206 property_offsets.append(current_offset) |
237 current_offset += len(property["name"]) + 1 | 207 current_offset += len(property["name"]) + 1 |
238 | 208 |
| 209 css_name_and_enum_pairs = [(property['name'], property_id) for property_
id, property in self._properties.items()] |
| 210 for name, aliased_name in self._aliases.items(): |
| 211 css_name_and_enum_pairs.append((name, css_properties.css_name_to_enu
m(aliased_name))) |
| 212 |
239 gperf_input = GPERF_TEMPLATE % { | 213 gperf_input = GPERF_TEMPLATE % { |
240 'license': license.license_for_generated_cpp(), | 214 'license': license.license_for_generated_cpp(), |
241 'class_name': self.class_name, | 215 'class_name': self.class_name, |
242 'property_name_strings': '\n'.join(map(lambda property: ' "%(name
)s\\0"' % property, self._properties)), | 216 'property_name_strings': '\n'.join(map(lambda property: ' "%(name
)s\\0"' % property, self._properties_list)), |
243 'property_name_offsets': '\n'.join(map(lambda offset: ' %d,' % of
fset, property_offsets)), | 217 'property_name_offsets': '\n'.join(map(lambda offset: ' %d,' % of
fset, property_offsets)), |
244 'property_to_enum_map': '\n'.join(map(lambda property: '%(name)s, %(
enum_name)s' % property, self._properties + self._aliases)), | 218 'property_to_enum_map': '\n'.join(map(lambda property: '%s, %s' % pr
operty, css_name_and_enum_pairs)), |
245 'internal_properties': '\n'.join(map(self._case_properties, filter(l
ambda property: property['is_internal'], self._properties))), | 219 'internal_properties': '\n'.join("case %s:" % property_id for proper
ty_id, property in self._properties.items() if property['is_internal']), |
246 } | 220 } |
247 # FIXME: If we could depend on Python 2.7, we would use subprocess.check
_output | 221 # FIXME: If we could depend on Python 2.7, we would use subprocess.check
_output |
248 gperf_args = [self.gperf_path, '--key-positions=*', '-P', '-n'] | 222 gperf_args = [self.gperf_path, '--key-positions=*', '-P', '-n'] |
249 gperf_args.extend(['-m', '50']) # Pick best of 50 attempts. | 223 gperf_args.extend(['-m', '50']) # Pick best of 50 attempts. |
250 gperf_args.append('-D') # Allow duplicate hashes -> More compact code. | 224 gperf_args.append('-D') # Allow duplicate hashes -> More compact code. |
251 gperf = subprocess.Popen(gperf_args, stdin=subprocess.PIPE, stdout=subpr
ocess.PIPE, universal_newlines=True) | 225 gperf = subprocess.Popen(gperf_args, stdin=subprocess.PIPE, stdout=subpr
ocess.PIPE, universal_newlines=True) |
252 return gperf.communicate(gperf_input)[0] | 226 return gperf.communicate(gperf_input)[0] |
253 | 227 |
254 | 228 |
255 if __name__ == "__main__": | 229 if __name__ == "__main__": |
256 in_generator.Maker(CSSPropertiesWriter).main(sys.argv) | 230 in_generator.Maker(CSSPropertyNamesWriter).main(sys.argv) |
OLD | NEW |