1 | // Copyright (C) 2021 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #ifndef QSSG_GLSLLEXER_H |
5 | #define QSSG_GLSLLEXER_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtQuick3DGlslParser/private/glsl_p.h> |
19 | #include <QtCore/qstring.h> |
20 | #include <qstringlist.h> |
21 | |
22 | QT_BEGIN_NAMESPACE |
23 | |
24 | namespace GLSL { |
25 | |
26 | class Q_QUICK3DGLSLPARSER_EXPORT Token |
27 | { |
28 | public: |
29 | int kind; |
30 | int position; |
31 | int length; |
32 | int line; // ### remove |
33 | |
34 | union { |
35 | int matchingBrace; |
36 | int i; // integer value |
37 | const QString *string; // string value |
38 | void *ptr; |
39 | }; |
40 | |
41 | Token() |
42 | : kind(0), position(0), length(0), line(0), ptr(nullptr) {} |
43 | |
44 | bool is(int k) const { return k == kind; } |
45 | bool isNot(int k) const { return k != kind; } |
46 | |
47 | int begin() const { return position; } |
48 | int end() const { return position + length; } |
49 | }; |
50 | |
51 | class Q_QUICK3DGLSLPARSER_EXPORT Lexer |
52 | { |
53 | public: |
54 | Lexer(Engine *engine, const char *source, unsigned size); |
55 | ~Lexer(); |
56 | |
57 | enum Variant { |
58 | // Extra flag bits added to tokens by Lexer::classify() that |
59 | // indicate which variant of GLSL the keyword belongs to. |
60 | Variant_GLSL_120 = 0x00010000, // 1.20 and higher |
61 | Variant_GLSL_150 = 0x00020000, // 1.50 and higher |
62 | Variant_GLSL_400 = 0x00040000, // 4.00 and higher |
63 | Variant_GLSL_ES_100 = 0x00080000, // ES 1.00 and higher |
64 | Variant_VertexShader = 0x00200000, |
65 | Variant_FragmentShader = 0x00400000, |
66 | Variant_Reserved = 0x80000000, |
67 | Variant_Mask = 0xFFFF0000, |
68 | Variant_All = 0xFFFF0000 |
69 | }; |
70 | |
71 | union Value { |
72 | int i; |
73 | const QString *string; |
74 | void *ptr; |
75 | }; |
76 | |
77 | Engine *engine() const { return _engine; } |
78 | |
79 | int state() const { return _state; } |
80 | void setState(int state) { _state = state; } |
81 | |
82 | int variant() const { return _variant; } |
83 | void setVariant(int flags) { _variant = flags; } |
84 | |
85 | bool scanKeywords() const { return _scanKeywords; } |
86 | void setScanKeywords(bool scanKeywords) { _scanKeywords = scanKeywords; } |
87 | |
88 | bool () const { return _scanComments; } |
89 | void (bool ) { _scanComments = scanComments; } |
90 | |
91 | int yylex(Token *tk); |
92 | int findKeyword(const char *word, int length) const; |
93 | |
94 | void *yyval() const { return _yyval.ptr; } |
95 | |
96 | static QStringList keywords(int variant); |
97 | |
98 | private: |
99 | static int classify(const char *s, int len); |
100 | |
101 | void yyinp(); |
102 | int yylex_helper(const char **position, int *line); |
103 | |
104 | void warning(int line, const QString &message); |
105 | void error(int line, const QString &message); |
106 | |
107 | private: |
108 | Engine *_engine; |
109 | const char *_source; |
110 | const char *_it; |
111 | QT_WARNING_PUSH QT_WARNING_DISABLE_GCC("-Wattributes" ) |
112 | [[maybe_unused]] int _size; |
113 | QT_WARNING_POP |
114 | int _yychar; |
115 | int _lineno; |
116 | int _state; |
117 | int _variant; |
118 | unsigned _scanKeywords: 1; |
119 | unsigned : 1; |
120 | Value _yyval; |
121 | }; |
122 | |
123 | } // namespace GLSL |
124 | |
125 | QT_END_NAMESPACE |
126 | |
127 | #endif // QSSG_GLSLLEXER_H |
128 | |