Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(314)

Side by Side Diff: Source/build/scripts/make_css_value_keywords.py

Issue 1302273005: Minor clean-up to getValueName (i.e. CSSValueID -> string conversion) (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 import os.path 3 import os.path
4 import re 4 import re
5 import subprocess 5 import subprocess
6 import sys 6 import sys
7 7
8 from in_file import InFile 8 from in_file import InFile
9 from name_utilities import upper_first_letter 9 from name_utilities import upper_first_letter
10 import in_generator 10 import in_generator
(...skipping 11 matching lines...) Expand all
22 22
23 namespace blink { 23 namespace blink {
24 24
25 enum CSSValueID { 25 enum CSSValueID {
26 %(value_keyword_enums)s 26 %(value_keyword_enums)s
27 }; 27 };
28 28
29 const int numCSSValueKeywords = %(value_keywords_count)d; 29 const int numCSSValueKeywords = %(value_keywords_count)d;
30 const size_t maxCSSValueKeywordLength = %(max_value_keyword_length)d; 30 const size_t maxCSSValueKeywordLength = %(max_value_keyword_length)d;
31 31
32 const char* getValueName(unsigned short id); 32 const char* getValueName(CSSValueID);
33 bool isValueAllowedInMode(unsigned short id, CSSParserMode mode); 33 bool isValueAllowedInMode(unsigned short id, CSSParserMode mode);
34 34
35 } // namespace blink 35 } // namespace blink
36 36
37 #endif // %(class_name)s_h 37 #endif // %(class_name)s_h
38 """ 38 """
39 39
40 GPERF_TEMPLATE = """ 40 GPERF_TEMPLATE = """
41 %%{ 41 %%{
42 %(license)s 42 %(license)s
43 43
44 #include "config.h" 44 #include "config.h"
45 #include "%(class_name)s.h" 45 #include "%(class_name)s.h"
46 #include "core/css/HashTools.h" 46 #include "core/css/HashTools.h"
47 #include <string.h> 47 #include <string.h>
48 48
49 namespace blink { 49 namespace blink {
50 static const char valueListStringPool[] = { 50 static const char valueListStringPool[] = {
51 "\\0"
52 %(value_keyword_strings)s 51 %(value_keyword_strings)s
53 }; 52 };
54 53
55 static const unsigned short valueListStringOffsets[] = { 54 static const unsigned short valueListStringOffsets[] = {
56 %(value_keyword_offsets)s 55 %(value_keyword_offsets)s
57 }; 56 };
58 57
59 %%} 58 %%}
60 %%struct-type 59 %%struct-type
61 struct Value; 60 struct Value;
62 %%omit-struct-type 61 %%omit-struct-type
63 %%language=C++ 62 %%language=C++
64 %%readonly-tables 63 %%readonly-tables
65 %%compare-strncmp 64 %%compare-strncmp
66 %%define class-name %(class_name)sHash 65 %%define class-name %(class_name)sHash
67 %%define lookup-function-name findValueImpl 66 %%define lookup-function-name findValueImpl
68 %%define hash-function-name value_hash_function 67 %%define hash-function-name value_hash_function
69 %%define slot-name nameOffset 68 %%define slot-name nameOffset
70 %%define word-array-name value_word_list 69 %%define word-array-name value_word_list
71 %%pic 70 %%pic
72 %%enum 71 %%enum
73 %%%% 72 %%%%
74 %(value_keyword_to_enum_map)s 73 %(value_keyword_to_enum_map)s
75 %%%% 74 %%%%
76 const Value* findValue(register const char* str, register unsigned int len) 75 const Value* findValue(register const char* str, register unsigned int len)
77 { 76 {
78 return CSSValueKeywordsHash::findValueImpl(str, len); 77 return CSSValueKeywordsHash::findValueImpl(str, len);
79 } 78 }
80 79
81 const char* getValueName(unsigned short id) 80 const char* getValueName(CSSValueID id)
82 { 81 {
83 if (id >= numCSSValueKeywords || id <= 0) 82 ASSERT(id > 0 && id < numCSSValueKeywords);
84 return 0; 83 return valueListStringPool + valueListStringOffsets[id - 1];
85 return valueListStringPool + valueListStringOffsets[id];
86 } 84 }
87 85
88 bool isValueAllowedInMode(unsigned short id, CSSParserMode mode) 86 bool isValueAllowedInMode(unsigned short id, CSSParserMode mode)
89 { 87 {
90 switch (id) { 88 switch (id) {
91 %(ua_sheet_mode_values_keywords)s 89 %(ua_sheet_mode_values_keywords)s
92 return isUASheetBehavior(mode); 90 return isUASheetBehavior(mode);
93 %(quirks_mode_or_ua_sheet_mode_values_keywords)s 91 %(quirks_mode_or_ua_sheet_mode_values_keywords)s
94 return isUASheetBehavior(mode) || isQuirksModeBehavior(mode); 92 return isUASheetBehavior(mode) || isQuirksModeBehavior(mode);
95 default: 93 default:
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 'class_name': self.class_name, 139 'class_name': self.class_name,
142 'value_keyword_enums': "\n".join(enum_enties), 140 'value_keyword_enums': "\n".join(enum_enties),
143 'value_keywords_count': len(enum_enties), 141 'value_keywords_count': len(enum_enties),
144 'max_value_keyword_length': reduce(max, map(len, map(lambda property : property['name'], self._value_keywords))), 142 'max_value_keyword_length': reduce(max, map(len, map(lambda property : property['name'], self._value_keywords))),
145 } 143 }
146 144
147 def _value_keywords_with_mode(self, mode): 145 def _value_keywords_with_mode(self, mode):
148 return filter(lambda property: property['mode'] == mode, self._value_key words) 146 return filter(lambda property: property['mode'] == mode, self._value_key words)
149 147
150 def generate_implementation(self): 148 def generate_implementation(self):
151 keyword_offsets = [0] 149 keyword_offsets = []
152 current_offset = 1 150 current_offset = 0
153 for keyword in self._value_keywords: 151 for keyword in self._value_keywords:
154 keyword_offsets.append(current_offset) 152 keyword_offsets.append(current_offset)
155 current_offset += len(keyword["name"]) + 1 153 current_offset += len(keyword["name"]) + 1
156 154
157 gperf_input = GPERF_TEMPLATE % { 155 gperf_input = GPERF_TEMPLATE % {
158 'license': license.license_for_generated_cpp(), 156 'license': license.license_for_generated_cpp(),
159 'class_name': self.class_name, 157 'class_name': self.class_name,
160 'value_keyword_strings': '\n'.join(map(lambda property: ' "%(name )s\\0"' % property, self._value_keywords)), 158 'value_keyword_strings': '\n'.join(map(lambda property: ' "%(name )s\\0"' % property, self._value_keywords)),
161 'value_keyword_offsets': '\n'.join(map(lambda offset: ' %d,' % offs et, keyword_offsets)), 159 'value_keyword_offsets': '\n'.join(map(lambda offset: ' %d,' % offs et, keyword_offsets)),
162 'value_keyword_to_enum_map': '\n'.join('%(lower_name)s, %(enum_name) s' % keyword for keyword in self._value_keywords), 160 'value_keyword_to_enum_map': '\n'.join('%(lower_name)s, %(enum_name) s' % keyword for keyword in self._value_keywords),
163 'ua_sheet_mode_values_keywords': '\n '.join(map(self._case_va lue_keyword, self._value_keywords_with_mode('UASheet'))), 161 'ua_sheet_mode_values_keywords': '\n '.join(map(self._case_va lue_keyword, self._value_keywords_with_mode('UASheet'))),
164 'quirks_mode_or_ua_sheet_mode_values_keywords': '\n '.join(map(se lf._case_value_keyword, self._value_keywords_with_mode('QuirksOrUASheet'))), 162 'quirks_mode_or_ua_sheet_mode_values_keywords': '\n '.join(map(se lf._case_value_keyword, self._value_keywords_with_mode('QuirksOrUASheet'))),
165 } 163 }
166 # FIXME: If we could depend on Python 2.7, we would use subprocess.check _output 164 # FIXME: If we could depend on Python 2.7, we would use subprocess.check _output
167 gperf_args = [self.gperf_path, '--key-positions=*', '-P', '-n'] 165 gperf_args = [self.gperf_path, '--key-positions=*', '-P', '-n']
168 gperf_args.extend(['-m', '50']) # Pick best of 50 attempts. 166 gperf_args.extend(['-m', '50']) # Pick best of 50 attempts.
169 gperf_args.append('-D') # Allow duplicate hashes -> More compact code. 167 gperf_args.append('-D') # Allow duplicate hashes -> More compact code.
170 gperf = subprocess.Popen(gperf_args, stdin=subprocess.PIPE, stdout=subpr ocess.PIPE, universal_newlines=True) 168 gperf = subprocess.Popen(gperf_args, stdin=subprocess.PIPE, stdout=subpr ocess.PIPE, universal_newlines=True)
171 return gperf.communicate(gperf_input)[0] 169 return gperf.communicate(gperf_input)[0]
172 170
173 171
174 if __name__ == "__main__": 172 if __name__ == "__main__":
175 in_generator.Maker(CSSValueKeywordsWriter).main(sys.argv) 173 in_generator.Maker(CSSValueKeywordsWriter).main(sys.argv)
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698