summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/bindings/scripts/IDLParser.pm
blob: 927deb916f119db73c3c1a494ad0de3c3877b249 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# 
# KDOM IDL parser
#
# Copyright (C) 2005 Nikolas Zimmermann <wildfox@kde.org>
# 
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
# 
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
# 
# You should have received a copy of the GNU Library General Public License
# along with this library; see the file COPYING.LIB.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
# 

package IDLParser;

use strict;

use IPC::Open2;
use IDLStructure;

use constant MODE_UNDEF    => 0; # Default mode.

use constant MODE_MODULE  => 10; # 'module' section
use constant MODE_INTERFACE  => 11; # 'interface' section
use constant MODE_EXCEPTION  => 12; # 'exception' section
use constant MODE_ALIAS    => 13; # 'alias' section

# Helper variables
my @temporaryContent = "";

my $parseMode = MODE_UNDEF;
my $preservedParseMode = MODE_UNDEF;

my $beQuiet; # Should not display anything on STDOUT?
my $document = 0; # Will hold the resulting 'idlDocument'
my $parentsOnly = 0; # If 1, parse only enough to populate parents list

# Default Constructor
sub new
{
    my $object = shift;
    my $reference = { };

    $document = 0;
    $beQuiet = shift;

    bless($reference, $object);
    return $reference;
}

