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