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
|
/*
Copyright (C) 2003 by Walter Schreppers
Copyright (C) 2004 by Cies Breijs
This program is free software; you can redistribute it and/or
modify it under the terms of version 2 of the GNU General Public
License as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <tqdom.h>
#include <tqfile.h>
#include <kdebug.h>
#include <klocale.h>
#include "settings.h"
#include "lexer.h"
Lexer::Lexer(TQTextIStream& iStream)
{
inputStream = &iStream;
row = 1;
col = 1;
prevCol = 1;
translate = new Translate();
}
Token Lexer::lex()
{
skipSpaces(); // skips the white space that it quite likely (indentation) infront of the Token
Token currentToken;
currentToken.type = tokNotSet; // not really needed
currentToken.look = "";
currentToken.value = 0;
currentToken.start.row = row;
currentToken.start.col = col;
TQChar currentChar = getChar();
if ( inputStream->atEnd() )
{
kdDebug(0)<<"Lexer::lex(), got EOF."<<endl;
currentToken.type = tokEOF;
currentToken.look = "EOF";
ungetChar(currentChar); // unget the currentChar and fix the row/col values
return currentToken;
}
if (currentChar == '#')
{
while ( !inputStream->atEnd() && !(currentChar == '\x0a' || currentChar == '\n') )
currentChar = getChar();
}
// if (currentChar.category() == TQChar::Separator_Line) somehow doesnt work
if (currentChar == '\x0a' || currentChar == '\n')
{
currentToken.type = tokEOL;
currentToken.look = "EOL";
}
else if (currentChar.isLetter() || currentChar == '[' || currentChar == ']')
{
ungetChar(currentChar);
// sets currentToken.look by reference, and set the currentToken.type to tokUnknown
currentToken.type = getWord(currentToken.look);
setTokenType(currentToken); // gets the actual tokenType
}
else if ( currentChar.isNumber() )
{
ungetChar(currentChar);
// set currentToken.value/look by reference, and set the currentToken.type to tokNumber
currentToken.type = getNumber(currentToken.value, currentToken.look);
}
else if (currentChar == '>')
{
currentChar = getChar();
if (currentChar == '=')
{
currentToken.type = tokGe;
currentToken.look = ">=";
}
else
{
ungetChar(currentChar);
currentToken.type = tokGt;
currentToken.look = ">";
}
}
else if (currentChar == '<')
{
currentChar = getChar();
if ( currentChar == '=' )
{
currentToken.type = tokLe;
currentToken.look = "<=";
}
else
{
ungetChar(currentChar);
currentToken.type = tokLt;
currentToken.look = ">";
}
}
else if (currentChar == '!')
{
currentChar = getChar();
if (currentChar == '=')
{
currentToken.type = tokNe;
currentToken.look = "!=";
}
else
{
ungetChar(currentChar);
currentToken.type = tokNot;
currentToken.look = "!";
}
}
else if (currentChar == '=')
{
currentChar = getChar();
if (currentChar == '=')
{
currentToken.type = tokEq;
currentToken.look = "==";
}
else
{
ungetChar(currentChar);
currentToken.type = tokAssign;
currentToken.look = "=";
}
}
else if (currentChar == '(')
{
currentToken.type = tokBraceOpen;
currentToken.look = "(";
}
else if (currentChar == ')')
{
currentToken.type = tokBraceClose;
currentToken.look = ")";
}
else if (currentChar == '+')
{
currentToken.type = tokPlus;
currentToken.look = "+";
}
else if (currentChar == '-')
{
currentToken.type = tokMinus;
currentToken.look = "-";
}
else if (currentChar == '*')
{
currentToken.type = tokMul;
currentToken.look = "*";
}
else if (currentChar == '/')
{
currentToken.type = tokDev;
currentToken.look = "/";
}
else if (currentChar == ',')
{
currentToken.type = tokComma;
currentToken.look = ",";
}
else if (currentChar == '"')
{
getString(currentToken);
}
else
{
currentToken.type = tokUnknown;
currentToken.look = currentChar;
}
currentToken.end.row = row;
currentToken.end.col = col;
return currentToken;
}
// PRIVATEs
TQChar Lexer::getChar()
{
TQChar c;
if ( !putBackChar.isNull() )
{
c = putBackChar; // use the char that is stored to be put back
// kdDebug(0)<<"Lexer::getChar(), restored: '"<<c<<"' @ ("<<row<<", "<<col<<")"<<endl;
putBackChar = TQChar(); // and set putBackChar back to NULL
if (c == '\x0a' || c == '\n')
{
row++;
prevCol = col;
col = 1;
}
else
{
col++;
}
}
else
{
*inputStream >> c; // take a TQChar of the inputStream
// kdDebug(0)<<"Lexer::getChar(): '"<<c<<"' @ ("<<row<<", "<<col<<")"<<endl;
if (c == '\x0a' || c == '\n')
{
row++;
prevCol = col;
col = 1;
}
else
{
col++;
}
}
return c;
}
void Lexer::ungetChar(TQChar c)
{
if (c == '\x0a' || c == '\n')
{
row--;
col = prevCol;
}
else
{
col--;
}
putBackChar = c;
// kdDebug(0)<<"Lexer::ungetChar(), saved char: '"<<c<<"' and steped back to ("<<row<<", "<<col<<")"<<endl;
}
int Lexer::getWord(TQString& word)
{
// kdDebug(0)<<"Lexer::getWord()"<<endl;
TQChar currentChar = getChar();
if ( currentChar.isLetter() || currentChar == '[' || currentChar == ']' ) {
while ( ( currentChar.isLetterOrNumber() || currentChar == '_' || currentChar == '[' || currentChar == ']' ) && !inputStream->atEnd() )
{
word += currentChar;
currentChar = getChar();
}
kdDebug(0)<<"Lexer::getWord(), got NAME: '"<<word<<"'"<<endl;
ungetChar(currentChar); //read one too much
return tokUnknown; // returns tokUnknown, actual token is to be determained later in Lexer::setTokenType
}
else return tokError;
}
void Lexer::setTokenType(Token& currentToken)
{
if (currentToken.type == tokUnknown)
{
// make lowercase copy of the word as it was found in the inputStream
TQString k = currentToken.look.lower();
// if the key is an alias translate that alias to a key
if ( !translate->alias2key(k).isEmpty() ) k = translate->alias2key(k);
if (k == translate->name2key("begin") ) currentToken.type = tokBegin;
else if (k == translate->name2key("end") ) currentToken.type = tokEnd;
else if (k == translate->name2key("while") ) currentToken.type = tokWhile;
else if (k == translate->name2key("if") ) currentToken.type = tokIf;
else if (k == translate->name2key("else") ) currentToken.type = tokElse;
else if (k == translate->name2key("for") ) currentToken.type = tokFor;
else if (k == translate->name2key("to") ) currentToken.type = tokTo;
else if (k == translate->name2key("step") ) currentToken.type = tokStep;
else if (k == translate->name2key("and") ) currentToken.type = tokAnd;
else if (k == translate->name2key("or") ) currentToken.type = tokOr;
else if (k == translate->name2key("not") ) currentToken.type = tokNot;
else if (k == translate->name2key("return") ) currentToken.type = tokReturn;
else if (k == translate->name2key("break") ) currentToken.type = tokBreak;
else if (k == translate->name2key("run") ) currentToken.type = tokRun;
else if (k == translate->name2key("foreach") ) currentToken.type = tokForEach;
else if (k == translate->name2key("in") ) currentToken.type = tokIn;
else if (k == translate->name2key("learn") ) currentToken.type = tokLearn;
else if (k == translate->name2key("clear") ) currentToken.type = tokClear;
else if (k == translate->name2key("go") ) currentToken.type = tokGo;
else if (k == translate->name2key("gox") ) currentToken.type = tokGoX;
else if (k == translate->name2key("goy") ) currentToken.type = tokGoY;
else if (k == translate->name2key("forward") ) currentToken.type = tokForward;
else if (k == translate->name2key("backward") ) currentToken.type = tokBackward;
else if (k == translate->name2key("direction") ) currentToken.type = tokDirection;
else if (k == translate->name2key("turnleft") ) currentToken.type = tokTurnLeft;
else if (k == translate->name2key("turnright") ) currentToken.type = tokTurnRight;
else if (k == translate->name2key("center") ) currentToken.type = tokCenter;
else if (k == translate->name2key("setpenwidth") ) currentToken.type = tokSetPenWidth;
else if (k == translate->name2key("penup") ) currentToken.type = tokPenUp;
else if (k == translate->name2key("pendown") ) currentToken.type = tokPenDown;
else if (k == translate->name2key("setfgcolor") ) currentToken.type = tokSetFgColor;
else if (k == translate->name2key("setbgcolor") ) currentToken.type = tokSetBgColor;
else if (k == translate->name2key("resizecanvas") ) currentToken.type = tokResizeCanvas;
else if (k == translate->name2key("spriteshow") ) currentToken.type = tokSpriteShow;
else if (k == translate->name2key("spritehide") ) currentToken.type = tokSpriteHide;
else if (k == translate->name2key("spritepress") ) currentToken.type = tokSpritePress;
else if (k == translate->name2key("spritechange") ) currentToken.type = tokSpriteChange;
else if (k == translate->name2key("do") ) currentToken.type = tokDo; // dummy commands
else if (k == translate->name2key("message") ) currentToken.type = tokMessage;
else if (k == translate->name2key("inputwindow") ) currentToken.type = tokInputWindow;
else if (k == translate->name2key("print") ) currentToken.type = tokPrint;
else if (k == translate->name2key("fonttype") ) currentToken.type = tokFontType;
else if (k == translate->name2key("fontsize") ) currentToken.type = tokFontSize;
else if (k == translate->name2key("repeat") ) currentToken.type = tokRepeat;
else if (k == translate->name2key("random") ) currentToken.type = tokRandom;
else if (k == translate->name2key("wait") ) currentToken.type = tokWait;
else if (k == translate->name2key("wrapon") ) currentToken.type = tokWrapOn;
else if (k == translate->name2key("wrapoff") ) currentToken.type = tokWrapOff;
else if (k == translate->name2key("reset") ) currentToken.type = tokReset;
else
{
kdDebug(0)<<"Lexer::setTokenType, found UNKNOWN word @ ("<<currentToken.start.row<<", "<<currentToken.start.col<<"), can be anything"<<endl;
// t.type = tokUnknown; is allready
}
kdDebug(0)<<"Lexer::setTokenType, found tok-number: '"<<currentToken.type<<"' with the key: '"<<k<<"' @ ("<<currentToken.start.row<<", "<<currentToken.start.col<<")"<<endl;
}
}
void Lexer::skipSpaces()
{
// kdDebug(0)<<"Lexer::skipSpaces(), skipping SPACES."<<endl;
TQChar currentChar = getChar();
// when the Separator_* groups can be identified in the TQChar thing would be easier
while ( !inputStream->atEnd() && ( currentChar.isSpace() && !(currentChar == '\x0a' || currentChar == '\n') ) )
{
currentChar = getChar();
}
ungetChar(currentChar); // unget the tokEOL we likely just found
}
int Lexer::getNumber(Value& num, TQString& look)
{
// by reference the value (Value) and look part are set
// kdDebug(0)<<"Lexer::getNumber()"<<endl;
bool hasPoint = false;
TQChar currentChar = getChar();
if ( currentChar.isNumber() )
{
while ( ( currentChar.isNumber() || (currentChar == '.' && !hasPoint) ) && !inputStream->atEnd() )
{
if (currentChar == '.')
{
hasPoint = true;
}
look += currentChar;
currentChar = getChar();
}
ungetChar(currentChar); //read one too much
num.setNumber( look.toDouble() );
kdDebug(0)<<"Lexer::getNumber(), got NUMBER: '"<<num.Number()<<"'"<<endl;
return tokNumber;
}
else return tokError;
}
void Lexer::getString(Token& currentToken)
{
TQString str = "\""; // start with a " cauz it just got lost
TQChar currentChar = TQChar(); // start empty
while ( currentChar != '"' && !(currentChar == '\x0a' || currentChar == '\n') && !inputStream->atEnd() )
{
currentChar = getChar();
if (currentChar == '\\') // escape sequence
{
currentChar = getChar();
switch (currentChar)
{
case 'n': str += '\n'; break;
case 't': str += '\t'; break;
case 'f': str += '\f'; break;
case '"': str += '"'; break;
}
}
else if (currentChar == '\x0a' || currentChar == '\n') // if the user forgot to delimit the string
{
ungetChar(currentChar);
break;
}
else str += currentChar;
}
currentToken.type = tokString;
currentToken.look = str;
kdDebug(0)<<"Lexer::getStringConstant, got STRINGCONSTANT: "<<currentToken.look<<"'"<<endl;
}
|