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
|
/* Starshatter: The Open Source Project
Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
Copyright (c) 1997-2006, Destroyer Studios LLC.
AUTHOR: John DiCamillo
OVERVIEW
========
Implementation of the generic Parser class
*/
#include "MemDebug.h"
#include "Reader.h"
#include "Token.h"
#include "Parser.h"
#include "Term.h"
#include "Utils.h"
#include <stdio.h>
#include <stdlib.h>
enum KEYS { KEY_TRUE, KEY_FALSE, KEY_DEF, KEY_MINUS };
static int dump_tokens = 0;
// +-------------------------------------------------------------------+
Term* error(char* msg, const Token& token)
{
static char buf[1024];
sprintf_s(buf, " near '%s' in line %d.", (const char*) token.symbol(), token.line());
return error(msg, buf);
}
// +-------------------------------------------------------------------+
Parser::Parser(Reader* r)
{
reader = r ? r : new(__FILE__, __LINE__) ConsoleReader;
lexer = new(__FILE__, __LINE__) Scanner(reader);
Token::addKey("true", KEY_TRUE);
Token::addKey("false", KEY_FALSE);
Token::addKey(":", KEY_DEF);
Token::addKey("-", KEY_MINUS);
}
Parser::~Parser()
{
delete lexer;
delete reader;
//Token::close();
}
Term*
Parser::ParseTerm()
{
Term* t = ParseTermBase();
if (t == 0) return t;
Term* t2 = ParseTermRest(t);
return t2;
}
Term*
Parser::ParseTermRest(Term* base)
{
Token t = lexer->Get();
switch (t.type()) {
default:
lexer->PutBack();
return base;
case Token::StringLiteral: {
// concatenate adjacent string literal tokens:
TermText* text = base->isText();
if (text) {
TermText* base2 = new(__FILE__, __LINE__) TermText(text->value() + t.symbol()(1, t.symbol().length()-2));
delete base;
return ParseTermRest(base2);
}
else {
lexer->PutBack();
}
}
break;
case Token::Keyword:
switch (t.key()) {
case KEY_DEF:
if (base->isText())
return new(__FILE__, __LINE__) TermDef(base->isText(), ParseTerm());
else
return error("(Parse) illegal lhs in def", t);
default:
lexer->PutBack();
return base;
}
break;
}
return base;
}
static int xtol(const char* p)
{
int n = 0;
while (*p) {
char digit = *p++;
n *= 16;
if (digit >= '0' && digit <= '9')
n += digit - '0';
else if (digit >= 'a' && digit <= 'f')
n += digit - 'a' + 10;
else if (digit >= 'A' && digit <= 'F')
n += digit - 'A' + 10;
}
return n;
}
Term*
Parser::ParseTermBase()
{
Token t = lexer->Get();
int n = 0;
double d = 0.0;
switch (t.type()) {
case Token::IntLiteral: {
if (dump_tokens)
Print("%s", t.symbol().data());
char nstr[256], *p = nstr;
for (int i = 0; i < (int) t.symbol().length(); i++)
if (t.symbol()[i] != '_')
*p++ = t.symbol()[i];
*p++ = '\0';
// handle hex notation:
if (nstr[1] == 'x')
n = xtol(nstr+2);
else
n = atol(nstr);
return new(__FILE__, __LINE__) TermNumber(n);
}
case Token::FloatLiteral: {
if (dump_tokens)
Print("%s", t.symbol().data());
char nstr[256], *p = nstr;
for (int i = 0; i < (int) t.symbol().length(); i++)
if (t.symbol()[i] != '_')
*p++ = t.symbol()[i];
*p++ = '\0';
d = atof(nstr);
return new(__FILE__, __LINE__) TermNumber(d);
}
case Token::StringLiteral:
if (dump_tokens)
Print("%s", t.symbol().data());
return new(__FILE__, __LINE__) TermText(t.symbol()(1, t.symbol().length()-2));
case Token::AlphaIdent:
if (dump_tokens)
Print("%s", t.symbol().data());
return new(__FILE__, __LINE__) TermText(t.symbol());
case Token::Keyword:
if (dump_tokens)
Print("%s", t.symbol().data());
switch (t.key()) {
case KEY_FALSE: return new(__FILE__, __LINE__) TermBool(0);
case KEY_TRUE: return new(__FILE__, __LINE__) TermBool(1);
case KEY_MINUS: {
Token next = lexer->Get();
if (next.type() == Token::IntLiteral) {
if (dump_tokens)
Print("%s", next.symbol().data());
char nstr[256], *p = nstr;
for (int i = 0; i < (int) next.symbol().length(); i++)
if (next.symbol()[i] != '_')
*p++ = next.symbol()[i];
*p++ = '\0';
n = -1 * atol(nstr);
return new(__FILE__, __LINE__) TermNumber(n);
}
else if (next.type() == Token::FloatLiteral) {
if (dump_tokens)
Print("%s", next.symbol().data());
char nstr[256], *p = nstr;
for (int i = 0; i < (int) next.symbol().length(); i++)
if (next.symbol()[i] != '_')
*p++ = next.symbol()[i];
*p++ = '\0';
d = -1.0 * atof(nstr);
return new(__FILE__, __LINE__) TermNumber(d);
}
else {
lexer->PutBack();
return error("(Parse) illegal token '-': number expected", next);
}
}
break;
default:
lexer->PutBack();
return 0;
}
case Token::LParen: return ParseArray();
case Token::LBrace: return ParseStruct();
case Token::CharLiteral:
return error("(Parse) illegal token ", t);
default:
lexer->PutBack();
return 0;
}
}
TermArray*
Parser::ParseArray()
{
TermList* elems = ParseTermList(0);
Token end = lexer->Get();
if (end.type() != Token::RParen)
return (TermArray*) error("(Parse) ')' missing in array-decl", end);
return new(__FILE__, __LINE__) TermArray(elems);
}
TermStruct*
Parser::ParseStruct()
{
TermList* elems = ParseTermList(1);
Token end = lexer->Get();
if (end.type() != Token::RBrace)
return (TermStruct*) error("(Parse) '}' missing in struct", end);
return new(__FILE__, __LINE__) TermStruct(elems);
}
TermList*
Parser::ParseTermList(int for_struct)
{
TermList* tlist = new(__FILE__, __LINE__) TermList;
Term* term = ParseTerm();
while (term) {
if (for_struct && !term->isDef()) {
return (TermList*) error("(Parse) non-definition term in struct");
}
else if (!for_struct && term->isDef()) {
return (TermList*) error("(Parse) illegal definition in array");
}
tlist->append(term);
Token t = lexer->Get();
/*** OLD WAY: COMMA SEPARATORS REQUIRED ***
if (t.type() != Token::Comma) {
lexer->PutBack();
term = 0;
}
else
term = ParseTerm();
/*******************************************/
// NEW WAY: COMMA SEPARATORS OPTIONAL:
if (t.type() != Token::Comma) {
lexer->PutBack();
}
term = ParseTerm();
}
return tlist;
}
|