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