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 | #ifndef SKSL_PARSER |
| 9 | #define SKSL_PARSER |
| 10 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 11 | #include <vector> |
| 12 | #include <memory> |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 13 | #include <unordered_map> |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 14 | #include <unordered_set> |
| 15 | #include "SkSLErrorReporter.h" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 16 | #include "ir/SkSLLayout.h" |
Ethan Nicholas | c576e93 | 2017-09-07 15:44:01 -0400 | [diff] [blame^] | 17 | #include "SkSLLexer.h" |
| 18 | #include "SkSLLayoutLexer.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 19 | |
| 20 | struct yy_buffer_state; |
| 21 | #define YY_TYPEDEF_YY_BUFFER_STATE |
| 22 | typedef struct yy_buffer_state *YY_BUFFER_STATE; |
| 23 | |
| 24 | namespace SkSL { |
| 25 | |
| 26 | struct ASTBlock; |
| 27 | struct ASTBreakStatement; |
| 28 | struct ASTContinueStatement; |
| 29 | struct ASTDeclaration; |
| 30 | struct ASTDiscardStatement; |
| 31 | struct ASTDoStatement; |
| 32 | struct ASTExpression; |
| 33 | struct ASTExpressionStatement; |
| 34 | struct ASTForStatement; |
| 35 | struct ASTIfStatement; |
| 36 | struct ASTInterfaceBlock; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 37 | struct ASTParameter; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 38 | struct ASTPrecision; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 39 | struct ASTReturnStatement; |
| 40 | struct ASTStatement; |
| 41 | struct ASTSuffix; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 42 | struct ASTSwitchCase; |
| 43 | struct ASTSwitchStatement; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 44 | struct ASTType; |
| 45 | struct ASTWhileStatement; |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 46 | struct ASTVarDeclarations; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 47 | struct Modifiers; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 48 | class SymbolTable; |
| 49 | |
| 50 | /** |
| 51 | * Consumes .sksl text and produces an abstract syntax tree describing the contents. |
| 52 | */ |
| 53 | class Parser { |
| 54 | public: |
Ethan Nicholas | c576e93 | 2017-09-07 15:44:01 -0400 | [diff] [blame^] | 55 | Parser(const char* text, size_t length, SymbolTable& types, ErrorReporter& errors); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * Consumes a complete .sksl file and produces a list of declarations. Errors are reported via |
| 59 | * the ErrorReporter; the return value may contain some declarations even when errors have |
| 60 | * occurred. |
| 61 | */ |
| 62 | std::vector<std::unique_ptr<ASTDeclaration>> file(); |
| 63 | |
Ethan Nicholas | c576e93 | 2017-09-07 15:44:01 -0400 | [diff] [blame^] | 64 | StringFragment text(Token token); |
| 65 | |
| 66 | Position position(Token token); |
| 67 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 68 | private: |
| 69 | /** |
Ethan Nicholas | c576e93 | 2017-09-07 15:44:01 -0400 | [diff] [blame^] | 70 | * Return the next token, including whitespace tokens, from the parse stream. |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 71 | */ |
Ethan Nicholas | c576e93 | 2017-09-07 15:44:01 -0400 | [diff] [blame^] | 72 | Token nextRawToken(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Return the next non-whitespace token from the parse stream. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 76 | */ |
| 77 | Token nextToken(); |
| 78 | |
| 79 | /** |
| 80 | * Push a token back onto the parse stream, so that it is the next one read. Only a single level |
| 81 | * of pushback is supported (that is, it is an error to call pushback() twice in a row without |
| 82 | * an intervening nextToken()). |
| 83 | */ |
| 84 | void pushback(Token t); |
| 85 | |
| 86 | /** |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 87 | * Returns the next non-whitespace token without consuming it from the stream. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 88 | */ |
| 89 | Token peek(); |
| 90 | |
| 91 | /** |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 92 | * Checks to see if the next token is of the specified type. If so, stores it in result (if |
| 93 | * result is non-null) and returns true. Otherwise, pushes it back and returns false. |
| 94 | */ |
| 95 | bool checkNext(Token::Kind kind, Token* result = nullptr); |
| 96 | |
| 97 | /** |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 98 | * Reads the next non-whitespace token and generates an error if it is not the expected type. |
| 99 | * The 'expected' string is part of the error message, which reads: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 100 | * |
| 101 | * "expected <expected>, but found '<actual text>'" |
| 102 | * |
| 103 | * If 'result' is non-null, it is set to point to the token that was read. |
| 104 | * Returns true if the read token was as expected, false otherwise. |
| 105 | */ |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 106 | bool expect(Token::Kind kind, const char* expected, Token* result = nullptr); |
Ethan Nicholas | c576e93 | 2017-09-07 15:44:01 -0400 | [diff] [blame^] | 107 | bool expect(Token::Kind kind, String expected, Token* result = nullptr); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 108 | |
Ethan Nicholas | c576e93 | 2017-09-07 15:44:01 -0400 | [diff] [blame^] | 109 | void error(Token token, String msg); |
| 110 | void error(int offset, String msg); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 111 | /** |
| 112 | * Returns true if the 'name' identifier refers to a type name. For instance, isType("int") will |
| 113 | * always return true. |
| 114 | */ |
Ethan Nicholas | c576e93 | 2017-09-07 15:44:01 -0400 | [diff] [blame^] | 115 | bool isType(StringFragment name); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 116 | |
| 117 | // these functions parse individual grammar rules from the current parse position; you probably |
| 118 | // don't need to call any of these outside of the parser. The function declarations in the .cpp |
| 119 | // file have comments describing the grammar rules. |
| 120 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 121 | std::unique_ptr<ASTDeclaration> precision(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 122 | |
| 123 | std::unique_ptr<ASTDeclaration> directive(); |
| 124 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 125 | std::unique_ptr<ASTDeclaration> section(); |
| 126 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 127 | std::unique_ptr<ASTDeclaration> declaration(); |
| 128 | |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 129 | std::unique_ptr<ASTVarDeclarations> varDeclarations(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 130 | |
| 131 | std::unique_ptr<ASTType> structDeclaration(); |
| 132 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 133 | std::unique_ptr<ASTVarDeclarations> structVarDeclaration(Modifiers modifiers); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 134 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 135 | std::unique_ptr<ASTVarDeclarations> varDeclarationEnd(Modifiers modifiers, |
| 136 | std::unique_ptr<ASTType> type, |
Ethan Nicholas | c576e93 | 2017-09-07 15:44:01 -0400 | [diff] [blame^] | 137 | StringFragment name); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 138 | |
| 139 | std::unique_ptr<ASTParameter> parameter(); |
| 140 | |
| 141 | int layoutInt(); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 142 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 143 | String layoutCode(); |
| 144 | |
| 145 | Layout::Key layoutKey(); |
| 146 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 147 | Layout layout(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 148 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 149 | Modifiers modifiers(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 150 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 151 | Modifiers modifiersWithDefaults(int defaultFlags); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 152 | |
| 153 | std::unique_ptr<ASTStatement> statement(); |
| 154 | |
| 155 | std::unique_ptr<ASTType> type(); |
| 156 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 157 | std::unique_ptr<ASTDeclaration> interfaceBlock(Modifiers mods); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 158 | |
| 159 | std::unique_ptr<ASTIfStatement> ifStatement(); |
| 160 | |
| 161 | std::unique_ptr<ASTDoStatement> doStatement(); |
| 162 | |
| 163 | std::unique_ptr<ASTWhileStatement> whileStatement(); |
| 164 | |
| 165 | std::unique_ptr<ASTForStatement> forStatement(); |
| 166 | |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 167 | std::unique_ptr<ASTSwitchCase> switchCase(); |
| 168 | |
| 169 | std::unique_ptr<ASTStatement> switchStatement(); |
| 170 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 171 | std::unique_ptr<ASTReturnStatement> returnStatement(); |
| 172 | |
| 173 | std::unique_ptr<ASTBreakStatement> breakStatement(); |
| 174 | |
| 175 | std::unique_ptr<ASTContinueStatement> continueStatement(); |
| 176 | |
| 177 | std::unique_ptr<ASTDiscardStatement> discardStatement(); |
| 178 | |
| 179 | std::unique_ptr<ASTBlock> block(); |
| 180 | |
| 181 | std::unique_ptr<ASTExpressionStatement> expressionStatement(); |
| 182 | |
| 183 | std::unique_ptr<ASTExpression> expression(); |
| 184 | |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 185 | std::unique_ptr<ASTExpression> commaExpression(); |
| 186 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 187 | std::unique_ptr<ASTExpression> assignmentExpression(); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 188 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 189 | std::unique_ptr<ASTExpression> ternaryExpression(); |
| 190 | |
| 191 | std::unique_ptr<ASTExpression> logicalOrExpression(); |
| 192 | |
| 193 | std::unique_ptr<ASTExpression> logicalXorExpression(); |
| 194 | |
| 195 | std::unique_ptr<ASTExpression> logicalAndExpression(); |
| 196 | |
| 197 | std::unique_ptr<ASTExpression> bitwiseOrExpression(); |
| 198 | |
| 199 | std::unique_ptr<ASTExpression> bitwiseXorExpression(); |
| 200 | |
| 201 | std::unique_ptr<ASTExpression> bitwiseAndExpression(); |
| 202 | |
| 203 | std::unique_ptr<ASTExpression> equalityExpression(); |
| 204 | |
| 205 | std::unique_ptr<ASTExpression> relationalExpression(); |
| 206 | |
| 207 | std::unique_ptr<ASTExpression> shiftExpression(); |
| 208 | |
| 209 | std::unique_ptr<ASTExpression> additiveExpression(); |
| 210 | |
| 211 | std::unique_ptr<ASTExpression> multiplicativeExpression(); |
| 212 | |
| 213 | std::unique_ptr<ASTExpression> unaryExpression(); |
| 214 | |
| 215 | std::unique_ptr<ASTExpression> postfixExpression(); |
| 216 | |
| 217 | std::unique_ptr<ASTSuffix> suffix(); |
| 218 | |
| 219 | std::unique_ptr<ASTExpression> term(); |
| 220 | |
| 221 | bool intLiteral(int64_t* dest); |
| 222 | |
| 223 | bool floatLiteral(double* dest); |
| 224 | |
| 225 | bool boolLiteral(bool* dest); |
| 226 | |
Ethan Nicholas | c576e93 | 2017-09-07 15:44:01 -0400 | [diff] [blame^] | 227 | bool identifier(StringFragment* dest); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 228 | |
Ethan Nicholas | c576e93 | 2017-09-07 15:44:01 -0400 | [diff] [blame^] | 229 | const char* fText; |
| 230 | Lexer fLexer; |
| 231 | LayoutLexer fLayoutLexer; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 232 | YY_BUFFER_STATE fBuffer; |
ethannicholas | cad6416 | 2016-10-27 10:54:02 -0700 | [diff] [blame] | 233 | // current parse depth, used to enforce a recursion limit to try to keep us from overflowing the |
| 234 | // stack on pathological inputs |
| 235 | int fDepth = 0; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 236 | Token fPushback; |
| 237 | SymbolTable& fTypes; |
| 238 | ErrorReporter& fErrors; |
ethannicholas | cad6416 | 2016-10-27 10:54:02 -0700 | [diff] [blame] | 239 | |
| 240 | friend class AutoDepth; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | } // namespace |
| 244 | |
| 245 | #endif |