-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathlexer.h
162 lines (130 loc) · 2.49 KB
/
lexer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#ifndef LEXER_HDR
#define LEXER_HDR
#include "util.h"
namespace glsl {
#define KEYWORD(...)
#define OPERATOR(...)
#define TYPENAME(...)
// Types
#define TYPE(X) kType_##X,
enum {
#include "lexemes.h"
};
#undef TYPE
#define TYPE(...)
// Keywords
#undef KEYWORD
#define KEYWORD(X) kKeyword_##X,
enum {
#include "lexemes.h"
};
#undef KEYWORD
#define KEYWORD(...)
// Operators
#undef OPERATOR
#define OPERATOR(X, ...) kOperator_##X,
enum {
#include "lexemes.h"
};
#undef OPERATOR
#define OPERATOR(...)
enum {
kCore,
kCompatibility,
kES
};
enum {
kEnable,
kRequire,
kWarn,
kDisable
};
struct keywordInfo {
const char *name;
int type;
};
struct operatorInfo {
const char *name;
const char *string;
int precedence;
};
struct directive {
enum {
kVersion,
kExtension
};
int type; // kVersion, kExtension
union {
struct {
int version;
int type; // kCore, kCompatibility, kES
} asVersion;
struct {
char* name;
int behavior; // kEnable, kRequire, kWarn, kDisable
} asExtension;
};
};
struct token {
int precedence() const;
private:
token();
friend struct lexer;
friend struct parser;
int m_type;
union {
char *asIdentifier;
directive asDirective;
int asInt;
int asKeyword;
int asOperator;
unsigned asUnsigned;
float asFloat;
double asDouble;
};
};
struct location {
location();
size_t column;
size_t line;
size_t position;
private:
friend struct lexer;
void advanceColumn(size_t count = 1);
void advanceLine();
};
struct lexer {
lexer(const char *data);
token read();
token peek();
const char *error() const;
void backup();
void restore();
size_t line() const;
size_t column() const;
protected:
friend struct parser;
size_t position() const;
int at(int offset = 0) const;
void read(token &out);
void read(token &out, bool);
void skipWhitespace(bool allowNewlines = false);
vector<char> readNumeric(bool isOctal, bool isHex);
private:
const char *m_data;
size_t m_length;
const char *m_error;
location m_location;
location m_backup;
};
inline size_t lexer::position() const {
return m_location.position;
}
inline size_t lexer::line() const {
return m_location.line;
}
inline size_t lexer::column() const {
return m_location.column;
}
}
#endif