3 # Copyright (C) 2011 Adam Barth <abarth@webkit.org>
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
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.
14 # THIS SOFTWARE IS PROVIDED BY GOOGLE, INC. ``AS IS'' AND ANY
15 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package InFilesCompiler;
42 my $defaultItemFactory;
49 my ($itemName, $property, $value) = @_;
51 $parsedItems{$itemName} = { &$defaultItemFactory($itemName) } if !defined($parsedItems{$itemName});
53 return unless $property;
55 die "Unknown property $property for $itemName\n" if !defined($parsedItems{$itemName}{$property});
56 $parsedItems{$itemName}{$property} = $value;
59 sub parameterHandler($$)
61 my ($parameter, $value) = @_;
63 die "Unknown parameter $parameter\n" if !defined($parsedParameters{$parameter});
64 $parsedParameters{$parameter} = $value;
72 my $defaultParametersRef = shift;
73 %parsedParameters = %{ $defaultParametersRef };
74 $defaultItemFactory = shift;
78 bless($reference, $object);
82 sub initializeFromCommandLine()
85 'input=s' => \$inputFile,
86 'outputDir=s' => \$outputDir,
89 die "You must specify --input <file>" unless length($inputFile);
93 # FIXME: Should we provide outputDir via an accessor?
100 my $generateCode = shift;
102 my $file = new IO::File;
103 open($file, $inputFile) or die "Failed to open file: $!";
105 my $InParser = InFilesParser->new();
106 $InParser->parse($file, \¶meterHandler, \&itemHandler);
109 die "Failed to read from file: $inputFile" if (keys %parsedItems == 0);
111 &$generateCode(\%parsedParameters, \%parsedItems);
117 * THIS FILE WAS AUTOMATICALLY GENERATED, DO NOT EDIT.
119 * Copyright (C) 2011 Google Inc. All rights reserved.
121 * Redistribution and use in source and binary forms, with or without
122 * modification, are permitted provided that the following conditions
124 * 1. Redistributions of source code must retain the above copyright
125 * notice, this list of conditions and the following disclaimer.
126 * 2. Redistributions in binary form must reproduce the above copyright
127 * notice, this list of conditions and the following disclaimer in the
128 * documentation and/or other materials provided with the distribution.
130 * THIS SOFTWARE IS PROVIDED BY GOOGLE, INC. ``AS IS'' AND ANY
131 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
132 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
133 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
134 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
135 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
136 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
137 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
138 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
139 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
140 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146 sub interfaceForItem($)
149 my $itemName = shift;
151 my $interfaceName = $parsedItems{$itemName}{"interfaceName"};
152 $interfaceName = $itemName unless $interfaceName;
154 return $interfaceName;
160 my $camelCase = shift;
162 return "EVENT" if $camelCase eq "Event";
163 return "EVENT_TARGET" if $camelCase eq "EventTarget";
164 return "EXCEPTION" if $camelCase eq "Exception";
166 die "Ok, you got me. This script is really just a giant hack. (\$camelCase=${camelCase})";
169 sub preferredConditional()
172 my $conditional = shift;
174 my @conditionals = split('\\|', $conditional);
175 return $conditionals[0];
178 sub conditionalStringFromAttributeValue()
181 my $conditional = shift;
183 return "ENABLE(" . join(') || ENABLE(', split('\\|', $conditional)) . ")";
186 sub generateInterfacesHeader()
191 my $namespace = $parsedParameters{"namespace"};
192 my $outputFile = "$outputDir/${namespace}Interfaces.h";
194 open F, ">$outputFile" or die "Failed to open file: $!";
198 print F "#ifndef ${namespace}Interfaces_h\n";
199 print F "#define ${namespace}Interfaces_h\n";
202 my %unconditionalInterfaces = ();
203 my %interfacesByConditional = ();
205 for my $itemName (sort keys %parsedItems) {
206 my $conditional = $parsedItems{$itemName}{"conditional"};
207 my $interfaceName = $object->interfaceForItem($itemName);
210 if (!defined($interfacesByConditional{$conditional})) {
211 $interfacesByConditional{$conditional} = ();
213 $interfacesByConditional{$conditional}{$interfaceName} = 1;
215 $unconditionalInterfaces{$interfaceName} = 1
219 my $macroStyledNamespace = $object->toMacroStyle($namespace);
221 for my $conditional (sort keys %interfacesByConditional) {
222 my $preferredConditional = $object->preferredConditional($conditional);
223 print F "#if " . $object->conditionalStringFromAttributeValue($conditional) . "\n";
224 print F "#define DOM_${macroStyledNamespace}_INTERFACES_FOR_EACH_$preferredConditional(macro) \\\n";
226 for my $interface (sort keys %{ $interfacesByConditional{$conditional} }) {
227 next if defined($unconditionalInterfaces{$interface});
228 print F " macro($interface) \\\n";
231 print F "// End of DOM_${macroStyledNamespace}_INTERFACES_FOR_EACH_$preferredConditional\n";
233 print F "#define DOM_${macroStyledNamespace}_INTERFACES_FOR_EACH_$preferredConditional(macro)\n";
238 print F "#define DOM_${macroStyledNamespace}_INTERFACES_FOR_EACH(macro) \\\n";
240 for my $interface (sort keys %unconditionalInterfaces) {
241 print F " macro($interface) \\\n";
244 for my $conditional (sort keys %interfacesByConditional) {
245 my $preferredConditional = $object->preferredConditional($conditional);
246 print F " DOM_${macroStyledNamespace}_INTERFACES_FOR_EACH_$preferredConditional(macro) \\\n";
250 print F "#endif // ${namespace}Interfaces_h\n";
255 sub generateHeadersHeader()
260 my $namespace = $parsedParameters{"namespace"};
261 my $outputFile = "$outputDir/${namespace}Headers.h";
263 open F, ">$outputFile" or die "Failed to open file: $!";
267 print F "#ifndef ${namespace}Headers_h\n";
268 print F "#define ${namespace}Headers_h\n";
271 my %includedInterfaces = ();
273 for my $itemName (sort keys %parsedItems) {
274 my $conditional = $parsedItems{$itemName}{"conditional"};
275 my $interfaceName = $object->interfaceForItem($itemName);
277 next if defined($includedInterfaces{$interfaceName});
278 $includedInterfaces{$interfaceName} = 1;
280 print F "#if " . $object->conditionalStringFromAttributeValue($conditional) . "\n" if $conditional;
281 print F "#include \"$interfaceName.h\"\n";
282 print F "#if USE(JSC)\n";
283 print F "#include \"JS$interfaceName.h\"\n";
284 print F "#elif USE(V8)\n";
285 print F "#include \"V8$interfaceName.h\"\n";
287 print F "#endif\n" if $conditional;
291 print F "#endif // ${namespace}Headers_h\n";