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.
21use strict;
22use warnings;
23
24use File::Basename;
25use File::Spec;
26use Getopt::Long;
27
28my $outputDir = ".";
29my $extraDefines = "";
30my $symbolsPrefix = "";
31my $preprocessor = "";
32my $preprocessOnly = 0;
33my $bison = "bison";
34
35GetOptions(
36    'outputDir=s' => \$outputDir,
37    'extraDefines=s' => \$extraDefines,
38    'bison=s' => \$bison,
39    'preprocessor=s' => \$preprocessor,
40    'preprocessOnly' => \$preprocessOnly,
41    'symbolsPrefix=s' => \$symbolsPrefix
42);
43
44my $grammarFilePath = $ARGV[0];
45my $grammarIncludesFilePath = @ARGV > 0 ? $ARGV[1] : "";
46
47if (!length($symbolsPrefix) && !$preprocessOnly) {
48    die "Need a symbols prefix to give to bison (e.g. cssyy, xpathyy)";
49}
50
51my ($filename, $basePath, $suffix) = fileparse($grammarFilePath, (".y", ".y.in"));
52
53if ($suffix eq ".y.in") {
54    my $grammarFileOutPath = File::Spec->join($outputDir, "$filename.y");
55    if (!$grammarIncludesFilePath) {
56        $grammarIncludesFilePath = "${basePath}${filename}.y.includes";
57    }
58
59    open GRAMMAR, ">$grammarFileOutPath" or die;
60    open INCLUDES, "<$grammarIncludesFilePath" or die;
61
62    require preprocessor;
63
64    while (<INCLUDES>) {
65        print GRAMMAR;
66    }
67    print GRAMMAR join("", applyPreprocessor($grammarFilePath, $extraDefines, $preprocessor));
68    close GRAMMAR;
69
70    $grammarFilePath = $grammarFileOutPath;
71
72    exit if $preprocessOnly;
73}
74
75my $fileBase = File::Spec->join($outputDir, $filename);
76system("$bison -d -p $symbolsPrefix $grammarFilePath -o $fileBase.cpp");
77
78open HEADER, ">$fileBase.h" or die;
79print HEADER << "EOF";
80#ifndef CSSGRAMMAR_H
81#define CSSGRAMMAR_H
82EOF
83
84open HPP, "<$fileBase.cpp.h" or open HPP, "<$fileBase.hpp" or die;
85while (<HPP>) {
86    print HEADER;
87}
88close HPP;
89
90print HEADER "#endif\n";
91close HEADER;
92
93unlink("$fileBase.cpp.h");
94unlink("$fileBase.hpp");
95
96