1 | #!/usr/bin/env python3
|
---|
2 | #
|
---|
3 | # Copyright (C) 2014 Apple Inc. All rights reserved.
|
---|
4 | #
|
---|
5 | # Redistribution and use in source and binary forms, with or without
|
---|
6 | # modification, are permitted provided that the following conditions
|
---|
7 | # are met:
|
---|
8 | # 1. Redistributions of source code must retain the above copyright
|
---|
9 | # notice, this list of conditions and the following disclaimer.
|
---|
10 | # 2. Redistributions in binary form must reproduce the above copyright
|
---|
11 | # notice, this list of conditions and the following disclaimer in the
|
---|
12 | # documentation and/or other materials provided with the distribution.
|
---|
13 | #
|
---|
14 | # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
|
---|
15 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
---|
16 | # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
17 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
|
---|
18 | # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
---|
19 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
---|
20 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
---|
21 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
---|
22 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
---|
23 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
---|
24 | # THE POSSIBILITY OF SUCH DAMAGE.
|
---|
25 |
|
---|
26 | import os
|
---|
27 | import sys
|
---|
28 | import subprocess
|
---|
29 |
|
---|
30 |
|
---|
31 | def enumerablePseudoType(stringPseudoType):
|
---|
32 | output = ['CSSSelector::PseudoClass']
|
---|
33 |
|
---|
34 | if stringPseudoType.endswith('('):
|
---|
35 | stringPseudoType = stringPseudoType[:-1]
|
---|
36 |
|
---|
37 | internalPrefix = '-internal-'
|
---|
38 | if (stringPseudoType.startswith(internalPrefix)):
|
---|
39 | stringPseudoType = stringPseudoType[len(internalPrefix):]
|
---|
40 |
|
---|
41 | webkitPrefix = '-webkit-'
|
---|
42 | if (stringPseudoType.startswith(webkitPrefix)):
|
---|
43 | stringPseudoType = stringPseudoType[len(webkitPrefix):]
|
---|
44 |
|
---|
45 | khtmlPrefix = '-khtml-'
|
---|
46 | if (stringPseudoType.startswith(khtmlPrefix)):
|
---|
47 | stringPseudoType = stringPseudoType[len(khtmlPrefix):]
|
---|
48 |
|
---|
49 | substring_start = 0
|
---|
50 | next_dash_position = stringPseudoType.find('-')
|
---|
51 | while (next_dash_position != -1):
|
---|
52 | output.append(stringPseudoType[substring_start].upper())
|
---|
53 | output.append(stringPseudoType[substring_start + 1:next_dash_position])
|
---|
54 | substring_start = next_dash_position + 1
|
---|
55 | next_dash_position = stringPseudoType.find('-', substring_start)
|
---|
56 |
|
---|
57 | output.append(stringPseudoType[substring_start].upper())
|
---|
58 | output.append(stringPseudoType[substring_start + 1:])
|
---|
59 | return ''.join(output)
|
---|
60 |
|
---|
61 |
|
---|
62 | def expand_ifdef_condition(condition):
|
---|
63 | return condition.replace('(', '_').replace(')', '')
|
---|
64 |
|
---|
65 | output_file = open('SelectorPseudoClassAndCompatibilityElementMap.gperf', 'w')
|
---|
66 |
|
---|
67 | output_file.write("""
|
---|
68 | %{
|
---|
69 | /*
|
---|
70 | * Copyright (C) 2014-2016 Apple Inc. All rights reserved.
|
---|
71 | *
|
---|
72 | * Redistribution and use in source and binary forms, with or without
|
---|
73 | * modification, are permitted provided that the following conditions
|
---|
74 | * are met:
|
---|
75 | * 1. Redistributions of source code must retain the above copyright
|
---|
76 | * notice, this list of conditions and the following disclaimer.
|
---|
77 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
78 | * notice, this list of conditions and the following disclaimer in the
|
---|
79 | * documentation and/or other materials provided with the distribution.
|
---|
80 | *
|
---|
81 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
|
---|
82 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
---|
83 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
84 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
|
---|
85 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
---|
86 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
---|
87 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
---|
88 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
---|
89 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
---|
90 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
---|
91 | * THE POSSIBILITY OF SUCH DAMAGE.
|
---|
92 | */
|
---|
93 |
|
---|
94 | // This file is automatically generated from SelectorPseudoTypeMap.in by makeprop, do not edit by hand.
|
---|
95 |
|
---|
96 | #include "config.h"
|
---|
97 | #include "SelectorPseudoTypeMap.h"
|
---|
98 |
|
---|
99 | #include "CSSParserSelector.h"
|
---|
100 |
|
---|
101 | IGNORE_WARNINGS_BEGIN("implicit-fallthrough")
|
---|
102 |
|
---|
103 | // Older versions of gperf like to use the `register` keyword.
|
---|
104 | #define register
|
---|
105 |
|
---|
106 | namespace WebCore {
|
---|
107 |
|
---|
108 | struct SelectorPseudoClassOrCompatibilityPseudoElementEntry {
|
---|
109 | const char* name;
|
---|
110 | PseudoClassOrCompatibilityPseudoElement pseudoTypes;
|
---|
111 | };
|
---|
112 |
|
---|
113 | %}
|
---|
114 | %struct-type
|
---|
115 | %define initializer-suffix ,{CSSSelector::PseudoClassUnknown,CSSSelector::PseudoElementUnknown}
|
---|
116 | %define class-name SelectorPseudoClassAndCompatibilityElementMapHash
|
---|
117 | %omit-struct-type
|
---|
118 | %language=C++
|
---|
119 | %readonly-tables
|
---|
120 | %global-table
|
---|
121 | %ignore-case
|
---|
122 | %compare-strncmp
|
---|
123 | %enum
|
---|
124 |
|
---|
125 | struct SelectorPseudoClassOrCompatibilityPseudoElementEntry;
|
---|
126 |
|
---|
127 | %%
|
---|
128 | """)
|
---|
129 |
|
---|
130 | webcore_defines = [i.strip() for i in sys.argv[-1].split(' ')]
|
---|
131 |
|
---|
132 | longest_keyword = 0
|
---|
133 |
|
---|
134 | ignore_until_endif = False
|
---|
135 | input_file = open(sys.argv[1], 'r')
|
---|
136 | for line in input_file:
|
---|
137 | line = line.strip()
|
---|
138 | if not line:
|
---|
139 | continue
|
---|
140 |
|
---|
141 | if line.startswith('#if '):
|
---|
142 | condition = line[4:].strip()
|
---|
143 | if expand_ifdef_condition(condition) not in webcore_defines:
|
---|
144 | ignore_until_endif = True
|
---|
145 | continue
|
---|
146 |
|
---|
147 | if line.startswith('#endif'):
|
---|
148 | ignore_until_endif = False
|
---|
149 | continue
|
---|
150 |
|
---|
151 | if ignore_until_endif:
|
---|
152 | continue
|
---|
153 |
|
---|
154 | keyword_definition = line.split(',')
|
---|
155 | if len(keyword_definition) == 1:
|
---|
156 | keyword = keyword_definition[0].strip()
|
---|
157 | output_file.write('"%s", {%s, CSSSelector::PseudoElementUnknown}\n' % (keyword, enumerablePseudoType(keyword)))
|
---|
158 | else:
|
---|
159 | output_file.write('"%s", {CSSSelector::%s, CSSSelector::%s}\n' % (keyword_definition[0].strip(), keyword_definition[1].strip(), keyword_definition[2].strip()))
|
---|
160 |
|
---|
161 | longest_keyword = max(longest_keyword, len(keyword))
|
---|
162 |
|
---|
163 | output_file.write("""%%
|
---|
164 |
|
---|
165 | static inline const SelectorPseudoClassOrCompatibilityPseudoElementEntry* parsePseudoClassAndCompatibilityElementString(const LChar* characters, unsigned length)
|
---|
166 | {
|
---|
167 | return SelectorPseudoClassAndCompatibilityElementMapHash::in_word_set(reinterpret_cast<const char*>(characters), length);
|
---|
168 | }""")
|
---|
169 |
|
---|
170 | output_file.write("""
|
---|
171 |
|
---|
172 | static inline const SelectorPseudoClassOrCompatibilityPseudoElementEntry* parsePseudoClassAndCompatibilityElementString(const UChar* characters, unsigned length)
|
---|
173 | {
|
---|
174 | const unsigned maxKeywordLength = %s;
|
---|
175 | LChar buffer[maxKeywordLength];
|
---|
176 | if (length > maxKeywordLength)
|
---|
177 | return nullptr;
|
---|
178 |
|
---|
179 | for (unsigned i = 0; i < length; ++i) {
|
---|
180 | UChar character = characters[i];
|
---|
181 | if (!isLatin1(character))
|
---|
182 | return nullptr;
|
---|
183 |
|
---|
184 | buffer[i] = static_cast<LChar>(character);
|
---|
185 | }
|
---|
186 | return parsePseudoClassAndCompatibilityElementString(buffer, length);
|
---|
187 | }
|
---|
188 | """ % longest_keyword)
|
---|
189 |
|
---|
190 | output_file.write("""
|
---|
191 | PseudoClassOrCompatibilityPseudoElement parsePseudoClassAndCompatibilityElementString(StringView pseudoTypeString)
|
---|
192 | {
|
---|
193 | const SelectorPseudoClassOrCompatibilityPseudoElementEntry* entry;
|
---|
194 | if (pseudoTypeString.is8Bit())
|
---|
195 | entry = parsePseudoClassAndCompatibilityElementString(pseudoTypeString.characters8(), pseudoTypeString.length());
|
---|
196 | else
|
---|
197 | entry = parsePseudoClassAndCompatibilityElementString(pseudoTypeString.characters16(), pseudoTypeString.length());
|
---|
198 |
|
---|
199 | if (entry)
|
---|
200 | return entry->pseudoTypes;
|
---|
201 | return { CSSSelector::PseudoClassUnknown, CSSSelector::PseudoElementUnknown };
|
---|
202 | }
|
---|
203 |
|
---|
204 | } // namespace WebCore
|
---|
205 |
|
---|
206 | IGNORE_WARNINGS_END
|
---|
207 |
|
---|
208 | """)
|
---|
209 | output_file.close()
|
---|
210 |
|
---|
211 | gperf_command = sys.argv[2]
|
---|
212 | if 'GPERF' in os.environ:
|
---|
213 | gperf_command = os.environ['GPERF']
|
---|
214 |
|
---|
215 | if subprocess.call([gperf_command, '--key-positions=*', '-m', '10', '-s', '2', 'SelectorPseudoClassAndCompatibilityElementMap.gperf', '--output-file=SelectorPseudoClassAndCompatibilityElementMap.cpp']) != 0:
|
---|
216 | print("Error when generating SelectorPseudoClassAndCompatibilityElementMap.cpp from SelectorPseudoClassAndCompatibilityElementMap.gperf :(")
|
---|
217 | sys.exit(gperf_return)
|
---|