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