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 switch (currentTokenCode()) {
00020
00021 default:
00022 return parseOperatorExpr();
00023
00024 case Lexer::TKN_INJ:
00025 return parseInjExpr();
00026
00027 case Lexer::TKN_PRJ:
00028 return parsePrjExpr();
00029 }
00030 }
00031
00032 Node Parser::parseSubroutineKeywordSelection()
00033 {
00034 assert(keywordSelectionFollows());
00035 Location loc = currentLocation();
00036 IdentifierInfo *key = parseIdentifierInfo();
00037
00038 ignoreToken();
00039 Node expr = parseExpr();
00040
00041 if (expr.isInvalid())
00042 return getInvalidNode();
00043 else
00044 return client.acceptKeywordSelector(key, loc, expr, true);
00045 }
00046
00047 Node Parser::parseInjExpr()
00048 {
00049 assert(currentTokenIs(Lexer::TKN_INJ));
00050
00051 Location loc = ignoreToken();
00052 Node expr = parseExpr();
00053
00054 if (expr.isValid())
00055 return client.acceptInj(loc, expr);
00056 return getInvalidNode();
00057 }
00058
00059 Node Parser::parsePrjExpr()
00060 {
00061 assert(currentTokenIs(Lexer::TKN_PRJ));
00062
00063 Location loc = ignoreToken();
00064 Node expr = parseExpr();
00065
00066 if (expr.isValid())
00067 return client.acceptPrj(loc, expr);
00068 return getInvalidNode();
00069 }
00070
00071 Node Parser::parseOperatorExpr()
00072 {
00073 return parseRelationalOperator();
00074 }
00075
00076 Node Parser::parseExponentialOperator()
00077 {
00078 IdentifierInfo *opInfo;
00079 Location loc;
00080 Node lhs = parsePrimaryExpr();
00081
00082 if (lhs.isInvalid())
00083 return getInvalidNode();
00084
00085 switch (currentTokenCode()) {
00086
00087 default:
00088 return lhs;
00089
00090 case Lexer::TKN_HAT:
00091 loc = currentLocation();
00092 opInfo = parseFunctionIdentifierInfo();
00093 break;
00094 }
00095
00096
00097
00098 Node rhs = parseExponentialOperator();
00099
00100 if (rhs.isValid()) {
00101 Node fname = client.acceptFunctionName(opInfo, loc, getNullNode());
00102 if (fname.isValid()) {
00103 NodeVector args;
00104 args.push_back(lhs);
00105 args.push_back(rhs);
00106 return client.acceptFunctionCall(fname, loc, args);
00107 }
00108 }
00109 return getInvalidNode();
00110 }
00111
00112 Node Parser::parseMultiplicativeOperator()
00113 {
00114 IdentifierInfo *opInfo;
00115 Location loc;
00116 Node lhs = parseExponentialOperator();
00117
00118 while (lhs.isValid()) {
00119 switch (currentTokenCode()) {
00120
00121 default:
00122 return lhs;
00123
00124 case Lexer::TKN_STAR:
00125 case Lexer::TKN_FSLASH:
00126 loc = currentLocation();
00127 opInfo = parseFunctionIdentifierInfo();
00128 break;
00129 }
00130
00131 Node rhs = parseExponentialOperator();
00132
00133 if (rhs.isValid()) {
00134 Node fname = client.acceptFunctionName(opInfo, loc, getNullNode());
00135 if (fname.isValid()) {
00136 NodeVector args;
00137 args.push_back(lhs);
00138 args.push_back(rhs);
00139 lhs = client.acceptFunctionCall(fname, loc, args);
00140 continue;
00141 }
00142 }
00143 return getInvalidNode();
00144 }
00145 return lhs;
00146 }
00147
00148 Node Parser::parseAdditiveOperator()
00149 {
00150 IdentifierInfo *opInfo;
00151 Location loc;
00152 Node lhs = parseMultiplicativeOperator();
00153
00154 while (lhs.isValid()) {
00155 switch (currentTokenCode()) {
00156
00157 default:
00158 return lhs;
00159
00160 case Lexer::TKN_PLUS:
00161 case Lexer::TKN_MINUS:
00162 loc = currentLocation();
00163 opInfo = parseFunctionIdentifierInfo();
00164 break;
00165 }
00166
00167 Node rhs = parseMultiplicativeOperator();
00168
00169 if (rhs.isValid()) {
00170 Node fname = client.acceptFunctionName(opInfo, loc, getNullNode());
00171 if (fname.isValid()) {
00172 NodeVector args;
00173 args.push_back(lhs);
00174 args.push_back(rhs);
00175 lhs = client.acceptFunctionCall(fname, loc, args);
00176 continue;
00177 }
00178 }
00179 return getInvalidNode();
00180 }
00181 return lhs;
00182 }
00183
00184 Node Parser::parseRelationalOperator()
00185 {
00186 IdentifierInfo *opInfo;
00187 Location loc;
00188 Node lhs = parseAdditiveOperator();
00189
00190 while (lhs.isValid()) {
00191 switch (currentTokenCode()) {
00192
00193 default:
00194 return lhs;
00195
00196 case Lexer::TKN_EQUAL:
00197 case Lexer::TKN_LESS:
00198 case Lexer::TKN_GREAT:
00199 case Lexer::TKN_LEQ:
00200 case Lexer::TKN_GEQ:
00201 case Lexer::TKN_DIAMOND:
00202 loc = currentLocation();
00203 opInfo = parseFunctionIdentifierInfo();
00204 break;
00205 }
00206
00207 Node rhs = parseAdditiveOperator();
00208
00209 if (rhs.isValid()) {
00210 Node fname = client.acceptFunctionName(opInfo, loc, getNullNode());
00211 if (fname.isValid()) {
00212 NodeVector args;
00213 args.push_back(lhs);
00214 args.push_back(rhs);
00215 lhs = client.acceptFunctionCall(fname, loc, args);
00216 continue;
00217 }
00218 }
00219 return getInvalidNode();
00220 }
00221 return lhs;
00222 }
00223
00224 Node Parser::parsePrimaryExpr()
00225 {
00226 if (reduceToken(Lexer::TKN_LPAREN)) {
00227 Node result = parseExpr();
00228 if (!reduceToken(Lexer::TKN_RPAREN))
00229 report(diag::UNEXPECTED_TOKEN_WANTED)
00230 << currentTokenString() << ")";
00231 return result;
00232 }
00233
00234 if (currentTokenIs(Lexer::TKN_INTEGER))
00235 return parseIntegerLiteral();
00236
00237 Node qual = getNullNode();
00238 if (qualifierFollows()) {
00239 qual = parseQualifier();
00240 if (qual.isInvalid())
00241 return getInvalidNode();
00242 }
00243
00244 Location loc = currentLocation();
00245 IdentifierInfo *name = parseIdentifierInfo();
00246
00247 if (!name)
00248 return getInvalidNode();
00249
00250 if (currentTokenIs(Lexer::TKN_LPAREN)) {
00251 NodeVector args;
00252 if (!parseSubroutineArgumentList(args))
00253 return getInvalidNode();
00254
00255 Node connective = client.acceptFunctionName(name, loc, qual);
00256
00257 if (connective.isValid())
00258 return client.acceptFunctionCall(connective, loc, args);
00259 else
00260 return getInvalidNode();
00261 }
00262 else
00263 return client.acceptDirectName(name, loc, qual);
00264 }
00265
00266 Node Parser::parseIntegerLiteral()
00267 {
00268 assert(currentTokenIs(Lexer::TKN_INTEGER));
00269
00270 typedef std::char_traits<char> Traits;
00271
00272 const char *rep = currentToken().getRep();
00273 unsigned repLen = currentToken().getLength();
00274 Location loc = ignoreToken();
00275
00276 llvm::APInt value;
00277 decimalLiteralToAPInt(rep, repLen, value);
00278 return client.acceptIntegerLiteral(value, loc);
00279 }
00280
00281