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 | */ |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 7 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 8 | #include "stdio.h" |
| 9 | #include "SkSLParser.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 10 | #include "ast/SkSLASTBinaryExpression.h" |
| 11 | #include "ast/SkSLASTBlock.h" |
| 12 | #include "ast/SkSLASTBoolLiteral.h" |
| 13 | #include "ast/SkSLASTBreakStatement.h" |
| 14 | #include "ast/SkSLASTCallSuffix.h" |
| 15 | #include "ast/SkSLASTContinueStatement.h" |
| 16 | #include "ast/SkSLASTDiscardStatement.h" |
| 17 | #include "ast/SkSLASTDoStatement.h" |
| 18 | #include "ast/SkSLASTExpression.h" |
| 19 | #include "ast/SkSLASTExpressionStatement.h" |
| 20 | #include "ast/SkSLASTExtension.h" |
| 21 | #include "ast/SkSLASTFieldSuffix.h" |
| 22 | #include "ast/SkSLASTFloatLiteral.h" |
| 23 | #include "ast/SkSLASTForStatement.h" |
| 24 | #include "ast/SkSLASTFunction.h" |
| 25 | #include "ast/SkSLASTIdentifier.h" |
| 26 | #include "ast/SkSLASTIfStatement.h" |
| 27 | #include "ast/SkSLASTIndexSuffix.h" |
| 28 | #include "ast/SkSLASTInterfaceBlock.h" |
| 29 | #include "ast/SkSLASTIntLiteral.h" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 30 | #include "ast/SkSLASTModifiersDeclaration.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 31 | #include "ast/SkSLASTParameter.h" |
| 32 | #include "ast/SkSLASTPrefixExpression.h" |
| 33 | #include "ast/SkSLASTReturnStatement.h" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 34 | #include "ast/SkSLASTSection.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 35 | #include "ast/SkSLASTStatement.h" |
| 36 | #include "ast/SkSLASTSuffixExpression.h" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 37 | #include "ast/SkSLASTSwitchCase.h" |
| 38 | #include "ast/SkSLASTSwitchStatement.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 39 | #include "ast/SkSLASTTernaryExpression.h" |
| 40 | #include "ast/SkSLASTType.h" |
| 41 | #include "ast/SkSLASTVarDeclaration.h" |
| 42 | #include "ast/SkSLASTVarDeclarationStatement.h" |
| 43 | #include "ast/SkSLASTWhileStatement.h" |
| 44 | #include "ir/SkSLSymbolTable.h" |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 45 | #include "ir/SkSLModifiers.h" |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 46 | #include "ir/SkSLType.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 47 | |
| 48 | namespace SkSL { |
| 49 | |
ethannicholas | cad6416 | 2016-10-27 10:54:02 -0700 | [diff] [blame] | 50 | #define MAX_PARSE_DEPTH 50 |
| 51 | |
| 52 | class AutoDepth { |
| 53 | public: |
| 54 | AutoDepth(Parser* p) |
| 55 | : fParser(p) { |
| 56 | fParser->fDepth++; |
| 57 | } |
| 58 | |
| 59 | ~AutoDepth() { |
| 60 | fParser->fDepth--; |
| 61 | } |
| 62 | |
| 63 | bool checkValid() { |
| 64 | if (fParser->fDepth > MAX_PARSE_DEPTH) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 65 | fParser->error(fParser->peek(), String("exceeded max parse depth")); |
ethannicholas | cad6416 | 2016-10-27 10:54:02 -0700 | [diff] [blame] | 66 | return false; |
| 67 | } |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | Parser* fParser; |
| 73 | }; |
| 74 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 75 | Parser::Parser(const char* text, size_t length, SymbolTable& types, ErrorReporter& errors) |
| 76 | : fText(text) |
| 77 | , fPushback(Token::INVALID, -1, -1) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 78 | , fTypes(types) |
| 79 | , fErrors(errors) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 80 | fLexer.start(text, length); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 83 | /* (directive | section | declaration)* END_OF_FILE */ |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 84 | std::vector<std::unique_ptr<ASTDeclaration>> Parser::file() { |
| 85 | std::vector<std::unique_ptr<ASTDeclaration>> result; |
| 86 | for (;;) { |
| 87 | switch (this->peek().fKind) { |
| 88 | case Token::END_OF_FILE: |
| 89 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 90 | case Token::DIRECTIVE: { |
| 91 | std::unique_ptr<ASTDeclaration> decl = this->directive(); |
| 92 | if (decl) { |
| 93 | result.push_back(std::move(decl)); |
| 94 | } |
| 95 | break; |
| 96 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 97 | case Token::SECTION: { |
| 98 | std::unique_ptr<ASTDeclaration> section = this->section(); |
| 99 | if (section) { |
| 100 | result.push_back(std::move(section)); |
| 101 | } |
| 102 | break; |
| 103 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 104 | default: { |
| 105 | std::unique_ptr<ASTDeclaration> decl = this->declaration(); |
| 106 | if (!decl) { |
| 107 | continue; |
| 108 | } |
| 109 | result.push_back(std::move(decl)); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 115 | Token Parser::nextRawToken() { |
| 116 | if (fPushback.fKind != Token::INVALID) { |
| 117 | Token result = fPushback; |
| 118 | fPushback.fKind = Token::INVALID; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 119 | return result; |
| 120 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 121 | Token result = fLexer.next(); |
| 122 | return result; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | Token Parser::nextToken() { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 126 | Token token = this->nextRawToken(); |
| 127 | while (token.fKind == Token::WHITESPACE || token.fKind == Token::LINE_COMMENT || |
| 128 | token.fKind == Token::BLOCK_COMMENT) { |
| 129 | token = this->nextRawToken(); |
| 130 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 131 | return token; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void Parser::pushback(Token t) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 135 | ASSERT(fPushback.fKind == Token::INVALID); |
Ethan Nicholas | 08b79b7 | 2017-08-14 10:35:37 -0400 | [diff] [blame] | 136 | fPushback = std::move(t); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | Token Parser::peek() { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 140 | if (fPushback.fKind == Token::INVALID) { |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 141 | fPushback = this->nextToken(); |
| 142 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 143 | return fPushback; |
| 144 | } |
| 145 | |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 146 | bool Parser::checkNext(Token::Kind kind, Token* result) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 147 | if (fPushback.fKind != Token::INVALID && fPushback.fKind != kind) { |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 148 | return false; |
| 149 | } |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 150 | Token next = this->nextToken(); |
| 151 | if (next.fKind == kind) { |
| 152 | if (result) { |
| 153 | *result = next; |
| 154 | } |
| 155 | return true; |
| 156 | } |
Ethan Nicholas | 08b79b7 | 2017-08-14 10:35:37 -0400 | [diff] [blame] | 157 | this->pushback(std::move(next)); |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 158 | return false; |
| 159 | } |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 160 | |
| 161 | bool Parser::expect(Token::Kind kind, const char* expected, Token* result) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 162 | Token next = this->nextToken(); |
| 163 | if (next.fKind == kind) { |
| 164 | if (result) { |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 165 | *result = std::move(next); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 166 | } |
| 167 | return true; |
| 168 | } else { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 169 | this->error(next, "expected " + String(expected) + ", but found '" + |
| 170 | this->text(next) + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 171 | return false; |
| 172 | } |
| 173 | } |
| 174 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 175 | StringFragment Parser::text(Token token) { |
| 176 | return StringFragment(fText + token.fOffset, token.fLength); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 177 | } |
| 178 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 179 | void Parser::error(Token token, String msg) { |
| 180 | this->error(token.fOffset, msg); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 183 | void Parser::error(int offset, String msg) { |
| 184 | fErrors.error(offset, msg); |
| 185 | } |
| 186 | |
| 187 | bool Parser::isType(StringFragment name) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 188 | return nullptr != fTypes[name]; |
| 189 | } |
| 190 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 191 | /* DIRECTIVE(#version) INT_LITERAL ("es" | "compatibility")? | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 192 | DIRECTIVE(#extension) IDENTIFIER COLON IDENTIFIER */ |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 193 | std::unique_ptr<ASTDeclaration> Parser::directive() { |
| 194 | Token start; |
| 195 | if (!this->expect(Token::DIRECTIVE, "a directive", &start)) { |
| 196 | return nullptr; |
| 197 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 198 | StringFragment text = this->text(start); |
| 199 | if (text == "#version") { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 200 | this->expect(Token::INT_LITERAL, "a version number"); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 201 | Token next = this->peek(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 202 | StringFragment nextText = this->text(next); |
| 203 | if (nextText == "es" || nextText == "compatibility") { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 204 | this->nextToken(); |
| 205 | } |
| 206 | // version is ignored for now; it will eventually become an error when we stop pretending |
| 207 | // to be GLSL |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 208 | return nullptr; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 209 | } else if (text == "#extension") { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 210 | Token name; |
| 211 | if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) { |
| 212 | return nullptr; |
| 213 | } |
| 214 | if (!this->expect(Token::COLON, "':'")) { |
| 215 | return nullptr; |
| 216 | } |
| 217 | // FIXME: need to start paying attention to this token |
| 218 | if (!this->expect(Token::IDENTIFIER, "an identifier")) { |
| 219 | return nullptr; |
| 220 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 221 | return std::unique_ptr<ASTDeclaration>(new ASTExtension(start.fOffset, |
| 222 | String(this->text(name)))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 223 | } else { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 224 | this->error(start, "unsupported directive '" + this->text(start) + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 225 | return nullptr; |
| 226 | } |
| 227 | } |
| 228 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 229 | /* SECTION LBRACE (LPAREN IDENTIFIER RPAREN)? <any sequence of tokens with balanced braces> |
| 230 | RBRACE */ |
| 231 | std::unique_ptr<ASTDeclaration> Parser::section() { |
| 232 | Token start; |
| 233 | if (!this->expect(Token::SECTION, "a section token", &start)) { |
| 234 | return nullptr; |
| 235 | } |
| 236 | String argument; |
| 237 | if (this->peek().fKind == Token::LPAREN) { |
| 238 | this->nextToken(); |
| 239 | Token argToken; |
| 240 | if (!this->expect(Token::IDENTIFIER, "an identifier", &argToken)) { |
| 241 | return nullptr; |
| 242 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 243 | argument = this->text(argToken); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 244 | if (!this->expect(Token::RPAREN, "')'")) { |
| 245 | return nullptr; |
| 246 | } |
| 247 | } |
| 248 | if (!this->expect(Token::LBRACE, "'{'")) { |
| 249 | return nullptr; |
| 250 | } |
| 251 | String text; |
| 252 | int level = 1; |
| 253 | for (;;) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 254 | Token next = this->nextRawToken(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 255 | switch (next.fKind) { |
| 256 | case Token::LBRACE: |
| 257 | ++level; |
| 258 | break; |
| 259 | case Token::RBRACE: |
| 260 | --level; |
| 261 | break; |
| 262 | case Token::END_OF_FILE: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 263 | this->error(start, "reached end of file while parsing section"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 264 | return nullptr; |
| 265 | default: |
| 266 | break; |
| 267 | } |
| 268 | if (!level) { |
| 269 | break; |
| 270 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 271 | text += this->text(next); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 272 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 273 | StringFragment name = this->text(start); |
| 274 | ++name.fChars; |
| 275 | --name.fLength; |
| 276 | return std::unique_ptr<ASTDeclaration>(new ASTSection(start.fOffset, |
| 277 | String(name), |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 278 | argument, |
| 279 | text)); |
| 280 | } |
| 281 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 282 | /* modifiers (structVarDeclaration | type IDENTIFIER ((LPAREN parameter |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 283 | (COMMA parameter)* RPAREN (block | SEMICOLON)) | SEMICOLON) | interfaceBlock) */ |
| 284 | std::unique_ptr<ASTDeclaration> Parser::declaration() { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 285 | Modifiers modifiers = this->modifiers(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 286 | Token lookahead = this->peek(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 287 | if (lookahead.fKind == Token::IDENTIFIER && !this->isType(this->text(lookahead))) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 288 | // we have an identifier that's not a type, could be the start of an interface block |
| 289 | return this->interfaceBlock(modifiers); |
| 290 | } |
| 291 | if (lookahead.fKind == Token::STRUCT) { |
| 292 | return this->structVarDeclaration(modifiers); |
| 293 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 294 | if (lookahead.fKind == Token::SEMICOLON) { |
| 295 | this->nextToken(); |
| 296 | return std::unique_ptr<ASTDeclaration>(new ASTModifiersDeclaration(modifiers)); |
| 297 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 298 | std::unique_ptr<ASTType> type(this->type()); |
| 299 | if (!type) { |
| 300 | return nullptr; |
| 301 | } |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 302 | if (type->fKind == ASTType::kStruct_Kind && this->checkNext(Token::SEMICOLON)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 303 | return nullptr; |
| 304 | } |
| 305 | Token name; |
| 306 | if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) { |
| 307 | return nullptr; |
| 308 | } |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 309 | if (this->checkNext(Token::LPAREN)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 310 | std::vector<std::unique_ptr<ASTParameter>> parameters; |
| 311 | while (this->peek().fKind != Token::RPAREN) { |
| 312 | if (parameters.size() > 0) { |
| 313 | if (!this->expect(Token::COMMA, "','")) { |
| 314 | return nullptr; |
| 315 | } |
| 316 | } |
| 317 | std::unique_ptr<ASTParameter> parameter = this->parameter(); |
| 318 | if (!parameter) { |
| 319 | return nullptr; |
| 320 | } |
| 321 | parameters.push_back(std::move(parameter)); |
| 322 | } |
| 323 | this->nextToken(); |
| 324 | std::unique_ptr<ASTBlock> body; |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 325 | if (!this->checkNext(Token::SEMICOLON)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 326 | body = this->block(); |
| 327 | if (!body) { |
| 328 | return nullptr; |
| 329 | } |
| 330 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 331 | return std::unique_ptr<ASTDeclaration>(new ASTFunction(name.fOffset, |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 332 | modifiers, |
| 333 | std::move(type), |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 334 | this->text(name), |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 335 | std::move(parameters), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 336 | std::move(body))); |
| 337 | } else { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 338 | return this->varDeclarationEnd(modifiers, std::move(type), this->text(name)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
| 342 | /* modifiers type IDENTIFIER varDeclarationEnd */ |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 343 | std::unique_ptr<ASTVarDeclarations> Parser::varDeclarations() { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 344 | Modifiers modifiers = this->modifiers(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 345 | std::unique_ptr<ASTType> type(this->type()); |
| 346 | if (!type) { |
| 347 | return nullptr; |
| 348 | } |
| 349 | Token name; |
| 350 | if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) { |
| 351 | return nullptr; |
| 352 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 353 | return this->varDeclarationEnd(modifiers, std::move(type), this->text(name)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* STRUCT IDENTIFIER LBRACE varDeclaration* RBRACE */ |
| 357 | std::unique_ptr<ASTType> Parser::structDeclaration() { |
| 358 | if (!this->expect(Token::STRUCT, "'struct'")) { |
| 359 | return nullptr; |
| 360 | } |
| 361 | Token name; |
| 362 | if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) { |
| 363 | return nullptr; |
| 364 | } |
| 365 | if (!this->expect(Token::LBRACE, "'{'")) { |
| 366 | return nullptr; |
| 367 | } |
| 368 | std::vector<Type::Field> fields; |
| 369 | while (this->peek().fKind != Token::RBRACE) { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 370 | std::unique_ptr<ASTVarDeclarations> decl = this->varDeclarations(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 371 | if (!decl) { |
| 372 | return nullptr; |
| 373 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 374 | for (const auto& var : decl->fVars) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 375 | auto type = (const Type*) fTypes[decl->fType->fName]; |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 376 | for (int i = (int) var.fSizes.size() - 1; i >= 0; i--) { |
ethannicholas | dd4645b | 2016-10-14 12:14:46 -0700 | [diff] [blame] | 377 | if (!var.fSizes[i] || var.fSizes[i]->fKind != ASTExpression::kInt_Kind) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 378 | this->error(decl->fOffset, "array size in struct field must be a constant"); |
ethannicholas | dd4645b | 2016-10-14 12:14:46 -0700 | [diff] [blame] | 379 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 380 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 381 | uint64_t columns = ((ASTIntLiteral&) *var.fSizes[i]).fValue; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 382 | String name = type->name() + "[" + to_string(columns) + "]"; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 383 | type = new Type(name, Type::kArray_Kind, *type, (int) columns); |
| 384 | fTypes.takeOwnership((Type*) type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 385 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 386 | fields.push_back(Type::Field(decl->fModifiers, var.fName, type)); |
| 387 | if (var.fValue) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 388 | this->error(decl->fOffset, "initializers are not permitted on struct fields"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | } |
| 392 | if (!this->expect(Token::RBRACE, "'}'")) { |
| 393 | return nullptr; |
| 394 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 395 | fTypes.add(this->text(name), std::unique_ptr<Type>(new Type(name.fOffset, this->text(name), |
| 396 | fields))); |
| 397 | return std::unique_ptr<ASTType>(new ASTType(name.fOffset, this->text(name), |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 398 | ASTType::kStruct_Kind, std::vector<int>())); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /* structDeclaration ((IDENTIFIER varDeclarationEnd) | SEMICOLON) */ |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 402 | std::unique_ptr<ASTVarDeclarations> Parser::structVarDeclaration(Modifiers modifiers) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 403 | std::unique_ptr<ASTType> type = this->structDeclaration(); |
| 404 | if (!type) { |
| 405 | return nullptr; |
| 406 | } |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 407 | Token name; |
| 408 | if (this->checkNext(Token::IDENTIFIER, &name)) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 409 | std::unique_ptr<ASTVarDeclarations> result = this->varDeclarationEnd(modifiers, |
| 410 | std::move(type), |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 411 | this->text(name)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 412 | if (result) { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 413 | for (const auto& var : result->fVars) { |
| 414 | if (var.fValue) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 415 | this->error(var.fValue->fOffset, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 416 | "struct variables cannot be initialized"); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | return result; |
| 421 | } |
| 422 | this->expect(Token::SEMICOLON, "';'"); |
| 423 | return nullptr; |
| 424 | } |
| 425 | |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 426 | /* (LBRACKET expression? RBRACKET)* (EQ assignmentExpression)? (COMMA IDENTIFER |
| 427 | (LBRACKET expression? RBRACKET)* (EQ assignmentExpression)?)* SEMICOLON */ |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 428 | std::unique_ptr<ASTVarDeclarations> Parser::varDeclarationEnd(Modifiers mods, |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 429 | std::unique_ptr<ASTType> type, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 430 | StringFragment name) { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 431 | std::vector<ASTVarDeclaration> vars; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 432 | std::vector<std::unique_ptr<ASTExpression>> currentVarSizes; |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 433 | while (this->checkNext(Token::LBRACKET)) { |
| 434 | if (this->checkNext(Token::RBRACKET)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 435 | currentVarSizes.push_back(nullptr); |
| 436 | } else { |
| 437 | std::unique_ptr<ASTExpression> size(this->expression()); |
| 438 | if (!size) { |
| 439 | return nullptr; |
| 440 | } |
| 441 | currentVarSizes.push_back(std::move(size)); |
| 442 | if (!this->expect(Token::RBRACKET, "']'")) { |
| 443 | return nullptr; |
| 444 | } |
| 445 | } |
| 446 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 447 | std::unique_ptr<ASTExpression> value; |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 448 | if (this->checkNext(Token::EQ)) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 449 | value = this->assignmentExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 450 | if (!value) { |
| 451 | return nullptr; |
| 452 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 453 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 454 | vars.emplace_back(name, std::move(currentVarSizes), std::move(value)); |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 455 | while (this->checkNext(Token::COMMA)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 456 | Token name; |
| 457 | if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) { |
| 458 | return nullptr; |
| 459 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 460 | currentVarSizes.clear(); |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 461 | value.reset(); |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 462 | while (this->checkNext(Token::LBRACKET)) { |
| 463 | if (this->checkNext(Token::RBRACKET)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 464 | currentVarSizes.push_back(nullptr); |
| 465 | } else { |
| 466 | std::unique_ptr<ASTExpression> size(this->expression()); |
| 467 | if (!size) { |
| 468 | return nullptr; |
| 469 | } |
| 470 | currentVarSizes.push_back(std::move(size)); |
| 471 | if (!this->expect(Token::RBRACKET, "']'")) { |
| 472 | return nullptr; |
| 473 | } |
| 474 | } |
| 475 | } |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 476 | if (this->checkNext(Token::EQ)) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 477 | value = this->assignmentExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 478 | if (!value) { |
| 479 | return nullptr; |
| 480 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 481 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 482 | vars.emplace_back(this->text(name), std::move(currentVarSizes), std::move(value)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 483 | } |
| 484 | if (!this->expect(Token::SEMICOLON, "';'")) { |
| 485 | return nullptr; |
| 486 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 487 | return std::unique_ptr<ASTVarDeclarations>(new ASTVarDeclarations(std::move(mods), |
| 488 | std::move(type), |
| 489 | std::move(vars))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /* modifiers type IDENTIFIER (LBRACKET INT_LITERAL RBRACKET)? */ |
| 493 | std::unique_ptr<ASTParameter> Parser::parameter() { |
Ethan Nicholas | c6f5e10 | 2017-03-31 14:53:17 -0400 | [diff] [blame] | 494 | Modifiers modifiers = this->modifiersWithDefaults(0); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 495 | std::unique_ptr<ASTType> type = this->type(); |
| 496 | if (!type) { |
| 497 | return nullptr; |
| 498 | } |
| 499 | Token name; |
| 500 | if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) { |
| 501 | return nullptr; |
| 502 | } |
| 503 | std::vector<int> sizes; |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 504 | while (this->checkNext(Token::LBRACKET)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 505 | Token sizeToken; |
| 506 | if (!this->expect(Token::INT_LITERAL, "a positive integer", &sizeToken)) { |
| 507 | return nullptr; |
| 508 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 509 | sizes.push_back(SkSL::stoi(this->text(sizeToken))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 510 | if (!this->expect(Token::RBRACKET, "']'")) { |
| 511 | return nullptr; |
| 512 | } |
| 513 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 514 | return std::unique_ptr<ASTParameter>(new ASTParameter(name.fOffset, modifiers, std::move(type), |
| 515 | this->text(name), std::move(sizes))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /** (EQ INT_LITERAL)? */ |
| 519 | int Parser::layoutInt() { |
| 520 | if (!this->expect(Token::EQ, "'='")) { |
| 521 | return -1; |
| 522 | } |
| 523 | Token resultToken; |
| 524 | if (this->expect(Token::INT_LITERAL, "a non-negative integer", &resultToken)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 525 | return SkSL::stoi(this->text(resultToken)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 526 | } |
| 527 | return -1; |
| 528 | } |
| 529 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 530 | /** EQ <any sequence of tokens with balanced parentheses and no top-level comma> */ |
| 531 | String Parser::layoutCode() { |
| 532 | if (!this->expect(Token::EQ, "'='")) { |
| 533 | return ""; |
| 534 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 535 | Token start = this->nextRawToken(); |
Ethan Nicholas | 08b79b7 | 2017-08-14 10:35:37 -0400 | [diff] [blame] | 536 | this->pushback(start); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 537 | String code; |
| 538 | int level = 1; |
| 539 | bool done = false; |
| 540 | while (!done) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 541 | Token next = this->nextRawToken(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 542 | switch (next.fKind) { |
| 543 | case Token::LPAREN: |
| 544 | ++level; |
| 545 | break; |
| 546 | case Token::RPAREN: |
| 547 | --level; |
| 548 | break; |
| 549 | case Token::COMMA: |
| 550 | if (level == 1) { |
| 551 | done = true; |
| 552 | } |
| 553 | break; |
| 554 | case Token::END_OF_FILE: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 555 | this->error(start, "reached end of file while parsing layout"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 556 | return nullptr; |
| 557 | default: |
| 558 | break; |
| 559 | } |
| 560 | if (!level) { |
| 561 | done = true; |
| 562 | } |
Ethan Nicholas | 08b79b7 | 2017-08-14 10:35:37 -0400 | [diff] [blame] | 563 | if (done) { |
| 564 | this->pushback(std::move(next)); |
| 565 | } |
| 566 | else { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 567 | code += this->text(next); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | return code; |
| 571 | } |
| 572 | |
| 573 | /** (EQ IDENTIFIER('identity'))? */ |
| 574 | Layout::Key Parser::layoutKey() { |
| 575 | if (this->peek().fKind == Token::EQ) { |
| 576 | this->expect(Token::EQ, "'='"); |
| 577 | Token key; |
| 578 | if (this->expect(Token::IDENTIFIER, "an identifer", &key)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 579 | if (this->text(key) == "identity") { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 580 | return Layout::kIdentity_Key; |
| 581 | } else { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 582 | this->error(key, "unsupported layout key"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | } |
| 586 | return Layout::kKey_Key; |
| 587 | } |
| 588 | |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 589 | /* LAYOUT LPAREN IDENTIFIER (EQ INT_LITERAL)? (COMMA IDENTIFIER (EQ INT_LITERAL)?)* RPAREN */ |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 590 | Layout Parser::layout() { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 591 | int location = -1; |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 592 | int offset = -1; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 593 | int binding = -1; |
| 594 | int index = -1; |
| 595 | int set = -1; |
| 596 | int builtin = -1; |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 597 | int inputAttachmentIndex = -1; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 598 | bool originUpperLeft = false; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 599 | bool overrideCoverage = false; |
| 600 | bool blendSupportAllEquations = false; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 601 | Layout::Format format = Layout::Format::kUnspecified; |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 602 | bool pushConstant = false; |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 603 | Layout::Primitive primitive = Layout::kUnspecified_Primitive; |
| 604 | int maxVertices = -1; |
| 605 | int invocations = -1; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 606 | String when; |
| 607 | Layout::Key key = Layout::kNo_Key; |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 608 | if (this->checkNext(Token::LAYOUT)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 609 | if (!this->expect(Token::LPAREN, "'('")) { |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 610 | return Layout(location, offset, binding, index, set, builtin, inputAttachmentIndex, |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 611 | originUpperLeft, overrideCoverage, blendSupportAllEquations, format, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 612 | pushConstant, primitive, maxVertices, invocations, when, key); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 613 | } |
| 614 | for (;;) { |
| 615 | Token t = this->nextToken(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 616 | String text = this->text(t); |
| 617 | fLayoutLexer.start(text.c_str(), text.size()); |
| 618 | int token = fLayoutLexer.next().fKind; |
| 619 | if (token != LayoutToken::INVALID) { |
Ethan Nicholas | bfe15f6 | 2017-03-01 11:46:51 -0500 | [diff] [blame] | 620 | switch (token) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 621 | case LayoutToken::LOCATION: |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 622 | location = this->layoutInt(); |
| 623 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 624 | case LayoutToken::OFFSET: |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 625 | offset = this->layoutInt(); |
| 626 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 627 | case LayoutToken::BINDING: |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 628 | binding = this->layoutInt(); |
| 629 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 630 | case LayoutToken::INDEX: |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 631 | index = this->layoutInt(); |
| 632 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 633 | case LayoutToken::SET: |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 634 | set = this->layoutInt(); |
| 635 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 636 | case LayoutToken::BUILTIN: |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 637 | builtin = this->layoutInt(); |
| 638 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 639 | case LayoutToken::INPUT_ATTACHMENT_INDEX: |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 640 | inputAttachmentIndex = this->layoutInt(); |
| 641 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 642 | case LayoutToken::ORIGIN_UPPER_LEFT: |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 643 | originUpperLeft = true; |
| 644 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 645 | case LayoutToken::OVERRIDE_COVERAGE: |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 646 | overrideCoverage = true; |
| 647 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 648 | case LayoutToken::BLEND_SUPPORT_ALL_EQUATIONS: |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 649 | blendSupportAllEquations = true; |
| 650 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 651 | case LayoutToken::PUSH_CONSTANT: |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 652 | pushConstant = true; |
| 653 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 654 | case LayoutToken::POINTS: |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 655 | primitive = Layout::kPoints_Primitive; |
| 656 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 657 | case LayoutToken::LINES: |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 658 | primitive = Layout::kLines_Primitive; |
| 659 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 660 | case LayoutToken::LINE_STRIP: |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 661 | primitive = Layout::kLineStrip_Primitive; |
| 662 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 663 | case LayoutToken::LINES_ADJACENCY: |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 664 | primitive = Layout::kLinesAdjacency_Primitive; |
| 665 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 666 | case LayoutToken::TRIANGLES: |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 667 | primitive = Layout::kTriangles_Primitive; |
| 668 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 669 | case LayoutToken::TRIANGLE_STRIP: |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 670 | primitive = Layout::kTriangleStrip_Primitive; |
| 671 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 672 | case LayoutToken::TRIANGLES_ADJACENCY: |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 673 | primitive = Layout::kTrianglesAdjacency_Primitive; |
| 674 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 675 | case LayoutToken::MAX_VERTICES: |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 676 | maxVertices = this->layoutInt(); |
| 677 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 678 | case LayoutToken::INVOCATIONS: |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 679 | invocations = this->layoutInt(); |
| 680 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 681 | case LayoutToken::WHEN: |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 682 | when = this->layoutCode(); |
| 683 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 684 | case LayoutToken::KEY: |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 685 | key = this->layoutKey(); |
| 686 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 687 | default: |
| 688 | ASSERT(false); |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 689 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 690 | } else if (Layout::ReadFormat(this->text(t), &format)) { |
Brian Salomon | 2a51de8 | 2016-11-16 12:06:01 -0500 | [diff] [blame] | 691 | // AST::ReadFormat stored the result in 'format'. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 692 | } else { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 693 | this->error(t, ("'" + this->text(t) + "' is not a valid layout qualifier").c_str()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 694 | } |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 695 | if (this->checkNext(Token::RPAREN)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 696 | break; |
| 697 | } |
| 698 | if (!this->expect(Token::COMMA, "','")) { |
| 699 | break; |
| 700 | } |
| 701 | } |
| 702 | } |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 703 | return Layout(location, offset, binding, index, set, builtin, inputAttachmentIndex, |
| 704 | originUpperLeft, overrideCoverage, blendSupportAllEquations, format, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 705 | pushConstant, primitive, maxVertices, invocations, when, key); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 708 | /* layout? (UNIFORM | CONST | IN | OUT | INOUT | LOWP | MEDIUMP | HIGHP | FLAT | NOPERSPECTIVE | |
Ethan Nicholas | 0dd30d9 | 2017-05-01 16:57:07 -0400 | [diff] [blame] | 709 | READONLY | WRITEONLY | COHERENT | VOLATILE | RESTRICT | BUFFER)* */ |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 710 | Modifiers Parser::modifiers() { |
| 711 | Layout layout = this->layout(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 712 | int flags = 0; |
| 713 | for (;;) { |
| 714 | // TODO: handle duplicate / incompatible flags |
| 715 | switch (peek().fKind) { |
| 716 | case Token::UNIFORM: |
| 717 | this->nextToken(); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 718 | flags |= Modifiers::kUniform_Flag; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 719 | break; |
| 720 | case Token::CONST: |
| 721 | this->nextToken(); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 722 | flags |= Modifiers::kConst_Flag; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 723 | break; |
| 724 | case Token::IN: |
| 725 | this->nextToken(); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 726 | flags |= Modifiers::kIn_Flag; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 727 | break; |
| 728 | case Token::OUT: |
| 729 | this->nextToken(); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 730 | flags |= Modifiers::kOut_Flag; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 731 | break; |
| 732 | case Token::INOUT: |
| 733 | this->nextToken(); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 734 | flags |= Modifiers::kIn_Flag; |
| 735 | flags |= Modifiers::kOut_Flag; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 736 | break; |
| 737 | case Token::LOWP: |
| 738 | this->nextToken(); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 739 | flags |= Modifiers::kLowp_Flag; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 740 | break; |
| 741 | case Token::MEDIUMP: |
| 742 | this->nextToken(); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 743 | flags |= Modifiers::kMediump_Flag; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 744 | break; |
| 745 | case Token::HIGHP: |
| 746 | this->nextToken(); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 747 | flags |= Modifiers::kHighp_Flag; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 748 | break; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 749 | case Token::FLAT: |
| 750 | this->nextToken(); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 751 | flags |= Modifiers::kFlat_Flag; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 752 | break; |
| 753 | case Token::NOPERSPECTIVE: |
| 754 | this->nextToken(); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 755 | flags |= Modifiers::kNoPerspective_Flag; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 756 | break; |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 757 | case Token::READONLY: |
| 758 | this->nextToken(); |
| 759 | flags |= Modifiers::kReadOnly_Flag; |
| 760 | break; |
| 761 | case Token::WRITEONLY: |
| 762 | this->nextToken(); |
| 763 | flags |= Modifiers::kWriteOnly_Flag; |
| 764 | break; |
| 765 | case Token::COHERENT: |
| 766 | this->nextToken(); |
| 767 | flags |= Modifiers::kCoherent_Flag; |
| 768 | break; |
| 769 | case Token::VOLATILE: |
| 770 | this->nextToken(); |
| 771 | flags |= Modifiers::kVolatile_Flag; |
| 772 | break; |
| 773 | case Token::RESTRICT: |
| 774 | this->nextToken(); |
| 775 | flags |= Modifiers::kRestrict_Flag; |
| 776 | break; |
Ethan Nicholas | 0dd30d9 | 2017-05-01 16:57:07 -0400 | [diff] [blame] | 777 | case Token::BUFFER: |
| 778 | this->nextToken(); |
| 779 | flags |= Modifiers::kBuffer_Flag; |
| 780 | break; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 781 | case Token::HASSIDEEFFECTS: |
| 782 | this->nextToken(); |
| 783 | flags |= Modifiers::kHasSideEffects_Flag; |
| 784 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 785 | default: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 786 | return Modifiers(layout, flags); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | } |
| 790 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 791 | Modifiers Parser::modifiersWithDefaults(int defaultFlags) { |
| 792 | Modifiers result = this->modifiers(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 793 | if (!result.fFlags) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 794 | return Modifiers(result.fLayout, defaultFlags); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 795 | } |
| 796 | return result; |
| 797 | } |
| 798 | |
| 799 | /* ifStatement | forStatement | doStatement | whileStatement | block | expression */ |
| 800 | std::unique_ptr<ASTStatement> Parser::statement() { |
| 801 | Token start = this->peek(); |
| 802 | switch (start.fKind) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 803 | case Token::IF: // fall through |
| 804 | case Token::STATIC_IF: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 805 | return this->ifStatement(); |
| 806 | case Token::FOR: |
| 807 | return this->forStatement(); |
| 808 | case Token::DO: |
| 809 | return this->doStatement(); |
| 810 | case Token::WHILE: |
| 811 | return this->whileStatement(); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 812 | case Token::SWITCH: // fall through |
| 813 | case Token::STATIC_SWITCH: |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 814 | return this->switchStatement(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 815 | case Token::RETURN: |
| 816 | return this->returnStatement(); |
| 817 | case Token::BREAK: |
| 818 | return this->breakStatement(); |
| 819 | case Token::CONTINUE: |
| 820 | return this->continueStatement(); |
| 821 | case Token::DISCARD: |
| 822 | return this->discardStatement(); |
| 823 | case Token::LBRACE: |
| 824 | return this->block(); |
| 825 | case Token::SEMICOLON: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 826 | this->nextToken(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 827 | return std::unique_ptr<ASTStatement>(new ASTBlock(start.fOffset, |
ethannicholas | 0730be7 | 2016-09-01 07:59:02 -0700 | [diff] [blame] | 828 | std::vector<std::unique_ptr<ASTStatement>>())); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 829 | case Token::CONST: // fall through |
| 830 | case Token::HIGHP: // fall through |
| 831 | case Token::MEDIUMP: // fall through |
| 832 | case Token::LOWP: { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 833 | auto decl = this->varDeclarations(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 834 | if (!decl) { |
| 835 | return nullptr; |
| 836 | } |
| 837 | return std::unique_ptr<ASTStatement>(new ASTVarDeclarationStatement(std::move(decl))); |
| 838 | } |
| 839 | case Token::IDENTIFIER: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 840 | if (this->isType(this->text(start))) { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 841 | auto decl = this->varDeclarations(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 842 | if (!decl) { |
| 843 | return nullptr; |
| 844 | } |
| 845 | return std::unique_ptr<ASTStatement>(new ASTVarDeclarationStatement( |
| 846 | std::move(decl))); |
| 847 | } |
| 848 | // fall through |
| 849 | default: |
| 850 | return this->expressionStatement(); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 851 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 854 | /* IDENTIFIER(type) (LBRACKET intLiteral? RBRACKET)* */ |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 855 | std::unique_ptr<ASTType> Parser::type() { |
| 856 | Token type; |
| 857 | if (!this->expect(Token::IDENTIFIER, "a type", &type)) { |
| 858 | return nullptr; |
| 859 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 860 | if (!this->isType(this->text(type))) { |
| 861 | this->error(type, ("no type named '" + this->text(type) + "'").c_str()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 862 | return nullptr; |
| 863 | } |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 864 | std::vector<int> sizes; |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 865 | while (this->checkNext(Token::LBRACKET)) { |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 866 | if (this->peek().fKind != Token::RBRACKET) { |
| 867 | int64_t i; |
| 868 | if (this->intLiteral(&i)) { |
| 869 | sizes.push_back(i); |
| 870 | } else { |
| 871 | return nullptr; |
| 872 | } |
| 873 | } else { |
| 874 | sizes.push_back(-1); |
| 875 | } |
| 876 | this->expect(Token::RBRACKET, "']'"); |
| 877 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 878 | return std::unique_ptr<ASTType>(new ASTType(type.fOffset, this->text(type), |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 879 | ASTType::kIdentifier_Kind, sizes)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 882 | /* IDENTIFIER LBRACE varDeclaration* RBRACE (IDENTIFIER (LBRACKET expression? RBRACKET)*)? */ |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 883 | std::unique_ptr<ASTDeclaration> Parser::interfaceBlock(Modifiers mods) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 884 | Token name; |
| 885 | if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) { |
| 886 | return nullptr; |
| 887 | } |
| 888 | if (peek().fKind != Token::LBRACE) { |
| 889 | // we only get into interfaceBlock if we found a top-level identifier which was not a type. |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 890 | // 99% of the time, the user was not actually intending to create an interface block, so |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 891 | // it's better to report it as an unknown type |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 892 | this->error(name, "no type named '" + this->text(name) + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 893 | return nullptr; |
| 894 | } |
| 895 | this->nextToken(); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 896 | std::vector<std::unique_ptr<ASTVarDeclarations>> decls; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 897 | while (this->peek().fKind != Token::RBRACE) { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 898 | std::unique_ptr<ASTVarDeclarations> decl = this->varDeclarations(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 899 | if (!decl) { |
| 900 | return nullptr; |
| 901 | } |
| 902 | decls.push_back(std::move(decl)); |
| 903 | } |
| 904 | this->nextToken(); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 905 | std::vector<std::unique_ptr<ASTExpression>> sizes; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 906 | StringFragment instanceName; |
| 907 | Token instanceNameToken; |
| 908 | if (this->checkNext(Token::IDENTIFIER, &instanceNameToken)) { |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 909 | while (this->checkNext(Token::LBRACKET)) { |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 910 | if (this->peek().fKind != Token::RBRACKET) { |
| 911 | std::unique_ptr<ASTExpression> size = this->expression(); |
| 912 | if (!size) { |
| 913 | return nullptr; |
| 914 | } |
| 915 | sizes.push_back(std::move(size)); |
| 916 | } else { |
| 917 | sizes.push_back(nullptr); |
| 918 | } |
| 919 | this->expect(Token::RBRACKET, "']'"); |
| 920 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 921 | instanceName = this->text(instanceNameToken); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 922 | } |
| 923 | this->expect(Token::SEMICOLON, "';'"); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 924 | return std::unique_ptr<ASTDeclaration>(new ASTInterfaceBlock(name.fOffset, mods, |
| 925 | this->text(name), |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 926 | std::move(decls), |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 927 | instanceName, |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 928 | std::move(sizes))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | /* IF LPAREN expression RPAREN statement (ELSE statement)? */ |
| 932 | std::unique_ptr<ASTIfStatement> Parser::ifStatement() { |
| 933 | Token start; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 934 | bool isStatic = this->checkNext(Token::STATIC_IF, &start); |
| 935 | if (!isStatic && !this->expect(Token::IF, "'if'", &start)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 936 | return nullptr; |
| 937 | } |
| 938 | if (!this->expect(Token::LPAREN, "'('")) { |
| 939 | return nullptr; |
| 940 | } |
| 941 | std::unique_ptr<ASTExpression> test(this->expression()); |
| 942 | if (!test) { |
| 943 | return nullptr; |
| 944 | } |
| 945 | if (!this->expect(Token::RPAREN, "')'")) { |
| 946 | return nullptr; |
| 947 | } |
| 948 | std::unique_ptr<ASTStatement> ifTrue(this->statement()); |
| 949 | if (!ifTrue) { |
| 950 | return nullptr; |
| 951 | } |
| 952 | std::unique_ptr<ASTStatement> ifFalse; |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 953 | if (this->checkNext(Token::ELSE)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 954 | ifFalse = this->statement(); |
| 955 | if (!ifFalse) { |
| 956 | return nullptr; |
| 957 | } |
| 958 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 959 | return std::unique_ptr<ASTIfStatement>(new ASTIfStatement(start.fOffset, |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 960 | isStatic, |
| 961 | std::move(test), |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 962 | std::move(ifTrue), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 963 | std::move(ifFalse))); |
| 964 | } |
| 965 | |
| 966 | /* DO statement WHILE LPAREN expression RPAREN SEMICOLON */ |
| 967 | std::unique_ptr<ASTDoStatement> Parser::doStatement() { |
| 968 | Token start; |
| 969 | if (!this->expect(Token::DO, "'do'", &start)) { |
| 970 | return nullptr; |
| 971 | } |
| 972 | std::unique_ptr<ASTStatement> statement(this->statement()); |
| 973 | if (!statement) { |
| 974 | return nullptr; |
| 975 | } |
| 976 | if (!this->expect(Token::WHILE, "'while'")) { |
| 977 | return nullptr; |
| 978 | } |
| 979 | if (!this->expect(Token::LPAREN, "'('")) { |
| 980 | return nullptr; |
| 981 | } |
| 982 | std::unique_ptr<ASTExpression> test(this->expression()); |
| 983 | if (!test) { |
| 984 | return nullptr; |
| 985 | } |
| 986 | if (!this->expect(Token::RPAREN, "')'")) { |
| 987 | return nullptr; |
| 988 | } |
| 989 | if (!this->expect(Token::SEMICOLON, "';'")) { |
| 990 | return nullptr; |
| 991 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 992 | return std::unique_ptr<ASTDoStatement>(new ASTDoStatement(start.fOffset, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 993 | std::move(statement), |
| 994 | std::move(test))); |
| 995 | } |
| 996 | |
| 997 | /* WHILE LPAREN expression RPAREN STATEMENT */ |
| 998 | std::unique_ptr<ASTWhileStatement> Parser::whileStatement() { |
| 999 | Token start; |
| 1000 | if (!this->expect(Token::WHILE, "'while'", &start)) { |
| 1001 | return nullptr; |
| 1002 | } |
| 1003 | if (!this->expect(Token::LPAREN, "'('")) { |
| 1004 | return nullptr; |
| 1005 | } |
| 1006 | std::unique_ptr<ASTExpression> test(this->expression()); |
| 1007 | if (!test) { |
| 1008 | return nullptr; |
| 1009 | } |
| 1010 | if (!this->expect(Token::RPAREN, "')'")) { |
| 1011 | return nullptr; |
| 1012 | } |
| 1013 | std::unique_ptr<ASTStatement> statement(this->statement()); |
| 1014 | if (!statement) { |
| 1015 | return nullptr; |
| 1016 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1017 | return std::unique_ptr<ASTWhileStatement>(new ASTWhileStatement(start.fOffset, |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1018 | std::move(test), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1019 | std::move(statement))); |
| 1020 | } |
| 1021 | |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1022 | /* CASE expression COLON statement* */ |
| 1023 | std::unique_ptr<ASTSwitchCase> Parser::switchCase() { |
| 1024 | Token start; |
| 1025 | if (!this->expect(Token::CASE, "'case'", &start)) { |
| 1026 | return nullptr; |
| 1027 | } |
| 1028 | std::unique_ptr<ASTExpression> value = this->expression(); |
| 1029 | if (!value) { |
| 1030 | return nullptr; |
| 1031 | } |
| 1032 | if (!this->expect(Token::COLON, "':'")) { |
| 1033 | return nullptr; |
| 1034 | } |
| 1035 | std::vector<std::unique_ptr<ASTStatement>> statements; |
| 1036 | while (this->peek().fKind != Token::RBRACE && this->peek().fKind != Token::CASE && |
| 1037 | this->peek().fKind != Token::DEFAULT) { |
| 1038 | std::unique_ptr<ASTStatement> s = this->statement(); |
| 1039 | if (!s) { |
| 1040 | return nullptr; |
| 1041 | } |
| 1042 | statements.push_back(std::move(s)); |
| 1043 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1044 | return std::unique_ptr<ASTSwitchCase>(new ASTSwitchCase(start.fOffset, std::move(value), |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1045 | std::move(statements))); |
| 1046 | } |
| 1047 | |
| 1048 | /* SWITCH LPAREN expression RPAREN LBRACE switchCase* (DEFAULT COLON statement*)? RBRACE */ |
| 1049 | std::unique_ptr<ASTStatement> Parser::switchStatement() { |
| 1050 | Token start; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1051 | bool isStatic = this->checkNext(Token::STATIC_SWITCH, &start); |
| 1052 | if (!isStatic && !this->expect(Token::SWITCH, "'switch'", &start)) { |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1053 | return nullptr; |
| 1054 | } |
| 1055 | if (!this->expect(Token::LPAREN, "'('")) { |
| 1056 | return nullptr; |
| 1057 | } |
| 1058 | std::unique_ptr<ASTExpression> value(this->expression()); |
| 1059 | if (!value) { |
| 1060 | return nullptr; |
| 1061 | } |
| 1062 | if (!this->expect(Token::RPAREN, "')'")) { |
| 1063 | return nullptr; |
| 1064 | } |
| 1065 | if (!this->expect(Token::LBRACE, "'{'")) { |
| 1066 | return nullptr; |
| 1067 | } |
| 1068 | std::vector<std::unique_ptr<ASTSwitchCase>> cases; |
| 1069 | while (this->peek().fKind == Token::CASE) { |
| 1070 | std::unique_ptr<ASTSwitchCase> c = this->switchCase(); |
| 1071 | if (!c) { |
| 1072 | return nullptr; |
| 1073 | } |
| 1074 | cases.push_back(std::move(c)); |
| 1075 | } |
| 1076 | // Requiring default: to be last (in defiance of C and GLSL) was a deliberate decision. Other |
| 1077 | // parts of the compiler may rely upon this assumption. |
| 1078 | if (this->peek().fKind == Token::DEFAULT) { |
| 1079 | Token defaultStart; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1080 | ASSERT_RESULT(this->expect(Token::DEFAULT, "'default'", &defaultStart)); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1081 | if (!this->expect(Token::COLON, "':'")) { |
| 1082 | return nullptr; |
| 1083 | } |
| 1084 | std::vector<std::unique_ptr<ASTStatement>> statements; |
| 1085 | while (this->peek().fKind != Token::RBRACE) { |
| 1086 | std::unique_ptr<ASTStatement> s = this->statement(); |
| 1087 | if (!s) { |
| 1088 | return nullptr; |
| 1089 | } |
| 1090 | statements.push_back(std::move(s)); |
| 1091 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1092 | cases.emplace_back(new ASTSwitchCase(defaultStart.fOffset, nullptr, |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1093 | std::move(statements))); |
| 1094 | } |
| 1095 | if (!this->expect(Token::RBRACE, "'}'")) { |
| 1096 | return nullptr; |
| 1097 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1098 | return std::unique_ptr<ASTStatement>(new ASTSwitchStatement(start.fOffset, |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1099 | isStatic, |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1100 | std::move(value), |
| 1101 | std::move(cases))); |
| 1102 | } |
| 1103 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1104 | /* FOR LPAREN (declaration | expression)? SEMICOLON expression? SEMICOLON expression? RPAREN |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1105 | STATEMENT */ |
| 1106 | std::unique_ptr<ASTForStatement> Parser::forStatement() { |
| 1107 | Token start; |
| 1108 | if (!this->expect(Token::FOR, "'for'", &start)) { |
| 1109 | return nullptr; |
| 1110 | } |
| 1111 | if (!this->expect(Token::LPAREN, "'('")) { |
| 1112 | return nullptr; |
| 1113 | } |
| 1114 | std::unique_ptr<ASTStatement> initializer; |
| 1115 | Token nextToken = this->peek(); |
| 1116 | switch (nextToken.fKind) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1117 | case Token::SEMICOLON: |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1118 | this->nextToken(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1119 | break; |
ethannicholas | a54401d | 2016-10-14 08:37:32 -0700 | [diff] [blame] | 1120 | case Token::CONST: { |
| 1121 | std::unique_ptr<ASTVarDeclarations> vd = this->varDeclarations(); |
| 1122 | if (!vd) { |
| 1123 | return nullptr; |
| 1124 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1125 | initializer = std::unique_ptr<ASTStatement>(new ASTVarDeclarationStatement( |
ethannicholas | a54401d | 2016-10-14 08:37:32 -0700 | [diff] [blame] | 1126 | std::move(vd))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1127 | break; |
ethannicholas | a54401d | 2016-10-14 08:37:32 -0700 | [diff] [blame] | 1128 | } |
| 1129 | case Token::IDENTIFIER: { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1130 | if (this->isType(this->text(nextToken))) { |
ethannicholas | a54401d | 2016-10-14 08:37:32 -0700 | [diff] [blame] | 1131 | std::unique_ptr<ASTVarDeclarations> vd = this->varDeclarations(); |
| 1132 | if (!vd) { |
| 1133 | return nullptr; |
| 1134 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1135 | initializer = std::unique_ptr<ASTStatement>(new ASTVarDeclarationStatement( |
ethannicholas | a54401d | 2016-10-14 08:37:32 -0700 | [diff] [blame] | 1136 | std::move(vd))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1137 | break; |
| 1138 | } |
ethannicholas | a54401d | 2016-10-14 08:37:32 -0700 | [diff] [blame] | 1139 | } // fall through |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1140 | default: |
| 1141 | initializer = this->expressionStatement(); |
| 1142 | } |
| 1143 | std::unique_ptr<ASTExpression> test; |
| 1144 | if (this->peek().fKind != Token::SEMICOLON) { |
| 1145 | test = this->expression(); |
| 1146 | if (!test) { |
| 1147 | return nullptr; |
| 1148 | } |
| 1149 | } |
| 1150 | if (!this->expect(Token::SEMICOLON, "';'")) { |
| 1151 | return nullptr; |
| 1152 | } |
| 1153 | std::unique_ptr<ASTExpression> next; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1154 | if (this->peek().fKind != Token::RPAREN) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1155 | next = this->expression(); |
| 1156 | if (!next) { |
| 1157 | return nullptr; |
| 1158 | } |
| 1159 | } |
| 1160 | if (!this->expect(Token::RPAREN, "')'")) { |
| 1161 | return nullptr; |
| 1162 | } |
| 1163 | std::unique_ptr<ASTStatement> statement(this->statement()); |
| 1164 | if (!statement) { |
| 1165 | return nullptr; |
| 1166 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1167 | return std::unique_ptr<ASTForStatement>(new ASTForStatement(start.fOffset, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1168 | std::move(initializer), |
| 1169 | std::move(test), std::move(next), |
| 1170 | std::move(statement))); |
| 1171 | } |
| 1172 | |
| 1173 | /* RETURN expression? SEMICOLON */ |
| 1174 | std::unique_ptr<ASTReturnStatement> Parser::returnStatement() { |
| 1175 | Token start; |
| 1176 | if (!this->expect(Token::RETURN, "'return'", &start)) { |
| 1177 | return nullptr; |
| 1178 | } |
| 1179 | std::unique_ptr<ASTExpression> expression; |
| 1180 | if (this->peek().fKind != Token::SEMICOLON) { |
| 1181 | expression = this->expression(); |
| 1182 | if (!expression) { |
| 1183 | return nullptr; |
| 1184 | } |
| 1185 | } |
| 1186 | if (!this->expect(Token::SEMICOLON, "';'")) { |
| 1187 | return nullptr; |
| 1188 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1189 | return std::unique_ptr<ASTReturnStatement>(new ASTReturnStatement(start.fOffset, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1190 | std::move(expression))); |
| 1191 | } |
| 1192 | |
| 1193 | /* BREAK SEMICOLON */ |
| 1194 | std::unique_ptr<ASTBreakStatement> Parser::breakStatement() { |
| 1195 | Token start; |
| 1196 | if (!this->expect(Token::BREAK, "'break'", &start)) { |
| 1197 | return nullptr; |
| 1198 | } |
| 1199 | if (!this->expect(Token::SEMICOLON, "';'")) { |
| 1200 | return nullptr; |
| 1201 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1202 | return std::unique_ptr<ASTBreakStatement>(new ASTBreakStatement(start.fOffset)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | /* CONTINUE SEMICOLON */ |
| 1206 | std::unique_ptr<ASTContinueStatement> Parser::continueStatement() { |
| 1207 | Token start; |
| 1208 | if (!this->expect(Token::CONTINUE, "'continue'", &start)) { |
| 1209 | return nullptr; |
| 1210 | } |
| 1211 | if (!this->expect(Token::SEMICOLON, "';'")) { |
| 1212 | return nullptr; |
| 1213 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1214 | return std::unique_ptr<ASTContinueStatement>(new ASTContinueStatement(start.fOffset)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | /* DISCARD SEMICOLON */ |
| 1218 | std::unique_ptr<ASTDiscardStatement> Parser::discardStatement() { |
| 1219 | Token start; |
| 1220 | if (!this->expect(Token::DISCARD, "'continue'", &start)) { |
| 1221 | return nullptr; |
| 1222 | } |
| 1223 | if (!this->expect(Token::SEMICOLON, "';'")) { |
| 1224 | return nullptr; |
| 1225 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1226 | return std::unique_ptr<ASTDiscardStatement>(new ASTDiscardStatement(start.fOffset)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | /* LBRACE statement* RBRACE */ |
| 1230 | std::unique_ptr<ASTBlock> Parser::block() { |
ethannicholas | cad6416 | 2016-10-27 10:54:02 -0700 | [diff] [blame] | 1231 | AutoDepth depth(this); |
| 1232 | if (!depth.checkValid()) { |
| 1233 | return nullptr; |
| 1234 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1235 | Token start; |
| 1236 | if (!this->expect(Token::LBRACE, "'{'", &start)) { |
| 1237 | return nullptr; |
| 1238 | } |
| 1239 | std::vector<std::unique_ptr<ASTStatement>> statements; |
| 1240 | for (;;) { |
| 1241 | switch (this->peek().fKind) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1242 | case Token::RBRACE: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1243 | this->nextToken(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1244 | return std::unique_ptr<ASTBlock>(new ASTBlock(start.fOffset, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1245 | std::move(statements))); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1246 | case Token::END_OF_FILE: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1247 | this->error(this->peek(), "expected '}', but found end of file"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1248 | return nullptr; |
| 1249 | default: { |
| 1250 | std::unique_ptr<ASTStatement> statement = this->statement(); |
| 1251 | if (!statement) { |
| 1252 | return nullptr; |
| 1253 | } |
| 1254 | statements.push_back(std::move(statement)); |
| 1255 | } |
| 1256 | } |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | /* expression SEMICOLON */ |
| 1261 | std::unique_ptr<ASTExpressionStatement> Parser::expressionStatement() { |
| 1262 | std::unique_ptr<ASTExpression> expr = this->expression(); |
| 1263 | if (expr) { |
| 1264 | if (this->expect(Token::SEMICOLON, "';'")) { |
| 1265 | ASTExpressionStatement* result = new ASTExpressionStatement(std::move(expr)); |
| 1266 | return std::unique_ptr<ASTExpressionStatement>(result); |
| 1267 | } |
| 1268 | } |
| 1269 | return nullptr; |
| 1270 | } |
| 1271 | |
| 1272 | /* assignmentExpression */ |
| 1273 | std::unique_ptr<ASTExpression> Parser::expression() { |
ethannicholas | cad6416 | 2016-10-27 10:54:02 -0700 | [diff] [blame] | 1274 | AutoDepth depth(this); |
| 1275 | if (!depth.checkValid()) { |
| 1276 | return nullptr; |
| 1277 | } |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1278 | return this->commaExpression(); |
| 1279 | } |
| 1280 | |
| 1281 | /* assignmentExpression (COMMA assignmentExpression)* */ |
| 1282 | std::unique_ptr<ASTExpression> Parser::commaExpression() { |
| 1283 | std::unique_ptr<ASTExpression> result = this->assignmentExpression(); |
| 1284 | if (!result) { |
| 1285 | return nullptr; |
| 1286 | } |
| 1287 | Token t; |
| 1288 | while (this->checkNext(Token::COMMA, &t)) { |
| 1289 | std::unique_ptr<ASTExpression> right = this->commaExpression(); |
| 1290 | if (!right) { |
| 1291 | return nullptr; |
| 1292 | } |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 1293 | result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right))); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1294 | } |
| 1295 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | /* ternaryExpression ((EQEQ | STAREQ | SLASHEQ | PERCENTEQ | PLUSEQ | MINUSEQ | SHLEQ | SHREQ | |
| 1299 | BITWISEANDEQ | BITWISEXOREQ | BITWISEOREQ | LOGICALANDEQ | LOGICALXOREQ | LOGICALOREQ) |
| 1300 | assignmentExpression)* |
| 1301 | */ |
| 1302 | std::unique_ptr<ASTExpression> Parser::assignmentExpression() { |
| 1303 | std::unique_ptr<ASTExpression> result = this->ternaryExpression(); |
| 1304 | if (!result) { |
| 1305 | return nullptr; |
| 1306 | } |
| 1307 | for (;;) { |
| 1308 | switch (this->peek().fKind) { |
| 1309 | case Token::EQ: // fall through |
| 1310 | case Token::STAREQ: // fall through |
| 1311 | case Token::SLASHEQ: // fall through |
| 1312 | case Token::PERCENTEQ: // fall through |
| 1313 | case Token::PLUSEQ: // fall through |
| 1314 | case Token::MINUSEQ: // fall through |
| 1315 | case Token::SHLEQ: // fall through |
| 1316 | case Token::SHREQ: // fall through |
| 1317 | case Token::BITWISEANDEQ: // fall through |
| 1318 | case Token::BITWISEXOREQ: // fall through |
| 1319 | case Token::BITWISEOREQ: // fall through |
| 1320 | case Token::LOGICALANDEQ: // fall through |
| 1321 | case Token::LOGICALXOREQ: // fall through |
| 1322 | case Token::LOGICALOREQ: { |
| 1323 | Token t = this->nextToken(); |
| 1324 | std::unique_ptr<ASTExpression> right = this->assignmentExpression(); |
| 1325 | if (!right) { |
| 1326 | return nullptr; |
| 1327 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1328 | result = std::unique_ptr<ASTExpression>(new ASTBinaryExpression(std::move(result), |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 1329 | std::move(t), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1330 | std::move(right))); |
| 1331 | } |
| 1332 | default: |
| 1333 | return result; |
| 1334 | } |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | /* logicalOrExpression ('?' expression ':' assignmentExpression)? */ |
| 1339 | std::unique_ptr<ASTExpression> Parser::ternaryExpression() { |
| 1340 | std::unique_ptr<ASTExpression> result = this->logicalOrExpression(); |
| 1341 | if (!result) { |
| 1342 | return nullptr; |
| 1343 | } |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 1344 | if (this->checkNext(Token::QUESTION)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1345 | std::unique_ptr<ASTExpression> trueExpr = this->expression(); |
| 1346 | if (!trueExpr) { |
| 1347 | return nullptr; |
| 1348 | } |
| 1349 | if (this->expect(Token::COLON, "':'")) { |
| 1350 | std::unique_ptr<ASTExpression> falseExpr = this->assignmentExpression(); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1351 | return std::unique_ptr<ASTExpression>(new ASTTernaryExpression(std::move(result), |
| 1352 | std::move(trueExpr), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1353 | std::move(falseExpr))); |
| 1354 | } |
| 1355 | return nullptr; |
| 1356 | } |
| 1357 | return result; |
| 1358 | } |
| 1359 | |
| 1360 | /* logicalXorExpression (LOGICALOR logicalXorExpression)* */ |
| 1361 | std::unique_ptr<ASTExpression> Parser::logicalOrExpression() { |
| 1362 | std::unique_ptr<ASTExpression> result = this->logicalXorExpression(); |
| 1363 | if (!result) { |
| 1364 | return nullptr; |
| 1365 | } |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 1366 | Token t; |
| 1367 | while (this->checkNext(Token::LOGICALOR, &t)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1368 | std::unique_ptr<ASTExpression> right = this->logicalXorExpression(); |
| 1369 | if (!right) { |
| 1370 | return nullptr; |
| 1371 | } |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 1372 | result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1373 | } |
| 1374 | return result; |
| 1375 | } |
| 1376 | |
| 1377 | /* logicalAndExpression (LOGICALXOR logicalAndExpression)* */ |
| 1378 | std::unique_ptr<ASTExpression> Parser::logicalXorExpression() { |
| 1379 | std::unique_ptr<ASTExpression> result = this->logicalAndExpression(); |
| 1380 | if (!result) { |
| 1381 | return nullptr; |
| 1382 | } |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 1383 | Token t; |
| 1384 | while (this->checkNext(Token::LOGICALXOR, &t)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1385 | std::unique_ptr<ASTExpression> right = this->logicalAndExpression(); |
| 1386 | if (!right) { |
| 1387 | return nullptr; |
| 1388 | } |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 1389 | result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1390 | } |
| 1391 | return result; |
| 1392 | } |
| 1393 | |
| 1394 | /* bitwiseOrExpression (LOGICALAND bitwiseOrExpression)* */ |
| 1395 | std::unique_ptr<ASTExpression> Parser::logicalAndExpression() { |
| 1396 | std::unique_ptr<ASTExpression> result = this->bitwiseOrExpression(); |
| 1397 | if (!result) { |
| 1398 | return nullptr; |
| 1399 | } |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 1400 | Token t; |
| 1401 | while (this->checkNext(Token::LOGICALAND, &t)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1402 | std::unique_ptr<ASTExpression> right = this->bitwiseOrExpression(); |
| 1403 | if (!right) { |
| 1404 | return nullptr; |
| 1405 | } |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 1406 | result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1407 | } |
| 1408 | return result; |
| 1409 | } |
| 1410 | |
| 1411 | /* bitwiseXorExpression (BITWISEOR bitwiseXorExpression)* */ |
| 1412 | std::unique_ptr<ASTExpression> Parser::bitwiseOrExpression() { |
| 1413 | std::unique_ptr<ASTExpression> result = this->bitwiseXorExpression(); |
| 1414 | if (!result) { |
| 1415 | return nullptr; |
| 1416 | } |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 1417 | Token t; |
| 1418 | while (this->checkNext(Token::BITWISEOR, &t)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1419 | std::unique_ptr<ASTExpression> right = this->bitwiseXorExpression(); |
| 1420 | if (!right) { |
| 1421 | return nullptr; |
| 1422 | } |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 1423 | result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1424 | } |
| 1425 | return result; |
| 1426 | } |
| 1427 | |
| 1428 | /* bitwiseAndExpression (BITWISEXOR bitwiseAndExpression)* */ |
| 1429 | std::unique_ptr<ASTExpression> Parser::bitwiseXorExpression() { |
| 1430 | std::unique_ptr<ASTExpression> result = this->bitwiseAndExpression(); |
| 1431 | if (!result) { |
| 1432 | return nullptr; |
| 1433 | } |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 1434 | Token t; |
| 1435 | while (this->checkNext(Token::BITWISEXOR, &t)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1436 | std::unique_ptr<ASTExpression> right = this->bitwiseAndExpression(); |
| 1437 | if (!right) { |
| 1438 | return nullptr; |
| 1439 | } |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 1440 | result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1441 | } |
| 1442 | return result; |
| 1443 | } |
| 1444 | |
| 1445 | /* equalityExpression (BITWISEAND equalityExpression)* */ |
| 1446 | std::unique_ptr<ASTExpression> Parser::bitwiseAndExpression() { |
| 1447 | std::unique_ptr<ASTExpression> result = this->equalityExpression(); |
| 1448 | if (!result) { |
| 1449 | return nullptr; |
| 1450 | } |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 1451 | Token t; |
| 1452 | while (this->checkNext(Token::BITWISEAND, &t)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1453 | std::unique_ptr<ASTExpression> right = this->equalityExpression(); |
| 1454 | if (!right) { |
| 1455 | return nullptr; |
| 1456 | } |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 1457 | result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1458 | } |
| 1459 | return result; |
| 1460 | } |
| 1461 | |
| 1462 | /* relationalExpression ((EQEQ | NEQ) relationalExpression)* */ |
| 1463 | std::unique_ptr<ASTExpression> Parser::equalityExpression() { |
| 1464 | std::unique_ptr<ASTExpression> result = this->relationalExpression(); |
| 1465 | if (!result) { |
| 1466 | return nullptr; |
| 1467 | } |
| 1468 | for (;;) { |
| 1469 | switch (this->peek().fKind) { |
| 1470 | case Token::EQEQ: // fall through |
| 1471 | case Token::NEQ: { |
| 1472 | Token t = this->nextToken(); |
| 1473 | std::unique_ptr<ASTExpression> right = this->relationalExpression(); |
| 1474 | if (!right) { |
| 1475 | return nullptr; |
| 1476 | } |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 1477 | result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1478 | break; |
| 1479 | } |
| 1480 | default: |
| 1481 | return result; |
| 1482 | } |
| 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | /* shiftExpression ((LT | GT | LTEQ | GTEQ) shiftExpression)* */ |
| 1487 | std::unique_ptr<ASTExpression> Parser::relationalExpression() { |
| 1488 | std::unique_ptr<ASTExpression> result = this->shiftExpression(); |
| 1489 | if (!result) { |
| 1490 | return nullptr; |
| 1491 | } |
| 1492 | for (;;) { |
| 1493 | switch (this->peek().fKind) { |
| 1494 | case Token::LT: // fall through |
| 1495 | case Token::GT: // fall through |
| 1496 | case Token::LTEQ: // fall through |
| 1497 | case Token::GTEQ: { |
| 1498 | Token t = this->nextToken(); |
| 1499 | std::unique_ptr<ASTExpression> right = this->shiftExpression(); |
| 1500 | if (!right) { |
| 1501 | return nullptr; |
| 1502 | } |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 1503 | result.reset(new ASTBinaryExpression(std::move(result), std::move(t), |
| 1504 | std::move(right))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1505 | break; |
| 1506 | } |
| 1507 | default: |
| 1508 | return result; |
| 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | /* additiveExpression ((SHL | SHR) additiveExpression)* */ |
| 1514 | std::unique_ptr<ASTExpression> Parser::shiftExpression() { |
| 1515 | std::unique_ptr<ASTExpression> result = this->additiveExpression(); |
| 1516 | if (!result) { |
| 1517 | return nullptr; |
| 1518 | } |
| 1519 | for (;;) { |
| 1520 | switch (this->peek().fKind) { |
| 1521 | case Token::SHL: // fall through |
| 1522 | case Token::SHR: { |
| 1523 | Token t = this->nextToken(); |
| 1524 | std::unique_ptr<ASTExpression> right = this->additiveExpression(); |
| 1525 | if (!right) { |
| 1526 | return nullptr; |
| 1527 | } |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 1528 | result.reset(new ASTBinaryExpression(std::move(result), std::move(t), |
| 1529 | std::move(right))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1530 | break; |
| 1531 | } |
| 1532 | default: |
| 1533 | return result; |
| 1534 | } |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | /* multiplicativeExpression ((PLUS | MINUS) multiplicativeExpression)* */ |
| 1539 | std::unique_ptr<ASTExpression> Parser::additiveExpression() { |
| 1540 | std::unique_ptr<ASTExpression> result = this->multiplicativeExpression(); |
| 1541 | if (!result) { |
| 1542 | return nullptr; |
| 1543 | } |
| 1544 | for (;;) { |
| 1545 | switch (this->peek().fKind) { |
| 1546 | case Token::PLUS: // fall through |
| 1547 | case Token::MINUS: { |
| 1548 | Token t = this->nextToken(); |
| 1549 | std::unique_ptr<ASTExpression> right = this->multiplicativeExpression(); |
| 1550 | if (!right) { |
| 1551 | return nullptr; |
| 1552 | } |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 1553 | result.reset(new ASTBinaryExpression(std::move(result), std::move(t), |
| 1554 | std::move(right))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1555 | break; |
| 1556 | } |
| 1557 | default: |
| 1558 | return result; |
| 1559 | } |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | /* unaryExpression ((STAR | SLASH | PERCENT) unaryExpression)* */ |
| 1564 | std::unique_ptr<ASTExpression> Parser::multiplicativeExpression() { |
| 1565 | std::unique_ptr<ASTExpression> result = this->unaryExpression(); |
| 1566 | if (!result) { |
| 1567 | return nullptr; |
| 1568 | } |
| 1569 | for (;;) { |
| 1570 | switch (this->peek().fKind) { |
| 1571 | case Token::STAR: // fall through |
| 1572 | case Token::SLASH: // fall through |
| 1573 | case Token::PERCENT: { |
| 1574 | Token t = this->nextToken(); |
| 1575 | std::unique_ptr<ASTExpression> right = this->unaryExpression(); |
| 1576 | if (!right) { |
| 1577 | return nullptr; |
| 1578 | } |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 1579 | result.reset(new ASTBinaryExpression(std::move(result), std::move(t), |
| 1580 | std::move(right))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1581 | break; |
| 1582 | } |
| 1583 | default: |
| 1584 | return result; |
| 1585 | } |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | /* postfixExpression | (PLUS | MINUS | NOT | PLUSPLUS | MINUSMINUS) unaryExpression */ |
| 1590 | std::unique_ptr<ASTExpression> Parser::unaryExpression() { |
| 1591 | switch (this->peek().fKind) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1592 | case Token::PLUS: // fall through |
| 1593 | case Token::MINUS: // fall through |
| 1594 | case Token::LOGICALNOT: // fall through |
| 1595 | case Token::BITWISENOT: // fall through |
| 1596 | case Token::PLUSPLUS: // fall through |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1597 | case Token::MINUSMINUS: { |
| 1598 | Token t = this->nextToken(); |
| 1599 | std::unique_ptr<ASTExpression> expr = this->unaryExpression(); |
| 1600 | if (!expr) { |
| 1601 | return nullptr; |
| 1602 | } |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 1603 | return std::unique_ptr<ASTExpression>(new ASTPrefixExpression(std::move(t), |
| 1604 | std::move(expr))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1605 | } |
| 1606 | default: |
| 1607 | return this->postfixExpression(); |
| 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | /* term suffix* */ |
| 1612 | std::unique_ptr<ASTExpression> Parser::postfixExpression() { |
| 1613 | std::unique_ptr<ASTExpression> result = this->term(); |
| 1614 | if (!result) { |
| 1615 | return nullptr; |
| 1616 | } |
| 1617 | for (;;) { |
| 1618 | switch (this->peek().fKind) { |
| 1619 | case Token::LBRACKET: // fall through |
| 1620 | case Token::DOT: // fall through |
| 1621 | case Token::LPAREN: // fall through |
| 1622 | case Token::PLUSPLUS: // fall through |
| 1623 | case Token::MINUSMINUS: { |
| 1624 | std::unique_ptr<ASTSuffix> s = this->suffix(); |
| 1625 | if (!s) { |
| 1626 | return nullptr; |
| 1627 | } |
| 1628 | result.reset(new ASTSuffixExpression(std::move(result), std::move(s))); |
| 1629 | break; |
| 1630 | } |
| 1631 | default: |
| 1632 | return result; |
| 1633 | } |
| 1634 | } |
| 1635 | } |
| 1636 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1637 | /* LBRACKET expression? RBRACKET | DOT IDENTIFIER | LPAREN parameters RPAREN | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1638 | PLUSPLUS | MINUSMINUS */ |
| 1639 | std::unique_ptr<ASTSuffix> Parser::suffix() { |
| 1640 | Token next = this->nextToken(); |
| 1641 | switch (next.fKind) { |
| 1642 | case Token::LBRACKET: { |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 1643 | if (this->checkNext(Token::RBRACKET)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1644 | return std::unique_ptr<ASTSuffix>(new ASTIndexSuffix(next.fOffset)); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1645 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1646 | std::unique_ptr<ASTExpression> e = this->expression(); |
| 1647 | if (!e) { |
| 1648 | return nullptr; |
| 1649 | } |
| 1650 | this->expect(Token::RBRACKET, "']' to complete array access expression"); |
| 1651 | return std::unique_ptr<ASTSuffix>(new ASTIndexSuffix(std::move(e))); |
| 1652 | } |
| 1653 | case Token::DOT: { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1654 | int offset = this->peek().fOffset; |
| 1655 | StringFragment text; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1656 | if (this->identifier(&text)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1657 | return std::unique_ptr<ASTSuffix>(new ASTFieldSuffix(offset, std::move(text))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1658 | } |
| 1659 | return nullptr; |
| 1660 | } |
| 1661 | case Token::LPAREN: { |
| 1662 | std::vector<std::unique_ptr<ASTExpression>> parameters; |
| 1663 | if (this->peek().fKind != Token::RPAREN) { |
| 1664 | for (;;) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1665 | std::unique_ptr<ASTExpression> expr = this->assignmentExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1666 | if (!expr) { |
| 1667 | return nullptr; |
| 1668 | } |
| 1669 | parameters.push_back(std::move(expr)); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1670 | if (!this->checkNext(Token::COMMA)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1671 | break; |
| 1672 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1673 | } |
| 1674 | } |
| 1675 | this->expect(Token::RPAREN, "')' to complete function parameters"); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1676 | return std::unique_ptr<ASTSuffix>(new ASTCallSuffix(next.fOffset, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1677 | std::move(parameters))); |
| 1678 | } |
| 1679 | case Token::PLUSPLUS: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1680 | return std::unique_ptr<ASTSuffix>(new ASTSuffix(next.fOffset, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1681 | ASTSuffix::kPostIncrement_Kind)); |
| 1682 | case Token::MINUSMINUS: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1683 | return std::unique_ptr<ASTSuffix>(new ASTSuffix(next.fOffset, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1684 | ASTSuffix::kPostDecrement_Kind)); |
| 1685 | default: { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1686 | this->error(next, "expected expression suffix, but found '" + this->text(next) + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1687 | "'\n"); |
| 1688 | return nullptr; |
| 1689 | } |
| 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | /* IDENTIFIER | intLiteral | floatLiteral | boolLiteral | '(' expression ')' */ |
| 1694 | std::unique_ptr<ASTExpression> Parser::term() { |
| 1695 | std::unique_ptr<ASTExpression> result; |
| 1696 | Token t = this->peek(); |
| 1697 | switch (t.fKind) { |
| 1698 | case Token::IDENTIFIER: { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1699 | StringFragment text; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1700 | if (this->identifier(&text)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1701 | result.reset(new ASTIdentifier(t.fOffset, std::move(text))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1702 | } |
| 1703 | break; |
| 1704 | } |
| 1705 | case Token::INT_LITERAL: { |
| 1706 | int64_t i; |
| 1707 | if (this->intLiteral(&i)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1708 | result.reset(new ASTIntLiteral(t.fOffset, i)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1709 | } |
| 1710 | break; |
| 1711 | } |
| 1712 | case Token::FLOAT_LITERAL: { |
| 1713 | double f; |
| 1714 | if (this->floatLiteral(&f)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1715 | result.reset(new ASTFloatLiteral(t.fOffset, f)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1716 | } |
| 1717 | break; |
| 1718 | } |
| 1719 | case Token::TRUE_LITERAL: // fall through |
| 1720 | case Token::FALSE_LITERAL: { |
| 1721 | bool b; |
| 1722 | if (this->boolLiteral(&b)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1723 | result.reset(new ASTBoolLiteral(t.fOffset, b)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1724 | } |
| 1725 | break; |
| 1726 | } |
| 1727 | case Token::LPAREN: { |
| 1728 | this->nextToken(); |
| 1729 | result = this->expression(); |
| 1730 | if (result) { |
| 1731 | this->expect(Token::RPAREN, "')' to complete expression"); |
| 1732 | } |
| 1733 | break; |
| 1734 | } |
| 1735 | default: |
| 1736 | this->nextToken(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1737 | this->error(t.fOffset, "expected expression, but found '" + this->text(t) + "'\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1738 | result = nullptr; |
| 1739 | } |
| 1740 | return result; |
| 1741 | } |
| 1742 | |
| 1743 | /* INT_LITERAL */ |
| 1744 | bool Parser::intLiteral(int64_t* dest) { |
| 1745 | Token t; |
| 1746 | if (this->expect(Token::INT_LITERAL, "integer literal", &t)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1747 | *dest = SkSL::stol(this->text(t)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1748 | return true; |
| 1749 | } |
| 1750 | return false; |
| 1751 | } |
| 1752 | |
| 1753 | /* FLOAT_LITERAL */ |
| 1754 | bool Parser::floatLiteral(double* dest) { |
| 1755 | Token t; |
| 1756 | if (this->expect(Token::FLOAT_LITERAL, "float literal", &t)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1757 | *dest = SkSL::stod(this->text(t)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1758 | return true; |
| 1759 | } |
| 1760 | return false; |
| 1761 | } |
| 1762 | |
| 1763 | /* TRUE_LITERAL | FALSE_LITERAL */ |
| 1764 | bool Parser::boolLiteral(bool* dest) { |
| 1765 | Token t = this->nextToken(); |
| 1766 | switch (t.fKind) { |
| 1767 | case Token::TRUE_LITERAL: |
| 1768 | *dest = true; |
| 1769 | return true; |
| 1770 | case Token::FALSE_LITERAL: |
| 1771 | *dest = false; |
| 1772 | return true; |
| 1773 | default: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1774 | this->error(t, "expected 'true' or 'false', but found '" + this->text(t) + "'\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1775 | return false; |
| 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | /* IDENTIFIER */ |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1780 | bool Parser::identifier(StringFragment* dest) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1781 | Token t; |
| 1782 | if (this->expect(Token::IDENTIFIER, "identifier", &t)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1783 | *dest = this->text(t); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1784 | return true; |
| 1785 | } |
| 1786 | return false; |
| 1787 | } |
| 1788 | |
| 1789 | } // namespace |