OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #include "stdio.h" |
| 9 #include "SkSLParser.h" |
| 10 #include "SkSLToken.h" |
| 11 |
| 12 #define register |
| 13 #ifdef __clang__ |
| 14 #pragma clang diagnostic push |
| 15 #pragma clang diagnostic ignored "-Wunneeded-internal-declaration" |
| 16 #endif |
| 17 #include "lex.sksl.c" |
| 18 #ifdef __clang__ |
| 19 #pragma clang diagnostic pop |
| 20 #endif |
| 21 #undef register |
| 22 |
| 23 #include "ast/SkSLASTBinaryExpression.h" |
| 24 #include "ast/SkSLASTBlock.h" |
| 25 #include "ast/SkSLASTBoolLiteral.h" |
| 26 #include "ast/SkSLASTBreakStatement.h" |
| 27 #include "ast/SkSLASTCallSuffix.h" |
| 28 #include "ast/SkSLASTContinueStatement.h" |
| 29 #include "ast/SkSLASTDiscardStatement.h" |
| 30 #include "ast/SkSLASTDoStatement.h" |
| 31 #include "ast/SkSLASTExpression.h" |
| 32 #include "ast/SkSLASTExpressionStatement.h" |
| 33 #include "ast/SkSLASTExtension.h" |
| 34 #include "ast/SkSLASTFieldSuffix.h" |
| 35 #include "ast/SkSLASTFloatLiteral.h" |
| 36 #include "ast/SkSLASTForStatement.h" |
| 37 #include "ast/SkSLASTFunction.h" |
| 38 #include "ast/SkSLASTIdentifier.h" |
| 39 #include "ast/SkSLASTIfStatement.h" |
| 40 #include "ast/SkSLASTIndexSuffix.h" |
| 41 #include "ast/SkSLASTInterfaceBlock.h" |
| 42 #include "ast/SkSLASTIntLiteral.h" |
| 43 #include "ast/SkSLASTParameter.h" |
| 44 #include "ast/SkSLASTPrefixExpression.h" |
| 45 #include "ast/SkSLASTReturnStatement.h" |
| 46 #include "ast/SkSLASTStatement.h" |
| 47 #include "ast/SkSLASTSuffixExpression.h" |
| 48 #include "ast/SkSLASTTernaryExpression.h" |
| 49 #include "ast/SkSLASTType.h" |
| 50 #include "ast/SkSLASTVarDeclaration.h" |
| 51 #include "ast/SkSLASTVarDeclarationStatement.h" |
| 52 #include "ast/SkSLASTWhileStatement.h" |
| 53 #include "ir/SkSLSymbolTable.h" |
| 54 |
| 55 namespace SkSL { |
| 56 |
| 57 Parser::Parser(std::string text, SymbolTable& types, ErrorReporter& errors) |
| 58 : fPushback(Position(-1, -1), Token::INVALID_TOKEN, "") |
| 59 , fTypes(types) |
| 60 , fErrors(errors) { |
| 61 sksllex_init(&fScanner); |
| 62 fBuffer = sksl_scan_string(text.c_str(), fScanner); |
| 63 skslset_lineno(1, fScanner); |
| 64 |
| 65 if (false) { |
| 66 // avoid unused warning |
| 67 yyunput(0, nullptr, fScanner); |
| 68 } |
| 69 } |
| 70 |
| 71 Parser::~Parser() { |
| 72 sksl_delete_buffer(fBuffer, fScanner); |
| 73 } |
| 74 |
| 75 /* (precision | directive | declaration)* END_OF_FILE */ |
| 76 std::vector<std::unique_ptr<ASTDeclaration>> Parser::file() { |
| 77 std::vector<std::unique_ptr<ASTDeclaration>> result; |
| 78 for (;;) { |
| 79 switch (this->peek().fKind) { |
| 80 case Token::END_OF_FILE: |
| 81 return result; |
| 82 case Token::PRECISION: |
| 83 this->precision(); |
| 84 break; |
| 85 case Token::DIRECTIVE: { |
| 86 std::unique_ptr<ASTDeclaration> decl = this->directive(); |
| 87 if (decl) { |
| 88 result.push_back(std::move(decl)); |
| 89 } |
| 90 break; |
| 91 } |
| 92 default: { |
| 93 std::unique_ptr<ASTDeclaration> decl = this->declaration(); |
| 94 if (!decl) { |
| 95 continue; |
| 96 } |
| 97 result.push_back(std::move(decl)); |
| 98 } |
| 99 } |
| 100 } |
| 101 } |
| 102 |
| 103 Token Parser::nextToken() { |
| 104 if (fPushback.fKind != Token::INVALID_TOKEN) { |
| 105 Token result = fPushback; |
| 106 fPushback.fKind = Token::INVALID_TOKEN; |
| 107 fPushback.fText = ""; |
| 108 return result; |
| 109 } |
| 110 int token = sksllex(fScanner); |
| 111 return Token(Position(skslget_lineno(fScanner), -1), (Token::Kind) token, |
| 112 token == Token::END_OF_FILE ? "<end of file>" : |
| 113 std::string(skslget_text(fScanner
))); |
| 114 } |
| 115 |
| 116 void Parser::pushback(Token t) { |
| 117 ASSERT(fPushback.fKind == Token::INVALID_TOKEN); |
| 118 fPushback = t; |
| 119 } |
| 120 |
| 121 Token Parser::peek() { |
| 122 fPushback = this->nextToken(); |
| 123 return fPushback; |
| 124 } |
| 125 |
| 126 bool Parser::expect(Token::Kind kind, std::string expected, Token* result) { |
| 127 Token next = this->nextToken(); |
| 128 if (next.fKind == kind) { |
| 129 if (result) { |
| 130 *result = next; |
| 131 } |
| 132 return true; |
| 133 } else { |
| 134 this->error(next.fPosition, "expected " + expected + ", but found '" + n
ext.fText + "'"); |
| 135 return false; |
| 136 } |
| 137 } |
| 138 |
| 139 void Parser::error(Position p, std::string msg) { |
| 140 fErrors.error(p, msg); |
| 141 } |
| 142 |
| 143 bool Parser::isType(std::string name) { |
| 144 return nullptr != fTypes[name]; |
| 145 } |
| 146 |
| 147 /* PRECISION (LOWP | MEDIUMP | HIGHP) type SEMICOLON */ |
| 148 void Parser::precision() { |
| 149 if (!this->expect(Token::PRECISION, "'precision'")) { |
| 150 return; |
| 151 } |
| 152 Token p = this->nextToken(); |
| 153 switch (p.fKind) { |
| 154 case Token::LOWP: // fall through |
| 155 case Token::MEDIUMP: // fall through |
| 156 case Token::HIGHP: |
| 157 // ignored for now |
| 158 break; |
| 159 default: |
| 160 this->error(p.fPosition, "expected 'lowp', 'mediump', or 'highp', bu
t found '" + |
| 161 p.fText + "'"); |
| 162 return; |
| 163 } |
| 164 if (!this->type()) { |
| 165 return; |
| 166 } |
| 167 this->expect(Token::SEMICOLON, "';'"); |
| 168 } |
| 169 |
| 170 /* DIRECTIVE(#version) INT_LITERAL | DIRECTIVE(#extension) IDENTIFIER COLON IDEN
TIFIER */ |
| 171 std::unique_ptr<ASTDeclaration> Parser::directive() { |
| 172 Token start; |
| 173 if (!this->expect(Token::DIRECTIVE, "a directive", &start)) { |
| 174 return nullptr; |
| 175 } |
| 176 if (start.fText == "#version") { |
| 177 this->expect(Token::INT_LITERAL, "a version number"); |
| 178 // ignored for now |
| 179 return nullptr; |
| 180 } else if (start.fText == "#extension") { |
| 181 Token name; |
| 182 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) { |
| 183 return nullptr; |
| 184 } |
| 185 if (!this->expect(Token::COLON, "':'")) { |
| 186 return nullptr; |
| 187 } |
| 188 // FIXME: need to start paying attention to this token |
| 189 if (!this->expect(Token::IDENTIFIER, "an identifier")) { |
| 190 return nullptr; |
| 191 } |
| 192 return std::unique_ptr<ASTDeclaration>(new ASTExtension(start.fPosition,
|
| 193 std::move(name.f
Text))); |
| 194 } else { |
| 195 this->error(start.fPosition, "unsupported directive '" + start.fText + "
'"); |
| 196 return nullptr; |
| 197 } |
| 198 } |
| 199 |
| 200 /* modifiers (structVarDeclaration | type IDENTIFIER ((LPAREN parameter |
| 201 (COMMA parameter)* RPAREN (block | SEMICOLON)) | SEMICOLON) | interfaceBlock)
*/ |
| 202 std::unique_ptr<ASTDeclaration> Parser::declaration() { |
| 203 ASTModifiers modifiers = this->modifiers(); |
| 204 Token lookahead = this->peek(); |
| 205 if (lookahead.fKind == Token::IDENTIFIER && !this->isType(lookahead.fText))
{ |
| 206 // we have an identifier that's not a type, could be the start of an int
erface block |
| 207 return this->interfaceBlock(modifiers); |
| 208 } |
| 209 if (lookahead.fKind == Token::STRUCT) { |
| 210 return this->structVarDeclaration(modifiers); |
| 211 } |
| 212 std::unique_ptr<ASTType> type(this->type()); |
| 213 if (!type) { |
| 214 return nullptr; |
| 215 } |
| 216 if (type->fKind == ASTType::kStruct_Kind && peek().fKind == Token::SEMICOLON
) { |
| 217 this->nextToken(); |
| 218 return nullptr; |
| 219 } |
| 220 Token name; |
| 221 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) { |
| 222 return nullptr; |
| 223 } |
| 224 if (!modifiers.fFlags && this->peek().fKind == Token::LPAREN) { |
| 225 this->nextToken(); |
| 226 std::vector<std::unique_ptr<ASTParameter>> parameters; |
| 227 while (this->peek().fKind != Token::RPAREN) { |
| 228 if (parameters.size() > 0) { |
| 229 if (!this->expect(Token::COMMA, "','")) { |
| 230 return nullptr; |
| 231 } |
| 232 } |
| 233 std::unique_ptr<ASTParameter> parameter = this->parameter(); |
| 234 if (!parameter) { |
| 235 return nullptr; |
| 236 } |
| 237 parameters.push_back(std::move(parameter)); |
| 238 } |
| 239 this->nextToken(); |
| 240 std::unique_ptr<ASTBlock> body; |
| 241 if (this->peek().fKind == Token::SEMICOLON) { |
| 242 this->nextToken(); |
| 243 } else { |
| 244 body = this->block(); |
| 245 if (!body) { |
| 246 return nullptr; |
| 247 } |
| 248 } |
| 249 return std::unique_ptr<ASTDeclaration>(new ASTFunction(name.fPosition, s
td::move(type), |
| 250 std::move(name.fT
ext), |
| 251 std::move(paramet
ers), |
| 252 std::move(body)))
; |
| 253 } else { |
| 254 return this->varDeclarationEnd(modifiers, std::move(type), name.fText); |
| 255 } |
| 256 } |
| 257 |
| 258 /* modifiers type IDENTIFIER varDeclarationEnd */ |
| 259 std::unique_ptr<ASTVarDeclaration> Parser::varDeclaration() { |
| 260 ASTModifiers modifiers = this->modifiers(); |
| 261 std::unique_ptr<ASTType> type(this->type()); |
| 262 if (!type) { |
| 263 return nullptr; |
| 264 } |
| 265 Token name; |
| 266 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) { |
| 267 return nullptr; |
| 268 } |
| 269 return this->varDeclarationEnd(modifiers, std::move(type), std::move(name.fT
ext)); |
| 270 } |
| 271 |
| 272 /* STRUCT IDENTIFIER LBRACE varDeclaration* RBRACE */ |
| 273 std::unique_ptr<ASTType> Parser::structDeclaration() { |
| 274 if (!this->expect(Token::STRUCT, "'struct'")) { |
| 275 return nullptr; |
| 276 } |
| 277 Token name; |
| 278 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) { |
| 279 return nullptr; |
| 280 } |
| 281 if (!this->expect(Token::LBRACE, "'{'")) { |
| 282 return nullptr; |
| 283 } |
| 284 std::vector<Type::Field> fields; |
| 285 while (this->peek().fKind != Token::RBRACE) { |
| 286 std::unique_ptr<ASTVarDeclaration> decl = this->varDeclaration(); |
| 287 if (!decl) { |
| 288 return nullptr; |
| 289 } |
| 290 for (size_t i = 0; i < decl->fNames.size(); i++) { |
| 291 auto type = std::static_pointer_cast<Type>(fTypes[decl->fType->fName
]); |
| 292 for (int j = (int) decl->fSizes[i].size() - 1; j >= 0; j--) { |
| 293 if (decl->fSizes[i][j]->fKind == ASTExpression::kInt_Kind) { |
| 294 this->error(decl->fPosition, "array size in struct field mus
t be a constant"); |
| 295 } |
| 296 uint64_t columns = ((ASTIntLiteral&) *decl->fSizes[i][j]).fValue
; |
| 297 std::string name = type->name() + "[" + to_string(columns) + "]"
; |
| 298 type = std::shared_ptr<Type>(new Type(name, Type::kArray_Kind, s
td::move(type), |
| 299 (int) columns)); |
| 300 } |
| 301 fields.push_back(Type::Field(decl->fModifiers, decl->fNames[i], std:
:move(type))); |
| 302 if (decl->fValues[i]) { |
| 303 this->error(decl->fPosition, "initializers are not permitted on
struct fields"); |
| 304 } |
| 305 } |
| 306 } |
| 307 if (!this->expect(Token::RBRACE, "'}'")) { |
| 308 return nullptr; |
| 309 } |
| 310 std::shared_ptr<Type> type(new Type(name.fText, fields)); |
| 311 fTypes.add(type->fName, type); |
| 312 return std::unique_ptr<ASTType>(new ASTType(name.fPosition, type->fName, |
| 313 ASTType::kStruct_Kind)); |
| 314 } |
| 315 |
| 316 /* structDeclaration ((IDENTIFIER varDeclarationEnd) | SEMICOLON) */ |
| 317 std::unique_ptr<ASTVarDeclaration> Parser::structVarDeclaration(ASTModifiers mod
ifiers) { |
| 318 std::unique_ptr<ASTType> type = this->structDeclaration(); |
| 319 if (!type) { |
| 320 return nullptr; |
| 321 } |
| 322 if (peek().fKind == Token::IDENTIFIER) { |
| 323 Token name = this->nextToken(); |
| 324 std::unique_ptr<ASTVarDeclaration> result = this->varDeclarationEnd(modi
fiers, |
| 325 std:
:move(type), |
| 326 std:
:move(name.fText)); |
| 327 if (result) { |
| 328 for (size_t i = 0; i < result->fValues.size(); i++) { |
| 329 if (result->fValues[i]) { |
| 330 this->error(result->fValues[i]->fPosition, |
| 331 "struct variables cannot be initialized"); |
| 332 } |
| 333 } |
| 334 } |
| 335 return result; |
| 336 } |
| 337 this->expect(Token::SEMICOLON, "';'"); |
| 338 return nullptr; |
| 339 } |
| 340 |
| 341 /* (LBRACKET expression? RBRACKET)* (EQ expression)? (COMMA IDENTIFER |
| 342 (LBRACKET expression? RBRACKET)* (EQ expression)?)* SEMICOLON */ |
| 343 std::unique_ptr<ASTVarDeclaration> Parser::varDeclarationEnd(ASTModifiers mods, |
| 344 std::unique_ptr<AST
Type> type, |
| 345 std::string name) { |
| 346 std::vector<std::string> names; |
| 347 std::vector<std::vector<std::unique_ptr<ASTExpression>>> sizes; |
| 348 names.push_back(name); |
| 349 std::vector<std::unique_ptr<ASTExpression>> currentVarSizes; |
| 350 while (this->peek().fKind == Token::LBRACKET) { |
| 351 this->nextToken(); |
| 352 if (this->peek().fKind == Token::RBRACKET) { |
| 353 this->nextToken(); |
| 354 currentVarSizes.push_back(nullptr); |
| 355 } else { |
| 356 std::unique_ptr<ASTExpression> size(this->expression()); |
| 357 if (!size) { |
| 358 return nullptr; |
| 359 } |
| 360 currentVarSizes.push_back(std::move(size)); |
| 361 if (!this->expect(Token::RBRACKET, "']'")) { |
| 362 return nullptr; |
| 363 } |
| 364 } |
| 365 } |
| 366 sizes.push_back(std::move(currentVarSizes)); |
| 367 std::vector<std::unique_ptr<ASTExpression>> values; |
| 368 if (this->peek().fKind == Token::EQ) { |
| 369 this->nextToken(); |
| 370 std::unique_ptr<ASTExpression> value(this->expression()); |
| 371 if (!value) { |
| 372 return nullptr; |
| 373 } |
| 374 values.push_back(std::move(value)); |
| 375 } else { |
| 376 values.push_back(nullptr); |
| 377 } |
| 378 while (this->peek().fKind == Token::COMMA) { |
| 379 this->nextToken(); |
| 380 Token name; |
| 381 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) { |
| 382 return nullptr; |
| 383 } |
| 384 names.push_back(name.fText); |
| 385 currentVarSizes.clear(); |
| 386 while (this->peek().fKind == Token::LBRACKET) { |
| 387 this->nextToken(); |
| 388 if (this->peek().fKind == Token::RBRACKET) { |
| 389 this->nextToken(); |
| 390 currentVarSizes.push_back(nullptr); |
| 391 } else { |
| 392 std::unique_ptr<ASTExpression> size(this->expression()); |
| 393 if (!size) { |
| 394 return nullptr; |
| 395 } |
| 396 currentVarSizes.push_back(std::move(size)); |
| 397 if (!this->expect(Token::RBRACKET, "']'")) { |
| 398 return nullptr; |
| 399 } |
| 400 } |
| 401 } |
| 402 sizes.push_back(std::move(currentVarSizes)); |
| 403 if (this->peek().fKind == Token::EQ) { |
| 404 this->nextToken(); |
| 405 std::unique_ptr<ASTExpression> value(this->expression()); |
| 406 if (!value) { |
| 407 return nullptr; |
| 408 } |
| 409 values.push_back(std::move(value)); |
| 410 } else { |
| 411 values.push_back(nullptr); |
| 412 } |
| 413 } |
| 414 if (!this->expect(Token::SEMICOLON, "';'")) { |
| 415 return nullptr; |
| 416 } |
| 417 return std::unique_ptr<ASTVarDeclaration>(new ASTVarDeclaration(std::move(mo
ds), |
| 418 std::move(ty
pe), |
| 419 std::move(na
mes), |
| 420 std::move(si
zes), |
| 421 std::move(va
lues))); |
| 422 } |
| 423 |
| 424 /* modifiers type IDENTIFIER (LBRACKET INT_LITERAL RBRACKET)? */ |
| 425 std::unique_ptr<ASTParameter> Parser::parameter() { |
| 426 ASTModifiers modifiers = this->modifiersWithDefaults(ASTModifiers::kIn_Flag)
; |
| 427 std::unique_ptr<ASTType> type = this->type(); |
| 428 if (!type) { |
| 429 return nullptr; |
| 430 } |
| 431 Token name; |
| 432 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) { |
| 433 return nullptr; |
| 434 } |
| 435 std::vector<int> sizes; |
| 436 while (this->peek().fKind == Token::LBRACKET) { |
| 437 this->nextToken(); |
| 438 Token sizeToken; |
| 439 if (!this->expect(Token::INT_LITERAL, "a positive integer", &sizeToken))
{ |
| 440 return nullptr; |
| 441 } |
| 442 sizes.push_back(SkSL::stoi(sizeToken.fText)); |
| 443 if (!this->expect(Token::RBRACKET, "']'")) { |
| 444 return nullptr; |
| 445 } |
| 446 } |
| 447 return std::unique_ptr<ASTParameter>(new ASTParameter(name.fPosition, modifi
ers, |
| 448 std::move(type), name.
fText, |
| 449 std::move(sizes))); |
| 450 } |
| 451 |
| 452 /** (EQ INT_LITERAL)? */ |
| 453 int Parser::layoutInt() { |
| 454 if (!this->expect(Token::EQ, "'='")) { |
| 455 return -1; |
| 456 } |
| 457 Token resultToken; |
| 458 if (this->expect(Token::INT_LITERAL, "a non-negative integer", &resultToken)
) { |
| 459 return SkSL::stoi(resultToken.fText); |
| 460 } |
| 461 return -1; |
| 462 } |
| 463 |
| 464 /* LAYOUT LPAREN IDENTIFIER EQ INT_LITERAL (COMMA IDENTIFIER EQ INT_LITERAL)* |
| 465 RPAREN */ |
| 466 ASTLayout Parser::layout() { |
| 467 int location = -1; |
| 468 int binding = -1; |
| 469 int index = -1; |
| 470 int set = -1; |
| 471 int builtin = -1; |
| 472 if (this->peek().fKind == Token::LAYOUT) { |
| 473 this->nextToken(); |
| 474 if (!this->expect(Token::LPAREN, "'('")) { |
| 475 return ASTLayout(location, binding, index, set, builtin); |
| 476 } |
| 477 for (;;) { |
| 478 Token t = this->nextToken(); |
| 479 if (t.fText == "location") { |
| 480 location = this->layoutInt(); |
| 481 } else if (t.fText == "binding") { |
| 482 binding = this->layoutInt(); |
| 483 } else if (t.fText == "index") { |
| 484 index = this->layoutInt(); |
| 485 } else if (t.fText == "set") { |
| 486 set = this->layoutInt(); |
| 487 } else if (t.fText == "builtin") { |
| 488 builtin = this->layoutInt(); |
| 489 } else { |
| 490 this->error(t.fPosition, ("'" + t.fText + |
| 491 "' is not a valid layout qualifier").c
_str()); |
| 492 } |
| 493 if (this->peek().fKind == Token::RPAREN) { |
| 494 this->nextToken(); |
| 495 break; |
| 496 } |
| 497 if (!this->expect(Token::COMMA, "','")) { |
| 498 break; |
| 499 } |
| 500 } |
| 501 } |
| 502 return ASTLayout(location, binding, index, set, builtin); |
| 503 } |
| 504 |
| 505 /* layout? (UNIFORM | CONST | IN | OUT | INOUT | LOWP | |
| 506 MEDIUMP | HIGHP)* */ |
| 507 ASTModifiers Parser::modifiers() { |
| 508 ASTLayout layout = this->layout(); |
| 509 int flags = 0; |
| 510 for (;;) { |
| 511 // TODO: handle duplicate / incompatible flags |
| 512 switch (peek().fKind) { |
| 513 case Token::UNIFORM: |
| 514 this->nextToken(); |
| 515 flags |= ASTModifiers::kUniform_Flag; |
| 516 break; |
| 517 case Token::CONST: |
| 518 this->nextToken(); |
| 519 flags |= ASTModifiers::kConst_Flag; |
| 520 break; |
| 521 case Token::IN: |
| 522 this->nextToken(); |
| 523 flags |= ASTModifiers::kIn_Flag; |
| 524 break; |
| 525 case Token::OUT: |
| 526 this->nextToken(); |
| 527 flags |= ASTModifiers::kOut_Flag; |
| 528 break; |
| 529 case Token::INOUT: |
| 530 this->nextToken(); |
| 531 flags |= ASTModifiers::kIn_Flag; |
| 532 flags |= ASTModifiers::kOut_Flag; |
| 533 break; |
| 534 case Token::LOWP: |
| 535 this->nextToken(); |
| 536 flags |= ASTModifiers::kLowp_Flag; |
| 537 break; |
| 538 case Token::MEDIUMP: |
| 539 this->nextToken(); |
| 540 flags |= ASTModifiers::kMediump_Flag; |
| 541 break; |
| 542 case Token::HIGHP: |
| 543 this->nextToken(); |
| 544 flags |= ASTModifiers::kHighp_Flag; |
| 545 break; |
| 546 default: |
| 547 return ASTModifiers(layout, flags); |
| 548 } |
| 549 } |
| 550 } |
| 551 |
| 552 ASTModifiers Parser::modifiersWithDefaults(int defaultFlags) { |
| 553 ASTModifiers result = this->modifiers(); |
| 554 if (!result.fFlags) { |
| 555 return ASTModifiers(result.fLayout, defaultFlags); |
| 556 } |
| 557 return result; |
| 558 } |
| 559 |
| 560 /* ifStatement | forStatement | doStatement | whileStatement | block | expressio
n */ |
| 561 std::unique_ptr<ASTStatement> Parser::statement() { |
| 562 Token start = this->peek(); |
| 563 switch (start.fKind) { |
| 564 case Token::IF: |
| 565 return this->ifStatement(); |
| 566 case Token::FOR: |
| 567 return this->forStatement(); |
| 568 case Token::DO: |
| 569 return this->doStatement(); |
| 570 case Token::WHILE: |
| 571 return this->whileStatement(); |
| 572 case Token::RETURN: |
| 573 return this->returnStatement(); |
| 574 case Token::BREAK: |
| 575 return this->breakStatement(); |
| 576 case Token::CONTINUE: |
| 577 return this->continueStatement(); |
| 578 case Token::DISCARD: |
| 579 return this->discardStatement(); |
| 580 case Token::LBRACE: |
| 581 return this->block(); |
| 582 case Token::SEMICOLON: |
| 583 this->nextToken(); |
| 584 return std::unique_ptr<ASTStatement>(new ASTBlock(start.fPosition, {
})); |
| 585 case Token::CONST: // fall through |
| 586 case Token::HIGHP: // fall through |
| 587 case Token::MEDIUMP: // fall through |
| 588 case Token::LOWP: { |
| 589 auto decl = this->varDeclaration(); |
| 590 if (!decl) { |
| 591 return nullptr; |
| 592 } |
| 593 return std::unique_ptr<ASTStatement>(new ASTVarDeclarationStatement(
std::move(decl))); |
| 594 } |
| 595 case Token::IDENTIFIER: |
| 596 if (this->isType(start.fText)) { |
| 597 auto decl = this->varDeclaration(); |
| 598 if (!decl) { |
| 599 return nullptr; |
| 600 } |
| 601 return std::unique_ptr<ASTStatement>(new ASTVarDeclarationStatem
ent( |
| 602
std::move(decl))); |
| 603 } |
| 604 // fall through |
| 605 default: |
| 606 return this->expressionStatement(); |
| 607 } |
| 608 } |
| 609 |
| 610 /* IDENTIFIER(type) */ |
| 611 std::unique_ptr<ASTType> Parser::type() { |
| 612 Token type; |
| 613 if (!this->expect(Token::IDENTIFIER, "a type", &type)) { |
| 614 return nullptr; |
| 615 } |
| 616 if (!this->isType(type.fText)) { |
| 617 this->error(type.fPosition, ("no type named '" + type.fText + "'").c_str
()); |
| 618 return nullptr; |
| 619 } |
| 620 return std::unique_ptr<ASTType>(new ASTType(type.fPosition, std::move(type.f
Text), |
| 621 ASTType::kIdentifier_Kind)); |
| 622 } |
| 623 |
| 624 /* IDENTIFIER LBRACE varDeclaration* RBRACE */ |
| 625 std::unique_ptr<ASTDeclaration> Parser::interfaceBlock(ASTModifiers mods) { |
| 626 Token name; |
| 627 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) { |
| 628 return nullptr; |
| 629 } |
| 630 if (peek().fKind != Token::LBRACE) { |
| 631 // we only get into interfaceBlock if we found a top-level identifier wh
ich was not a type. |
| 632 // 99% of the time, the user was not actually intending to create an int
erface block, so |
| 633 // it's better to report it as an unknown type |
| 634 this->error(name.fPosition, "no type named '" + name.fText + "'"); |
| 635 return nullptr; |
| 636 } |
| 637 this->nextToken(); |
| 638 std::vector<std::unique_ptr<ASTVarDeclaration>> decls; |
| 639 while (this->peek().fKind != Token::RBRACE) { |
| 640 std::unique_ptr<ASTVarDeclaration> decl = this->varDeclaration(); |
| 641 if (!decl) { |
| 642 return nullptr; |
| 643 } |
| 644 decls.push_back(std::move(decl)); |
| 645 } |
| 646 this->nextToken(); |
| 647 std::string valueName; |
| 648 if (this->peek().fKind == Token::IDENTIFIER) { |
| 649 valueName = this->nextToken().fText; |
| 650 } |
| 651 this->expect(Token::SEMICOLON, "';'"); |
| 652 return std::unique_ptr<ASTDeclaration>(new ASTInterfaceBlock(name.fPosition,
mods, |
| 653 name.fText, std
::move(valueName), |
| 654 std::move(decls
))); |
| 655 } |
| 656 |
| 657 /* IF LPAREN expression RPAREN statement (ELSE statement)? */ |
| 658 std::unique_ptr<ASTIfStatement> Parser::ifStatement() { |
| 659 Token start; |
| 660 if (!this->expect(Token::IF, "'if'", &start)) { |
| 661 return nullptr; |
| 662 } |
| 663 if (!this->expect(Token::LPAREN, "'('")) { |
| 664 return nullptr; |
| 665 } |
| 666 std::unique_ptr<ASTExpression> test(this->expression()); |
| 667 if (!test) { |
| 668 return nullptr; |
| 669 } |
| 670 if (!this->expect(Token::RPAREN, "')'")) { |
| 671 return nullptr; |
| 672 } |
| 673 std::unique_ptr<ASTStatement> ifTrue(this->statement()); |
| 674 if (!ifTrue) { |
| 675 return nullptr; |
| 676 } |
| 677 std::unique_ptr<ASTStatement> ifFalse; |
| 678 if (this->peek().fKind == Token::ELSE) { |
| 679 this->nextToken(); |
| 680 ifFalse = this->statement(); |
| 681 if (!ifFalse) { |
| 682 return nullptr; |
| 683 } |
| 684 } |
| 685 return std::unique_ptr<ASTIfStatement>(new ASTIfStatement(start.fPosition, s
td::move(test), |
| 686 std::move(ifTrue),
|
| 687 std::move(ifFalse)
)); |
| 688 } |
| 689 |
| 690 /* DO statement WHILE LPAREN expression RPAREN SEMICOLON */ |
| 691 std::unique_ptr<ASTDoStatement> Parser::doStatement() { |
| 692 Token start; |
| 693 if (!this->expect(Token::DO, "'do'", &start)) { |
| 694 return nullptr; |
| 695 } |
| 696 std::unique_ptr<ASTStatement> statement(this->statement()); |
| 697 if (!statement) { |
| 698 return nullptr; |
| 699 } |
| 700 if (!this->expect(Token::WHILE, "'while'")) { |
| 701 return nullptr; |
| 702 } |
| 703 if (!this->expect(Token::LPAREN, "'('")) { |
| 704 return nullptr; |
| 705 } |
| 706 std::unique_ptr<ASTExpression> test(this->expression()); |
| 707 if (!test) { |
| 708 return nullptr; |
| 709 } |
| 710 if (!this->expect(Token::RPAREN, "')'")) { |
| 711 return nullptr; |
| 712 } |
| 713 if (!this->expect(Token::SEMICOLON, "';'")) { |
| 714 return nullptr; |
| 715 } |
| 716 return std::unique_ptr<ASTDoStatement>(new ASTDoStatement(start.fPosition, |
| 717 std::move(statemen
t), |
| 718 std::move(test))); |
| 719 } |
| 720 |
| 721 /* WHILE LPAREN expression RPAREN STATEMENT */ |
| 722 std::unique_ptr<ASTWhileStatement> Parser::whileStatement() { |
| 723 Token start; |
| 724 if (!this->expect(Token::WHILE, "'while'", &start)) { |
| 725 return nullptr; |
| 726 } |
| 727 if (!this->expect(Token::LPAREN, "'('")) { |
| 728 return nullptr; |
| 729 } |
| 730 std::unique_ptr<ASTExpression> test(this->expression()); |
| 731 if (!test) { |
| 732 return nullptr; |
| 733 } |
| 734 if (!this->expect(Token::RPAREN, "')'")) { |
| 735 return nullptr; |
| 736 } |
| 737 std::unique_ptr<ASTStatement> statement(this->statement()); |
| 738 if (!statement) { |
| 739 return nullptr; |
| 740 } |
| 741 return std::unique_ptr<ASTWhileStatement>(new ASTWhileStatement(start.fPosit
ion, |
| 742 std::move(te
st), |
| 743 std::move(st
atement))); |
| 744 } |
| 745 |
| 746 /* FOR LPAREN (declaration | expression)? SEMICOLON expression? SEMICOLON expres
sion? RPAREN |
| 747 STATEMENT */ |
| 748 std::unique_ptr<ASTForStatement> Parser::forStatement() { |
| 749 Token start; |
| 750 if (!this->expect(Token::FOR, "'for'", &start)) { |
| 751 return nullptr; |
| 752 } |
| 753 if (!this->expect(Token::LPAREN, "'('")) { |
| 754 return nullptr; |
| 755 } |
| 756 std::unique_ptr<ASTStatement> initializer; |
| 757 Token nextToken = this->peek(); |
| 758 switch (nextToken.fKind) { |
| 759 case Token::SEMICOLON: |
| 760 break; |
| 761 case Token::CONST: |
| 762 initializer = std::unique_ptr<ASTStatement>(new ASTVarDeclarationSta
tement( |
| 763 this-
>varDeclaration())); |
| 764 break; |
| 765 case Token::IDENTIFIER: |
| 766 if (this->isType(nextToken.fText)) { |
| 767 initializer = std::unique_ptr<ASTStatement>(new ASTVarDeclaratio
nStatement( |
| 768 this-
>varDeclaration())); |
| 769 break; |
| 770 } |
| 771 // fall through |
| 772 default: |
| 773 initializer = this->expressionStatement(); |
| 774 } |
| 775 std::unique_ptr<ASTExpression> test; |
| 776 if (this->peek().fKind != Token::SEMICOLON) { |
| 777 test = this->expression(); |
| 778 if (!test) { |
| 779 return nullptr; |
| 780 } |
| 781 } |
| 782 if (!this->expect(Token::SEMICOLON, "';'")) { |
| 783 return nullptr; |
| 784 } |
| 785 std::unique_ptr<ASTExpression> next; |
| 786 if (this->peek().fKind != Token::SEMICOLON) { |
| 787 next = this->expression(); |
| 788 if (!next) { |
| 789 return nullptr; |
| 790 } |
| 791 } |
| 792 if (!this->expect(Token::RPAREN, "')'")) { |
| 793 return nullptr; |
| 794 } |
| 795 std::unique_ptr<ASTStatement> statement(this->statement()); |
| 796 if (!statement) { |
| 797 return nullptr; |
| 798 } |
| 799 return std::unique_ptr<ASTForStatement>(new ASTForStatement(start.fPosition,
|
| 800 std::move(initia
lizer), |
| 801 std::move(test),
std::move(next), |
| 802 std::move(statem
ent))); |
| 803 } |
| 804 |
| 805 /* RETURN expression? SEMICOLON */ |
| 806 std::unique_ptr<ASTReturnStatement> Parser::returnStatement() { |
| 807 Token start; |
| 808 if (!this->expect(Token::RETURN, "'return'", &start)) { |
| 809 return nullptr; |
| 810 } |
| 811 std::unique_ptr<ASTExpression> expression; |
| 812 if (this->peek().fKind != Token::SEMICOLON) { |
| 813 expression = this->expression(); |
| 814 if (!expression) { |
| 815 return nullptr; |
| 816 } |
| 817 } |
| 818 if (!this->expect(Token::SEMICOLON, "';'")) { |
| 819 return nullptr; |
| 820 } |
| 821 return std::unique_ptr<ASTReturnStatement>(new ASTReturnStatement(start.fPos
ition, |
| 822 std::move(
expression))); |
| 823 } |
| 824 |
| 825 /* BREAK SEMICOLON */ |
| 826 std::unique_ptr<ASTBreakStatement> Parser::breakStatement() { |
| 827 Token start; |
| 828 if (!this->expect(Token::BREAK, "'break'", &start)) { |
| 829 return nullptr; |
| 830 } |
| 831 if (!this->expect(Token::SEMICOLON, "';'")) { |
| 832 return nullptr; |
| 833 } |
| 834 return std::unique_ptr<ASTBreakStatement>(new ASTBreakStatement(start.fPosit
ion)); |
| 835 } |
| 836 |
| 837 /* CONTINUE SEMICOLON */ |
| 838 std::unique_ptr<ASTContinueStatement> Parser::continueStatement() { |
| 839 Token start; |
| 840 if (!this->expect(Token::CONTINUE, "'continue'", &start)) { |
| 841 return nullptr; |
| 842 } |
| 843 if (!this->expect(Token::SEMICOLON, "';'")) { |
| 844 return nullptr; |
| 845 } |
| 846 return std::unique_ptr<ASTContinueStatement>(new ASTContinueStatement(start.
fPosition)); |
| 847 } |
| 848 |
| 849 /* DISCARD SEMICOLON */ |
| 850 std::unique_ptr<ASTDiscardStatement> Parser::discardStatement() { |
| 851 Token start; |
| 852 if (!this->expect(Token::DISCARD, "'continue'", &start)) { |
| 853 return nullptr; |
| 854 } |
| 855 if (!this->expect(Token::SEMICOLON, "';'")) { |
| 856 return nullptr; |
| 857 } |
| 858 return std::unique_ptr<ASTDiscardStatement>(new ASTDiscardStatement(start.fP
osition)); |
| 859 } |
| 860 |
| 861 /* LBRACE statement* RBRACE */ |
| 862 std::unique_ptr<ASTBlock> Parser::block() { |
| 863 Token start; |
| 864 if (!this->expect(Token::LBRACE, "'{'", &start)) { |
| 865 return nullptr; |
| 866 } |
| 867 std::vector<std::unique_ptr<ASTStatement>> statements; |
| 868 for (;;) { |
| 869 switch (this->peek().fKind) { |
| 870 case Token::RBRACE: |
| 871 this->nextToken(); |
| 872 return std::unique_ptr<ASTBlock>(new ASTBlock(start.fPosition, |
| 873 std::move(statemen
ts))); |
| 874 case Token::END_OF_FILE: |
| 875 this->error(this->peek().fPosition, "expected '}', but found end
of file"); |
| 876 return nullptr; |
| 877 default: { |
| 878 std::unique_ptr<ASTStatement> statement = this->statement(); |
| 879 if (!statement) { |
| 880 return nullptr; |
| 881 } |
| 882 statements.push_back(std::move(statement)); |
| 883 } |
| 884 } |
| 885 } |
| 886 } |
| 887 |
| 888 /* expression SEMICOLON */ |
| 889 std::unique_ptr<ASTExpressionStatement> Parser::expressionStatement() { |
| 890 std::unique_ptr<ASTExpression> expr = this->expression(); |
| 891 if (expr) { |
| 892 if (this->expect(Token::SEMICOLON, "';'")) { |
| 893 ASTExpressionStatement* result = new ASTExpressionStatement(std::mov
e(expr)); |
| 894 return std::unique_ptr<ASTExpressionStatement>(result); |
| 895 } |
| 896 } |
| 897 return nullptr; |
| 898 } |
| 899 |
| 900 /* assignmentExpression */ |
| 901 std::unique_ptr<ASTExpression> Parser::expression() { |
| 902 return this->assignmentExpression(); |
| 903 } |
| 904 |
| 905 /* ternaryExpression ((EQEQ | STAREQ | SLASHEQ | PERCENTEQ | PLUSEQ | MINUSEQ |
SHLEQ | SHREQ | |
| 906 BITWISEANDEQ | BITWISEXOREQ | BITWISEOREQ | LOGICALANDEQ | LOGICALXOREQ | LOG
ICALOREQ) |
| 907 assignmentExpression)* |
| 908 */ |
| 909 std::unique_ptr<ASTExpression> Parser::assignmentExpression() { |
| 910 std::unique_ptr<ASTExpression> result = this->ternaryExpression(); |
| 911 if (!result) { |
| 912 return nullptr; |
| 913 } |
| 914 for (;;) { |
| 915 switch (this->peek().fKind) { |
| 916 case Token::EQ: // fall through |
| 917 case Token::STAREQ: // fall through |
| 918 case Token::SLASHEQ: // fall through |
| 919 case Token::PERCENTEQ: // fall through |
| 920 case Token::PLUSEQ: // fall through |
| 921 case Token::MINUSEQ: // fall through |
| 922 case Token::SHLEQ: // fall through |
| 923 case Token::SHREQ: // fall through |
| 924 case Token::BITWISEANDEQ: // fall through |
| 925 case Token::BITWISEXOREQ: // fall through |
| 926 case Token::BITWISEOREQ: // fall through |
| 927 case Token::LOGICALANDEQ: // fall through |
| 928 case Token::LOGICALXOREQ: // fall through |
| 929 case Token::LOGICALOREQ: { |
| 930 Token t = this->nextToken(); |
| 931 std::unique_ptr<ASTExpression> right = this->assignmentExpressio
n(); |
| 932 if (!right) { |
| 933 return nullptr; |
| 934 } |
| 935 result = std::unique_ptr<ASTExpression>(new ASTBinaryExpression(
std::move(result), |
| 936
t, |
| 937
std::move(right))); |
| 938 } |
| 939 default: |
| 940 return result; |
| 941 } |
| 942 } |
| 943 } |
| 944 |
| 945 /* logicalOrExpression ('?' expression ':' assignmentExpression)? */ |
| 946 std::unique_ptr<ASTExpression> Parser::ternaryExpression() { |
| 947 std::unique_ptr<ASTExpression> result = this->logicalOrExpression(); |
| 948 if (!result) { |
| 949 return nullptr; |
| 950 } |
| 951 if (this->peek().fKind == Token::QUESTION) { |
| 952 Token question = this->nextToken(); |
| 953 std::unique_ptr<ASTExpression> trueExpr = this->expression(); |
| 954 if (!trueExpr) { |
| 955 return nullptr; |
| 956 } |
| 957 if (this->expect(Token::COLON, "':'")) { |
| 958 std::unique_ptr<ASTExpression> falseExpr = this->assignmentExpressio
n(); |
| 959 return std::unique_ptr<ASTExpression>(new ASTTernaryExpression(std::
move(result), |
| 960 std::
move(trueExpr), |
| 961 std::
move(falseExpr))); |
| 962 } |
| 963 return nullptr; |
| 964 } |
| 965 return result; |
| 966 } |
| 967 |
| 968 /* logicalXorExpression (LOGICALOR logicalXorExpression)* */ |
| 969 std::unique_ptr<ASTExpression> Parser::logicalOrExpression() { |
| 970 std::unique_ptr<ASTExpression> result = this->logicalXorExpression(); |
| 971 if (!result) { |
| 972 return nullptr; |
| 973 } |
| 974 while (this->peek().fKind == Token::LOGICALOR) { |
| 975 Token t = this->nextToken(); |
| 976 std::unique_ptr<ASTExpression> right = this->logicalXorExpression(); |
| 977 if (!right) { |
| 978 return nullptr; |
| 979 } |
| 980 result.reset(new ASTBinaryExpression(std::move(result), t, std::move(rig
ht))); |
| 981 } |
| 982 return result; |
| 983 } |
| 984 |
| 985 /* logicalAndExpression (LOGICALXOR logicalAndExpression)* */ |
| 986 std::unique_ptr<ASTExpression> Parser::logicalXorExpression() { |
| 987 std::unique_ptr<ASTExpression> result = this->logicalAndExpression(); |
| 988 if (!result) { |
| 989 return nullptr; |
| 990 } |
| 991 while (this->peek().fKind == Token::LOGICALXOR) { |
| 992 Token t = this->nextToken(); |
| 993 std::unique_ptr<ASTExpression> right = this->logicalAndExpression(); |
| 994 if (!right) { |
| 995 return nullptr; |
| 996 } |
| 997 result.reset(new ASTBinaryExpression(std::move(result), t, std::move(rig
ht))); |
| 998 } |
| 999 return result; |
| 1000 } |
| 1001 |
| 1002 /* bitwiseOrExpression (LOGICALAND bitwiseOrExpression)* */ |
| 1003 std::unique_ptr<ASTExpression> Parser::logicalAndExpression() { |
| 1004 std::unique_ptr<ASTExpression> result = this->bitwiseOrExpression(); |
| 1005 if (!result) { |
| 1006 return nullptr; |
| 1007 } |
| 1008 while (this->peek().fKind == Token::LOGICALAND) { |
| 1009 Token t = this->nextToken(); |
| 1010 std::unique_ptr<ASTExpression> right = this->bitwiseOrExpression(); |
| 1011 if (!right) { |
| 1012 return nullptr; |
| 1013 } |
| 1014 result.reset(new ASTBinaryExpression(std::move(result), t, std::move(rig
ht))); |
| 1015 } |
| 1016 return result; |
| 1017 } |
| 1018 |
| 1019 /* bitwiseXorExpression (BITWISEOR bitwiseXorExpression)* */ |
| 1020 std::unique_ptr<ASTExpression> Parser::bitwiseOrExpression() { |
| 1021 std::unique_ptr<ASTExpression> result = this->bitwiseXorExpression(); |
| 1022 if (!result) { |
| 1023 return nullptr; |
| 1024 } |
| 1025 while (this->peek().fKind == Token::BITWISEOR) { |
| 1026 Token t = this->nextToken(); |
| 1027 std::unique_ptr<ASTExpression> right = this->bitwiseXorExpression(); |
| 1028 if (!right) { |
| 1029 return nullptr; |
| 1030 } |
| 1031 result.reset(new ASTBinaryExpression(std::move(result), t, std::move(rig
ht))); |
| 1032 } |
| 1033 return result; |
| 1034 } |
| 1035 |
| 1036 /* bitwiseAndExpression (BITWISEXOR bitwiseAndExpression)* */ |
| 1037 std::unique_ptr<ASTExpression> Parser::bitwiseXorExpression() { |
| 1038 std::unique_ptr<ASTExpression> result = this->bitwiseAndExpression(); |
| 1039 if (!result) { |
| 1040 return nullptr; |
| 1041 } |
| 1042 while (this->peek().fKind == Token::BITWISEXOR) { |
| 1043 Token t = this->nextToken(); |
| 1044 std::unique_ptr<ASTExpression> right = this->bitwiseAndExpression(); |
| 1045 if (!right) { |
| 1046 return nullptr; |
| 1047 } |
| 1048 result.reset(new ASTBinaryExpression(std::move(result), t, std::move(rig
ht))); |
| 1049 } |
| 1050 return result; |
| 1051 } |
| 1052 |
| 1053 /* equalityExpression (BITWISEAND equalityExpression)* */ |
| 1054 std::unique_ptr<ASTExpression> Parser::bitwiseAndExpression() { |
| 1055 std::unique_ptr<ASTExpression> result = this->equalityExpression(); |
| 1056 if (!result) { |
| 1057 return nullptr; |
| 1058 } |
| 1059 while (this->peek().fKind == Token::BITWISEAND) { |
| 1060 Token t = this->nextToken(); |
| 1061 std::unique_ptr<ASTExpression> right = this->equalityExpression(); |
| 1062 if (!right) { |
| 1063 return nullptr; |
| 1064 } |
| 1065 result.reset(new ASTBinaryExpression(std::move(result), t, std::move(rig
ht))); |
| 1066 } |
| 1067 return result; |
| 1068 } |
| 1069 |
| 1070 /* relationalExpression ((EQEQ | NEQ) relationalExpression)* */ |
| 1071 std::unique_ptr<ASTExpression> Parser::equalityExpression() { |
| 1072 std::unique_ptr<ASTExpression> result = this->relationalExpression(); |
| 1073 if (!result) { |
| 1074 return nullptr; |
| 1075 } |
| 1076 for (;;) { |
| 1077 switch (this->peek().fKind) { |
| 1078 case Token::EQEQ: // fall through |
| 1079 case Token::NEQ: { |
| 1080 Token t = this->nextToken(); |
| 1081 std::unique_ptr<ASTExpression> right = this->relationalExpressio
n(); |
| 1082 if (!right) { |
| 1083 return nullptr; |
| 1084 } |
| 1085 result.reset(new ASTBinaryExpression(std::move(result), t, std::
move(right))); |
| 1086 break; |
| 1087 } |
| 1088 default: |
| 1089 return result; |
| 1090 } |
| 1091 } |
| 1092 } |
| 1093 |
| 1094 /* shiftExpression ((LT | GT | LTEQ | GTEQ) shiftExpression)* */ |
| 1095 std::unique_ptr<ASTExpression> Parser::relationalExpression() { |
| 1096 std::unique_ptr<ASTExpression> result = this->shiftExpression(); |
| 1097 if (!result) { |
| 1098 return nullptr; |
| 1099 } |
| 1100 for (;;) { |
| 1101 switch (this->peek().fKind) { |
| 1102 case Token::LT: // fall through |
| 1103 case Token::GT: // fall through |
| 1104 case Token::LTEQ: // fall through |
| 1105 case Token::GTEQ: { |
| 1106 Token t = this->nextToken(); |
| 1107 std::unique_ptr<ASTExpression> right = this->shiftExpression(); |
| 1108 if (!right) { |
| 1109 return nullptr; |
| 1110 } |
| 1111 result.reset(new ASTBinaryExpression(std::move(result), t, std::
move(right))); |
| 1112 break; |
| 1113 } |
| 1114 default: |
| 1115 return result; |
| 1116 } |
| 1117 } |
| 1118 } |
| 1119 |
| 1120 /* additiveExpression ((SHL | SHR) additiveExpression)* */ |
| 1121 std::unique_ptr<ASTExpression> Parser::shiftExpression() { |
| 1122 std::unique_ptr<ASTExpression> result = this->additiveExpression(); |
| 1123 if (!result) { |
| 1124 return nullptr; |
| 1125 } |
| 1126 for (;;) { |
| 1127 switch (this->peek().fKind) { |
| 1128 case Token::SHL: // fall through |
| 1129 case Token::SHR: { |
| 1130 Token t = this->nextToken(); |
| 1131 std::unique_ptr<ASTExpression> right = this->additiveExpression(
); |
| 1132 if (!right) { |
| 1133 return nullptr; |
| 1134 } |
| 1135 result.reset(new ASTBinaryExpression(std::move(result), t, std::
move(right))); |
| 1136 break; |
| 1137 } |
| 1138 default: |
| 1139 return result; |
| 1140 } |
| 1141 } |
| 1142 } |
| 1143 |
| 1144 /* multiplicativeExpression ((PLUS | MINUS) multiplicativeExpression)* */ |
| 1145 std::unique_ptr<ASTExpression> Parser::additiveExpression() { |
| 1146 std::unique_ptr<ASTExpression> result = this->multiplicativeExpression(); |
| 1147 if (!result) { |
| 1148 return nullptr; |
| 1149 } |
| 1150 for (;;) { |
| 1151 switch (this->peek().fKind) { |
| 1152 case Token::PLUS: // fall through |
| 1153 case Token::MINUS: { |
| 1154 Token t = this->nextToken(); |
| 1155 std::unique_ptr<ASTExpression> right = this->multiplicativeExpre
ssion(); |
| 1156 if (!right) { |
| 1157 return nullptr; |
| 1158 } |
| 1159 result.reset(new ASTBinaryExpression(std::move(result), t, std::
move(right))); |
| 1160 break; |
| 1161 } |
| 1162 default: |
| 1163 return result; |
| 1164 } |
| 1165 } |
| 1166 } |
| 1167 |
| 1168 /* unaryExpression ((STAR | SLASH | PERCENT) unaryExpression)* */ |
| 1169 std::unique_ptr<ASTExpression> Parser::multiplicativeExpression() { |
| 1170 std::unique_ptr<ASTExpression> result = this->unaryExpression(); |
| 1171 if (!result) { |
| 1172 return nullptr; |
| 1173 } |
| 1174 for (;;) { |
| 1175 switch (this->peek().fKind) { |
| 1176 case Token::STAR: // fall through |
| 1177 case Token::SLASH: // fall through |
| 1178 case Token::PERCENT: { |
| 1179 Token t = this->nextToken(); |
| 1180 std::unique_ptr<ASTExpression> right = this->unaryExpression(); |
| 1181 if (!right) { |
| 1182 return nullptr; |
| 1183 } |
| 1184 result.reset(new ASTBinaryExpression(std::move(result), t, std::
move(right))); |
| 1185 break; |
| 1186 } |
| 1187 default: |
| 1188 return result; |
| 1189 } |
| 1190 } |
| 1191 } |
| 1192 |
| 1193 /* postfixExpression | (PLUS | MINUS | NOT | PLUSPLUS | MINUSMINUS) unaryExpress
ion */ |
| 1194 std::unique_ptr<ASTExpression> Parser::unaryExpression() { |
| 1195 switch (this->peek().fKind) { |
| 1196 case Token::PLUS: // fall through |
| 1197 case Token::MINUS: // fall through |
| 1198 case Token::NOT: // fall through |
| 1199 case Token::PLUSPLUS: // fall through |
| 1200 case Token::MINUSMINUS: { |
| 1201 Token t = this->nextToken(); |
| 1202 std::unique_ptr<ASTExpression> expr = this->unaryExpression(); |
| 1203 if (!expr) { |
| 1204 return nullptr; |
| 1205 } |
| 1206 return std::unique_ptr<ASTExpression>(new ASTPrefixExpression(t, std
::move(expr))); |
| 1207 } |
| 1208 default: |
| 1209 return this->postfixExpression(); |
| 1210 } |
| 1211 } |
| 1212 |
| 1213 /* term suffix* */ |
| 1214 std::unique_ptr<ASTExpression> Parser::postfixExpression() { |
| 1215 std::unique_ptr<ASTExpression> result = this->term(); |
| 1216 if (!result) { |
| 1217 return nullptr; |
| 1218 } |
| 1219 for (;;) { |
| 1220 switch (this->peek().fKind) { |
| 1221 case Token::LBRACKET: // fall through |
| 1222 case Token::DOT: // fall through |
| 1223 case Token::LPAREN: // fall through |
| 1224 case Token::PLUSPLUS: // fall through |
| 1225 case Token::MINUSMINUS: { |
| 1226 std::unique_ptr<ASTSuffix> s = this->suffix(); |
| 1227 if (!s) { |
| 1228 return nullptr; |
| 1229 } |
| 1230 result.reset(new ASTSuffixExpression(std::move(result), std::mov
e(s))); |
| 1231 break; |
| 1232 } |
| 1233 default: |
| 1234 return result; |
| 1235 } |
| 1236 } |
| 1237 } |
| 1238 |
| 1239 /* LBRACKET expression RBRACKET | DOT IDENTIFIER | LPAREN parameters RPAREN | |
| 1240 PLUSPLUS | MINUSMINUS */ |
| 1241 std::unique_ptr<ASTSuffix> Parser::suffix() { |
| 1242 Token next = this->nextToken(); |
| 1243 switch (next.fKind) { |
| 1244 case Token::LBRACKET: { |
| 1245 std::unique_ptr<ASTExpression> e = this->expression(); |
| 1246 if (!e) { |
| 1247 return nullptr; |
| 1248 } |
| 1249 this->expect(Token::RBRACKET, "']' to complete array access expressi
on"); |
| 1250 return std::unique_ptr<ASTSuffix>(new ASTIndexSuffix(std::move(e))); |
| 1251 } |
| 1252 case Token::DOT: { |
| 1253 Position pos = this->peek().fPosition; |
| 1254 std::string text; |
| 1255 if (this->identifier(&text)) { |
| 1256 return std::unique_ptr<ASTSuffix>(new ASTFieldSuffix(pos, std::m
ove(text))); |
| 1257 } |
| 1258 return nullptr; |
| 1259 } |
| 1260 case Token::LPAREN: { |
| 1261 std::vector<std::unique_ptr<ASTExpression>> parameters; |
| 1262 if (this->peek().fKind != Token::RPAREN) { |
| 1263 for (;;) { |
| 1264 std::unique_ptr<ASTExpression> expr = this->expression(); |
| 1265 if (!expr) { |
| 1266 return nullptr; |
| 1267 } |
| 1268 parameters.push_back(std::move(expr)); |
| 1269 if (this->peek().fKind != Token::COMMA) { |
| 1270 break; |
| 1271 } |
| 1272 this->nextToken(); |
| 1273 } |
| 1274 } |
| 1275 this->expect(Token::RPAREN, "')' to complete function parameters"); |
| 1276 return std::unique_ptr<ASTSuffix>(new ASTCallSuffix(next.fPosition, |
| 1277 std::move(parame
ters))); |
| 1278 } |
| 1279 case Token::PLUSPLUS: |
| 1280 return std::unique_ptr<ASTSuffix>(new ASTSuffix(next.fPosition, |
| 1281 ASTSuffix::kPostIncr
ement_Kind)); |
| 1282 case Token::MINUSMINUS: |
| 1283 return std::unique_ptr<ASTSuffix>(new ASTSuffix(next.fPosition, |
| 1284 ASTSuffix::kPostDecr
ement_Kind)); |
| 1285 default: { |
| 1286 this->error(next.fPosition, "expected expression suffix, but found
'" + next.fText + |
| 1287 "'\n"); |
| 1288 return nullptr; |
| 1289 } |
| 1290 } |
| 1291 } |
| 1292 |
| 1293 /* IDENTIFIER | intLiteral | floatLiteral | boolLiteral | '(' expression ')' */ |
| 1294 std::unique_ptr<ASTExpression> Parser::term() { |
| 1295 std::unique_ptr<ASTExpression> result; |
| 1296 Token t = this->peek(); |
| 1297 switch (t.fKind) { |
| 1298 case Token::IDENTIFIER: { |
| 1299 std::string text; |
| 1300 if (this->identifier(&text)) { |
| 1301 result.reset(new ASTIdentifier(t.fPosition, std::move(text))); |
| 1302 } |
| 1303 break; |
| 1304 } |
| 1305 case Token::INT_LITERAL: { |
| 1306 int64_t i; |
| 1307 if (this->intLiteral(&i)) { |
| 1308 result.reset(new ASTIntLiteral(t.fPosition, i)); |
| 1309 } |
| 1310 break; |
| 1311 } |
| 1312 case Token::FLOAT_LITERAL: { |
| 1313 double f; |
| 1314 if (this->floatLiteral(&f)) { |
| 1315 result.reset(new ASTFloatLiteral(t.fPosition, f)); |
| 1316 } |
| 1317 break; |
| 1318 } |
| 1319 case Token::TRUE_LITERAL: // fall through |
| 1320 case Token::FALSE_LITERAL: { |
| 1321 bool b; |
| 1322 if (this->boolLiteral(&b)) { |
| 1323 result.reset(new ASTBoolLiteral(t.fPosition, b)); |
| 1324 } |
| 1325 break; |
| 1326 } |
| 1327 case Token::LPAREN: { |
| 1328 this->nextToken(); |
| 1329 result = this->expression(); |
| 1330 if (result) { |
| 1331 this->expect(Token::RPAREN, "')' to complete expression"); |
| 1332 } |
| 1333 break; |
| 1334 } |
| 1335 default: |
| 1336 this->nextToken(); |
| 1337 this->error(t.fPosition, "expected expression, but found '" + t.fTe
xt + "'\n"); |
| 1338 result = nullptr; |
| 1339 } |
| 1340 return result; |
| 1341 } |
| 1342 |
| 1343 /* INT_LITERAL */ |
| 1344 bool Parser::intLiteral(int64_t* dest) { |
| 1345 Token t; |
| 1346 if (this->expect(Token::INT_LITERAL, "integer literal", &t)) { |
| 1347 *dest = SkSL::stol(t.fText); |
| 1348 return true; |
| 1349 } |
| 1350 return false; |
| 1351 } |
| 1352 |
| 1353 /* FLOAT_LITERAL */ |
| 1354 bool Parser::floatLiteral(double* dest) { |
| 1355 Token t; |
| 1356 if (this->expect(Token::FLOAT_LITERAL, "float literal", &t)) { |
| 1357 *dest = SkSL::stod(t.fText); |
| 1358 return true; |
| 1359 } |
| 1360 return false; |
| 1361 } |
| 1362 |
| 1363 /* TRUE_LITERAL | FALSE_LITERAL */ |
| 1364 bool Parser::boolLiteral(bool* dest) { |
| 1365 Token t = this->nextToken(); |
| 1366 switch (t.fKind) { |
| 1367 case Token::TRUE_LITERAL: |
| 1368 *dest = true; |
| 1369 return true; |
| 1370 case Token::FALSE_LITERAL: |
| 1371 *dest = false; |
| 1372 return true; |
| 1373 default: |
| 1374 this->error(t.fPosition, "expected 'true' or 'false', but found '" +
t.fText + "'\n"); |
| 1375 return false; |
| 1376 } |
| 1377 } |
| 1378 |
| 1379 /* IDENTIFIER */ |
| 1380 bool Parser::identifier(std::string* dest) { |
| 1381 Token t; |
| 1382 if (this->expect(Token::IDENTIFIER, "identifier", &t)) { |
| 1383 *dest = t.fText; |
| 1384 return true; |
| 1385 } |
| 1386 return false; |
| 1387 } |
| 1388 |
| 1389 } // namespace |
OLD | NEW |