Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 <string> | |
| 12 #include <vector> | |
| 13 #include <unordered_set> | |
| 14 #include "SkSLErrorReporter.h" | |
| 15 #include "SkSLToken.h" | |
| 16 | |
| 17 struct yy_buffer_state; | |
| 18 #define YY_TYPEDEF_YY_BUFFER_STATE | |
| 19 typedef struct yy_buffer_state *YY_BUFFER_STATE; | |
| 20 | |
| 21 namespace SkSL { | |
| 22 | |
| 23 struct ASTBlock; | |
| 24 struct ASTBreakStatement; | |
| 25 struct ASTContinueStatement; | |
| 26 struct ASTDeclaration; | |
| 27 struct ASTDiscardStatement; | |
| 28 struct ASTDoStatement; | |
| 29 struct ASTExpression; | |
| 30 struct ASTExpressionStatement; | |
| 31 struct ASTForStatement; | |
| 32 struct ASTIfStatement; | |
| 33 struct ASTInterfaceBlock; | |
| 34 struct ASTLayout; | |
| 35 struct ASTModifiers; | |
| 36 struct ASTParameter; | |
| 37 struct ASTReturnStatement; | |
| 38 struct ASTStatement; | |
| 39 struct ASTSuffix; | |
| 40 struct ASTType; | |
| 41 struct ASTWhileStatement; | |
| 42 struct ASTVarDeclaration; | |
| 43 class SymbolTable; | |
| 44 | |
| 45 /** | |
| 46 * Consumes .sksl text and produces an abstract syntax tree describing the conte nts. | |
| 47 */ | |
| 48 class Parser { | |
| 49 public: | |
| 50 Parser(std::string text, std::shared_ptr<SymbolTable> types, ErrorReporter& errors); | |
|
dogben
2016/06/21 17:53:48
nit: Would "const std::string& text" work here?
n
| |
| 51 | |
| 52 ~Parser(); | |
| 53 | |
| 54 /** | |
| 55 * Consumes a complete .sksl file and produces a list of declarations. Error s are reported via | |
| 56 * the ErrorReporter; the return value may contain some declarations even wh en errors have | |
| 57 * occurred. | |
| 58 */ | |
| 59 std::vector<std::unique_ptr<ASTDeclaration>> file(); | |
| 60 | |
| 61 // these functions parse individual grammar rules from the current parse pos ition; you probably | |
| 62 // don't need to call any of these outside of the parser. The function decla rations in the .cpp | |
| 63 // file have comments describing the grammar rules. | |
| 64 | |
| 65 std::unique_ptr<ASTDeclaration> directive(); | |
| 66 | |
| 67 void precision(); | |
| 68 | |
| 69 std::unique_ptr<ASTDeclaration> declaration(); | |
| 70 | |
| 71 std::unique_ptr<ASTType> type(); | |
| 72 | |
| 73 std::unique_ptr<ASTParameter> parameter(); | |
| 74 | |
| 75 ASTLayout layout(); | |
| 76 | |
| 77 ASTModifiers modifiers(); | |
| 78 | |
| 79 std::unique_ptr<ASTStatement> statement(); | |
| 80 | |
| 81 std::unique_ptr<ASTType> structDeclaration(); | |
| 82 | |
| 83 std::unique_ptr<ASTVarDeclaration> varDeclaration(); | |
| 84 | |
| 85 std::unique_ptr<ASTDeclaration> interfaceBlock(ASTModifiers mods); | |
| 86 | |
| 87 std::unique_ptr<ASTIfStatement> ifStatement(); | |
| 88 | |
| 89 std::unique_ptr<ASTWhileStatement> whileStatement(); | |
| 90 | |
| 91 std::unique_ptr<ASTDoStatement> doStatement(); | |
| 92 | |
| 93 std::unique_ptr<ASTForStatement> forStatement(); | |
| 94 | |
| 95 std::unique_ptr<ASTReturnStatement> returnStatement(); | |
| 96 | |
| 97 std::unique_ptr<ASTBreakStatement> breakStatement(); | |
| 98 | |
| 99 std::unique_ptr<ASTContinueStatement> continueStatement(); | |
| 100 | |
| 101 std::unique_ptr<ASTDiscardStatement> discardStatement(); | |
| 102 | |
| 103 std::unique_ptr<ASTBlock> block(); | |
| 104 | |
| 105 std::unique_ptr<ASTExpressionStatement> expressionStatement(); | |
| 106 | |
| 107 std::unique_ptr<ASTExpression> expression(); | |
| 108 | |
| 109 std::unique_ptr<ASTExpression> assignmentExpression(); | |
| 110 | |
| 111 std::unique_ptr<ASTExpression> ternaryExpression(); | |
| 112 | |
| 113 std::unique_ptr<ASTExpression> logicalOrExpression(); | |
| 114 | |
| 115 std::unique_ptr<ASTExpression> logicalXorExpression(); | |
| 116 | |
| 117 std::unique_ptr<ASTExpression> logicalAndExpression(); | |
| 118 | |
| 119 std::unique_ptr<ASTExpression> bitwiseOrExpression(); | |
| 120 | |
| 121 std::unique_ptr<ASTExpression> bitwiseXorExpression(); | |
| 122 | |
| 123 std::unique_ptr<ASTExpression> bitwiseAndExpression(); | |
| 124 | |
| 125 std::unique_ptr<ASTExpression> equalityExpression(); | |
| 126 | |
| 127 std::unique_ptr<ASTExpression> relationalExpression(); | |
| 128 | |
| 129 std::unique_ptr<ASTExpression> shiftExpression(); | |
| 130 | |
| 131 std::unique_ptr<ASTExpression> additiveExpression(); | |
| 132 | |
| 133 std::unique_ptr<ASTExpression> multiplicativeExpression(); | |
| 134 | |
| 135 std::unique_ptr<ASTExpression> unaryExpression(); | |
| 136 | |
| 137 std::unique_ptr<ASTExpression> postfixExpression(); | |
| 138 | |
| 139 std::unique_ptr<ASTSuffix> suffix(); | |
| 140 | |
| 141 std::unique_ptr<ASTExpression> term(); | |
| 142 | |
| 143 bool intLiteral(int64_t* dest); | |
| 144 | |
| 145 bool floatLiteral(double* dest); | |
| 146 | |
| 147 bool boolLiteral(bool* dest); | |
| 148 | |
| 149 bool identifier(std::string* dest); | |
| 150 | |
| 151 std::unique_ptr<ASTVarDeclaration> varDeclarationEnd(ASTModifiers modifiers, | |
| 152 std::unique_ptr<ASTType > type, | |
| 153 std::string name); | |
| 154 | |
| 155 /** | |
| 156 * Return the next token from the parse stream. | |
| 157 */ | |
| 158 Token nextToken(); | |
| 159 | |
| 160 /** | |
| 161 * Push a token back onto the parse stream, so that it is the next one read. Only a single level | |
| 162 * of pushback is supported (that is, it is an error to call pushback() twic e in a row without | |
| 163 * an intervening nextToken()). | |
|
dogben
2016/06/21 17:53:48
nit: Maybe also note that you can't call pushback
| |
| 164 */ | |
| 165 void pushback(Token t); | |
| 166 | |
| 167 /** | |
| 168 * Returns the next token without consuming it from the stream. | |
| 169 */ | |
| 170 Token peek(); | |
| 171 | |
| 172 /** | |
| 173 * Reads the next token and generates an error if it is not the expected typ e. The 'expected' | |
| 174 * string is part of the error message, which reads: | |
| 175 * | |
| 176 * "expected <expected>, but found '<actual text>'" | |
| 177 * | |
| 178 * If 'result' is non-null, it is set to point to the token that was read. | |
| 179 * Returns true if the read token was as expected, false otherwise. | |
| 180 */ | |
| 181 bool expect(Token::Kind kind, std::string expected, Token* result = nullptr) ; | |
| 182 | |
| 183 void error(Position p, std::string msg); | |
| 184 | |
| 185 /** | |
| 186 * Returns true if the 'name' identifier refers to a type name. For instance , isType("int") will | |
| 187 * always return true. | |
| 188 */ | |
| 189 bool isType(std::string name); | |
| 190 | |
| 191 void* fScanner; | |
|
dogben
2016/06/21 17:53:48
nit: Any reason not to make fields and helper meth
| |
| 192 YY_BUFFER_STATE fBuffer; | |
| 193 Token fPushback; | |
| 194 std::shared_ptr<SymbolTable> fTypes; | |
| 195 ErrorReporter& fErrors; | |
| 196 }; | |
| 197 | |
| 198 } // namespace | |
| 199 | |
| 200 #endif | |
| OLD | NEW |