forked from stevemonaco/Atlas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AtlasParser.cpp
362 lines (314 loc) · 8.36 KB
/
AtlasParser.cpp
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "stdafx.h"
#include <map>
#include <cstring>
#include <list>
#include <fstream>
#include <sstream>
#include <utility>
#include "AtlasTypes.h"
#include "AtlasParser.h"
#include "GenericVariable.h"
#include "AtlasLogger.h"
#include "AtlasCore.h"
using namespace std;
AtlasParser::AtlasParser(VariableMap* Map)
{
VarMap = Map;
// Initialize the function lookup map
for (unsigned int i = 0; i < CommandCount; i++)
CmdMap.insert(multimap<string, unsigned int>::value_type(CommandStrings[i], i));
for (unsigned int i = 0; i < TypeCount; i++)
TypeMap.insert(multimap<string, unsigned int>::value_type(TypeStrings[i], i));
CurrentLine = 0;
CurBlock.StartLine = -1;
}
AtlasParser::~AtlasParser()
{
}
bool AtlasParser::ParseFile(ifstream& infile)
{
list<string> text;
unsigned char utfheader[4];
string line;
// Detect UTF-8 header
if (infile.peek() == 0xEF)
{
infile.read((char*)utfheader, 3);
if (utfheader[0] != 0xEF || utfheader[1] != 0xBB || utfheader[2] != 0xBF)
infile.seekg(ios::beg); // Seek beginning, not a UTF-8 header
}
// Read the file
while (!infile.eof())
{
getline(infile, line);
text.push_back(line);
}
infile.close();
CurrentLine = 1;
// Parse the file and build the series of AtlasBlocks
for (ListStringIt it = text.begin(); it != text.end(); it++)
{
ParseLine(*it);
CurrentLine++;
}
if (!CurBlock.Commands.empty() || !CurBlock.TextLines.empty())
FlushBlock();
for (ListErrorIt i = Logger.Errors.begin(); i != Logger.Errors.end(); i++)
{
if (i->Severity == FATALERROR)
return false;
}
return true;
}
void AtlasParser::ParseLine(string& line)
{
size_t firstchar = line.find_first_not_of(" \t", 0);
if (firstchar == string::npos) // All whitespace
{
string s = "";
AddText(s);
return;
}
string editline = line.substr(firstchar, line.length() - firstchar);
switch (line[firstchar])
{
case '#': // Atlas command
if (CurBlock.TextLines.empty()) // No text, build more commands
{
ParseCommand(editline);
}
else // Clear, parse the command
{
FlushBlock();
ParseCommand(editline);
}
break;
case '/': // Possible comment
if (line.length() > firstchar + 1)
{
if (line[firstchar + 1] != '/') // Not a comment "//", but text
AddText(line);
// else; Comment
}
else // Single text character of '/'
AddText(line);
break;
default: // Text
AddText(line);
break;
}
}
inline void AtlasParser::FlushBlock()
{
Blocks.push_back(CurBlock);
CurBlock.TextLines.clear();
CurBlock.Commands.clear();
CurBlock.StartLine = -1;
}
inline void AtlasParser::AddText(string& text)
{
if (CurBlock.StartLine == -1)
CurBlock.StartLine = CurrentLine;
CurBlock.TextLines.push_back(text);
}
inline void AtlasParser::ParseCommand(string& line)
{
if (line[0] != '#')
printf("Bug, %s %d. Should start with a '#'\n'%s'", __FILE__, __LINE__, line.c_str());
size_t curpos = 1;
std::string CmdStr;
Parameter Param;
Command Command;
Param.Type = P_INVALID;
char ch;
while (curpos < line.length() && (ch = line[curpos]) != '(')
{
if (isalpha(ch) || isdigit(ch))
CmdStr += ch;
else Logger.ReportError(CurrentLine, "Invalid syntax: Nonalphabetical character in command");
curpos++;
}
curpos = line.find_first_not_of(" \t", curpos + 1); // Skip ')', get first non
// wspace character
// Parse parameters
unsigned int ParamNum = 1;
while (curpos < line.length() && (ch = line[curpos]) != ')')
{
if (ch == ',')
{
// Trim trailing whitespace
size_t Last;
for (Last = Param.Value.length() - 1; Last > 0; Last--)
if (Param.Value[Last] != ' ' && Param.Value[Last] != '\t')
break;
if (Last < Param.Value.length())
Param.Value.erase(Last + 1);
Param.Type = IdentifyType(Param.Value);
if (Param.Type == P_INVALID)
Logger.ReportError(CurrentLine, "Invalid argument for %s for parameter %d", CmdStr.c_str(), ParamNum);
Command.Parameters.push_back(Param);
Param.Type = P_INVALID;
Param.Value.clear();
ParamNum++;
curpos = line.find_first_not_of(" \t", curpos + 1);
}
else
{
Param.Value += ch;
curpos++;
}
}
// Trim trailing whitespace
size_t Last;
for (Last = Param.Value.length() - 1; Last > 0; Last--)
if (Param.Value[Last] != ' ' && Param.Value[Last] != '\t')
break;
if (Last < Param.Value.length())
Param.Value.erase(Last + 1);
Param.Type = IdentifyType(Param.Value);
if (Param.Type == P_INVALID)
Logger.ReportError(CurrentLine, "Invalid argument for %s for parameter %d", CmdStr.c_str(), ParamNum);
for (ListErrorIt i = Logger.Errors.begin(); i != Logger.Errors.end(); i++)
if (i->Severity = FATALERROR)
return;
Command.Parameters.push_back(Param);
AddCommand(CmdStr, Command);
}
inline unsigned int AtlasParser::IdentifyType(string& str)
{
if (str.empty())
return P_VOID;
size_t charpos = 0;
// Check for number (int/uint)
if (str[0] == '$')
{
charpos = str.find_first_of('-', 1);
if (charpos == 1 || charpos == string::npos)
{
charpos = str.find_first_not_of("-0123456789ABCDEF", 1);
if (charpos == string::npos)
return P_NUMBER;
}
}
else
{
charpos = str.find_first_of('-');
if (charpos == 0 || charpos == string::npos)
{
charpos = str.find_first_not_of("0123456789", 1);
if (charpos == string::npos)
return P_NUMBER;
}
}
// Check for double
charpos = str.find_first_not_of("0123456789.", 0);
if (charpos == string::npos)
return P_DOUBLE;
// Check for variable
charpos = str.find_first_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJLMNOPQRSTUVWXYZ_", 0);
if (charpos == 0)
{
charpos = str.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJLMNOPQRSTUVWXYZ0123456789_", 0);
if (charpos == string::npos)
return P_VARIABLE;
}
// Check for string
if ((str[0] == '"') && (str[str.length() - 1] == '"'))
{
str = str.substr(1, str.length() - 2);
return P_STRING;
}
return P_INVALID;
}
inline bool AtlasParser::AddCommand(string& CmdStr, Command& Cmd)
{
bool bFound = false;
unsigned int CmdNum = 0;
Cmd.Line = CurrentLine;
pair<StrCmdMapIt, StrCmdMapIt> val = CmdMap.equal_range(CmdStr);
if (val.first == val.second) // not found
{
Logger.ReportError(CurrentLine, "Invalid command %s", CmdStr.c_str());
return false;
}
// Found one or more matches
for (StrCmdMapIt i = val.first; i != val.second; i++)
{
CmdNum = i->second;
if (ParamCount[CmdNum] != Cmd.Parameters.size())
continue;
// Found a matching arg count function, check types
ListParamIt it = Cmd.Parameters.begin();
for (unsigned int j = 0; j < Cmd.Parameters.size(); j++, it++)
{
if ((it->Type == P_VARIABLE) && (CmdNum != CMD_VAR)) // Type-checking for vars
{
GenericVariable* var = VarMap->GetVar(it->Value);
if (var) // Found, check the type
{
if (var->GetType() != Types[CmdNum][j]) // Type mismatch
break;
else
it->Type = Types[CmdNum][j];
}
else // NULL
{
Logger.ReportError(CurrentLine, "Undefined variable %s", (it->Value).c_str());
return false;
}
}
if (it->Type != Types[CmdNum][j]) // Type checking for everything
break;
if (j == Cmd.Parameters.size() - 1) // Verified final parameter
bFound = true;
}
if (bFound)
break;
}
if (bFound) // Successful lookup
{
Cmd.Function = CmdNum;
if (CmdNum == CMD_VAR) // Variable declaration, handled here explicitly
{
return AddUnitializedVariable(Cmd.Parameters[0].Value, Cmd.Parameters[1].Value);
}
else
{
// Hack for preallocating just enough embedded pointers
if (CmdNum == CMD_EMBSET || CmdNum == CMD_EMBWRITE)
{
int ptrcount = StringToUInt(Cmd.Parameters[0].Value);
if (ptrcount > MaxEmbPtr)
MaxEmbPtr = ptrcount;
}
CurBlock.Commands.push_back(Cmd);
}
return true;
}
else
{
ostringstream ErrorStr;
ErrorStr << "Invalid parameters " << "(";
if (Cmd.Parameters.size() > 0)
ErrorStr << TypeStrings[Cmd.Parameters.front().Type];
for (unsigned int i = 1; i < Cmd.Parameters.size(); i++)
ErrorStr << "," << TypeStrings[Cmd.Parameters[i].Type];
ErrorStr << ") for " << CmdStr;
Logger.ReportError(CurrentLine, "%s", ErrorStr.str().c_str());
return false;
}
}
inline bool AtlasParser::AddUnitializedVariable(string& VarName, string& Type)
{
StrTypeMapIt it = TypeMap.find(Type);
if (it == TypeMap.end()) // not found
{
Logger.ReportError(CurrentLine, "Invalid VAR declaration of type %s", Type.c_str());
return false;
}
else // Add to the variable map
{
VarMap->AddVar(VarName, 0, it->second);
return true;
}
}