r10071: Configure checks for IRIX build environment. Test whether we can
[Samba.git] / source / pidl / idl.yp
blob0f5b17e0fa0a6cae2186280512a694bfa1523c9b
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 identifier '=' anytext ';' 
66         {{
67                      "TYPE"  => "CONST", 
68                      "DTYPE"  => $_[2],
69                      "NAME"  => $_[3],
70                      "VALUE" => $_[5],
71                      "FILE" => $_[0]->YYData->{INPUT_FILENAME},
72                      "LINE" => $_[0]->YYData->{LINE},
73         }}
74         | 'const' identifier identifier array_len '=' anytext ';' 
75         {{
76                      "TYPE"  => "CONST", 
77                      "DTYPE"  => $_[2],
78                      "NAME"  => $_[3],
79                      "ARRAY_LEN" => $_[4],
80                      "VALUE" => $_[6],
81                      "FILE" => $_[0]->YYData->{INPUT_FILENAME},
82                      "LINE" => $_[0]->YYData->{LINE},
83         }}
87 function: property_list type identifier '(' element_list2 ')' ';' 
88          {{
89                 "TYPE" => "FUNCTION",
90                 "NAME" => $_[3],
91                 "RETURN_TYPE" => $_[2],
92                 "PROPERTIES" => $_[1],
93                 "ELEMENTS" => $_[5],
94                 "FILE" => $_[0]->YYData->{INPUT_FILENAME},
95                 "LINE" => $_[0]->YYData->{LINE},
96           }}
99 declare: 'declare' property_list decl_type identifier';' 
100         {{
101                      "TYPE" => "DECLARE", 
102                      "PROPERTIES" => $_[2],
103                      "NAME" => $_[4],
104                      "DATA" => $_[3],
105                      "FILE" => $_[0]->YYData->{INPUT_FILENAME},
106                      "LINE" => $_[0]->YYData->{LINE},
107         }}
110 decl_type: decl_enum | decl_bitmap
113 decl_enum: 'enum' 
114         {{
115                      "TYPE" => "ENUM"
116         }}
119 decl_bitmap: 'bitmap' 
120         {{
121                      "TYPE" => "BITMAP"
122         }}
125 typedef: 'typedef' property_list type identifier array_len ';' 
126         {{
127                      "TYPE" => "TYPEDEF", 
128                      "PROPERTIES" => $_[2],
129                      "NAME" => $_[4],
130                      "DATA" => $_[3],
131                      "ARRAY_LEN" => $_[5],
132                      "FILE" => $_[0]->YYData->{INPUT_FILENAME},
133                      "LINE" => $_[0]->YYData->{LINE},
134         }}
137 usertype: struct | union | enum | bitmap;
139 typedecl: usertype ';' { $_[1] };
141 type: usertype | identifier 
142         | void { "void" }
145 enum: 'enum' optional_identifier '{' enum_elements '}' 
146         {{
147              "TYPE" => "ENUM", 
148                          "NAME" => $_[2],
149                      "ELEMENTS" => $_[4]
150         }}
153 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 '}' 
163         {{
164              "TYPE" => "BITMAP", 
165                          "NAME" => $_[2],
166                      "ELEMENTS" => $_[4]
167         }}
170 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 '}' 
179         {{
180              "TYPE" => "STRUCT", 
181                          "NAME" => $_[2],
182                      "ELEMENTS" => $_[4]
183         }}
186 empty_element: property_list ';'
187         {{
188                  "NAME" => "",
189                  "TYPE" => "EMPTY",
190                  "PROPERTIES" => $_[1],
191                  "POINTERS" => 0,
192                  "ARRAY_LEN" => [],
193                  "FILE" => $_[0]->YYData->{INPUT_FILENAME},
194                  "LINE" => $_[0]->YYData->{LINE},
195          }}
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] }
204 union_elements: 
205     #empty
206     | union_elements optional_base_element { push(@{$_[1]}, $_[2]); $_[1] }
209 union: 'union' optional_identifier '{' union_elements '}' 
210         {{
211              "TYPE" => "UNION", 
212                      "NAME" => $_[2],
213                      "ELEMENTS" => $_[4]
214         }}
217 base_element: property_list type pointers identifier array_len
218               {{
219                            "NAME" => $_[4],
220                            "TYPE" => $_[2],
221                            "PROPERTIES" => $_[1],
222                            "POINTERS" => $_[3],
223                            "ARRAY_LEN" => $_[5],
224                        "FILE" => $_[0]->YYData->{INPUT_FILENAME},
225                        "LINE" => $_[0]->YYData->{LINE},
226               }}
230 pointers: 
231   #empty            
232    { 0 }
233     | pointers '*'  { $_[1]+1 }
236 element_list1: 
237     #empty
238     | element_list1 base_element ';' { push(@{$_[1]}, $_[2]); $_[1] }
241 element_list2: 
242     #empty
243     | 'void' 
244     | base_element { [ $_[1] ] }
245     | element_list2 ',' base_element { push(@{$_[1]}, $_[3]); $_[1] }
248 array_len: 
249     #empty                        { [] }
250     | '[' ']' array_len           { push(@{$_[3]}, "*"); $_[3] }
251     | '[' anytext ']' array_len   { push(@{$_[4]}, "$_[2]"); $_[4] }
255 property_list: 
256     #empty
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]" }}
268 listtext:
269     anytext 
270     | listtext ',' anytext { "$_[1] $_[3]" }
273 commalisttext:
274     anytext 
275     | commalisttext ',' anytext { "$_[1],$_[3]" }
278 anytext:  #empty
279     { "" }
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
301 optional_identifier: 
302         IDENTIFIER
303    | #empty { undef }
306 constant: CONSTANT
309 text: TEXT { "\"$_[1]\"" }
312 optional_semicolon: 
313         #empty
314         | ';'
318 #####################################
319 # start code
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
328 sub CleanData($)
330     sub CleanData($);
331     my($v) = shift;
332     if (ref($v) eq "ARRAY") {
333         foreach my $i (0 .. $#{$v}) {
334             CleanData($v->[$i]);
335             if (ref($v->[$i]) eq "ARRAY" && $#{$v->[$i]}==-1) { 
336                     $v->[$i] = undef; 
337                     next; 
338             }
339         }
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}) {
344             CleanData($v->{$x});
345             if (!defined $v->{$x}) { delete($v->{$x}); next; }
346             if (ref($v->{$x}) eq "ARRAY" && $#{$v->{$x}}==-1) { delete($v->{$x}); next; }
347         }
348     }
349         return $v;
352 sub _Error {
353     if (exists $_[0]->YYData->{ERRMSG}) {
354                 print $_[0]->YYData->{ERRMSG};
355                 delete $_[0]->YYData->{ERRMSG};
356                 return;
357         };
358         my $line = $_[0]->YYData->{LINE};
359         my $last_token = $_[0]->YYData->{LAST_TOKEN};
360         my $file = $_[0]->YYData->{INPUT_FILENAME};
361         
362         print "$file:$line: Syntax error near '$last_token'\n";
365 sub _Lexer($)
367         my($parser)=shift;
369     $parser->YYData->{INPUT} or return('',undef);
371 again:
372         $parser->YYData->{INPUT} =~ s/^[ \t]*//;
374         for ($parser->YYData->{INPUT}) {
375                 if (/^\#/) {
376                         if (s/^\# (\d+) \"(.*?)\"( \d+|)//) {
377                                 $parser->YYData->{LINE} = $1-1;
378                                 $parser->YYData->{INPUT_FILENAME} = $2;
379                                 goto again;
380                         }
381                         if (s/^\#line (\d+) \"(.*?)\"( \d+|)//) {
382                                 $parser->YYData->{LINE} = $1-1;
383                                 $parser->YYData->{INPUT_FILENAME} = $2;
384                                 goto again;
385                         }
386                         if (s/^(\#.*)$//m) {
387                                 goto again;
388                         }
389                 }
390                 if (s/^(\n)//) {
391                         $parser->YYData->{LINE}++;
392                         goto again;
393                 }
394                 if (s/^\"(.*?)\"//) {
395                         $parser->YYData->{LAST_TOKEN} = $1;
396                         return('TEXT',$1); 
397                 }
398                 if (s/^(\d+)(\W|$)/$2/) {
399                         $parser->YYData->{LAST_TOKEN} = $1;
400                         return('CONSTANT',$1); 
401                 }
402                 if (s/^([\w_]+)//) {
403                         $parser->YYData->{LAST_TOKEN} = $1;
404                         if ($1 =~ 
405                             /^(coclass|interface|const|typedef|declare|union
406                               |struct|enum|bitmap|void)$/x) {
407                                 return $1;
408                         }
409                         return('IDENTIFIER',$1);
410                 }
411                 if (s/^(.)//s) {
412                         $parser->YYData->{LAST_TOKEN} = $1;
413                         return($1,$1);
414                 }
415         }
418 sub parse_idl($$)
420         my ($self,$filename) = @_;
422         my $saved_delim = $/;
423         undef $/;
424         my $cpp = $ENV{CPP};
425         if (! defined $cpp) {
426                 $cpp = $config::config{'CPP'};
427         }
428         my $data = `$cpp -D__PIDL__ -xc $filename`;
429         $/ = $saved_delim;
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);