-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathToken.c
170 lines (149 loc) · 4.91 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
#include "Token.h"
#include "Error.h"
#include "Util.h"
#include <assert.h>
#include <string.h>
// We create a LUT that keeps track of the last token on any given lineNumber
// in any given sourceFile. This way we can look these up for error messages
// without having to store them for every individual token.
static void StoreLocation(TokenArray* t, size_t tokenIndex, uint16_t lineNumber, char* sourceFile)
{
if (t->locationsCount == 0 || (t->locations[(t->locationsCount - 1)]).location.lineNumber != lineNumber ||
strcmp(t->locations[t->locationsCount - 1].location.sourceFile, sourceFile) != 0)
{
t->locationsCount++;
if (t->locationsCount > t->maxLocationsCount)
{
t->maxLocationsCount += 128;
t->locations = xrealloc(t->locations, (t->maxLocationsCount) * sizeof(TokenSourceGroup));
}
char* newString = NULL;
// No need to create another copy of the file name string in memory if it is still the same
if (t->locationsCount >= 2 && strcmp(t->locations[t->locationsCount - 2].location.sourceFile, sourceFile) == 0)
{
newString = t->locations[t->locationsCount - 2].location.sourceFile;
}
else
{
newString = xmalloc(strlen(sourceFile) + 1);
strcpy(newString, sourceFile);
}
t->locations[t->locationsCount - 1].location.sourceFile = newString;
t->locations[t->locationsCount - 1].location.lineNumber = lineNumber;
t->locations[t->locationsCount - 1].highestTokenIndex = tokenIndex;
}
else
{
t->locations[t->locationsCount - 1].highestTokenIndex = tokenIndex;
}
}
static TokenArray* currentTokenArray = NULL;
SourceLocation Token_GetLocationP(const Token* token)
{
size_t offset = (void*)token - (void*)currentTokenArray->tokens;
offset /= sizeof(Token);
assert(offset < currentTokenArray->curLength);
return Token_GetLocation(offset);
}
SourceLocation Token_GetLocation(size_t tokenIndex)
{
for (size_t i = 0; i < currentTokenArray->locationsCount; i++)
{
if (currentTokenArray->locations[i].highestTokenIndex >= tokenIndex)
return currentTokenArray->locations[i].location;
}
assert(0);
}
Token Token_Get(TokenType type)
{
Token t = {type, NULL};
return t;
}
Token Token_GetString(TokenType type, char* string)
{
Token t = {type, string};
return t;
}
Token Token_GetInt(TokenType type, int32_t* integer)
{
Token t = {type, integer};
return t;
}
TokenArray* Token_CreateArray(size_t size)
{
TokenArray* arr = xmalloc(sizeof(TokenArray));
arr->tokens = xmalloc(sizeof(Token) * size);
arr->maxLength = size;
arr->curLength = 0;
arr->locationsCount = 0;
arr->maxLocationsCount = 32;
arr->locations = xmalloc(sizeof(TokenSourceGroup) * 32);
currentTokenArray = arr;
return arr;
}
void Token_DeleteArray(TokenArray* array)
{
// Freeing the strings is a little more involved, as they are
// reused for many locations
for (size_t i = 0; i < array->locationsCount; i++)
{
void* cur = array->locations[i].location.sourceFile;
void* next;
if (i + 1 >= array->locationsCount)
next = NULL;
else
next = array->locations[i + 1].location.sourceFile;
if (cur != next)
free(cur);
}
for (size_t i = 0; i < array->curLength; i++)
{
void* data = array->tokens[i].data;
if (data)
free(data);
}
free(array->tokens);
free(array->locations);
free(array);
}
void Token_AppendArray(Token t, TokenArray* array, uint16_t lineNumber, char* sourceFile)
{
if (array->curLength + 1 > array->maxLength)
{
array->tokens = xrealloc(array->tokens, (array->maxLength *= 2) * sizeof(Token));
}
StoreLocation(array, array->curLength, lineNumber, sourceFile);
array->tokens[array->curLength++] = t;
}
// Some helper methods for easier parsing
void* PopNext(size_t* i, TokenType type)
{
if (++(*i) >= currentTokenArray->curLength)
SyntaxErrorAtIndex(*i - 1);
if (currentTokenArray->tokens[*i].type != type)
SyntaxErrorAtIndex(*i);
return currentTokenArray->tokens[*i].data;
}
void* PopNextInc(size_t* i, TokenType type)
{
if (++(*i) >= currentTokenArray->curLength)
SyntaxErrorAtIndex(*i - 1);
if (currentTokenArray->tokens[*i].type != type)
SyntaxErrorAtIndex(*i);
if (++(*i) >= currentTokenArray->curLength)
SyntaxErrorAtIndex(*i - 1);
return currentTokenArray->tokens[(*i) - 1].data;
}
void* PopCur(size_t* i, TokenType type)
{
if (currentTokenArray->tokens[*i].type != type)
SyntaxErrorAtIndex(*i);
if (++(*i) >= currentTokenArray->curLength)
SyntaxErrorAtIndex(*i - 1);
return currentTokenArray->tokens[(*i) - 1].data;
}
void Inc(size_t* i)
{
if (++(*i) >= currentTokenArray->curLength)
SyntaxErrorAtIndex(*i);
}