1 2 #line 210 "./glsl.g" 3 4 /**************************************************************************** 5 ** 6 ** Copyright (C) 2016 The Qt Company Ltd. 7 ** Contact: https://www.qt.io/licensing/ 8 ** 9 ** This file is part of Qt Creator. 10 ** 11 ** Commercial License Usage 12 ** Licensees holding valid commercial Qt licenses may use this file in 13 ** accordance with the commercial license agreement provided with the 14 ** Software or, alternatively, in accordance with the terms contained in 15 ** a written agreement between you and The Qt Company. For licensing terms 16 ** and conditions see https://www.qt.io/terms-conditions. For further 17 ** information use the contact form at https://www.qt.io/contact-us. 18 ** 19 ** GNU General Public License Usage 20 ** Alternatively, this file may be used under the terms of the GNU 21 ** General Public License version 3 as published by the Free Software 22 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT 23 ** included in the packaging of this file. Please review the following 24 ** information to ensure the GNU General Public License requirements will 25 ** be met: https://www.gnu.org/licenses/gpl-3.0.html. 26 ** 27 ****************************************************************************/ 28 29 #pragma once 30 31 #include "glslparsertable_p.h" 32 #include "glsllexer.h" 33 #include "glslast.h" 34 #include "glslengine.h" 35 #include <vector> 36 #include <stack> 37 38 namespace GLSL { 39 40 class GLSL_EXPORT Parser: public GLSLParserTable 41 { 42 public: 43 union Value { 44 void *ptr; 45 const QString *string; 46 AST *ast; 47 List<AST *> *ast_list; 48 DeclarationAST *declaration; 49 List<DeclarationAST *> *declaration_list; 50 ExpressionAST *expression; 51 List<ExpressionAST *> *expression_list; 52 StatementAST *statement; 53 List<StatementAST *> *statement_list; 54 TypeAST *type; 55 StructTypeAST::Field *field; 56 List<StructTypeAST::Field *> *field_list; 57 TranslationUnitAST *translation_unit; 58 FunctionIdentifierAST *function_identifier; 59 AST::Kind kind; 60 TypeAST::Precision precision; 61 struct { 62 StatementAST *thenClause; 63 StatementAST *elseClause; 64 } ifstmt; 65 struct { 66 ExpressionAST *condition; 67 ExpressionAST *increment; 68 } forstmt; 69 struct { 70 FunctionIdentifierAST *id; 71 List<ExpressionAST *> *arguments; 72 } function; 73 int qualifier; 74 LayoutQualifierAST *layout; 75 List<LayoutQualifierAST *> *layout_list; 76 struct { 77 int qualifier; 78 List<LayoutQualifierAST *> *layout_list; 79 } type_qualifier; 80 struct { 81 TypeAST *type; 82 const QString *name; 83 } param_declarator; 84 ParameterDeclarationAST *param_declaration; 85 FunctionDeclarationAST *function_declaration; 86 }; 87 88 Parser(Engine *engine, const char *source, unsigned size, int variant); 89 ~Parser(); 90 parse()91 TranslationUnitAST *parse() { 92 if (AST *u = parse(T_FEED_GLSL)) 93 return u->asTranslationUnit(); 94 return nullptr; 95 } 96 parseExpression()97 ExpressionAST *parseExpression() { 98 if (AST *u = parse(T_FEED_EXPRESSION)) 99 return u->asExpression(); 100 return nullptr; 101 } 102 103 AST *parse(int startToken); 104 105 private: 106 // 1-based location(int n)107 int &location(int n) { return _locationStack[_tos + n - 1]; } sym(int n)108 Value &sym(int n) { return _symStack[_tos + n - 1]; } ast(int n)109 AST *&ast(int n) { return _symStack[_tos + n - 1].ast; } string(int n)110 const QString *&string(int n) { return _symStack[_tos + n - 1].string; } expression(int n)111 ExpressionAST *&expression(int n) { return _symStack[_tos + n - 1].expression; } statement(int n)112 StatementAST *&statement(int n) { return _symStack[_tos + n - 1].statement; } type(int n)113 TypeAST *&type(int n) { return _symStack[_tos + n - 1].type; } function(int n)114 FunctionDeclarationAST *&function(int n) { return _symStack[_tos + n - 1].function_declaration; } 115 consumeToken()116 inline int consumeToken() { 117 if (_index < int(_tokens.size())) 118 return _index++; 119 return static_cast<int>(_tokens.size()) - 1; 120 } tokenAt(int index)121 inline const Token &tokenAt(int index) const { 122 if (index == 0) 123 return _startToken; 124 return _tokens.at(index); 125 } tokenKind(int index)126 inline int tokenKind(int index) const { 127 if (index == 0) 128 return _startToken.kind; 129 return _tokens.at(index).kind; 130 } 131 void reduce(int ruleno); 132 warning(int line,const QString & message)133 void warning(int line, const QString &message) 134 { 135 _engine->warning(line, message); 136 } 137 error(int line,const QString & message)138 void error(int line, const QString &message) 139 { 140 _engine->error(line, message); 141 } 142 143 template <typename T> makeAstNode()144 T *makeAstNode() 145 { 146 T *node = new (_engine->pool()) T (); 147 node->lineno = yyloc >= 0 ? (_tokens[yyloc].line + 1) : 0; 148 return node; 149 } 150 151 template <typename T, typename A1> makeAstNode(A1 a1)152 T *makeAstNode(A1 a1) 153 { 154 T *node = new (_engine->pool()) T (a1); 155 node->lineno = yyloc >= 0 ? (_tokens[yyloc].line + 1) : 0; 156 return node; 157 } 158 159 template <typename T, typename A1, typename A2> makeAstNode(A1 a1,A2 a2)160 T *makeAstNode(A1 a1, A2 a2) 161 { 162 T *node = new (_engine->pool()) T (a1, a2); 163 node->lineno = yyloc >= 0 ? (_tokens[yyloc].line + 1) : 0; 164 return node; 165 } 166 167 template <typename T, typename A1, typename A2, typename A3> makeAstNode(A1 a1,A2 a2,A3 a3)168 T *makeAstNode(A1 a1, A2 a2, A3 a3) 169 { 170 T *node = new (_engine->pool()) T (a1, a2, a3); 171 node->lineno = yyloc >= 0 ? (_tokens[yyloc].line + 1) : 0; 172 return node; 173 } 174 175 template <typename T, typename A1, typename A2, typename A3, typename A4> makeAstNode(A1 a1,A2 a2,A3 a3,A4 a4)176 T *makeAstNode(A1 a1, A2 a2, A3 a3, A4 a4) 177 { 178 T *node = new (_engine->pool()) T (a1, a2, a3, a4); 179 node->lineno = yyloc >= 0 ? (_tokens[yyloc].line + 1) : 0; 180 return node; 181 } 182 makeBasicType(int token)183 TypeAST *makeBasicType(int token) 184 { 185 TypeAST *type = new (_engine->pool()) BasicTypeAST(token, spell[token]); 186 type->lineno = yyloc >= 0 ? (_tokens[yyloc].line + 1) : 0; 187 return type; 188 } 189 190 private: 191 Engine *_engine; 192 int _tos; 193 int _index; 194 int yyloc; 195 int yytoken; 196 int yyrecovering; 197 bool _recovered; 198 Token _startToken; 199 std::vector<int> _stateStack; 200 std::vector<int> _locationStack; 201 std::vector<Value> _symStack; 202 std::vector<Token> _tokens; 203 }; 204 205 } // namespace GLSL 206