Pristine Ack-5.5
[Ack-5.5.git] / lang / m2 / m2mm / declar.g
1 /*
2  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3  * See the copyright notice in the ACK home directory, in the file "Copyright".
4  *
5  * Author: Ceriel J.H. Jacobs
6  */
7
8 /* D E C L A R A T I O N S */
9
10 /* stripped down version of the one in the Modula-2 compiler */
11
12 /* $Id: declar.g,v 1.3 1994/06/24 12:44:47 ceriel Exp $ */
13
14 ProcedureHeading :
15         PROCEDURE IDENT
16         [
17                 '('
18                 [
19                         FPSection
20                         [
21                                 ';' FPSection
22                         ]*
23                 |
24                 ]
25                 ')'
26                 [
27                         ':' qualtype
28                 |
29                         /* empty */
30                 ]
31         |
32                 /* empty */
33         ]
34 ;
35
36 block :
37         [ %persistent
38                 declaration
39         ]*
40         [ %default
41                 BEGIN StatementSequence
42         |
43                 /* empty */
44         ]
45         END
46 ;
47
48 declaration :
49         CONST [ ConstantDeclaration ';' ]*
50 |
51         TYPE [ TypeDeclaration ';' ]*
52 |
53         VAR [ VariableDeclaration ';' ]*
54 |
55         ProcedureHeading ';' block IDENT ';'
56 |
57         ModuleDeclaration ';'
58 ;
59
60 FPSection :
61         var IdentList ':' FormalType
62 ;
63
64 FormalType :
65         ARRAY OF qualtype
66 |
67          qualtype
68 ;
69
70 TypeDeclaration :
71         IDENT '=' type
72 ;
73
74 type :
75  %default
76         SimpleType
77 |
78         ArrayType
79 |
80         RecordType
81 |
82         SetType
83 |
84         PointerType
85 |
86         ProcedureType
87 ;
88
89 SimpleType :
90         qualtype
91         [
92                 /* empty */
93         |
94                 SubrangeType
95                 /* The subrange type is given a base type by the
96                    qualident (this is new modula-2).
97                 */
98         ]
99 |
100         enumeration
101 |
102         SubrangeType
103 ;
104
105 enumeration :
106         '(' IdentList ')'
107 ;
108
109 IdentList :
110         IDENT
111         [ %persistent
112                 ',' IDENT
113         ]*
114 ;
115
116 SubrangeType :
117         /*
118            This is not exactly the rule in the new report, but see
119            the rule for "SimpleType".
120         */
121         '[' ConstExpression UPTO ConstExpression ']'
122 ;
123
124 ArrayType :
125         ARRAY SimpleType
126         [
127                 ',' SimpleType
128         ]*
129         OF type
130 ;
131
132 RecordType :
133         RECORD FieldListSequence END
134 ;
135
136 FieldListSequence :
137         FieldList
138         [
139                 ';' FieldList
140         ]*
141 ;
142
143 FieldList :
144         IdentList ':' type
145 |
146         CASE
147         /* Also accept old fashioned Modula-2 syntax, but give a warning.
148            Sorry for the complicated code.
149         */
150         [
151                 qualident
152                 [
153                         ':' qualtype
154                         /* This is correct, in both kinds of Modula-2, if
155                            the first qualident is a single identifier.
156                         */
157                 |
158                         /* empty */
159                         /* Old fashioned! the first qualident now represents
160                            the type
161                         */
162                 ]
163         |
164                 ':' qualtype
165                         /* Aha, third edition. Well done! */
166         ]
167         OF variant
168         [
169                 '|' variant
170         ]*
171         [
172                 ELSE FieldListSequence
173         |
174                 /* empty */
175         ]
176         END
177 |
178         /* empty */
179 ;
180
181 variant :
182         [
183                 CaseLabelList ':' FieldListSequence
184         |
185                 /* empty */
186         ]
187                         /* Changed rule in new modula-2 */
188 ;
189
190 CaseLabelList :
191         CaseLabels
192         [       
193                 ',' CaseLabels
194         ]*
195 ;
196
197 CaseLabels :
198         ConstExpression
199         [
200                 UPTO ConstExpression
201         |
202                 /* empty */
203         ]
204 ;
205
206 SetType :
207         SET OF SimpleType
208 ;
209
210 /*      In a pointer type definition, the type pointed at does not
211         have to be declared yet, so be careful about identifying
212         type-identifiers
213 */
214 PointerType :
215         POINTER TO type
216 ;
217
218 qualtype :
219         qualident
220 ;
221
222 ProcedureType :
223         PROCEDURE 
224         [
225                 FormalTypeList
226         |
227                 /* empty */
228         ]
229 ;
230
231 FormalTypeList :
232         '('
233         [
234                 VarFormalType
235                 [
236                         ',' VarFormalType
237                 ]*
238         |
239                 /* empty */
240         ]
241         ')'
242         [       
243                 ':' qualtype
244         |
245                 /* empty */
246         ]
247 ;
248
249 VarFormalType :
250         var FormalType
251 ;
252
253 var :
254         VAR
255 |
256         /* empty */
257 ;
258
259 ConstantDeclaration :
260         IDENT '=' ConstExpression
261 ;
262
263 VariableDeclaration :
264         IdentAddr
265         [ %persistent
266                 ',' IdentAddr
267         ]*
268         ':' type
269 ;
270
271 IdentAddr :
272         IDENT
273         [       
274                 '[' ConstExpression ']'
275         |
276                 /* empty */
277         ]
278 ;