Mercurial > projects > dil
comparison trunk/src/Parser.d @ 239:7911f6a92e6e
- Added 'new' and 'delete' declarations to Token and uncommented next and prev members. Added HEAD to TOK.
- Tokens are stored in a doubly linked list by the Lexer now.
- Added method scanNext() which can be used by peek() and nextToken().
- Saving 'token' in Parser.try_().
author | aziz |
---|---|
date | Mon, 30 Jul 2007 12:36:04 +0000 |
parents | f3c6c15961bb |
children | deab661906ae |
comparison
equal
deleted
inserted
replaced
238:f3c6c15961bb | 239:7911f6a92e6e |
---|---|
23 Information[] errors; | 23 Information[] errors; |
24 | 24 |
25 this(char[] srcText, string fileName) | 25 this(char[] srcText, string fileName) |
26 { | 26 { |
27 lx = new Lexer(srcText, fileName); | 27 lx = new Lexer(srcText, fileName); |
28 token = &lx.token; | |
29 } | 28 } |
30 | 29 |
31 char* prev; | 30 char* prev; |
32 | 31 |
33 void start() | 32 void start() |
39 void nT() | 38 void nT() |
40 { | 39 { |
41 do | 40 do |
42 { | 41 { |
43 lx.nextToken(); | 42 lx.nextToken(); |
43 token = lx.token; | |
44 if (!trying) | 44 if (!trying) |
45 { | 45 { |
46 writef("\33[32m%s\33[0m", token.type); | 46 writef("\33[32m%s\33[0m", token.type); |
47 try | 47 try |
48 writef("%s", prev[0 .. token.end - prev]); | 48 writef("%s", prev[0 .. token.end - prev]); |
66 ReturnType try_(ReturnType)(lazy ReturnType parseMethod, out bool success) | 66 ReturnType try_(ReturnType)(lazy ReturnType parseMethod, out bool success) |
67 { | 67 { |
68 writef("\33[31mtry_\33[0m"); | 68 writef("\33[31mtry_\33[0m"); |
69 ++trying; | 69 ++trying; |
70 // auto len = errors.length; | 70 // auto len = errors.length; |
71 auto oldToken = token; | |
71 auto oldCount = errorCount; | 72 auto oldCount = errorCount; |
72 auto lexerState = lx.getState(); | 73 // auto lexerState = lx.getState(); |
73 auto result = parseMethod(); | 74 auto result = parseMethod(); |
74 // If the length of the array changed we know an error occurred. | 75 // If the length of the array changed we know an error occurred. |
75 if (errorCount != oldCount) | 76 if (errorCount != oldCount) |
76 { | 77 { |
77 lexerState.restore(); // Restore state of the Lexer object. | 78 // lexerState.restore(); // Restore state of the Lexer object. |
78 // errors = errors[0..len]; // Remove errors that were added when parseMethod() was called. | 79 // errors = errors[0..len]; // Remove errors that were added when parseMethod() was called. |
80 token = oldToken; | |
81 lx.token = oldToken; | |
79 errorCount = oldCount; | 82 errorCount = oldCount; |
80 success = false; | 83 success = false; |
81 } | 84 } |
82 else | 85 else |
83 success = true; | 86 success = true; |
86 return result; | 89 return result; |
87 } | 90 } |
88 | 91 |
89 TOK peekNext() | 92 TOK peekNext() |
90 { | 93 { |
91 Token next; | 94 Token* next = token; |
92 lx.peek(next); | 95 lx.peek(next); |
93 return next.type; | 96 return next.type; |
94 } | 97 } |
95 | 98 |
96 /++++++++++++++++++++++++++++++ | 99 /++++++++++++++++++++++++++++++ |
1445 Statement s; | 1448 Statement s; |
1446 Declaration d; | 1449 Declaration d; |
1447 switch (token.type) | 1450 switch (token.type) |
1448 { | 1451 { |
1449 case T.Align: | 1452 case T.Align: |
1453 // TODO: don't call parseAttributeSpecifier(). | |
1450 d = parseAttributeSpecifier(); | 1454 d = parseAttributeSpecifier(); |
1451 goto case_DeclarationStatement; | 1455 goto case_DeclarationStatement; |
1452 /+ Not applicable for statements. | 1456 /+ Not applicable for statements. |
1453 // T.Private, | 1457 // T.Private, |
1454 // T.Package, | 1458 // T.Package, |
1755 Lcommon: | 1759 Lcommon: |
1756 addStorageClass(); | 1760 addStorageClass(); |
1757 nT(); | 1761 nT(); |
1758 s = new AttributeStatement(token.type, parse()); | 1762 s = new AttributeStatement(token.type, parse()); |
1759 break; | 1763 break; |
1764 // TODO: allow "scope class", "abstract scope class" in function bodies? | |
1765 //case T.Class: | |
1760 default: | 1766 default: |
1761 s = new DeclarationStatement(parseDeclaration(stc)); | 1767 s = new DeclarationStatement(parseDeclaration(stc)); |
1762 } | 1768 } |
1763 return s; | 1769 return s; |
1764 } | 1770 } |
3084 | 3090 |
3085 bool tokenAfterParenIs(TOK tok) | 3091 bool tokenAfterParenIs(TOK tok) |
3086 { | 3092 { |
3087 // We count nested parentheses tokens because template types may appear inside parameter lists; e.g. (int x, Foo!(int) y). | 3093 // We count nested parentheses tokens because template types may appear inside parameter lists; e.g. (int x, Foo!(int) y). |
3088 assert(token.type == T.LParen); | 3094 assert(token.type == T.LParen); |
3089 Token next; | 3095 Token* next = token; |
3090 uint level = 1; | 3096 uint level = 1; |
3091 while (1) | 3097 while (1) |
3092 { | 3098 { |
3093 lx.peek(next); | 3099 lx.peek(next); |
3094 switch (next.type) | 3100 switch (next.type) |