r15099: An attempt to fix BSD make portability issues. With these changes Samba 4...
[Samba.git] / source4 / pidl / idl.yp
blob62f636cb00469e22c677d3bab19fc91f94a08dae
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
10 # enormously
11 %left   '-' '+' '&' '|' '*' '>' '.' '/' '(' ')' '[' ',' ';'
14 ################
15 # grammar
17 idl: 
18         #empty  { {} }
19         | idl interface { push(@{$_[1]}, $_[2]); $_[1] }
20         | idl coclass { push(@{$_[1]}, $_[2]); $_[1] }
23 coclass: property_list 'coclass' identifier '{' interface_names '}' optional_semicolon
24           {$_[3] => {
25                "TYPE" => "COCLASS", 
26                "PROPERTIES" => $_[1],
27                "NAME" => $_[3],
28                "DATA" => $_[5],
29                    "FILE" => $_[0]->YYData->{INPUT_FILENAME},
30                    "LINE" => $_[0]->YYData->{LINE},
31           }}
34 interface_names:
35         #empty { {} }
36         | interface_names 'interface' identifier ';' { push(@{$_[1]}, $_[2]); $_[1] }
39 interface: property_list 'interface' identifier base_interface '{' definitions '}' optional_semicolon
40           {$_[3] => {
41                "TYPE" => "INTERFACE", 
42                "PROPERTIES" => $_[1],
43                "NAME" => $_[3],
44                "BASE" => $_[4],
45                "DATA" => $_[6],
46                    "FILE" => $_[0]->YYData->{INPUT_FILENAME},
47                    "LINE" => $_[0]->YYData->{LINE},
48           }}
51 base_interface:
52         #empty
53         | ':' identifier { $_[2] }
56 definitions: 
57       definition              { [ $_[1] ] }    
58     | definitions definition  { push(@{$_[1]}, $_[2]); $_[1] }
59 ;    
62 definition: function | const | typedef | declare | typedecl
65 const: 'const' identifier pointers identifier '=' anytext ';' 
66         {{
67                      "TYPE"  => "CONST", 
68                      "DTYPE"  => $_[2],
69                          "POINTERS" => $_[3],
70                      "NAME"  => $_[4],
71                      "VALUE" => $_[6],
72                      "FILE" => $_[0]->YYData->{INPUT_FILENAME},
73                      "LINE" => $_[0]->YYData->{LINE},
74         }}
75         | 'const' identifier pointers identifier array_len '=' anytext ';' 
76         {{
77                      "TYPE"  => "CONST", 
78                      "DTYPE"  => $_[2],
79                          "POINTERS" => $_[3],
80                      "NAME"  => $_[4],
81                      "ARRAY_LEN" => $_[5],
82                      "VALUE" => $_[7],
83                      "FILE" => $_[0]->YYData->{INPUT_FILENAME},
84                      "LINE" => $_[0]->YYData->{LINE},
85         }}
89 function: property_list type identifier '(' element_list2 ')' ';' 
90          {{
91                 "TYPE" => "FUNCTION",
92                 "NAME" => $_[3],
93                 "RETURN_TYPE" => $_[2],
94                 "PROPERTIES" => $_[1],
95                 "ELEMENTS" => $_[5],
96                 "FILE" => $_[0]->YYData->{INPUT_FILENAME},
97                 "LINE" => $_[0]->YYData->{LINE},
98           }}
101 declare: 'declare' property_list decl_type identifier';' 
102         {{
103                      "TYPE" => "DECLARE", 
104                      "PROPERTIES" => $_[2],
105                      "NAME" => $_[4],
106                      "DATA" => $_[3],
107                      "FILE" => $_[0]->YYData->{INPUT_FILENAME},
108                      "LINE" => $_[0]->YYData->{LINE},
109         }}
112 decl_type: decl_enum | decl_bitmap
115 decl_enum: 'enum' 
116         {{
117                      "TYPE" => "ENUM"
118         }}
121 decl_bitmap: 'bitmap' 
122         {{
123                      "TYPE" => "BITMAP"
124         }}
127 typedef: 'typedef' property_list type identifier array_len ';' 
128         {{
129                      "TYPE" => "TYPEDEF", 
130                      "PROPERTIES" => $_[2],
131                      "NAME" => $_[4],
132                      "DATA" => $_[3],
133                      "ARRAY_LEN" => $_[5],
134                      "FILE" => $_[0]->YYData->{INPUT_FILENAME},
135                      "LINE" => $_[0]->YYData->{LINE},
136         }}
139 usertype: struct | union | enum | bitmap;
141 typedecl: usertype ';' { $_[1] };
143 sign: 'signed' | 'unsigned';
145 existingtype: 
146         | sign identifier { "$_[1] $_[2]" }
147         | identifier 
150 type: usertype | existingtype | void { "void" } ;
152 enum_body: '{' enum_elements '}' { $_[2] };
153 opt_enum_body: | enum_body;
154 enum: 'enum' optional_identifier opt_enum_body
155         {{
156              "TYPE" => "ENUM", 
157                          "NAME" => $_[2],
158                      "ELEMENTS" => $_[3]
159         }}
162 enum_elements: 
163       enum_element                    { [ $_[1] ] }            
164     | enum_elements ',' enum_element  { push(@{$_[1]}, $_[3]); $_[1] }
167 enum_element: identifier 
168               | identifier '=' anytext { "$_[1]$_[2]$_[3]" }
171 bitmap_body: '{' bitmap_elements '}' { $_[2] };
172 opt_bitmap_body: | bitmap_body;
173 bitmap: 'bitmap' optional_identifier opt_bitmap_body
174         {{
175              "TYPE" => "BITMAP", 
176                          "NAME" => $_[2],
177                      "ELEMENTS" => $_[3]
178         }}
181 bitmap_elements: 
182       bitmap_element                    { [ $_[1] ] }            
183     | bitmap_elements ',' bitmap_element  { push(@{$_[1]}, $_[3]); $_[1] }
186 bitmap_element: identifier '=' anytext { "$_[1] ( $_[3] )" }
189 struct_body: '{' element_list1 '}' { $_[2] };
190 opt_struct_body: | struct_body;
192 struct: 'struct' optional_identifier opt_struct_body
193         {{
194              "TYPE" => "STRUCT", 
195                          "NAME" => $_[2],
196                      "ELEMENTS" => $_[3]
197         }}
200 empty_element: property_list ';'
201         {{
202                  "NAME" => "",
203                  "TYPE" => "EMPTY",
204                  "PROPERTIES" => $_[1],
205                  "POINTERS" => 0,
206                  "ARRAY_LEN" => [],
207                  "FILE" => $_[0]->YYData->{INPUT_FILENAME},
208                  "LINE" => $_[0]->YYData->{LINE},
209          }}
212 base_or_empty: base_element ';' | empty_element;
214 optional_base_element:
215         property_list base_or_empty { $_[2]->{PROPERTIES} = FlattenHash([$_[1],$_[2]->{PROPERTIES}]); $_[2] }
218 union_elements: 
219     #empty
220     | union_elements optional_base_element { push(@{$_[1]}, $_[2]); $_[1] }
223 union_body: '{' union_elements '}' { $_[2] };
224 opt_union_body: | union_body;
226 union: 'union' optional_identifier opt_union_body
227         {{
228              "TYPE" => "UNION", 
229                      "NAME" => $_[2],
230                      "ELEMENTS" => $_[3]
231         }}
234 base_element: property_list type pointers identifier array_len
235               {{
236                            "NAME" => $_[4],
237                            "TYPE" => $_[2],
238                            "PROPERTIES" => $_[1],
239                            "POINTERS" => $_[3],
240                            "ARRAY_LEN" => $_[5],
241                        "FILE" => $_[0]->YYData->{INPUT_FILENAME},
242                        "LINE" => $_[0]->YYData->{LINE},
243               }}
247 pointers: 
248   #empty            
249    { 0 }
250     | pointers '*'  { $_[1]+1 }
253 element_list1: 
254     #empty
255     | element_list1 base_element ';' { push(@{$_[1]}, $_[2]); $_[1] }
258 element_list2: 
259     #empty
260     | 'void' 
261     | base_element { [ $_[1] ] }
262     | element_list2 ',' base_element { push(@{$_[1]}, $_[3]); $_[1] }
265 array_len: 
266     #empty                        { [] }
267     | '[' ']' array_len           { push(@{$_[3]}, "*"); $_[3] }
268     | '[' anytext ']' array_len   { push(@{$_[4]}, "$_[2]"); $_[4] }
272 property_list: 
273     #empty
274     | property_list '[' properties ']' { FlattenHash([$_[1],$_[3]]); }
277 properties: property          { $_[1] }
278     | properties ',' property { FlattenHash([$_[1], $_[3]]); }
281 property: identifier                   {{ "$_[1]" => "1"     }}
282           | identifier '(' listtext ')' {{ "$_[1]" => "$_[3]" }}
285 listtext:
286     anytext 
287     | listtext ',' anytext { "$_[1] $_[3]" }
290 commalisttext:
291     anytext 
292     | commalisttext ',' anytext { "$_[1],$_[3]" }
295 anytext:  #empty
296     { "" }
297     | identifier | constant | text
298     | anytext '-' anytext  { "$_[1]$_[2]$_[3]" }
299     | anytext '.' anytext  { "$_[1]$_[2]$_[3]" }
300     | anytext '*' anytext  { "$_[1]$_[2]$_[3]" }
301     | anytext '>' anytext  { "$_[1]$_[2]$_[3]" }
302     | anytext '<' anytext  { "$_[1]$_[2]$_[3]" }
303     | anytext '|' anytext  { "$_[1]$_[2]$_[3]" }
304     | anytext '&' anytext  { "$_[1]$_[2]$_[3]" }
305     | anytext '/' anytext  { "$_[1]$_[2]$_[3]" }
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 '(' commalisttext ')' anytext  { "$_[1]$_[2]$_[3]$_[4]$_[5]" }
312     | anytext '{' commalisttext '}' anytext  { "$_[1]$_[2]$_[3]$_[4]$_[5]" }
315 identifier: IDENTIFIER
318 optional_identifier: 
319         IDENTIFIER
320    | #empty { undef }
323 constant: CONSTANT
326 text: TEXT { "\"$_[1]\"" }
329 optional_semicolon: 
330         #empty
331         | ';'
335 #####################################
336 # start code
339 #####################################################################
340 # flatten an array of hashes into a single hash
341 sub FlattenHash($) 
343     my $a = shift;
344     my %b;
345     for my $d (@{$a}) {
346         for my $k (keys %{$d}) {
347             $b{$k} = $d->{$k};
348         }
349     }
350     return \%b;
355 #####################################################################
356 # traverse a perl data structure removing any empty arrays or
357 # hashes and any hash elements that map to undef
358 sub CleanData($)
360     sub CleanData($);
361     my($v) = shift;
362         return undef if (not defined($v));
363     if (ref($v) eq "ARRAY") {
364         foreach my $i (0 .. $#{$v}) {
365             CleanData($v->[$i]);
366             if (ref($v->[$i]) eq "ARRAY" && $#{$v->[$i]}==-1) { 
367                     $v->[$i] = undef; 
368                     next; 
369             }
370         }
371         # this removes any undefined elements from the array
372         @{$v} = grep { defined $_ } @{$v};
373     } elsif (ref($v) eq "HASH") {
374         foreach my $x (keys %{$v}) {
375             CleanData($v->{$x});
376             if (!defined $v->{$x}) { delete($v->{$x}); next; }
377             if (ref($v->{$x}) eq "ARRAY" && $#{$v->{$x}}==-1) { delete($v->{$x}); next; }
378         }
379     }
380         return $v;
383 sub _Error {
384     if (exists $_[0]->YYData->{ERRMSG}) {
385                 print $_[0]->YYData->{ERRMSG};
386                 delete $_[0]->YYData->{ERRMSG};
387                 return;
388         };
389         my $line = $_[0]->YYData->{LINE};
390         my $last_token = $_[0]->YYData->{LAST_TOKEN};
391         my $file = $_[0]->YYData->{INPUT_FILENAME};
392         
393         print "$file:$line: Syntax error near '$last_token'\n";
396 sub _Lexer($)
398         my($parser)=shift;
400     $parser->YYData->{INPUT} or return('',undef);
402 again:
403         $parser->YYData->{INPUT} =~ s/^[ \t]*//;
405         for ($parser->YYData->{INPUT}) {
406                 if (/^\#/) {
407                         if (s/^\# (\d+) \"(.*?)\"( \d+|)//) {
408                                 $parser->YYData->{LINE} = $1-1;
409                                 $parser->YYData->{INPUT_FILENAME} = $2;
410                                 goto again;
411                         }
412                         if (s/^\#line (\d+) \"(.*?)\"( \d+|)//) {
413                                 $parser->YYData->{LINE} = $1-1;
414                                 $parser->YYData->{INPUT_FILENAME} = $2;
415                                 goto again;
416                         }
417                         if (s/^(\#.*)$//m) {
418                                 goto again;
419                         }
420                 }
421                 if (s/^(\n)//) {
422                         $parser->YYData->{LINE}++;
423                         goto again;
424                 }
425                 if (s/^\"(.*?)\"//) {
426                         $parser->YYData->{LAST_TOKEN} = $1;
427                         return('TEXT',$1); 
428                 }
429                 if (s/^(\d+)(\W|$)/$2/) {
430                         $parser->YYData->{LAST_TOKEN} = $1;
431                         return('CONSTANT',$1); 
432                 }
433                 if (s/^([\w_]+)//) {
434                         $parser->YYData->{LAST_TOKEN} = $1;
435                         if ($1 =~ 
436                             /^(coclass|interface|const|typedef|declare|union
437                               |struct|enum|bitmap|void|unsigned|signed)$/x) {
438                                 return $1;
439                         }
440                         return('IDENTIFIER',$1);
441                 }
442                 if (s/^(.)//s) {
443                         $parser->YYData->{LAST_TOKEN} = $1;
444                         return($1,$1);
445                 }
446         }
449 sub parse_string
451         my ($data,$filename) = @_;
453         my $self = new Parse::Pidl::IDL;
455     $self->YYData->{INPUT_FILENAME} = $filename;
456     $self->YYData->{INPUT} = $data;
457     $self->YYData->{LINE} = 0;
458     $self->YYData->{LAST_TOKEN} = "NONE";
460         my $idl = $self->YYParse( yylex => \&_Lexer, yyerror => \&_Error );
462         return CleanData($idl);
465 sub parse_file($)
467         my ($filename) = @_;
469         my $saved_delim = $/;
470         undef $/;
471         my $cpp = $ENV{CPP};
472         if (! defined $cpp) {
473                 $cpp = "cpp";
474         }
475         my $data = `$cpp -D__PIDL__ -xc $filename`;
476         $/ = $saved_delim;
478         return parse_string($data, $filename);