# Returns the parsed 'idlDocument'
sub Parse
{
    my $object = shift;
    my $fileName = shift;
    my $defines = shift;
    my $preprocessor = shift;
    $parentsOnly = shift;

    if (!$preprocessor) {
        require Config;
        my $gccLocation = "";
        if ($ENV{CC}) {
            $gccLocation = $ENV{CC};
        } elsif (($Config::Config{'osname'}) =~ /solaris/i) {
            $gccLocation = "/usr/sfw/bin/gcc";
        } else {
            $gccLocation = "/usr/bin/gcc";
        }
        $preprocessor = $gccLocation . " -E -P -x c++";
    }

    if (!$defines) {
        $defines = "";
    }

    print " | *** Starting to parse $fileName...\n |\n" unless $beQuiet;

    my $pid = open2(\*PP_OUT, \*PP_IN, split(' ', $preprocessor), (map { "-D$_" } split(' ', $defines)), $fileName);
    close PP_IN;
    my @documentContent = <PP_OUT>;
    close PP_OUT;
    waitpid($pid, 0);

    my $dataAvailable = 0;

    # Simple IDL Parser (tm)
    foreach (@documentContent) {
        my $newParseMode = $object->DetermineParseMode($_);

        if ($newParseMode ne MODE_UNDEF) {
            if ($dataAvailable eq 0) {
                $dataAvailable = 1; # Start node building...
            } else {
                $object->ProcessSection();
            }
        }

        # Update detected data stream mode...
        if ($newParseMode ne MODE_UNDEF) {
            $parseMode = $newParseMode;
        }

        push(@temporaryContent, $_);
    }

    # Check if there is anything remaining to parse...
    if (($parseMode ne MODE_UNDEF) and ($#temporaryContent > 0)) {
        $object->ProcessSection();
    }

    print " | *** Finished parsing!\n" unless $beQuiet;
 
    $document->fileName($fileName);

    return $document;
}

sub ParseModule
{
    my $object = shift;
    my $dataNode = shift;

    print " |- Trying to parse module...\n" unless $beQuiet;

    my $data = join("", @temporaryContent);
    $data =~ /$IDLStructure::moduleSelector/;

    my $moduleName = (defined($1) ? $1 : die("Parsing error!\nSource:\n$data\n)"));
    $dataNode->module($moduleName);

    print "  |----> Module; NAME \"$moduleName\"\n |-\n |\n" unless $beQuiet;
}

sub dumpExtendedAttributes
{
    my $padStr = shift;
    my $attrs = shift;

    if (!%{$attrs}) {
        return "";
    }

    my @temp;
    while ((my $name, my $value) = each(%{$attrs})) {
        push(@temp, "$name=$value");
    }

    return $padStr . "[" . join(", ", @temp) . "]";
}

sub parseExtendedAttributes
{
    my $str = shift;
    $str =~ s/\[\s*(.*?)\s*\]/$1/g;

    my %attrs = ();

    foreach my $value (split(/\s*,\s*/, $str)) {
        (my $name, my $val) = split(/\s*=\s*/, $value, 2);

        # Attributes with no value are set to be true
        $val = 1 unless defined $val;
        $attrs{$name} = $val;
        die("Invalid extended attribute name: '$name'\n") if $name =~ /\s/;
    }

    return \%attrs;
}

sub ParseInterface
{
    my $object = shift;
    my $dataNode = shift;
    my $sectionName = shift;

    my $data = join("", @temporaryContent);

    # Look for end-of-interface mark
    $data =~ /};/g;
    $data = substr($data, index($data, $sectionName), pos($data) - length($data));

    $data =~ s/[\n\r]/ /g;

    # Beginning of the regexp parsing magic
    if ($sectionName eq "exception") {
        print " |- Trying to parse exception...\n" unless $beQuiet;

        my $exceptionName = "";
        my $exceptionData = "";
        my $exceptionDataName = "";
        my $exceptionDataType = "";

        # Match identifier of the exception, and enclosed data...
        $data =~ /$IDLStructure::exceptionSelector/;
        $exceptionName = (defined($1) ? $1 : die("Parsing error!\nSource:\n$data\n)"));
        $exceptionData = (defined($2) ? $2 : die("Parsing error!\nSource:\n$data\n)"));

        ('' =~ /^/); # Reset variables needed for regexp matching

        # ... parse enclosed data (get. name & type)
        $exceptionData =~ /$IDLStructure::exceptionSubSelector/;
        $exceptionDataType = (defined($1) ? $1 : die("Parsing error!\nSource:\n$data\n)"));
        $exceptionDataName = (defined($2) ? $2 : die("Parsing error!\nSource:\n$data\n)"));

        # Fill in domClass datastructure
        $dataNode->name($exceptionName);

        my $newDataNode = new domAttribute();
        $newDataNode->type("readonly attribute");
        $newDataNode->signature(new domSignature());

        $newDataNode->signature->name($exceptionDataName);
        $newDataNode->signature->type($exceptionDataType);

        my $arrayRef = $dataNode->attributes;
        push(@$arrayRef, $newDataNode);

        print "  |----> Exception; NAME \"$exceptionName\" DATA TYPE \"$exceptionDataType\" DATA NAME \"$exceptionDataName\"\n |-\n |\n" unless $beQuiet;
    } elsif ($sectionName eq "interface") {
        print " |- Trying to parse interface...\n" unless $beQuiet;

        my $interfaceName = "";
        my $interfaceData = "";

        # Match identifier of the interface, and enclosed data...
        $data =~ /$IDLStructure::interfaceSelector/;

        my $interfaceExtendedAttributes = (defined($1) ? $1 : " "); chop($interfaceExtendedAttributes);
        $interfaceName = (defined($2) ? $2 : die("Parsing error!\nSource:\n$data\n)"));
        my $interfaceBase = (defined($3) ? $3 : "");
        $interfaceData = (defined($4) ? $4 : die("Parsing error!\nSource:\n$data\n)"));

        # Fill in known parts of the domClass datastructure now...
        $dataNode->name($interfaceName);
        $dataNode->extendedAttributes(parseExtendedAttributes($interfaceExtendedAttributes));

        # Inheritance detection
        my @interfaceParents = split(/,/, $interfaceBase);
        foreach(@interfaceParents) {
            my $line = $_;
            $line =~ s/\s*//g;

            my $arrayRef = $dataNode->parents;
            push(@$arrayRef, $line);
        }

        return if $parentsOnly;

        $interfaceData =~ s/[\n\r]/ /g;
        my @interfaceMethods = split(/;/, $interfaceData);

        foreach my $line (@interfaceMethods) {
            if ($line =~ /\Wattribute\W/) {
                $line =~ /$IDLStructure::interfaceAttributeSelector/;

                my $attributeType = (defined($1) ? $1 : die("Parsing error!\nSource:\n$line\n)"));
                my $attributeExtendedAttributes = (defined($2) ? $2 : " "); chop($attributeExtendedAttributes);

                my $attributeDataType = (defined($3) ? $3 : die("Parsing error!\nSource:\n$line\n)"));
                my $attributeDataName = (defined($4) ? $4 : die("Parsing error!\nSource:\n$line\n)"));
  
                ('' =~ /^/); # Reset variables needed for regexp matching

                $line =~ /$IDLStructure::getterRaisesSelector/;
                my $getterException = (defined($1) ? $1 : "");

                $line =~ /$IDLStructure::setterRaisesSelector/;
                my $setterException = (defined($1) ? $1 : "");

                my $newDataNode = new domAttribute();
                $newDataNode->type($attributeType);
                $newDataNode->signature(new domSignature());

                $newDataNode->signature->name($attributeDataName);
                $newDataNode->signature->type($attributeDataType);
                $newDataNode->signature->extendedAttributes(parseExtendedAttributes($attributeExtendedAttributes));

                my $arrayRef = $dataNode->attributes;
                push(@$arrayRef, $newDataNode);

                print "  |  |>  Attribute; TYPE \"$attributeType\" DATA NAME \"$attributeDataName\" DATA TYPE \"$attributeDataType\" GET EXCEPTION? \"$getterException\" SET EXCEPTION? \"$setterException\"" .
                    dumpExtendedAttributes("\n  |                 ", $newDataNode->signature->extendedAttributes) . "\n" unless $beQuiet;

                $getterException =~ s/\s+//g;
                $setterException =~ s/\s+//g;
                @{$newDataNode->getterExceptions} = split(/,/, $getterException);
                @{$newDataNode->setterExceptions} = split(/,/, $setterException);
            } elsif (($line !~ s/^\s*$//g) and ($line !~ /^\s*const/)) {
                $line =~ /$IDLStructure::interfaceMethodSelector/ or die "Parsing error!\nSource:\n$line\n)";

                my $methodExtendedAttributes = (defined($1) ? $1 : " "); chop($methodExtendedAttributes);
                my $methodType = (defined($2) ? $2 : die("Parsing error!\nSource:\n$line\n)"));
                my $methodName = (defined($3) ? $3 : die("Parsing error!\nSource:\n$line\n)"));
                my $methodSignature = (defined($4) ? $4 : die("Parsing error!\nSource:\n$line\n)"));

                ('' =~ /^/); # Reset variables needed for regexp matching

                $line =~ /$IDLStructure::raisesSelector/;
                my $methodException = (defined($1) ? $1 : "");

                my $newDataNode = new domFunction();

                $newDataNode->signature(new domSignature());
                $newDataNode->signature->name($methodName);
                $newDataNode->signature->type($methodType);
                $newDataNode->signature->extendedAttributes(parseExtendedAttributes($methodExtendedAttributes));

                print "  |  |-  Method; TYPE \"$methodType\" NAME \"$methodName\" EXCEPTION? \"$methodException\"" .
                    dumpExtendedAttributes("\n  |              ", $newDataNode->signature->extendedAttributes) . "\n" unless $beQuiet;

                $methodException =~ s/\s+//g;
                @{$newDataNode->raisesExceptions} = split(/,/, $methodException);

                # Split arguments at commas but only if the comma
                # is not within attribute brackets, expressed here
                # as being followed by a ']' without a preceding '['.
                # Note that this assumes that attributes don't nest.
                my @params = split(/,(?![^[]*\])/, $methodSignature);
                foreach(@params) {
                    my $line = $_;

                    $line =~ /$IDLStructure::interfaceParameterSelector/;
                    my $paramDirection = $1;
                    my $paramExtendedAttributes = (defined($2) ? $2 : " "); chop($paramExtendedAttributes);
                    my $paramType = (defined($3) ? $3 : die("Parsing error!\nSource:\n$line\n)"));
                    my $paramName = (defined($4) ? $4 : die("Parsing error!\nSource:\n$line\n)"));

                    my $paramDataNode = new domSignature();
                    $paramDataNode->direction($paramDirection);
                    $paramDataNode->name($paramName);
                    $paramDataNode->type($paramType);
                    $paramDataNode->extendedAttributes(parseExtendedAttributes($paramExtendedAttributes));

                    my $arrayRef = $newDataNode->parameters;
                    push(@$arrayRef, $paramDataNode);

                    print "  |   |>  Param; TYPE \"$paramType\" NAME \"$paramName\"" . 
                        dumpExtendedAttributes("\n  |              ", $paramDataNode->extendedAttributes) . "\n" unless $beQuiet;          
                }

                my $arrayRef = $dataNode->functions;
                push(@$arrayRef, $newDataNode);
            } elsif ($line =~ /^\s*const/) {
                $line =~ /$IDLStructure::constantSelector/;
                my $constType = (defined($1) ? $1 : die("Parsing error!\nSource:\n$line\n)"));
                my $constName = (defined($2) ? $2 : die("Parsing error!\nSource:\n$line\n)"));
                my $constValue = (defined($3) ? $3 : die("Parsing error!\nSource:\n$line\n)"));

                my $newDataNode = new domConstant();
                $newDataNode->name($constName);
                $newDataNode->type($constType);
                $newDataNode->value($constValue);

                my $arrayRef = $dataNode->constants;
                push(@$arrayRef, $newDataNode);

                print "  |   |>  Constant; TYPE \"$constType\" NAME \"$constName\" VALUE \"$constValue\"\n" unless $beQuiet;
            }
        }

        print "  |----> Interface; NAME \"$interfaceName\"" .
            dumpExtendedAttributes("\n  |                 ", $dataNode->extendedAttributes) . "\n |-\n |\n" unless $beQuiet;
    }
}

# Internal helper
sub DetermineParseMode
{
    my $object = shift;  
    my $line = shift;

    my $mode = MODE_UNDEF;
    if ($_ =~ /module/) {
        $mode = MODE_MODULE;
    } elsif ($_ =~ /interface/) {
        $mode = MODE_INTERFACE;
    } elsif ($_ =~ /exception/) {
        $mode = MODE_EXCEPTION;
    } elsif ($_ =~ /(\A|\b)alias/) {
        # The (\A|\b) above is needed so we don't match attributes
        # whose names contain the substring "alias".
        $mode = MODE_ALIAS;
    }

    return $mode;
}

# Internal helper
sub ProcessSection
{
    my $object = shift;
  
    if ($parseMode eq MODE_MODULE) {
        die ("Two modules in one file! Fatal error!\n") if ($document ne 0);
        $document = new idlDocument();
        $object->ParseModule($document);
    } elsif ($parseMode eq MODE_INTERFACE) {
        my $node = new domClass();
        $object->ParseInterface($node, "interface");
    
        die ("No module specified! Fatal Error!\n") if ($document eq 0);
        my $arrayRef = $document->classes;
        push(@$arrayRef, $node);
    } elsif($parseMode eq MODE_EXCEPTION) {
        my $node = new domClass();
        $object->ParseInterface($node, "exception");

        die ("No module specified! Fatal Error!\n") if ($document eq 0);
        my $arrayRef = $document->classes;
        push(@$arrayRef, $node);
    } elsif($parseMode eq MODE_ALIAS) {
        print " |- Trying to parse alias...\n" unless $beQuiet;
    
        my $line = join("", @temporaryContent);
        $line =~ /$IDLStructure::aliasSelector/;

        my $interfaceName = (defined($1) ? $1 : die("Parsing error!\nSource:\n$line\n)"));
        my $wrapperName = (defined($2) ? $2 : die("Parsing error!\nSource:\n$line\n)"));
    
        print "  |----> Alias; INTERFACE \"$interfaceName\" WRAPPER \"$wrapperName\"\n |-\n |\n" unless $beQuiet;

        # FIXME: Check if alias is already in aliases
        my $aliases = $document->aliases;
        $aliases->{$interfaceName} = $wrapperName;
    }

    @temporaryContent = "";
}

1;