summaryrefslogtreecommitdiffstats
path: root/otherlibs/labltk/compiler/parser.mly
blob: 6dc7aff329071f487ca5a25a1c3ae683071cd617 (plain)
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
/***********************************************************************/
/*                                                                     */
/*                 MLTk, Tcl/Tk interface of OCaml                     */
/*                                                                     */
/*    Francois Rouaix, Francois Pessaux, Jun Furuse and Pierre Weis    */
/*               projet Cristal, INRIA Rocquencourt                    */
/*            Jacques Garrigue, Kyoto University RIMS                  */
/*                                                                     */
/*  Copyright 2002 Institut National de Recherche en Informatique et   */
/*  en Automatique and Kyoto University.  All rights reserved.         */
/*  This file is distributed under the terms of the GNU Library        */
/*  General Public License, with the special exception on linking      */
/*  described in file ../../../LICENSE.                                */
/*                                                                     */
/***********************************************************************/

/* $Id$ */

%{

open Tables

%}

/* Tokens */
%token <string> IDENT
%token <string> STRING
%token EOF

%token LPAREN           /* "(" */
%token RPAREN           /* ")" */
%token COMMA            /* "," */
%token SEMICOLON        /* ";" */
%token COLON            /* ":" */
%token QUESTION         /* "?" */
%token LBRACKET         /* "[" */
%token RBRACKET         /* "]" */
%token LBRACE           /* "{" */
%token RBRACE           /* "}" */
%token SLASH            /* "/" */

%token TYINT            /* "int" */
%token TYFLOAT          /* "float" */
%token TYBOOL           /* "bool" */
%token TYCHAR           /* "char" */
%token TYSTRING         /* "string" */
%token LIST             /* "list" */

%token AS               /* "as" */
%token VARIANT          /* "variant" */
%token WIDGET           /* "widget" */
%token OPTION           /* "option" */
%token TYPE             /* "type" */
%token SEQUENCE         /* "sequence" */
%token SUBTYPE          /* "subtype" */
%token FUNCTION         /* "function" */
%token MODULE           /* "module" */
%token EXTERNAL         /* "external" */
%token UNSAFE           /* "unsafe" */
/* Entry points */
%start entry
%type <unit> entry

%%
TypeName:
   IDENT { String.uncapitalize $1 }
 | WIDGET { "widget" }
;

/* Atomic types */
Type0 :
    TYINT
      { Int }
  | TYFLOAT
      { Float }
  | TYBOOL
      { Bool }
  | TYCHAR
      { Char }
  | TYSTRING
      { String }
  | TypeName
      { UserDefined $1 }
;

/* Camltk/Labltk types */
Type0_5:
  | Type0 SLASH Type0 { if !Flags.camltk then $1 else $3 }
  | Type0 { $1 }
;

/* with subtypes */
Type1 :
    Type0_5
      { $1 }
  | TypeName LPAREN IDENT RPAREN
     { Subtype ($1, $3) }
  | WIDGET LPAREN IDENT RPAREN
     { Subtype ("widget", $3) }
  | OPTION LPAREN IDENT RPAREN
     { Subtype ("options", $3) }
  | Type1 AS STRING
     { As ($1, $3) }
  | LBRACE Type_list RBRACE
      { Product $2 }
;

/* with list constructors */
Type2 :
    Type1
     { $1 }
  | Type2 LIST
     { List $1 }
;

Labeled_type2 :
    Type2
      { "", $1 }
  | IDENT COLON Type2
      { $1, $3 }
;

/* products */
Type_list :
    Type2 COMMA Type_list
      { $1 :: $3 }
  | Type2
      { [$1] }
;

/* records */
Type_record :
    Labeled_type2 COMMA Type_record
      { $1 :: $3 }
  | Labeled_type2
      { [$1] }
;

/* callback arguments or function results*/
FType :
    LPAREN RPAREN
      { Unit }
  | LPAREN Type2 RPAREN
      { $2 }
  | LPAREN Type_record RPAREN
      { Record $2 }
;

Type :
    Type2
      { $1 }
  | FUNCTION FType
      { Function $2 }
;



SimpleArg:
    STRING
      {StringArg $1}
  | Type
      {TypeArg ("", $1) }
;

