Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
| 2 | |
| 3 | # Copyright (C) 2008 Julien Chaffraix <jchaffraix@webkit.org> |
| 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 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. |
| 25 | # |
| 26 | |
| 27 | use strict; |
| 28 | |
| 29 | package InFilesParser; |
| 30 | |
| 31 | my $isParsingCommonParameters; |
| 32 | my $hasStartedParsing; |
| 33 | |
| 34 | # Helper functions |
| 35 | |
| 36 | sub trimComment |
| 37 | { |
| 38 | my $string = shift; |
| 39 | $string =~ s/#.+$//; |
| 40 | chomp($string); |
| 41 | return $string; |
| 42 | } |
| 43 | |
| 44 | sub trimWS |
| 45 | { |
| 46 | my $string = shift; |
| 47 | $string =~ s/^\s+//; |
| 48 | $string =~ s/\s+$//; |
| 49 | chomp($string); |
| 50 | return $string; |
| 51 | } |
| 52 | |
| 53 | sub trimQuoteAndWS |
| 54 | { |
| 55 | my $string = shift; |
| 56 | $string =~ s/\"([^\"]+)\"/$1/; |
| 57 | return trimWS($string); |
| 58 | } |
| 59 | |
| 60 | # Default constructor |
| 61 | |
| 62 | sub new |
| 63 | { |
| 64 | my $object = shift; |
| 65 | my $reference = { }; |
| 66 | |
| 67 | # Initialize the parser. |
| 68 | $isParsingCommonParameters = 1; |
| 69 | $hasStartedParsing = 0; |
| 70 | |
| 71 | bless($reference, $object); |
| 72 | return $reference; |
| 73 | } |
| 74 | |
| 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. |
| 80 | sub parse($) |
| 81 | { |
| 82 | my $object = shift; |
| 83 | my $fileStream = shift; # IO::File only |
| 84 | my $commonParameterHandler = shift; |
| 85 | my $perTagHandler = shift; |
| 86 | |
| 87 | foreach (<$fileStream>) { |
| 88 | # Ignore whitespace, in case the .in files have the wrong EOL |
| 89 | # markers and those are getting treated as whitespace. |
| 90 | $_ = trimWS($_); |
| 91 | |
| 92 | # Empty line, change from common parameter part |
| 93 | # to per tag part if we have started parsing. |
| 94 | if (/^$/) { |
| 95 | if ($hasStartedParsing) { |
| 96 | $isParsingCommonParameters = 0; |
| 97 | } |
| 98 | next; |
| 99 | } |
| 100 | |
| 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 |
| 103 | # parameters part. |
| 104 | $hasStartedParsing = 1; |
| 105 | |
| 106 | if (/^#/) { |
| 107 | next; |
| 108 | } |
| 109 | |
| 110 | $_ = trimComment($_); |
| 111 | |
| 112 | if ($isParsingCommonParameters) { |
| 113 | my ($name, $value) = split '=', $_; |
| 114 | |
| 115 | $name = trimWS($name); |
| 116 | if (defined($value)) { |
| 117 | $value = trimQuoteAndWS($value); |
| 118 | } else { |
| 119 | # We default to 1 as it eases the syntax. |
| 120 | $value = "1"; |
| 121 | } |
| 122 | |
| 123 | &$commonParameterHandler($name, $value); |
| 124 | } else { |
| 125 | # Parsing per-tag parameters. |
| 126 | |
| 127 | # Split the tag name ($1) from the optionnal parameter(s) ($2) |
| 128 | /^(\S+)\s*(.*)$/; |
| 129 | my $elementName = $1; |
| 130 | |
| 131 | if ($2) { |
| 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); |
| 139 | } else { |
| 140 | # We default to 1 as it eases the syntax. |
| 141 | $value = "1"; |
| 142 | } |
| 143 | |
| 144 | &$perTagHandler($elementName, $option, $value); |
| 145 | } |
| 146 | } else { |
| 147 | # No parameter was given so call it with empty strings. |
| 148 | &$perTagHandler($elementName, "", ""); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | 1; |