This repository has been archived by the owner on Dec 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
token.c
251 lines (226 loc) · 4.83 KB
/
token.c
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* token.c - Source code for tokens.
*
* Copyright (C) 2014 Andrew Schwartzmeyer
*
* This file released under the AGPLv3 license.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <libgen.h>
#include "token.h"
#include "logger.h"
#include "parser.tab.h"
static const int TEXT_CHUNK_SIZE = 128;
static size_t token_sval_size;
static char *print_category(int t);
static void token_realloc_sval(struct token *t);
static void token_realloc_text(struct token *t, const char *s);
/* malloc token and assign values */
struct token *token_new(int category, int lineno,
const char *text, const char* filename)
{
struct token *t = malloc(sizeof(*t));
if (t == NULL)
log_error("token_new(): could not malloc token");
t->category = category;
t->lineno = lineno;
t->text = strdup(text);
t->filename = strdup(filename);
switch(category) {
case INTEGER:
t->ival = atoi(text);
break;
case FALSE:
t->ival = 0;
break;
case TRUE:
t->ival = 1;
break;
case FLOATING:
t->fval = atof(text);
break;
case STRING:
t->ssize = 0; /* append null later */
token_sval_size = TEXT_CHUNK_SIZE;
t->sval = calloc(token_sval_size, sizeof(char));
break;
default:
break;
}
return t;
}
/* free internal values */
void token_free(struct token *t)
{
log_assert(t);
free(t->text);
free(t->filename);
if (t->category == STRING)
free(t->sval);
free(t);
}
/* print a token */
void token_print(struct token *t)
{
char *filename = strdup(t->filename);
log_assert(filename);
printf("%-5d%-12s%-12s%s ",
t->lineno,
basename(filename),
print_category(t->category),
t->text);
free(filename);
if (t->category == INTEGER)
printf("-> %d", t->ival);
else if (t->category == FLOATING)
printf("-> %f", t->fval);
else if (t->category == CHARACTER)
printf("-> %c", t->ival);
else if (t->category == STRING)
printf("-> %s", t->sval);
printf("\n");
}
/*
* append a single char to sval (used for processing escape codes)
*/
void token_push_sval_char(struct token *t, char c)
{
++t->ssize;
token_realloc_sval(t);
t->sval[t->ssize-1] = c; /* 0 indexed */
}
/*
* append a string (without escape codes) to sval
*
* memcpy is used over strcat because the current value of sval is not
* guaranteed to be a null-terminated string, and may very well have
* embedded null characters
*/
void token_push_sval_string(struct token *t, const char *s)
{
size_t end = t->ssize;
t->ssize += strlen(s);
token_realloc_sval(t);
memcpy(t->sval + end, s, strlen(s));
}
/*
* called at end of string pattern
*
* Resets token_sval_size to 0. Appends terminating null
* character. Shrinks string with realloc to recorded length of
* string. 'strlen' cannot be used because of potentially embedded
* null characters.
*/
void token_finish_sval(struct token *t)
{
token_sval_size = 0;
token_push_sval_char(t, '\0');
t->sval = realloc(t->sval, t->ssize);
log_assert(t->sval);
}
/*
* append string to text field (these are actual null terminated
* strings, so the issues with sval do not apply)
*/
void token_push_text(struct token *t, const char* s)
{
token_realloc_text(t, s);
strcat(t->text, s);
}
/*
* reallocate t->sval with additional chunks of memory
*
* "If the new size you specify is the same as the old size, realloc
* is guaranteed to change nothing and return the same address that
* you gave." - section 3.2.2.4 of libc manual
*
* Therefore this function is idempotent if another chunk of memory is
* not required; otherwise, it increments it exactly as needed.
*/
static void token_realloc_sval(struct token *t)
{
while (t->ssize > token_sval_size)
token_sval_size += TEXT_CHUNK_SIZE;
t->sval = realloc(t->sval, token_sval_size);
log_assert(t->sval);
}
/*
* reallocate text field to append string and terminating null
*/
static void token_realloc_text(struct token *t, const char *s)
{
size_t size = strlen(t->text)+strlen(s)+1;
t->text = realloc(t->text, size);
log_assert(t->text);
}
#define R(rule) case rule: return #rule
static char *print_category(int t)
{
switch(t) {
R(IDENTIFIER);
R(INTEGER);
R(FLOATING);
R(CHARACTER);
R(STRING);
R(CLASS_NAME);
R(COLONCOLON);
R(DOTSTAR);
R(ADDEQ);
R(SUBEQ);
R(MULEQ);
R(DIVEQ);
R(MODEQ);
R(XOREQ);
R(ANDEQ);
R(OREQ);
R(SL);
R(SR);
R(SREQ);
R(SLEQ);
R(EQ);
R(NOTEQ);
R(LTEQ);
R(GTEQ);
R(ANDAND);
R(OROR);
R(PLUSPLUS);
R(MINUSMINUS);
R(ARROWSTAR);
R(ARROW);
R(BOOL);
R(BREAK);
R(CASE);
R(CHAR);
R(CLASS);
R(CONTINUE);
R(DEFAULT);
R(DELETE);
R(DO);
R(DOUBLE);
R(ELSE);
R(FALSE);
R(FLOAT);
R(FOR);
R(IF);
R(INT);
R(LONG);
R(NEW);
R(PRIVATE);
R(PROTECTED);
R(PUBLIC);
R(RETURN);
R(SHORT);
R(SIGNED);
R(SIZEOF);
R(STRUCT);
R(SWITCH);
R(TRUE);
R(UNSIGNED);
R(VOID);
R(WHILE);
}
return NULL; /* error */
}
#undef R