makegrammar.pl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #! /usr/bin/perl
  2. #
  3. # This file is part of the WebKit project
  4. #
  5. # Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Library General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2 of the License, or (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. # Library General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Library General Public License
  18. # along with this library; see the file COPYING.LIB. If not, write to
  19. # the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  20. # Boston, MA 02110-1301, USA.
  21. use strict;
  22. use warnings;
  23. use File::Basename;
  24. use File::Spec;
  25. use Getopt::Long;
  26. my $outputDir = ".";
  27. my $extraDefines = "";
  28. my $symbolsPrefix = "";
  29. my $preprocessor = "";
  30. my $preprocessOnly = 0;
  31. my $bison = "bison";
  32. GetOptions(
  33. 'outputDir=s' => \$outputDir,
  34. 'extraDefines=s' => \$extraDefines,
  35. 'bison=s' => \$bison,
  36. 'preprocessor=s' => \$preprocessor,
  37. 'preprocessOnly' => \$preprocessOnly,
  38. 'symbolsPrefix=s' => \$symbolsPrefix
  39. );
  40. my $grammarFilePath = $ARGV[0];
  41. my $grammarIncludesFilePath = @ARGV > 0 ? $ARGV[1] : "";
  42. if (!length($symbolsPrefix) && !$preprocessOnly) {
  43. die "Need a symbols prefix to give to bison (e.g. cssyy, xpathyy)";
  44. }
  45. my ($filename, $basePath, $suffix) = fileparse($grammarFilePath, (".y", ".y.in"));
  46. if ($suffix eq ".y.in") {
  47. my $grammarFileOutPath = File::Spec->join($outputDir, "$filename.y");
  48. if (!$grammarIncludesFilePath) {
  49. $grammarIncludesFilePath = "${basePath}${filename}.y.includes";
  50. }
  51. open GRAMMAR, ">$grammarFileOutPath" or die;
  52. open INCLUDES, "<$grammarIncludesFilePath" or die;
  53. require preprocessor;
  54. while (<INCLUDES>) {
  55. print GRAMMAR;
  56. }
  57. print GRAMMAR join("", applyPreprocessor($grammarFilePath, $extraDefines, $preprocessor));
  58. close GRAMMAR;
  59. $grammarFilePath = $grammarFileOutPath;
  60. exit if $preprocessOnly;
  61. }
  62. my $fileBase = File::Spec->join($outputDir, $filename);
  63. system("$bison -d -p $symbolsPrefix $grammarFilePath -o $fileBase.cpp");
  64. open HEADER, ">$fileBase.h" or die;
  65. print HEADER << "EOF";
  66. #ifndef CSSGRAMMAR_H
  67. #define CSSGRAMMAR_H
  68. EOF
  69. open HPP, "<$fileBase.cpp.h" or open HPP, "<$fileBase.hpp" or die;
  70. while (<HPP>) {
  71. print HEADER;
  72. }
  73. close HPP;
  74. print HEADER "#endif\n";
  75. close HEADER;
  76. unlink("$fileBase.cpp.h");
  77. unlink("$fileBase.hpp");