Mercurial > projects > ddmd
comparison dmd/Parser.d @ 79:43073c7c7769
updated to 2.035
also implemented a few missing functions
still crashes in Import.importAll though
author | Trass3r |
---|---|
date | Mon, 30 Aug 2010 03:57:51 +0200 |
parents | ad4792a1cfd6 |
children | be2ab491772e |
comparison
equal
deleted
inserted
replaced
78:b98fa8a4bf04 | 79:43073c7c7769 |
---|---|
137 import dmd.CompoundStatement; | 137 import dmd.CompoundStatement; |
138 import dmd.ConditionalStatement; | 138 import dmd.ConditionalStatement; |
139 import dmd.CompoundDeclarationStatement; | 139 import dmd.CompoundDeclarationStatement; |
140 import dmd.Argument; | 140 import dmd.Argument; |
141 import dmd.ParseStatementFlags; | 141 import dmd.ParseStatementFlags; |
142 import dmd.TypeNewArray; | |
142 import dmd.TypeNext; | 143 import dmd.TypeNext; |
143 import dmd.TypeInstance; | 144 import dmd.TypeInstance; |
144 import dmd.TypePointer; | 145 import dmd.TypePointer; |
145 import dmd.TypeDArray; | 146 import dmd.TypeDArray; |
146 import dmd.TypeAArray; | 147 import dmd.TypeAArray; |
227 typeof(return) decldefs; | 228 typeof(return) decldefs; |
228 | 229 |
229 // ModuleDeclation leads off | 230 // ModuleDeclation leads off |
230 if (token.value == TOK.TOKmodule) | 231 if (token.value == TOK.TOKmodule) |
231 { | 232 { |
232 ubyte* comment = token.blockComment; | 233 string comment = token.blockComment; |
233 bool safe = false; | 234 bool safe = false; |
234 | 235 |
235 nextToken(); | 236 nextToken(); |
236 version (DMDV2) { | 237 version (DMDV2) { |
237 if (token.value == TOK.TOKlparen) | 238 if (token.value == TOK.TOKlparen) |
310 Dsymbols aelse; | 311 Dsymbols aelse; |
311 PROT prot; | 312 PROT prot; |
312 STC stc; | 313 STC stc; |
313 STC storageClass; | 314 STC storageClass; |
314 Condition condition; | 315 Condition condition; |
315 ubyte* comment; | 316 string comment; |
316 | 317 |
317 //printf("Parser.parseDeclDefs()\n"); | 318 //printf("Parser.parseDeclDefs()\n"); |
318 decldefs = new Dsymbols(); | 319 decldefs = new Dsymbols(); |
319 do | 320 do |
320 { | 321 { |
745 * storageClass ident = init, ident = init, ... ; | 746 * storageClass ident = init, ident = init, ... ; |
746 * and return the array of them. | 747 * and return the array of them. |
747 * Starts with token on the first ident. | 748 * Starts with token on the first ident. |
748 * Ends with scanner past closing ';' | 749 * Ends with scanner past closing ';' |
749 */ | 750 */ |
750 version (DMDV2) { | 751 version (DMDV2) |
751 Dsymbols parseAutoDeclarations(STC storageClass, ubyte* comment) | 752 { |
753 Dsymbols parseAutoDeclarations(STC storageClass, const(char)[] comment) | |
752 { | 754 { |
753 auto a = new Dsymbols; | 755 auto a = new Dsymbols; |
754 | 756 |
755 while (true) | 757 while (true) |
756 { | 758 { |
1902 else if (token.value == TOK.TOKlcurly) | 1904 else if (token.value == TOK.TOKlcurly) |
1903 { | 1905 { |
1904 //printf("enum definition\n"); | 1906 //printf("enum definition\n"); |
1905 e.members = new Dsymbols(); | 1907 e.members = new Dsymbols(); |
1906 nextToken(); | 1908 nextToken(); |
1907 ubyte* comment = token.blockComment; | 1909 string comment = token.blockComment; |
1908 while (token.value != TOK.TOKrcurly) | 1910 while (token.value != TOK.TOKrcurly) |
1909 { | 1911 { |
1910 /* Can take the following forms: | 1912 /* Can take the following forms: |
1911 * 1. ident | 1913 * 1. ident |
1912 * 2. ident = value | 1914 * 2. ident = value |
2411 return t; | 2413 return t; |
2412 } | 2414 } |
2413 | 2415 |
2414 Type parseBasicType2(Type t) | 2416 Type parseBasicType2(Type t) |
2415 { | 2417 { |
2416 //printf("parseBasicType2()\n"); | 2418 //writef("parseBasicType2()\n"); |
2417 while (1) | 2419 while (1) |
2418 { | 2420 { |
2419 switch (token.value) | 2421 switch (token.value) |
2420 { | 2422 { |
2421 case TOK.TOKmul: | 2423 case TOK.TOKmul: |
2422 t = new TypePointer(t); | 2424 t = new TypePointer(t); |
2423 nextToken(); | 2425 nextToken(); |
2424 continue; | 2426 continue; |
2425 | 2427 |
2426 case TOK.TOKlbracket: | 2428 case TOK.TOKlbracket: |
2427 // Handle []. Make sure things like | 2429 // Handle []. Make sure things like |
2428 // int[3][1] a; | 2430 // int[3][1] a; |
2429 // is (array[1] of array[3] of int) | 2431 // is (array[1] of array[3] of int) |
2430 nextToken(); | 2432 nextToken(); |
2431 if (token.value == TOK.TOKrbracket) | 2433 if (token.value == TOK.TOKrbracket) |
2432 { | |
2433 t = new TypeDArray(t); // [] | |
2434 nextToken(); | |
2435 } | |
2436 else if (isDeclaration(&token, 0, TOK.TOKrbracket, null)) | |
2437 { // It's an associative array declaration | |
2438 | |
2439 //printf("it's an associative array\n"); | |
2440 Type index = parseType(); // [ type ] | |
2441 t = new TypeAArray(t, index); | |
2442 check(TOK.TOKrbracket); | |
2443 } | |
2444 else | |
2445 { | |
2446 //printf("it's type[expression]\n"); | |
2447 inBrackets++; | |
2448 Expression e = parseExpression(); // [ expression ] | |
2449 if (token.value == TOK.TOKslice) | |
2450 { | 2434 { |
2451 nextToken(); | 2435 t = new TypeDArray(t); // [] |
2452 Expression e2 = parseExpression(); // [ exp .. exp ] | 2436 nextToken(); |
2453 t = new TypeSlice(t, e, e2); | 2437 } |
2438 else if (token.value == TOKnew && peekNext() == TOKrbracket) | |
2439 { | |
2440 t = new TypeNewArray(t); // [new] | |
2441 nextToken(); | |
2442 nextToken(); | |
2443 } | |
2444 else if (isDeclaration(&token, 0, TOK.TOKrbracket, null)) | |
2445 { // It's an associative array declaration | |
2446 | |
2447 //printf("it's an associative array\n"); | |
2448 Type index = parseType(); // [ type ] | |
2449 t = new TypeAArray(t, index); | |
2450 check(TOK.TOKrbracket); | |
2454 } | 2451 } |
2455 else | 2452 else |
2456 t = new TypeSArray(t,e); | 2453 { |
2457 inBrackets--; | 2454 //printf("it's type[expression]\n"); |
2458 check(TOK.TOKrbracket); | 2455 inBrackets++; |
2459 } | 2456 Expression e = parseExpression(); // [ expression ] |
2460 continue; | 2457 if (token.value == TOK.TOKslice) |
2461 | 2458 { |
2462 case TOK.TOKdelegate: | 2459 nextToken(); |
2463 case TOK.TOKfunction: | 2460 Expression e2 = parseExpression(); // [ exp .. exp ] |
2464 { // Handle delegate declaration: | 2461 t = new TypeSlice(t, e, e2); |
2465 // t delegate(parameter list) nothrow pure | 2462 } |
2466 // t function(parameter list) nothrow pure | 2463 else |
2467 Arguments arguments; | 2464 t = new TypeSArray(t,e); |
2468 int varargs; | 2465 inBrackets--; |
2469 bool ispure = false; | 2466 check(TOK.TOKrbracket); |
2470 bool isnothrow = false; | 2467 } |
2471 TOK save = token.value; | 2468 continue; |
2472 | 2469 |
2473 nextToken(); | 2470 case TOK.TOKdelegate: |
2474 arguments = parseParameters(&varargs); | 2471 case TOK.TOKfunction: |
2475 while (1) | 2472 { // Handle delegate declaration: |
2476 { // Postfixes | 2473 // t delegate(parameter list) nothrow pure |
2477 if (token.value == TOK.TOKpure) | 2474 // t function(parameter list) nothrow pure |
2478 ispure = true; | 2475 Arguments arguments; |
2479 else if (token.value == TOK.TOKnothrow) | 2476 int varargs; |
2480 isnothrow = true; | 2477 bool ispure = false; |
2478 bool isnothrow = false; | |
2479 TOK save = token.value; | |
2480 | |
2481 nextToken(); | |
2482 arguments = parseParameters(&varargs); | |
2483 while (1) | |
2484 { // Postfixes | |
2485 if (token.value == TOK.TOKpure) | |
2486 ispure = true; | |
2487 else if (token.value == TOK.TOKnothrow) | |
2488 isnothrow = true; | |
2489 else | |
2490 break; | |
2491 nextToken(); | |
2492 } | |
2493 TypeFunction tf = new TypeFunction(arguments, t, varargs, linkage); | |
2494 tf.ispure = ispure; | |
2495 tf.isnothrow = isnothrow; | |
2496 if (save == TOK.TOKdelegate) | |
2497 t = new TypeDelegate(tf); | |
2481 else | 2498 else |
2482 break; | 2499 t = new TypePointer(tf); // pointer to function |
2483 nextToken(); | 2500 continue; |
2484 } | 2501 } |
2485 TypeFunction tf = new TypeFunction(arguments, t, varargs, linkage); | 2502 |
2486 tf.ispure = ispure; | 2503 default: |
2487 tf.isnothrow = isnothrow; | 2504 return t; |
2488 if (save == TOK.TOKdelegate) | 2505 } |
2489 t = new TypeDelegate(tf); | 2506 assert(0); |
2490 else | |
2491 t = new TypePointer(tf); // pointer to function | |
2492 continue; | |
2493 } | |
2494 | |
2495 default: | |
2496 return t; | |
2497 } | |
2498 assert(0); | |
2499 } | 2507 } |
2500 assert(0); | 2508 assert(0); |
2501 return null; | 2509 return null; |
2502 } | 2510 } |
2503 | 2511 |
2553 nextToken(); | 2561 nextToken(); |
2554 if (token.value == TOK.TOKrbracket) | 2562 if (token.value == TOK.TOKrbracket) |
2555 { // It's a dynamic array | 2563 { // It's a dynamic array |
2556 ta = new TypeDArray(t); // [] | 2564 ta = new TypeDArray(t); // [] |
2557 nextToken(); | 2565 nextToken(); |
2566 } | |
2567 else if (token.value == TOKnew && peekNext() == TOKrbracket) | |
2568 { | |
2569 t = new TypeNewArray(t); // [new] | |
2570 nextToken(); | |
2571 nextToken(); | |
2558 } | 2572 } |
2559 else if (isDeclaration(&token, 0, TOK.TOKrbracket, null)) | 2573 else if (isDeclaration(&token, 0, TOK.TOKrbracket, null)) |
2560 { // It's an associative array | 2574 { // It's an associative array |
2561 | 2575 |
2562 //printf("it's an associative array\n"); | 2576 //printf("it's an associative array\n"); |
2692 Type t; | 2706 Type t; |
2693 Type tfirst; | 2707 Type tfirst; |
2694 Identifier ident; | 2708 Identifier ident; |
2695 Dsymbols a; | 2709 Dsymbols a; |
2696 TOK tok = TOK.TOKreserved; | 2710 TOK tok = TOK.TOKreserved; |
2697 ubyte* comment = token.blockComment; | 2711 string comment = token.blockComment; |
2698 LINK link = linkage; | 2712 LINK link = linkage; |
2699 | 2713 |
2700 //printf("parseDeclarations() %s\n", token.toChars()); | 2714 //printf("parseDeclarations() %s\n", token.toChars()); |
2701 if (storage_class) | 2715 if (storage_class) |
2702 { ts = null; // infer type | 2716 { ts = null; // infer type |
3287 s = new DeclarationStatement(loc, d); | 3301 s = new DeclarationStatement(loc, d); |
3288 break; | 3302 break; |
3289 } | 3303 } |
3290 | 3304 |
3291 case TOK.TOKlcurly: | 3305 case TOK.TOKlcurly: |
3292 { Statements statements; | 3306 { |
3293 | 3307 nextToken(); |
3294 nextToken(); | 3308 Statements statements = new Statements(); |
3295 statements = new Statements(); | 3309 while (token.value != TOK.TOKrcurly && token.value != TOKeof) |
3296 while (token.value != TOK.TOKrcurly) | |
3297 { | 3310 { |
3298 statements.push(cast(void*)parseStatement(ParseStatementFlags.PSsemi | ParseStatementFlags.PScurlyscope)); | 3311 statements.push(cast(void*)parseStatement(ParseStatementFlags.PSsemi | ParseStatementFlags.PScurlyscope)); |
3299 } | 3312 } |
3300 endloc = this.loc; | 3313 endloc = this.loc; |
3301 s = new CompoundStatement(loc, statements); | 3314 s = new CompoundStatement(loc, statements); |
3642 } | 3655 } |
3643 | 3656 |
3644 statements = new Statements(); | 3657 statements = new Statements(); |
3645 while (token.value != TOK.TOKcase && | 3658 while (token.value != TOK.TOKcase && |
3646 token.value != TOK.TOKdefault && | 3659 token.value != TOK.TOKdefault && |
3660 token.value != TOKeof && | |
3647 token.value != TOK.TOKrcurly) | 3661 token.value != TOK.TOKrcurly) |
3648 { | 3662 { |
3649 statements.push(cast(void*)parseStatement(ParseStatementFlags.PSsemi | ParseStatementFlags.PScurlyscope)); | 3663 statements.push(cast(void*)parseStatement(ParseStatementFlags.PSsemi | ParseStatementFlags.PScurlyscope)); |
3650 } | 3664 } |
3651 s = new CompoundStatement(loc, statements); | 3665 s = new CompoundStatement(loc, statements); |
3677 check(TOK.TOKcolon); | 3691 check(TOK.TOKcolon); |
3678 | 3692 |
3679 statements = new Statements(); | 3693 statements = new Statements(); |
3680 while (token.value != TOK.TOKcase && | 3694 while (token.value != TOK.TOKcase && |
3681 token.value != TOK.TOKdefault && | 3695 token.value != TOK.TOKdefault && |
3696 token.value != TOKeof && | |
3682 token.value != TOK.TOKrcurly) | 3697 token.value != TOK.TOKrcurly) |
3683 { | 3698 { |
3684 statements.push(cast(void*)parseStatement(ParseStatementFlags.PSsemi | ParseStatementFlags.PScurlyscope)); | 3699 statements.push(cast(void*)parseStatement(ParseStatementFlags.PSsemi | ParseStatementFlags.PScurlyscope)); |
3685 } | 3700 } |
3686 s = new CompoundStatement(loc, statements); | 3701 s = new CompoundStatement(loc, statements); |
4460 | 4475 |
4461 case TOK.TOKlbracket: | 4476 case TOK.TOKlbracket: |
4462 t = peek(t); | 4477 t = peek(t); |
4463 if (t.value == TOK.TOKrbracket) | 4478 if (t.value == TOK.TOKrbracket) |
4464 { | 4479 { |
4480 t = peek(t); | |
4481 } | |
4482 else if (t.value == TOKnew && peek(t).value == TOKrbracket) | |
4483 { | |
4484 t = peek(t); | |
4465 t = peek(t); | 4485 t = peek(t); |
4466 } | 4486 } |
4467 else if (isDeclaration(t, 0, TOK.TOKrbracket, &t)) | 4487 else if (isDeclaration(t, 0, TOK.TOKrbracket, &t)) |
4468 { | 4488 { |
4469 // It's an associative array declaration | 4489 // It's an associative array declaration |
6099 | 6119 |
6100 e = new NewExp(loc, thisexp, newargs, t, arguments); | 6120 e = new NewExp(loc, thisexp, newargs, t, arguments); |
6101 return e; | 6121 return e; |
6102 } | 6122 } |
6103 | 6123 |
6104 void addComment(Dsymbol s, ubyte* blockComment) | 6124 void addComment(Dsymbol s, const(char)[] blockComment) |
6105 { | 6125 { |
6106 s.addComment(combineComments(blockComment, token.lineComment)); | 6126 s.addComment(combineComments(blockComment, token.lineComment)); |
6107 token.lineComment = null; | 6127 token.lineComment = null; |
6108 } | 6128 } |
6109 } | 6129 } |