Arg:
    STRING
      {StringArg $1}
  | Type
      {TypeArg ("", $1) }
  | IDENT COLON Type
      {TypeArg ($1, $3)}
  | QUESTION IDENT COLON LBRACKET SimpleArgList RBRACKET DefaultList
      {OptionalArgs ( $2, $5, $7 )}
  | QUESTION WIDGET COLON LBRACKET SimpleArgList RBRACKET DefaultList
      {OptionalArgs ( "widget", $5, $7 )}
  | QUESTION IDENT COLON LBRACKET SimpleArgList RBRACKET
      {OptionalArgs ( $2, $5, [] )}
  | QUESTION WIDGET COLON LBRACKET SimpleArgList RBRACKET
      {OptionalArgs ( "widget", $5, [] )}
  | WIDGET COLON Type
      {TypeArg ("widget", $3)}
  | Template
      { $1 }
;

SimpleArgList:
    SimpleArg SEMICOLON SimpleArgList
       { $1 :: $3}
  | SimpleArg
      { [$1] }
;

ArgList:
    Arg SEMICOLON ArgList
       { $1 :: $3}
  | Arg
      { [$1] }
;

/* DefaultList Only one TypeArg in ArgList and it must be unlabeled */
DefaultList :
    LBRACKET LBRACE ArgList RBRACE RBRACKET
      {$3}

/* Template */
Template :
    LBRACKET ArgList RBRACKET
      { ListArg $2 }
;


/* Constructors for type declarations */
Constructor :
    IDENT Template
      {{ component = Constructor;
         ml_name = $1;
         var_name = getvarname $1 $2;
         template = $2;
         result = Unit;
         safe = true }}
  | IDENT LPAREN IDENT RPAREN Template
      {{ component = Constructor;
         ml_name = $1;
         var_name = $3;
         template = $5;
         result = Unit;
         safe = true }}
;

AbbrevConstructor :
    Constructor
      { Full $1 }
 |  IDENT
      { Abbrev $1 }
;

Constructors :
  Constructor Constructors
   { $1 :: $2 }
| Constructor
   { [$1] }
;

AbbrevConstructors :
  AbbrevConstructor AbbrevConstructors
   { $1 :: $2 }
| AbbrevConstructor
   { [$1] }
;

Safe:
   /* */
  { true }
 | UNSAFE
  { false }

Command :
   Safe FUNCTION FType IDENT Template
     {{component = Command; ml_name = $4; var_name = "";
       template = $5; result = $3; safe = $1 }}
;

External :
  Safe EXTERNAL IDENT STRING
     {{component = External; ml_name = $3; var_name = "";
       template = StringArg $4; result = Unit; safe = $1}}
;

Option :
   OPTION IDENT Template
     {{component = Constructor; ml_name = $2; var_name = getvarname $2 $3;
       template = $3; result = Unit; safe = true }}
   /* Abbreviated */
|   OPTION IDENT LPAREN IDENT RPAREN Template
     {{component = Constructor; ml_name = $2; var_name = $4;
       template = $6; result = Unit; safe = true }}
   /* Abbreviated */
|  OPTION IDENT
     { retrieve_option $2 }
;

WidgetComponents :
  /* */
  { [] }
 | Command WidgetComponents
  { $1 :: $2 }
 | Option WidgetComponents
  { $1 :: $2 }
 | External WidgetComponents
  { $1 :: $2 }
;

ModuleComponents :
  /* */
  { [] }
 | Command ModuleComponents
  { $1 :: $2 }
 | External ModuleComponents
  { $1 :: $2 }
;

ParserArity :
  /* */
  { OneToken }
 | SEQUENCE
  { MultipleToken }
;



entry :
  TYPE ParserArity TypeName LBRACE Constructors RBRACE
    { enter_type $3 $2 $5 }
| VARIANT TYPE ParserArity TypeName LBRACE Constructors RBRACE
    { enter_type $4 $3 $6 ~variant: true }
| TYPE ParserArity TypeName EXTERNAL
    { enter_external_type $3 $2 }
| SUBTYPE ParserArity OPTION LPAREN IDENT RPAREN LBRACE AbbrevConstructors RBRACE
    { enter_subtype "options" $2 $5 $8 }
| SUBTYPE ParserArity TypeName LPAREN IDENT RPAREN LBRACE AbbrevConstructors RBRACE
    { enter_subtype $3 $2 $5 $8 }
| Command
    { enter_function $1 }
| WIDGET IDENT LBRACE WidgetComponents RBRACE
    { enter_widget $2 $4 }
| MODULE IDENT LBRACE ModuleComponents RBRACE
    { enter_module (String.uncapitalize $2) $4 }
| EOF
    { raise End_of_file }
;