comparison trunk/src/dil/Parser.d @ 504:9076c4cea2a4

Changed occur. of Token* to Identifier* and refactored Parser. Module dil.IdTable imports dil.Identifier and dil.IdentsEnum publicly. Removed file TokenIDs.d
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Tue, 11 Dec 2007 17:02:42 +0100
parents 4e14cd1b24da
children 3bb94ba21490
comparison
equal deleted inserted replaced
503:fa63ef408790 504:9076c4cea2a4
12 import dil.Statements; 12 import dil.Statements;
13 import dil.Expressions; 13 import dil.Expressions;
14 import dil.Types; 14 import dil.Types;
15 import dil.Enums; 15 import dil.Enums;
16 import dil.CompilerInfo; 16 import dil.CompilerInfo;
17 import dil.IdentsEnum;
18 import dil.IdTable; 17 import dil.IdTable;
19 import common; 18 import common;
20 19
21 /++ 20 /++
22 The Parser produces an abstract syntax tree (AST) by analyzing 21 The Parser produces an abstract syntax tree (AST) by analyzing
159 auto begin = token; 158 auto begin = token;
160 ModuleFQN moduleFQN; 159 ModuleFQN moduleFQN;
161 do 160 do
162 { 161 {
163 nT(); 162 nT();
164 moduleFQN ~= requireId(); 163 moduleFQN ~= requireIdentifier("expected module identifier, not '{}'");
165 } while (token.type == T.Dot) 164 } while (token.type == T.Dot)
166 require(T.Semicolon); 165 require(T.Semicolon);
167 return set(new ModuleDeclaration(moduleFQN), begin); 166 return set(new ModuleDeclaration(moduleFQN), begin);
168 } 167 }
169 168
933 } 932 }
934 933
935 assert(token.type == T.Import); 934 assert(token.type == T.Import);
936 935
937 ModuleFQN[] moduleFQNs; 936 ModuleFQN[] moduleFQNs;
938 Token*[] moduleAliases; 937 Identifier*[] moduleAliases;
939 Token*[] bindNames; 938 Identifier*[] bindNames;
940 Token*[] bindAliases; 939 Identifier*[] bindAliases;
941 940
942 nT(); // Skip import keyword. 941 nT(); // Skip import keyword.
943 while (1) 942 while (1)
944 { 943 {
945 ModuleFQN moduleFQN; 944 ModuleFQN moduleFQN;
946 Token* moduleAlias; 945 Identifier* moduleAlias;
947
948 moduleAlias = requireId();
949 946
950 // AliasName = ModuleName 947 // AliasName = ModuleName
951 if (token.type == T.Assign) 948 if (peekNext() == T.Assign)
952 { 949 {
953 nT(); 950 moduleAlias = requireIdentifier("expected alias module name, not '{}'");
954 moduleFQN ~= requireId(); 951 nT(); // Skip =
955 }
956 else // import Identifier [^=]
957 {
958 moduleFQN ~= moduleAlias;
959 moduleAlias = null;
960 } 952 }
961 953
962 // Identifier(.Identifier)* 954 // Identifier(.Identifier)*
963 while (token.type == T.Dot) 955 while (1)
964 { 956 {
965 nT(); 957 moduleFQN ~= requireIdentifier("expected module identifier, not '{}'");
966 moduleFQN ~= requireId(); 958 if (token.type != T.Dot)
959 break;
960 nT();
967 } 961 }
968 962
969 // Push identifiers. 963 // Push identifiers.
970 moduleFQNs ~= moduleFQN; 964 moduleFQNs ~= moduleFQN;
971 moduleAliases ~= moduleAlias; 965 moduleAliases ~= moduleAlias;
977 971
978 if (token.type == T.Colon) 972 if (token.type == T.Colon)
979 { 973 {
980 // BindAlias = BindName(, BindAlias = BindName)*; 974 // BindAlias = BindName(, BindAlias = BindName)*;
981 // BindName(, BindName)*; 975 // BindName(, BindName)*;
982 Token* bindName, bindAlias;
983 do 976 do
984 { 977 {
985 nT(); 978 nT();
986 bindAlias = requireId(); 979 Identifier* bindAlias;
987 980 // BindAlias = BindName
988 if (token.type == T.Assign) 981 if (peekNext() == T.Assign)
989 { 982 {
990 nT(); 983 bindAlias = requireIdentifier("expected alias name, not '{}'");
991 bindName = requireId(); 984 nT(); // Skip =
992 } 985 }
993 else
994 {
995 bindName = bindAlias;
996 bindAlias = null;
997 }
998
999 // Push identifiers. 986 // Push identifiers.
1000 bindNames ~= bindName; 987 bindNames ~= requireIdentifier("expected an identifier, not '{}'");
1001 bindAliases ~= bindAlias; 988 bindAliases ~= bindAlias;
1002 } while (token.type == T.Comma) 989 } while (token.type == T.Comma)
1003 } 990 }
1004 991
1005 require(T.Semicolon); 992 require(T.Semicolon);
4449 { 4436 {
4450 nT(); 4437 nT();
4451 require(tok); 4438 require(tok);
4452 } 4439 }
4453 4440
4441 Identifier* requireIdentifier()
4442 {
4443 Identifier* id;
4444 if (token.type == T.Identifier)
4445 (id = token.ident), nT();
4446 else
4447 error(MID.ExpectedButFound, "Identifier", token.srcText);
4448 return id;
4449 }
4450
4451 /++
4452 Params:
4453 errorMsg = an error that has no message ID yet.
4454 +/
4455 Identifier* requireIdentifier(char[] errorMsg)
4456 {
4457 Identifier* id;
4458 if (token.type == T.Identifier)
4459 (id = token.ident), nT();
4460 else
4461 error(token, errorMsg, token.srcText);
4462 return id;
4463 }
4464
4465 Identifier* requireIdentifier(MID mid)
4466 {
4467 Identifier* id;
4468 if (token.type == T.Identifier)
4469 (id = token.ident), nT();
4470 else
4471 error(mid, token.srcText);
4472 return id;
4473 }
4474
4454 Token* requireId() 4475 Token* requireId()
4455 { 4476 {
4456 if (token.type == T.Identifier) 4477 if (token.type == T.Identifier)
4457 { 4478 {
4458 auto id = token; 4479 auto id = token;