benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 1 | #!/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 | |
| 26 | import os |
| 27 | import sys |
utatane.tea@gmail.com | 89eb7d7 | 2018-05-04 05:28:12 +0000 | [diff] [blame] | 28 | import subprocess |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 29 | |
| 30 | |
| 31 | def enumerablePseudoType(stringPseudoType): |
benjamin@webkit.org | f4ffe86 | 2014-04-20 20:43:35 +0000 | [diff] [blame] | 32 | output = ['CSSSelector::PseudoClass'] |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 33 | |
| 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 | |
| 58 | def expand_ifdef_condition(condition): |
| 59 | return condition.replace('(', '_').replace(')', '') |
| 60 | |
benjamin@webkit.org | f724589 | 2014-03-29 03:08:59 +0000 | [diff] [blame] | 61 | output_file = open('SelectorPseudoClassAndCompatibilityElementMap.gperf', 'w') |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 62 | |
| 63 | output_file.write(""" |
| 64 | %{ |
| 65 | /* |
hyatt@apple.com | b313af9 | 2016-09-08 21:09:04 +0000 | [diff] [blame] | 66 | * Copyright (C) 2014-2016 Apple Inc. All rights reserved. |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 67 | * |
| 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.com | 0a85f1b | 2016-12-10 20:38:50 +0000 | [diff] [blame] | 95 | #include "CSSParserSelector.h" |
darin@apple.com | 048cc3a | 2016-01-22 17:17:04 +0000 | [diff] [blame] | 96 | |
guijemont@igalia.com | 65db7e4 | 2018-09-12 15:09:16 +0000 | [diff] [blame] | 97 | IGNORE_WARNINGS_BEGIN("implicit-fallthrough") |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 98 | |
zandobersek@gmail.com | 4cae92f | 2018-05-14 12:52:25 +0000 | [diff] [blame] | 99 | // Older versions of gperf like to use the `register` keyword. |
| 100 | #define register |
| 101 | |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 102 | namespace WebCore { |
| 103 | |
benjamin@webkit.org | f724589 | 2014-03-29 03:08:59 +0000 | [diff] [blame] | 104 | struct SelectorPseudoClassOrCompatibilityPseudoElementEntry { |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 105 | const char* name; |
benjamin@webkit.org | f724589 | 2014-03-29 03:08:59 +0000 | [diff] [blame] | 106 | PseudoClassOrCompatibilityPseudoElement pseudoTypes; |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | %} |
| 110 | %struct-type |
benjamin@webkit.org | f4ffe86 | 2014-04-20 20:43:35 +0000 | [diff] [blame] | 111 | %define initializer-suffix ,{CSSSelector::PseudoClassUnknown,CSSSelector::PseudoElementUnknown} |
benjamin@webkit.org | f724589 | 2014-03-29 03:08:59 +0000 | [diff] [blame] | 112 | %define class-name SelectorPseudoClassAndCompatibilityElementMapHash |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 113 | %omit-struct-type |
| 114 | %language=C++ |
| 115 | %readonly-tables |
| 116 | %global-table |
| 117 | %ignore-case |
| 118 | %compare-strncmp |
| 119 | %enum |
| 120 | |
benjamin@webkit.org | f724589 | 2014-03-29 03:08:59 +0000 | [diff] [blame] | 121 | struct SelectorPseudoClassOrCompatibilityPseudoElementEntry; |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 122 | |
| 123 | %% |
| 124 | """) |
| 125 | |
| 126 | webcore_defines = [i.strip() for i in sys.argv[-1].split(' ')] |
| 127 | |
| 128 | longest_keyword = 0 |
| 129 | |
| 130 | ignore_until_endif = False |
| 131 | input_file = open(sys.argv[1], 'r') |
| 132 | for line in input_file: |
benjamin@webkit.org | f724589 | 2014-03-29 03:08:59 +0000 | [diff] [blame] | 133 | line = line.strip() |
| 134 | if not line: |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 135 | continue |
| 136 | |
benjamin@webkit.org | f724589 | 2014-03-29 03:08:59 +0000 | [diff] [blame] | 137 | if line.startswith('#if '): |
| 138 | condition = line[4:].strip() |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 139 | if expand_ifdef_condition(condition) not in webcore_defines: |
| 140 | ignore_until_endif = True |
| 141 | continue |
| 142 | |
benjamin@webkit.org | f724589 | 2014-03-29 03:08:59 +0000 | [diff] [blame] | 143 | if line.startswith('#endif'): |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 144 | ignore_until_endif = False |
| 145 | continue |
| 146 | |
| 147 | if ignore_until_endif: |
| 148 | continue |
| 149 | |
benjamin@webkit.org | f724589 | 2014-03-29 03:08:59 +0000 | [diff] [blame] | 150 | keyword_definition = line.split(',') |
| 151 | if len(keyword_definition) == 1: |
| 152 | keyword = keyword_definition[0].strip() |
benjamin@webkit.org | ef42be4 | 2014-04-07 20:26:44 +0000 | [diff] [blame] | 153 | output_file.write('"%s", {%s, CSSSelector::PseudoElementUnknown}\n' % (keyword, enumerablePseudoType(keyword))) |
benjamin@webkit.org | f724589 | 2014-03-29 03:08:59 +0000 | [diff] [blame] | 154 | 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.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 157 | longest_keyword = max(longest_keyword, len(keyword)) |
| 158 | |
| 159 | output_file.write("""%% |
| 160 | |
benjamin@webkit.org | f724589 | 2014-03-29 03:08:59 +0000 | [diff] [blame] | 161 | static inline const SelectorPseudoClassOrCompatibilityPseudoElementEntry* parsePseudoClassAndCompatibilityElementString(const LChar* characters, unsigned length) |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 162 | { |
benjamin@webkit.org | f724589 | 2014-03-29 03:08:59 +0000 | [diff] [blame] | 163 | return SelectorPseudoClassAndCompatibilityElementMapHash::in_word_set(reinterpret_cast<const char*>(characters), length); |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 164 | }""") |
| 165 | |
| 166 | output_file.write(""" |
| 167 | |
benjamin@webkit.org | f724589 | 2014-03-29 03:08:59 +0000 | [diff] [blame] | 168 | static inline const SelectorPseudoClassOrCompatibilityPseudoElementEntry* parsePseudoClassAndCompatibilityElementString(const UChar* characters, unsigned length) |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 169 | { |
| 170 | const unsigned maxKeywordLength = %s; |
| 171 | LChar buffer[maxKeywordLength]; |
| 172 | if (length > maxKeywordLength) |
benjamin@webkit.org | f724589 | 2014-03-29 03:08:59 +0000 | [diff] [blame] | 173 | return nullptr; |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 174 | |
| 175 | for (unsigned i = 0; i < length; ++i) { |
| 176 | UChar character = characters[i]; |
darin@apple.com | 31887ff | 2019-08-18 15:12:19 +0000 | [diff] [blame] | 177 | if (!isLatin1(character)) |
benjamin@webkit.org | f724589 | 2014-03-29 03:08:59 +0000 | [diff] [blame] | 178 | return nullptr; |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 179 | |
| 180 | buffer[i] = static_cast<LChar>(character); |
| 181 | } |
benjamin@webkit.org | f724589 | 2014-03-29 03:08:59 +0000 | [diff] [blame] | 182 | return parsePseudoClassAndCompatibilityElementString(buffer, length); |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 183 | } |
| 184 | """ % longest_keyword) |
| 185 | |
| 186 | output_file.write(""" |
darin@apple.com | effb058 | 2019-06-29 21:50:00 +0000 | [diff] [blame] | 187 | PseudoClassOrCompatibilityPseudoElement parsePseudoClassAndCompatibilityElementString(StringView pseudoTypeString) |
hyatt@apple.com | b313af9 | 2016-09-08 21:09:04 +0000 | [diff] [blame] | 188 | { |
| 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.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 200 | } // namespace WebCore |
| 201 | |
guijemont@igalia.com | 65db7e4 | 2018-09-12 15:09:16 +0000 | [diff] [blame] | 202 | IGNORE_WARNINGS_END |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 203 | |
| 204 | """) |
| 205 | output_file.close() |
| 206 | |
commit-queue@webkit.org | b0d437e | 2016-01-26 06:12:50 +0000 | [diff] [blame] | 207 | gperf_command = sys.argv[2] |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 208 | if 'GPERF' in os.environ: |
| 209 | gperf_command = os.environ['GPERF'] |
| 210 | |
utatane.tea@gmail.com | 89eb7d7 | 2018-05-04 05:28:12 +0000 | [diff] [blame] | 211 | if subprocess.call([gperf_command, '--key-positions=*', '-m', '10', '-s', '2', 'SelectorPseudoClassAndCompatibilityElementMap.gperf', '--output-file=SelectorPseudoClassAndCompatibilityElementMap.cpp']) != 0: |
benjamin@webkit.org | f724589 | 2014-03-29 03:08:59 +0000 | [diff] [blame] | 212 | print("Error when generating SelectorPseudoClassAndCompatibilityElementMap.cpp from SelectorPseudoClassAndCompatibilityElementMap.gperf :(") |
benjamin@webkit.org | e06e2b8 | 2014-03-10 21:00:26 +0000 | [diff] [blame] | 213 | sys.exit(gperf_return) |