File: parser_midl.mly

package info (click to toggle)
camlidl 1.05-7
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 872 kB
  • ctags: 1,131
  • sloc: ml: 4,894; ansic: 940; cpp: 897; makefile: 294; sh: 182
file content (613 lines) | stat: -rw-r--r-- 16,465 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/***********************************************************************/
/*                                                                     */
/*                              CamlIDL                                */
/*                                                                     */
/*            Xavier Leroy, projet Cristal, INRIA Rocquencourt         */
/*                                                                     */
/*  Copyright 1999 Institut National de Recherche en Informatique et   */
/*  en Automatique.  All rights reserved.  This file is distributed    */
/*  under the terms of the Q Public License version 1.0                */
/*                                                                     */
/***********************************************************************/

/* $Id: parser_midl.mly,v 1.19 2002/04/19 14:42:35 xleroy Exp $ */

/* Parser for Microsoft IDL */

%{

open Printf
open Cvttyp
open Idltypes
open Funct
open Typedef
open Constdecl
open Intf
open File
open Parse_aux

%}

/* Tokens */

%token AMPER
%token AMPERAMPER
%token BANG
%token BANGEQUAL
%token BAR
%token BARBAR
%token BOOLEAN
%token BYTE
%token CARET
%token CASE
%token CHAR
%token <char> CHARACTER
%token COLON
%token COMMA
%token CONST
%token CPP_QUOTE
%token DEFAULT
%token DOT
%token DOUBLE
%token ENUM
%token EOF
%token EQUAL
%token EQUALEQUAL
%token FALSE
%token FLOAT
%token GREATER
%token GREATEREQUAL
%token GREATERGREATER
%token GREATERGREATERGREATER
%token HANDLE_T
%token HYPER
%token <string> IDENT
%token IMPORT
%token INT
%token INT64
%token INTERFACE
%token <int64> INTEGER
%token LBRACE
%token LBRACKET
%token LESS
%token LESSEQUAL
%token LESSLESS
%token LONG
%token LPAREN
%token MINUS
%token NULL
%token PERCENT
%token PLUS
%token QUESTIONMARK
%token QUOTE
%token RBRACE
%token RBRACKET
%token RPAREN
%token SEMI
%token SHORT
%token SIGNED
%token SIZEOF
%token SLASH
%token SMALL
%token STAR
%token <string> STRING
%token STRUCT
%token SWITCH
%token TILDE
%token TRUE
%token TYPEDEF
%token <string> TYPEIDENT
%token UNION
%token UNSIGNED
%token <string> UUID
%token VOID
%token WCHAR_T

/* Precedences and associativities. Lower precedences come first. */

%right QUESTIONMARK prec_conditional
%left BARBAR
%left AMPERAMPER
%left BAR
%left CARET
%left AMPER
%left EQUALEQUAL BANGEQUAL
%left LESS LESSEQUAL GREATER GREATEREQUAL
%left LESSLESS GREATERGREATER GREATERGREATERGREATER
%left PLUS MINUS
%left STAR SLASH PERCENT
%right prec_uminus BANG TILDE prec_deref prec_addressof prec_cast
%left DOT prec_dot MINUSGREATER LBRACKET prec_subscript

/* Start symbol */

%start file
%type <File.components> file

%%

/* Main entry point */

file: component_list EOF
        { List.rev $1 }
;

/* Components */

component_list:
    /*empty*/
        { [] }
  | component_list component
        { $2 @ $1 }
;

component:
    const_decl SEMI
        { [Comp_constdecl $1] }
  | type_decl SEMI
        { List.map (fun td -> Comp_typedecl td) (List.rev $1) }
  | attributes struct_declarator SEMI
        /* Attributes are ignored, they are allowed just to avoid a
           parsing ambiguity with fun_decl */
        { [Comp_structdecl $2] }
  | attributes union_declarator SEMI
        { [Comp_uniondecl $2] }
  | attributes enum_declarator SEMI
        { [Comp_enumdecl $2] }
  | attributes STRUCT opt_ident SEMI
        { [Comp_structdecl {sd_name = $3; sd_mod = "";
                            sd_stamp = 0; sd_fields = []}] }
  | attributes UNION opt_ident SWITCH LPAREN simple_type_spec ident RPAREN SEMI
        { [Comp_structdecl {sd_name = $3; sd_mod = "";
                            sd_stamp = 0; sd_fields = []}] }
  | attributes UNION opt_ident SEMI
        { [Comp_uniondecl {ud_name = $3; ud_mod = "";
                           ud_stamp = 0; ud_cases = []}] }
  | fun_decl SEMI
        { [Comp_fundecl $1] }
  | interface_attributes INTERFACE tydef_ident opt_superinterface
    LBRACE component_list RBRACE opt_semi
    /* Valid MIDL attributes: object uuid local endpoint version
           pointer_default implicit_handle auto_handle */
        { let i = make_interface $3 $1 $4 (List.rev $6) in
          restore_defaults(); i }
  | interface_attributes INTERFACE tydef_ident SEMI
        { let i = [make_forward_interface $3] in
          restore_defaults(); i }
  | IMPORT STRING SEMI
        { read_import $2 }
  | quote opt_semi
        { let (kind, txt) = make_diversion $1 in [Comp_diversion(kind, txt)] }
