00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "comma/parser/Parser.h"
00028 #include <cassert>
00029 #include <cstring>
00030 #include <vector>
00031 #include <algorithm>
00032
00033 using namespace comma;
00034
00035 Parser::Parser(TextProvider &txtProvider,
00036 IdentifierPool &idPool,
00037 Bridge &bridge,
00038 Diagnostic &diag)
00039 : txtProvider(txtProvider),
00040 idPool(idPool),
00041 action(bridge),
00042 diagnostic(diag),
00043 lexer(txtProvider, diag),
00044 seenError(false)
00045 {
00046
00047 lexer.scan(token[0]);
00048 lexer.scan(token[1]);
00049 }
00050
00051 Lexer::Token &Parser::currentToken()
00052 {
00053 return token[0];
00054 }
00055
00056 Lexer::Token &Parser::nextToken()
00057 {
00058 token[0] = token[1];
00059 lexer.scan(token[1]);
00060 return token[0];
00061 }
00062
00063 Lexer::Token &Parser::peekToken()
00064 {
00065 return token[1];
00066 }
00067
00068 void Parser::ignoreToken()
00069 {
00070 nextToken();
00071 }
00072
00073 bool Parser::currentTokenIs(Lexer::Code code)
00074 {
00075 return currentToken().getCode() == code;
00076 }
00077
00078 bool Parser::nextTokenIs(Lexer::Code code)
00079 {
00080 return peekToken().getCode() == code;
00081 }
00082
00083 Lexer::Code Parser::currentTokenCode()
00084 {
00085 return currentToken().getCode();
00086 }
00087
00088 Lexer::Code Parser::peekTokenCode()
00089 {
00090 return peekToken().getCode();
00091 }
00092
00093 bool Parser::expectToken(Lexer::Code code)
00094 {
00095 if (peekToken().getCode() == code) {
00096 ignoreToken();
00097 return true;
00098 }
00099 return false;
00100 }
00101
00102 bool Parser::reduceToken(Lexer::Code code)
00103 {
00104 if (currentTokenIs(code)) {
00105 ignoreToken();
00106 return true;
00107 }
00108 return false;
00109 }
00110
00111 bool Parser::requireToken(Lexer::Code code)
00112 {
00113 bool status = reduceToken(code);
00114 if (!status)
00115 report(diag::UNEXPECTED_TOKEN_WANTED)
00116 << currentToken().getString()
00117 << Lexer::tokenString(code);
00118 return status;
00119 }
00120
00121 bool Parser::seekToken(Lexer::Code code)
00122 {
00123 while (!currentTokenIs(Lexer::TKN_EOT)) {
00124 if (currentTokenIs(code))
00125 return true;
00126 else
00127 ignoreToken();
00128 }
00129 return false;
00130 }
00131
00132 bool Parser::seekAndConsumeToken(Lexer::Code code)
00133 {
00134 bool status = seekToken(code);
00135 if (status) ignoreToken();
00136 return status;
00137 }
00138
00139 bool Parser::seekTokens(Lexer::Code code0,
00140 Lexer::Code code1, Lexer::Code code2,
00141 Lexer::Code code3, Lexer::Code code4)
00142 {
00143 Lexer::Code codes[] = { code0, code1, code2, code3, code4 };
00144 Lexer::Code *end = &codes[5];
00145
00146 while (!currentTokenIs(Lexer::TKN_EOT))
00147 {
00148 if (end != std::find(codes, end, currentTokenCode()))
00149 return true;
00150 else
00151 ignoreToken();
00152 }
00153 return false;
00154 }
00155
00156 bool Parser::seekAndConsumeTokens(Lexer::Code code0,
00157 Lexer::Code code1, Lexer::Code code2,
00158 Lexer::Code code3, Lexer::Code code4)
00159 {
00160 bool status = seekTokens(code0, code1, code2, code3, code4);
00161 if (status) ignoreToken();
00162 return status;
00163 }
00164
00165
00166
00167
00168
00169 bool Parser::seekEndLabel(const char *label)
00170 {
00171 while (seekAndConsumeToken(Lexer::TKN_END))
00172 {
00173 IdentifierInfo *info = parseIdentifierInfo();
00174
00175 if (info && strcmp(info->getString(), label) == 0) {
00176 reduceToken(Lexer::TKN_SEMI);
00177 return true;
00178 }
00179 }
00180 return false;
00181 }
00182
00183 Location Parser::currentLocation()
00184 {
00185 return currentToken().getLocation();
00186 }
00187
00188 unsigned Parser::currentLine()
00189 {
00190 return txtProvider.getLine(currentLocation());
00191 }
00192
00193 unsigned Parser::currentColumn()
00194 {
00195 return txtProvider.getColumn(currentLocation());
00196 }
00197
00198 IdentifierInfo *Parser::getIdentifierInfo(const Lexer::Token &tkn)
00199 {
00200 const char *rep = tkn.getRep();
00201 unsigned length = tkn.getLength();
00202 IdentifierInfo *info = &idPool.getIdentifierInfo(rep, length);
00203 return info;
00204 }
00205
00206 bool Parser::unitExprFollows()
00207 {
00208 return currentTokenIs(Lexer::TKN_LPAREN) && nextTokenIs(Lexer::TKN_RPAREN);
00209 }
00210
00211 bool Parser::argumentSelectorFollows()
00212 {
00213 return currentTokenIs(Lexer::TKN_IDENTIFIER)
00214 && nextTokenIs(Lexer::TKN_RDARROW);
00215 }
00216
00217 IdentifierInfo *Parser::parseIdentifierInfo()
00218 {
00219 IdentifierInfo *info;
00220
00221 switch (currentTokenCode()) {
00222 case Lexer::TKN_IDENTIFIER:
00223 info = getIdentifierInfo(currentToken());
00224 ignoreToken();
00225 break;
00226
00227 case Lexer::TKN_EOT:
00228 report(diag::PREMATURE_EOS);
00229 info = 0;
00230 break;
00231
00232 default:
00233 report(diag::UNEXPECTED_TOKEN) << currentToken().getString();
00234 ignoreToken();
00235 info = 0;
00236 }
00237 return info;
00238 }
00239
00240 IdentifierInfo *Parser::parseFunctionIdentifierInfo()
00241 {
00242 IdentifierInfo *info;
00243
00244 if (Lexer::isFunctionGlyph(currentToken())) {
00245 const char *rep = Lexer::tokenString(currentToken());
00246 info = &idPool.getIdentifierInfo(rep);
00247 ignoreToken();
00248 }
00249 else
00250 info = parseIdentifierInfo();
00251 return info;
00252 }
00253
00254
00255
00256 bool Parser::parseEndTag(IdentifierInfo *expectedTag)
00257 {
00258 bool status = requireToken(Lexer::TKN_END);
00259 if (status) {
00260 if (expectedTag) {
00261 if (currentTokenIs(Lexer::TKN_SEMI)) {
00262 report(diag::EXPECTED_END_TAG) << expectedTag->getString();
00263 status = false;
00264 }
00265 else {
00266 Location tagLocation = currentLocation();
00267 IdentifierInfo *tag = parseIdentifierInfo();
00268 if (tag && tag != expectedTag) {
00269 report(tagLocation, diag::EXPECTED_END_TAG)
00270 << expectedTag->getString();
00271 status = false;
00272 }
00273 }
00274 }
00275 status = requireToken(Lexer::TKN_SEMI);
00276 }
00277 return status;
00278 }
00279
00280
00281
00282 Node Parser::parseModelParameter()
00283 {
00284 IdentifierInfo *formal;
00285 Location loc;
00286 Node type;
00287
00288 loc = currentLocation();
00289
00290 if ( !(formal = parseIdentifierInfo())) {
00291 seekTokens(Lexer::TKN_SEMI, Lexer::TKN_RPAREN);
00292 return Node::getInvalidNode();
00293 }
00294
00295 if (!requireToken(Lexer::TKN_COLON)) {
00296 seekTokens(Lexer::TKN_SEMI, Lexer::TKN_RPAREN);
00297 return Node::getInvalidNode();
00298 }
00299
00300 type = parseModelInstantiation();
00301 if (type.isValid())
00302 return action.acceptModelParameter(formal, type, loc);
00303 else {
00304 seekTokens(Lexer::TKN_SEMI, Lexer::TKN_RPAREN);
00305 return Node::getInvalidNode();
00306 }
00307 }
00308
00309
00310
00311 void Parser::parseModelParameterization()
00312 {
00313 assert(currentTokenIs(Lexer::TKN_LPAREN));
00314 ignoreToken();
00315
00316
00317 if (currentTokenIs(Lexer::TKN_RPAREN)) {
00318 report(diag::ILLEGAL_EMPTY_PARAMS);
00319 ignoreToken();
00320 return;
00321 }
00322
00323 NodeVector parameters;
00324 LocationVector locations;
00325 do {
00326 Location loc = currentLocation();
00327 Node node = parseModelParameter();
00328 if (node.isValid()) {
00329 parameters.push_back(node);
00330 locations.push_back(loc);
00331 }
00332 } while (reduceToken(Lexer::TKN_SEMI));
00333
00334
00335
00336 action.acceptModelParameterList(¶meters[0],
00337 &locations[0], parameters.size());
00338
00339 requireToken(Lexer::TKN_RPAREN);
00340 }
00341
00342
00343
00344 void Parser::parseModelSupersignatures()
00345 {
00346 NodeVector supers;
00347 do {
00348 Location loc = currentLocation();
00349 Node super = parseModelInstantiation();
00350 if (super.isValid()) {
00351 Node result = action.acceptModelSupersignature(super, loc);
00352
00353
00354 if (result.isValid())
00355 supers.push_back(result);
00356 else
00357 action.deleteNode(result);
00358 }
00359 else {
00360
00361
00362
00363 seekTokens(Lexer::TKN_COMMA, Lexer::TKN_WITH,
00364 Lexer::TKN_ADD, Lexer::TKN_END);
00365 }
00366 } while (reduceToken(Lexer::TKN_COMMA));
00367 action.acceptModelSupersignatureList(&supers[0], supers.size());
00368 }
00369
00370 void Parser::parseSignatureComponents()
00371 {
00372 std::vector<Node> components;
00373
00374
00375 while (currentTokenIs(Lexer::TKN_FUNCTION)) {
00376 Location location;
00377 IdentifierInfo *name;
00378 Node type;
00379
00380 location = currentLocation();
00381 ignoreToken();
00382 name = parseFunctionIdentifierInfo();
00383
00384 if (!name) seekTokens(Lexer::TKN_FUNCTION,
00385 Lexer::TKN_END, Lexer::TKN_ADD);
00386
00387 type = parseFunctionProto();
00388 if (type.isInvalid())
00389 seekTokens(Lexer::TKN_FUNCTION,
00390 Lexer::TKN_END, Lexer::TKN_ADD);
00391
00392 if (requireToken(Lexer::TKN_SEMI)) {
00393 Node component =
00394 action.acceptSignatureComponent(name, type, location);
00395 if (component.isValid())
00396 components.push_back(component);
00397 }
00398 }
00399 action.acceptSignatureComponentList(&components[0], components.size());
00400 }
00401
00402 void Parser::parseModel()
00403 {
00404 IdentifierInfo *info;
00405 Location loc;
00406 bool parsingDomain;
00407
00408 assert(currentTokenIs(Lexer::TKN_SIGNATURE) ||
00409 currentTokenIs(Lexer::TKN_DOMAIN));
00410
00411 parsingDomain = currentTokenIs(Lexer::TKN_DOMAIN) ? true : false;
00412 loc = currentLocation();
00413 ignoreToken();
00414
00415
00416
00417 info = parseIdentifierInfo();
00418 if (!info) return;
00419
00420 if (parsingDomain)
00421 action.beginDomainDefinition(info, loc);
00422 else
00423 action.beginSignatureDefinition(info, loc);
00424
00425 if (currentTokenIs(Lexer::TKN_LPAREN))
00426 parseModelParameterization();
00427 else
00428 action.acceptModelParameterList(0, 0, 0);
00429
00430
00431 if (reduceToken(Lexer::TKN_COLON)) {
00432 if (!currentTokenIs(Lexer::TKN_WITH))
00433 parseModelSupersignatures();
00434 if (reduceToken(Lexer::TKN_WITH))
00435 parseSignatureComponents();
00436 }
00437
00438
00439 if (!parseEndTag(info))
00440 seekTokens(Lexer::TKN_SIGNATURE, Lexer::TKN_DOMAIN);
00441
00442 action.endModelDefinition();
00443 }
00444
00445 Node Parser::parseModelInstantiation()
00446 {
00447 IdentifierInfo *info;
00448 Node type;
00449 Location loc = currentLocation();
00450
00451 if (reduceToken(Lexer::TKN_PERCENT))
00452 return action.acceptPercent(loc);
00453
00454 info = parseIdentifierInfo();
00455 if (!info) return type;
00456
00457 if (reduceToken(Lexer::TKN_LPAREN)) {
00458 if (reduceToken(Lexer::TKN_RPAREN)) {
00459
00460
00461
00462 type = action.acceptTypeIdentifier(info, loc);
00463 if (type.isValid()) report(loc, diag::ILLEGAL_EMPTY_PARAMS);
00464 }
00465 else {
00466 NodeVector arguments;
00467 LocationVector argumentLocs;
00468 IdInfoVector selectors;
00469 LocationVector selectorLocs;
00470 bool allOK = true;
00471
00472 do {
00473 Location loc = currentLocation();
00474
00475 if (argumentSelectorFollows()) {
00476 selectors.push_back(parseIdentifierInfo());
00477 selectorLocs.push_back(loc);
00478 ignoreToken();
00479 loc = currentLocation();
00480 }
00481 else if (!selectors.empty()) {
00482
00483 report(loc, diag::POSITIONAL_FOLLOWING_SELECTED_PARAMETER);
00484 allOK = false;
00485 }
00486
00487 Node argument = parseModelInstantiation();
00488 if (argument.isValid()) {
00489 arguments.push_back(argument);
00490 argumentLocs.push_back(loc);
00491 }
00492 else
00493 allOK = false;
00494
00495 } while (reduceToken(Lexer::TKN_COMMA));
00496 requireToken(Lexer::TKN_RPAREN);
00497
00498
00499
00500 if (allOK) {
00501 Node *args = &arguments[0];
00502 Location *argLocs = &argumentLocs[0];
00503 IdentifierInfo **selections = &selectors[0];
00504 Location *selectLocs = &selectorLocs[0];
00505 unsigned arity = arguments.size();
00506 unsigned numSelects = selectors.size();
00507 type = action.acceptTypeApplication(
00508 info, args, argLocs, arity,
00509 selections, selectLocs, numSelects, loc);
00510 }
00511 else {
00512
00513 NodeVector::iterator iter;
00514 NodeVector::iterator endIter = arguments.end();
00515 for (iter = arguments.begin(); iter != endIter; ++iter)
00516 action.deleteNode(*iter);
00517 }
00518 }
00519 }
00520 else
00521 type = action.acceptTypeIdentifier(info, loc);
00522
00523 return type;
00524 }
00525
00526 bool Parser::parseFormalParameter(ParameterInfo ¶mInfo, parseNodeFn parser)
00527 {
00528 IdentifierInfo *formal;
00529 Location formalLocation;
00530 Node type;
00531 Location typeLocation;
00532
00533 formalLocation = currentLocation();
00534 formal = parseIdentifierInfo();
00535 if (!formal) return false;
00536
00537 if (!requireToken(Lexer::TKN_COLON)) return false;
00538
00539 typeLocation = currentLocation();
00540 type = (this->*parser)();
00541 if (type.isInvalid()) return false;
00542
00543 paramInfo.formal = formal;
00544 paramInfo.formalLocation = formalLocation;
00545 paramInfo.type = type;
00546 paramInfo.typeLocation = typeLocation;
00547 return true;
00548 }
00549
00550 Node Parser::parseFunctionProto()
00551 {
00552 IdInfoVector formalIdentifiers;
00553 LocationVector formalLocations;
00554 NodeVector parameterTypes;
00555 LocationVector typeLocations;
00556 Node returnType;
00557 Location returnLocation;
00558 Location functionLocation;
00559 bool allOK = true;
00560
00561 if (unitExprFollows()) {
00562
00563 ignoreToken();
00564 ignoreToken();
00565 }
00566 else {
00567 if (requireToken(Lexer::TKN_LPAREN)) {
00568
00569
00570 do {
00571 ParameterInfo paramInfo;
00572 parseNodeFn parser = &Parser::parseModelInstantiation;
00573 if (parseFormalParameter(paramInfo, parser)) {
00574 formalIdentifiers.push_back(paramInfo.formal);
00575 formalLocations.push_back(paramInfo.formalLocation);
00576 parameterTypes.push_back(paramInfo.type);
00577 typeLocations.push_back(paramInfo.typeLocation);
00578 }
00579 else allOK = false;
00580 } while (reduceToken(Lexer::TKN_SEMI));
00581
00582
00583 if (!requireToken(Lexer::TKN_RPAREN)) {
00584 deleteNodes(parameterTypes);
00585 return Node::getInvalidNode();
00586 }
00587 }
00588 else
00589 return Node::getInvalidNode();
00590 }
00591
00592
00593 if (!requireToken(Lexer::TKN_RETURN)) {
00594 deleteNodes(parameterTypes);
00595 return Node::getInvalidNode();
00596 }
00597
00598 returnLocation = currentLocation();
00599 returnType = parseModelInstantiation();
00600 if (returnType.isValid() && allOK) {
00601 IdentifierInfo **formals = &formalIdentifiers[0];
00602 Location *formalLocs = &formalLocations[0];
00603 Node *types = ¶meterTypes[0];
00604 Location *typeLocs = &typeLocations[0];
00605 unsigned arity = formalIdentifiers.size();
00606
00607 return action.acceptFunctionType(formals, formalLocs,
00608 types, typeLocs, arity,
00609 returnType, returnLocation);
00610 }
00611
00612
00613 deleteNodes(parameterTypes);
00614 action.deleteNode(returnType);
00615 return Node::getInvalidNode();
00616 }
00617
00618 bool Parser::parseTopLevelDeclaration()
00619 {
00620 for (;;) {
00621 switch (currentTokenCode()) {
00622 case Lexer::TKN_SIGNATURE:
00623 case Lexer::TKN_DOMAIN:
00624 parseModel();
00625 return true;
00626
00627 case Lexer::TKN_EOT:
00628 return false;
00629 break;
00630
00631 default:
00632
00633
00634 report(diag::UNEXPECTED_TOKEN) << currentToken().getString();
00635 seekTokens(Lexer::TKN_DOMAIN, Lexer::TKN_SIGNATURE);
00636 }
00637 }
00638 }