1 ########################
2 # IDL Parse::Yapp parser
3 # Copyright (C) Andrew Tridgell <tridge@samba.org>
4 # released under the GNU GPL version 2 or later
8 # the precedence actually doesn't matter at all for this grammar, but
9 # by providing a precedence we reduce the number of conflicts
11 %left '-' '+' '&' '|' '*' '>' '.' '/' '(' ')' '[' ',' ';'
19 | idl interface { push(@{$_[1]}, $_[2]); $_[1] }
20 | idl coclass { push(@{$_[1]}, $_[2]); $_[1] }
23 coclass: property_list 'coclass' identifier '{' interface_names '}' optional_semicolon
26 "PROPERTIES" => $_[1],
29 "FILE" => $_[0]->YYData->{INPUT_FILENAME},
30 "LINE" => $_[0]->YYData->{LINE},
36 | interface_names 'interface' identifier ';' { push(@{$_[1]}, $_[2]); $_[1] }
39 interface: property_list 'interface' identifier base_interface '{' definitions '}' optional_semicolon
41 "TYPE" => "INTERFACE",
42 "PROPERTIES" => $_[1],
46 "FILE" => $_[0]->YYData->{INPUT_FILENAME},
47 "LINE" => $_[0]->YYData->{LINE},
53 | ':' identifier { $_[2] }
57 definition { [ $_[1] ] }
58 | definitions definition { push(@{$_[1]}, $_[2]); $_[1] }
62 definition: function | const | typedef | declare | typedecl
65 const: 'const' identifier pointers identifier '=' anytext ';'
72 "FILE" => $_[0]->YYData->{INPUT_FILENAME},
73 "LINE" => $_[0]->YYData->{LINE},
75 | 'const' identifier pointers identifier array_len '=' anytext ';'
83 "FILE" => $_[0]->YYData->{INPUT_FILENAME},
84 "LINE" => $_[0]->YYData->{LINE},
89 function: property_list type identifier '(' element_list2 ')' ';'
93 "RETURN_TYPE" => $_[2],
94 "PROPERTIES" => $_[1],
96 "FILE" => $_[0]->YYData->{INPUT_FILENAME},
97 "LINE" => $_[0]->YYData->{LINE},
101 declare: 'declare' property_list decl_type identifier';'
104 "PROPERTIES" => $_[2],
107 "FILE" => $_[0]->YYData->{INPUT_FILENAME},
108 "LINE" => $_[0]->YYData->{LINE},
112 decl_type: decl_enum | decl_bitmap | decl_union
121 decl_bitmap: 'bitmap'
133 typedef: 'typedef' property_list type identifier array_len ';'
136 "PROPERTIES" => $_[2],
139 "ARRAY_LEN" => $_[5],
140 "FILE" => $_[0]->YYData->{INPUT_FILENAME},
141 "LINE" => $_[0]->YYData->{LINE},
145 usertype: struct | union | enum | bitmap;
147 typedecl: usertype ';' { $_[1] };
149 sign: 'signed' | 'unsigned';
152 | sign identifier { "$_[1] $_[2]" }
156 type: usertype | existingtype | void { "void" } ;
158 enum_body: '{' enum_elements '}' { $_[2] };
159 opt_enum_body: | enum_body;
160 enum: 'enum' optional_identifier opt_enum_body
169 enum_element { [ $_[1] ] }
170 | enum_elements ',' enum_element { push(@{$_[1]}, $_[3]); $_[1] }
173 enum_element: identifier
174 | identifier '=' anytext { "$_[1]$_[2]$_[3]" }
177 bitmap_body: '{' opt_bitmap_elements '}' { $_[2] };
178 opt_bitmap_body: | bitmap_body;
179 bitmap: 'bitmap' optional_identifier opt_bitmap_body
188 bitmap_element { [ $_[1] ] }
189 | bitmap_elements ',' bitmap_element { push(@{$_[1]}, $_[3]); $_[1] }
192 opt_bitmap_elements: | bitmap_elements;
194 bitmap_element: identifier '=' anytext { "$_[1] ( $_[3] )" }
197 struct_body: '{' element_list1 '}' { $_[2] };
198 opt_struct_body: | struct_body;
200 struct: 'struct' optional_identifier opt_struct_body
208 empty_element: property_list ';'
212 "PROPERTIES" => $_[1],
215 "FILE" => $_[0]->YYData->{INPUT_FILENAME},
216 "LINE" => $_[0]->YYData->{LINE},
220 base_or_empty: base_element ';' | empty_element;
222 optional_base_element:
223 property_list base_or_empty { $_[2]->{PROPERTIES} = FlattenHash([$_[1],$_[2]->{PROPERTIES}]); $_[2] }
228 | union_elements optional_base_element { push(@{$_[1]}, $_[2]); $_[1] }
231 union_body: '{' union_elements '}' { $_[2] };
232 opt_union_body: | union_body;
234 union: 'union' optional_identifier opt_union_body
242 base_element: property_list type pointers identifier array_len
246 "PROPERTIES" => $_[1],
248 "ARRAY_LEN" => $_[5],
249 "FILE" => $_[0]->YYData->{INPUT_FILENAME},
250 "LINE" => $_[0]->YYData->{LINE},
258 | pointers '*' { $_[1]+1 }
263 | element_list1 base_element ';' { push(@{$_[1]}, $_[2]); $_[1] }
269 | base_element { [ $_[1] ] }
270 | element_list2 ',' base_element { push(@{$_[1]}, $_[3]); $_[1] }
275 | '[' ']' array_len { push(@{$_[3]}, "*"); $_[3] }
276 | '[' anytext ']' array_len { push(@{$_[4]}, "$_[2]"); $_[4] }
282 | property_list '[' properties ']' { FlattenHash([$_[1],$_[3]]); }
285 properties: property { $_[1] }
286 | properties ',' property { FlattenHash([$_[1], $_[3]]); }
289 property: identifier {{ "$_[1]" => "1" }}
290 | identifier '(' listtext ')' {{ "$_[1]" => "$_[3]" }}
295 | listtext ',' anytext { "$_[1] $_[3]" }
300 | commalisttext ',' anytext { "$_[1],$_[3]" }
305 | identifier | constant | text
306 | anytext '-' anytext { "$_[1]$_[2]$_[3]" }
307 | anytext '.' anytext { "$_[1]$_[2]$_[3]" }
308 | anytext '*' anytext { "$_[1]$_[2]$_[3]" }
309 | anytext '>' anytext { "$_[1]$_[2]$_[3]" }
310 | anytext '<' anytext { "$_[1]$_[2]$_[3]" }
311 | anytext '|' anytext { "$_[1]$_[2]$_[3]" }
312 | anytext '&' anytext { "$_[1]$_[2]$_[3]" }
313 | anytext '/' anytext { "$_[1]$_[2]$_[3]" }
314 | anytext '?' anytext { "$_[1]$_[2]$_[3]" }
315 | anytext ':' anytext { "$_[1]$_[2]$_[3]" }
316 | anytext '=' anytext { "$_[1]$_[2]$_[3]" }
317 | anytext '+' anytext { "$_[1]$_[2]$_[3]" }
318 | anytext '~' anytext { "$_[1]$_[2]$_[3]" }
319 | anytext '(' commalisttext ')' anytext { "$_[1]$_[2]$_[3]$_[4]$_[5]" }
320 | anytext '{' commalisttext '}' anytext { "$_[1]$_[2]$_[3]$_[4]$_[5]" }
323 identifier: IDENTIFIER
334 text: TEXT { "\"$_[1]\"" }
343 #####################################
347 #####################################################################
348 # flatten an array of hashes into a single hash
354 for my $k (keys %{$d}) {
363 #####################################################################
364 # traverse a perl data structure removing any empty arrays or
365 # hashes and any hash elements that map to undef
370 return undef if (not defined($v));
371 if (ref($v) eq "ARRAY") {
372 foreach my $i (0 .. $#{$v}) {
374 if (ref($v->[$i]) eq "ARRAY" && $#{$v->[$i]}==-1) {
379 # this removes any undefined elements from the array
380 @{$v} = grep { defined $_ } @{$v};
381 } elsif (ref($v) eq "HASH") {
382 foreach my $x (keys %{$v}) {
384 if (!defined $v->{$x}) { delete($v->{$x}); next; }
385 if (ref($v->{$x}) eq "ARRAY" && $#{$v->{$x}}==-1) { delete($v->{$x}); next; }
392 if (exists $_[0]->YYData->{ERRMSG}) {
393 print $_[0]->YYData->{ERRMSG};
394 delete $_[0]->YYData->{ERRMSG};
397 my $line = $_[0]->YYData->{LINE};
398 my $last_token = $_[0]->YYData->{LAST_TOKEN};
399 my $file = $_[0]->YYData->{INPUT_FILENAME};
401 print "$file:$line: Syntax error near '$last_token'\n";
408 $parser->YYData->{INPUT} or return('',undef);
411 $parser->YYData->{INPUT} =~ s/^[ \t]*//;
413 for ($parser->YYData->{INPUT}) {
415 if (s/^\# (\d+) \"(.*?)\"( \d+|)//) {
416 $parser->YYData->{LINE} = $1-1;
417 $parser->YYData->{INPUT_FILENAME} = $2;
420 if (s/^\#line (\d+) \"(.*?)\"( \d+|)//) {
421 $parser->YYData->{LINE} = $1-1;
422 $parser->YYData->{INPUT_FILENAME} = $2;
430 $parser->YYData->{LINE}++;
433 if (s/^\"(.*?)\"//) {
434 $parser->YYData->{LAST_TOKEN} = $1;
437 if (s/^(\d+)(\W|$)/$2/) {
438 $parser->YYData->{LAST_TOKEN} = $1;
439 return('CONSTANT',$1);
442 $parser->YYData->{LAST_TOKEN} = $1;
444 /^(coclass|interface|const|typedef|declare|union
445 |struct|enum|bitmap|void|unsigned|signed)$/x) {
448 return('IDENTIFIER',$1);
451 $parser->YYData->{LAST_TOKEN} = $1;
459 my ($data,$filename) = @_;
461 my $self = new Parse::Pidl::IDL;
463 $self->YYData->{INPUT_FILENAME} = $filename;
464 $self->YYData->{INPUT} = $data;
465 $self->YYData->{LINE} = 0;
466 $self->YYData->{LAST_TOKEN} = "NONE";
468 my $idl = $self->YYParse( yylex => \&_Lexer, yyerror => \&_Error );
470 return CleanData($idl);
475 my ($filename,$incdirs) = @_;
477 my $saved_delim = $/;
480 if (! defined $cpp) {
483 my $includes = map { " -I$_" } @$incdirs;
484 my $data = `$cpp -D__PIDL__$includes -xc $filename`;
487 return parse_string($data, $filename);