;

/* Constant declaration */

const_decl:
    CONST attributes type_spec pointer_opt IDENT EQUAL lexpr
        { make_const_decl $2 ($4 $3) $5 $7 }
;
/* Typedef */

type_decl:
    /* Valid MIDL attributes: handle, switch_type, switch_is, transmit_as,
       ref, unique, ptr, context_handle, ignore, string */
    TYPEDEF attributes type_spec declarators
        { make_typedef $2 $3 (List.rev $4) }
;

/* Function declaration */

fun_decl:
    /* Valid MIDL attributes: callback, local, ref, unique, ptr, string,
       ignore, context_handle */
    attributes type_spec pointer_opt IDENT
    LPAREN param_list_declarator RPAREN opt_quotes
        { make_fun_declaration $1 ($3 $2) $4 $6 $8 }
;
opt_quotes:
    opt_quotes QUOTE LPAREN STRING RPAREN
        { ("call", $4) :: $1 }
  | opt_quotes QUOTE LPAREN ident COMMA STRING RPAREN
        { ($4, $6) :: $1 }
  | /* empty */
        { [] }
;
    
/* Parameter lists */

param_list_declarator:
    /*empty*/
        { [] }
  | VOID
        { [] }
  | param_declarators
        { List.rev $1 }
;
param_declarators:
    param_declarator
        { [$1] }
  | param_declarators COMMA param_declarator
        { $3 :: $1 }
;
param_declarator:
    /* Valid MIDL attributes: in, out, first_is, last_is,
      length_is, max_is, size_is, switch_type, switch_is, ref, unique, ptr,
      context_handle, string */
    attributes type_spec declarator
        { make_param $1 $2 $3 }
;

/* Type specifications */

type_spec:
    simple_type_spec
        { $1 }
  | STRUCT opt_ident
        { Type_struct {sd_name=$2; sd_mod = ""; sd_stamp=0; sd_fields=[]} }
  | struct_declarator
        { Type_struct $1 }
  | UNION opt_ident
        { Type_union({ud_name=$2; ud_mod = ""; ud_stamp=0; ud_cases=[]},
                      no_switch) }
  | union_declarator
        { Type_union($1, no_switch) }
  | ENUM opt_ident
        { Type_enum({en_name=$2; en_mod = ""; en_stamp=0; en_consts=[]},
                    no_enum_attr) }
  | enum_declarator
        { Type_enum($1, no_enum_attr) }
  | CONST type_spec
        { make_type_const $2 }
  | type_spec CONST
        { make_type_const $1 }
;

simple_type_spec:
    FLOAT                                       { Type_float }
  | DOUBLE                                      { Type_double }
  | INT                                         { make_int Int }
  | UNSIGNED INT                                { make_int UInt }
  | SIGNED INT                                  { make_int Int }
  | integer_size opt_int                        { make_int $1 }
  | UNSIGNED integer_size opt_int               { make_unsigned $2 }
  | integer_size UNSIGNED opt_int               { make_unsigned $1 }
  | SIGNED integer_size opt_int                 { make_signed $2 }
  | integer_size SIGNED opt_int                 { make_signed $1 }
  | CHAR                                        { make_int Char }
  | UNSIGNED CHAR                               { make_int UChar }
  | SIGNED CHAR                                 { make_int SChar }
  | BOOLEAN                                     { make_int Boolean }
  | BYTE                                        { make_int Byte }
  | INT64                                       { make_int Hyper }
  | UNSIGNED INT64                              { make_int UHyper }
  | SIGNED INT64                                { make_int Hyper }
  | VOID                                        { Type_void }
  | TYPEIDENT                                   { Type_named("", $1) }
  | WCHAR_T                                     { wchar_t_type() }
  | HANDLE_T                                    { handle_t_type() }
;
integer_size:
    LONG                                        { Long }
  | SMALL                                       { Small }
  | SHORT                                       { Short }
  | HYPER                                       { Hyper }
  | LONG LONG                                   { Hyper }
;
opt_int:
    /* nothing */                               { () }
  | INT                                         { () }
;

/* Declarators */

declarators:
    declarator
        { [$1] }
  | declarators COMMA declarator
        { $3 :: $1 }
