glslparser.h 6.33 KB
Newer Older
1

Erik Verbruggen's avatar
Erik Verbruggen committed
2
#line 218 "./glsl.g"
3

4
5
6
7
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
8
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
9
10
11
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
con's avatar
con committed
12
** No Commercial Usage
13
**
con's avatar
con committed
14
15
16
17
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
18
19
20
21
22
23
24
25
26
27
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
con's avatar
con committed
28
29
30
31
32
33
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
34
35
36
**
**************************************************************************/

37
38
39
#include "glslparsertable_p.h"
#include "glsllexer.h"
#include "glslast.h"
40
#include "glslengine.h"
41
42
43
44
45
#include <vector>
#include <stack>

namespace GLSL {

46
class GLSL_EXPORT Parser: public GLSLParserTable
47
48
{
public:
49
50
    union Value {
        void *ptr;
51
        const QString *string;
52
53
        AST *ast;
        List<AST *> *ast_list;
54
55
56
57
58
59
60
61
62
63
64
        DeclarationAST *declaration;
        List<DeclarationAST *> *declaration_list;
        ExpressionAST *expression;
        List<ExpressionAST *> *expression_list;
        StatementAST *statement;
        List<StatementAST *> *statement_list;
        TypeAST *type;
        StructTypeAST::Field *field;
        List<StructTypeAST::Field *> *field_list;
        TranslationUnitAST *translation_unit;
        FunctionIdentifierAST *function_identifier;
65
        AST::Kind kind;
66
        TypeAST::Precision precision;
67
        struct {
68
69
            StatementAST *thenClause;
            StatementAST *elseClause;
70
71
        } ifstmt;
        struct {
72
73
            ExpressionAST *condition;
            ExpressionAST *increment;
74
75
        } forstmt;
        struct {
76
77
            FunctionIdentifierAST *id;
            List<ExpressionAST *> *arguments;
78
        } function;
79
80
81
82
83
84
85
        int qualifier;
        LayoutQualifier *layout;
        List<LayoutQualifier *> *layout_list;
        struct {
            int qualifier;
            List<LayoutQualifier *> *layout_list;
        } type_qualifier;
86
        struct {
87
            TypeAST *type;
88
            const QString *name;
89
        } param_declarator;
90
91
        ParameterDeclarationAST *param_declaration;
        FunctionDeclarationAST *function_declaration;
92
93
94
    };

    Parser(Engine *engine, const char *source, unsigned size, int variant);
95
96
    ~Parser();

97
98
99
100
101
102
103
104
105
106
107
108
109
    TranslationUnitAST *parse() {
        if (AST *u = parse(T_FEED_GLSL))
            return u->asTranslationUnit();
        return 0;
    }

    ExpressionAST *parseExpression() {
        if (AST *u = parse(T_FEED_EXPRESSION))
            return u->asExpression();
        return 0;
    }

    AST *parse(int startToken);
110
111

private:
112
    // 1-based
113
    int &location(int n) { return _locationStack[_tos + n - 1]; }
114
115
    Value &sym(int n) { return _symStack[_tos + n - 1]; }
    AST *&ast(int n) { return _symStack[_tos + n - 1].ast; }
116
    const QString *&string(int n) { return _symStack[_tos + n - 1].string; }
117
118
119
120
    ExpressionAST *&expression(int n) { return _symStack[_tos + n - 1].expression; }
    StatementAST *&statement(int n) { return _symStack[_tos + n - 1].statement; }
    TypeAST *&type(int n) { return _symStack[_tos + n - 1].type; }
    FunctionDeclarationAST *&function(int n) { return _symStack[_tos + n - 1].function_declaration; }
121

Roberto Raggi's avatar
Roberto Raggi committed
122
123
124
125
126
    inline int consumeToken() {
        if (_index < int(_tokens.size()))
            return _index++;
        return _tokens.size() - 1;
    }
127
128
129
130
131
132
133
134
135
136
    inline const Token &tokenAt(int index) const {
        if (index == 0)
            return _startToken;
        return _tokens.at(index);
    }
    inline int tokenKind(int index) const {
        if (index == 0)
            return _startToken.kind;
        return _tokens.at(index).kind;
    }
137
    void reduce(int ruleno);
138

Roberto Raggi's avatar
Roberto Raggi committed
139
140
    void warning(int line, const QString &message)
    {
141
        _engine->warning(line, message);
Roberto Raggi's avatar
Roberto Raggi committed
142
143
144
145
    }

    void error(int line, const QString &message)
    {
146
        _engine->error(line, message);
Roberto Raggi's avatar
Roberto Raggi committed
147
148
    }

149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    template <typename T>
    T *makeAstNode()
    {
        T *node = new (_engine->pool()) T ();
        node->lineno = yyloc >= 0 ? (_tokens[yyloc].line + 1) : 0;
        return node;
    }

    template <typename T, typename A1>
    T *makeAstNode(A1 a1)
    {
        T *node = new (_engine->pool()) T (a1);
        node->lineno = yyloc >= 0 ? (_tokens[yyloc].line + 1) : 0;
        return node;
    }

    template <typename T, typename A1, typename A2>
    T *makeAstNode(A1 a1, A2 a2)
    {
        T *node = new (_engine->pool()) T (a1, a2);
        node->lineno = yyloc >= 0 ? (_tokens[yyloc].line + 1) : 0;
        return node;
    }

    template <typename T, typename A1, typename A2, typename A3>
    T *makeAstNode(A1 a1, A2 a2, A3 a3)
    {
        T *node = new (_engine->pool()) T (a1, a2, a3);
        node->lineno = yyloc >= 0 ? (_tokens[yyloc].line + 1) : 0;
        return node;
    }

    template <typename T, typename A1, typename A2, typename A3, typename A4>
    T *makeAstNode(A1 a1, A2 a2, A3 a3, A4 a4)
    {
        T *node = new (_engine->pool()) T (a1, a2, a3, a4);
        node->lineno = yyloc >= 0 ? (_tokens[yyloc].line + 1) : 0;
        return node;
    }

189
    TypeAST *makeBasicType(int token)
190
    {
191
        TypeAST *type = new (_engine->pool()) BasicTypeAST(token, spell[token]);
192
193
194
195
        type->lineno = yyloc >= 0 ? (_tokens[yyloc].line + 1) : 0;
        return type;
    }

196
private:
197
    Engine *_engine;
198
199
    int _tos;
    int _index;
200
    int yyloc;
Roberto Raggi's avatar
Roberto Raggi committed
201
202
203
    int yytoken;
    int yyrecovering;
    bool _recovered;
204
    Token _startToken;
205
206
    std::vector<int> _stateStack;
    std::vector<int> _locationStack;
207
    std::vector<Value> _symStack;
208
209
210
    std::vector<Token> _tokens;
};

211
} // namespace GLSL