OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / libs / glsl / glsllexer.h
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #ifndef GLSLLEXER_H
35 #define GLSLLEXER_H
36
37 #include "glsl.h"
38 #include <QtCore/qstring.h>
39 #include <QtCore/qstringlist.h>
40
41 namespace GLSL {
42
43 class GLSL_EXPORT Token
44 {
45 public:
46     int kind;
47     int position;
48     int length;
49     int line; // ### remove
50
51     union {
52         int matchingBrace;
53         int i; // integer value
54         const QString *string; // string value
55         void *ptr;
56     };
57
58     Token()
59         : kind(0), position(0), length(0), line(0), ptr(0) {}
60
61     bool is(int k) const { return k == kind; }
62     bool isNot(int k) const { return k != kind; }
63
64     int begin() const { return position; }
65     int end() const { return position + length; }
66 };
67
68 class GLSL_EXPORT Lexer
69 {
70 public:
71     Lexer(Engine *engine, const char *source, unsigned size);
72     ~Lexer();
73
74     enum
75     {
76         // Extra flag bits added to tokens by Lexer::classify() that
77         // indicate which variant of GLSL the keyword belongs to.
78         Variant_GLSL_120            = 0x00010000,   // 1.20 and higher
79         Variant_GLSL_150            = 0x00020000,   // 1.50 and higher
80         Variant_GLSL_400            = 0x00040000,   // 4.00 and higher
81         Variant_GLSL_ES_100         = 0x00080000,   // ES 1.00 and higher
82         Variant_VertexShader        = 0x00200000,
83         Variant_FragmentShader      = 0x00400000,
84         Variant_Reserved            = 0x80000000,
85         Variant_Mask                = 0xFFFF0000,
86         Variant_All                 = 0xFFFF0000
87     };
88
89     union Value {
90         int i;
91         const QString *string;
92         void *ptr;
93     };
94
95     Engine *engine() const { return _engine; }
96
97     int state() const { return _state; }
98     void setState(int state) { _state = state; }
99
100     int variant() const { return _variant; }
101     void setVariant(int flags) { _variant = flags; }
102
103     bool scanKeywords() const { return _scanKeywords; }
104     void setScanKeywords(bool scanKeywords) { _scanKeywords = scanKeywords; }
105
106     bool scanComments() const { return _scanComments; }
107     void setScanComments(bool scanComments) { _scanComments = scanComments; }
108
109     int yylex(Token *tk);
110     int findKeyword(const char *word, int length) const;
111
112     void *yyval() const { return _yyval.ptr; }
113
114     static QStringList keywords(int variant);
115
116 private:
117     static int classify(const char *s, int len);
118
119     void yyinp();
120     int yylex_helper(const char **position, int *line);
121
122     void warning(int line, const QString &message);
123     void error(int line, const QString &message);
124
125 private:
126     Engine *_engine;
127     const char *_source;
128     const char *_it;
129     int _size;
130     int _yychar;
131     int _lineno;
132     int _state;
133     int _variant;
134     unsigned _scanKeywords: 1;
135     unsigned _scanComments: 1;
136     Value _yyval;
137 };
138
139 } // end of namespace GLSL
140
141 #endif // GLSLLEXER_H