;
declarator:
    pointer_opt direct_declarator
        { fun ty -> $2($1(ty)) }
;
pointer_opt:
    /* empty */
        { fun ty -> ty }
  | pointer_opt STAR
        { fun ty -> $1(Type_pointer(!pointer_default, ty)) }
  | pointer_opt STAR CONST
        { fun ty -> $1(Type_const(Type_pointer(!pointer_default, ty))) }
;
direct_declarator:
    ident
        { fun ty -> ($1, ty) }
  | LPAREN declarator RPAREN
        { $2 }
  | direct_declarator array_bounds_declarator
        { fun ty -> let (id, ty1) = $1 ty in (id, Type_array($2, ty1)) }
;
array_bounds_declarator:
    LBRACKET RBRACKET                           { no_bounds }
  | LBRACKET STAR RBRACKET                      { no_bounds }
  | LBRACKET lexpr RBRACKET                     { one_bound $2 }
;

/* Struct declaration and discriminated unions */

union_name:
    ident 
        { $1 }
  | /* empty */
        { "u" }
;
struct_declarator:
    STRUCT opt_ident LBRACE field_declarators RBRACE
        { {sd_name = $2; sd_mod = ""; sd_stamp = 0; sd_fields = $4} } 
  | UNION opt_ident SWITCH LPAREN simple_type_spec ident RPAREN union_name
    LBRACE union_body RBRACE
        { make_discriminated_union $2 $8 $6 $5 (List.rev $10) }
;
field_declarators:
    field_declarator
        { $1 }
  | field_declarators field_declarator
        { $1 @ $2 }
;
field_declarator:
    /* Valid MIDL attributes: first_is, last_is, length_is, max_is,
       size_is, string, ignore, context_handle, ref, unique, ptr,
       switch_type */
    attributes type_spec declarators SEMI
        { make_fields $1 $2 (List.rev $3) }
;

/* Union declaration */

union_declarator:
  | UNION opt_ident LBRACE union_body RBRACE
        { {ud_name = $2; ud_mod = ""; ud_stamp = 0; ud_cases = List.rev $4} }
;
union_body:
    union_case                                          { [$1] }
  | union_body union_case                               { $2 :: $1 }
;
union_case:
    case_list opt_field_declarator SEMI
        { {case_labels = List.rev $1; case_field = $2} }
  | DEFAULT COLON opt_field_declarator SEMI
        { {case_labels = []; case_field = $3} }
;
case_list:
    case_label                                          { [$1] }
  | case_list case_label                                { $2 :: $1 }
;
case_label:
    CASE ident COLON                                    { $2 }
;
opt_field_declarator:
    /* empty */
        { None }
  | attributes type_spec declarator
  /* Valid MIDL attributes: first_is, last_is, length_is, max_is, size_is,
     string, ignore, context_handle, ref, unique, ptr, switch_type,
     switch_is */
        { Some(make_field $1 $2 $3) }
;

/* Enumerated types */

enum_declarator:
    ENUM opt_ident LBRACE enum_cases opt_comma RBRACE
         { {en_name = $2; en_mod = ""; en_stamp = 0; en_consts = List.rev $4} }
;
enum_cases:
    enum_case                                           { [$1] }
  | enum_cases COMMA enum_case                          { $3 :: $1 }
;
enum_case:
    ident
      { {const_name = $1; const_val = None} }
  | ident EQUAL lexpr
      { {const_name = $1; const_val = Some $3} }
;
opt_comma:
    COMMA                                               { () }
  | /* empty */                                         { () }
;

/* Attributes */

interface_attributes:
    attributes          { let a = $1 in save_defaults(); update_defaults a; a }
;
attributes:
    /* empty */                                         { [] }
  | LBRACKET attribute_list RBRACKET                    { List.rev $2 }
;
attribute_list:
    attribute                                           { [$1] }
  | /*empty*/                                           { [] }
  | attribute_list COMMA attribute                      { $3 :: $1 }
  | attribute_list COMMA                                { $1 }
;
attribute:
    ident
        { ($1, []) }
  | ident LPAREN attr_vars RPAREN
        { ($1, List.rev $3) }
  | STAR attribute
        { make_star_attribute $2 }
  | attribute STAR
        { make_star_attribute $1 }
  | ident UUID
        { ($1, [Expr_string $2]) }
;
attr_vars:
    attr_var
        { [$1] }
  | attr_vars COMMA attr_var
        { $3 :: $1 }
;
attr_var:
    lexpr
        { $1 }
  | /*nothing*/
        { null_attr_var }
;

/* Limited expressions */

