Mercurial > projects > dil
comparison trunk/src/dil/parser/Parser.d @ 726:7917811f8116
AggregateDeclarations are wrapped inside TemplateDeclarations now.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Sat, 02 Feb 2008 19:58:09 +0100 |
parents | 684ec5932b2e |
children | efd87fdb1c00 |
comparison
equal
deleted
inserted
replaced
725:84291c0a9e13 | 726:7917811f8116 |
---|---|
1018 error(token, MSG.ExpectedEnumBody, token.srcText); | 1018 error(token, MSG.ExpectedEnumBody, token.srcText); |
1019 | 1019 |
1020 return new EnumDeclaration(enumName, baseType, members, hasBody); | 1020 return new EnumDeclaration(enumName, baseType, members, hasBody); |
1021 } | 1021 } |
1022 | 1022 |
1023 /// Puts a declaration inside a template declaration. | |
1024 Declaration putInsideTemplateDeclaration(Token* begin, Identifier* name, | |
1025 Declaration aggregateDecl, | |
1026 TemplateParameters tparams) | |
1027 { | |
1028 set(aggregateDecl, begin); | |
1029 auto decls = new CompoundDeclaration; | |
1030 decls ~= aggregateDecl; | |
1031 set(decls, begin); | |
1032 return new TemplateDeclaration(name, tparams, decls); | |
1033 } | |
1034 | |
1023 Declaration parseClassDeclaration() | 1035 Declaration parseClassDeclaration() |
1024 { | 1036 { |
1025 assert(token.kind == T.Class); | 1037 assert(token.kind == T.Class); |
1038 auto begin = token; | |
1026 nT(); // Skip class keyword. | 1039 nT(); // Skip class keyword. |
1027 | 1040 |
1028 Identifier* className; | 1041 Identifier* className; |
1029 TemplateParameters tparams; | 1042 TemplateParameters tparams; |
1030 BaseClassType[] bases; | 1043 BaseClassType[] bases; |
1031 CompoundDeclaration decls; | 1044 CompoundDeclaration decls; |
1032 | 1045 |
1033 className = requireIdentifier(MSG.ExpectedClassName); | 1046 className = requireIdentifier(MSG.ExpectedClassName); |
1034 | 1047 |
1035 if (token.kind == T.LParen) | 1048 bool isTemplate = token.kind == T.LParen; |
1049 if (isTemplate) | |
1036 tparams = parseTemplateParameterList(); | 1050 tparams = parseTemplateParameterList(); |
1037 | 1051 |
1038 if (token.kind == T.Colon) | 1052 if (token.kind == T.Colon) |
1039 bases = parseBaseClasses(); | 1053 bases = parseBaseClasses(); |
1040 | 1054 |
1043 else if (token.kind == T.LBrace) | 1057 else if (token.kind == T.LBrace) |
1044 decls = parseDeclarationDefinitionsBody(); | 1058 decls = parseDeclarationDefinitionsBody(); |
1045 else | 1059 else |
1046 error(token, MSG.ExpectedClassBody, token.srcText); | 1060 error(token, MSG.ExpectedClassBody, token.srcText); |
1047 | 1061 |
1048 return new ClassDeclaration(className, tparams, bases, decls); | 1062 Declaration d = new ClassDeclaration(className, /+tparams, +/bases, decls); |
1063 if (isTemplate) | |
1064 d = putInsideTemplateDeclaration(begin, className, d, tparams); | |
1065 return d; | |
1049 } | 1066 } |
1050 | 1067 |
1051 BaseClassType[] parseBaseClasses(bool colonLeadsOff = true) | 1068 BaseClassType[] parseBaseClasses(bool colonLeadsOff = true) |
1052 { | 1069 { |
1053 if (colonLeadsOff) | 1070 if (colonLeadsOff) |
1082 } | 1099 } |
1083 | 1100 |
1084 Declaration parseInterfaceDeclaration() | 1101 Declaration parseInterfaceDeclaration() |
1085 { | 1102 { |
1086 assert(token.kind == T.Interface); | 1103 assert(token.kind == T.Interface); |
1104 auto begin = token; | |
1087 nT(); // Skip interface keyword. | 1105 nT(); // Skip interface keyword. |
1088 | 1106 |
1089 Identifier* name; | 1107 Identifier* name; |
1090 TemplateParameters tparams; | 1108 TemplateParameters tparams; |
1091 BaseClassType[] bases; | 1109 BaseClassType[] bases; |
1092 CompoundDeclaration decls; | 1110 CompoundDeclaration decls; |
1093 | 1111 |
1094 name = requireIdentifier(MSG.ExpectedInterfaceName); | 1112 name = requireIdentifier(MSG.ExpectedInterfaceName); |
1095 | 1113 |
1096 if (token.kind == T.LParen) | 1114 bool isTemplate = token.kind == T.LParen; |
1115 if (isTemplate) | |
1097 tparams = parseTemplateParameterList(); | 1116 tparams = parseTemplateParameterList(); |
1098 | 1117 |
1099 if (token.kind == T.Colon) | 1118 if (token.kind == T.Colon) |
1100 bases = parseBaseClasses(); | 1119 bases = parseBaseClasses(); |
1101 | 1120 |
1104 else if (token.kind == T.LBrace) | 1123 else if (token.kind == T.LBrace) |
1105 decls = parseDeclarationDefinitionsBody(); | 1124 decls = parseDeclarationDefinitionsBody(); |
1106 else | 1125 else |
1107 error(token, MSG.ExpectedInterfaceBody, token.srcText); | 1126 error(token, MSG.ExpectedInterfaceBody, token.srcText); |
1108 | 1127 |
1109 return new InterfaceDeclaration(name, tparams, bases, decls); | 1128 Declaration d = new InterfaceDeclaration(name, /+tparams, +/bases, decls); |
1129 if (isTemplate) | |
1130 d = putInsideTemplateDeclaration(begin, name, d, tparams); | |
1131 return d; | |
1110 } | 1132 } |
1111 | 1133 |
1112 Declaration parseStructOrUnionDeclaration() | 1134 Declaration parseStructOrUnionDeclaration() |
1113 { | 1135 { |
1114 assert(token.kind == T.Struct || token.kind == T.Union); | 1136 assert(token.kind == T.Struct || token.kind == T.Union); |
1115 TOK tok = token.kind; | 1137 auto begin = token; |
1116 nT(); // Skip struct or union keyword. | 1138 nT(); // Skip struct or union keyword. |
1117 | 1139 |
1118 Identifier* name; | 1140 Identifier* name; |
1119 TemplateParameters tparams; | 1141 TemplateParameters tparams; |
1120 CompoundDeclaration decls; | 1142 CompoundDeclaration decls; |
1121 | 1143 |
1122 name = optionalIdentifier(); | 1144 name = optionalIdentifier(); |
1123 | 1145 |
1124 if (name && token.kind == T.LParen) | 1146 bool isTemplate = name && token.kind == T.LParen; |
1147 if (isTemplate) | |
1125 tparams = parseTemplateParameterList(); | 1148 tparams = parseTemplateParameterList(); |
1126 | 1149 |
1127 if (name && skipped(T.Semicolon)) | 1150 if (name && skipped(T.Semicolon)) |
1128 {} | 1151 {} |
1129 else if (token.kind == T.LBrace) | 1152 else if (token.kind == T.LBrace) |
1130 decls = parseDeclarationDefinitionsBody(); | 1153 decls = parseDeclarationDefinitionsBody(); |
1131 else | 1154 else |
1132 error(token, tok == T.Struct ? | 1155 error(token, begin.kind == T.Struct ? |
1133 MSG.ExpectedStructBody : | 1156 MSG.ExpectedStructBody : |
1134 MSG.ExpectedUnionBody, token.srcText); | 1157 MSG.ExpectedUnionBody, token.srcText); |
1135 | 1158 |
1136 if (tok == T.Struct) | 1159 Declaration d; |
1137 { | 1160 if (begin.kind == T.Struct) |
1138 auto sd = new StructDeclaration(name, tparams, decls); | 1161 { |
1162 auto sd = new StructDeclaration(name, /+tparams, +/decls); | |
1139 sd.setAlignSize(this.alignSize); | 1163 sd.setAlignSize(this.alignSize); |
1140 return sd; | 1164 d = sd; |
1141 } | 1165 } |
1142 else | 1166 else |
1143 return new UnionDeclaration(name, tparams, decls); | 1167 d = new UnionDeclaration(name, /+tparams, +/decls); |
1168 | |
1169 if (isTemplate) | |
1170 d = putInsideTemplateDeclaration(begin, name, d, tparams); | |
1171 return d; | |
1144 } | 1172 } |
1145 | 1173 |
1146 Declaration parseConstructorDeclaration() | 1174 Declaration parseConstructorDeclaration() |
1147 { | 1175 { |
1148 assert(token.kind == T.This); | 1176 assert(token.kind == T.This); |