Mercurial > projects > ddmd
annotate dmd/Parser.d @ 122:c77e9f4f1793
Statements -> Vector
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Thu, 02 Sep 2010 23:37:49 +0100 |
parents | 347de076ad34 |
children | 9e39c7de8438 |
rev | line source |
---|---|
0 | 1 module dmd.Parser; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.Lexer; |
5 import dmd.PostBlitDeclaration; | |
6 import dmd.FileInitExp; | |
7 import dmd.LineInitExp; | |
8 import dmd.EnumMember; | |
9 import dmd.CtorDeclaration; | |
10 import dmd.ShlAssignExp; | |
11 import dmd.ShrAssignExp; | |
12 import dmd.UshrAssignExp; | |
13 import dmd.CatAssignExp; | |
14 import dmd.StaticIfCondition; | |
15 import dmd.TraitsExp; | |
13 | 16 import dmd.TemplateMixin; |
0 | 17 import dmd.BaseClass; |
18 import dmd.AssignExp; | |
19 import dmd.TemplateInstance; | |
20 import dmd.NewExp; | |
21 import dmd.ArrayExp; | |
22 import dmd.DotTemplateInstanceExp; | |
23 import dmd.ClassDeclaration; | |
24 import dmd.NewAnonClassExp; | |
25 import dmd.InterfaceDeclaration; | |
26 import dmd.StructDeclaration; | |
27 import dmd.UnionDeclaration; | |
28 import dmd.AnonDeclaration; | |
29 import dmd.StructInitializer; | |
30 import dmd.ArrayInitializer; | |
31 import dmd.ExpInitializer; | |
32 import dmd.TemplateAliasParameter; | |
33 import dmd.TemplateTupleParameter; | |
34 import dmd.TemplateThisParameter; | |
35 import dmd.TemplateValueParameter; | |
36 import dmd.VoidInitializer; | |
37 import dmd.VersionCondition; | |
38 import dmd.DotIdExp; | |
39 import dmd.DebugCondition; | |
40 import dmd.PostExp; | |
41 import dmd.CallExp; | |
42 import dmd.SliceExp; | |
43 import dmd.FuncExp; | |
44 import dmd.AssocArrayLiteralExp; | |
45 import dmd.ArrayLiteralExp; | |
46 import dmd.IsExp; | |
47 import dmd.FuncLiteralDeclaration; | |
48 import dmd.AssertExp; | |
49 import dmd.CompileExp; | |
50 import dmd.FileExp; | |
19
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
51 import dmd.TemplateMixin; |
0 | 52 import dmd.TemplateParameter; |
53 import dmd.TemplateTypeParameter; | |
54 import dmd.TypeidExp; | |
55 import dmd.StringExp; | |
56 import dmd.ScopeExp; | |
57 import dmd.IdentifierExp; | |
58 import dmd.DollarExp; | |
59 import dmd.ThisExp; | |
60 import dmd.SuperExp; | |
61 import dmd.NullExp; | |
62 import dmd.RealExp; | |
63 import dmd.TypeExp; | |
64 import dmd.AddrExp; | |
65 import dmd.MOD; | |
66 import dmd.IntegerExp; | |
67 import dmd.CastExp; | |
68 import dmd.PtrExp; | |
69 import dmd.NegExp; | |
70 import dmd.XorAssignExp; | |
71 import dmd.OrAssignExp; | |
72 import dmd.UAddExp; | |
73 import dmd.NotExp; | |
74 import dmd.ComExp; | |
75 import dmd.DeleteExp; | |
76 import dmd.MulAssignExp; | |
77 import dmd.ModAssignExp; | |
78 import dmd.MinAssignExp; | |
79 import dmd.DivAssignExp; | |
80 import dmd.AndAssignExp; | |
81 import dmd.AddAssignExp; | |
82 import dmd.ModuleDeclaration; | |
83 import dmd.CaseRangeStatement; | |
84 import dmd.CommaExp; | |
85 import dmd.XorExp; | |
86 import dmd.CondExp; | |
87 import dmd.CmpExp; | |
88 import dmd.InExp; | |
89 import dmd.OrOrExp; | |
90 import dmd.OrExp; | |
91 import dmd.AddExp; | |
92 import dmd.MinExp; | |
93 import dmd.CatExp; | |
94 import dmd.AndAndExp; | |
95 import dmd.EqualExp; | |
96 import dmd.ShlExp; | |
97 import dmd.ShrExp; | |
98 import dmd.DivExp; | |
99 import dmd.MulExp; | |
100 import dmd.ModExp; | |
101 import dmd.UshrExp; | |
102 import dmd.IdentityExp; | |
103 import dmd.AndExp; | |
104 import dmd.Id; | |
105 import dmd.LabelStatement; | |
106 import dmd.ExpStatement; | |
107 import dmd.StaticAssertStatement; | |
108 import dmd.DeclarationStatement; | |
109 import dmd.ScopeStatement; | |
110 import dmd.PragmaStatement; | |
111 import dmd.WhileStatement; | |
112 import dmd.DoStatement; | |
113 import dmd.ForStatement; | |
114 import dmd.OnScopeStatement; | |
115 import dmd.IfStatement; | |
116 import dmd.SwitchStatement; | |
117 import dmd.CaseStatement; | |
118 import dmd.DefaultStatement; | |
119 import dmd.GotoDefaultStatement; | |
120 import dmd.GotoCaseStatement; | |
121 import dmd.GotoStatement; | |
122 import dmd.SynchronizedStatement; | |
123 import dmd.WithStatement; | |
124 import dmd.Catch; | |
125 import dmd.TryCatchStatement; | |
126 import dmd.TryFinallyStatement; | |
127 import dmd.ThrowStatement; | |
128 import dmd.VolatileStatement; | |
129 import dmd.ReturnStatement; | |
130 import dmd.BreakStatement; | |
131 import dmd.ContinueStatement; | |
132 import dmd.AsmStatement; | |
133 import dmd.TypeReturn; | |
134 import dmd.TypeTypeof; | |
135 import dmd.ForeachRangeStatement; | |
136 import dmd.ForeachStatement; | |
137 import dmd.CompileStatement; | |
138 import dmd.CompoundStatement; | |
139 import dmd.ConditionalStatement; | |
140 import dmd.CompoundDeclarationStatement; | |
141 import dmd.Argument; | |
142 import dmd.ParseStatementFlags; | |
79 | 143 import dmd.TypeNewArray; |
0 | 144 import dmd.TypeNext; |
145 import dmd.TypeInstance; | |
146 import dmd.TypePointer; | |
147 import dmd.TypeDArray; | |
148 import dmd.TypeAArray; | |
149 import dmd.TypeSlice; | |
150 import dmd.TypeSArray; | |
151 import dmd.TemplateInstance; | |
152 import dmd.TypeIdentifier; | |
153 import dmd.VarDeclaration; | |
154 import dmd.TypeFunction; | |
155 import dmd.TypeDelegate; | |
156 import dmd.TY; | |
157 import dmd.LinkDeclaration; | |
158 import dmd.Declaration; | |
159 import dmd.AggregateDeclaration; | |
160 import dmd.TypedefDeclaration; | |
161 import dmd.AliasDeclaration; | |
162 import dmd.LINK; | |
163 import dmd.Loc; | |
164 import dmd.Module; | |
165 import dmd.Array; | |
166 import dmd.Expression; | |
167 import dmd.TemplateDeclaration; | |
168 import dmd.ArrayTypes; | |
169 import dmd.Dsymbol; | |
170 import dmd.StaticAssert; | |
171 import dmd.TypeQualified; | |
172 import dmd.Condition; | |
173 import dmd.PostBlitDeclaration; | |
174 import dmd.DtorDeclaration; | |
175 import dmd.ConditionalDeclaration; | |
176 import dmd.StaticCtorDeclaration; | |
177 import dmd.StaticDtorDeclaration; | |
178 import dmd.InvariantDeclaration; | |
179 import dmd.UnitTestDeclaration; | |
180 import dmd.NewDeclaration; | |
181 import dmd.DeleteDeclaration; | |
182 import dmd.EnumDeclaration; | |
183 import dmd.Import; | |
184 import dmd.Type; | |
185 import dmd.Identifier; | |
186 import dmd.FuncDeclaration; | |
187 import dmd.Statement; | |
188 import dmd.Initializer; | |
189 import dmd.Token; | |
190 import dmd.TOK; | |
191 import dmd.ParseStatementFlags; | |
192 import dmd.PROT; | |
193 import dmd.STC; | |
194 import dmd.Util; | |
195 import dmd.CompileDeclaration; | |
196 import dmd.StaticIfDeclaration; | |
197 import dmd.StorageClassDeclaration; | |
198 import dmd.LinkDeclaration; | |
199 import dmd.ProtDeclaration; | |
200 import dmd.AlignDeclaration; | |
201 import dmd.PragmaDeclaration; | |
202 import dmd.DebugSymbol; | |
203 import dmd.VersionSymbol; | |
204 import dmd.AliasThis; | |
205 import dmd.Global; | |
206 | |
207 import core.stdc.string : memcpy; | |
208 | |
34 | 209 import std.exception; |
4 | 210 import core.memory; |
0 | 211 |
212 class Parser : Lexer | |
213 { | |
214 ModuleDeclaration md; | |
215 LINK linkage; | |
216 Loc endloc; // set to location of last right curly | |
217 int inBrackets; // inside [] of array index or slice | |
218 | |
219 this(Module module_, ubyte* base, uint length, int doDocComment) | |
220 { | |
221 super(module_, base, 0, length, doDocComment, 0); | |
222 //printf("Parser.Parser()\n"); | |
223 linkage = LINK.LINKd; | |
224 //nextToken(); // start up the scanner | |
225 } | |
226 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
227 Dsymbols parseModule() |
0 | 228 { |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
229 typeof(return) decldefs; |
0 | 230 |
231 // ModuleDeclation leads off | |
232 if (token.value == TOK.TOKmodule) | |
233 { | |
79 | 234 string comment = token.blockComment; |
0 | 235 bool safe = false; |
236 | |
237 nextToken(); | |
238 version (DMDV2) { | |
239 if (token.value == TOK.TOKlparen) | |
240 { | |
241 nextToken(); | |
242 if (token.value != TOK.TOKidentifier) | |
243 { | |
244 error("module (system) identifier expected"); | |
245 goto Lerr; | |
246 } | |
247 Identifier id = token.ident; | |
248 | |
249 if (id is Id.system) | |
250 safe = true; | |
251 else | |
252 error("(safe) expected, not %s", id.toChars()); | |
253 nextToken(); | |
254 check(TOK.TOKrparen); | |
255 } | |
256 } | |
257 | |
258 if (token.value != TOK.TOKidentifier) | |
259 { | |
260 error("Identifier expected following module"); | |
261 goto Lerr; | |
262 } | |
263 else | |
264 { | |
265 Array a = null; | |
266 Identifier id = token.ident; | |
267 while (nextToken() == TOK.TOKdot) | |
268 { | |
269 if (!a) | |
270 a = new Array(); | |
271 a.push(cast(void*)id); | |
272 nextToken(); | |
273 if (token.value != TOK.TOKidentifier) | |
274 { error("Identifier expected following package"); | |
275 goto Lerr; | |
276 } | |
277 id = token.ident; | |
278 } | |
279 | |
280 md = new ModuleDeclaration(a, id, safe); | |
281 | |
282 if (token.value != TOK.TOKsemicolon) | |
283 error("';' expected following module declaration instead of %s", token.toChars()); | |
284 | |
285 nextToken(); | |
286 addComment(mod, comment); | |
287 } | |
288 } | |
289 | |
290 decldefs = parseDeclDefs(0); | |
291 if (token.value != TOK.TOKeof) | |
292 { | |
293 error("unrecognized declaration"); | |
294 goto Lerr; | |
295 } | |
296 | |
297 return decldefs; | |
298 | |
299 Lerr: | |
300 while (token.value != TOK.TOKsemicolon && token.value != TOK.TOKeof) | |
301 nextToken(); | |
302 | |
303 nextToken(); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
304 return new Dsymbols(); |
0 | 305 } |
306 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
307 Dsymbols parseDeclDefs(int once) |
0 | 308 { |
309 Dsymbol s; | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
310 Dsymbols decldefs; |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
311 Dsymbols a; |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
312 Dsymbols aelse; |
0 | 313 PROT prot; |
314 STC stc; | |
315 STC storageClass; | |
316 Condition condition; | |
79 | 317 string comment; |
0 | 318 |
319 //printf("Parser.parseDeclDefs()\n"); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
320 decldefs = new Dsymbols(); |
0 | 321 do |
322 { | |
323 comment = token.blockComment; | |
324 storageClass = STC.STCundefined; | |
325 switch (token.value) | |
326 { | |
327 case TOK.TOKenum: | |
328 { /* Determine if this is a manifest constant declaration, | |
329 * or a conventional enum. | |
330 */ | |
331 Token *t = peek(&token); | |
332 if (t.value == TOK.TOKlcurly || t.value == TOK.TOKcolon) | |
333 s = parseEnum(); | |
334 else if (t.value != TOK.TOKidentifier) | |
335 goto Ldeclaration; | |
336 else | |
337 { | |
338 t = peek(t); | |
339 if (t.value == TOK.TOKlcurly || t.value == TOK.TOKcolon || | |
340 t.value == TOK.TOKsemicolon) | |
341 s = parseEnum(); | |
342 else | |
343 goto Ldeclaration; | |
344 } | |
345 break; | |
346 } | |
347 | |
348 case TOK.TOKstruct: | |
349 case TOK.TOKunion: | |
350 case TOK.TOKclass: | |
351 case TOK.TOKinterface: | |
352 s = parseAggregate(); | |
353 break; | |
354 | |
355 case TOK.TOKimport: | |
356 s = parseImport(decldefs, 0); | |
357 break; | |
358 | |
359 case TOK.TOKtemplate: | |
360 s = cast(Dsymbol)parseTemplateDeclaration(); | |
361 break; | |
362 | |
363 case TOK.TOKmixin: | |
364 { Loc loc = this.loc; | |
365 if (peek(&token).value == TOK.TOKlparen) | |
366 { // mixin(string) | |
367 nextToken(); | |
368 check(TOK.TOKlparen, "mixin"); | |
369 Expression e = parseAssignExp(); | |
370 check(TOK.TOKrparen); | |
371 check(TOK.TOKsemicolon); | |
372 s = new CompileDeclaration(loc, e); | |
373 break; | |
374 } | |
375 s = parseMixin(); | |
376 break; | |
377 } | |
378 | |
379 case TOK.TOKwchar: case TOK.TOKdchar: | |
380 case TOK.TOKbit: case TOK.TOKbool: case TOK.TOKchar: | |
381 case TOK.TOKint8: case TOK.TOKuns8: | |
382 case TOK.TOKint16: case TOK.TOKuns16: | |
383 case TOK.TOKint32: case TOK.TOKuns32: | |
384 case TOK.TOKint64: case TOK.TOKuns64: | |
385 case TOK.TOKfloat32: case TOK.TOKfloat64: case TOK.TOKfloat80: | |
386 case TOK.TOKimaginary32: case TOK.TOKimaginary64: case TOK.TOKimaginary80: | |
387 case TOK.TOKcomplex32: case TOK.TOKcomplex64: case TOK.TOKcomplex80: | |
388 case TOK.TOKvoid: | |
389 case TOK.TOKalias: | |
390 case TOK.TOKtypedef: | |
391 case TOK.TOKidentifier: | |
392 case TOK.TOKtypeof: | |
393 case TOK.TOKdot: | |
394 Ldeclaration: | |
395 a = parseDeclarations(STC.STCundefined); | |
396 decldefs.append(a); | |
397 continue; | |
398 | |
399 case TOK.TOKthis: | |
400 s = parseCtor(); | |
401 break; | |
402 | |
403 static if (false) { // dead end, use this(this){} instead | |
404 case TOK.TOKassign: | |
405 s = parsePostBlit(); | |
406 break; | |
407 } | |
408 case TOK.TOKtilde: | |
409 s = parseDtor(); | |
410 break; | |
411 | |
412 case TOK.TOKinvariant: | |
413 { Token *t; | |
414 t = peek(&token); | |
415 if (t.value == TOK.TOKlparen) | |
416 { | |
417 if (peek(t).value == TOK.TOKrparen) | |
418 // invariant() forms start of class invariant | |
419 s = parseInvariant(); | |
420 else | |
421 // invariant(type) | |
422 goto Ldeclaration; | |
423 } | |
424 else | |
425 { | |
426 stc = STC.STCimmutable; | |
427 goto Lstc; | |
428 } | |
429 break; | |
430 } | |
431 | |
432 case TOK.TOKunittest: | |
433 s = parseUnitTest(); | |
434 break; | |
435 | |
436 case TOK.TOKnew: | |
437 s = parseNew(); | |
438 break; | |
439 | |
440 case TOK.TOKdelete: | |
441 s = parseDelete(); | |
442 break; | |
443 | |
444 case TOK.TOKeof: | |
445 case TOK.TOKrcurly: | |
446 return decldefs; | |
447 | |
448 case TOK.TOKstatic: | |
449 nextToken(); | |
450 if (token.value == TOK.TOKthis) | |
451 s = parseStaticCtor(); | |
452 else if (token.value == TOK.TOKtilde) | |
453 s = parseStaticDtor(); | |
454 else if (token.value == TOK.TOKassert) | |
455 s = parseStaticAssert(); | |
456 else if (token.value == TOK.TOKif) | |
457 { condition = parseStaticIfCondition(); | |
458 a = parseBlock(); | |
459 aelse = null; | |
460 if (token.value == TOK.TOKelse) | |
461 { nextToken(); | |
462 aelse = parseBlock(); | |
463 } | |
464 s = new StaticIfDeclaration(condition, a, aelse); | |
465 break; | |
466 } | |
467 else if (token.value == TOK.TOKimport) | |
468 { | |
469 s = parseImport(decldefs, 1); | |
470 } | |
471 else | |
472 { stc = STC.STCstatic; | |
473 goto Lstc2; | |
474 } | |
475 break; | |
476 | |
477 case TOK.TOKconst: | |
478 if (peek(&token).value == TOK.TOKlparen) | |
479 goto Ldeclaration; | |
480 stc = STC.STCconst; | |
481 goto Lstc; | |
482 | |
483 case TOK.TOKimmutable: | |
484 if (peek(&token).value == TOK.TOKlparen) | |
485 goto Ldeclaration; | |
486 stc = STC.STCimmutable; | |
487 goto Lstc; | |
488 | |
489 case TOK.TOKshared: | |
490 if (peek(&token).value == TOK.TOKlparen) | |
491 goto Ldeclaration; | |
492 stc = STC.STCshared; | |
493 goto Lstc; | |
494 | |
495 case TOK.TOKfinal: stc = STC.STCfinal; goto Lstc; | |
496 case TOK.TOKauto: stc = STC.STCauto; goto Lstc; | |
497 case TOK.TOKscope: stc = STC.STCscope; goto Lstc; | |
498 case TOK.TOKoverride: stc = STC.STCoverride; goto Lstc; | |
499 case TOK.TOKabstract: stc = STC.STCabstract; goto Lstc; | |
500 case TOK.TOKsynchronized: stc = STC.STCsynchronized; goto Lstc; | |
501 case TOK.TOKdeprecated: stc = STC.STCdeprecated; goto Lstc; | |
502 version (DMDV2) { | |
503 case TOK.TOKnothrow: stc = STC.STCnothrow; goto Lstc; | |
504 case TOK.TOKpure: stc = STC.STCpure; goto Lstc; | |
505 case TOK.TOKref: stc = STC.STCref; goto Lstc; | |
506 case TOK.TOKtls: stc = STC.STCtls; goto Lstc; | |
507 case TOK.TOKgshared: | |
508 stc = STC.STCgshared; goto Lstc; | |
509 //case TOK.TOKmanifest: stc = STC.STCmanifest; goto Lstc; | |
510 } | |
511 | |
512 Lstc: | |
513 if (storageClass & stc) | |
514 error("redundant storage class %s", Token.toChars(token.value)); | |
515 composeStorageClass(storageClass | stc); | |
516 nextToken(); | |
517 Lstc2: | |
518 storageClass |= stc; | |
519 switch (token.value) | |
520 { | |
521 case TOK.TOKconst: | |
522 case TOK.TOKinvariant: | |
523 case TOK.TOKimmutable: | |
524 case TOK.TOKshared: | |
525 // If followed by a (, it is not a storage class | |
526 if (peek(&token).value == TOK.TOKlparen) | |
527 break; | |
528 if (token.value == TOK.TOKconst) | |
529 stc = STC.STCconst; | |
530 else if (token.value == TOK.TOKshared) | |
531 stc = STC.STCshared; | |
532 else | |
533 stc = STC.STCimmutable; | |
534 goto Lstc; | |
535 case TOK.TOKfinal: stc = STC.STCfinal; goto Lstc; | |
536 case TOK.TOKauto: stc = STC.STCauto; goto Lstc; | |
537 case TOK.TOKscope: stc = STC.STCscope; goto Lstc; | |
538 case TOK.TOKoverride: stc = STC.STCoverride; goto Lstc; | |
539 case TOK.TOKabstract: stc = STC.STCabstract; goto Lstc; | |
540 case TOK.TOKsynchronized: stc = STC.STCsynchronized; goto Lstc; | |
541 case TOK.TOKdeprecated: stc = STC.STCdeprecated; goto Lstc; | |
542 case TOK.TOKnothrow: stc = STC.STCnothrow; goto Lstc; | |
543 case TOK.TOKpure: stc = STC.STCpure; goto Lstc; | |
544 case TOK.TOKref: stc = STC.STCref; goto Lstc; | |
545 case TOK.TOKtls: stc = STC.STCtls; goto Lstc; | |
546 case TOK.TOKgshared: stc = STC.STCgshared; goto Lstc; | |
547 //case TOK.TOKmanifest: stc = STC.STCmanifest; goto Lstc; | |
548 default: | |
549 break; | |
550 } | |
551 | |
552 /* Look for auto initializers: | |
553 * storage_class identifier = initializer; | |
554 */ | |
555 if (token.value == TOK.TOKidentifier && | |
556 peek(&token).value == TOK.TOKassign) | |
557 { | |
558 a = parseAutoDeclarations(storageClass, comment); | |
559 decldefs.append(a); | |
560 continue; | |
561 } | |
562 | |
563 /* Look for return type inference for template functions. | |
564 */ | |
565 Token *tk; | |
566 if (token.value == TOK.TOKidentifier && | |
567 (tk = peek(&token)).value == TOK.TOKlparen && | |
568 skipParens(tk, &tk) && | |
569 (peek(tk).value == TOK.TOKlparen || | |
570 peek(tk).value == TOK.TOKlcurly) | |
571 ) | |
572 { | |
573 a = parseDeclarations(storageClass); | |
574 decldefs.append(a); | |
575 continue; | |
576 } | |
577 a = parseBlock(); | |
578 s = new StorageClassDeclaration(storageClass, a); | |
579 break; | |
580 | |
581 case TOK.TOKextern: | |
582 if (peek(&token).value != TOK.TOKlparen) | |
583 { | |
584 stc = STC.STCextern; | |
585 goto Lstc; | |
586 } | |
587 { | |
588 LINK linksave = linkage; | |
589 linkage = parseLinkage(); | |
590 a = parseBlock(); | |
591 s = new LinkDeclaration(linkage, a); | |
592 linkage = linksave; | |
593 break; | |
594 } | |
595 | |
596 case TOK.TOKprivate: prot = PROT.PROTprivate; goto Lprot; | |
597 case TOK.TOKpackage: prot = PROT.PROTpackage; goto Lprot; | |
598 case TOK.TOKprotected: prot = PROT.PROTprotected; goto Lprot; | |
599 case TOK.TOKpublic: prot = PROT.PROTpublic; goto Lprot; | |
600 case TOK.TOKexport: prot = PROT.PROTexport; goto Lprot; | |
601 Lprot: | |
602 nextToken(); | |
603 switch (token.value) | |
604 { | |
605 case TOK.TOKprivate: | |
606 case TOK.TOKpackage: | |
607 case TOK.TOKprotected: | |
608 case TOK.TOKpublic: | |
609 case TOK.TOKexport: | |
610 error("redundant protection attribute"); | |
611 break; | |
612 default: | |
613 break; | |
614 } | |
615 a = parseBlock(); | |
616 s = new ProtDeclaration(prot, a); | |
617 break; | |
618 | |
619 case TOK.TOKalign: | |
620 { uint n; | |
621 | |
622 s = null; | |
623 nextToken(); | |
624 if (token.value == TOK.TOKlparen) | |
625 { | |
626 nextToken(); | |
627 if (token.value == TOK.TOKint32v) | |
628 n = cast(uint)token.uns64value; | |
629 else | |
630 { error("integer expected, not %s", token.toChars()); | |
631 n = 1; | |
632 } | |
633 nextToken(); | |
634 check(TOK.TOKrparen); | |
635 } | |
636 else | |
637 n = global.structalign; // default | |
638 | |
639 a = parseBlock(); | |
640 s = new AlignDeclaration(n, a); | |
641 break; | |
642 } | |
643 | |
644 case TOK.TOKpragma: | |
645 { Identifier ident; | |
646 Expressions args = null; | |
647 | |
648 nextToken(); | |
649 check(TOK.TOKlparen); | |
650 if (token.value != TOK.TOKidentifier) | |
651 { error("pragma(identifier expected"); | |
652 goto Lerror; | |
653 } | |
654 ident = token.ident; | |
655 nextToken(); | |
656 if (token.value == TOK.TOKcomma && peekNext() != TOK.TOKrparen) | |
657 args = parseArguments(); // pragma(identifier, args...) | |
658 else | |
659 check(TOK.TOKrparen); // pragma(identifier) | |
660 | |
661 if (token.value == TOK.TOKsemicolon) | |
662 a = null; | |
663 else | |
664 a = parseBlock(); | |
665 s = new PragmaDeclaration(loc, ident, args, a); | |
666 break; | |
667 } | |
668 | |
669 case TOK.TOKdebug: | |
670 nextToken(); | |
671 if (token.value == TOK.TOKassign) | |
672 { | |
673 nextToken(); | |
674 if (token.value == TOK.TOKidentifier) | |
675 s = new DebugSymbol(loc, token.ident); | |
676 else if (token.value == TOK.TOKint32v) | |
677 s = new DebugSymbol(loc, cast(uint)token.uns64value); | |
678 else | |
679 { error("identifier or integer expected, not %s", token.toChars()); | |
680 s = null; | |
681 } | |
682 nextToken(); | |
683 if (token.value != TOK.TOKsemicolon) | |
684 error("semicolon expected"); | |
685 nextToken(); | |
686 break; | |
687 } | |
688 | |
689 condition = parseDebugCondition(); | |
690 goto Lcondition; | |
691 | |
692 case TOK.TOKversion: | |
693 nextToken(); | |
694 if (token.value == TOK.TOKassign) | |
695 { | |
696 nextToken(); | |
697 if (token.value == TOK.TOKidentifier) | |
698 s = new VersionSymbol(loc, token.ident); | |
699 else if (token.value == TOK.TOKint32v) | |
700 s = new VersionSymbol(loc, cast(uint)token.uns64value); | |
701 else | |
702 { error("identifier or integer expected, not %s", token.toChars()); | |
703 s = null; | |
704 } | |
705 nextToken(); | |
706 if (token.value != TOK.TOKsemicolon) | |
707 error("semicolon expected"); | |
708 nextToken(); | |
709 break; | |
710 } | |
711 condition = parseVersionCondition(); | |
712 goto Lcondition; | |
713 | |
714 Lcondition: | |
715 a = parseBlock(); | |
716 aelse = null; | |
717 if (token.value == TOK.TOKelse) | |
718 { nextToken(); | |
719 aelse = parseBlock(); | |
720 } | |
721 s = new ConditionalDeclaration(condition, a, aelse); | |
722 break; | |
723 | |
724 case TOK.TOKsemicolon: // empty declaration | |
725 nextToken(); | |
726 continue; | |
727 | |
728 default: | |
729 error("Declaration expected, not '%s'",token.toChars()); | |
730 Lerror: | |
731 while (token.value != TOK.TOKsemicolon && token.value != TOK.TOKeof) | |
732 nextToken(); | |
733 nextToken(); | |
734 s = null; | |
735 continue; | |
736 } | |
737 if (s) | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
738 { decldefs.push(s); |
0 | 739 addComment(s, comment); |
740 } | |
741 } while (!once); | |
742 return decldefs; | |
743 } | |
744 | |
745 /***************************************** | |
746 * Parse auto declarations of the form: | |
747 * storageClass ident = init, ident = init, ... ; | |
748 * and return the array of them. | |
749 * Starts with token on the first ident. | |
750 * Ends with scanner past closing ';' | |
751 */ | |
79 | 752 version (DMDV2) |
753 { | |
754 Dsymbols parseAutoDeclarations(STC storageClass, const(char)[] comment) | |
0 | 755 { |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
756 auto a = new Dsymbols; |
0 | 757 |
758 while (true) | |
759 { | |
760 Identifier ident = token.ident; | |
761 nextToken(); // skip over ident | |
762 assert(token.value == TOKassign); | |
763 nextToken(); // skip over '=' | |
764 Initializer init = parseInitializer(); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
765 auto v = new VarDeclaration(loc, null, ident, init); |
0 | 766 v.storage_class = storageClass; |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
767 a.push(v); |
0 | 768 if (token.value == TOKsemicolon) |
769 { | |
770 nextToken(); | |
771 addComment(v, comment); | |
772 } | |
773 else if (token.value == TOKcomma) | |
774 { | |
775 nextToken(); | |
776 if (token.value == TOKidentifier && | |
777 peek(&token).value == TOKassign) | |
778 { | |
779 addComment(v, comment); | |
780 continue; | |
781 } | |
782 else | |
783 error("Identifier expected following comma"); | |
784 } | |
785 else | |
786 error("semicolon expected following auto declaration, not '%s'", token.toChars()); | |
787 break; | |
788 } | |
789 return a; | |
790 } | |
791 } | |
792 /******************************************** | |
793 * Parse declarations after an align, protection, or extern decl. | |
794 */ | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
795 Dsymbols parseBlock() |
0 | 796 { |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
797 Dsymbols a = null; |
0 | 798 Dsymbol ss; |
799 | |
800 //printf("parseBlock()\n"); | |
801 switch (token.value) | |
802 { | |
803 case TOK.TOKsemicolon: | |
804 error("declaration expected following attribute, not ';'"); | |
805 nextToken(); | |
806 break; | |
807 | |
808 case TOK.TOKeof: | |
809 error("declaration expected following attribute, not EOF"); | |
810 break; | |
811 | |
812 case TOK.TOKlcurly: | |
813 nextToken(); | |
814 a = parseDeclDefs(0); | |
815 if (token.value != TOK.TOKrcurly) | |
816 { /* { */ | |
817 error("matching '}' expected, not %s", token.toChars()); | |
818 } | |
819 else | |
820 nextToken(); | |
821 break; | |
822 | |
823 case TOK.TOKcolon: | |
824 nextToken(); | |
825 static if (false) { | |
826 a = null; | |
827 } else { | |
828 a = parseDeclDefs(0); // grab declarations up to closing curly bracket | |
829 } | |
830 break; | |
831 | |
832 default: | |
833 a = parseDeclDefs(1); | |
834 break; | |
835 } | |
836 return a; | |
837 } | |
838 | |
839 void composeStorageClass(STC stc) | |
840 { | |
841 STC u = stc; | |
842 u &= STC.STCconst | STC.STCimmutable | STC.STCmanifest; | |
843 if (u & (u - 1)) | |
844 error("conflicting storage class %s", Token.toChars(token.value)); | |
845 | |
846 u = stc; | |
847 u &= STC.STCgshared | STC.STCshared | STC.STCtls; | |
848 if (u & (u - 1)) | |
849 error("conflicting storage class %s", Token.toChars(token.value)); | |
850 } | |
851 | |
852 /************************************** | |
853 * Parse constraint. | |
854 * Constraint is of the form: | |
855 * if ( ConstraintExpression ) | |
856 */ | |
857 version (DMDV2) { | |
858 Expression parseConstraint() | |
859 { | |
860 Expression e = null; | |
861 | |
862 if (token.value == TOKif) | |
863 { | |
864 nextToken(); // skip over 'if' | |
865 check(TOKlparen); | |
866 e = parseExpression(); | |
867 check(TOKrparen); | |
868 } | |
869 return e; | |
870 } | |
871 } | |
872 /************************************** | |
873 * Parse a TemplateDeclaration. | |
874 */ | |
875 TemplateDeclaration parseTemplateDeclaration() | |
876 { | |
877 TemplateDeclaration tempdecl; | |
878 Identifier id; | |
879 TemplateParameters tpl; | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
880 Dsymbols decldefs; |
0 | 881 Expression constraint = null; |
882 Loc loc = this.loc; | |
883 | |
884 nextToken(); | |
885 if (token.value != TOKidentifier) | |
886 { | |
887 error("TemplateIdentifier expected following template"); | |
888 goto Lerr; | |
889 } | |
890 id = token.ident; | |
891 nextToken(); | |
892 tpl = parseTemplateParameterList(); | |
893 if (!tpl) | |
894 goto Lerr; | |
895 | |
896 constraint = parseConstraint(); | |
897 | |
898 if (token.value != TOKlcurly) | |
899 { | |
900 error("members of template declaration expected"); | |
901 goto Lerr; | |
902 } | |
903 else | |
904 { | |
905 nextToken(); | |
906 decldefs = parseDeclDefs(0); | |
907 if (token.value != TOKrcurly) | |
908 { | |
909 error("template member expected"); | |
910 goto Lerr; | |
911 } | |
912 nextToken(); | |
913 } | |
914 | |
915 tempdecl = new TemplateDeclaration(loc, id, tpl, constraint, decldefs); | |
916 return tempdecl; | |
917 | |
918 Lerr: | |
919 return null; | |
920 } | |
921 | |
922 /****************************************** | |
923 * Parse template parameter list. | |
924 * Input: | |
925 * flag 0: parsing "( list )" | |
926 * 1: parsing non-empty "list )" | |
927 */ | |
928 TemplateParameters parseTemplateParameterList(int flag = 0) | |
929 { | |
930 TemplateParameters tpl = new TemplateParameters(); | |
931 | |
932 if (!flag && token.value != TOKlparen) | |
933 { | |
934 error("parenthesized TemplateParameterList expected following TemplateIdentifier"); | |
935 goto Lerr; | |
936 } | |
937 nextToken(); | |
938 | |
939 // Get array of TemplateParameters | |
940 if (flag || token.value != TOKrparen) | |
941 { | |
942 int isvariadic = 0; | |
943 | |
944 while (true) | |
945 { | |
946 TemplateParameter tp; | |
947 Identifier tp_ident = null; | |
948 Type tp_spectype = null; | |
949 Type tp_valtype = null; | |
950 Type tp_defaulttype = null; | |
951 Expression tp_specvalue = null; | |
952 Expression tp_defaultvalue = null; | |
953 Token* t; | |
954 | |
955 // Get TemplateParameter | |
956 | |
957 // First, look ahead to see if it is a TypeParameter or a ValueParameter | |
958 t = peek(&token); | |
959 if (token.value == TOKalias) | |
960 { | |
961 // AliasParameter | |
962 nextToken(); | |
963 Type spectype = null; | |
964 if (isDeclaration(&token, 2, TOKreserved, null)) | |
965 { | |
966 spectype = parseType(&tp_ident); | |
967 } | |
968 else | |
969 { | |
970 if (token.value != TOKidentifier) | |
971 { | |
972 error("identifier expected for template alias parameter"); | |
973 goto Lerr; | |
974 } | |
975 tp_ident = token.ident; | |
976 nextToken(); | |
977 } | |
978 Object spec = null; | |
979 if (token.value == TOKcolon) // : Type | |
980 { | |
981 nextToken(); | |
982 if (isDeclaration(&token, 0, TOKreserved, null)) | |
983 spec = parseType(); | |
984 else | |
985 spec = parseCondExp(); | |
986 } | |
987 Object def = null; | |
988 if (token.value == TOKassign) // = Type | |
989 { | |
990 nextToken(); | |
991 if (isDeclaration(&token, 0, TOKreserved, null)) | |
992 def = parseType(); | |
993 else | |
994 def = parseCondExp(); | |
995 } | |
996 tp = new TemplateAliasParameter(loc, tp_ident, spectype, spec, def); | |
997 } | |
998 else if (t.value == TOKcolon || t.value == TOKassign || | |
999 t.value == TOKcomma || t.value == TOKrparen) | |
1000 { // TypeParameter | |
1001 if (token.value != TOKidentifier) | |
1002 { error("identifier expected for template type parameter"); | |
1003 goto Lerr; | |
1004 } | |
1005 tp_ident = token.ident; | |
1006 nextToken(); | |
1007 if (token.value == TOKcolon) // : Type | |
1008 { | |
1009 nextToken(); | |
1010 tp_spectype = parseType(); | |
1011 } | |
1012 if (token.value == TOKassign) // = Type | |
1013 { | |
1014 nextToken(); | |
1015 tp_defaulttype = parseType(); | |
1016 } | |
1017 tp = new TemplateTypeParameter(loc, tp_ident, tp_spectype, tp_defaulttype); | |
1018 } | |
1019 else if (token.value == TOKidentifier && t.value == TOKdotdotdot) | |
1020 { // ident... | |
1021 if (isvariadic) | |
1022 error("variadic template parameter must be last"); | |
1023 isvariadic = 1; | |
1024 tp_ident = token.ident; | |
1025 nextToken(); | |
1026 nextToken(); | |
1027 tp = new TemplateTupleParameter(loc, tp_ident); | |
1028 } | |
1029 /// version (DMDV2) { | |
1030 else if (token.value == TOKthis) | |
1031 { // ThisParameter | |
1032 nextToken(); | |
1033 if (token.value != TOKidentifier) | |
1034 { error("identifier expected for template this parameter"); | |
1035 goto Lerr; | |
1036 } | |
1037 tp_ident = token.ident; | |
1038 nextToken(); | |
1039 if (token.value == TOKcolon) // : Type | |
1040 { | |
1041 nextToken(); | |
1042 tp_spectype = parseType(); | |
1043 } | |
1044 if (token.value == TOKassign) // = Type | |
1045 { | |
1046 nextToken(); | |
1047 tp_defaulttype = parseType(); | |
1048 } | |
1049 tp = new TemplateThisParameter(loc, tp_ident, tp_spectype, tp_defaulttype); | |
1050 } | |
1051 /// } | |
1052 else | |
1053 { // ValueParameter | |
1054 tp_valtype = parseType(&tp_ident); | |
1055 if (!tp_ident) | |
1056 { | |
1057 error("identifier expected for template value parameter"); | |
1058 tp_ident = new Identifier("error", TOKidentifier); | |
1059 } | |
1060 if (token.value == TOKcolon) // : CondExpression | |
1061 { | |
1062 nextToken(); | |
1063 tp_specvalue = parseCondExp(); | |
1064 } | |
1065 if (token.value == TOKassign) // = CondExpression | |
1066 { | |
1067 nextToken(); | |
1068 tp_defaultvalue = parseDefaultInitExp(); | |
1069 } | |
1070 tp = new TemplateValueParameter(loc, tp_ident, tp_valtype, tp_specvalue, tp_defaultvalue); | |
1071 } | |
121
347de076ad34
TemplateParameters -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1072 tpl.push(tp); |
0 | 1073 if (token.value != TOKcomma) |
1074 break; | |
1075 nextToken(); | |
1076 } | |
1077 } | |
1078 check(TOKrparen); | |
1079 | |
1080 Lerr: | |
1081 return tpl; | |
1082 } | |
19
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1083 |
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1084 /****************************************** |
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1085 * Parse template mixin. |
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1086 * mixin Foo; |
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1087 * mixin Foo!(args); |
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1088 * mixin a.b.c!(args).Foo!(args); |
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1089 * mixin Foo!(args) identifier; |
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1090 * mixin typeof(expr).identifier!(args); |
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1091 */ |
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1092 |
0 | 1093 Dsymbol parseMixin() |
1094 { | |
13 | 1095 TemplateMixin tm; |
1096 Identifier id; | |
1097 Type tqual; | |
1098 Objects tiargs; | |
1099 Array idents; | |
1100 | |
1101 //printf("parseMixin()\n"); | |
1102 nextToken(); | |
1103 tqual = null; | |
1104 if (token.value == TOKdot) | |
1105 { | |
1106 id = Id.empty; | |
1107 } | |
1108 else | |
1109 { | |
1110 if (token.value == TOKtypeof) | |
1111 { | |
1112 tqual = parseTypeof(); | |
1113 check(TOKdot); | |
1114 } | |
1115 if (token.value != TOKidentifier) | |
1116 { | |
1117 error("identifier expected, not %s", token.toChars()); | |
1118 id = Id.empty; | |
1119 } | |
1120 else | |
1121 id = token.ident; | |
1122 nextToken(); | |
1123 } | |
1124 | |
1125 idents = new Array(); | |
1126 while (1) | |
1127 { | |
1128 tiargs = null; | |
1129 if (token.value == TOKnot) | |
1130 { | |
1131 nextToken(); | |
1132 if (token.value == TOKlparen) | |
1133 tiargs = parseTemplateArgumentList(); | |
1134 else | |
1135 tiargs = parseTemplateArgument(); | |
1136 } | |
1137 if (token.value != TOKdot) | |
1138 break; | |
1139 if (tiargs) | |
1140 { | |
1141 TemplateInstance tempinst = new TemplateInstance(loc, id); | |
1142 tempinst.tiargs = tiargs; | |
1143 id = cast(Identifier)tempinst; | |
1144 tiargs = null; | |
1145 } | |
1146 idents.push(cast(void*)id); | |
1147 nextToken(); | |
1148 if (token.value != TOKidentifier) | |
1149 { | |
1150 error("identifier expected following '.' instead of '%s'", token.toChars()); | |
1151 break; | |
1152 } | |
1153 id = token.ident; | |
1154 nextToken(); | |
1155 } | |
1156 idents.push(cast(void*)id); | |
1157 if (token.value == TOKidentifier) | |
1158 { | |
1159 id = token.ident; | |
1160 nextToken(); | |
1161 } | |
1162 else | |
1163 id = null; | |
1164 | |
1165 tm = new TemplateMixin(loc, id, tqual, idents, tiargs); | |
1166 if (token.value != TOKsemicolon) | |
1167 error("';' expected after mixin"); | |
1168 nextToken(); | |
1169 return tm; | |
0 | 1170 } |
1171 | |
1172 /****************************************** | |
1173 * Parse template argument list. | |
1174 * Input: | |
1175 * current token is opening '(' | |
1176 * Output: | |
1177 * current token is one after closing ')' | |
1178 */ | |
1179 Objects parseTemplateArgumentList() | |
1180 { | |
1181 //printf("Parser.parseTemplateArgumentList()\n"); | |
1182 if (token.value != TOKlparen && token.value != TOKlcurly) | |
1183 { | |
1184 error("!(TemplateArgumentList) expected following TemplateIdentifier"); | |
1185 return new Objects(); | |
1186 } | |
1187 return parseTemplateArgumentList2(); | |
1188 } | |
1189 | |
1190 Objects parseTemplateArgumentList2() | |
1191 { | |
1192 //printf("Parser.parseTemplateArgumentList2()\n"); | |
1193 Objects tiargs = new Objects(); | |
1194 TOK endtok = TOKrparen; | |
1195 nextToken(); | |
1196 | |
1197 // Get TemplateArgumentList | |
1198 if (token.value != endtok) | |
1199 { | |
1200 while (1) | |
1201 { | |
1202 // See if it is an Expression or a Type | |
1203 if (isDeclaration(&token, 0, TOKreserved, null)) | |
1204 { // Template argument is a type | |
1205 Type ta = parseType(); | |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
1206 tiargs.push(ta); |
0 | 1207 } |
1208 else | |
1209 { // Template argument is an expression | |
1210 Expression ea = parseAssignExp(); | |
1211 | |
1212 if (ea.op == TOKfunction) | |
1213 { | |
1214 FuncLiteralDeclaration fd = (cast(FuncExp)ea).fd; | |
1215 if (fd.type.ty == Tfunction) | |
1216 { | |
1217 TypeFunction tf = cast(TypeFunction)fd.type; | |
1218 /* If there are parameters that consist of only an identifier, | |
1219 * rather than assuming the identifier is a type, as we would | |
1220 * for regular function declarations, assume the identifier | |
1221 * is the parameter name, and we're building a template with | |
1222 * a deduced type. | |
1223 */ | |
1224 TemplateParameters tpl = null; | |
1225 for (int i = 0; i < tf.parameters.dim; i++) | |
1226 { | |
1227 Argument param = cast(Argument)tf.parameters.data[i]; | |
1228 if (param.ident is null && | |
1229 param.type && | |
1230 param.type.ty == Tident && | |
1231 (cast(TypeIdentifier)param.type).idents.dim == 0 | |
1232 ) | |
1233 { | |
1234 /* Switch parameter type to parameter identifier, | |
1235 * parameterize with template type parameter _T | |
1236 */ | |
1237 TypeIdentifier pt = cast(TypeIdentifier)param.type; | |
1238 param.ident = pt.ident; | |
1239 Identifier id = Lexer.uniqueId("__T"); | |
1240 param.type = new TypeIdentifier(pt.loc, id); | |
121
347de076ad34
TemplateParameters -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1241 auto tp = new TemplateTypeParameter(fd.loc, id, null, null); |
0 | 1242 if (!tpl) |
1243 tpl = new TemplateParameters(); | |
121
347de076ad34
TemplateParameters -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1244 tpl.push(tp); |
0 | 1245 } |
1246 } | |
1247 | |
1248 if (tpl) | |
1249 { | |
1250 // Wrap a template around function fd | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
1251 auto decldefs = new Dsymbols(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
1252 decldefs.push(fd); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
1253 auto tempdecl = new TemplateDeclaration(fd.loc, fd.ident, tpl, null, decldefs); |
0 | 1254 tempdecl.literal = 1; // it's a template 'literal' |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
1255 tiargs.push(tempdecl); |
0 | 1256 goto L1; |
1257 } | |
1258 } | |
1259 } | |
1260 | |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
1261 tiargs.push(ea); |
0 | 1262 } |
1263 L1: | |
1264 if (token.value != TOKcomma) | |
1265 break; | |
1266 nextToken(); | |
1267 } | |
1268 } | |
1269 check(endtok, "template argument list"); | |
1270 return tiargs; | |
1271 } | |
1272 | |
2 | 1273 /***************************** |
1274 * Parse single template argument, to support the syntax: | |
1275 * foo!arg | |
1276 * Input: | |
1277 * current token is the arg | |
1278 */ | |
0 | 1279 Objects parseTemplateArgument() |
1280 { | |
2 | 1281 //printf("parseTemplateArgument()\n"); |
1282 Objects tiargs = new Objects(); | |
1283 Type ta; | |
1284 switch (token.value) | |
1285 { | |
1286 case TOKidentifier: | |
1287 ta = new TypeIdentifier(loc, token.ident); | |
1288 goto LabelX; | |
1289 | |
1290 case TOKvoid: ta = Type.tvoid; goto LabelX; | |
1291 case TOKint8: ta = Type.tint8; goto LabelX; | |
1292 case TOKuns8: ta = Type.tuns8; goto LabelX; | |
1293 case TOKint16: ta = Type.tint16; goto LabelX; | |
1294 case TOKuns16: ta = Type.tuns16; goto LabelX; | |
1295 case TOKint32: ta = Type.tint32; goto LabelX; | |
1296 case TOKuns32: ta = Type.tuns32; goto LabelX; | |
1297 case TOKint64: ta = Type.tint64; goto LabelX; | |
1298 case TOKuns64: ta = Type.tuns64; goto LabelX; | |
1299 case TOKfloat32: ta = Type.tfloat32; goto LabelX; | |
1300 case TOKfloat64: ta = Type.tfloat64; goto LabelX; | |
1301 case TOKfloat80: ta = Type.tfloat80; goto LabelX; | |
1302 case TOKimaginary32: ta = Type.timaginary32; goto LabelX; | |
1303 case TOKimaginary64: ta = Type.timaginary64; goto LabelX; | |
1304 case TOKimaginary80: ta = Type.timaginary80; goto LabelX; | |
1305 case TOKcomplex32: ta = Type.tcomplex32; goto LabelX; | |
1306 case TOKcomplex64: ta = Type.tcomplex64; goto LabelX; | |
1307 case TOKcomplex80: ta = Type.tcomplex80; goto LabelX; | |
1308 case TOKbit: ta = Type.tbit; goto LabelX; | |
1309 case TOKbool: ta = Type.tbool; goto LabelX; | |
1310 case TOKchar: ta = Type.tchar; goto LabelX; | |
1311 case TOKwchar: ta = Type.twchar; goto LabelX; | |
1312 case TOKdchar: ta = Type.tdchar; goto LabelX; | |
1313 LabelX: | |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
1314 tiargs.push(ta); |
2 | 1315 nextToken(); |
1316 break; | |
1317 | |
1318 case TOKint32v: | |
1319 case TOKuns32v: | |
1320 case TOKint64v: | |
1321 case TOKuns64v: | |
1322 case TOKfloat32v: | |
1323 case TOKfloat64v: | |
1324 case TOKfloat80v: | |
1325 case TOKimaginary32v: | |
1326 case TOKimaginary64v: | |
1327 case TOKimaginary80v: | |
1328 case TOKnull: | |
1329 case TOKtrue: | |
1330 case TOKfalse: | |
1331 case TOKcharv: | |
1332 case TOKwcharv: | |
1333 case TOKdcharv: | |
1334 case TOKstring: | |
1335 case TOKfile: | |
1336 case TOKline: | |
1337 { | |
1338 // Template argument is an expression | |
1339 Expression ea = parsePrimaryExp(); | |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
1340 tiargs.push(ea); |
2 | 1341 break; |
1342 } | |
1343 | |
1344 default: | |
1345 error("template argument expected following !"); | |
1346 break; | |
1347 } | |
1348 | |
1349 if (token.value == TOKnot) | |
1350 error("multiple ! arguments are not allowed"); | |
1351 return tiargs; | |
0 | 1352 } |
1353 | |
1354 /********************************** | |
1355 * Parse a static assertion. | |
1356 */ | |
1357 StaticAssert parseStaticAssert() | |
1358 { | |
1359 Loc loc = this.loc; | |
1360 Expression exp; | |
1361 Expression msg = null; | |
1362 | |
1363 //printf("parseStaticAssert()\n"); | |
1364 nextToken(); | |
1365 check(TOK.TOKlparen); | |
1366 exp = parseAssignExp(); | |
1367 if (token.value == TOK.TOKcomma) | |
1368 { | |
1369 nextToken(); | |
1370 msg = parseAssignExp(); | |
1371 } | |
1372 | |
1373 check(TOK.TOKrparen); | |
1374 check(TOK.TOKsemicolon); | |
1375 | |
1376 return new StaticAssert(loc, exp, msg); | |
1377 } | |
1378 | |
1379 TypeQualified parseTypeof() | |
1380 { | |
1381 TypeQualified t; | |
1382 Loc loc = this.loc; | |
1383 | |
1384 nextToken(); | |
1385 check(TOK.TOKlparen); | |
1386 if (token.value == TOK.TOKreturn) // typeof(return) | |
1387 { | |
1388 nextToken(); | |
1389 t = new TypeReturn(loc); | |
1390 } | |
1391 else | |
1392 { | |
1393 Expression exp = parseExpression(); // typeof(expression) | |
1394 t = new TypeTypeof(loc, exp); | |
1395 } | |
1396 check(TOK.TOKrparen); | |
1397 return t; | |
1398 } | |
1399 | |
1400 /*********************************** | |
1401 * Parse extern (linkage) | |
1402 * The parser is on the 'extern' token. | |
1403 */ | |
1404 LINK parseLinkage() | |
1405 { | |
1406 LINK link = LINK.LINKdefault; | |
1407 nextToken(); | |
1408 assert(token.value == TOK.TOKlparen); | |
1409 nextToken(); | |
1410 if (token.value == TOK.TOKidentifier) | |
1411 { | |
1412 Identifier id = token.ident; | |
1413 | |
1414 nextToken(); | |
1415 if (id == Id.Windows) | |
1416 link = LINK.LINKwindows; | |
1417 else if (id == Id.Pascal) | |
1418 link = LINK.LINKpascal; | |
1419 else if (id == Id.D) | |
1420 link = LINK.LINKd; | |
1421 else if (id == Id.C) | |
1422 { | |
1423 link = LINK.LINKc; | |
1424 if (token.value == TOK.TOKplusplus) | |
1425 { | |
1426 link = LINK.LINKcpp; | |
1427 nextToken(); | |
1428 } | |
1429 } | |
1430 else if (id == Id.System) | |
1431 { | |
114 | 1432 version (Windows) |
1433 { | |
0 | 1434 link = LINK.LINKwindows; |
114 | 1435 } |
1436 else | |
1437 { | |
0 | 1438 link = LINK.LINKc; |
1439 } | |
1440 } | |
1441 else | |
1442 { | |
1443 error("valid linkage identifiers are D, C, C++, Pascal, Windows, System"); | |
1444 link = LINK.LINKd; | |
1445 } | |
1446 } | |
1447 else | |
1448 { | |
1449 link = LINK.LINKd; // default | |
1450 } | |
1451 check(TOK.TOKrparen); | |
1452 | |
1453 return link; | |
1454 } | |
1455 | |
1456 | |
1457 /************************************** | |
1458 * Parse a debug conditional | |
1459 */ | |
1460 Condition parseDebugCondition() | |
1461 { | |
1462 Condition c; | |
1463 | |
1464 if (token.value == TOK.TOKlparen) | |
1465 { | |
1466 nextToken(); | |
1467 uint level = 1; | |
1468 Identifier id = null; | |
1469 | |
1470 if (token.value == TOK.TOKidentifier) | |
1471 id = token.ident; | |
1472 else if (token.value == TOK.TOKint32v) | |
1473 level = cast(uint)token.uns64value; | |
1474 else | |
1475 error("identifier or integer expected, not %s", token.toChars()); | |
1476 | |
1477 nextToken(); | |
1478 check(TOK.TOKrparen); | |
1479 | |
1480 c = new DebugCondition(mod, level, id); | |
1481 } | |
1482 else | |
1483 c = new DebugCondition(mod, 1, null); | |
1484 | |
1485 return c; | |
1486 } | |
1487 | |
1488 /************************************** | |
1489 * Parse a version conditional | |
1490 */ | |
1491 Condition parseVersionCondition() | |
1492 { | |
1493 Condition c; | |
1494 uint level = 1; | |
1495 Identifier id = null; | |
1496 | |
1497 if (token.value == TOK.TOKlparen) | |
1498 { | |
1499 nextToken(); | |
1500 if (token.value == TOK.TOKidentifier) | |
1501 id = token.ident; | |
1502 else if (token.value == TOK.TOKint32v) | |
1503 level = cast(uint)token.uns64value; | |
1504 else { | |
1505 version (DMDV2) { | |
1506 /* Allow: | |
1507 * version (unittest) | |
1508 * even though unittest is a keyword | |
1509 */ | |
1510 if (token.value == TOK.TOKunittest) | |
1511 id = Lexer.idPool(Token.toChars(TOK.TOKunittest)); | |
1512 else | |
1513 error("identifier or integer expected, not %s", token.toChars()); | |
1514 } else { | |
1515 error("identifier or integer expected, not %s", token.toChars()); | |
1516 } | |
1517 } | |
1518 nextToken(); | |
1519 check(TOK.TOKrparen); | |
1520 } | |
1521 else | |
1522 error("(condition) expected following version"); | |
1523 | |
1524 c = new VersionCondition(mod, level, id); | |
1525 | |
1526 return c; | |
1527 } | |
1528 | |
1529 /*********************************************** | |
1530 * static if (expression) | |
1531 * body | |
1532 * else | |
1533 * body | |
1534 */ | |
1535 Condition parseStaticIfCondition() | |
1536 { | |
1537 Expression exp; | |
1538 Condition condition; | |
1539 Array aif; | |
1540 Array aelse; | |
1541 Loc loc = this.loc; | |
1542 | |
1543 nextToken(); | |
1544 if (token.value == TOKlparen) | |
1545 { | |
1546 nextToken(); | |
1547 exp = parseAssignExp(); | |
1548 check(TOKrparen); | |
1549 } | |
1550 else | |
1551 { | |
1552 error("(expression) expected following static if"); | |
1553 exp = null; | |
1554 } | |
1555 condition = new StaticIfCondition(loc, exp); | |
1556 return condition; | |
1557 } | |
1558 | |
1559 /***************************************** | |
1560 * Parse a constructor definition: | |
1561 * this(parameters) { body } | |
1562 * or postblit: | |
1563 * this(this) { body } | |
1564 * or constructor template: | |
1565 * this(templateparameters)(parameters) { body } | |
1566 * Current token is 'this'. | |
1567 */ | |
1568 | |
1569 Dsymbol parseCtor() | |
1570 { | |
1571 Loc loc = this.loc; | |
1572 | |
1573 nextToken(); | |
1574 if (token.value == TOK.TOKlparen && peek(&token).value == TOK.TOKthis) | |
1575 { // this(this) { ... } | |
1576 nextToken(); | |
1577 nextToken(); | |
1578 check(TOK.TOKrparen); | |
1579 PostBlitDeclaration f = new PostBlitDeclaration(loc, Loc(0)); | |
1580 parseContracts(f); | |
1581 return f; | |
1582 } | |
1583 | |
1584 /* Look ahead to see if: | |
1585 * this(...)(...) | |
1586 * which is a constructor template | |
1587 */ | |
1588 TemplateParameters tpl = null; | |
1589 if (token.value == TOK.TOKlparen && peekPastParen(&token).value == TOK.TOKlparen) | |
1590 { tpl = parseTemplateParameterList(); | |
1591 | |
1592 int varargs; | |
1593 Arguments arguments = parseParameters(&varargs); | |
1594 | |
1595 Expression constraint = null; | |
1596 if (tpl) | |
1597 constraint = parseConstraint(); | |
1598 | |
1599 CtorDeclaration f = new CtorDeclaration(loc, Loc(0), arguments, varargs); | |
1600 parseContracts(f); | |
1601 | |
1602 // Wrap a template around it | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
1603 auto decldefs = new Dsymbols(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
1604 decldefs.push(f); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
1605 auto tempdecl = new TemplateDeclaration(loc, f.ident, tpl, constraint, decldefs); |
0 | 1606 return tempdecl; |
1607 } | |
1608 | |
1609 /* Just a regular constructor | |
1610 */ | |
1611 int varargs; | |
1612 Arguments arguments = parseParameters(&varargs); | |
1613 CtorDeclaration f = new CtorDeclaration(loc, Loc(0), arguments, varargs); | |
1614 parseContracts(f); | |
1615 return f; | |
1616 } | |
1617 | |
1618 PostBlitDeclaration parsePostBlit() | |
1619 { | |
1620 assert(false); | |
1621 } | |
1622 | |
1623 /***************************************** | |
1624 * Parse a destructor definition: | |
1625 * ~this() { body } | |
1626 * Current token is '~'. | |
1627 */ | |
1628 DtorDeclaration parseDtor() | |
1629 { | |
1630 DtorDeclaration f; | |
1631 Loc loc = this.loc; | |
1632 | |
1633 nextToken(); | |
1634 check(TOKthis); | |
1635 check(TOKlparen); | |
1636 check(TOKrparen); | |
1637 | |
1638 f = new DtorDeclaration(loc, Loc(0)); | |
1639 parseContracts(f); | |
1640 return f; | |
1641 } | |
1642 | |
1643 /***************************************** | |
1644 * Parse a static constructor definition: | |
1645 * static this() { body } | |
1646 * Current token is 'this'. | |
1647 */ | |
1648 StaticCtorDeclaration parseStaticCtor() | |
1649 { | |
1650 StaticCtorDeclaration f; | |
1651 Loc loc = this.loc; | |
1652 | |
1653 nextToken(); | |
1654 check(TOKlparen); | |
1655 check(TOKrparen); | |
1656 | |
1657 f = new StaticCtorDeclaration(loc, Loc(0)); | |
1658 parseContracts(f); | |
1659 return f; | |
1660 } | |
1661 | |
1662 /***************************************** | |
1663 * Parse a static destructor definition: | |
1664 * static ~this() { body } | |
1665 * Current token is '~'. | |
1666 */ | |
1667 StaticDtorDeclaration parseStaticDtor() | |
1668 { | |
1669 StaticDtorDeclaration f; | |
1670 Loc loc = this.loc; | |
1671 | |
1672 nextToken(); | |
1673 check(TOKthis); | |
1674 check(TOKlparen); | |
1675 check(TOKrparen); | |
1676 | |
1677 f = new StaticDtorDeclaration(loc, Loc(0)); | |
1678 parseContracts(f); | |
1679 return f; | |
1680 } | |
1681 | |
1682 /***************************************** | |
1683 * Parse an invariant definition: | |
1684 * invariant() { body } | |
1685 * Current token is 'invariant'. | |
1686 */ | |
1687 InvariantDeclaration parseInvariant() | |
1688 { | |
1689 InvariantDeclaration f; | |
1690 Loc loc = this.loc; | |
1691 | |
1692 nextToken(); | |
1693 if (token.value == TOKlparen) // optional () | |
1694 { | |
1695 nextToken(); | |
1696 check(TOKrparen); | |
1697 } | |
1698 | |
1699 f = new InvariantDeclaration(loc, Loc(0)); | |
1700 f.fbody = parseStatement(ParseStatementFlags.PScurly); | |
1701 return f; | |
1702 } | |
1703 | |
1704 /***************************************** | |
1705 * Parse a unittest definition: | |
1706 * unittest { body } | |
1707 * Current token is 'unittest'. | |
1708 */ | |
1709 UnitTestDeclaration parseUnitTest() | |
1710 { | |
1711 Loc loc = this.loc; | |
1712 | |
1713 nextToken(); | |
1714 | |
1715 UnitTestDeclaration f = new UnitTestDeclaration(loc, this.loc); | |
1716 f.fbody = parseStatement(ParseStatementFlags.PScurly); | |
1717 | |
1718 return f; | |
1719 } | |
1720 | |
1721 NewDeclaration parseNew() | |
1722 { | |
1723 assert(false); | |
1724 } | |
1725 | |
1726 DeleteDeclaration parseDelete() | |
1727 { | |
1728 assert(false); | |
1729 } | |
1730 | |
1731 Arguments parseParameters(int* pvarargs) | |
1732 { | |
1733 Arguments arguments = new Arguments(); | |
1734 int varargs = 0; | |
1735 int hasdefault = 0; | |
1736 | |
1737 check(TOK.TOKlparen); | |
1738 while (1) | |
1739 { Type *tb; | |
1740 Identifier ai = null; | |
1741 Type at; | |
1742 Argument a; | |
1743 STC storageClass = STC.STCundefined; | |
1744 STC stc; | |
1745 Expression ae; | |
1746 | |
1747 for ( ;1; nextToken()) | |
1748 { | |
1749 switch (token.value) | |
1750 { | |
1751 case TOK.TOKrparen: | |
1752 break; | |
1753 | |
1754 case TOK.TOKdotdotdot: | |
1755 varargs = 1; | |
1756 nextToken(); | |
1757 break; | |
1758 | |
1759 case TOK.TOKconst: | |
1760 if (peek(&token).value == TOK.TOKlparen) | |
1761 goto Ldefault; | |
1762 stc = STC.STCconst; | |
1763 goto L2; | |
1764 | |
1765 case TOK.TOKinvariant: | |
1766 case TOK.TOKimmutable: | |
1767 if (peek(&token).value == TOK.TOKlparen) | |
1768 goto Ldefault; | |
1769 stc = STC.STCimmutable; | |
1770 goto L2; | |
1771 | |
1772 case TOK.TOKshared: | |
1773 if (peek(&token).value == TOK.TOKlparen) | |
1774 goto Ldefault; | |
1775 stc = STC.STCshared; | |
1776 goto L2; | |
1777 | |
1778 case TOK.TOKin: stc = STC.STCin; goto L2; | |
1779 case TOK.TOKout: stc = STC.STCout; goto L2; | |
1780 case TOK.TOKinout: | |
1781 case TOK.TOKref: stc = STC.STCref; goto L2; | |
1782 case TOK.TOKlazy: stc = STC.STClazy; goto L2; | |
1783 case TOK.TOKscope: stc = STC.STCscope; goto L2; | |
1784 case TOK.TOKfinal: stc = STC.STCfinal; goto L2; | |
1785 L2: | |
1786 if (storageClass & stc || | |
1787 (storageClass & STC.STCin && stc & (STC.STCconst | STC.STCscope)) || | |
1788 (stc & STC.STCin && storageClass & (STC.STCconst | STC.STCscope)) | |
1789 ) | |
1790 error("redundant storage class %s", Token.toChars(token.value)); | |
1791 storageClass |= stc; | |
1792 composeStorageClass(storageClass); | |
1793 continue; | |
1794 | |
1795 static if (false) { | |
1796 case TOK.TOKstatic: stc = STC.STCstatic; goto L2; | |
1797 case TOK.TOKauto: storageClass = STC.STCauto; goto L4; | |
1798 case TOK.TOKalias: storageClass = STC.STCalias; goto L4; | |
1799 L4: | |
1800 nextToken(); | |
1801 if (token.value == TOK.TOKidentifier) | |
1802 { ai = token.ident; | |
1803 nextToken(); | |
1804 } | |
1805 else | |
1806 ai = null; | |
1807 at = null; // no type | |
1808 ae = null; // no default argument | |
1809 if (token.value == TOK.TOKassign) // = defaultArg | |
1810 { nextToken(); | |
1811 ae = parseDefaultInitExp(); | |
1812 hasdefault = 1; | |
1813 } | |
1814 else | |
1815 { if (hasdefault) | |
1816 error("default argument expected for alias %s", | |
1817 ai ? ai.toChars() : ""); | |
1818 } | |
1819 goto L3; | |
1820 } | |
1821 | |
1822 default: | |
1823 Ldefault: | |
1824 stc = (storageClass & (STC.STCin | STC.STCout | STC.STCref | STC.STClazy)); | |
1825 if (stc & (stc - 1)) // if stc is not a power of 2 | |
1826 error("incompatible parameter storage classes"); | |
1827 if ((storageClass & (STC.STCconst | STC.STCout)) == (STC.STCconst | STC.STCout)) | |
1828 error("out cannot be const"); | |
1829 if ((storageClass & (STC.STCimmutable | STC.STCout)) == (STC.STCimmutable | STC.STCout)) | |
1830 error("out cannot be immutable"); | |
1831 if ((storageClass & STC.STCscope) && | |
1832 (storageClass & (STC.STCref | STC.STCout))) | |
1833 error("scope cannot be ref or out"); | |
1834 at = parseType(&ai); | |
1835 ae = null; | |
1836 if (token.value == TOK.TOKassign) // = defaultArg | |
1837 { nextToken(); | |
1838 ae = parseDefaultInitExp(); | |
1839 hasdefault = 1; | |
1840 } | |
1841 else | |
1842 { if (hasdefault) | |
1843 error("default argument expected for %s", | |
1844 ai ? ai.toChars() : at.toChars()); | |
1845 } | |
1846 if (token.value == TOK.TOKdotdotdot) | |
1847 { /* This is: | |
1848 * at ai ... | |
1849 */ | |
1850 | |
1851 if (storageClass & (STC.STCout | STC.STCref)) | |
1852 error("variadic argument cannot be out or ref"); | |
1853 varargs = 2; | |
1854 a = new Argument(storageClass, at, ai, ae); | |
1855 arguments.push(cast(void*)a); | |
1856 nextToken(); | |
1857 break; | |
1858 } | |
1859 L3: | |
1860 a = new Argument(storageClass, at, ai, ae); | |
1861 arguments.push(cast(void*)a); | |
1862 if (token.value == TOK.TOKcomma) | |
1863 { nextToken(); | |
1864 goto L1; | |
1865 } | |
1866 break; | |
1867 } | |
1868 break; | |
1869 } | |
1870 break; | |
1871 | |
1872 L1: ; | |
1873 } | |
1874 check(TOK.TOKrparen); | |
1875 *pvarargs = varargs; | |
1876 return arguments; | |
1877 } | |
1878 | |
1879 EnumDeclaration parseEnum() | |
1880 { | |
1881 EnumDeclaration e; | |
1882 Identifier id; | |
1883 Type memtype; | |
1884 Loc loc = this.loc; | |
1885 | |
1886 //printf("Parser.parseEnum()\n"); | |
1887 nextToken(); | |
1888 if (token.value == TOK.TOKidentifier) | |
1889 { | |
1890 id = token.ident; | |
1891 nextToken(); | |
1892 } | |
1893 else | |
1894 id = null; | |
1895 | |
1896 if (token.value == TOK.TOKcolon) | |
1897 { | |
1898 nextToken(); | |
1899 memtype = parseBasicType(); | |
1900 memtype = parseDeclarator(memtype, null, null); | |
1901 } | |
1902 else | |
1903 memtype = null; | |
1904 | |
1905 e = new EnumDeclaration(loc, id, memtype); | |
1906 if (token.value == TOK.TOKsemicolon && id) | |
1907 nextToken(); | |
1908 else if (token.value == TOK.TOKlcurly) | |
1909 { | |
1910 //printf("enum definition\n"); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
1911 e.members = new Dsymbols(); |
0 | 1912 nextToken(); |
79 | 1913 string comment = token.blockComment; |
0 | 1914 while (token.value != TOK.TOKrcurly) |
1915 { | |
1916 /* Can take the following forms: | |
1917 * 1. ident | |
1918 * 2. ident = value | |
1919 * 3. type ident = value | |
1920 */ | |
1921 | |
1922 loc = this.loc; | |
1923 | |
1924 Type type = null; | |
1925 Identifier ident; | |
1926 Token* tp = peek(&token); | |
1927 if (token.value == TOK.TOKidentifier && | |
1928 (tp.value == TOK.TOKassign || tp.value == TOK.TOKcomma || tp.value == TOK.TOKrcurly)) | |
1929 { | |
1930 ident = token.ident; | |
1931 type = null; | |
1932 nextToken(); | |
1933 } | |
1934 else | |
1935 { | |
1936 type = parseType(&ident, null); | |
1937 if (id || memtype) | |
1938 error("type only allowed if anonymous enum and no enum type"); | |
1939 } | |
1940 | |
1941 Expression value; | |
1942 if (token.value == TOK.TOKassign) | |
1943 { | |
1944 nextToken(); | |
1945 value = parseAssignExp(); | |
1946 } | |
1947 else | |
1948 { | |
1949 value = null; | |
1950 if (type) | |
1951 error("if type, there must be an initializer"); | |
1952 } | |
1953 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
1954 auto em = new EnumMember(loc, ident, value, type); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
1955 e.members.push(em); |
0 | 1956 |
1957 if (token.value == TOK.TOKrcurly) { | |
1958 ; | |
1959 } else { | |
1960 addComment(em, comment); | |
1961 comment = null; | |
1962 check(TOK.TOKcomma); | |
1963 } | |
1964 addComment(em, comment); | |
1965 comment = token.blockComment; | |
1966 } | |
1967 nextToken(); | |
1968 } | |
1969 else | |
1970 error("enum declaration is invalid"); | |
1971 | |
1972 //printf("-parseEnum() %s\n", e.toChars()); | |
1973 return e; | |
1974 } | |
1975 | |
1976 Dsymbol parseAggregate() | |
1977 { | |
1978 AggregateDeclaration a = null; | |
1979 int anon = 0; | |
1980 TOK tok; | |
1981 Identifier id; | |
1982 TemplateParameters tpl = null; | |
1983 Expression constraint = null; | |
1984 | |
1985 //printf("Parser.parseAggregate()\n"); | |
1986 tok = token.value; | |
1987 nextToken(); | |
1988 if (token.value != TOK.TOKidentifier) | |
1989 { | |
1990 id = null; | |
1991 } | |
1992 else | |
1993 { | |
1994 id = token.ident; | |
1995 nextToken(); | |
1996 | |
1997 if (token.value == TOK.TOKlparen) | |
1998 { | |
1999 // Class template declaration. | |
2000 | |
2001 // Gather template parameter list | |
2002 tpl = parseTemplateParameterList(); | |
2003 constraint = parseConstraint(); | |
2004 } | |
2005 } | |
2006 | |
2007 Loc loc = this.loc; | |
2008 switch (tok) | |
2009 { case TOK.TOKclass: | |
2010 case TOK.TOKinterface: | |
2011 { | |
2012 if (!id) | |
2013 error("anonymous classes not allowed"); | |
2014 | |
2015 // Collect base class(es) | |
2016 BaseClasses baseclasses = null; | |
2017 if (token.value == TOK.TOKcolon) | |
2018 { | |
2019 nextToken(); | |
2020 baseclasses = parseBaseClasses(); | |
2021 | |
2022 if (token.value != TOK.TOKlcurly) | |
2023 error("members expected"); | |
2024 } | |
2025 | |
2026 if (tok == TOK.TOKclass) | |
2027 a = new ClassDeclaration(loc, id, baseclasses); | |
2028 else | |
2029 a = new InterfaceDeclaration(loc, id, baseclasses); | |
2030 break; | |
2031 } | |
2032 | |
2033 case TOK.TOKstruct: | |
2034 if (id) | |
2035 a = new StructDeclaration(loc, id); | |
2036 else | |
2037 anon = 1; | |
2038 break; | |
2039 | |
2040 case TOK.TOKunion: | |
2041 if (id) | |
2042 a = new UnionDeclaration(loc, id); | |
2043 else | |
2044 anon = 2; | |
2045 break; | |
2046 | |
2047 default: | |
2048 assert(0); | |
2049 break; | |
2050 } | |
2051 if (a && token.value == TOK.TOKsemicolon) | |
2052 { nextToken(); | |
2053 } | |
2054 else if (token.value == TOK.TOKlcurly) | |
2055 { | |
2056 //printf("aggregate definition\n"); | |
2057 nextToken(); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2058 auto decl = parseDeclDefs(0); |
0 | 2059 if (token.value != TOK.TOKrcurly) |
2060 error("} expected following member declarations in aggregate"); | |
2061 nextToken(); | |
2062 if (anon) | |
2063 { | |
2064 /* Anonymous structs/unions are more like attributes. | |
2065 */ | |
2066 return new AnonDeclaration(loc, anon - 1, decl); | |
2067 } | |
2068 else | |
2069 a.members = decl; | |
2070 } | |
2071 else | |
2072 { | |
2073 error("{ } expected following aggregate declaration"); | |
2074 a = new StructDeclaration(loc, null); | |
2075 } | |
2076 | |
2077 if (tpl) | |
2078 { // Wrap a template around the aggregate declaration | |
2079 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2080 auto decldefs = new Dsymbols(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2081 decldefs.push(a); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2082 auto tempdecl = new TemplateDeclaration(loc, id, tpl, constraint, decldefs); |
0 | 2083 return tempdecl; |
2084 } | |
2085 | |
2086 return a; | |
2087 } | |
2088 | |
2089 BaseClasses parseBaseClasses() | |
2090 { | |
2091 BaseClasses baseclasses = new BaseClasses(); | |
2092 | |
2093 for (; 1; nextToken()) | |
2094 { | |
2095 PROT protection = PROT.PROTpublic; | |
2096 switch (token.value) | |
2097 { | |
2098 case TOK.TOKprivate: | |
2099 protection = PROT.PROTprivate; | |
2100 nextToken(); | |
2101 break; | |
2102 case TOK.TOKpackage: | |
2103 protection = PROT.PROTpackage; | |
2104 nextToken(); | |
2105 break; | |
2106 case TOK.TOKprotected: | |
2107 protection = PROT.PROTprotected; | |
2108 nextToken(); | |
2109 break; | |
2110 case TOK.TOKpublic: | |
2111 protection = PROT.PROTpublic; | |
2112 nextToken(); | |
2113 break; | |
2114 default: | |
2115 break; /// | |
2116 } | |
2117 if (token.value == TOK.TOKidentifier) | |
2118 { | |
2119 BaseClass b = new BaseClass(parseBasicType(), protection); | |
2120 baseclasses.push(cast(void*)b); | |
2121 if (token.value != TOK.TOKcomma) | |
2122 break; | |
2123 } | |
2124 else | |
2125 { | |
2126 error("base classes expected instead of %s", token.toChars()); | |
2127 return null; | |
2128 } | |
2129 } | |
2130 return baseclasses; | |
2131 } | |
2132 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2133 Import parseImport(Dsymbols decldefs, int isstatic) |
0 | 2134 { |
2135 Import s; | |
2136 Identifier id; | |
2137 Identifier aliasid = null; | |
2138 Array a; | |
2139 Loc loc; | |
2140 | |
2141 //printf("Parser.parseImport()\n"); | |
2142 do | |
2143 { | |
2144 L1: | |
2145 nextToken(); | |
2146 if (token.value != TOK.TOKidentifier) | |
2147 { | |
2148 error("Identifier expected following import"); | |
2149 break; | |
2150 } | |
2151 | |
2152 loc = this.loc; | |
2153 a = null; | |
2154 id = token.ident; | |
2155 nextToken(); | |
2156 if (!aliasid && token.value == TOK.TOKassign) | |
2157 { | |
2158 aliasid = id; | |
2159 goto L1; | |
2160 } | |
2161 while (token.value == TOK.TOKdot) | |
2162 { | |
2163 if (!a) | |
2164 a = new Array(); | |
2165 a.push(cast(void*)id); | |
2166 nextToken(); | |
2167 if (token.value != TOK.TOKidentifier) | |
2168 { | |
2169 error("identifier expected following package"); | |
2170 break; | |
2171 } | |
2172 id = token.ident; | |
2173 nextToken(); | |
2174 } | |
2175 | |
2176 s = new Import(loc, a, id, aliasid, isstatic); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2177 decldefs.push(s); |
0 | 2178 |
2179 /* Look for | |
2180 * : alias=name, alias=name; | |
2181 * syntax. | |
2182 */ | |
2183 if (token.value == TOK.TOKcolon) | |
2184 { | |
2185 do | |
2186 { | |
2187 Identifier name; | |
2188 | |
2189 nextToken(); | |
2190 if (token.value != TOK.TOKidentifier) | |
2191 { | |
2192 error("Identifier expected following :"); | |
2193 break; | |
2194 } | |
2195 Identifier alias_ = token.ident; | |
2196 nextToken(); | |
2197 if (token.value == TOK.TOKassign) | |
2198 { | |
2199 nextToken(); | |
2200 if (token.value != TOK.TOKidentifier) | |
2201 { | |
2202 error("Identifier expected following %s=", alias_.toChars()); | |
2203 break; | |
2204 } | |
2205 name = token.ident; | |
2206 nextToken(); | |
2207 } | |
2208 else | |
2209 { | |
2210 name = alias_; | |
2211 alias_ = null; | |
2212 } | |
2213 s.addAlias(name, alias_); | |
2214 } while (token.value == TOK.TOKcomma); | |
2215 | |
2216 break; // no comma-separated imports of this form | |
2217 } | |
2218 | |
2219 aliasid = null; | |
2220 | |
2221 } while (token.value == TOK.TOKcomma); | |
2222 | |
2223 if (token.value == TOK.TOKsemicolon) | |
2224 nextToken(); | |
2225 else | |
2226 { | |
2227 error("';' expected"); | |
2228 nextToken(); | |
2229 } | |
2230 | |
2231 return null; | |
2232 } | |
2233 | |
2234 Type parseType(Identifier* pident = null, TemplateParameters* tpl = null) | |
2235 { | |
2236 Type t; | |
2237 | |
2238 /* Take care of the storage class prefixes that | |
2239 * serve as type attributes: | |
2240 * const shared, shared const, const, invariant, shared | |
2241 */ | |
2242 if (token.value == TOK.TOKconst && peekNext() == TOK.TOKshared && peekNext2() != TOK.TOKlparen || | |
2243 token.value == TOK.TOKshared && peekNext() == TOK.TOKconst && peekNext2() != TOK.TOKlparen) | |
2244 { | |
2245 nextToken(); | |
2246 nextToken(); | |
2247 /* shared const type | |
2248 */ | |
2249 t = parseType(pident, tpl); | |
2250 t = t.makeSharedConst(); | |
2251 return t; | |
2252 } | |
2253 else if (token.value == TOK.TOKconst && peekNext() != TOK.TOKlparen) | |
2254 { | |
2255 nextToken(); | |
2256 /* const type | |
2257 */ | |
2258 t = parseType(pident, tpl); | |
2259 t = t.makeConst(); | |
2260 return t; | |
2261 } | |
2262 else if ((token.value == TOK.TOKinvariant || token.value == TOK.TOKimmutable) && | |
2263 peekNext() != TOK.TOKlparen) | |
2264 { | |
2265 nextToken(); | |
2266 /* invariant type | |
2267 */ | |
2268 t = parseType(pident, tpl); | |
2269 t = t.makeInvariant(); | |
2270 return t; | |
2271 } | |
2272 else if (token.value == TOK.TOKshared && peekNext() != TOK.TOKlparen) | |
2273 { | |
2274 nextToken(); | |
2275 /* shared type | |
2276 */ | |
2277 t = parseType(pident, tpl); | |
2278 t = t.makeShared(); | |
2279 return t; | |
2280 } | |
2281 else | |
2282 t = parseBasicType(); | |
2283 t = parseDeclarator(t, pident, tpl); | |
2284 return t; | |
2285 } | |
2286 | |
2287 Type parseBasicType() | |
2288 { | |
2289 Type t; | |
2290 Identifier id; | |
2291 TypeQualified tid; | |
2292 | |
2293 //printf("parseBasicType()\n"); | |
2294 switch (token.value) | |
2295 { | |
2296 case TOK.TOKvoid: t = Type.tvoid; goto LabelX; | |
2297 case TOK.TOKint8: t = Type.tint8; goto LabelX; | |
2298 case TOK.TOKuns8: t = Type.tuns8; goto LabelX; | |
2299 case TOK.TOKint16: t = Type.tint16; goto LabelX; | |
2300 case TOK.TOKuns16: t = Type.tuns16; goto LabelX; | |
2301 case TOK.TOKint32: t = Type.tint32; goto LabelX; | |
2302 case TOK.TOKuns32: t = Type.tuns32; goto LabelX; | |
2303 case TOK.TOKint64: t = Type.tint64; goto LabelX; | |
2304 case TOK.TOKuns64: t = Type.tuns64; goto LabelX; | |
2305 case TOK.TOKfloat32: t = Type.tfloat32; goto LabelX; | |
2306 case TOK.TOKfloat64: t = Type.tfloat64; goto LabelX; | |
2307 case TOK.TOKfloat80: t = Type.tfloat80; goto LabelX; | |
2308 case TOK.TOKimaginary32: t = Type.timaginary32; goto LabelX; | |
2309 case TOK.TOKimaginary64: t = Type.timaginary64; goto LabelX; | |
2310 case TOK.TOKimaginary80: t = Type.timaginary80; goto LabelX; | |
2311 case TOK.TOKcomplex32: t = Type.tcomplex32; goto LabelX; | |
2312 case TOK.TOKcomplex64: t = Type.tcomplex64; goto LabelX; | |
2313 case TOK.TOKcomplex80: t = Type.tcomplex80; goto LabelX; | |
2314 case TOK.TOKbit: t = Type.tbit; goto LabelX; | |
2315 case TOK.TOKbool: t = Type.tbool; goto LabelX; | |
2316 case TOK.TOKchar: t = Type.tchar; goto LabelX; | |
2317 case TOK.TOKwchar: t = Type.twchar; goto LabelX; | |
2318 case TOK.TOKdchar: t = Type.tdchar; goto LabelX; | |
2319 LabelX: | |
2320 nextToken(); | |
2321 break; | |
2322 | |
2323 case TOK.TOKidentifier: | |
2324 id = token.ident; | |
2325 nextToken(); | |
2326 if (token.value == TOK.TOKnot) | |
2327 { // ident!(template_arguments) | |
2328 TemplateInstance tempinst = new TemplateInstance(loc, id); | |
2329 nextToken(); | |
2330 if (token.value == TOK.TOKlparen) | |
2331 // ident!(template_arguments) | |
2332 tempinst.tiargs = parseTemplateArgumentList(); | |
2333 else | |
2334 // ident!template_argument | |
2335 tempinst.tiargs = parseTemplateArgument(); | |
2336 tid = new TypeInstance(loc, tempinst); | |
2337 goto Lident2; | |
2338 } | |
2339 Lident: | |
2340 tid = new TypeIdentifier(loc, id); | |
2341 Lident2: | |
2342 while (token.value == TOK.TOKdot) | |
2343 { nextToken(); | |
2344 if (token.value != TOK.TOKidentifier) | |
2345 { error("identifier expected following '.' instead of '%s'", token.toChars()); | |
2346 break; | |
2347 } | |
2348 id = token.ident; | |
2349 nextToken(); | |
2350 if (token.value == TOK.TOKnot) | |
2351 { | |
2352 TemplateInstance tempinst = new TemplateInstance(loc, id); | |
2353 nextToken(); | |
2354 if (token.value == TOK.TOKlparen) | |
2355 // ident!(template_arguments) | |
2356 tempinst.tiargs = parseTemplateArgumentList(); | |
2357 else | |
2358 // ident!template_argument | |
2359 tempinst.tiargs = parseTemplateArgument(); | |
56 | 2360 tid.addIdent(tempinst); |
0 | 2361 } |
2362 else | |
2363 tid.addIdent(id); | |
2364 } | |
2365 t = tid; | |
2366 break; | |
2367 | |
2368 case TOK.TOKdot: | |
2369 // Leading . as in .foo | |
2370 id = Id.empty; | |
2371 goto Lident; | |
2372 | |
2373 case TOK.TOKtypeof: | |
2374 // typeof(expression) | |
2375 tid = parseTypeof(); | |
2376 goto Lident2; | |
2377 | |
2378 case TOK.TOKconst: | |
2379 // const(type) | |
2380 nextToken(); | |
2381 check(TOK.TOKlparen); | |
2382 t = parseType(); | |
2383 check(TOK.TOKrparen); | |
2384 if (t.isShared()) | |
2385 t = t.makeSharedConst(); | |
2386 else | |
2387 t = t.makeConst(); | |
2388 break; | |
2389 | |
2390 case TOK.TOKinvariant: | |
2391 case TOK.TOKimmutable: | |
2392 // invariant(type) | |
2393 nextToken(); | |
2394 check(TOK.TOKlparen); | |
2395 t = parseType(); | |
2396 check(TOK.TOKrparen); | |
2397 t = t.makeInvariant(); | |
2398 break; | |
2399 | |
2400 case TOK.TOKshared: | |
2401 // shared(type) | |
2402 nextToken(); | |
2403 check(TOK.TOKlparen); | |
2404 t = parseType(); | |
2405 check(TOK.TOKrparen); | |
2406 if (t.isConst()) | |
2407 t = t.makeSharedConst(); | |
2408 else | |
2409 t = t.makeShared(); | |
2410 break; | |
2411 | |
2412 default: | |
2413 error("basic type expected, not %s", token.toChars()); | |
2414 t = Type.tint32; | |
2415 break; | |
2416 } | |
2417 return t; | |
2418 } | |
2419 | |
2420 Type parseBasicType2(Type t) | |
2421 { | |
79 | 2422 //writef("parseBasicType2()\n"); |
0 | 2423 while (1) |
2424 { | |
79 | 2425 switch (token.value) |
0 | 2426 { |
79 | 2427 case TOK.TOKmul: |
2428 t = new TypePointer(t); | |
2429 nextToken(); | |
2430 continue; | |
2431 | |
2432 case TOK.TOKlbracket: | |
2433 // Handle []. Make sure things like | |
2434 // int[3][1] a; | |
2435 // is (array[1] of array[3] of int) | |
0 | 2436 nextToken(); |
79 | 2437 if (token.value == TOK.TOKrbracket) |
2438 { | |
2439 t = new TypeDArray(t); // [] | |
2440 nextToken(); | |
2441 } | |
2442 else if (token.value == TOKnew && peekNext() == TOKrbracket) | |
0 | 2443 { |
79 | 2444 t = new TypeNewArray(t); // [new] |
2445 nextToken(); | |
2446 nextToken(); | |
2447 } | |
2448 else if (isDeclaration(&token, 0, TOK.TOKrbracket, null)) | |
2449 { // It's an associative array declaration | |
2450 | |
2451 //printf("it's an associative array\n"); | |
2452 Type index = parseType(); // [ type ] | |
2453 t = new TypeAArray(t, index); | |
2454 check(TOK.TOKrbracket); | |
0 | 2455 } |
2456 else | |
79 | 2457 { |
2458 //printf("it's type[expression]\n"); | |
2459 inBrackets++; | |
2460 Expression e = parseExpression(); // [ expression ] | |
2461 if (token.value == TOK.TOKslice) | |
2462 { | |
2463 nextToken(); | |
2464 Expression e2 = parseExpression(); // [ exp .. exp ] | |
2465 t = new TypeSlice(t, e, e2); | |
2466 } | |
2467 else | |
2468 t = new TypeSArray(t,e); | |
2469 inBrackets--; | |
2470 check(TOK.TOKrbracket); | |
2471 } | |
2472 continue; | |
2473 | |
2474 case TOK.TOKdelegate: | |
2475 case TOK.TOKfunction: | |
2476 { // Handle delegate declaration: | |
2477 // t delegate(parameter list) nothrow pure | |
2478 // t function(parameter list) nothrow pure | |
2479 Arguments arguments; | |
2480 int varargs; | |
2481 bool ispure = false; | |
2482 bool isnothrow = false; | |
2483 TOK save = token.value; | |
2484 | |
2485 nextToken(); | |
2486 arguments = parseParameters(&varargs); | |
2487 while (1) | |
2488 { // Postfixes | |
2489 if (token.value == TOK.TOKpure) | |
2490 ispure = true; | |
2491 else if (token.value == TOK.TOKnothrow) | |
2492 isnothrow = true; | |
2493 else | |
2494 break; | |
2495 nextToken(); | |
2496 } | |
2497 TypeFunction tf = new TypeFunction(arguments, t, varargs, linkage); | |
2498 tf.ispure = ispure; | |
2499 tf.isnothrow = isnothrow; | |
2500 if (save == TOK.TOKdelegate) | |
2501 t = new TypeDelegate(tf); | |
0 | 2502 else |
79 | 2503 t = new TypePointer(tf); // pointer to function |
2504 continue; | |
2505 } | |
2506 | |
2507 default: | |
2508 return t; | |
2509 } | |
2510 assert(0); | |
0 | 2511 } |
2512 assert(0); | |
2513 return null; | |
2514 } | |
2515 | |
2516 Type parseDeclarator(Type t, Identifier* pident, TemplateParameters* tpl = null) | |
2517 { | |
2518 Type ts; | |
2519 | |
2520 //printf("parseDeclarator(tpl = %p)\n", tpl); | |
2521 t = parseBasicType2(t); | |
2522 | |
2523 switch (token.value) | |
2524 { | |
2525 | |
2526 case TOK.TOKidentifier: | |
2527 if (pident) | |
2528 *pident = token.ident; | |
2529 else | |
2530 error("unexpected identifer '%s' in declarator", token.ident.toChars()); | |
2531 ts = t; | |
2532 nextToken(); | |
2533 break; | |
2534 | |
2535 case TOK.TOKlparen: | |
2536 /* Parse things with parentheses around the identifier, like: | |
2537 * int (*ident[3])[] | |
2538 * although the D style would be: | |
2539 * int[]*[3] ident | |
2540 */ | |
2541 nextToken(); | |
2542 ts = parseDeclarator(t, pident); | |
2543 check(TOK.TOKrparen); | |
2544 break; | |
2545 | |
2546 default: | |
2547 ts = t; | |
2548 break; | |
2549 } | |
2550 | |
2551 // parse DeclaratorSuffixes | |
2552 while (1) | |
2553 { | |
2554 switch (token.value) | |
2555 { | |
2556 version (CARRAYDECL) { | |
2557 /* Support C style array syntax: | |
2558 * int ident[] | |
2559 * as opposed to D-style: | |
2560 * int[] ident | |
2561 */ | |
2562 case TOK.TOKlbracket: | |
2563 { // This is the old C-style post [] syntax. | |
2564 TypeNext ta; | |
2565 nextToken(); | |
2566 if (token.value == TOK.TOKrbracket) | |
2567 { // It's a dynamic array | |
2568 ta = new TypeDArray(t); // [] | |
2569 nextToken(); | |
2570 } | |
79 | 2571 else if (token.value == TOKnew && peekNext() == TOKrbracket) |
2572 { | |
2573 t = new TypeNewArray(t); // [new] | |
2574 nextToken(); | |
2575 nextToken(); | |
2576 } | |
0 | 2577 else if (isDeclaration(&token, 0, TOK.TOKrbracket, null)) |
2578 { // It's an associative array | |
2579 | |
2580 //printf("it's an associative array\n"); | |
2581 Type index = parseType(); // [ type ] | |
2582 check(TOK.TOKrbracket); | |
2583 ta = new TypeAArray(t, index); | |
2584 } | |
2585 else | |
2586 { | |
2587 //printf("It's a static array\n"); | |
2588 Expression e = parseExpression(); // [ expression ] | |
2589 ta = new TypeSArray(t, e); | |
2590 check(TOK.TOKrbracket); | |
2591 } | |
2592 | |
2593 /* Insert ta into | |
2594 * ts . ... . t | |
2595 * so that | |
2596 * ts . ... . ta . t | |
2597 */ | |
2598 Type* pt; | |
2599 for (pt = &ts; *pt !is t; pt = &(cast(TypeNext)*pt).next) { | |
2600 ; | |
2601 } | |
2602 *pt = ta; | |
2603 continue; | |
2604 } | |
2605 } | |
2606 case TOK.TOKlparen: | |
2607 { | |
2608 if (tpl) | |
2609 { | |
2610 /* Look ahead to see if this is (...)(...), | |
2611 * i.e. a function template declaration | |
2612 */ | |
2613 if (peekPastParen(&token).value == TOK.TOKlparen) | |
2614 { | |
2615 //printf("function template declaration\n"); | |
2616 | |
2617 // Gather template parameter list | |
2618 *tpl = parseTemplateParameterList(); | |
2619 } | |
2620 } | |
2621 | |
2622 int varargs; | |
2623 Arguments arguments = parseParameters(&varargs); | |
2624 Type tf = new TypeFunction(arguments, t, varargs, linkage); | |
2625 | |
2626 /* Parse const/invariant/nothrow/pure postfix | |
2627 */ | |
2628 while (1) | |
2629 { | |
2630 switch (token.value) | |
2631 { | |
2632 case TOK.TOKconst: | |
2633 if (tf.isShared()) | |
2634 tf = tf.makeSharedConst(); | |
2635 else | |
2636 tf = tf.makeConst(); | |
2637 nextToken(); | |
2638 continue; | |
2639 | |
2640 case TOK.TOKinvariant: | |
2641 case TOK.TOKimmutable: | |
2642 tf = tf.makeInvariant(); | |
2643 nextToken(); | |
2644 continue; | |
2645 | |
2646 case TOK.TOKshared: | |
2647 if (tf.isConst()) | |
2648 tf = tf.makeSharedConst(); | |
2649 else | |
2650 tf = tf.makeShared(); | |
2651 nextToken(); | |
2652 continue; | |
2653 | |
2654 case TOK.TOKnothrow: | |
2655 (cast(TypeFunction)tf).isnothrow = 1; | |
2656 nextToken(); | |
2657 continue; | |
2658 | |
2659 case TOK.TOKpure: | |
2660 (cast(TypeFunction)tf).ispure = 1; | |
2661 nextToken(); | |
2662 continue; | |
2663 | |
2664 case TOK.TOKat: | |
2665 nextToken(); | |
2666 if (token.value != TOK.TOKidentifier) | |
2667 { error("attribute identifier expected"); | |
2668 nextToken(); | |
2669 continue; | |
2670 } | |
2671 Identifier id = token.ident; | |
2672 if (id is Id.property) | |
96 | 2673 (cast(TypeFunction)tf).isproperty = 1; |
0 | 2674 else |
96 | 2675 error("valid attribute identifiers are @property, not @%s", id.toChars()); |
0 | 2676 nextToken(); |
2677 continue; | |
2678 default: | |
2679 break; /// | |
2680 } | |
2681 break; | |
2682 } | |
2683 | |
2684 /* Insert tf into | |
2685 * ts . ... . t | |
2686 * so that | |
2687 * ts . ... . tf . t | |
2688 */ | |
2689 Type* pt; | |
2690 for (pt = &ts; *pt !is t; pt = &(cast(TypeNext)*pt).next) { | |
2691 ; | |
2692 } | |
2693 *pt = tf; | |
2694 break; | |
2695 } | |
2696 | |
2697 default: | |
2698 break; /// | |
2699 } | |
2700 break; | |
2701 } | |
2702 | |
2703 return ts; | |
2704 } | |
2705 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2706 Dsymbols parseDeclarations(STC storage_class) |
0 | 2707 { |
2708 STC stc; | |
2709 Type ts; | |
2710 Type t; | |
2711 Type tfirst; | |
2712 Identifier ident; | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2713 Dsymbols a; |
0 | 2714 TOK tok = TOK.TOKreserved; |
79 | 2715 string comment = token.blockComment; |
0 | 2716 LINK link = linkage; |
2717 | |
2718 //printf("parseDeclarations() %s\n", token.toChars()); | |
2719 if (storage_class) | |
2720 { ts = null; // infer type | |
2721 goto L2; | |
2722 } | |
2723 | |
2724 switch (token.value) | |
2725 { | |
2726 case TOK.TOKalias: | |
2727 /* Look for: | |
2728 * alias identifier this; | |
2729 */ | |
2730 tok = token.value; | |
2731 nextToken(); | |
2732 if (token.value == TOK.TOKidentifier && peek(&token).value == TOK.TOKthis) | |
2733 { | |
2734 AliasThis s = new AliasThis(this.loc, token.ident); | |
2735 nextToken(); | |
2736 check(TOK.TOKthis); | |
2737 check(TOK.TOKsemicolon); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2738 a = new Dsymbols(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2739 a.push(s); |
0 | 2740 addComment(s, comment); |
2741 return a; | |
2742 } | |
2743 break; | |
2744 case TOK.TOKtypedef: | |
2745 tok = token.value; | |
2746 nextToken(); | |
2747 break; | |
2748 default: | |
2749 break; | |
2750 } | |
2751 | |
2752 storage_class = STC.STCundefined; | |
2753 while (1) | |
2754 { | |
2755 switch (token.value) | |
2756 { | |
2757 case TOK.TOKconst: | |
2758 if (peek(&token).value == TOK.TOKlparen) | |
2759 break; // const as type constructor | |
2760 stc = STC.STCconst; // const as storage class | |
2761 goto L1; | |
2762 | |
2763 case TOK.TOKinvariant: | |
2764 case TOK.TOKimmutable: | |
2765 if (peek(&token).value == TOK.TOKlparen) | |
2766 break; | |
2767 stc = STC.STCimmutable; | |
2768 goto L1; | |
2769 | |
2770 case TOK.TOKshared: | |
2771 if (peek(&token).value == TOK.TOKlparen) | |
2772 break; | |
2773 stc = STC.STCshared; | |
2774 goto L1; | |
2775 | |
2776 case TOK.TOKstatic: stc = STC.STCstatic; goto L1; | |
2777 case TOK.TOKfinal: stc = STC.STCfinal; goto L1; | |
2778 case TOK.TOKauto: stc = STC.STCauto; goto L1; | |
2779 case TOK.TOKscope: stc = STC.STCscope; goto L1; | |
2780 case TOK.TOKoverride: stc = STC.STCoverride; goto L1; | |
2781 case TOK.TOKabstract: stc = STC.STCabstract; goto L1; | |
2782 case TOK.TOKsynchronized: stc = STC.STCsynchronized; goto L1; | |
2783 case TOK.TOKdeprecated: stc = STC.STCdeprecated; goto L1; | |
2784 version (DMDV2) { | |
2785 case TOK.TOKnothrow: stc = STC.STCnothrow; goto L1; | |
2786 case TOK.TOKpure: stc = STC.STCpure; goto L1; | |
2787 case TOK.TOKref: stc = STC.STCref; goto L1; | |
2788 case TOK.TOKtls: stc = STC.STCtls; goto L1; | |
2789 case TOK.TOKgshared: stc = STC.STCgshared; goto L1; | |
2790 case TOK.TOKenum: stc = STC.STCmanifest; goto L1; | |
2791 } | |
2792 L1: | |
2793 if (storage_class & stc) | |
2794 error("redundant storage class '%s'", token.toChars()); | |
2795 storage_class = (storage_class | stc); | |
2796 composeStorageClass(storage_class); | |
2797 nextToken(); | |
2798 continue; | |
2799 | |
2800 case TOK.TOKextern: | |
2801 if (peek(&token).value != TOK.TOKlparen) | |
2802 { stc = STC.STCextern; | |
2803 goto L1; | |
2804 } | |
2805 | |
2806 link = parseLinkage(); | |
2807 continue; | |
2808 | |
2809 default: | |
2810 break; | |
2811 } | |
2812 break; | |
2813 } | |
2814 | |
2815 /* Look for auto initializers: | |
2816 * storage_class identifier = initializer; | |
2817 */ | |
2818 if (storage_class && | |
2819 token.value == TOK.TOKidentifier && | |
2820 peek(&token).value == TOK.TOKassign) | |
2821 { | |
2822 return parseAutoDeclarations(storage_class, comment); | |
2823 } | |
2824 | |
2825 if (token.value == TOK.TOKclass) | |
2826 { | |
2827 AggregateDeclaration s = cast(AggregateDeclaration)parseAggregate(); | |
2828 s.storage_class |= storage_class; | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2829 a = new Dsymbols(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2830 a.push(s); |
0 | 2831 addComment(s, comment); |
2832 return a; | |
2833 } | |
2834 | |
2835 /* Look for return type inference for template functions. | |
2836 */ | |
2837 { | |
2838 Token *tk; | |
2839 if (storage_class && | |
2840 token.value == TOK.TOKidentifier && | |
2841 (tk = peek(&token)).value == TOK.TOKlparen && | |
2842 skipParens(tk, &tk) && | |
2843 peek(tk).value == TOK.TOKlparen) | |
2844 { | |
2845 ts = null; | |
2846 } | |
2847 else | |
2848 { | |
2849 ts = parseBasicType(); | |
2850 ts = parseBasicType2(ts); | |
2851 } | |
2852 } | |
2853 | |
2854 L2: | |
2855 tfirst = null; | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2856 a = new Dsymbols(); |
0 | 2857 |
2858 while (1) | |
2859 { | |
2860 Loc loc = this.loc; | |
2861 TemplateParameters tpl = null; | |
2862 | |
2863 ident = null; | |
2864 t = parseDeclarator(ts, &ident, &tpl); | |
2865 assert(t); | |
2866 if (!tfirst) | |
2867 tfirst = t; | |
2868 else if (t != tfirst) | |
2869 error("multiple declarations must have the same type, not %s and %s", | |
2870 tfirst.toChars(), t.toChars()); | |
2871 if (!ident) | |
2872 error("no identifier for declarator %s", t.toChars()); | |
2873 | |
2874 if (tok == TOK.TOKtypedef || tok == TOK.TOKalias) | |
2875 { Declaration v; | |
2876 Initializer init = null; | |
2877 | |
2878 if (token.value == TOK.TOKassign) | |
2879 { | |
2880 nextToken(); | |
2881 init = parseInitializer(); | |
2882 } | |
2883 if (tok == TOK.TOKtypedef) | |
2884 v = new TypedefDeclaration(loc, ident, t, init); | |
2885 else | |
2886 { if (init) | |
2887 error("alias cannot have initializer"); | |
2888 v = new AliasDeclaration(loc, ident, t); | |
2889 } | |
2890 v.storage_class = storage_class; | |
2891 if (link == linkage) | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2892 a.push(v); |
0 | 2893 else |
2894 { | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2895 auto ax = new Dsymbols(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2896 ax.push(v); |
0 | 2897 Dsymbol s = new LinkDeclaration(link, ax); |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2898 a.push(s); |
0 | 2899 } |
2900 switch (token.value) | |
2901 { case TOK.TOKsemicolon: | |
2902 nextToken(); | |
2903 addComment(v, comment); | |
2904 break; | |
2905 | |
2906 case TOK.TOKcomma: | |
2907 nextToken(); | |
2908 addComment(v, comment); | |
2909 continue; | |
2910 | |
2911 default: | |
2912 error("semicolon expected to close %s declaration", Token.toChars(tok)); | |
2913 break; | |
2914 } | |
2915 } | |
2916 else if (t.ty == TY.Tfunction) | |
2917 { | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2918 auto tf = cast(TypeFunction)t; |
0 | 2919 Expression constraint = null; |
2920 static if (false) { | |
2921 if (Argument.isTPL(tf.parameters)) | |
2922 { | |
2923 if (!tpl) | |
2924 tpl = new TemplateParameters(); | |
2925 } | |
2926 } | |
2927 FuncDeclaration f = | |
2928 new FuncDeclaration(loc, Loc(0), ident, storage_class, t); | |
2929 addComment(f, comment); | |
2930 if (tpl) | |
2931 constraint = parseConstraint(); | |
2932 parseContracts(f); | |
2933 addComment(f, null); | |
2934 Dsymbol s; | |
2935 if (link == linkage) | |
2936 { | |
2937 s = f; | |
2938 } | |
2939 else | |
2940 { | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2941 auto ax = new Dsymbols(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2942 ax.push(f); |
0 | 2943 s = new LinkDeclaration(link, ax); |
2944 } | |
2945 /* A template parameter list means it's a function template | |
2946 */ | |
2947 if (tpl) | |
2948 { | |
2949 // Wrap a template around the function declaration | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2950 auto decldefs = new Dsymbols(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2951 decldefs.push(s); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2952 auto tempdecl = |
0 | 2953 new TemplateDeclaration(loc, s.ident, tpl, constraint, decldefs); |
2954 s = tempdecl; | |
2955 } | |
2956 addComment(s, comment); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2957 a.push(s); |
0 | 2958 } |
2959 else | |
2960 { | |
2961 Initializer init = null; | |
2962 if (token.value == TOK.TOKassign) | |
2963 { | |
2964 nextToken(); | |
2965 init = parseInitializer(); | |
2966 } | |
2967 | |
2968 VarDeclaration v = new VarDeclaration(loc, t, ident, init); | |
2969 v.storage_class = storage_class; | |
2970 if (link == linkage) | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2971 a.push(v); |
0 | 2972 else |
2973 { | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2974 auto ax = new Dsymbols(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2975 ax.push(v); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2976 auto s = new LinkDeclaration(link, ax); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2977 a.push(s); |
0 | 2978 } |
2979 switch (token.value) | |
2980 { case TOK.TOKsemicolon: | |
2981 nextToken(); | |
2982 addComment(v, comment); | |
2983 break; | |
2984 | |
2985 case TOK.TOKcomma: | |
2986 nextToken(); | |
2987 addComment(v, comment); | |
2988 continue; | |
2989 | |
2990 default: | |
2991 error("semicolon expected, not '%s'", token.toChars()); | |
2992 break; | |
2993 } | |
2994 } | |
2995 break; | |
2996 } | |
2997 return a; | |
2998 } | |
2999 | |
3000 void parseContracts(FuncDeclaration f) | |
3001 { | |
3002 Type tb; | |
3003 LINK linksave = linkage; | |
3004 | |
3005 // The following is irrelevant, as it is overridden by sc.linkage in | |
3006 // TypeFunction.semantic | |
3007 linkage = LINK.LINKd; // nested functions have D linkage | |
3008 L1: | |
3009 switch (token.value) | |
3010 { | |
3011 case TOK.TOKlcurly: | |
3012 if (f.frequire || f.fensure) | |
3013 error("missing body { ... } after in or out"); | |
3014 f.fbody = parseStatement(ParseStatementFlags.PSsemi); | |
3015 f.endloc = endloc; | |
3016 break; | |
3017 | |
3018 case TOK.TOKbody: | |
3019 nextToken(); | |
3020 f.fbody = parseStatement(ParseStatementFlags.PScurly); | |
3021 f.endloc = endloc; | |
3022 break; | |
3023 | |
3024 case TOK.TOKsemicolon: | |
3025 if (f.frequire || f.fensure) | |
3026 error("missing body { ... } after in or out"); | |
3027 nextToken(); | |
3028 break; | |
3029 | |
3030 static if (false) { // Do we want this for function declarations, so we can do: | |
3031 // int x, y, foo(), z; | |
3032 case TOK.TOKcomma: | |
3033 nextToken(); | |
3034 continue; | |
3035 } | |
3036 | |
3037 static if (false) { // Dumped feature | |
3038 case TOK.TOKthrow: | |
3039 if (!f.fthrows) | |
3040 f.fthrows = new Array(); | |
3041 nextToken(); | |
3042 check(TOK.TOKlparen); | |
3043 while (1) | |
3044 { | |
3045 tb = parseBasicType(); | |
3046 f.fthrows.push(tb); | |
3047 if (token.value == TOK.TOKcomma) | |
3048 { nextToken(); | |
3049 continue; | |
3050 } | |
3051 break; | |
3052 } | |
3053 check(TOK.TOKrparen); | |
3054 goto L1; | |
3055 } | |
3056 | |
3057 case TOK.TOKin: | |
3058 nextToken(); | |
3059 if (f.frequire) | |
3060 error("redundant 'in' statement"); | |
3061 f.frequire = parseStatement(ParseStatementFlags.PScurly | ParseStatementFlags.PSscope); | |
3062 goto L1; | |
3063 | |
3064 case TOK.TOKout: | |
3065 // parse: out (identifier) { statement } | |
3066 nextToken(); | |
3067 if (token.value != TOK.TOKlcurly) | |
3068 { | |
3069 check(TOK.TOKlparen); | |
3070 if (token.value != TOK.TOKidentifier) | |
3071 error("(identifier) following 'out' expected, not %s", token.toChars()); | |
3072 f.outId = token.ident; | |
3073 nextToken(); | |
3074 check(TOK.TOKrparen); | |
3075 } | |
3076 if (f.fensure) | |
3077 error("redundant 'out' statement"); | |
3078 f.fensure = parseStatement(ParseStatementFlags.PScurly | ParseStatementFlags.PSscope); | |
3079 goto L1; | |
3080 | |
3081 default: | |
3082 error("semicolon expected following function declaration"); | |
3083 break; | |
3084 } | |
3085 linkage = linksave; | |
3086 } | |
3087 | |
3088 Statement parseStatement(ParseStatementFlags flags) | |
3089 { | |
3090 Statement s; | |
3091 Token* t; | |
3092 Condition condition; | |
3093 Statement ifbody; | |
3094 Statement elsebody; | |
3095 bool isfinal; | |
3096 Loc loc = this.loc; | |
3097 | |
3098 //printf("parseStatement()\n"); | |
3099 | |
3100 if (flags & ParseStatementFlags.PScurly && token.value != TOK.TOKlcurly) | |
3101 error("statement expected to be { }, not %s", token.toChars()); | |
3102 | |
3103 switch (token.value) | |
3104 { | |
3105 case TOK.TOKidentifier: | |
3106 /* A leading identifier can be a declaration, label, or expression. | |
3107 * The easiest case to check first is label: | |
3108 */ | |
3109 t = peek(&token); | |
3110 if (t.value == TOK.TOKcolon) | |
3111 { // It's a label | |
3112 | |
3113 Identifier ident = token.ident; | |
3114 nextToken(); | |
3115 nextToken(); | |
3116 s = parseStatement(ParseStatementFlags.PSsemi); | |
3117 s = new LabelStatement(loc, ident, s); | |
3118 break; | |
3119 } | |
3120 // fallthrough to TOK.TOKdot | |
3121 case TOK.TOKdot: | |
3122 case TOK.TOKtypeof: | |
3123 if (isDeclaration(&token, 2, TOK.TOKreserved, null)) | |
3124 goto Ldeclaration; | |
3125 else | |
3126 goto Lexp; | |
3127 break; | |
3128 | |
3129 case TOK.TOKassert: | |
3130 case TOK.TOKthis: | |
3131 case TOK.TOKsuper: | |
3132 case TOK.TOKint32v: | |
3133 case TOK.TOKuns32v: | |
3134 case TOK.TOKint64v: | |
3135 case TOK.TOKuns64v: | |
3136 case TOK.TOKfloat32v: | |
3137 case TOK.TOKfloat64v: | |
3138 case TOK.TOKfloat80v: | |
3139 case TOK.TOKimaginary32v: | |
3140 case TOK.TOKimaginary64v: | |
3141 case TOK.TOKimaginary80v: | |
3142 case TOK.TOKcharv: | |
3143 case TOK.TOKwcharv: | |
3144 case TOK.TOKdcharv: | |
3145 case TOK.TOKnull: | |
3146 case TOK.TOKtrue: | |
3147 case TOK.TOKfalse: | |
3148 case TOK.TOKstring: | |
3149 case TOK.TOKlparen: | |
3150 case TOK.TOKcast: | |
3151 case TOK.TOKmul: | |
3152 case TOK.TOKmin: | |
3153 case TOK.TOKadd: | |
3154 case TOK.TOKplusplus: | |
3155 case TOK.TOKminusminus: | |
3156 case TOK.TOKnew: | |
3157 case TOK.TOKdelete: | |
3158 case TOK.TOKdelegate: | |
3159 case TOK.TOKfunction: | |
3160 case TOK.TOKtypeid: | |
3161 case TOK.TOKis: | |
3162 case TOK.TOKlbracket: | |
3163 version (DMDV2) { | |
3164 case TOK.TOKtraits: | |
3165 case TOK.TOKfile: | |
3166 case TOK.TOKline: | |
3167 } | |
3168 Lexp: | |
3169 { Expression exp; | |
3170 | |
3171 exp = parseExpression(); | |
3172 check(TOK.TOKsemicolon, "statement"); | |
3173 s = new ExpStatement(loc, exp); | |
3174 break; | |
3175 } | |
3176 | |
3177 case TOK.TOKstatic: | |
3178 { // Look ahead to see if it's static assert() or static if() | |
3179 Token *tt; | |
3180 | |
3181 tt = peek(&token); | |
3182 if (tt.value == TOK.TOKassert) | |
3183 { | |
3184 nextToken(); | |
3185 s = new StaticAssertStatement(parseStaticAssert()); | |
3186 break; | |
3187 } | |
3188 if (tt.value == TOK.TOKif) | |
3189 { | |
3190 nextToken(); | |
3191 condition = parseStaticIfCondition(); | |
3192 goto Lcondition; | |
3193 } | |
3194 goto Ldeclaration; | |
3195 } | |
3196 | |
3197 case TOK.TOKfinal: | |
3198 if (peekNext() == TOK.TOKswitch) | |
3199 { | |
3200 nextToken(); | |
3201 isfinal = true; | |
3202 goto Lswitch; | |
3203 } | |
3204 goto Ldeclaration; | |
3205 | |
3206 case TOK.TOKwchar: case TOK.TOKdchar: | |
3207 case TOK.TOKbit: case TOK.TOKbool: case TOK.TOKchar: | |
3208 case TOK.TOKint8: case TOK.TOKuns8: | |
3209 case TOK.TOKint16: case TOK.TOKuns16: | |
3210 case TOK.TOKint32: case TOK.TOKuns32: | |
3211 case TOK.TOKint64: case TOK.TOKuns64: | |
3212 case TOK.TOKfloat32: case TOK.TOKfloat64: case TOK.TOKfloat80: | |
3213 case TOK.TOKimaginary32: case TOK.TOKimaginary64: case TOK.TOKimaginary80: | |
3214 case TOK.TOKcomplex32: case TOK.TOKcomplex64: case TOK.TOKcomplex80: | |
3215 case TOK.TOKvoid: | |
3216 case TOK.TOKtypedef: | |
3217 case TOK.TOKalias: | |
3218 case TOK.TOKconst: | |
3219 case TOK.TOKauto: | |
3220 case TOK.TOKextern: | |
3221 case TOK.TOKinvariant: | |
3222 version (DMDV2) { | |
3223 case TOK.TOKimmutable: | |
3224 case TOK.TOKshared: | |
3225 case TOK.TOKnothrow: | |
3226 case TOK.TOKpure: | |
3227 case TOK.TOKtls: | |
3228 case TOK.TOKgshared: | |
3229 } | |
3230 // case TOK.TOKtypeof: | |
3231 Ldeclaration: | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
3232 { Dsymbols a; |
0 | 3233 |
3234 a = parseDeclarations(STC.STCundefined); | |
3235 if (a.dim > 1) | |
3236 { | |
3237 Statements as = new Statements(); | |
3238 as.reserve(a.dim); | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
3239 foreach(Dsymbol d; a) |
0 | 3240 { |
3241 s = new DeclarationStatement(loc, d); | |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
121
diff
changeset
|
3242 as.push(s); |
0 | 3243 } |
3244 s = new CompoundDeclarationStatement(loc, as); | |
3245 } | |
3246 else if (a.dim == 1) | |
3247 { | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
3248 auto d = a[0]; |
0 | 3249 s = new DeclarationStatement(loc, d); |
3250 } | |
3251 else | |
3252 assert(0); | |
3253 if (flags & ParseStatementFlags.PSscope) | |
3254 s = new ScopeStatement(loc, s); | |
3255 break; | |
3256 } | |
3257 | |
3258 case TOK.TOKstruct: | |
3259 case TOK.TOKunion: | |
3260 case TOK.TOKclass: | |
3261 case TOK.TOKinterface: | |
3262 { Dsymbol d; | |
3263 | |
3264 d = parseAggregate(); | |
3265 s = new DeclarationStatement(loc, d); | |
3266 break; | |
3267 } | |
3268 | |
3269 case TOK.TOKenum: | |
3270 { /* Determine if this is a manifest constant declaration, | |
3271 * or a conventional enum. | |
3272 */ | |
3273 Dsymbol d; | |
3274 Token* tt = peek(&token); | |
3275 if (tt.value == TOK.TOKlcurly || tt.value == TOK.TOKcolon) | |
3276 d = parseEnum(); | |
3277 else if (tt.value != TOK.TOKidentifier) | |
3278 goto Ldeclaration; | |
3279 else | |
3280 { | |
3281 tt = peek(tt); | |
3282 if (tt.value == TOK.TOKlcurly || tt.value == TOK.TOKcolon || | |
3283 tt.value == TOK.TOKsemicolon) | |
3284 d = parseEnum(); | |
3285 else | |
3286 goto Ldeclaration; | |
3287 } | |
3288 s = new DeclarationStatement(loc, d); | |
3289 break; | |
3290 } | |
3291 | |
3292 case TOK.TOKmixin: | |
3293 { t = peek(&token); | |
3294 if (t.value == TOK.TOKlparen) | |
3295 { // mixin(string) | |
3296 nextToken(); | |
3297 check(TOK.TOKlparen, "mixin"); | |
3298 Expression e = parseAssignExp(); | |
3299 check(TOK.TOKrparen); | |
3300 check(TOK.TOKsemicolon); | |
3301 s = new CompileStatement(loc, e); | |
3302 break; | |
3303 } | |
3304 Dsymbol d = parseMixin(); | |
3305 s = new DeclarationStatement(loc, d); | |
3306 break; | |
3307 } | |
3308 | |
3309 case TOK.TOKlcurly: | |
79 | 3310 { |
3311 nextToken(); | |
3312 Statements statements = new Statements(); | |
3313 while (token.value != TOK.TOKrcurly && token.value != TOKeof) | |
0 | 3314 { |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
121
diff
changeset
|
3315 statements.push(parseStatement(ParseStatementFlags.PSsemi | ParseStatementFlags.PScurlyscope)); |
0 | 3316 } |
3317 endloc = this.loc; | |
3318 s = new CompoundStatement(loc, statements); | |
3319 if (flags & (ParseStatementFlags.PSscope | ParseStatementFlags.PScurlyscope)) | |
3320 s = new ScopeStatement(loc, s); | |
3321 nextToken(); | |
3322 break; | |
3323 } | |
3324 | |
3325 case TOK.TOKwhile: | |
3326 { Expression condition2; | |
3327 Statement body_; | |
3328 | |
3329 nextToken(); | |
3330 check(TOK.TOKlparen); | |
3331 condition2 = parseExpression(); | |
3332 check(TOK.TOKrparen); | |
3333 body_ = parseStatement(ParseStatementFlags.PSscope); | |
3334 s = new WhileStatement(loc, condition2, body_); | |
3335 break; | |
3336 } | |
3337 | |
3338 case TOK.TOKsemicolon: | |
3339 if (!(flags & ParseStatementFlags.PSsemi)) | |
3340 error("use '{ }' for an empty statement, not a ';'"); | |
3341 nextToken(); | |
3342 s = new ExpStatement(loc, null); | |
3343 break; | |
3344 | |
3345 case TOK.TOKdo: | |
3346 { Statement body_; | |
3347 Expression condition2; | |
3348 | |
3349 nextToken(); | |
3350 body_ = parseStatement(ParseStatementFlags.PSscope); | |
3351 check(TOK.TOKwhile); | |
3352 check(TOK.TOKlparen); | |
3353 condition2 = parseExpression(); | |
3354 check(TOK.TOKrparen); | |
3355 s = new DoStatement(loc, body_, condition2); | |
3356 break; | |
3357 } | |
3358 | |
3359 case TOK.TOKfor: | |
3360 { | |
3361 Statement init; | |
3362 Expression condition2; | |
3363 Expression increment; | |
3364 Statement body_; | |
3365 | |
3366 nextToken(); | |
3367 check(TOK.TOKlparen); | |
3368 if (token.value == TOK.TOKsemicolon) | |
3369 { init = null; | |
3370 nextToken(); | |
3371 } | |
3372 else | |
3373 { init = parseStatement(cast(ParseStatementFlags)0); | |
3374 } | |
3375 if (token.value == TOK.TOKsemicolon) | |
3376 { | |
3377 condition2 = null; | |
3378 nextToken(); | |
3379 } | |
3380 else | |
3381 { | |
3382 condition2 = parseExpression(); | |
3383 check(TOK.TOKsemicolon, "for condition"); | |
3384 } | |
3385 if (token.value == TOK.TOKrparen) | |
3386 { increment = null; | |
3387 nextToken(); | |
3388 } | |
3389 else | |
3390 { increment = parseExpression(); | |
3391 check(TOK.TOKrparen); | |
3392 } | |
3393 body_ = parseStatement(ParseStatementFlags.PSscope); | |
3394 s = new ForStatement(loc, init, condition2, increment, body_); | |
3395 if (init) | |
3396 s = new ScopeStatement(loc, s); | |
3397 break; | |
3398 } | |
3399 | |
3400 case TOK.TOKforeach: | |
3401 case TOK.TOKforeach_reverse: | |
3402 { | |
3403 TOK op = token.value; | |
3404 Arguments arguments; | |
3405 | |
3406 Statement d; | |
3407 Statement body_; | |
3408 Expression aggr; | |
3409 | |
3410 nextToken(); | |
3411 check(TOK.TOKlparen); | |
3412 | |
3413 arguments = new Arguments(); | |
3414 | |
3415 while (1) | |
3416 { | |
3417 Type tb; | |
3418 Identifier ai = null; | |
3419 Type at; | |
3420 STC storageClass = STC.STCundefined; | |
3421 Argument a; | |
3422 | |
3423 if (token.value == TOK.TOKinout || token.value == TOK.TOKref) | |
3424 { storageClass = STC.STCref; | |
3425 nextToken(); | |
3426 } | |
3427 if (token.value == TOK.TOKidentifier) | |
3428 { | |
3429 Token *tt = peek(&token); | |
3430 if (tt.value == TOK.TOKcomma || tt.value == TOK.TOKsemicolon) | |
3431 { ai = token.ident; | |
3432 at = null; // infer argument type | |
3433 nextToken(); | |
3434 goto Larg; | |
3435 } | |
3436 } | |
3437 at = parseType(&ai); | |
3438 if (!ai) | |
3439 error("no identifier for declarator %s", at.toChars()); | |
3440 Larg: | |
3441 a = new Argument(storageClass, at, ai, null); | |
3442 arguments.push(cast(void*)a); | |
3443 if (token.value == TOK.TOKcomma) | |
3444 { nextToken(); | |
3445 continue; | |
3446 } | |
3447 break; | |
3448 } | |
3449 check(TOK.TOKsemicolon); | |
3450 | |
3451 aggr = parseExpression(); | |
3452 if (token.value == TOK.TOKslice && arguments.dim == 1) | |
3453 { | |
3454 Argument a = cast(Argument)arguments.data[0]; | |
3455 delete arguments; | |
3456 nextToken(); | |
3457 Expression upr = parseExpression(); | |
3458 check(TOK.TOKrparen); | |
3459 body_ = parseStatement(cast(ParseStatementFlags)0); | |
3460 s = new ForeachRangeStatement(loc, op, a, aggr, upr, body_); | |
3461 } | |
3462 else | |
3463 { | |
3464 check(TOK.TOKrparen); | |
3465 body_ = parseStatement(cast(ParseStatementFlags)0); | |
3466 s = new ForeachStatement(loc, op, arguments, aggr, body_); | |
3467 } | |
3468 break; | |
3469 } | |
3470 | |
3471 case TOK.TOKif: | |
3472 { Argument arg = null; | |
3473 Expression condition2; | |
3474 Statement ifbody2; | |
3475 Statement elsebody2; | |
3476 | |
3477 nextToken(); | |
3478 check(TOK.TOKlparen); | |
3479 | |
3480 if (token.value == TOK.TOKauto) | |
3481 { | |
3482 nextToken(); | |
3483 if (token.value == TOK.TOKidentifier) | |
3484 { | |
3485 Token *tt = peek(&token); | |
3486 if (tt.value == TOK.TOKassign) | |
3487 { | |
3488 arg = new Argument(STC.STCundefined, null, token.ident, null); | |
3489 nextToken(); | |
3490 nextToken(); | |
3491 } | |
3492 else | |
3493 { error("= expected following auto identifier"); | |
3494 goto Lerror; | |
3495 } | |
3496 } | |
3497 else | |
3498 { error("identifier expected following auto"); | |
3499 goto Lerror; | |
3500 } | |
3501 } | |
3502 else if (isDeclaration(&token, 2, TOK.TOKassign, null)) | |
3503 { | |
3504 Type at; | |
3505 Identifier ai; | |
3506 | |
3507 at = parseType(&ai); | |
3508 check(TOK.TOKassign); | |
3509 arg = new Argument(STC.STCundefined, at, ai, null); | |
3510 } | |
3511 | |
3512 // Check for " ident;" | |
3513 else if (token.value == TOK.TOKidentifier) | |
3514 { | |
3515 Token *tt = peek(&token); | |
3516 if (tt.value == TOK.TOKcomma || tt.value == TOK.TOKsemicolon) | |
3517 { | |
3518 arg = new Argument(STC.STCundefined, null, token.ident, null); | |
3519 nextToken(); | |
3520 nextToken(); | |
3521 if (1 || !global.params.useDeprecated) | |
3522 error("if (v; e) is deprecated, use if (auto v = e)"); | |
3523 } | |
3524 } | |
3525 | |
3526 condition2 = parseExpression(); | |
3527 check(TOK.TOKrparen); | |
3528 ifbody2 = parseStatement(ParseStatementFlags.PSscope); | |
3529 if (token.value == TOK.TOKelse) | |
3530 { | |
3531 nextToken(); | |
3532 elsebody2 = parseStatement(ParseStatementFlags.PSscope); | |
3533 } | |
3534 else | |
3535 elsebody2 = null; | |
3536 s = new IfStatement(loc, arg, condition2, ifbody2, elsebody2); | |
3537 break; | |
3538 } | |
3539 | |
3540 case TOK.TOKscope: | |
3541 if (peek(&token).value != TOK.TOKlparen) | |
3542 goto Ldeclaration; // scope used as storage class | |
3543 nextToken(); | |
3544 check(TOK.TOKlparen); | |
3545 if (token.value != TOK.TOKidentifier) | |
3546 { error("scope identifier expected"); | |
3547 goto Lerror; | |
3548 } | |
3549 else | |
3550 { TOK tt = TOK.TOKon_scope_exit; | |
3551 Identifier id = token.ident; | |
3552 | |
3553 if (id == Id.exit) | |
3554 tt = TOK.TOKon_scope_exit; | |
3555 else if (id == Id.failure) | |
3556 tt = TOK.TOKon_scope_failure; | |
3557 else if (id == Id.success) | |
3558 tt = TOK.TOKon_scope_success; | |
3559 else | |
3560 error("valid scope identifiers are exit, failure, or success, not %s", id.toChars()); | |
3561 nextToken(); | |
3562 check(TOK.TOKrparen); | |
3563 Statement st = parseStatement(ParseStatementFlags.PScurlyscope); | |
3564 s = new OnScopeStatement(loc, tt, st); | |
3565 break; | |
3566 } | |
3567 | |
3568 case TOK.TOKdebug: | |
3569 nextToken(); | |
3570 condition = parseDebugCondition(); | |
3571 goto Lcondition; | |
3572 | |
3573 case TOK.TOKversion: | |
3574 nextToken(); | |
3575 condition = parseVersionCondition(); | |
3576 goto Lcondition; | |
3577 | |
3578 Lcondition: | |
3579 ifbody = parseStatement(cast(ParseStatementFlags)0 /*ParseStatementFlags.PSsemi*/); | |
3580 elsebody = null; | |
3581 if (token.value == TOK.TOKelse) | |
3582 { | |
3583 nextToken(); | |
3584 elsebody = parseStatement(cast(ParseStatementFlags)0 /*ParseStatementFlags.PSsemi*/); | |
3585 } | |
3586 s = new ConditionalStatement(loc, condition, ifbody, elsebody); | |
3587 break; | |
3588 | |
3589 case TOK.TOKpragma: | |
3590 { Identifier ident; | |
3591 Expressions args = null; | |
3592 Statement body_; | |
3593 | |
3594 nextToken(); | |
3595 check(TOK.TOKlparen); | |
3596 if (token.value != TOK.TOKidentifier) | |
3597 { error("pragma(identifier expected"); | |
3598 goto Lerror; | |
3599 } | |
3600 ident = token.ident; | |
3601 nextToken(); | |
3602 if (token.value == TOK.TOKcomma && peekNext() != TOK.TOKrparen) | |
3603 args = parseArguments(); // pragma(identifier, args...); | |
3604 else | |
3605 check(TOK.TOKrparen); // pragma(identifier); | |
3606 if (token.value == TOK.TOKsemicolon) | |
3607 { nextToken(); | |
3608 body_ = null; | |
3609 } | |
3610 else | |
3611 body_ = parseStatement(ParseStatementFlags.PSsemi); | |
3612 s = new PragmaStatement(loc, ident, args, body_); | |
3613 break; | |
3614 } | |
3615 | |
3616 case TOK.TOKswitch: | |
3617 isfinal = false; | |
3618 goto Lswitch; | |
3619 | |
3620 Lswitch: | |
3621 { | |
3622 nextToken(); | |
3623 check(TOK.TOKlparen); | |
3624 Expression condition2 = parseExpression(); | |
3625 check(TOK.TOKrparen); | |
3626 Statement body_ = parseStatement(ParseStatementFlags.PSscope); | |
3627 s = new SwitchStatement(loc, condition2, body_, isfinal); | |
3628 break; | |
3629 } | |
3630 | |
3631 case TOK.TOKcase: | |
3632 { Expression exp; | |
3633 Statements statements; | |
3634 scope Array cases = new Array(); // array of Expression's | |
3635 Expression last = null; | |
3636 | |
3637 while (1) | |
3638 { | |
3639 nextToken(); | |
3640 exp = parseAssignExp(); | |
3641 cases.push(cast(void*)exp); | |
3642 if (token.value != TOK.TOKcomma) | |
3643 break; | |
3644 } | |
3645 check(TOK.TOKcolon); | |
3646 | |
3647 version (DMDV2) { | |
3648 /* case exp: .. case last: | |
3649 */ | |
3650 if (token.value == TOK.TOKslice) | |
3651 { | |
3652 if (cases.dim > 1) | |
3653 error("only one case allowed for start of case range"); | |
3654 nextToken(); | |
3655 check(TOK.TOKcase); | |
3656 last = parseAssignExp(); | |
3657 check(TOK.TOKcolon); | |
3658 } | |
3659 } | |
3660 | |
3661 statements = new Statements(); | |
3662 while (token.value != TOK.TOKcase && | |
3663 token.value != TOK.TOKdefault && | |
79 | 3664 token.value != TOKeof && |
0 | 3665 token.value != TOK.TOKrcurly) |
3666 { | |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
121
diff
changeset
|
3667 statements.push(parseStatement(ParseStatementFlags.PSsemi | ParseStatementFlags.PScurlyscope)); |
0 | 3668 } |
3669 s = new CompoundStatement(loc, statements); | |
3670 s = new ScopeStatement(loc, s); | |
3671 | |
3672 ///version (DMDV2) { | |
3673 if (last) | |
3674 { | |
3675 s = new CaseRangeStatement(loc, exp, last, s); | |
3676 } | |
3677 else | |
3678 ///} | |
3679 { | |
3680 // Keep cases in order by building the case statements backwards | |
3681 for (int i = cases.dim; i; i--) | |
3682 { | |
3683 exp = cast(Expression)cases.data[i - 1]; | |
3684 s = new CaseStatement(loc, exp, s); | |
3685 } | |
3686 } | |
3687 break; | |
3688 } | |
3689 | |
3690 case TOK.TOKdefault: | |
3691 { | |
3692 Statements statements; | |
3693 | |
3694 nextToken(); | |
3695 check(TOK.TOKcolon); | |
3696 | |
3697 statements = new Statements(); | |
3698 while (token.value != TOK.TOKcase && | |
3699 token.value != TOK.TOKdefault && | |
79 | 3700 token.value != TOKeof && |
0 | 3701 token.value != TOK.TOKrcurly) |
3702 { | |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
121
diff
changeset
|
3703 statements.push(parseStatement(ParseStatementFlags.PSsemi | ParseStatementFlags.PScurlyscope)); |
0 | 3704 } |
3705 s = new CompoundStatement(loc, statements); | |
3706 s = new ScopeStatement(loc, s); | |
3707 s = new DefaultStatement(loc, s); | |
3708 break; | |
3709 } | |
3710 | |
3711 case TOK.TOKreturn: | |
3712 { Expression exp; | |
3713 | |
3714 nextToken(); | |
3715 if (token.value == TOK.TOKsemicolon) | |
3716 exp = null; | |
3717 else | |
3718 exp = parseExpression(); | |
3719 check(TOK.TOKsemicolon, "return statement"); | |
3720 s = new ReturnStatement(loc, exp); | |
3721 break; | |
3722 } | |
3723 | |
3724 case TOK.TOKbreak: | |
3725 { Identifier ident; | |
3726 | |
3727 nextToken(); | |
3728 if (token.value == TOK.TOKidentifier) | |
3729 { ident = token.ident; | |
3730 nextToken(); | |
3731 } | |
3732 else | |
3733 ident = null; | |
3734 check(TOK.TOKsemicolon, "break statement"); | |
3735 s = new BreakStatement(loc, ident); | |
3736 break; | |
3737 } | |
3738 | |
3739 case TOK.TOKcontinue: | |
3740 { Identifier ident; | |
3741 | |
3742 nextToken(); | |
3743 if (token.value == TOK.TOKidentifier) | |
3744 { ident = token.ident; | |
3745 nextToken(); | |
3746 } | |
3747 else | |
3748 ident = null; | |
3749 check(TOK.TOKsemicolon, "continue statement"); | |
3750 s = new ContinueStatement(loc, ident); | |
3751 break; | |
3752 } | |
3753 | |
3754 case TOK.TOKgoto: | |
3755 { Identifier ident; | |
3756 | |
3757 nextToken(); | |
3758 if (token.value == TOK.TOKdefault) | |
3759 { | |
3760 nextToken(); | |
3761 s = new GotoDefaultStatement(loc); | |
3762 } | |
3763 else if (token.value == TOK.TOKcase) | |
3764 { | |
3765 Expression exp = null; | |
3766 | |
3767 nextToken(); | |
3768 if (token.value != TOK.TOKsemicolon) | |
3769 exp = parseExpression(); | |
3770 s = new GotoCaseStatement(loc, exp); | |
3771 } | |
3772 else | |
3773 { | |
3774 if (token.value != TOK.TOKidentifier) | |
3775 { error("Identifier expected following goto"); | |
3776 ident = null; | |
3777 } | |
3778 else | |
3779 { ident = token.ident; | |
3780 nextToken(); | |
3781 } | |
3782 s = new GotoStatement(loc, ident); | |
3783 } | |
3784 check(TOK.TOKsemicolon, "goto statement"); | |
3785 break; | |
3786 } | |
3787 | |
3788 case TOK.TOKsynchronized: | |
3789 { Expression exp; | |
3790 Statement body_; | |
3791 | |
3792 nextToken(); | |
3793 if (token.value == TOK.TOKlparen) | |
3794 { | |
3795 nextToken(); | |
3796 exp = parseExpression(); | |
3797 check(TOK.TOKrparen); | |
3798 } | |
3799 else | |
3800 exp = null; | |
3801 body_ = parseStatement(ParseStatementFlags.PSscope); | |
3802 s = new SynchronizedStatement(loc, exp, body_); | |
3803 break; | |
3804 } | |
3805 | |
3806 case TOK.TOKwith: | |
3807 { Expression exp; | |
3808 Statement body_; | |
3809 | |
3810 nextToken(); | |
3811 check(TOK.TOKlparen); | |
3812 exp = parseExpression(); | |
3813 check(TOK.TOKrparen); | |
3814 body_ = parseStatement(ParseStatementFlags.PSscope); | |
3815 s = new WithStatement(loc, exp, body_); | |
3816 break; | |
3817 } | |
3818 | |
3819 case TOK.TOKtry: | |
3820 { Statement body_; | |
3821 Array catches = null; | |
3822 Statement finalbody = null; | |
3823 | |
3824 nextToken(); | |
3825 body_ = parseStatement(ParseStatementFlags.PSscope); | |
3826 while (token.value == TOK.TOKcatch) | |
3827 { | |
3828 Statement handler; | |
3829 Catch c; | |
3830 Type tt; | |
3831 Identifier id; | |
3832 Loc loc2 = this.loc; | |
3833 | |
3834 nextToken(); | |
3835 if (token.value == TOK.TOKlcurly) | |
3836 { | |
3837 tt = null; | |
3838 id = null; | |
3839 } | |
3840 else | |
3841 { | |
3842 check(TOK.TOKlparen); | |
3843 id = null; | |
3844 tt = parseType(&id); | |
3845 check(TOK.TOKrparen); | |
3846 } | |
3847 handler = parseStatement(cast(ParseStatementFlags)0); | |
3848 c = new Catch(loc2, tt, id, handler); | |
3849 if (!catches) | |
3850 catches = new Array(); | |
3851 catches.push(cast(void*)c); | |
3852 } | |
3853 | |
3854 if (token.value == TOK.TOKfinally) | |
3855 { nextToken(); | |
3856 finalbody = parseStatement(cast(ParseStatementFlags)0); | |
3857 } | |
3858 | |
3859 s = body_; | |
3860 if (!catches && !finalbody) | |
3861 error("catch or finally expected following try"); | |
3862 else | |
3863 { if (catches) | |
3864 s = new TryCatchStatement(loc, body_, catches); | |
3865 if (finalbody) | |
3866 s = new TryFinallyStatement(loc, s, finalbody); | |
3867 } | |
3868 break; | |
3869 } | |
3870 | |
3871 case TOK.TOKthrow: | |
3872 { Expression exp; | |
3873 | |
3874 nextToken(); | |
3875 exp = parseExpression(); | |
3876 check(TOK.TOKsemicolon, "throw statement"); | |
3877 s = new ThrowStatement(loc, exp); | |
3878 break; | |
3879 } | |
3880 | |
3881 case TOK.TOKvolatile: | |
3882 nextToken(); | |
3883 s = parseStatement(ParseStatementFlags.PSsemi | ParseStatementFlags.PScurlyscope); | |
3884 version (DMDV2) { | |
3885 if (!global.params.useDeprecated) | |
3886 error("volatile statements deprecated; used synchronized statements instead"); | |
3887 } | |
3888 s = new VolatileStatement(loc, s); | |
3889 break; | |
3890 | |
3891 case TOK.TOKasm: | |
3892 { Statements statements; | |
3893 Identifier label; | |
3894 Loc labelloc; | |
3895 Token* toklist; | |
3896 Token** ptoklist; | |
3897 | |
3898 // Parse the asm block into a sequence of AsmStatements, | |
3899 // each AsmStatement is one instruction. | |
3900 // Separate out labels. | |
3901 // Defer parsing of AsmStatements until semantic processing. | |
3902 | |
3903 nextToken(); | |
3904 check(TOK.TOKlcurly); | |
3905 toklist = null; | |
3906 ptoklist = &toklist; | |
3907 label = null; | |
3908 statements = new Statements(); | |
3909 while (1) | |
3910 { | |
3911 switch (token.value) | |
3912 { | |
3913 case TOK.TOKidentifier: | |
3914 if (!toklist) | |
3915 { | |
3916 // Look ahead to see if it is a label | |
3917 t = peek(&token); | |
3918 if (t.value == TOK.TOKcolon) | |
3919 { // It's a label | |
3920 label = token.ident; | |
3921 labelloc = this.loc; | |
3922 nextToken(); | |
3923 nextToken(); | |
3924 continue; | |
3925 } | |
3926 } | |
3927 goto Ldefault; | |
3928 | |
3929 case TOK.TOKrcurly: | |
3930 if (toklist || label) | |
3931 { | |
3932 error("asm statements must end in ';'"); | |
3933 } | |
3934 break; | |
3935 | |
3936 case TOK.TOKsemicolon: | |
3937 s = null; | |
3938 if (toklist || label) | |
3939 { // Create AsmStatement from list of tokens we've saved | |
3940 s = new AsmStatement(this.loc, toklist); | |
3941 toklist = null; | |
3942 ptoklist = &toklist; | |
3943 if (label) | |
3944 { | |
3945 s = new LabelStatement(labelloc, label, s); | |
3946 label = null; | |
3947 } | |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
121
diff
changeset
|
3948 statements.push(s); |
0 | 3949 } |
3950 nextToken(); | |
3951 continue; | |
3952 | |
3953 case TOK.TOKeof: | |
3954 /* { */ | |
3955 error("matching '}' expected, not end of file"); | |
3956 break; | |
3957 | |
3958 default: | |
3959 Ldefault: | |
3960 *ptoklist = new Token(); | |
3961 memcpy(*ptoklist, &token, Token.sizeof); | |
3962 ptoklist = &(*ptoklist).next; | |
3963 *ptoklist = null; | |
3964 | |
3965 nextToken(); | |
3966 continue; | |
3967 } | |
3968 break; | |
3969 } | |
3970 s = new CompoundStatement(loc, statements); | |
3971 nextToken(); | |
3972 break; | |
3973 } | |
3974 | |
3975 default: | |
3976 error("found '%s' instead of statement", token.toChars()); | |
3977 goto Lerror; | |
3978 | |
3979 Lerror: | |
3980 while (token.value != TOK.TOKrcurly && | |
3981 token.value != TOK.TOKsemicolon && | |
3982 token.value != TOK.TOKeof) | |
3983 nextToken(); | |
3984 if (token.value == TOK.TOKsemicolon) | |
3985 nextToken(); | |
3986 s = null; | |
3987 break; | |
3988 } | |
3989 | |
3990 return s; | |
3991 } | |
3992 | |
3993 /***************************************** | |
3994 * Parse initializer for variable declaration. | |
3995 */ | |
3996 Initializer parseInitializer() | |
3997 { | |
3998 StructInitializer is_; | |
3999 ArrayInitializer ia; | |
4000 ExpInitializer ie; | |
4001 Expression e; | |
4002 Identifier id; | |
4003 Initializer value; | |
4004 int comma; | |
4005 Loc loc = this.loc; | |
4006 Token* t; | |
4007 int braces; | |
4008 int brackets; | |
4009 | |
4010 switch (token.value) | |
4011 { | |
4012 case TOK.TOKlcurly: | |
4013 /* Scan ahead to see if it is a struct initializer or | |
4014 * a function literal. | |
4015 * If it contains a ';', it is a function literal. | |
4016 * Treat { } as a struct initializer. | |
4017 */ | |
4018 braces = 1; | |
4019 for (t = peek(&token); 1; t = peek(t)) | |
4020 { | |
4021 switch (t.value) | |
4022 { | |
4023 case TOK.TOKsemicolon: | |
4024 case TOK.TOKreturn: | |
4025 goto Lexpression; | |
4026 | |
4027 case TOK.TOKlcurly: | |
4028 braces++; | |
4029 continue; | |
4030 | |
4031 case TOK.TOKrcurly: | |
4032 if (--braces == 0) | |
4033 break; | |
4034 continue; | |
4035 | |
4036 case TOK.TOKeof: | |
4037 break; | |
4038 | |
4039 default: | |
4040 continue; | |
4041 } | |
4042 break; | |
4043 } | |
4044 | |
4045 is_ = new StructInitializer(loc); | |
4046 nextToken(); | |
4047 comma = 0; | |
4048 while (1) | |
4049 { | |
4050 switch (token.value) | |
4051 { | |
4052 case TOK.TOKidentifier: | |
4053 if (comma == 1) | |
4054 error("comma expected separating field initializers"); | |
4055 t = peek(&token); | |
4056 if (t.value == TOK.TOKcolon) | |
4057 { | |
4058 id = token.ident; | |
4059 nextToken(); | |
4060 nextToken(); // skip over ':' | |
4061 } | |
4062 else | |
4063 { | |
4064 id = null; | |
4065 } | |
4066 value = parseInitializer(); | |
4067 is_.addInit(id, value); | |
4068 comma = 1; | |
4069 continue; | |
4070 | |
4071 case TOK.TOKcomma: | |
4072 nextToken(); | |
4073 comma = 2; | |
4074 continue; | |
4075 | |
4076 case TOK.TOKrcurly: // allow trailing comma's | |
4077 nextToken(); | |
4078 break; | |
4079 | |
4080 case TOK.TOKeof: | |
4081 error("found EOF instead of initializer"); | |
4082 break; | |
4083 | |
4084 default: | |
4085 value = parseInitializer(); | |
4086 is_.addInit(null, value); | |
4087 comma = 1; | |
4088 continue; | |
4089 //error("found '%s' instead of field initializer", token.toChars()); | |
4090 //break; | |
4091 } | |
4092 break; | |
4093 } | |
4094 return is_; | |
4095 | |
4096 case TOK.TOKlbracket: | |
4097 /* Scan ahead to see if it is an array initializer or | |
4098 * an expression. | |
4099 * If it ends with a ';' ',' or '}', it is an array initializer. | |
4100 */ | |
4101 brackets = 1; | |
4102 for (t = peek(&token); 1; t = peek(t)) | |
4103 { | |
4104 switch (t.value) | |
4105 { | |
4106 case TOK.TOKlbracket: | |
4107 brackets++; | |
4108 continue; | |
4109 | |
4110 case TOK.TOKrbracket: | |
4111 if (--brackets == 0) | |
4112 { | |
4113 t = peek(t); | |
4114 if (t.value != TOK.TOKsemicolon && | |
4115 t.value != TOK.TOKcomma && | |
4116 t.value != TOK.TOKrcurly) | |
4117 goto Lexpression; | |
4118 break; | |
4119 } | |
4120 continue; | |
4121 | |
4122 case TOK.TOKeof: | |
4123 break; | |
4124 | |
4125 default: | |
4126 continue; | |
4127 } | |
4128 break; | |
4129 } | |
4130 | |
4131 ia = new ArrayInitializer(loc); | |
4132 nextToken(); | |
4133 comma = 0; | |
4134 while (true) | |
4135 { | |
4136 switch (token.value) | |
4137 { | |
4138 default: | |
4139 if (comma == 1) | |
4140 { | |
4141 error("comma expected separating array initializers, not %s", token.toChars()); | |
4142 nextToken(); | |
4143 break; | |
4144 } | |
4145 e = parseAssignExp(); | |
4146 if (!e) | |
4147 break; | |
4148 if (token.value == TOK.TOKcolon) | |
4149 { | |
4150 nextToken(); | |
4151 value = parseInitializer(); | |
4152 } | |
4153 else | |
4154 { value = new ExpInitializer(e.loc, e); | |
4155 e = null; | |
4156 } | |
4157 ia.addInit(e, value); | |
4158 comma = 1; | |
4159 continue; | |
4160 | |
4161 case TOK.TOKlcurly: | |
4162 case TOK.TOKlbracket: | |
4163 if (comma == 1) | |
4164 error("comma expected separating array initializers, not %s", token.toChars()); | |
4165 value = parseInitializer(); | |
4166 ia.addInit(null, value); | |
4167 comma = 1; | |
4168 continue; | |
4169 | |
4170 case TOK.TOKcomma: | |
4171 nextToken(); | |
4172 comma = 2; | |
4173 continue; | |
4174 | |
4175 case TOK.TOKrbracket: // allow trailing comma's | |
4176 nextToken(); | |
4177 break; | |
4178 | |
4179 case TOK.TOKeof: | |
4180 error("found '%s' instead of array initializer", token.toChars()); | |
4181 break; | |
4182 } | |
4183 break; | |
4184 } | |
4185 return ia; | |
4186 | |
4187 case TOK.TOKvoid: | |
4188 t = peek(&token); | |
4189 if (t.value == TOK.TOKsemicolon || t.value == TOK.TOKcomma) | |
4190 { | |
4191 nextToken(); | |
4192 return new VoidInitializer(loc); | |
4193 } | |
4194 goto Lexpression; | |
4195 | |
4196 default: | |
4197 Lexpression: | |
4198 e = parseAssignExp(); | |
4199 ie = new ExpInitializer(loc, e); | |
4200 return ie; | |
4201 } | |
4202 } | |
4203 | |
4204 /***************************************** | |
4205 * Parses default argument initializer expression that is an assign expression, | |
4206 * with special handling for __FILE__ and __LINE__. | |
4207 */ | |
4208 version (DMDV2) { | |
4209 Expression parseDefaultInitExp() | |
4210 { | |
4211 if (token.value == TOK.TOKfile || | |
4212 token.value == TOK.TOKline) | |
4213 { | |
4214 Token* t = peek(&token); | |
4215 if (t.value == TOK.TOKcomma || t.value == TOK.TOKrparen) | |
4216 { | |
4217 Expression e; | |
4218 | |
4219 if (token.value == TOK.TOKfile) | |
4220 e = new FileInitExp(loc); | |
4221 else | |
4222 e = new LineInitExp(loc); | |
4223 nextToken(); | |
4224 return e; | |
4225 } | |
4226 } | |
4227 | |
4228 Expression e = parseAssignExp(); | |
4229 return e; | |
4230 } | |
4231 } | |
4232 void check(Loc loc, TOK value) | |
4233 { | |
4234 if (token.value != value) | |
4235 error(loc, "found '%s' when expecting '%s'", token.toChars(), Token.toChars(value)); | |
4236 nextToken(); | |
4237 } | |
4238 | |
4239 void check(TOK value) | |
4240 { | |
4241 check(loc, value); | |
4242 } | |
4243 | |
4244 void check(TOK value, string string_) | |
4245 { | |
4246 if (token.value != value) { | |
4247 error("found '%s' when expecting '%s' following '%s'", token.toChars(), Token.toChars(value), string_); | |
4248 } | |
4249 nextToken(); | |
4250 } | |
4251 | |
4252 /************************************ | |
4253 * Determine if the scanner is sitting on the start of a declaration. | |
4254 * Input: | |
4255 * needId 0 no identifier | |
4256 * 1 identifier optional | |
4257 * 2 must have identifier | |
4258 * Output: | |
4259 * if *pt is not null, it is set to the ending token, which would be endtok | |
4260 */ | |
4261 | |
4262 bool isDeclaration(Token* t, int needId, TOK endtok, Token** pt) | |
4263 { | |
4264 //printf("isDeclaration(needId = %d)\n", needId); | |
4265 int haveId = 0; | |
4266 | |
4267 version (DMDV2) { | |
4268 if ((t.value == TOK.TOKconst || | |
4269 t.value == TOK.TOKinvariant || | |
4270 t.value == TOK.TOKimmutable || | |
4271 t.value == TOK.TOKshared) && | |
4272 peek(t).value != TOK.TOKlparen) | |
4273 { | |
4274 /* const type | |
4275 * immutable type | |
4276 * shared type | |
4277 */ | |
4278 t = peek(t); | |
4279 } | |
4280 } | |
4281 | |
4282 if (!isBasicType(&t)) | |
4283 { | |
4284 goto Lisnot; | |
4285 } | |
4286 if (!isDeclarator(&t, &haveId, endtok)) | |
4287 goto Lisnot; | |
4288 if ( needId == 1 || | |
4289 (needId == 0 && !haveId) || | |
4290 (needId == 2 && haveId)) | |
4291 { | |
4292 if (pt) | |
4293 *pt = t; | |
4294 goto Lis; | |
4295 } | |
4296 else | |
4297 goto Lisnot; | |
4298 | |
4299 Lis: | |
4300 //printf("\tis declaration, t = %s\n", t.toChars()); | |
4301 return true; | |
4302 | |
4303 Lisnot: | |
4304 //printf("\tis not declaration\n"); | |
4305 return false; | |
4306 } | |
4307 | |
4308 bool isBasicType(Token** pt) | |
4309 { | |
4310 // This code parallels parseBasicType() | |
4311 Token* t = *pt; | |
4312 Token* t2; | |
4313 int parens; | |
4314 int haveId = 0; | |
4315 | |
4316 switch (t.value) | |
4317 { | |
4318 case TOKwchar: | |
4319 case TOKdchar: | |
4320 case TOKbit: | |
4321 case TOKbool: | |
4322 case TOKchar: | |
4323 case TOKint8: | |
4324 case TOKuns8: | |
4325 case TOKint16: | |
4326 case TOKuns16: | |
4327 case TOKint32: | |
4328 case TOKuns32: | |
4329 case TOKint64: | |
4330 case TOKuns64: | |
4331 case TOKfloat32: | |
4332 case TOKfloat64: | |
4333 case TOKfloat80: | |
4334 case TOKimaginary32: | |
4335 case TOKimaginary64: | |
4336 case TOKimaginary80: | |
4337 case TOKcomplex32: | |
4338 case TOKcomplex64: | |
4339 case TOKcomplex80: | |
4340 case TOKvoid: | |
4341 t = peek(t); | |
4342 break; | |
4343 | |
4344 case TOK.TOKidentifier: | |
4345 L5: | |
4346 t = peek(t); | |
4347 if (t.value == TOK.TOKnot) | |
4348 { | |
4349 goto L4; | |
4350 } | |
4351 goto L3; | |
4352 while (1) | |
4353 { | |
4354 L2: | |
4355 t = peek(t); | |
4356 L3: | |
4357 if (t.value == TOK.TOKdot) | |
4358 { | |
4359 Ldot: | |
4360 t = peek(t); | |
4361 if (t.value != TOK.TOKidentifier) | |
4362 goto Lfalse; | |
4363 t = peek(t); | |
4364 if (t.value != TOK.TOKnot) | |
4365 goto L3; | |
4366 L4: | |
4367 /* Seen a ! | |
4368 * Look for: | |
4369 * !( args ), !identifier, etc. | |
4370 */ | |
4371 t = peek(t); | |
4372 switch (t.value) | |
4373 { case TOK.TOKidentifier: | |
4374 goto L5; | |
4375 case TOK.TOKlparen: | |
4376 if (!skipParens(t, &t)) | |
4377 goto Lfalse; | |
4378 break; | |
4379 case TOK.TOKwchar: case TOK.TOKdchar: | |
4380 case TOK.TOKbit: case TOK.TOKbool: case TOK.TOKchar: | |
4381 case TOK.TOKint8: case TOK.TOKuns8: | |
4382 case TOK.TOKint16: case TOK.TOKuns16: | |
4383 case TOK.TOKint32: case TOK.TOKuns32: | |
4384 case TOK.TOKint64: case TOK.TOKuns64: | |
4385 case TOK.TOKfloat32: case TOK.TOKfloat64: case TOK.TOKfloat80: | |
4386 case TOK.TOKimaginary32: case TOK.TOKimaginary64: case TOK.TOKimaginary80: | |
4387 case TOK.TOKcomplex32: case TOK.TOKcomplex64: case TOK.TOKcomplex80: | |
4388 case TOK.TOKvoid: | |
4389 case TOK.TOKint32v: | |
4390 case TOK.TOKuns32v: | |
4391 case TOK.TOKint64v: | |
4392 case TOK.TOKuns64v: | |
4393 case TOK.TOKfloat32v: | |
4394 case TOK.TOKfloat64v: | |
4395 case TOK.TOKfloat80v: | |
4396 case TOK.TOKimaginary32v: | |
4397 case TOK.TOKimaginary64v: | |
4398 case TOK.TOKimaginary80v: | |
4399 case TOK.TOKnull: | |
4400 case TOK.TOKtrue: | |
4401 case TOK.TOKfalse: | |
4402 case TOK.TOKcharv: | |
4403 case TOK.TOKwcharv: | |
4404 case TOK.TOKdcharv: | |
4405 case TOK.TOKstring: | |
4406 case TOK.TOKfile: | |
4407 case TOK.TOKline: | |
4408 goto L2; | |
4409 default: | |
4410 goto Lfalse; | |
4411 } | |
4412 } | |
4413 else | |
4414 break; | |
4415 } | |
4416 break; | |
4417 | |
4418 case TOK.TOKdot: | |
4419 goto Ldot; | |
4420 | |
4421 case TOK.TOKtypeof: | |
4422 /* typeof(exp).identifier... | |
4423 */ | |
4424 t = peek(t); | |
4425 if (t.value != TOK.TOKlparen) | |
4426 goto Lfalse; | |
4427 if (!skipParens(t, &t)) | |
4428 goto Lfalse; | |
4429 goto L2; | |
4430 | |
4431 case TOK.TOKconst: | |
4432 case TOK.TOKinvariant: | |
4433 case TOK.TOKimmutable: | |
4434 case TOK.TOKshared: | |
4435 // const(type) or immutable(type) or shared(type) | |
4436 t = peek(t); | |
4437 if (t.value != TOK.TOKlparen) | |
4438 goto Lfalse; | |
4439 t = peek(t); | |
4440 if (!isDeclaration(t, 0, TOK.TOKrparen, &t)) | |
4441 { | |
4442 goto Lfalse; | |
4443 } | |
4444 t = peek(t); | |
4445 break; | |
4446 | |
4447 default: | |
4448 goto Lfalse; | |
4449 } | |
4450 *pt = t; | |
4451 //printf("is\n"); | |
4452 return true; | |
4453 | |
4454 Lfalse: | |
4455 //printf("is not\n"); | |
4456 return false; | |
4457 } | |
4458 | |
4459 bool isDeclarator(Token** pt, int* haveId, TOK endtok) | |
4460 { | |
4461 // This code parallels parseDeclarator() | |
4462 Token* t = *pt; | |
4463 bool parens; | |
4464 | |
4465 //printf("Parser.isDeclarator()\n"); | |
4466 //t.print(); | |
4467 if (t.value == TOK.TOKassign) | |
4468 return false; | |
4469 | |
4470 while (true) | |
4471 { | |
4472 parens = false; | |
4473 switch (t.value) | |
4474 { | |
4475 case TOK.TOKmul: | |
4476 //case TOKand: | |
4477 t = peek(t); | |
4478 continue; | |
4479 | |
4480 case TOK.TOKlbracket: | |
4481 t = peek(t); | |
4482 if (t.value == TOK.TOKrbracket) | |
4483 { | |
4484 t = peek(t); | |
4485 } | |
79 | 4486 else if (t.value == TOKnew && peek(t).value == TOKrbracket) |
4487 { | |
4488 t = peek(t); | |
4489 t = peek(t); | |
4490 } | |
0 | 4491 else if (isDeclaration(t, 0, TOK.TOKrbracket, &t)) |
4492 { | |
4493 // It's an associative array declaration | |
4494 t = peek(t); | |
4495 } | |
4496 else | |
4497 { | |
4498 // [ expression ] | |
4499 // [ expression .. expression ] | |
4500 if (!isExpression(&t)) | |
4501 return false; | |
4502 if (t.value == TOK.TOKslice) | |
4503 { | |
4504 t = peek(t); | |
4505 if (!isExpression(&t)) | |
4506 return false; | |
4507 } | |
4508 if (t.value != TOK.TOKrbracket) | |
4509 return false; | |
4510 t = peek(t); | |
4511 } | |
4512 continue; | |
4513 | |
4514 case TOK.TOKidentifier: | |
4515 if (*haveId) | |
4516 return false; | |
4517 *haveId = true; | |
4518 t = peek(t); | |
4519 break; | |
4520 | |
4521 case TOK.TOKlparen: | |
4522 t = peek(t); | |
4523 | |
4524 if (t.value == TOK.TOKrparen) | |
4525 return false; // () is not a declarator | |
4526 | |
4527 /* Regard ( identifier ) as not a declarator | |
4528 * BUG: what about ( *identifier ) in | |
4529 * f(*p)(x); | |
4530 * where f is a class instance with overloaded () ? | |
4531 * Should we just disallow C-style function pointer declarations? | |
4532 */ | |
4533 if (t.value == TOK.TOKidentifier) | |
4534 { | |
4535 Token *t2 = peek(t); | |
4536 if (t2.value == TOK.TOKrparen) | |
4537 return false; | |
4538 } | |
4539 | |
4540 if (!isDeclarator(&t, haveId, TOK.TOKrparen)) | |
4541 return false; | |
4542 t = peek(t); | |
4543 parens = true; | |
4544 break; | |
4545 | |
4546 case TOK.TOKdelegate: | |
4547 case TOK.TOKfunction: | |
4548 t = peek(t); | |
4549 if (!isParameters(&t)) | |
4550 return false; | |
4551 continue; | |
4552 default: | |
4553 break; /// | |
4554 } | |
4555 break; | |
4556 } | |
4557 | |
4558 while (1) | |
4559 { | |
4560 switch (t.value) | |
4561 { | |
4562 version (CARRAYDECL) { | |
4563 case TOK.TOKlbracket: | |
4564 parens = false; | |
4565 t = peek(t); | |
4566 if (t.value == TOK.TOKrbracket) | |
4567 { | |
4568 t = peek(t); | |
4569 } | |
4570 else if (isDeclaration(t, 0, TOKrbracket, &t)) | |
4571 { | |
4572 // It's an associative array declaration | |
4573 t = peek(t); | |
4574 } | |
4575 else | |
4576 { | |
4577 // [ expression ] | |
4578 if (!isExpression(&t)) | |
4579 return false; | |
4580 if (t.value != TOK.TOKrbracket) | |
4581 return false; | |
4582 t = peek(t); | |
4583 } | |
4584 continue; | |
4585 } | |
4586 | |
4587 case TOK.TOKlparen: | |
4588 parens = false; | |
4589 if (!isParameters(&t)) | |
4590 return false; | |
4591 version (DMDV2) { | |
4592 while (true) | |
4593 { | |
4594 switch (t.value) | |
4595 { | |
4596 case TOK.TOKconst: | |
4597 case TOK.TOKinvariant: | |
4598 case TOK.TOKimmutable: | |
4599 case TOK.TOKshared: | |
4600 case TOK.TOKpure: | |
4601 case TOK.TOKnothrow: | |
4602 t = peek(t); | |
4603 continue; | |
4604 case TOK.TOKat: | |
4605 t = peek(t); // skip '@' | |
4606 t = peek(t); // skip identifier | |
4607 continue; | |
4608 default: | |
4609 break; | |
4610 } | |
4611 break; | |
4612 } | |
4613 } | |
4614 continue; | |
4615 | |
4616 // Valid tokens that follow a declaration | |
4617 case TOK.TOKrparen: | |
4618 case TOK.TOKrbracket: | |
4619 case TOK.TOKassign: | |
4620 case TOK.TOKcomma: | |
4621 case TOK.TOKsemicolon: | |
4622 case TOK.TOKlcurly: | |
4623 case TOK.TOKin: | |
4624 // The !parens is to disallow unnecessary parentheses | |
4625 if (!parens && (endtok == TOK.TOKreserved || endtok == t.value)) | |
4626 { | |
4627 *pt = t; | |
4628 return true; | |
4629 } | |
4630 return false; | |
4631 | |
4632 default: | |
4633 return false; | |
4634 } | |
4635 } | |
4636 } | |
4637 | |
4638 bool isParameters(Token** pt) | |
4639 { | |
4640 // This code parallels parseParameters() | |
4641 Token* t = *pt; | |
4642 | |
4643 //printf("isParameters()\n"); | |
4644 if (t.value != TOKlparen) | |
4645 return false; | |
4646 | |
4647 t = peek(t); | |
4648 for (;1; t = peek(t)) | |
4649 { | |
4650 L1: | |
4651 switch (t.value) | |
4652 { | |
4653 case TOKrparen: | |
4654 break; | |
4655 | |
4656 case TOKdotdotdot: | |
4657 t = peek(t); | |
4658 break; | |
4659 | |
4660 case TOKin: | |
4661 case TOKout: | |
4662 case TOKinout: | |
4663 case TOKref: | |
4664 case TOKlazy: | |
4665 case TOKfinal: | |
4666 continue; | |
4667 | |
4668 case TOKconst: | |
4669 case TOKinvariant: | |
4670 case TOKimmutable: | |
4671 case TOKshared: | |
4672 t = peek(t); | |
4673 if (t.value == TOKlparen) | |
4674 { | |
4675 t = peek(t); | |
4676 if (!isDeclaration(t, 0, TOKrparen, &t)) | |
4677 return false; | |
4678 t = peek(t); // skip past closing ')' | |
4679 goto L2; | |
4680 } | |
4681 goto L1; | |
4682 | |
4683 static if (false) { | |
4684 case TOKstatic: | |
4685 continue; | |
4686 case TOKauto: | |
4687 case TOKalias: | |
4688 t = peek(t); | |
4689 if (t.value == TOKidentifier) | |
4690 t = peek(t); | |
4691 if (t.value == TOKassign) | |
4692 { t = peek(t); | |
4693 if (!isExpression(&t)) | |
4694 return false; | |
4695 } | |
4696 goto L3; | |
4697 } | |
4698 | |
4699 default: | |
4700 { | |
4701 if (!isBasicType(&t)) | |
4702 return false; | |
4703 L2: | |
4704 int tmp = false; | |
4705 if (t.value != TOKdotdotdot && | |
4706 !isDeclarator(&t, &tmp, TOKreserved)) | |
4707 return false; | |
4708 if (t.value == TOKassign) | |
4709 { | |
4710 t = peek(t); | |
4711 if (!isExpression(&t)) | |
4712 return false; | |
4713 } | |
4714 if (t.value == TOKdotdotdot) | |
4715 { | |
4716 t = peek(t); | |
4717 break; | |
4718 } | |
4719 } | |
4720 L3: | |
4721 if (t.value == TOKcomma) | |
4722 { | |
4723 continue; | |
4724 } | |
4725 break; | |
4726 } | |
4727 break; | |
4728 } | |
4729 | |
4730 if (t.value != TOKrparen) | |
4731 return false; | |
4732 | |
4733 t = peek(t); | |
4734 *pt = t; | |
4735 return true; | |
4736 } | |
4737 | |
4738 bool isExpression(Token** pt) | |
4739 { | |
4740 // This is supposed to determine if something is an expression. | |
4741 // What it actually does is scan until a closing right bracket | |
4742 // is found. | |
4743 | |
4744 Token* t = *pt; | |
4745 int brnest = 0; | |
4746 int panest = 0; | |
4747 int curlynest = 0; | |
4748 | |
4749 for (;; t = peek(t)) | |
4750 { | |
4751 switch (t.value) | |
4752 { | |
4753 case TOKlbracket: | |
4754 brnest++; | |
4755 continue; | |
4756 | |
4757 case TOKrbracket: | |
4758 if (--brnest >= 0) | |
4759 continue; | |
4760 break; | |
4761 | |
4762 case TOKlparen: | |
4763 panest++; | |
4764 continue; | |
4765 | |
4766 case TOKcomma: | |
4767 if (brnest || panest) | |
4768 continue; | |
4769 break; | |
4770 | |
4771 case TOKrparen: | |
4772 if (--panest >= 0) | |
4773 continue; | |
4774 break; | |
4775 | |
4776 case TOKlcurly: | |
4777 curlynest++; | |
4778 continue; | |
4779 | |
4780 case TOKrcurly: | |
4781 if (--curlynest >= 0) | |
4782 continue; | |
4783 return false; | |
4784 | |
4785 case TOKslice: | |
4786 if (brnest) | |
4787 continue; | |
4788 break; | |
4789 | |
4790 case TOKsemicolon: | |
4791 if (curlynest) | |
4792 continue; | |
4793 return false; | |
4794 | |
4795 case TOKeof: | |
4796 return false; | |
4797 | |
4798 default: | |
4799 continue; | |
4800 } | |
4801 break; | |
4802 } | |
4803 | |
4804 *pt = t; | |
4805 return true; | |
4806 } | |
4807 | |
4808 int isTemplateInstance(Token t, Token* pt) | |
4809 { | |
4810 assert(false); | |
4811 } | |
4812 | |
4813 /******************************************* | |
4814 * Skip parens, brackets. | |
4815 * Input: | |
4816 * t is on opening ( | |
4817 * Output: | |
4818 * *pt is set to closing token, which is ')' on success | |
4819 * Returns: | |
4820 * !=0 successful | |
4821 * 0 some parsing error | |
4822 */ | |
4823 bool skipParens(Token* t, Token** pt) | |
4824 { | |
4825 int parens = 0; | |
4826 | |
4827 while (1) | |
4828 { | |
4829 switch (t.value) | |
4830 { | |
4831 case TOKlparen: | |
4832 parens++; | |
4833 break; | |
4834 | |
4835 case TOKrparen: | |
4836 parens--; | |
4837 if (parens < 0) | |
4838 goto Lfalse; | |
4839 if (parens == 0) | |
4840 goto Ldone; | |
4841 break; | |
4842 | |
4843 case TOKeof: | |
4844 case TOKsemicolon: | |
4845 goto Lfalse; | |
4846 | |
4847 default: | |
4848 break; | |
4849 } | |
4850 t = peek(t); | |
4851 } | |
4852 | |
4853 Ldone: | |
4854 if (*pt) | |
4855 *pt = t; | |
4856 return true; | |
4857 | |
4858 Lfalse: | |
4859 return false; | |
4860 } | |
4861 | |
4862 Expression parseExpression() | |
4863 { | |
4864 Expression e; | |
4865 Expression e2; | |
4866 Loc loc = this.loc; | |
4867 | |
4868 //printf("Parser.parseExpression() loc = %d\n", loc.linnum); | |
4869 e = parseAssignExp(); | |
4870 while (token.value == TOK.TOKcomma) | |
4871 { | |
4872 nextToken(); | |
4873 e2 = parseAssignExp(); | |
4874 e = new CommaExp(loc, e, e2); | |
4875 loc = this.loc; | |
4876 } | |
4877 return e; | |
4878 } | |
4879 | |
4880 Expression parsePrimaryExp() | |
4881 { | |
4882 Expression e; | |
4883 Type t; | |
4884 Identifier id; | |
4885 TOK save; | |
4886 Loc loc = this.loc; | |
4887 | |
4888 //printf("parsePrimaryExp(): loc = %d\n", loc.linnum); | |
4889 switch (token.value) | |
4890 { | |
4891 case TOK.TOKidentifier: | |
4892 id = token.ident; | |
4893 nextToken(); | |
4894 if (token.value == TOK.TOKnot && peekNext() != TOK.TOKis) | |
4895 { // identifier!(template-argument-list) | |
4896 TemplateInstance tempinst; | |
4897 | |
4898 tempinst = new TemplateInstance(loc, id); | |
4899 nextToken(); | |
4900 if (token.value == TOK.TOKlparen) | |
4901 // ident!(template_arguments) | |
4902 tempinst.tiargs = parseTemplateArgumentList(); | |
4903 else | |
4904 // ident!template_argument | |
4905 tempinst.tiargs = parseTemplateArgument(); | |
4906 e = new ScopeExp(loc, tempinst); | |
4907 } | |
4908 else | |
4909 e = new IdentifierExp(loc, id); | |
4910 break; | |
4911 | |
4912 case TOK.TOKdollar: | |
4913 if (!inBrackets) | |
4914 error("'$' is valid only inside [] of index or slice"); | |
4915 e = new DollarExp(loc); | |
4916 nextToken(); | |
4917 break; | |
4918 | |
4919 case TOK.TOKdot: | |
4920 // Signal global scope '.' operator with "" identifier | |
4921 e = new IdentifierExp(loc, Id.empty); | |
4922 break; | |
4923 | |
4924 case TOK.TOKthis: | |
4925 e = new ThisExp(loc); | |
4926 nextToken(); | |
4927 break; | |
4928 | |
4929 case TOK.TOKsuper: | |
4930 e = new SuperExp(loc); | |
4931 nextToken(); | |
4932 break; | |
4933 | |
4934 case TOK.TOKint32v: | |
4935 e = new IntegerExp(loc, token.int32value, Type.tint32); | |
4936 nextToken(); | |
4937 break; | |
4938 | |
4939 case TOK.TOKuns32v: | |
4940 e = new IntegerExp(loc, token.uns32value, Type.tuns32); | |
4941 nextToken(); | |
4942 break; | |
4943 | |
4944 case TOK.TOKint64v: | |
4945 e = new IntegerExp(loc, token.int64value, Type.tint64); | |
4946 nextToken(); | |
4947 break; | |
4948 | |
4949 case TOK.TOKuns64v: | |
4950 e = new IntegerExp(loc, token.uns64value, Type.tuns64); | |
4951 nextToken(); | |
4952 break; | |
4953 | |
4954 case TOK.TOKfloat32v: | |
4955 e = new RealExp(loc, token.float80value, Type.tfloat32); | |
4956 nextToken(); | |
4957 break; | |
4958 | |
4959 case TOK.TOKfloat64v: | |
4960 e = new RealExp(loc, token.float80value, Type.tfloat64); | |
4961 nextToken(); | |
4962 break; | |
4963 | |
4964 case TOK.TOKfloat80v: | |
4965 e = new RealExp(loc, token.float80value, Type.tfloat80); | |
4966 nextToken(); | |
4967 break; | |
4968 | |
4969 case TOK.TOKimaginary32v: | |
4970 e = new RealExp(loc, token.float80value, Type.timaginary32); | |
4971 nextToken(); | |
4972 break; | |
4973 | |
4974 case TOK.TOKimaginary64v: | |
4975 e = new RealExp(loc, token.float80value, Type.timaginary64); | |
4976 nextToken(); | |
4977 break; | |
4978 | |
4979 case TOK.TOKimaginary80v: | |
4980 e = new RealExp(loc, token.float80value, Type.timaginary80); | |
4981 nextToken(); | |
4982 break; | |
4983 | |
4984 case TOK.TOKnull: | |
4985 e = new NullExp(loc); | |
4986 nextToken(); | |
4987 break; | |
4988 | |
4989 version (DMDV2) { | |
4990 case TOK.TOKfile: | |
4991 { | |
4992 string s = loc.filename ? loc.filename : mod.ident.toChars(); | |
4993 e = new StringExp(loc, s, 0); | |
4994 nextToken(); | |
4995 break; | |
4996 } | |
4997 | |
4998 case TOK.TOKline: | |
4999 e = new IntegerExp(loc, loc.linnum, Type.tint32); | |
5000 nextToken(); | |
5001 break; | |
5002 } | |
5003 | |
5004 case TOK.TOKtrue: | |
5005 e = new IntegerExp(loc, 1, Type.tbool); | |
5006 nextToken(); | |
5007 break; | |
5008 | |
5009 case TOK.TOKfalse: | |
5010 e = new IntegerExp(loc, 0, Type.tbool); | |
5011 nextToken(); | |
5012 break; | |
5013 | |
5014 case TOK.TOKcharv: | |
5015 e = new IntegerExp(loc, token.uns32value, Type.tchar); | |
5016 nextToken(); | |
5017 break; | |
5018 | |
5019 case TOK.TOKwcharv: | |
5020 e = new IntegerExp(loc, token.uns32value, Type.twchar); | |
5021 nextToken(); | |
5022 break; | |
5023 | |
5024 case TOK.TOKdcharv: | |
5025 e = new IntegerExp(loc, token.uns32value, Type.tdchar); | |
5026 nextToken(); | |
5027 break; | |
5028 | |
5029 case TOK.TOKstring: | |
5030 { | |
5031 const(char)* s; | |
5032 uint len; | |
5033 ubyte postfix; | |
5034 | |
5035 // cat adjacent strings | |
5036 s = token.ustring; | |
5037 len = token.len; | |
5038 postfix = token.postfix; | |
5039 while (1) | |
5040 { | |
5041 nextToken(); | |
5042 if (token.value == TOK.TOKstring) | |
5043 { uint len1; | |
5044 uint len2; | |
5045 char* s2; | |
5046 | |
5047 if (token.postfix) | |
5048 { if (token.postfix != postfix) | |
5049 error("mismatched string literal postfixes '%c' and '%c'", postfix, token.postfix); | |
5050 postfix = token.postfix; | |
5051 } | |
5052 | |
5053 len1 = len; | |
5054 len2 = token.len; | |
5055 len = len1 + len2; | |
2 | 5056 s2 = cast(char*)GC.malloc((len + 1) * ubyte.sizeof); |
0 | 5057 memcpy(s2, s, len1 * ubyte.sizeof); |
5058 memcpy(s2 + len1, token.ustring, (len2 + 1) * ubyte.sizeof); | |
5059 s = s2; | |
5060 } | |
5061 else | |
5062 break; | |
5063 } | |
5064 e = new StringExp(loc, assumeUnique(s[0..len]), postfix); | |
5065 break; | |
5066 } | |
5067 | |
5068 case TOK.TOKvoid: t = Type.tvoid; goto LabelX; | |
5069 case TOK.TOKint8: t = Type.tint8; goto LabelX; | |
5070 case TOK.TOKuns8: t = Type.tuns8; goto LabelX; | |
5071 case TOK.TOKint16: t = Type.tint16; goto LabelX; | |
5072 case TOK.TOKuns16: t = Type.tuns16; goto LabelX; | |
5073 case TOK.TOKint32: t = Type.tint32; goto LabelX; | |
5074 case TOK.TOKuns32: t = Type.tuns32; goto LabelX; | |
5075 case TOK.TOKint64: t = Type.tint64; goto LabelX; | |
5076 case TOK.TOKuns64: t = Type.tuns64; goto LabelX; | |
5077 case TOK.TOKfloat32: t = Type.tfloat32; goto LabelX; | |
5078 case TOK.TOKfloat64: t = Type.tfloat64; goto LabelX; | |
5079 case TOK.TOKfloat80: t = Type.tfloat80; goto LabelX; | |
5080 case TOK.TOKimaginary32: t = Type.timaginary32; goto LabelX; | |
5081 case TOK.TOKimaginary64: t = Type.timaginary64; goto LabelX; | |
5082 case TOK.TOKimaginary80: t = Type.timaginary80; goto LabelX; | |
5083 case TOK.TOKcomplex32: t = Type.tcomplex32; goto LabelX; | |
5084 case TOK.TOKcomplex64: t = Type.tcomplex64; goto LabelX; | |
5085 case TOK.TOKcomplex80: t = Type.tcomplex80; goto LabelX; | |
5086 case TOK.TOKbit: t = Type.tbit; goto LabelX; | |
5087 case TOK.TOKbool: t = Type.tbool; goto LabelX; | |
5088 case TOK.TOKchar: t = Type.tchar; goto LabelX; | |
5089 case TOK.TOKwchar: t = Type.twchar; goto LabelX; | |
5090 case TOK.TOKdchar: t = Type.tdchar; goto LabelX; | |
5091 LabelX: | |
5092 nextToken(); | |
5093 L1: | |
5094 check(TOK.TOKdot, t.toChars()); | |
5095 if (token.value != TOK.TOKidentifier) | |
5096 { error("found '%s' when expecting identifier following '%s.'", token.toChars(), t.toChars()); | |
5097 goto Lerr; | |
5098 } | |
5099 e = typeDotIdExp(loc, t, token.ident); | |
5100 nextToken(); | |
5101 break; | |
5102 | |
5103 case TOK.TOKtypeof: | |
5104 { | |
5105 t = parseTypeof(); | |
5106 e = new TypeExp(loc, t); | |
5107 break; | |
5108 } | |
5109 | |
5110 case TOK.TOKtypeid: | |
5111 { Type tt; | |
5112 | |
5113 nextToken(); | |
5114 check(TOK.TOKlparen, "typeid"); | |
5115 tt = parseType(); // ( type ) | |
5116 check(TOK.TOKrparen); | |
5117 e = new TypeidExp(loc, tt); | |
5118 break; | |
5119 } | |
5120 | |
5121 version (DMDV2) { | |
5122 case TOK.TOKtraits: | |
5123 { /* __traits(identifier, args...) | |
5124 */ | |
5125 Identifier ident; | |
5126 Objects args = null; | |
5127 | |
5128 nextToken(); | |
5129 check(TOK.TOKlparen); | |
5130 if (token.value != TOK.TOKidentifier) | |
5131 { error("__traits(identifier, args...) expected"); | |
5132 goto Lerr; | |
5133 } | |
5134 ident = token.ident; | |
5135 nextToken(); | |
5136 if (token.value == TOK.TOKcomma) | |
5137 args = parseTemplateArgumentList2(); // __traits(identifier, args...) | |
5138 else | |
5139 check(TOK.TOKrparen); // __traits(identifier) | |
5140 | |
5141 e = new TraitsExp(loc, ident, args); | |
5142 break; | |
5143 } | |
5144 } | |
5145 | |
5146 case TOK.TOKis: | |
5147 { Type targ; | |
5148 Identifier ident = null; | |
5149 Type tspec = null; | |
5150 TOK tok = TOK.TOKreserved; | |
5151 TOK tok2 = TOK.TOKreserved; | |
5152 TemplateParameters tpl = null; | |
5153 Loc loc2 = this.loc; | |
5154 | |
5155 nextToken(); | |
5156 if (token.value == TOK.TOKlparen) | |
5157 { | |
5158 nextToken(); | |
5159 targ = parseType(&ident); | |
5160 if (token.value == TOK.TOKcolon || token.value == TOK.TOKequal) | |
5161 { | |
5162 tok = token.value; | |
5163 nextToken(); | |
5164 if (tok == TOK.TOKequal && | |
5165 (token.value == TOK.TOKtypedef || | |
5166 token.value == TOK.TOKstruct || | |
5167 token.value == TOK.TOKunion || | |
5168 token.value == TOK.TOKclass || | |
5169 token.value == TOK.TOKsuper || | |
5170 token.value == TOK.TOKenum || | |
5171 token.value == TOK.TOKinterface || | |
5172 ///version (DMDV2) { | |
5173 token.value == TOK.TOKconst && peek(&token).value == TOK.TOKrparen || | |
5174 token.value == TOK.TOKinvariant && peek(&token).value == TOK.TOKrparen || | |
5175 token.value == TOK.TOKimmutable && peek(&token).value == TOK.TOKrparen || | |
5176 token.value == TOK.TOKshared && peek(&token).value == TOK.TOKrparen || | |
5177 ///} | |
5178 token.value == TOK.TOKfunction || | |
5179 token.value == TOK.TOKdelegate || | |
5180 token.value == TOK.TOKreturn)) | |
5181 { | |
5182 tok2 = token.value; | |
5183 nextToken(); | |
5184 } | |
5185 else | |
5186 { | |
5187 tspec = parseType(); | |
5188 } | |
5189 } | |
5190 if (ident && tspec) | |
5191 { | |
5192 if (token.value == TOK.TOKcomma) | |
5193 tpl = parseTemplateParameterList(1); | |
5194 else | |
5195 { tpl = new TemplateParameters(); | |
5196 check(TOK.TOKrparen); | |
5197 } | |
5198 TemplateParameter tp = new TemplateTypeParameter(loc2, ident, null, null); | |
121
347de076ad34
TemplateParameters -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
5199 tpl.insert(0, tp); |
0 | 5200 } |
5201 else | |
5202 check(TOK.TOKrparen); | |
5203 } | |
5204 else | |
5205 { error("(type identifier : specialization) expected following is"); | |
5206 goto Lerr; | |
5207 } | |
5208 e = new IsExp(loc2, targ, ident, tok, tspec, tok2, tpl); | |
5209 break; | |
5210 } | |
5211 | |
5212 case TOK.TOKassert: | |
5213 { | |
5214 Expression msg = null; | |
5215 | |
5216 nextToken(); | |
5217 check(TOK.TOKlparen, "assert"); | |
5218 e = parseAssignExp(); | |
5219 if (token.value == TOK.TOKcomma) | |
5220 { nextToken(); | |
5221 msg = parseAssignExp(); | |
5222 } | |
5223 check(TOK.TOKrparen); | |
5224 e = new AssertExp(loc, e, msg); | |
5225 break; | |
5226 } | |
5227 | |
5228 case TOK.TOKmixin: | |
5229 { | |
5230 nextToken(); | |
5231 check(TOK.TOKlparen, "mixin"); | |
5232 e = parseAssignExp(); | |
5233 check(TOK.TOKrparen); | |
5234 e = new CompileExp(loc, e); | |
5235 break; | |
5236 } | |
5237 | |
5238 case TOK.TOKimport: | |
5239 { | |
5240 nextToken(); | |
5241 check(TOK.TOKlparen, "import"); | |
5242 e = parseAssignExp(); | |
5243 check(TOK.TOKrparen); | |
5244 e = new FileExp(loc, e); | |
5245 break; | |
5246 } | |
5247 | |
5248 case TOK.TOKlparen: | |
5249 if (peekPastParen(&token).value == TOK.TOKlcurly) | |
5250 { // (arguments) { statements... } | |
5251 save = TOK.TOKdelegate; | |
5252 goto case_delegate; | |
5253 } | |
5254 // ( expression ) | |
5255 nextToken(); | |
5256 e = parseExpression(); | |
5257 check(loc, TOK.TOKrparen); | |
5258 break; | |
5259 | |
5260 case TOK.TOKlbracket: | |
5261 { /* Parse array literals and associative array literals: | |
5262 * [ value, value, value ... ] | |
5263 * [ key:value, key:value, key:value ... ] | |
5264 */ | |
5265 Expressions values = new Expressions(); | |
5266 Expressions keys = null; | |
5267 | |
5268 nextToken(); | |
5269 if (token.value != TOK.TOKrbracket) | |
5270 { | |
5271 while (token.value != TOK.TOKeof) | |
5272 { | |
5273 Expression e2 = parseAssignExp(); | |
5274 if (token.value == TOK.TOKcolon && (keys || values.dim == 0)) | |
5275 { | |
5276 nextToken(); | |
5277 if (!keys) | |
5278 keys = new Expressions(); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
5279 keys.push(e2); |
0 | 5280 e2 = parseAssignExp(); |
5281 } | |
5282 else if (keys) | |
5283 { | |
5284 error("'key:value' expected for associative array literal"); | |
5285 delete keys; | |
5286 keys = null; | |
5287 } | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
5288 values.push(e2); |
0 | 5289 if (token.value == TOK.TOKrbracket) |
5290 break; | |
5291 check(TOK.TOKcomma); | |
5292 } | |
5293 } | |
5294 check(TOK.TOKrbracket); | |
5295 | |
5296 if (keys) | |
5297 e = new AssocArrayLiteralExp(loc, keys, values); | |
5298 else | |
5299 e = new ArrayLiteralExp(loc, values); | |
5300 break; | |
5301 } | |
5302 | |
5303 case TOK.TOKlcurly: | |
5304 // { statements... } | |
5305 save = TOK.TOKdelegate; | |
5306 goto case_delegate; | |
5307 | |
5308 case TOK.TOKfunction: | |
5309 case TOK.TOKdelegate: | |
5310 save = token.value; | |
5311 nextToken(); | |
5312 case_delegate: | |
5313 { | |
5314 /* function type(parameters) { body } pure nothrow | |
5315 * delegate type(parameters) { body } pure nothrow | |
5316 * (parameters) { body } | |
5317 * { body } | |
5318 */ | |
5319 Arguments arguments; | |
5320 int varargs; | |
5321 FuncLiteralDeclaration fd; | |
5322 Type tt; | |
5323 bool isnothrow = false; | |
5324 bool ispure = false; | |
5325 | |
5326 if (token.value == TOK.TOKlcurly) | |
5327 { | |
5328 tt = null; | |
5329 varargs = 0; | |
5330 arguments = new Arguments(); | |
5331 } | |
5332 else | |
5333 { | |
5334 if (token.value == TOK.TOKlparen) | |
5335 tt = null; | |
5336 else | |
5337 { | |
5338 tt = parseBasicType(); | |
5339 tt = parseBasicType2(tt); // function return type | |
5340 } | |
5341 arguments = parseParameters(&varargs); | |
5342 while (1) | |
5343 { | |
5344 if (token.value == TOK.TOKpure) | |
5345 ispure = true; | |
5346 else if (token.value == TOK.TOKnothrow) | |
5347 isnothrow = true; | |
5348 else | |
5349 break; | |
5350 nextToken(); | |
5351 } | |
5352 } | |
5353 | |
5354 TypeFunction tf = new TypeFunction(arguments, tt, varargs, linkage); | |
5355 tf.ispure = ispure; | |
5356 tf.isnothrow = isnothrow; | |
5357 fd = new FuncLiteralDeclaration(loc, Loc(0), tf, save, null); | |
5358 parseContracts(fd); | |
5359 e = new FuncExp(loc, fd); | |
5360 break; | |
5361 } | |
5362 | |
5363 default: | |
5364 error("expression expected, not '%s'", token.toChars()); | |
5365 Lerr: | |
5366 // Anything for e, as long as it's not null | |
5367 e = new IntegerExp(loc, 0, Type.tint32); | |
5368 nextToken(); | |
5369 break; | |
5370 } | |
5371 return e; | |
5372 } | |
5373 | |
5374 Expression parseUnaryExp() | |
5375 { | |
5376 Expression e; | |
5377 Loc loc = this.loc; | |
5378 | |
5379 switch (token.value) | |
5380 { | |
5381 case TOK.TOKand: | |
5382 nextToken(); | |
5383 e = parseUnaryExp(); | |
5384 e = new AddrExp(loc, e); | |
5385 break; | |
5386 | |
5387 case TOK.TOKplusplus: | |
5388 nextToken(); | |
5389 e = parseUnaryExp(); | |
5390 e = new AddAssignExp(loc, e, new IntegerExp(loc, 1, Type.tint32)); | |
5391 break; | |
5392 | |
5393 case TOK.TOKminusminus: | |
5394 nextToken(); | |
5395 e = parseUnaryExp(); | |
5396 e = new MinAssignExp(loc, e, new IntegerExp(loc, 1, Type.tint32)); | |
5397 break; | |
5398 | |
5399 case TOK.TOKmul: | |
5400 nextToken(); | |
5401 e = parseUnaryExp(); | |
5402 e = new PtrExp(loc, e); | |
5403 break; | |
5404 | |
5405 case TOK.TOKmin: | |
5406 nextToken(); | |
5407 e = parseUnaryExp(); | |
5408 e = new NegExp(loc, e); | |
5409 break; | |
5410 | |
5411 case TOK.TOKadd: | |
5412 nextToken(); | |
5413 e = parseUnaryExp(); | |
5414 e = new UAddExp(loc, e); | |
5415 break; | |
5416 | |
5417 case TOK.TOKnot: | |
5418 nextToken(); | |
5419 e = parseUnaryExp(); | |
5420 e = new NotExp(loc, e); | |
5421 break; | |
5422 | |
5423 case TOK.TOKtilde: | |
5424 nextToken(); | |
5425 e = parseUnaryExp(); | |
5426 e = new ComExp(loc, e); | |
5427 break; | |
5428 | |
5429 case TOK.TOKdelete: | |
5430 nextToken(); | |
5431 e = parseUnaryExp(); | |
5432 e = new DeleteExp(loc, e); | |
5433 break; | |
5434 | |
5435 case TOK.TOKnew: | |
5436 e = parseNewExp(null); | |
5437 break; | |
5438 | |
5439 case TOK.TOKcast: // cast(type) expression | |
5440 { | |
5441 nextToken(); | |
5442 check(TOK.TOKlparen); | |
5443 /* Look for cast(), cast(const), cast(immutable), | |
5444 * cast(shared), cast(shared const) | |
5445 */ | |
5446 MOD m; | |
5447 if (token.value == TOK.TOKrparen) | |
5448 { | |
5449 m = MOD.MODundefined; | |
5450 goto Lmod1; | |
5451 } | |
5452 else if (token.value == TOK.TOKconst && peekNext() == TOK.TOKrparen) | |
5453 { | |
5454 m = MOD.MODconst; | |
5455 goto Lmod2; | |
5456 } | |
5457 else if ((token.value == TOK.TOKimmutable || token.value == TOK.TOKinvariant) && peekNext() == TOK.TOKrparen) | |
5458 { | |
5459 m = MOD.MODinvariant; | |
5460 goto Lmod2; | |
5461 } | |
5462 else if (token.value == TOK.TOKshared && peekNext() == TOK.TOKrparen) | |
5463 { | |
5464 m = MOD.MODshared; | |
5465 goto Lmod2; | |
5466 } | |
5467 else if (token.value == TOK.TOKconst && peekNext() == TOK.TOKshared && peekNext2() == TOK.TOKrparen || | |
5468 token.value == TOK.TOKshared && peekNext() == TOK.TOKconst && peekNext2() == TOK.TOKrparen) | |
5469 { | |
5470 m = MOD.MODshared | MOD.MODconst; | |
5471 nextToken(); | |
5472 Lmod2: | |
5473 nextToken(); | |
5474 Lmod1: | |
5475 nextToken(); | |
5476 e = parseUnaryExp(); | |
5477 e = new CastExp(loc, e, m); | |
5478 } | |
5479 else | |
5480 { | |
5481 Type t = parseType(); // ( type ) | |
5482 check(TOK.TOKrparen); | |
5483 e = parseUnaryExp(); | |
5484 e = new CastExp(loc, e, t); | |
5485 } | |
5486 break; | |
5487 } | |
5488 | |
5489 case TOK.TOKlparen: | |
5490 { Token *tk; | |
5491 | |
5492 tk = peek(&token); | |
5493 version (CCASTSYNTAX) { | |
5494 // If cast | |
5495 if (isDeclaration(tk, 0, TOK.TOKrparen, &tk)) | |
5496 { | |
5497 tk = peek(tk); // skip over right parenthesis | |
5498 switch (tk.value) | |
5499 { | |
5500 case TOK.TOKnot: | |
5501 tk = peek(tk); | |
5502 if (tk.value == TOK.TOKis) // !is | |
5503 break; | |
5504 case TOK.TOKdot: | |
5505 case TOK.TOKplusplus: | |
5506 case TOK.TOKminusminus: | |
5507 case TOK.TOKdelete: | |
5508 case TOK.TOKnew: | |
5509 case TOK.TOKlparen: | |
5510 case TOK.TOKidentifier: | |
5511 case TOK.TOKthis: | |
5512 case TOK.TOKsuper: | |
5513 case TOK.TOKint32v: | |
5514 case TOK.TOKuns32v: | |
5515 case TOK.TOKint64v: | |
5516 case TOK.TOKuns64v: | |
5517 case TOK.TOKfloat32v: | |
5518 case TOK.TOKfloat64v: | |
5519 case TOK.TOKfloat80v: | |
5520 case TOK.TOKimaginary32v: | |
5521 case TOK.TOKimaginary64v: | |
5522 case TOK.TOKimaginary80v: | |
5523 case TOK.TOKnull: | |
5524 case TOK.TOKtrue: | |
5525 case TOK.TOKfalse: | |
5526 case TOK.TOKcharv: | |
5527 case TOK.TOKwcharv: | |
5528 case TOK.TOKdcharv: | |
5529 case TOK.TOKstring: | |
5530 static if (false) { | |
5531 case TOK.TOKtilde: | |
5532 case TOK.TOKand: | |
5533 case TOK.TOKmul: | |
5534 case TOK.TOKmin: | |
5535 case TOK.TOKadd: | |
5536 } | |
5537 case TOK.TOKfunction: | |
5538 case TOK.TOKdelegate: | |
5539 case TOK.TOKtypeof: | |
5540 version (DMDV2) { | |
5541 case TOK.TOKfile: | |
5542 case TOK.TOKline: | |
5543 } | |
5544 case TOK.TOKwchar: case TOK.TOKdchar: | |
5545 case TOK.TOKbit: case TOK.TOKbool: case TOK.TOKchar: | |
5546 case TOK.TOKint8: case TOK.TOKuns8: | |
5547 case TOK.TOKint16: case TOK.TOKuns16: | |
5548 case TOK.TOKint32: case TOK.TOKuns32: | |
5549 case TOK.TOKint64: case TOK.TOKuns64: | |
5550 case TOK.TOKfloat32: case TOK.TOKfloat64: case TOK.TOKfloat80: | |
5551 case TOK.TOKimaginary32: case TOK.TOKimaginary64: case TOK.TOKimaginary80: | |
5552 case TOK.TOKcomplex32: case TOK.TOKcomplex64: case TOK.TOKcomplex80: | |
5553 case TOK.TOKvoid: // (type)int.size | |
5554 { | |
5555 // (type) una_exp | |
5556 nextToken(); | |
5557 Type t = parseType(); | |
5558 check(TOK.TOKrparen); | |
5559 | |
5560 // if .identifier | |
5561 if (token.value == TOK.TOKdot) | |
5562 { | |
5563 nextToken(); | |
5564 if (token.value != TOK.TOKidentifier) | |
5565 { | |
5566 error("Identifier expected following (type)."); | |
5567 return null; | |
5568 } | |
5569 e = typeDotIdExp(loc, t, token.ident); | |
5570 nextToken(); | |
5571 e = parsePostExp(e); | |
5572 } | |
5573 else | |
5574 { | |
5575 e = parseUnaryExp(); | |
5576 e = new CastExp(loc, e, t); | |
5577 error("C style cast illegal, use %s", e.toChars()); | |
5578 } | |
5579 return e; | |
5580 } | |
5581 | |
5582 default: | |
5583 break; /// | |
5584 } | |
5585 } | |
5586 } | |
5587 e = parsePrimaryExp(); | |
5588 e = parsePostExp(e); | |
5589 break; | |
5590 } | |
5591 default: | |
5592 e = parsePrimaryExp(); | |
5593 e = parsePostExp(e); | |
5594 break; | |
5595 } | |
5596 assert(e); | |
5597 return e; | |
5598 } | |
5599 | |
5600 Expression parsePostExp(Expression e) | |
5601 { | |
5602 Loc loc; | |
5603 | |
5604 while (1) | |
5605 { | |
5606 loc = this.loc; | |
5607 switch (token.value) | |
5608 { | |
5609 case TOK.TOKdot: | |
5610 nextToken(); | |
5611 if (token.value == TOK.TOKidentifier) | |
5612 { Identifier id = token.ident; | |
5613 | |
5614 nextToken(); | |
5615 if (token.value == TOK.TOKnot && peekNext() != TOK.TOKis) | |
5616 { // identifier!(template-argument-list) | |
5617 TemplateInstance tempinst = new TemplateInstance(loc, id); | |
5618 nextToken(); | |
5619 if (token.value == TOK.TOKlparen) | |
5620 // ident!(template_arguments) | |
5621 tempinst.tiargs = parseTemplateArgumentList(); | |
5622 else | |
5623 // ident!template_argument | |
5624 tempinst.tiargs = parseTemplateArgument(); | |
5625 e = new DotTemplateInstanceExp(loc, e, tempinst); | |
5626 } | |
5627 else | |
5628 e = new DotIdExp(loc, e, id); | |
5629 continue; | |
5630 } | |
5631 else if (token.value == TOK.TOKnew) | |
5632 { | |
5633 e = parseNewExp(e); | |
5634 continue; | |
5635 } | |
5636 else | |
5637 error("identifier expected following '.', not '%s'", token.toChars()); | |
5638 break; | |
5639 | |
5640 case TOK.TOKplusplus: | |
5641 e = new PostExp(TOK.TOKplusplus, loc, e); | |
5642 break; | |
5643 | |
5644 case TOK.TOKminusminus: | |
5645 e = new PostExp(TOK.TOKminusminus, loc, e); | |
5646 break; | |
5647 | |
5648 case TOK.TOKlparen: | |
5649 e = new CallExp(loc, e, parseArguments()); | |
5650 continue; | |
5651 | |
5652 case TOK.TOKlbracket: | |
5653 { // array dereferences: | |
5654 // array[index] | |
5655 // array[] | |
5656 // array[lwr .. upr] | |
5657 Expression index; | |
5658 Expression upr; | |
5659 | |
5660 inBrackets++; | |
5661 nextToken(); | |
5662 if (token.value == TOK.TOKrbracket) | |
5663 { // array[] | |
5664 e = new SliceExp(loc, e, null, null); | |
5665 nextToken(); | |
5666 } | |
5667 else | |
5668 { | |
5669 index = parseAssignExp(); | |
5670 if (token.value == TOK.TOKslice) | |
5671 { // array[lwr .. upr] | |
5672 nextToken(); | |
5673 upr = parseAssignExp(); | |
5674 e = new SliceExp(loc, e, index, upr); | |
5675 } | |
5676 else | |
5677 { // array[index, i2, i3, i4, ...] | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
5678 auto arguments = new Expressions(); |
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
5679 arguments.push(index); |
0 | 5680 if (token.value == TOK.TOKcomma) |
5681 { | |
5682 nextToken(); | |
5683 while (1) | |
5684 { Expression arg; | |
5685 | |
5686 arg = parseAssignExp(); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
5687 arguments.push(arg); |
0 | 5688 if (token.value == TOK.TOKrbracket) |
5689 break; | |
5690 check(TOK.TOKcomma); | |
5691 } | |
5692 } | |
5693 e = new ArrayExp(loc, e, arguments); | |
5694 } | |
5695 check(TOK.TOKrbracket); | |
5696 inBrackets--; | |
5697 } | |
5698 continue; | |
5699 } | |
5700 | |
5701 default: | |
5702 return e; | |
5703 } | |
5704 nextToken(); | |
5705 } | |
5706 | |
5707 assert(false); | |
5708 } | |
5709 | |
5710 Expression parseMulExp() | |
5711 { | |
5712 Expression e; | |
5713 Expression e2; | |
5714 Loc loc = this.loc; | |
5715 | |
5716 e = parseUnaryExp(); | |
5717 while (1) | |
5718 { | |
5719 switch (token.value) | |
5720 { | |
5721 case TOK.TOKmul: nextToken(); e2 = parseUnaryExp(); e = new MulExp(loc,e,e2); continue; | |
5722 case TOK.TOKdiv: nextToken(); e2 = parseUnaryExp(); e = new DivExp(loc,e,e2); continue; | |
5723 case TOK.TOKmod: nextToken(); e2 = parseUnaryExp(); e = new ModExp(loc,e,e2); continue; | |
5724 | |
5725 default: | |
5726 break; | |
5727 } | |
5728 break; | |
5729 } | |
5730 return e; | |
5731 } | |
5732 | |
5733 Expression parseShiftExp() | |
5734 { | |
5735 Expression e; | |
5736 Expression e2; | |
5737 Loc loc = this.loc; | |
5738 | |
5739 e = parseAddExp(); | |
5740 while (1) | |
5741 { | |
5742 switch (token.value) | |
5743 { | |
5744 case TOK.TOKshl: nextToken(); e2 = parseAddExp(); e = new ShlExp(loc,e,e2); continue; | |
5745 case TOK.TOKshr: nextToken(); e2 = parseAddExp(); e = new ShrExp(loc,e,e2); continue; | |
5746 case TOK.TOKushr: nextToken(); e2 = parseAddExp(); e = new UshrExp(loc,e,e2); continue; | |
5747 | |
5748 default: | |
5749 break; | |
5750 } | |
5751 break; | |
5752 } | |
5753 return e; | |
5754 } | |
5755 | |
5756 Expression parseAddExp() | |
5757 { | |
5758 Expression e; | |
5759 Expression e2; | |
5760 Loc loc = this.loc; | |
5761 | |
5762 e = parseMulExp(); | |
5763 while (1) | |
5764 { | |
5765 switch (token.value) | |
5766 { | |
5767 case TOK.TOKadd: nextToken(); e2 = parseMulExp(); e = new AddExp(loc,e,e2); continue; | |
5768 case TOK.TOKmin: nextToken(); e2 = parseMulExp(); e = new MinExp(loc,e,e2); continue; | |
5769 case TOK.TOKtilde: nextToken(); e2 = parseMulExp(); e = new CatExp(loc,e,e2); continue; | |
5770 | |
5771 default: | |
5772 break; | |
5773 } | |
5774 break; | |
5775 } | |
5776 return e; | |
5777 } | |
5778 | |
5779 Expression parseRelExp() | |
5780 { | |
5781 assert(false); | |
5782 } | |
5783 | |
5784 Expression parseEqualExp() | |
5785 { | |
5786 assert(false); | |
5787 } | |
5788 | |
5789 Expression parseCmpExp() | |
5790 { | |
5791 Expression e; | |
5792 Expression e2; | |
5793 Token* t; | |
5794 Loc loc = this.loc; | |
5795 | |
5796 e = parseShiftExp(); | |
5797 TOK op = token.value; | |
5798 | |
5799 switch (op) | |
5800 { | |
5801 case TOK.TOKequal: | |
5802 case TOK.TOKnotequal: | |
5803 nextToken(); | |
5804 e2 = parseShiftExp(); | |
5805 e = new EqualExp(op, loc, e, e2); | |
5806 break; | |
5807 | |
5808 case TOK.TOKis: | |
5809 op = TOK.TOKidentity; | |
5810 goto L1; | |
5811 | |
5812 case TOK.TOKnot: | |
5813 // Attempt to identify '!is' | |
5814 t = peek(&token); | |
5815 if (t.value != TOK.TOKis) | |
5816 break; | |
5817 nextToken(); | |
5818 op = TOK.TOKnotidentity; | |
5819 goto L1; | |
5820 | |
5821 L1: | |
5822 nextToken(); | |
5823 e2 = parseShiftExp(); | |
5824 e = new IdentityExp(op, loc, e, e2); | |
5825 break; | |
5826 | |
5827 case TOK.TOKlt: | |
5828 case TOK.TOKle: | |
5829 case TOK.TOKgt: | |
5830 case TOK.TOKge: | |
5831 case TOK.TOKunord: | |
5832 case TOK.TOKlg: | |
5833 case TOK.TOKleg: | |
5834 case TOK.TOKule: | |
5835 case TOK.TOKul: | |
5836 case TOK.TOKuge: | |
5837 case TOK.TOKug: | |
5838 case TOK.TOKue: | |
5839 nextToken(); | |
5840 e2 = parseShiftExp(); | |
5841 e = new CmpExp(op, loc, e, e2); | |
5842 break; | |
5843 | |
5844 case TOK.TOKin: | |
5845 nextToken(); | |
5846 e2 = parseShiftExp(); | |
5847 e = new InExp(loc, e, e2); | |
5848 break; | |
5849 | |
5850 default: | |
5851 break; | |
5852 } | |
5853 return e; | |
5854 } | |
5855 | |
5856 Expression parseAndExp() | |
5857 { | |
5858 Expression e; | |
5859 Expression e2; | |
5860 Loc loc = this.loc; | |
5861 | |
5862 if (global.params.Dversion == 1) | |
5863 { | |
5864 e = parseEqualExp(); | |
5865 while (token.value == TOK.TOKand) | |
5866 { | |
5867 nextToken(); | |
5868 e2 = parseEqualExp(); | |
5869 e = new AndExp(loc,e,e2); | |
5870 loc = this.loc; | |
5871 } | |
5872 } | |
5873 else | |
5874 { | |
5875 e = parseCmpExp(); | |
5876 while (token.value == TOK.TOKand) | |
5877 { | |
5878 nextToken(); | |
5879 e2 = parseCmpExp(); | |
5880 e = new AndExp(loc,e,e2); | |
5881 loc = this.loc; | |
5882 } | |
5883 } | |
5884 return e; | |
5885 } | |
5886 | |
5887 Expression parseXorExp() | |
5888 { | |
5889 Expression e; | |
5890 Expression e2; | |
5891 Loc loc = this.loc; | |
5892 | |
5893 e = parseAndExp(); | |
5894 while (token.value == TOK.TOKxor) | |
5895 { | |
5896 nextToken(); | |
5897 e2 = parseAndExp(); | |
5898 e = new XorExp(loc, e, e2); | |
5899 } | |
5900 | |
5901 return e; | |
5902 } | |
5903 | |
5904 Expression parseOrExp() | |
5905 { | |
5906 Expression e; | |
5907 Expression e2; | |
5908 Loc loc = this.loc; | |
5909 | |
5910 e = parseXorExp(); | |
5911 while (token.value == TOK.TOKor) | |
5912 { | |
5913 nextToken(); | |
5914 e2 = parseXorExp(); | |
5915 e = new OrExp(loc, e, e2); | |
5916 } | |
5917 return e; | |
5918 } | |
5919 | |
5920 Expression parseAndAndExp() | |
5921 { | |
5922 Expression e; | |
5923 Expression e2; | |
5924 Loc loc = this.loc; | |
5925 | |
5926 e = parseOrExp(); | |
5927 while (token.value == TOK.TOKandand) | |
5928 { | |
5929 nextToken(); | |
5930 e2 = parseOrExp(); | |
5931 e = new AndAndExp(loc, e, e2); | |
5932 } | |
5933 return e; | |
5934 } | |
5935 | |
5936 Expression parseOrOrExp() | |
5937 { | |
5938 Expression e; | |
5939 Expression e2; | |
5940 Loc loc = this.loc; | |
5941 | |
5942 e = parseAndAndExp(); | |
5943 while (token.value == TOK.TOKoror) | |
5944 { | |
5945 nextToken(); | |
5946 e2 = parseAndAndExp(); | |
5947 e = new OrOrExp(loc, e, e2); | |
5948 } | |
5949 | |
5950 return e; | |
5951 } | |
5952 | |
5953 Expression parseCondExp() | |
5954 { | |
5955 Expression e; | |
5956 Expression e1; | |
5957 Expression e2; | |
5958 Loc loc = this.loc; | |
5959 | |
5960 e = parseOrOrExp(); | |
5961 if (token.value == TOK.TOKquestion) | |
5962 { | |
5963 nextToken(); | |
5964 e1 = parseExpression(); | |
5965 check(TOK.TOKcolon); | |
5966 e2 = parseCondExp(); | |
5967 e = new CondExp(loc, e, e1, e2); | |
5968 } | |
5969 return e; | |
5970 } | |
5971 | |
5972 Expression parseAssignExp() | |
5973 { | |
5974 Expression e; | |
5975 Expression e2; | |
5976 Loc loc; | |
5977 | |
5978 e = parseCondExp(); | |
5979 while (1) | |
5980 { | |
5981 loc = this.loc; | |
5982 switch (token.value) | |
5983 { | |
5984 case TOK.TOKassign: nextToken(); e2 = parseAssignExp(); e = new AssignExp(loc,e,e2); continue; | |
5985 case TOK.TOKaddass: nextToken(); e2 = parseAssignExp(); e = new AddAssignExp(loc,e,e2); continue; | |
5986 case TOK.TOKminass: nextToken(); e2 = parseAssignExp(); e = new MinAssignExp(loc,e,e2); continue; | |
5987 case TOK.TOKmulass: nextToken(); e2 = parseAssignExp(); e = new MulAssignExp(loc,e,e2); continue; | |
5988 case TOK.TOKdivass: nextToken(); e2 = parseAssignExp(); e = new DivAssignExp(loc,e,e2); continue; | |
5989 case TOK.TOKmodass: nextToken(); e2 = parseAssignExp(); e = new ModAssignExp(loc,e,e2); continue; | |
5990 case TOK.TOKandass: nextToken(); e2 = parseAssignExp(); e = new AndAssignExp(loc,e,e2); continue; | |
5991 case TOK.TOKorass: nextToken(); e2 = parseAssignExp(); e = new OrAssignExp(loc,e,e2); continue; | |
5992 case TOK.TOKxorass: nextToken(); e2 = parseAssignExp(); e = new XorAssignExp(loc,e,e2); continue; | |
5993 case TOK.TOKshlass: nextToken(); e2 = parseAssignExp(); e = new ShlAssignExp(loc,e,e2); continue; | |
5994 case TOK.TOKshrass: nextToken(); e2 = parseAssignExp(); e = new ShrAssignExp(loc,e,e2); continue; | |
5995 case TOK.TOKushrass: nextToken(); e2 = parseAssignExp(); e = new UshrAssignExp(loc,e,e2); continue; | |
5996 case TOK.TOKcatass: nextToken(); e2 = parseAssignExp(); e = new CatAssignExp(loc,e,e2); continue; | |
5997 | |
5998 default: | |
5999 break; | |
6000 } | |
6001 break; | |
6002 } | |
6003 | |
6004 return e; | |
6005 } | |
6006 | |
6007 /************************* | |
6008 * Collect argument list. | |
6009 * Assume current token is ',', '(' or '['. | |
6010 */ | |
6011 Expressions parseArguments() | |
6012 { | |
6013 // function call | |
6014 Expressions arguments = new Expressions(); | |
6015 Expression arg; | |
6016 TOK endtok; | |
6017 | |
6018 if (token.value == TOK.TOKlbracket) | |
6019 endtok = TOK.TOKrbracket; | |
6020 else | |
6021 endtok = TOK.TOKrparen; | |
6022 | |
6023 { | |
6024 nextToken(); | |
6025 if (token.value != endtok) | |
6026 { | |
6027 while (1) | |
6028 { | |
6029 arg = parseAssignExp(); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
6030 arguments.push(arg); |
0 | 6031 if (token.value == endtok) |
6032 break; | |
6033 check(TOK.TOKcomma); | |
6034 } | |
6035 } | |
6036 check(endtok); | |
6037 } | |
6038 return arguments; | |
6039 } | |
6040 | |
6041 Expression parseNewExp(Expression thisexp) | |
6042 { | |
6043 Type t; | |
6044 Expressions newargs; | |
6045 Expressions arguments = null; | |
6046 Expression e; | |
6047 Loc loc = this.loc; | |
6048 | |
6049 nextToken(); | |
6050 newargs = null; | |
6051 if (token.value == TOKlparen) | |
6052 { | |
6053 newargs = parseArguments(); | |
6054 } | |
6055 | |
6056 // An anonymous nested class starts with "class" | |
6057 if (token.value == TOKclass) | |
6058 { | |
6059 nextToken(); | |
6060 if (token.value == TOKlparen) | |
6061 arguments = parseArguments(); | |
6062 | |
6063 BaseClasses baseclasses = null; | |
6064 if (token.value != TOKlcurly) | |
6065 baseclasses = parseBaseClasses(); | |
6066 | |
6067 Identifier id = null; | |
6068 ClassDeclaration cd = new ClassDeclaration(loc, id, baseclasses); | |
6069 | |
6070 if (token.value != TOKlcurly) | |
6071 { | |
6072 error("{ members } expected for anonymous class"); | |
6073 cd.members = null; | |
6074 } | |
6075 else | |
6076 { | |
6077 nextToken(); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
6078 auto decl = parseDeclDefs(0); |
0 | 6079 if (token.value != TOKrcurly) |
6080 error("class member expected"); | |
6081 nextToken(); | |
6082 cd.members = decl; | |
6083 } | |
6084 | |
6085 e = new NewAnonClassExp(loc, thisexp, newargs, cd, arguments); | |
6086 | |
6087 return e; | |
6088 } | |
6089 | |
6090 t = parseBasicType(); | |
6091 t = parseBasicType2(t); | |
6092 if (t.ty == Taarray) | |
6093 { | |
6094 TypeAArray taa = cast(TypeAArray)t; | |
6095 Type index = taa.index; | |
6096 | |
6097 Expression e2 = index.toExpression(); | |
6098 if (e2) | |
6099 { | |
6100 arguments = new Expressions(); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
6101 arguments.push(e2); |
0 | 6102 t = new TypeDArray(taa.next); |
6103 } | |
6104 else | |
6105 { | |
6106 error("need size of rightmost array, not type %s", index.toChars()); | |
6107 return new NullExp(loc); | |
6108 } | |
6109 } | |
6110 else if (t.ty == Tsarray) | |
6111 { | |
6112 TypeSArray tsa = cast(TypeSArray)t; | |
6113 Expression ee = tsa.dim; | |
6114 | |
6115 arguments = new Expressions(); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
6116 arguments.push(ee); |
0 | 6117 t = new TypeDArray(tsa.next); |
6118 } | |
6119 else if (token.value == TOKlparen) | |
6120 { | |
6121 arguments = parseArguments(); | |
6122 } | |
6123 | |
6124 e = new NewExp(loc, thisexp, newargs, t, arguments); | |
6125 return e; | |
6126 } | |
6127 | |
79 | 6128 void addComment(Dsymbol s, const(char)[] blockComment) |
0 | 6129 { |
6130 s.addComment(combineComments(blockComment, token.lineComment)); | |
6131 token.lineComment = null; | |
6132 } | |
19
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
6133 } |