My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#include "Core.h"

NAMESPACE_UPP

#define LLOG(x) // LOG(x)
#define LTIMING(x) // RTIMING(x)

void CParser::ThrowError(const char *s) {
LLOG("CParser::Error: " << s);
LLOG(~String(term, min(strlen((const char *)term), 512U)));
Pos pos = GetPos();
Error err(fn + Format("(%d,%d): ", line, pos.GetColumn()) + s);
// err.term = (const char *)term;
throw err;
}

bool CParser::Spaces0() {
LTIMING("Spaces");
if((byte)*term > ' ' &&
!(term[0] == '/' && term[1] == '/') &&
!(term[0] == '/' && term[1] == '*'))
return false;
for(;;) {
if(term[0] == '/' && term[1] == '/') {
term += 2;
while(*term && *term != '\n')
term++;
}
else
if(term[0] == '/' && term[1] == '*') {
term += 2;
while(*term) {
if(term[0] == '*' && term[1] == '/') {
term += 2;
break;
}
if(*term++ == '\n') line++;
}
}
if(!*term) break;
if((byte)*term > ' ') break;
if(*term == '\n') {
line++;
lineptr = term + 1;
}
term++;
}
return true;
}

const char *CParser::IsId0(const char *s) const {
const char *t = term + 1;
s++;
while(*s) {
if(*t != *s)
return NULL;
t++;
s++;
}
return IsAlNum(*t) || *t == '_' ? NULL : t;
}

bool CParser::Id0(const char *s) {
LTIMING("Id");
const char *t = IsId0(s);
if(!t)
return false;
term = t;
DoSpaces();
return true;
}

void CParser::PassId(const char *s) throw(Error) {
LTIMING("PassId");
if(!Id(s))
ThrowError(String("missing '") + s + "\'");
}

void CParser::PassChar(char c) throw(Error) {
LTIMING("PassChar");
if(!Char(c))
ThrowError(String("missing '") + c + "\'");
}

void CParser::PassChar2(char c1, char c2) throw(Error) {
LTIMING("PassChar2");
if(!Char2(c1, c2))
ThrowError(String("missing '") + c1 + c2 + "\'");
}

void CParser::PassChar3(char c1, char c2, char c3) throw(Error) {
LTIMING("PassChar3");
if(!Char3(c1, c2, c3))
ThrowError(String("missing '") + c1 + c2 + c3 + "\'");
}

String CParser::ReadId() throw(Error) {
LTIMING("ReadId");
if(!IsId())
ThrowError("missing id");
String result;
const char *b = term;
const char *p = b;
while(iscid(*p))
p++;
term = p;
DoSpaces();
return String(b, (int)(uintptr_t)(p - b));
}

String CParser::ReadIdt() throw(Error) {
if(!IsId())
ThrowError("missing id");
StringBuffer result;
int lvl = 0;
while(IsAlNum(*term) || *term == '_' || *term == '<' || *term == '>' ||
*term == ':' || (*term == ',' || *term == ' ' ) && lvl > 0 ) {
if(*term == '<') lvl++;
if(*term == '>') lvl--;
result.Cat(*term++);
}
DoSpaces();
return result;
}

bool CParser::IsInt() const {
LTIMING("IsInt");
const char *t = term;
if(*t == '-' || *t == '+') {
t++;
while(*t <= ' ')
t++;
}
return IsDigit(*t);
}

int CParser::ReadInt() throw(Error) {
LTIMING("ReadInt");
int sign = 1;
if(*term == '-') {
sign = -1;
term++;
}
else
if(*term == '+')
term++;
Spaces();
if(!IsDigit(*term))
ThrowError("missing number");
int n = 0;
while(IsDigit(*term))
n = 10 * n + *term++ - '0';
DoSpaces();
return sign * n;
}

int CParser::ReadInt(int min, int max) throw(Error)
{
int n = ReadInt();
if(n < min || n > max)
ThrowError("number is out of range");
return n;
}

bool CParser::IsNumber(int base) const
{
if(IsDigit(*term))
return true;
int q = ToUpper(*term) - 'A';
return q >= 0 && q < base - 10;
}

