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
121 decl_bitmap: 'bitmap'
127 typedef: 'typedef' property_list type identifier array_len ';'
130 "PROPERTIES" => $_[2],
133 "ARRAY_LEN" => $_[5],
134 "FILE" => $_[0]->YYData->{INPUT_FILENAME},
135 "LINE" => $_[0]->YYData->{LINE},
139 usertype: struct | union | enum | bitmap;
141 typedecl: usertype ';' { $_[1] };
143 type: usertype | identifier
147 enum: 'enum' optional_identifier '{' enum_elements '}'
156 enum_element { [ $_[1] ] }
157 | enum_elements ',' enum_element { push(@{$_[1]}, $_[3]); $_[1] }
160 enum_element: identifier
161 | identifier '=' anytext { "$_[1]$_[2]$_[3]" }
164 bitmap: 'bitmap' optional_identifier '{' bitmap_elements '}'
173 bitmap_element { [ $_[1] ] }
174 | bitmap_elements ',' bitmap_element { push(@{$_[1]}, $_[3]); $_[1] }
177 bitmap_element: identifier '=' anytext { "$_[1] ( $_[3] )" }
180 struct: 'struct' optional_identifier '{' element_list1 '}'
188 empty_element: property_list ';'
192 "PROPERTIES" => $_[1],
195 "FILE" => $_[0]->YYData->{INPUT_FILENAME},
196 "LINE" => $_[0]->YYData->{LINE},
200 base_or_empty: base_element ';' | empty_element;
202 optional_base_element:
203 property_list base_or_empty { $_[2]->{PROPERTIES} = Parse::Pidl::Util::FlattenHash([$_[1],$_[2]->{PROPERTIES}]); $_[2] }
208 | union_elements optional_base_element { push(@{$_[1]}, $_[2]); $_[1] }
211 union: 'union' optional_identifier '{' union_elements '}'
219 base_element: property_list type pointers identifier array_len
223 "PROPERTIES" => $_[1],
225 "ARRAY_LEN" => $_[5],
226 "FILE" => $_[0]->YYData->{INPUT_FILENAME},
227 "LINE" => $_[0]->YYData->{LINE},
235 | pointers '*' { $_[1]+1 }
240 | element_list1 base_element ';' { push(@{$_[1]}, $_[2]); $_[1] }
246 | base_element { [ $_[1] ] }
247 | element_list2 ',' base_element { push(@{$_[1]}, $_[3]); $_[1] }
252 | '[' ']' array_len { push(@{$_[3]}, "*"); $_[3] }
253 | '[' anytext ']' array_len { push(@{$_[4]}, "$_[2]"); $_[4] }
259 | property_list '[' properties ']' { Parse::Pidl::Util::FlattenHash([$_[1],$_[3]]); }
262 properties: property { $_[1] }
263 | properties ',' property { Parse::Pidl::Util::FlattenHash([$_[1], $_[3]]); }
266 property: identifier {{ "$_[1]" => "1" }}
267 | identifier '(' listtext ')' {{ "$_[1]" => "$_[3]" }}
272 | listtext ',' anytext { "$_[1] $_[3]" }
277 | commalisttext ',' anytext { "$_[1],$_[3]" }
282 | identifier | constant | text
283 | anytext '-' anytext { "$_[1]$_[2]$_[3]" }
284 | anytext '.' anytext { "$_[1]$_[2]$_[3]" }
285 | anytext '*' anytext { "$_[1]$_[2]$_[3]" }
286 | anytext '>' anytext { "$_[1]$_[2]$_[3]" }
287 | anytext '<' anytext { "$_[1]$_[2]$_[3]" }
288 | anytext '|' anytext { "$_[1]$_[2]$_[3]" }
289 | anytext '&' anytext { "$_[1]$_[2]$_[3]" }
290 | anytext '/' anytext { "$_[1]$_[2]$_[3]" }
291 | anytext '?' anytext { "$_[1]$_[2]$_[3]" }
292 | anytext ':' anytext { "$_[1]$_[2]$_[3]" }
293 | anytext '=' anytext { "$_[1]$_[2]$_[3]" }
294 | anytext '+' anytext { "$_[1]$_[2]$_[3]" }
295 | anytext '~' anytext { "$_[1]$_[2]$_[3]" }
296 | anytext '(' commalisttext ')' anytext { "$_[1]$_[2]$_[3]$_[4]$_[5]" }
297 | anytext '{' commalisttext '}' anytext { "$_[1]$_[2]$_[3]$_[4]$_[5]" }
300 identifier: IDENTIFIER
311 text: TEXT { "\"$_[1]\"" }
320 #####################################
324 use Parse::Pidl::Util;
326 #####################################################################
327 # traverse a perl data structure removing any empty arrays or
328 # hashes and any hash elements that map to undef
333 return undef if (not defined($v));
334 if (ref($v) eq "ARRAY") {
335 foreach my $i (0 .. $#{$v}) {
337 if (ref($v->[$i]) eq "ARRAY" && $#{$v->[$i]}==-1) {
342 # this removes any undefined elements from the array
343 @{$v} = grep { defined $_ } @{$v};
344 } elsif (ref($v) eq "HASH") {
345 foreach my $x (keys %{$v}) {
347 if (!defined $v->{$x}) { delete($v->{$x}); next; }
348 if (ref($v->{$x}) eq "ARRAY" && $#{$v->{$x}}==-1) { delete($v->{$x}); next; }
355 if (exists $_[0]->YYData->{ERRMSG}) {
356 print $_[0]->YYData->{ERRMSG};
357 delete $_[0]->YYData->{ERRMSG};
360 my $line = $_[0]->YYData->{LINE};
361 my $last_token = $_[0]->YYData->{LAST_TOKEN};
362 my $file = $_[0]->YYData->{INPUT_FILENAME};
364 print "$file:$line: Syntax error near '$last_token'\n";
371 $parser->YYData->{INPUT} or return('',undef);
374 $parser->YYData->{INPUT} =~ s/^[ \t]*//;
376 for ($parser->YYData->{INPUT}) {
378 if (s/^\# (\d+) \"(.*?)\"( \d+|)//) {
379 $parser->YYData->{LINE} = $1-1;
380 $parser->YYData->{INPUT_FILENAME} = $2;
383 if (s/^\#line (\d+) \"(.*?)\"( \d+|)//) {
384 $parser->YYData->{LINE} = $1-1;
385 $parser->YYData->{INPUT_FILENAME} = $2;
393 $parser->YYData->{LINE}++;
396 if (s/^\"(.*?)\"//) {
397 $parser->YYData->{LAST_TOKEN} = $1;
400 if (s/^(\d+)(\W|$)/$2/) {
401 $parser->YYData->{LAST_TOKEN} = $1;
402 return('CONSTANT',$1);
405 $parser->YYData->{LAST_TOKEN} = $1;
407 /^(coclass|interface|const|typedef|declare|union
408 |struct|enum|bitmap|void)$/x) {
411 return('IDENTIFIER',$1);
414 $parser->YYData->{LAST_TOKEN} = $1;
422 my ($data,$filename) = @_;
424 my $self = new Parse::Pidl::IDL;
426 $self->YYData->{INPUT_FILENAME} = $filename;
427 $self->YYData->{INPUT} = $data;
428 $self->YYData->{LINE} = 0;
429 $self->YYData->{LAST_TOKEN} = "NONE";
431 my $idl = $self->YYParse( yylex => \&_Lexer, yyerror => \&_Error );
433 return CleanData($idl);
440 my $saved_delim = $/;
443 if (! defined $cpp) {
446 my $data = `$cpp -D__PIDL__ -xc $filename`;
449 return parse_string($data, $filename);