00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "comma/parser/Parser.h"
00010
00011 #include "llvm/ADT/APInt.h"
00012
00013 #include <cassert>
00014
00015 using namespace comma;
00016
00017 Node Parser::parseExpr()
00018 {
00019 return parseOperatorExpr();
00020 }
00021
00022 Node Parser::parseOperatorExpr()
00023 {
00024 Node lhs = parseRelationalOperator();
00025
00026 if (lhs.isInvalid())
00027 return getInvalidNode();
00028
00029 Lexer::Code currentKind = currentTokenCode();
00030 if (currentKind == Lexer::TKN_AND ||
00031 currentKind == Lexer::TKN_OR ||
00032 currentKind == Lexer::TKN_XOR)
00033 return parseLogicalOperator(currentKind, lhs);
00034
00035 return lhs;
00036 }
00037
00038 Node Parser::parseLogicalOperator(Lexer::Code expectedKind, Node lhs)
00039 {
00040 if (currentTokenCode() == expectedKind) {
00041 Location loc = currentLocation();
00042 IdentifierInfo *opInfo = parseFunctionIdentifier();
00043
00044 Node rhs = parseRelationalOperator();
00045
00046 if (rhs.isValid()) {
00047 Node prefix = client.acceptDirectName(opInfo, loc, false);
00048 if (prefix.isValid()) {
00049 NodeVector args;
00050 args.push_back(lhs);
00051 args.push_back(rhs);
00052
00053 Node op = client.acceptApplication(prefix, args);
00054 if (op.isValid())
00055 return parseLogicalOperator(expectedKind, op);
00056 }
00057 }
00058 return getInvalidNode();
00059 }
00060
00061
00062
00063 Lexer::Code currentKind = currentTokenCode();
00064 if (currentKind == Lexer::TKN_AND ||
00065 currentKind == Lexer::TKN_OR ||
00066 currentKind == Lexer::TKN_XOR) {
00067 report(diag::MIXED_LOGICAL_OPERATORS);
00068 return getInvalidNode();
00069 }
00070 else
00071 return lhs;
00072 }
00073
00074 Node Parser::parseExponentialOperator()
00075 {
00076 IdentifierInfo *opInfo;
00077 Location loc;
00078
00079 if (currentTokenIs(Lexer::TKN_NOT)) {
00080 loc = currentLocation();
00081 opInfo = parseFunctionIdentifier();
00082
00083 Node operand = parsePrimaryExpr();
00084
00085 if (operand.isValid()) {
00086 Node prefix = client.acceptDirectName(opInfo, loc, false);
00087 if (prefix.isValid()) {
00088 NodeVector args;
00089 args.push_back(operand);
00090 return client.acceptApplication(prefix, args);
00091 }
00092 }
00093 return getInvalidNode();
00094 }
00095
00096 Node lhs = parsePrimaryExpr();
00097
00098 if (lhs.isInvalid())
00099 return getInvalidNode();
00100
00101 switch (currentTokenCode()) {
00102
00103 default:
00104 return lhs;
00105
00106 case Lexer::TKN_POW:
00107 loc = currentLocation();
00108 opInfo = parseFunctionIdentifier();
00109 break;
00110 }
00111
00112
00113
00114 Node rhs = parseExponentialOperator();
00115
00116 if (rhs.isValid()) {
00117 Node prefix = client.acceptDirectName(opInfo, loc, false);
00118 if (prefix.isValid()) {
00119 NodeVector args;
00120 args.push_back(lhs);
00121 args.push_back(rhs);
00122 return client.acceptApplication(prefix, args);
00123 }
00124 }
00125 return getInvalidNode();
00126 }
00127
00128 Node Parser::parseMultiplicativeOperator()
00129 {
00130 IdentifierInfo *opInfo;
00131 Location loc;
00132 Node lhs = parseExponentialOperator();
00133
00134 while (lhs.isValid()) {
00135 switch (currentTokenCode()) {
00136
00137 default:
00138 return lhs;
00139
00140 case Lexer::TKN_STAR:
00141 case Lexer::TKN_FSLASH:
00142 case Lexer::TKN_MOD:
00143 case Lexer::TKN_REM:
00144 loc = currentLocation();
00145 opInfo = parseFunctionIdentifier();
00146 break;
00147 }
00148
00149 Node rhs = parseExponentialOperator();
00150
00151 if (rhs.isValid()) {
00152 Node prefix = client.acceptDirectName(opInfo, loc, false);
00153 if (prefix.isValid()) {
00154 NodeVector args;
00155 args.push_back(lhs);
00156 args.push_back(rhs);
00157 lhs = client.acceptApplication(prefix, args);
00158 continue;
00159 }
00160 }
00161 return getInvalidNode();
00162 }
00163 return lhs;
00164 }
00165
00166 Node Parser::parseAdditiveOperator()
00167 {
00168 Node lhs = getNullNode();
00169
00170 if (currentTokenIs(Lexer::TKN_PLUS) || currentTokenIs(Lexer::TKN_MINUS)) {
00171 Location loc = currentLocation();
00172 IdentifierInfo *opInfo = parseFunctionIdentifier();
00173
00174 lhs = parseMultiplicativeOperator();
00175
00176 if (!lhs.isValid())
00177 return getInvalidNode();
00178
00179 Node prefix = client.acceptDirectName(opInfo, loc, false);
00180 if (prefix.isValid()) {
00181 NodeVector args;
00182 args.push_back(lhs);
00183 lhs = client.acceptApplication(prefix, args);
00184 }
00185 }
00186 else
00187 lhs = parseMultiplicativeOperator();
00188
00189 return parseBinaryAdditiveOperator(lhs);
00190 }
00191
00192 Node Parser::parseBinaryAdditiveOperator(Node lhs)
00193 {
00194 IdentifierInfo *opInfo;
00195 Location loc;
00196
00197 while (lhs.isValid()) {
00198 switch (currentTokenCode()) {
00199
00200 default:
00201 return lhs;
00202
00203 case Lexer::TKN_PLUS:
00204 case Lexer::TKN_MINUS:
00205 loc = currentLocation();
00206 opInfo = parseFunctionIdentifier();
00207 break;
00208 }
00209
00210 Node rhs = parseMultiplicativeOperator();
00211
00212 if (rhs.isValid()) {
00213 Node prefix = client.acceptDirectName(opInfo, loc, false);
00214 if (prefix.isValid()) {
00215 NodeVector args;
00216 args.push_back(lhs);
00217 args.push_back(rhs);
00218 lhs = client.acceptApplication(prefix, args);
00219 continue;
00220 }
00221 }
00222 return getInvalidNode();
00223 }
00224 return lhs;
00225 }
00226
00227 Node Parser::parseRelationalOperator()
00228 {
00229 IdentifierInfo *opInfo;
00230 Location loc;
00231 Node lhs = parseAdditiveOperator();
00232
00233 while (lhs.isValid()) {
00234 switch (currentTokenCode()) {
00235
00236 default:
00237 return lhs;
00238
00239 case Lexer::TKN_EQUAL:
00240 case Lexer::TKN_NEQUAL:
00241 case Lexer::TKN_LESS:
00242 case Lexer::TKN_GREAT:
00243 case Lexer::TKN_LEQ:
00244 case Lexer::TKN_GEQ:
00245 loc = currentLocation();
00246 opInfo = parseFunctionIdentifier();
00247 break;
00248 }
00249
00250 Node rhs = parseAdditiveOperator();
00251
00252 if (rhs.isValid()) {
00253 Node prefix = client.acceptDirectName(opInfo, loc, false);
00254 if (prefix.isValid()) {
00255 NodeVector args;
00256 args.push_back(lhs);
00257 args.push_back(rhs);
00258 lhs = client.acceptApplication(prefix, args);
00259 continue;
00260 }
00261 }
00262 return getInvalidNode();
00263 }
00264 return lhs;
00265 }
00266
00267 Node Parser::parseParenExpr()
00268 {
00269 assert(currentTokenIs(Lexer::TKN_LPAREN));
00270
00271 if (aggregateFollows())
00272 return parseAggregate();
00273 else {
00274 ignoreToken();
00275 Node result = parseExpr();
00276 if (!reduceToken(Lexer::TKN_RPAREN))
00277 report(diag::UNEXPECTED_TOKEN_WANTED) <<
00278 currentTokenString() << ")";
00279 return result;
00280 }
00281 }
00282
00283 Node Parser::parsePrimaryExpr()
00284 {
00285 switch (currentTokenCode()) {
00286
00287 default: {
00288 Node name = parseName();
00289 if (qualificationFollows()) {
00290 if (name.isInvalid()) {
00291 ignoreToken();
00292 ignoreToken();
00293 seekCloseParen();
00294 return getInvalidNode();
00295 }
00296 else
00297 return parseQualifiedExpr(name);
00298 }
00299 else
00300 return name;
00301 }
00302
00303 case Lexer::TKN_LPAREN:
00304 return parseParenExpr();
00305
00306 case Lexer::TKN_INTEGER:
00307 return parseIntegerLiteral();
00308
00309 case Lexer::TKN_STRING:
00310 return parseStringLiteral();
00311
00312 case Lexer::TKN_NULL:
00313 return client.acceptNullExpr(ignoreToken());
00314
00315 case Lexer::TKN_NEW:
00316 return parseAllocatorExpr();
00317 }
00318 }
00319
00320 Node Parser::parseIntegerLiteral()
00321 {
00322 assert(currentTokenIs(Lexer::TKN_INTEGER));
00323
00324 const char *rep = currentToken().getRep();
00325 unsigned repLen = currentToken().getLength();
00326 Location loc = ignoreToken();
00327
00328 llvm::APInt value;
00329 decimalLiteralToAPInt(rep, repLen, value);
00330 return client.acceptIntegerLiteral(value, loc);
00331 }
00332
00333 Node Parser::parseStringLiteral()
00334 {
00335 assert(currentTokenIs(Lexer::TKN_STRING));
00336
00337 const char *rep = currentToken().getRep();
00338 unsigned repLen = currentToken().getLength();
00339 Location loc = ignoreToken();
00340
00341 return client.acceptStringLiteral(rep, repLen, loc);
00342 }
00343
00344 Node Parser::parseQualifiedExpr(Node qualifier)
00345 {
00346 assert(qualificationFollows());
00347
00348 ignoreToken();
00349 Node operand = parseParenExpr();
00350
00351 if (operand.isValid())
00352 return client.acceptQualifiedExpr(qualifier, operand);
00353 return getInvalidNode();
00354 }
00355
00356 Node Parser::parseAllocatorExpr()
00357 {
00358 assert(currentTokenIs(Lexer::TKN_NEW));
00359
00360 Location loc = ignoreToken();
00361
00362
00363
00364 Node operand = parseName();
00365
00366 if (operand.isInvalid()) {
00367
00368 if (qualificationFollows()) {
00369 ignoreToken();
00370 ignoreToken();
00371 seekCloseParen();
00372 }
00373 return getInvalidNode();
00374 }
00375
00376 if (qualificationFollows()) {
00377 operand = parseQualifiedExpr(operand);
00378 if (operand.isInvalid())
00379 return getInvalidNode();
00380 }
00381
00382 return client.acceptAllocatorExpr(operand, loc);
00383 }
00384
00385 Node Parser::parseOthersExpr()
00386 {
00387 assert(currentTokenIs(Lexer::TKN_OTHERS));
00388 Location loc = ignoreToken();
00389
00390 if (!requireToken(Lexer::TKN_RDARROW))
00391 return getInvalidNode();
00392
00393
00394
00395 Node result = getNullNode();
00396 if (!reduceToken(Lexer::TKN_DIAMOND))
00397 result = parseExpr();
00398
00399
00400
00401 if (!currentTokenIs(Lexer::TKN_RPAREN)) {
00402 report(loc, diag::OTHERS_COMPONENT_NOT_FINAL);
00403 return getInvalidNode();
00404 }
00405
00406 return result;
00407 }
00408
00409 bool Parser::parseAggregateComponent(bool &seenKeyedComponent)
00410 {
00411 NodeVector keys;
00412
00413 do {
00414 Location loc = currentLocation();
00415
00416
00417
00418 if (currentTokenIs(Lexer::TKN_IDENTIFIER) &&
00419 (nextTokenIs(Lexer::TKN_RDARROW) || nextTokenIs(Lexer::TKN_BAR))) {
00420 IdentifierInfo *name = parseIdentifier();
00421 Node key = client.acceptAggregateKey(name, loc);
00422 if (key.isValid())
00423 keys.push_back(key);
00424 continue;
00425 }
00426
00427 Node lower = parseExpr();
00428 if (lower.isInvalid()) {
00429 seekTokens(Lexer::TKN_BAR, Lexer::TKN_COMMA, Lexer::TKN_RPAREN);
00430 continue;
00431 }
00432
00433 if (currentTokenIs(Lexer::TKN_COMMA) ||
00434 currentTokenIs(Lexer::TKN_RPAREN)) {
00435 if (seenKeyedComponent) {
00436 report(loc, diag::POSITIONAL_FOLLOWING_KEYED_COMPONENT);
00437 seekCloseParen();
00438 return false;
00439 }
00440 client.acceptPositionalAggregateComponent(lower);
00441 return true;
00442 }
00443
00444 if (reduceToken(Lexer::TKN_DDOT)) {
00445 Node upper = parseExpr();
00446 if (upper.isInvalid())
00447 seekTokens(Lexer::TKN_BAR, Lexer::TKN_COMMA, Lexer::TKN_RPAREN);
00448 else {
00449 Node key = client.acceptAggregateKey(lower, upper);
00450 if (key.isValid())
00451 keys.push_back(key);
00452 }
00453 }
00454 else {
00455 Node key = client.acceptAggregateKey(lower);
00456 if (key.isValid())
00457 keys.push_back(key);
00458 }
00459 } while (reduceToken(Lexer::TKN_BAR));
00460
00461 seenKeyedComponent = true;
00462 if (requireToken(Lexer::TKN_RDARROW)) {
00463 Node expr = getNullNode();
00464 Location loc = currentLocation();
00465 if (!reduceToken(Lexer::TKN_DIAMOND))
00466 expr = parseExpr();
00467 if (expr.isValid())
00468 client.acceptKeyedAggregateComponent(keys, expr, loc);
00469 }
00470 return true;
00471 }
00472
00473 Node Parser::parseAggregate()
00474 {
00475 assert(currentTokenIs(Lexer::TKN_LPAREN));
00476
00477 client.beginAggregate(ignoreToken());
00478 bool seenKeyedComponent = false;
00479
00480
00481 do {
00482 if (currentTokenIs(Lexer::TKN_OTHERS)) {
00483 Location loc = currentLocation();
00484 Node others = parseOthersExpr();
00485
00486 if (others.isValid()) {
00487 client.acceptAggregateOthers(loc, others);
00488 requireToken(Lexer::TKN_RPAREN);
00489 }
00490 else
00491 seekCloseParen();
00492 return client.endAggregate();
00493 }
00494
00495 if (!parseAggregateComponent(seenKeyedComponent))
00496 return getInvalidNode();
00497
00498 } while (reduceToken(Lexer::TKN_COMMA));
00499
00500 requireToken(Lexer::TKN_RPAREN);
00501 return client.endAggregate();
00502 }
00503