uint32 CParser::ReadNumber(int base) throw(Error)
{
LTIMING("ReadNumber");
uint32 n = 0;
int q = ctoi(*term);
if(q < 0 || q >= base)
ThrowError("missing number");
for(;;) {
int c = ctoi(*term);
if(c < 0 || c >= base)
break;
n = base * n + c;
term++;
}
DoSpaces();
return n;
}

uint64 CParser::ReadNumber64(int base) throw(Error)
{
LTIMING("ReadNumber");
uint64 n = 0;
int q = ctoi(*term);
if(q < 0 || q >= base)
ThrowError("missing number");
for(;;) {
int c = ctoi(*term);
if(c < 0 || c >= base)
break;
n = base * n + c;
term++;
}
DoSpaces();
return n;
}

double CParser::ReadDouble() throw(Error)
{
LTIMING("ReadDouble");
int sign = 1;
if(*term == '-') {
sign = -1;
term++;
}
else
if(*term == '+')
term++;
Spaces();
if(!IsDigit(*term))
ThrowError("missing number");
double n = 0;
while(IsDigit(*term))
n = 10 * n + *term++ - '0';
if(Char('.')) {
double q = 1;
while(IsDigit(*term)) {
q = q / 10;
n += q * (*term++ - '0');
}
}
if(Char('e') || Char('E'))
n *= pow(10.0, ReadInt());
DoSpaces();
return sign * n;
}

String CParser::ReadOneString(int delim, bool chkend) throw(Error) {
if(!IsChar(delim))
ThrowError("missing string");
term++;
StringBuffer result;
for(;;) {
if(*term == delim) {
term++;
DoSpaces();
return result;
}
else
if(*term == '\\') {
switch(*++term) {
case 'a': result.Cat('\a'); term++; break;
case 'b': result.Cat('\b'); term++; break;
case 't': result.Cat('\t'); term++; break;
case 'v': result.Cat('\v'); term++; break;
case 'n': result.Cat('\n'); term++; break;
case 'r': result.Cat('\r'); term++; break;
case 'f': result.Cat('\f'); term++; break;
case 'x': {
int hex = 0;
if(IsXDigit(*++term)) {
hex = ctoi(*term);
if(IsXDigit(*++term)) {
hex = 16 * hex + (*term >= 'A' ? ToUpper(*term) - 'A' + 10 : *term - '0');
term++;
}
}
result.Cat(hex);
break;
}
case 'u':
if(uescape) {
int hex = 0;
if(IsXDigit(*++term)) {
hex = ctoi(*term);
if(IsXDigit(*++term)) {
hex = 16 * hex + (*term >= 'A' ? ToUpper(*term) - 'A' + 10 : *term - '0');
if(IsXDigit(*++term)) {
hex = 16 * hex + (*term >= 'A' ? ToUpper(*term) - 'A' + 10 : *term - '0');
if(IsXDigit(*++term)) {
hex = 16 * hex + (*term >= 'A' ? ToUpper(*term) - 'A' + 10 : *term - '0');
term++;
}
}
}
}
result.Cat(WString(hex, 1).ToString());
}
else
result.Cat(*term++);
break;
default:
if(*term >= '0' && *term <= '7') {
int oct = *term++ - '0';
if(*term >= '0' && *term <= '7')
oct = 8 * oct + *term++ - '0';
if(*term >= '0' && *term <= '7')
oct = 8 * oct + *term++ - '0';
result.Cat(oct);
}
else
result.Cat(*term++);
break;
}
}
else {
if(*term < ' ') {
if(chkend) {
ThrowError("Unterminated string");
return result;
}
if(*term == '\0')
return result;
}
result.Cat(*term++);
}
}
DoSpaces();
return result;
}

String CParser::ReadOneString(bool chkend) throw(Error)
{
return ReadOneString('\"', chkend);
}

String CParser::ReadString(int delim, bool chkend) throw(Error) {
LTIMING("ReadString");
String result;
do
result.Cat(ReadOneString(delim, chkend));
while(IsChar(delim));
return result;
}

String CParser::ReadString(bool chkend) throw(Error)
{
return ReadString('\"', chkend);
}

char CParser::GetChar()
{
char c = *term++;
if(c == '\n')
line++;
return c;
}

void CParser::SkipTerm()
{
LTIMING("SkipTerm");
if(IsId())
while(IsAlNum(*term) || *term == '_')
term++;
else
if(IsNumber())
while(IsDigit(*term))
term++;
else
if(IsString())
ReadString();
else
if(IsChar('\''))
ReadString('\'', false);
else
if(*term) {
if(*term == '\n')
line++;
term++;
}
DoSpaces();
}

