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