-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_token.cpp
More file actions
141 lines (127 loc) · 2.94 KB
/
Copy pathparser_token.cpp
File metadata and controls
141 lines (127 loc) · 2.94 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
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "parser_common.h"
#include "parser_token.h"
#define MAXCHAR 5000
static char charpool[MAXCHAR];
static int charptr = 0;
/*
* string_alloc: returns a pointer to a string of length len if possible
*/
static char *string_alloc(int len)
{
char *s;
if (charptr + len > MAXCHAR) {
fprintf(stderr, "out of memory\n");
exit(1);
}
s = charpool + charptr;
charptr += len;
return s;
}
/*
* reset_charptr: releases all memory allocated in preparation for the
* next query.
*
* No return value.
*/
void reset_charptr(void)
{
charptr = 0;
}
/*
* reset_scanner: resets the scanner after a syntax error
*
* No return value.
*/
void reset_scanner(void)
{
charptr = 0;
}
/*
* mk_string: allocates space for a string of length len and copies s into
* it.
*
* Returns:
* a pointer to the new string
*/
static char *mk_string(const char *s, int len)
{
char *copy;
/* allocate space for new string */
if ((copy = string_alloc(len + 1)) == NULL) {
printf("out of string space\n");
exit(1);
}
/* copy the string */
strncpy(copy, s, len + 1);
return copy;
}
string tokenName(TOKENKIND type)
{
switch (type)
{
case RW_CREATE: return "create";
case RW_DROP: return "drop";
case RW_TABLE: return "table";
case RW_INDEX: return "index";
case RW_LOAD: return "load";
case RW_HELP: return "help";
case RW_EXIT: return "exit";
case RW_PRINT: return "print";
case RW_SET: return "set";
case RW_AND: return "and";
case RW_INTO: return "into";
case RW_VALUES: return "values";
case RW_SELECT: return "select";
case RW_FROM: return "from";
case RW_WHERE: return "where";
case RW_ORDER: return "order";
case RW_GROUP: return "group";
case RW_BY: return "by";
case RW_DESC: return "desc";
case RW_ASC: return "asc";
case RW_INSERT: return "insert";
case RW_DELETE: return "delete";
case RW_UPDATE: return "update";
case RW_MAX: return "max";
case RW_MIN: return "min";
case RW_AVG: return "avg";
case RW_SUM: return "sum";
case RW_COUNT: return "count";
case RW_RESET: return "reset";
case RW_BUFFER: return "buffer";
case RW_ON: return "on";
case RW_OFF: return "off";
case T_EOF: return "eof";
case RW_COMMENT: return "comment";
case T_NE: return "not_equal";
case T_INT: return "int";
case T_FLOAT: return "float";
case T_STRING: return "string";
case RW_LPAREN: return "lparen";
case RW_RPAREN: return "rparen";
case RW_COMMA: return "comma";
case RW_SEMICOLON: return "semicolon";
}
return "";
}
//
// printToken ÓÃÓÚÊä³öTokenµÄÖµ
//
ostream& operator<<(ostream& os, const Token& tk)
{
os << tk.content << "[" << tokenName(tk.type) << "]" << "(" << tk.line << ")" << endl;
return os;
}
Token::Token(TOKENKIND type, size_t line, const string& str)
: type(type), line(line)
{
if (type == T_QSTRING) {
string buff = str.substr(1, str.size() - 2);
content = mk_string(buff.c_str(), buff.size());
}
else content = mk_string(str.c_str(), str.size());
}