InFilesParser.pm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/usr/bin/perl -w
  2. # Copyright (C) 2008 Julien Chaffraix <jchaffraix@webkit.org>
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions
  6. # are met:
  7. # 1. Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # 2. Redistributions in binary form must reproduce the above copyright
  10. # notice, this list of conditions and the following disclaimer in the
  11. # documentation and/or other materials provided with the distribution.
  12. #
  13. # THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  14. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  17. # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. #
  25. use strict;
  26. package InFilesParser;
  27. my $isParsingCommonParameters;
  28. my $hasStartedParsing;
  29. # Helper functions
  30. sub trimComment
  31. {
  32. my $string = shift;
  33. $string =~ s/#.+$//;
  34. chomp($string);
  35. return $string;
  36. }
  37. sub trimWS
  38. {
  39. my $string = shift;
  40. $string =~ s/^\s+//;
  41. $string =~ s/\s+$//;
  42. chomp($string);
  43. return $string;
  44. }
  45. sub trimQuoteAndWS
  46. {
  47. my $string = shift;
  48. $string =~ s/\"([^\"]+)\"/$1/;
  49. return trimWS($string);
  50. }
  51. # Default constructor
  52. sub new
  53. {
  54. my $object = shift;
  55. my $reference = { };
  56. # Initialize the parser.
  57. $isParsingCommonParameters = 1;
  58. $hasStartedParsing = 0;
  59. bless($reference, $object);
  60. return $reference;
  61. }
  62. # parse take 3 attributes:
  63. # - the filestream to read from (the caller has to open / close it).
  64. # - the commonParameterHandler called when parsing the first part of the file with the parameter and the value.
  65. # - the perTagHandler called for each optional parameter with the element name, the parameter and its value.
  66. # If no parameter were provided, it is called once with an empty parameter and value.
  67. sub parse($)
  68. {
  69. my $object = shift;
  70. my $fileStream = shift; # IO::File only
  71. my $commonParameterHandler = shift;
  72. my $perTagHandler = shift;
  73. foreach (<$fileStream>) {
  74. # Ignore whitespace, in case the .in files have the wrong EOL
  75. # markers and those are getting treated as whitespace.
  76. $_ = trimWS($_);
  77. # Empty line, change from common parameter part
  78. # to per tag part if we have started parsing.
  79. if (/^$/) {
  80. if ($hasStartedParsing) {
  81. $isParsingCommonParameters = 0;
  82. }
  83. next;
  84. }
  85. # There may be a few empty lines at the beginning of the file
  86. # so detect the first non empty line which starts the common
  87. # parameters part.
  88. $hasStartedParsing = 1;
  89. if (/^#/) {
  90. next;
  91. }
  92. $_ = trimComment($_);
  93. if ($isParsingCommonParameters) {
  94. my ($name, $value) = split '=', $_;
  95. $name = trimWS($name);
  96. if (defined($value)) {
  97. $value = trimQuoteAndWS($value);
  98. } else {
  99. # We default to 1 as it eases the syntax.
  100. $value = "1";
  101. }
  102. &$commonParameterHandler($name, $value);
  103. } else {
  104. # Parsing per-tag parameters.
  105. # Split the tag name ($1) from the optionnal parameter(s) ($2)
  106. /^(\S+)\s*(.*)$/;
  107. my $elementName = $1;
  108. if ($2) {
  109. my @options = split "," , $2;
  110. my ($option, $value);
  111. for (my $i = 0; $i < @options; ++$i) {
  112. ($option, $value) = split "=", $options[$i];
  113. $option = trimWS($option);
  114. if (defined($value)) {
  115. $value = trimQuoteAndWS($value);
  116. } else {
  117. # We default to 1 as it eases the syntax.
  118. $value = "1";
  119. }
  120. &$perTagHandler($elementName, $option, $value);
  121. }
  122. } else {
  123. # No parameter was given so call it with empty strings.
  124. &$perTagHandler($elementName, "", "");
  125. }
  126. }
  127. }
  128. }
  129. 1;