-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScanner.hpp
More file actions
209 lines (169 loc) · 3.54 KB
/
Scanner.hpp
File metadata and controls
209 lines (169 loc) · 3.54 KB
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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#ifndef __SCANNER__
#define __SCANNER__
#include <string>
#include <unistd.h>
#include "Exceptions.hpp"
// table of identifiers and buffer of Scanner sizes
#define TID_SIZE 100
#define BUF_SIZE 80
// error messages
#define NO_SUCH_LEXEM "No such lexem"
#define NO_SUCH_STATE "No such state"
// template class for exceptions instantiation
template class ParameterError <char>;
// types of possible lexems
enum TypeLex
{
// main commands
LEX_CREATE,
LEX_SELECT,
LEX_INSERT,
LEX_UPDATE,
LEX_DELETE,
LEX_DROP,
// other commands and clauses
LEX_FROM,
LEX_INTO,
LEX_SET,
LEX_WHERE,
LEX_LIKE,
LEX_ALL,
// bool
LEX_IN,
LEX_NOT,
LEX_OR,
LEX_AND,
// delimiters
LEX_LPAREN,
LEX_RPAREN,
LEX_COMMA,
LEX_EQ,
LEX_NEQ,
LEX_GREATER,
LEX_LESS,
LEX_GE,
LEX_LE,
LEX_PLUS,
LEX_MINUS,
LEX_MULT,
LEX_DIV,
LEX_MOD,
LEX_FIN,
// words and other
LEX_TABLE,
LEX_TEXT,
LEX_LONG,
LEX_ID,
LEX_NUM,
LEX_STR,
// for fields and tables
LEX_TABLE_ID,
LEX_TEXT_FIELD,
LEX_LONG_FIELD,
LEX_NULL
};
/*
-------------------------
Identifiers class
-------------------------
*/
struct Ident
{
Ident(const std::string &name_src = "", bool declare_src = false, TypeLex type_src = LEX_ID,
long val_src = 0);
friend std::ostream &operator<<(std::ostream &out, const Ident &ident);
std::string name;
bool declare;
TypeLex type;
long val;
};
/*
------------------------
TableIdent class
------------------------
*/
class TableIdent
{
public:
// constructor and destructor
TableIdent(size_t max_size_src);
~TableIdent();
// other
Ident &operator[] (size_t k);
size_t put(const std::string &name);
size_t size() const;
void delete_info(const std::string &name);
friend std::ostream &operator<<(std::ostream &out, TableIdent &table);
private:
Ident *idents;
size_t max_size, top;
};
/*
-------------------
Lexem class
-------------------
*/
class Lex
{
public:
Lex(TypeLex type_src = LEX_NULL, long val_src = 0);
// getters
TypeLex get_type() const;
long get_val() const;
// output
friend std::ostream &operator<<(std::ostream &out, Lex lexem);
std::string to_string() const;
private:
TypeLex type;
long val;
};
template <>
class ParameterError <Lex> : public ExceptionBase
{
public:
ParameterError(Lex param_src, const std::string &msg_src);
void print(std::ostream &out) const;
std::string get_msg() const;
private:
Lex param;
std::string msg;
};
/*
---------------------
Scanner class
---------------------
*/
class Scanner
{
public:
Scanner(int fd_src = 0);
Lex get_lex();
// table of identifiers
static TableIdent TID;
private:
friend class Lex;
friend std::ostream &operator<<(std::ostream &out, Lex lexem);
// table of service words and their lexems
static std::string TW[];
static TypeLex words[];
// table of delimiters and their lexems
static std::string TD[];
static TypeLex dlms[];
// for output
static std::string names[];
static TypeLex types[];
static std::string text[];
enum State { HALT, NUM, ID, STR, REL, DELIM };
State cur_state;
int cur;
char buf[BUF_SIZE];
size_t buf_top;
bool start;
int fd;
// service methods
void clear();
void add();
size_t look(const std::string &buffer, std::string *list);
void gc();
};
#endif