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 '=' anytext { "$_[1]$_[2]$_[3]" }
292 | anytext '+' anytext { "$_[1]$_[2]$_[3]" }
293 | anytext '~' anytext { "$_[1]$_[2]$_[3]" }
294 | anytext '(' commalisttext ')' anytext { "$_[1]$_[2]$_[3]$_[4]$_[5]" }
295 | anytext '{' commalisttext '}' anytext { "$_[1]$_[2]$_[3]$_[4]$_[5]" }
298 identifier: IDENTIFIER
309 text: TEXT { "\"$_[1]\"" }
318 #####################################
322 use config qw(%config);
323 use Parse::Pidl::Util;
325 #####################################################################
326 # traverse a perl data structure removing any empty arrays or
327 # hashes and any hash elements that map to undef
332 if (ref($v) eq "ARRAY") {
333 foreach my $i (0 .. $#{$v}) {
335 if (ref($v->[$i]) eq "ARRAY" && $#{$v->[$i]}==-1) {
340 # this removes any undefined elements from the array
341 @{$v} = grep { defined $_ } @{$v};
342 } elsif (ref($v) eq "HASH") {
343 foreach my $x (keys %{$v}) {
345 if (!defined $v->{$x}) { delete($v->{$x}); next; }
346 if (ref($v->{$x}) eq "ARRAY" && $#{$v->{$x}}==-1) { delete($v->{$x}); next; }
353 if (exists $_[0]->YYData->{ERRMSG}) {
354 print $_[0]->YYData->{ERRMSG};
355 delete $_[0]->YYData->{ERRMSG};
358 my $line = $_[0]->YYData->{LINE};
359 my $last_token = $_[0]->YYData->{LAST_TOKEN};
360 my $file = $_[0]->YYData->{INPUT_FILENAME};
362 print "$file:$line: Syntax error near '$last_token'\n";
369 $parser->YYData->{INPUT} or return('',undef);
372 $parser->YYData->{INPUT} =~ s/^[ \t]*//;
374 for ($parser->YYData->{INPUT}) {
376 if (s/^\# (\d+) \"(.*?)\"( \d+|)//) {
377 $parser->YYData->{LINE} = $1-1;
378 $parser->YYData->{INPUT_FILENAME} = $2;
381 if (s/^\#line (\d+) \"(.*?)\"( \d+|)//) {
382 $parser->YYData->{LINE} = $1-1;
383 $parser->YYData->{INPUT_FILENAME} = $2;
391 $parser->YYData->{LINE}++;
394 if (s/^\"(.*?)\"//) {
395 $parser->YYData->{LAST_TOKEN} = $1;
398 if (s/^(\d+)(\W|$)/$2/) {
399 $parser->YYData->{LAST_TOKEN} = $1;
400 return('CONSTANT',$1);
403 $parser->YYData->{LAST_TOKEN} = $1;
405 /^(coclass|interface|const|typedef|declare|union
406 |struct|enum|bitmap|void)$/x) {
409 return('IDENTIFIER',$1);
412 $parser->YYData->{LAST_TOKEN} = $1;
420 my ($self,$filename) = @_;
422 my $saved_delim = $/;
425 if (! defined $cpp) {
426 $cpp = $config::config{'CPP'};
428 my $data = `$cpp -D__PIDL__ -xc $filename`;
431 $self->YYData->{INPUT} = $data;
432 $self->YYData->{LINE} = 0;
433 $self->YYData->{LAST_TOKEN} = "NONE";
435 my $idl = $self->YYParse( yylex => \&_Lexer, yyerror => \&_Error );
437 return CleanData($idl);