3 # Copyright (C) 2008 Julien Chaffraix <jchaffraix@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 APPLE COMPUTER, 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.
29 package InFilesParser;
31 my $isParsingCommonParameters;
32 my $hasStartedParsing;
56 $string =~ s/\"([^\"]+)\"/$1/;
57 return trimWS($string);
67 # Initialize the parser.
68 $isParsingCommonParameters = 1;
69 $hasStartedParsing = 0;
71 bless($reference, $object);
75 # parse take 3 attributes:
76 # - the filestream to read from (the caller has to open / close it).
77 # - the commonParameterHandler called when parsing the first part of the file with the parameter and the value.
78 # - the perTagHandler called for each optional parameter with the element name, the parameter and its value.
79 # If no parameter were provided, it is called once with an empty parameter and value.
83 my $fileStream = shift; # IO::File only
84 my $commonParameterHandler = shift;
85 my $perTagHandler = shift;
87 foreach (<$fileStream>) {
88 # Ignore whitespace, in case the .in files have the wrong EOL
89 # markers and those are getting treated as whitespace.
92 # Empty line, change from common parameter part
93 # to per tag part if we have started parsing.
95 if ($hasStartedParsing) {
96 $isParsingCommonParameters = 0;
101 # There may be a few empty lines at the beginning of the file
102 # so detect the first non empty line which starts the common
104 $hasStartedParsing = 1;
110 $_ = trimComment($_);
112 if ($isParsingCommonParameters) {
113 my ($name, $value) = split '=', $_;
115 $name = trimWS($name);
116 if (defined($value)) {
117 $value = trimQuoteAndWS($value);
119 # We default to 1 as it eases the syntax.
123 &$commonParameterHandler($name, $value);
125 # Parsing per-tag parameters.
127 # Split the tag name ($1) from the optionnal parameter(s) ($2)
129 my $elementName = $1;
132 my @options = split "," , $2;
133 my ($option, $value);
134 for (my $i = 0; $i < @options; ++$i) {
135 ($option, $value) = split "=", $options[$i];
136 $option = trimWS($option);
137 if (defined($value)) {
138 $value = trimQuoteAndWS($value);
140 # We default to 1 as it eases the syntax.
144 &$perTagHandler($elementName, $option, $value);
147 # No parameter was given so call it with empty strings.
148 &$perTagHandler($elementName, "", "");