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 identifier '=' anytext ';'
71 "FILE" => $_[0]->YYData->{INPUT_FILENAME},
72 "LINE" => $_[0]->YYData->{LINE},
74 | 'const' identifier identifier array_len '=' anytext ';'
81 "FILE" => $_[0]->YYData->{INPUT_FILENAME},
82 "LINE" => $_[0]->YYData->{LINE},
87 function: property_list type identifier '(' element_list2 ')' ';'
91 "RETURN_TYPE" => $_[2],
92 "PROPERTIES" => $_[1],
94 "FILE" => $_[0]->YYData->{INPUT_FILENAME},
95 "LINE" => $_[0]->YYData->{LINE},
99 declare: 'declare' property_list decl_type identifier';'
102 "PROPERTIES" => $_[2],
105 "FILE" => $_[0]->YYData->{INPUT_FILENAME},
106 "LINE" => $_[0]->YYData->{LINE},
110 decl_type: decl_enum | decl_bitmap
119 decl_bitmap: 'bitmap'
125 typedef: 'typedef' property_list type identifier array_len ';'
128 "PROPERTIES" => $_[2],
131 "ARRAY_LEN" => $_[5],
132 "FILE" => $_[0]->YYData->{INPUT_FILENAME},
133 "LINE" => $_[0]->YYData->{LINE},
137 usertype: struct | union | enum | bitmap;
139 typedecl: usertype ';' { $_[1] };
141 type: usertype | identifier
145 enum: 'enum' optional_identifier '{' enum_elements '}'
154 enum_element { [ $_[1] ] }
155 | enum_elements ',' enum_element { push(@{$_[1]}, $_[3]); $_[1] }
158 enum_element: identifier
159 | identifier '=' anytext { "$_[1]$_[2]$_[3]" }
162 bitmap: 'bitmap' optional_identifier '{' bitmap_elements '}'
171 bitmap_element { [ $_[1] ] }
172 | bitmap_elements ',' bitmap_element { push(@{$_[1]}, $_[3]); $_[1] }
175 bitmap_element: identifier '=' anytext { "$_[1] ( $_[3] )" }
178 struct: 'struct' optional_identifier '{' element_list1 '}'
186 empty_element: property_list ';'
190 "PROPERTIES" => $_[1],
193 "FILE" => $_[0]->YYData->{INPUT_FILENAME},
194 "LINE" => $_[0]->YYData->{LINE},
198 base_or_empty: base_element ';' | empty_element;
200 optional_base_element:
201 property_list base_or_empty { $_[2]->{PROPERTIES} = Parse::Pidl::Util::FlattenHash([$_[1],$_[2]->{PROPERTIES}]); $_[2] }
206 | union_elements optional_base_element { push(@{$_[1]}, $_[2]); $_[1] }
209 union: 'union' optional_identifier '{' union_elements '}'
217 base_element: property_list type pointers identifier array_len
221 "PROPERTIES" => $_[1],
223 "ARRAY_LEN" => $_[5],
224 "FILE" => $_[0]->YYData->{INPUT_FILENAME},
225 "LINE" => $_[0]->YYData->{LINE},
233 | pointers '*' { $_[1]+1 }
238 | element_list1 base_element ';' { push(@{$_[1]}, $_[2]); $_[1] }
244 | base_element { [ $_[1] ] }
245 | element_list2 ',' base_element { push(@{$_[1]}, $_[3]); $_[1] }
250 | '[' ']' array_len { push(@{$_[3]}, "*"); $_[3] }
251 | '[' anytext ']' array_len { push(@{$_[4]}, "$_[2]"); $_[4] }
257 | property_list '[' properties ']' { Parse::Pidl::Util::FlattenHash([$_[1],$_[3]]); }
260 properties: property { $_[1] }
261 | properties ',' property { Parse::Pidl::Util::FlattenHash([$_[1], $_[3]]); }
264 property: identifier {{ "$_[1]" => "1" }}
265 | identifier '(' listtext ')' {{ "$_[1]" => "$_[3]" }}
270 | listtext ',' anytext { "$_[1] $_[3]" }
275 | commalisttext ',' anytext { "$_[1],$_[3]" }
280 | identifier | constant | text
281 | anytext '-' anytext { "$_[1]$_[2]$_[3]" }
282 | anytext '.' anytext { "$_[1]$_[2]$_[3]" }
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 '(' commalisttext ')' anytext { "$_[1]$_[2]$_[3]$_[4]$_[5]" }
292 | anytext '{' commalisttext '}' anytext { "$_[1]$_[2]$_[3]$_[4]$_[5]" }
295 identifier: IDENTIFIER
306 text: TEXT { "\"$_[1]\"" }
315 #####################################
319 use Parse::Pidl::Util;
321 #####################################################################
322 # traverse a perl data structure removing any empty arrays or
323 # hashes and any hash elements that map to undef
328 if (ref($v) eq "ARRAY") {
329 foreach my $i (0 .. $#{$v}) {
331 if (ref($v->[$i]) eq "ARRAY" && $#{$v->[$i]}==-1) {
336 # this removes any undefined elements from the array
337 @{$v} = grep { defined $_ } @{$v};
338 } elsif (ref($v) eq "HASH") {
339 foreach my $x (keys %{$v}) {
341 if (!defined $v->{$x}) { delete($v->{$x}); next; }
342 if (ref($v->{$x}) eq "ARRAY" && $#{$v->{$x}}==-1) { delete($v->{$x}); next; }
349 if (exists $_[0]->YYData->{ERRMSG}) {
350 print $_[0]->YYData->{ERRMSG};
351 delete $_[0]->YYData->{ERRMSG};
354 my $line = $_[0]->YYData->{LINE};
355 my $last_token = $_[0]->YYData->{LAST_TOKEN};
356 my $file = $_[0]->YYData->{INPUT_FILENAME};
358 print "$file:$line: Syntax error near '$last_token'\n";
365 $parser->YYData->{INPUT} or return('',undef);
368 $parser->YYData->{INPUT} =~ s/^[ \t]*//;
370 for ($parser->YYData->{INPUT}) {
372 if (s/^\# (\d+) \"(.*?)\"( \d+|)//) {
373 $parser->YYData->{LINE} = $1-1;
374 $parser->YYData->{INPUT_FILENAME} = $2;
377 if (s/^\#line (\d+) \"(.*?)\"( \d+|)//) {
378 $parser->YYData->{LINE} = $1-1;
379 $parser->YYData->{INPUT_FILENAME} = $2;
387 $parser->YYData->{LINE}++;
390 if (s/^\"(.*?)\"//) {
391 $parser->YYData->{LAST_TOKEN} = $1;
394 if (s/^(\d+)(\W|$)/$2/) {
395 $parser->YYData->{LAST_TOKEN} = $1;
396 return('CONSTANT',$1);
399 $parser->YYData->{LAST_TOKEN} = $1;
401 /^(coclass|interface|const|typedef|declare|union
402 |struct|enum|bitmap|void)$/x) {
405 return('IDENTIFIER',$1);
408 $parser->YYData->{LAST_TOKEN} = $1;
416 my ($self,$filename) = @_;
418 my $saved_delim = $/;
421 if (! defined $cpp) {
424 my $data = `$cpp -D__PIDL__ -xc $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);