1#! /usr/bin/perl
2#
3#   This file is part of the WebKit project
4#
5#   Copyright (C) 1999 Waldo Bastian (bastian@kde.org)
6#   Copyright (C) 2007, 2012 Apple Inc. All rights reserved.
7#   Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
8#   Copyright (C) 2010 Andras Becsi (abecsi@inf.u-szeged.hu), University of Szeged
9#
10#   This library is free software; you can redistribute it and/or
11#   modify it under the terms of the GNU Library General Public
12#   License as published by the Free Software Foundation; either
13#   version 2 of the License, or (at your option) any later version.
14#
15#   This library is distributed in the hope that it will be useful,
16#   but WITHOUT ANY WARRANTY; without even the implied warranty of
17#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18#   Library General Public License for more details.
19#
20#   You should have received a copy of the GNU Library General Public License
21#   along with this library; see the file COPYING.LIB.  If not, write to
22#   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23#   Boston, MA 02110-1301, USA.
24use Getopt::Long;
25use preprocessor;
26use strict;
27use warnings;
28
29my $defines;
30my $preprocessor;
31GetOptions('defines=s' => \$defines,
32           'preprocessor=s' => \$preprocessor);
33
34my @NAMES = applyPreprocessor("CSSValueKeywords.in", $defines, $preprocessor);
35
36my %namesHash;
37my @duplicates = ();
38
39my @names = ();
40foreach (@NAMES) {
41  next if (m/(^\s*$)/);
42  next if (/^#/);
43
44  # Input may use a different EOL sequence than $/, so avoid chomp.
45  $_ =~ s/[\r\n]+$//g;
46  # CSS values need to be lower case.
47  $_ = lc $_;
48  if (exists $namesHash{$_}) {
49    push @duplicates, $_;
50  } else {
51    $namesHash{$_} = 1;
52  }
53  push @names, $_;
54}
55
56if (@duplicates > 0) {
57    die 'Duplicate CSS value keywords  values: ', join(', ', @duplicates) . "\n";
58}
59
60open GPERF, ">CSSValueKeywords.gperf" || die "Could not open CSSValueKeywords.gperf for writing";
61print GPERF << "EOF";
62%{
63/* This file is automatically generated from CSSValueKeywords.in by makevalues, do not edit */
64
65#include \"CSSValueKeywords.h\"
66#include \"HashTools.h\"
67#include <string.h>
68
69namespace WebCore {
70%}
71%struct-type
72struct Value;
73%omit-struct-type
74%language=C++
75%readonly-tables
76%compare-strncmp
77%define class-name CSSValueKeywordsHash
78%define lookup-function-name findValueImpl
79%define hash-function-name value_hash_function
80%define word-array-name value_word_list
81%enum
82%%
83EOF
84
85foreach my $name (@names) {
86  my $id = $name;
87  $id =~ s/(^[^-])|-(.)/uc($1||$2)/ge;
88  print GPERF $name . ", CSSValue" . $id . "\n";
89}
90
91print GPERF << "EOF";
92%%
93static const char* const valueList[] = {
94    "",
95EOF
96
97foreach my $name (@names) {
98  print GPERF "    \"" . $name . "\",\n";
99}
100
101print GPERF << "EOF";
102    0
103};
104
105const Value* findValue(register const char* str, register unsigned int len)
106{
107    return CSSValueKeywordsHash::findValueImpl(str, len);
108}
109
110const char* getValueName(unsigned short id)
111{
112    if (id >= numCSSValueKeywords || id <= 0)
113        return 0;
114    return valueList[id];
115}
116
117} // namespace WebCore
118EOF
119close GPERF;
120
121
122open HEADER, ">CSSValueKeywords.h" || die "Could not open CSSValueKeywords.h for writing";
123print HEADER << "EOF";
124/* This file is automatically generated from CSSValueKeywords.in by makevalues, do not edit */
125
126#ifndef CSSValueKeywords_h
127#define CSSValueKeywords_h
128
129#include <string.h>
130
131namespace WebCore {
132
133enum CSSValueID {
134    CSSValueInvalid = 0,
135EOF
136
137my $i = 1;
138my $maxLen = 0;
139foreach my $name (@names) {
140  my $id = $name;
141  $id =~ s/(^[^-])|-(.)/uc($1||$2)/ge;
142  print HEADER "    CSSValue" . $id . " = " . $i . ",\n";
143  $i = $i + 1;
144  if (length($name) > $maxLen) {
145    $maxLen = length($name);
146  }
147}
148
149print HEADER "};\n\n";
150print HEADER "const int numCSSValueKeywords = " . $i . ";\n";
151print HEADER "const size_t maxCSSValueKeywordLength = " . $maxLen . ";\n";
152print HEADER << "EOF";
153
154const char* getValueName(unsigned short id);
155
156} // namespace WebCore
157
158#endif // CSSValueKeywords_h
159
160EOF
161close HEADER;
162
163my $gperf = $ENV{GPERF} ? $ENV{GPERF} : "gperf";
164system("\"$gperf\" --key-positions=\"*\" -D -n -s 2 CSSValueKeywords.gperf --output-file=CSSValueKeywords.cpp") == 0 || die "calling gperf failed: $?";
165