File: Tokenizer.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (457 lines) | stat: -rw-r--r-- 8,806 bytes parent folder | download | duplicates (2)
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
#include "stdafx.h"
#include "Tokenizer.h"
#include "Exception.h"
#include "Utils/TextReader.h"
#include "Utils/FileStream.h"


/**
 * Utils.
 */

static const wchar_t *operators = L"+?*&=.:!";
static const wchar_t *specials = L"[](){},-;<>~";

static bool isOperator(wchar_t c) {
	for (const wchar_t *p = operators; *p; p++)
		if (c == *p)
			return true;
	return false;
}

static bool isSpecial(wchar_t c) {
	for (const wchar_t *p = specials; *p; p++)
		if (c == *p)
			return true;
	return false;
}

static bool isWhitespace(wchar_t c) {
	switch (c) {
	case ' ':
	case '\n':
	case '\r':
	case '\t':
		return true;
	}
	return false;
}

static void advance(SrcPos &pos, const String &src, nat from, nat to) {
	// for (nat i = from; i < to; i++) {
	// 	if (src[i] == '\n') {
	// 		pos.line++;
	// 		pos.col = 0;
	// 	} else {
	// 		pos.col++;
	// 	}
	// }
	for (nat i = from; i < to; i++)
		if (src[i] != '\r')
			pos.pos++;
}


/**
 * Token.
 */

wostream &operator <<(wostream &to, const Token &token) {
	return to << token.token;
}

bool Token::isStr() const {
	return token.size() >= 2
		&& token[0] == '"'
		&& token[token.size()-1] == '"';
}

String Token::strVal() const {
	assert(isStr());
	return token.substr(1, token.size() - 2);
}


/**
 * Comment.
 */

wostream &operator <<(wostream &to, const Comment &comment) {
	return to << comment.str();
}

String Comment::str() const {
	std::wostringstream r;
	State state = start;
	Params par = { 0, 0, 0 };

	for (nat i = begin; i < end; i++) {
		if (const wchar_t *msg = parse(r, state, par, (*src)[i])) {
			SrcPos pos(fileId, 0);
			advance(pos, *src, 0, i);
			throw Error(L"Malformed comment: " + ::toS(msg), pos);
		}
	}

	return r.str();
}

const wchar_t *Comment::parse(std::wostringstream &r, State &state, Params &par, wchar_t ch) {
	// Just ignore any CR. We only want LF in the final output.
	if (ch == '\r')
		return null;

	switch (state) {
	case start:
		// Always a '/' that should be ignored.
		if (ch != '/')
			return L"Inconsistent comment. Internal error?";

		state = start2;
		return null;
	case start2:
		// Either '*' or '/'.
		if (ch == '*')
			state = multiStart;
		else if (ch == '/')
			state = singleStart;
		else
			return L"Unknown comment type. Internal error?";
		return null;
	case done:
		// Nothing more to do, just ignore the rest. (TODO: Assert on additional data?)
		return null;

		/**
		 * Single-line comments.
		 */

	case singleStart:
		// Find out the number of spaces before the comment starts.
		if (isWhitespace(ch)) {
			par.space++;
		} else {
			r << ch;
			state = singleInside;
		}
		return null;

	case singleInside:
		// Inside of a singleline comment.
		if (ch == '\n') {
			par.curr = 0;
			par.empty = 1;
			state = singleNewline;
		} else {
			r << ch;
		}
		return null;

	case singleNewline:
		// After a newline, before the first asterisk.
		if (ch == '/')
			state = singleHalf;
		else if (!isWhitespace(ch))
			return L"Expected // before the comment text.";
		return null;

	case singleHalf:
		// After the first '/'.
		if (ch == '/')
			state = singleBefore;
		else
			return L"Expected // before the comment text.";
		return null;

	case singleBefore:
		// Right after the second /. Count number of spaces and emit any text we find.
		if (ch == ' ') {
			par.curr++;
		} else if (ch == '\n') {
			par.empty++;
			par.curr = 0;
			state = singleNewline;
		} else if (!isWhitespace(ch)) {
			while (par.empty) {
				r << '\n';
				par.empty--;
			}

			if (par.curr < par.space)
				return L"Not enough indentation after //";
			for (nat i = par.space; i < par.curr; i++)
				r << ' ';
			par.curr = 0;

			r << ch;
			state = singleInside;
		}
		return null;


		/**
		 * Multi-line comments.
		 */

	case multiStart:
		// Skip remaining asterisks and compute the number of spaces to ignore after each asterisk.
		if (ch == '*') {
			par.space = 0;
		} else if (isWhitespace(ch)) {
			par.space++;
		} else {
			r << ch;
			state = multiInside;
		}
		return null;

	case multiInside:
		// Inside of a multiline comment.
		if (ch == '\n') {
			par.curr = 0;
			par.empty = 1;
			state = multiNewline;
		} else {
			r << ch;
		}
		return null;

	case multiNewline:
		// After a newline, before the first asterisk.
		if (ch == '*')
			state = multiBefore;
		else if (!isWhitespace(ch))
			return L"Expected at least one asterisk before the comment text.";
		return null;

	case multiBefore:
		// Right after an asterisk. Count number of spaces and emit any text we find.
		if (par.curr == 0 && ch == '/') {
			state = done;
		} else if (ch == ' ') {
			par.curr++;
		} else if (ch == '\n') {
			par.empty++;
			par.curr = 0;
			state = multiNewline;
		} else if (ch == '*') {
			// The comment pads on LHS with asterisks, we allow that since that is what is done in SQLite.
			state = multiBefore;
		} else if (!isWhitespace(ch)) {
			while (par.empty) {
				r << '\n';
				par.empty--;
			}

			if (par.curr < par.space)
				return L"Not enough indentation after the first asterisk.";
			for (nat i = par.space; i < par.curr; i++)
				r << ' ';
			par.curr = 0;

			r << ch;
			state = multiInside;
		}
		return null;
	}

	return L"Internal error: This state is not recognized.";
}