CParser::Pos CParser::GetPos() const
{
Pos p;
p.line = line;
p.fn = fn;
p.ptr = term;
p.wspc = wspc;
p.lineptr = lineptr;
return p;
}

int CParser::Pos::GetColumn(int tabsize) const
{
int pos = 1;
for(const char *s = lineptr; s < ptr; s++) {
if(*s == '\t')
pos = (pos + tabsize - 1) / tabsize * tabsize + 1;
else
pos++;
}
return pos;
}

void CParser::SetPos(const CParser::Pos& p)
{
line = p.line;
fn = p.fn;
term = p.ptr;
wspc = p.wspc;
lineptr = p.lineptr;
DoSpaces();
}

CParser::CParser(const char *ptr)
: term(ptr), wspc(ptr), lineptr(ptr)
{
line = 1;
skipspaces = true;
uescape = false;
Spaces();
}

CParser::CParser(const char *ptr, const char *fn, int line)
: term(ptr), wspc(ptr), lineptr(ptr), line(line), fn(fn)
{
skipspaces = true;
uescape = false;
Spaces();
}

CParser::CParser()
{
term = lineptr = wspc = NULL;
line = 0;
skipspaces = true;
uescape = false;
}

void CParser::Set(const char *_ptr, const char *_fn, int _line)
{
term = lineptr = wspc = _ptr;
fn = _fn;
line = _line;
skipspaces = true;
Spaces();
}

void CParser::Set(const char *_ptr)
{
Set(_ptr, "", 1);
}


inline void NextCStringLine(String& t, const char *linepfx, int& pl)
{
t << "\"\r\n" << (linepfx ? linepfx : "") << "\"";
pl = t.GetLength();
}

String AsCString(const char *s, const char *lim, int linemax, const char *linepfx, dword flags)
{
String t;
t.Cat('\"');
int pl = 0;
bool wasspace = false;
while(s < lim) {
if(t.GetLength() - pl > linemax && (!(flags & ASCSTRING_SMART) || wasspace))
NextCStringLine(t, linepfx, pl);
wasspace = false;
switch(*s) {
case '\a': t.Cat("\\a"); break;
case '\b': t.Cat("\\b"); break;
case '\f': t.Cat("\\f"); break;
case '\t': t.Cat("\\t"); break;
case '\v': t.Cat("\\v"); break;
case '\r': t.Cat("\\r"); break;
case '\"': t.Cat("\\\""); break;
case '\\': t.Cat("\\\\"); break;
case '\n': t.Cat("\\n"); wasspace = true; break;
default:
if(byte(*s) < 32 || (byte)*s >= 0x7f && (flags & ASCSTRING_OCTALHI) || (byte)*s == 0xff) {
char h[4];
int q = (byte)*s;
h[0] = '\\';
h[1] = (3 & (q >> 6)) + '0';
h[2] = (7 & (q >> 3)) + '0';
h[3] = (7 & q) + '0';
t.Cat(h, 4);
}
else {
t.Cat(*s);
if(*s == ' ')
wasspace = true;
}
break;
}
s++;
}
t.Cat('\"');
return t;
}

String AsCString(const char *s, int linemax, const char *linepfx, dword flags)
{
return AsCString(s, s + strlen(s), linemax, linepfx, flags);
}

String AsCString(const String& s, int linemax, const char *linepfx, dword flags)
{
return AsCString(s, s.End(), linemax, linepfx, flags);
}

END_UPP_NAMESPACE

Change log

r4235 by cxl on Dec 3, 2011   Diff
Core: CParser: GetSpacePtr
Go to: 
Project members, sign in to write a code review

Older revisions

r4160 by cxl on Nov 11, 2011   Diff
Core: CParser::UnicodeEscape - option
to escape javascript's \u, used in
JSON
r4124 by cxl on Nov 4, 2011   Diff
Core: CParser improved const-
correctness (many methods are now
const)
r4114 by cxl on Nov 1, 2011   Diff
Core: CParser::IsId(const char *),
improved 'visuals' for
ValueArray::ToString
All revisions of this file

File info

Size: 9851 bytes, 507 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting