141 {
142
144
146
148
149 string lexeme = null;
150
151 List<Token> tokenStream = new List<Token>();
152
154 {
155
157
159
161 {
162
164 switch (ch)
165 {
166
167 case ' ':
168 case '\t':
169 case '\r':
170 case '\n':
171 break;
172
173 case '(':
175 break;
176 case ')':
178 break;
179 case '[':
181 break;
182 case ']':
184 break;
185 case '{':
187 break;
188 case '}':
190 break;
191 case '.':
193 break;
194 case ':':
196 break;
197 case ',':
199 break;
200 case ';':
202 break;
203
204
205 case '=':
207 break;
208 case '+':
210 break;
211 case '-':
213 break;
214 case '*':
216 break;
217 case '/':
219 break;
220 case '%':
222 break;
223 case '^':
225 break;
226 case '&':
228 break;
229 case '|':
231 break;
232 case '!':
234 break;
235 case '>':
237 break;
238 case '<':
240 break;
241 case '\"':
242 lexeme = "";
244 break;
245 case '\'':
246 lexeme = "";
248 break;
249 case '~':
251 break;
252
253 default:
254 if (char.IsLetter(ch) || ch == '_')
255 {
257 lexeme = "" + ch;
258 }
259 else if (char.IsDigit(ch))
260 {
261 lexeme = "" + ch;
263 }
264 else
266 break;
267
268 }
269
270 break;
271
272 case State.BinaryNot:
273 if (ch == '=')
274 {
277 }
278 break;
279
281 switch (ch)
282 {
283 case '/':
285 break;
286 case '*':
288 break;
289 case '=':
292 break;
293 default:
297 break;
298 }
299 break;
300
301 case State.InlineComment:
302
303 if (ch == '\n')
305 break;
306
307 case State.BlockComment:
308 if (ch == '*')
309 {
310
312
313 if (next == '/')
314 {
316 break;
317 }
318
319 }
320 break;
321
323 if (ch == '=')
324 {
327 }
328 else
329 {
333 }
334 break;
335
337 if (ch == '+')
338 {
341 }
342 else if (ch == '=')
343 {
346 }
347 else
348 {
352 }
353 break;
354
356 if (ch == '-')
357 {
360 }
361 else if (ch == '=')
362 {
365 }
366 else
367 {
371 }
372 break;
373
375 if (ch == '=')
376 {
379 }
380 else
381 {
385 }
386 break;
387
389 if (ch == '=')
390 {
393 }
394 break;
395
397 if (ch == '=')
398 {
401 }
402 else
403 {
407 }
408 break;
409
411 if (ch == '&')
412 {
415 }
416 else if (ch == '=')
417 {
420 }
421 else
423 break;
424
426 if (ch == '|')
427 {
430 }
431 else if (ch == '=')
432 {
435 }
436 else
438 break;
439
441 if (ch == '=')
442 {
445 }
446 else
447 {
451 }
452 break;
453
455 if (ch == '=')
456 {
459 }
460 else if (ch == '>')
461 {
464 }
465 else
466 {
470 }
471 break;
472
474 if (ch == '=')
475 {
478 }
479 else if (ch == '<')
480 {
483 }
484 else
485 {
489 }
490 break;
491
492 case State.Identifier:
493
494 if (char.IsLetterOrDigit(ch) || ch == '_')
495 lexeme += ch;
496
497 else
498 {
499
501
502 if (lexeme == "null")
504 else if (lexeme == "true" || lexeme == "false")
506 else if (lexeme == "if")
508 else if (lexeme == "else")
510 else if (lexeme == "while")
512 else if (lexeme == "for")
514 else if (lexeme == "foreach")
516 else if (lexeme == "in")
518 else if (lexeme == "switch")
520 else if (lexeme == "case")
522 else if (lexeme == "default")
524 else if (lexeme == "break")
526 else if (lexeme == "continue")
528 else if (lexeme == "function")
530 else if (lexeme == "return")
532
533 else if (lexeme == "shared")
535 else if (lexeme == "var")
537 else if (lexeme == "volatile")
539 else if (lexeme == "struct")
541 else if (lexeme == "enum")
543
544 else if (lexeme == "include")
546 else if (lexeme == "lock")
548 else if (lexeme == "run")
550 else if (lexeme == "yield")
552 else if (lexeme == "notify")
554 else if (lexeme == "wait")
556 else
558
560 {
561
562 bool val = false;
563
564 if (lexeme == "true")
565 val = true;
566
567 tokenStream.Add(
new Token(tokenType, val,
line,
column, currentLine));
568
569 }
570
571 else
572 tokenStream.Add(
new Token(tokenType, lexeme,
line,
column, currentLine));
573
575
577
578 }
579 break;
580
582
583 while (ch != '\'')
584 {
585 lexeme += ch;
587 }
588 if (ch == '\'')
589 {
590 char c;
591 if (lexeme == "\\n") c = '\n';
592 else if (lexeme == "\\t") c = '\t';
593 else if (lexeme == "\\b") c = '\b';
594 else if (lexeme == "\\r") c = '\r';
595 else if (lexeme == "\\f") c = '\f';
596 else if (lexeme == "\\\'") c = '\'';
597 else if (lexeme == "\\\"") c = '\"';
598 else if (lexeme == "\\\\") c = '\\';
599 else c = char.Parse(lexeme);
602 }
603 else
604 throw new LexerException(
"Ein 'Character' darf genau ein Zeichen lang sein - ausgenommen Steuerzeichen!",
line,
column,
lines[
line]);
605 break;
606
608 if (ch == '\"')
609 {
612 }
613 else if (ch == '\\')
614 {
616 }
617 else if (ch == '\r' || ch == '\n')
618 {
619 throw new LexerException(
"Ein String darf sich nicht auf mehrere Zeilen erstrecken.",
line,
column,
lines[
line]);
620 }
621 else
622 {
623 lexeme += ch;
624 }
625 break;
626
627 case State.EscapeString:
628
629
630
631 if (ch == '"')
632 {
633 lexeme += '\"';
635 }
636 else if (ch == '\\')
637 {
638 lexeme += ch;
640 }
641 else if (ch == 'n')
642 {
643 lexeme += '\n';
645 }
646 else if (ch == 't')
647 {
648 lexeme += '\t';
650 }
651 else if (ch == 'r')
652 {
653 lexeme += '\r';
655 }
656 else if (ch == 'n')
657 {
658 lexeme += '\n';
660 }
661 else if (ch == 'b')
662 {
663 lexeme += '\b';
665 }
666 else
667 throw new LexerException(
"Das Escapezeichen '\\" + ch +
"' kann in Strings nicht verarbeitet werden.",
line,
column,
lines[
line]);
668
669 break;
670
672
673
674
675
676 if (char.IsDigit(ch))
677 lexeme += ch;
678 else if (ch == '.')
679 {
680 lexeme += '.';
682 }
683 else if (ch == 'x')
684 {
685 lexeme += ch;
687 }
688 else if (ch == 'b')
689 {
690 int intValue = Convert.ToInt32(lexeme, 2);
691
694 }
695 else if (ch == 'o')
696 {
697 int intValue = Convert.ToInt32(lexeme, 8);
700 }
701 else
702 {
703 int intValue = int.Parse(lexeme);
707 }
708 break;
709
711 if (char.IsDigit(ch))
712 lexeme += ch;
713 else
714 {
715 float floatValue = float.Parse(lexeme, System.Globalization.CultureInfo.InvariantCulture);
719 }
720 break;
721
723 if (char.IsDigit(ch) || char.IsLetter(ch))
724 {
725 if (char.IsLetter(ch) && !(ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F'))
726 throw new LexerException(
"Ein hexadezimaler Wert darf ausser Zahlen nur Buchstaben von 'a' - 'f' bzw. 'A' - 'F' enthalten.",
line,
column, currentLine);
727 lexeme += ch;
728 }
729 else
730 {
731 int intValue = Convert.ToInt32(lexeme, 16);
735 }
736 break;
737
738 default:
739 throw new LexerException(
"Unbekannter Lexer Status '" +
state +
"'.");
740
741 }
742
743 }
744
746 throw new LexerException("Unerwartetes Ende des TokenStream.");
747
748 return tokenStream;
749
750 }
void UndoChar()
Definition Lexer.cs:97
bool EndOfSource
Definition Lexer.cs:73
void InvalidCharacter(char ch)
Definition Lexer.cs:67
char ReadChar()
Definition Lexer.cs:77
TokenType
Known types of Token.
Definition Token.cs:12