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