lexpr:
    IDENT
        { Expr_ident $1 }
  | INTEGER
        { Expr_int $1 }
  | CHARACTER
        { Expr_int(Int64.of_int(Char.code $1)) }
  | TRUE
        { Expr_int Int64.one }
  | FALSE
        { Expr_int Int64.zero }
  | STRING
        { Expr_string $1 }
  | lexpr QUESTIONMARK lexpr COLON lexpr %prec prec_conditional
        { Expr_cond($1, $3, $5) }
  | lexpr BARBAR lexpr
        { Expr_sequor($1, $3) }
  | lexpr AMPERAMPER lexpr
        { Expr_sequand($1, $3) }
  | lexpr BAR lexpr
        { Expr_logor($1, $3) }
  | lexpr CARET lexpr
        { Expr_logxor($1, $3) }
  | lexpr AMPER lexpr
        { Expr_logand($1, $3) }
  | lexpr EQUALEQUAL lexpr
        { Expr_eq($1, $3) }
  | lexpr BANGEQUAL lexpr
        { Expr_ne($1, $3) }
  | lexpr LESS lexpr
        { Expr_lt($1, $3) }
  | lexpr GREATER lexpr
        { Expr_gt($1, $3) }
  | lexpr LESSEQUAL lexpr
        { Expr_le($1, $3) }
  | lexpr GREATEREQUAL lexpr
        { Expr_ge($1, $3) }
  | lexpr LESSLESS lexpr
        { Expr_lshift($1, $3) }
  | lexpr GREATERGREATER lexpr
        { Expr_rshift($1, $3) }
  | lexpr GREATERGREATERGREATER lexpr
        { Expr_rshift_unsigned($1, $3) }
  | lexpr PLUS lexpr
        { Expr_plus($1, $3) }
  | lexpr MINUS lexpr
        { Expr_minus($1, $3) }
  | lexpr STAR lexpr
        { Expr_times($1, $3) }
  | lexpr SLASH lexpr
        { Expr_div($1, $3) }
  | lexpr PERCENT lexpr
        { Expr_mod($1, $3) }
  | PLUS lexpr %prec prec_uminus
        { $2 }
  | MINUS lexpr %prec prec_uminus
        { Expr_neg($2) }
  | TILDE lexpr
        { Expr_lognot($2) }
  | BANG lexpr
        { Expr_boolnot($2) }
  | STAR lexpr %prec prec_deref
        { Expr_deref($2) }
  | AMPER lexpr %prec prec_addressof
        { Expr_addressof($2) }
  | LPAREN type_expr RPAREN lexpr %prec prec_cast
        { Expr_cast($2, $4) }
  | SIZEOF LPAREN type_expr RPAREN
        { Expr_sizeof($3) }
  | lexpr LBRACKET lexpr RBRACKET %prec prec_subscript
        { Expr_subscript($1, $3) }
  | lexpr MINUSGREATER ident
        { Expr_dereffield($1, $3) }
  | lexpr DOT ident %prec prec_dot
        { Expr_field($1, $3) }
  | lexpr DOT INTEGER %prec prec_dot
        /* This is a hack for parsing version attributes, e.g. version(0.1) */
        { Expr_field($1, Int64.to_string $3) }
  | LPAREN lexpr RPAREN
        { $2 }
;

type_expr:
    type_spec
        { $1 }
  | type_spec abstract_declarator
        { $2($1) }
;

abstract_declarator:
    STAR
        { fun ty -> Type_pointer(!pointer_default, ty) }
  | STAR direct_abstract_declarator
        { fun ty -> $2(Type_pointer(!pointer_default, ty)) }
  | direct_abstract_declarator
        { $1 }
;

direct_abstract_declarator:
    LPAREN abstract_declarator RPAREN
        { $2 }
  | direct_abstract_declarator array_bounds_declarator
        { fun ty -> Type_array($2, ty) }
;

/* Optional names for struct, union, enums */

opt_ident:
    /*empty*/                   { "" }
  | ident                       { $1 }
;

/* Optional name of superinterface for interfaces */

opt_superinterface:
    /*empty*/
        { None }
  | COLON ident
        { Some $2 }
;

/* Optional semicolon */

opt_semi:
    SEMI       { () }
  | /*empty*/  { () }
;

/* Any ident (type or not) */

ident:
    IDENT
        { $1 }
  | TYPEIDENT
        { $1 }
;

/* An ident that becomes a type name */

tydef_ident:
    ident
        { type_names := StringSet.add $1 !type_names; $1 }
;

/* Quotes (diversions) */

quote:
    QUOTE LPAREN STRING RPAREN
        { ("", $3) }
  | QUOTE LPAREN ident COMMA STRING RPAREN
        { ($3, $5) }
  | CPP_QUOTE LPAREN STRING RPAREN
        { ("h", $3) }
;