/**
 * Tokenizer.
 */

Tokenizer::Tokenizer(nat fileId) :
	src(readTextFile(SrcPos::files[fileId])),
	pathId(fileId), pos(0), srcPos(fileId, 0),
	commentBegin(0), commentEnd(0),
	nextToken(L"", SrcPos()) {

	nextToken = findNext();
}

Token Tokenizer::next() {
	Token t = peek();
	nextToken = findNext();
	return t;
}

void Tokenizer::skip() {
	nextToken = findNext();
}

Token Tokenizer::peek() {
	if (!more())
		throw Error(L"End of file!", srcPos);

	return nextToken;
}

void Tokenizer::expect(const String &t) {
	Token tok = next();
	if (tok.token != t)
		throw Error(L"Expected " + t + L" but got " + tok.token + L".", tok.pos);
}

bool Tokenizer::skipIf(const String &t) {
	Token tok = peek();
	if (tok.token == t) {
		skip();
		return true;
	} else {
		return false;
	}
}

Comment Tokenizer::comment() const {
	return Comment(src, srcPos.fileId, commentBegin, commentEnd);
}

void Tokenizer::clearComment() {
	commentBegin = commentEnd = 0;
}

bool Tokenizer::more() const {
	return !nextToken.empty();
}

void Tokenizer::advance(SrcPos &pos, nat from, nat to) const {
	::advance(pos, src, from, to);
}

Token Tokenizer::findNext() {
	State state = sStart;
	nat start = pos;
	nat from = pos; // from may be modified, while start may not.
	bool firstComment = true;
	SrcPos sStart = srcPos;

	while (state != sDone) {
		processChar(from, state, firstComment);
	}

	// Advance line-counting.
	advance(sStart, start, from);
	advance(srcPos, start, pos);

	return Token(src.substr(from, pos - from), sStart);
}

void Tokenizer::processChar(nat &start, State &state, bool &firstComment) {
	if (pos >= src.size()) {
		state = sDone;
		return;
	}

	wchar_t ch = src[pos];

	if (ch == '/' && pos+1 < src.size() && (src[pos+1] == '/' || src[pos+1] == '*')) {
		switch (state) {
		case sStart:
			if (src[pos+1] == '*') {
				state = sMlComment;
				commentBegin = pos;
			} else {
				state = sComment;
				if (firstComment) {
					commentBegin = pos;
					firstComment = false;
				}
			}
			break;
		case sString:
		case sComment:
		case sMlComment:
		case sPreproc:
			break;
		default:
			state = sDone;
			return;
		}
	}

	switch (state) {
	case sStart:
		pos++;
		if (ch == '\n') {
			// Do not count single-line comments that have a blank line inside them as a whole comment block.
			start = pos;
			firstComment = true;
		} else if (isWhitespace(ch)) {
			start = pos;
		} else if (isSpecial(ch)) {
			state = sDone;
		} else if (isOperator(ch)) {
			state = sOperator;
		} else if (ch == '"') {
			state = sString;
		} else if (ch == '#') {
			firstComment = true;
			state = sPreproc;
		} else {
			state = sText;
		}
		break;
	case sText:
		if (isOperator(ch) || isWhitespace(ch) || isSpecial(ch) || ch == '"') {
			state = sDone;
		} else {
			pos++;
		}
		break;
	case sOperator:
		if (!isOperator(ch)) {
			state = sDone;
		} else {
			pos++;
		}
		break;
	case sString:
		pos++;
		if (ch == '"') {
			state = sDone;
		} else if (ch == '\\') {
			pos++;
		}
		break;
	case sComment:
		start = ++pos;
		if (ch == '\n')
			state = sStart;
		else if (ch != '\r')
			commentEnd = start;
		break;
	case sMlComment:
		start = ++pos;
		if (ch == '*' && pos < src.size() && src[pos] == '/') {
			pos++;
			state = sStart;
		} else {
			commentEnd = start - 1;
		}
		break;
	case sPreproc:
		start = ++pos;
		if (ch == '\\') {
			state = sPreprocExtend;
		} else if (ch == '\n') {
			state = sStart;
		}
		break;
	case sPreprocExtend:
		start = ++pos;
		if (ch == '\n')
			state = sPreproc;
		else if (ch == '\r')
			;
		else
			state = sPreproc;
		break;
	case sDone:
		// pos--;
		break;
	}
}