blob: 2cf3c186705887ec41551e0893836a259fb98d30 [file] [log] [blame]
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +00001#!/usr/bin/env python
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
26import os
27import sys
utatane.tea@gmail.com89eb7d72018-05-04 05:28:12 +000028import subprocess
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +000029
30
31def enumerablePseudoType(stringPseudoType):
benjamin@webkit.orgf4ffe862014-04-20 20:43:35 +000032 output = ['CSSSelector::PseudoClass']
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +000033
34 if stringPseudoType.endswith('('):
35 stringPseudoType = stringPseudoType[:-1]
36
37 webkitPrefix = '-webkit-'
38 if (stringPseudoType.startswith(webkitPrefix)):
39 stringPseudoType = stringPseudoType[len(webkitPrefix):]
40
41 khtmlPrefix = '-khtml-'
42 if (stringPseudoType.startswith(khtmlPrefix)):
43 stringPseudoType = stringPseudoType[len(khtmlPrefix):]
44
45 substring_start = 0
46 next_dash_position = stringPseudoType.find('-')
47 while (next_dash_position != -1):
48 output.append(stringPseudoType[substring_start].upper())
49 output.append(stringPseudoType[substring_start + 1:next_dash_position])
50 substring_start = next_dash_position + 1
51 next_dash_position = stringPseudoType.find('-', substring_start)
52
53 output.append(stringPseudoType[substring_start].upper())
54 output.append(stringPseudoType[substring_start + 1:])
55 return ''.join(output)
56
57
58def expand_ifdef_condition(condition):
59 return condition.replace('(', '_').replace(')', '')
60
benjamin@webkit.orgf7245892014-03-29 03:08:59 +000061output_file = open('SelectorPseudoClassAndCompatibilityElementMap.gperf', 'w')
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +000062
63output_file.write("""
64%{
65/*
hyatt@apple.comb313af92016-09-08 21:09:04 +000066 * Copyright (C) 2014-2016 Apple Inc. All rights reserved.
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +000067 *
68 * Redistribution and use in source and binary forms, with or without
69 * modification, are permitted provided that the following conditions
70 * are met:
71 * 1. Redistributions of source code must retain the above copyright
72 * notice, this list of conditions and the following disclaimer.
73 * 2. Redistributions in binary form must reproduce the above copyright
74 * notice, this list of conditions and the following disclaimer in the
75 * documentation and/or other materials provided with the distribution.
76 *
77 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
78 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
79 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
80 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
81 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
82 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
83 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
84 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
85 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
86 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
87 * THE POSSIBILITY OF SUCH DAMAGE.
88 */
89
90// This file is automatically generated from SelectorPseudoTypeMap.in by makeprop, do not edit by hand.
91
92#include "config.h"
93#include "SelectorPseudoTypeMap.h"
94
hyatt@apple.com0a85f1b2016-12-10 20:38:50 +000095#include "CSSParserSelector.h"
darin@apple.com048cc3a2016-01-22 17:17:04 +000096
guijemont@igalia.com65db7e42018-09-12 15:09:16 +000097IGNORE_WARNINGS_BEGIN("implicit-fallthrough")
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +000098
zandobersek@gmail.com4cae92f2018-05-14 12:52:25 +000099// Older versions of gperf like to use the `register` keyword.
100#define register
101
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +0000102namespace WebCore {
103
benjamin@webkit.orgf7245892014-03-29 03:08:59 +0000104struct SelectorPseudoClassOrCompatibilityPseudoElementEntry {
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +0000105 const char* name;
benjamin@webkit.orgf7245892014-03-29 03:08:59 +0000106 PseudoClassOrCompatibilityPseudoElement pseudoTypes;
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +0000107};
108
109%}
110%struct-type
benjamin@webkit.orgf4ffe862014-04-20 20:43:35 +0000111%define initializer-suffix ,{CSSSelector::PseudoClassUnknown,CSSSelector::PseudoElementUnknown}
benjamin@webkit.orgf7245892014-03-29 03:08:59 +0000112%define class-name SelectorPseudoClassAndCompatibilityElementMapHash
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +0000113%omit-struct-type
114%language=C++
115%readonly-tables
116%global-table
117%ignore-case
118%compare-strncmp
119%enum
120
benjamin@webkit.orgf7245892014-03-29 03:08:59 +0000121struct SelectorPseudoClassOrCompatibilityPseudoElementEntry;
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +0000122
123%%
124""")
125
126webcore_defines = [i.strip() for i in sys.argv[-1].split(' ')]
127
128longest_keyword = 0
129
130ignore_until_endif = False
131input_file = open(sys.argv[1], 'r')
132for line in input_file:
benjamin@webkit.orgf7245892014-03-29 03:08:59 +0000133 line = line.strip()
134 if not line:
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +0000135 continue
136
benjamin@webkit.orgf7245892014-03-29 03:08:59 +0000137 if line.startswith('#if '):
138 condition = line[4:].strip()
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +0000139 if expand_ifdef_condition(condition) not in webcore_defines:
140 ignore_until_endif = True
141 continue
142
benjamin@webkit.orgf7245892014-03-29 03:08:59 +0000143 if line.startswith('#endif'):
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +0000144 ignore_until_endif = False
145 continue
146
147 if ignore_until_endif:
148 continue
149
benjamin@webkit.orgf7245892014-03-29 03:08:59 +0000150 keyword_definition = line.split(',')
151 if len(keyword_definition) == 1:
152 keyword = keyword_definition[0].strip()
benjamin@webkit.orgef42be42014-04-07 20:26:44 +0000153 output_file.write('"%s", {%s, CSSSelector::PseudoElementUnknown}\n' % (keyword, enumerablePseudoType(keyword)))
benjamin@webkit.orgf7245892014-03-29 03:08:59 +0000154 else:
155 output_file.write('"%s", {CSSSelector::%s, CSSSelector::%s}\n' % (keyword_definition[0].strip(), keyword_definition[1].strip(), keyword_definition[2].strip()))
156
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +0000157 longest_keyword = max(longest_keyword, len(keyword))
158
159output_file.write("""%%
160
benjamin@webkit.orgf7245892014-03-29 03:08:59 +0000161static inline const SelectorPseudoClassOrCompatibilityPseudoElementEntry* parsePseudoClassAndCompatibilityElementString(const LChar* characters, unsigned length)
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +0000162{
benjamin@webkit.orgf7245892014-03-29 03:08:59 +0000163 return SelectorPseudoClassAndCompatibilityElementMapHash::in_word_set(reinterpret_cast<const char*>(characters), length);
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +0000164}""")
165
166output_file.write("""
167
benjamin@webkit.orgf7245892014-03-29 03:08:59 +0000168static inline const SelectorPseudoClassOrCompatibilityPseudoElementEntry* parsePseudoClassAndCompatibilityElementString(const UChar* characters, unsigned length)
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +0000169{
170 const unsigned maxKeywordLength = %s;
171 LChar buffer[maxKeywordLength];
172 if (length > maxKeywordLength)
benjamin@webkit.orgf7245892014-03-29 03:08:59 +0000173 return nullptr;
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +0000174
175 for (unsigned i = 0; i < length; ++i) {
176 UChar character = characters[i];
darin@apple.com31887ff2019-08-18 15:12:19 +0000177 if (!isLatin1(character))
benjamin@webkit.orgf7245892014-03-29 03:08:59 +0000178 return nullptr;
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +0000179
180 buffer[i] = static_cast<LChar>(character);
181 }
benjamin@webkit.orgf7245892014-03-29 03:08:59 +0000182 return parsePseudoClassAndCompatibilityElementString(buffer, length);
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +0000183}
184""" % longest_keyword)
185
186output_file.write("""
darin@apple.comeffb0582019-06-29 21:50:00 +0000187PseudoClassOrCompatibilityPseudoElement parsePseudoClassAndCompatibilityElementString(StringView pseudoTypeString)
hyatt@apple.comb313af92016-09-08 21:09:04 +0000188{
189 const SelectorPseudoClassOrCompatibilityPseudoElementEntry* entry;
190 if (pseudoTypeString.is8Bit())
191 entry = parsePseudoClassAndCompatibilityElementString(pseudoTypeString.characters8(), pseudoTypeString.length());
192 else
193 entry = parsePseudoClassAndCompatibilityElementString(pseudoTypeString.characters16(), pseudoTypeString.length());
194
195 if (entry)
196 return entry->pseudoTypes;
197 return { CSSSelector::PseudoClassUnknown, CSSSelector::PseudoElementUnknown };
198}
199
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +0000200} // namespace WebCore
201
guijemont@igalia.com65db7e42018-09-12 15:09:16 +0000202IGNORE_WARNINGS_END
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +0000203
204""")
205output_file.close()
206
commit-queue@webkit.orgb0d437e2016-01-26 06:12:50 +0000207gperf_command = sys.argv[2]
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +0000208if 'GPERF' in os.environ:
209 gperf_command = os.environ['GPERF']
210
utatane.tea@gmail.com89eb7d72018-05-04 05:28:12 +0000211if subprocess.call([gperf_command, '--key-positions=*', '-m', '10', '-s', '2', 'SelectorPseudoClassAndCompatibilityElementMap.gperf', '--output-file=SelectorPseudoClassAndCompatibilityElementMap.cpp']) != 0:
benjamin@webkit.orgf7245892014-03-29 03:08:59 +0000212 print("Error when generating SelectorPseudoClassAndCompatibilityElementMap.cpp from SelectorPseudoClassAndCompatibilityElementMap.gperf :(")
benjamin@webkit.orge06e2b82014-03-10 21:00:26 +0000213 sys.exit(gperf_return)