149 string lexeme =
null;
151 List<Token> tokenStream =
new List<Token>();
156 string currentLine = lines[line];
158 char ch = ReadChar();
174 tokenStream.Add(
new Token(
TokenType.LeftParen,
"(", line, column, currentLine));
177 tokenStream.Add(
new Token(
TokenType.RightParen,
")", line, column, currentLine));
180 tokenStream.Add(
new Token(
TokenType.LeftBracket,
"[", line, column, currentLine));
183 tokenStream.Add(
new Token(
TokenType.RightBracket,
"]", line, column, currentLine));
186 tokenStream.Add(
new Token(
TokenType.LeftBrace,
"{", line, column, currentLine));
189 tokenStream.Add(
new Token(
TokenType.RightBrace,
"}", line, column, currentLine));
192 tokenStream.Add(
new Token(
TokenType.Period,
".", line, column, currentLine));
195 tokenStream.Add(
new Token(
TokenType.Colon,
":", line, column, currentLine));
198 tokenStream.Add(
new Token(
TokenType.Comma,
",", line, column, currentLine));
201 tokenStream.Add(
new Token(
TokenType.SemiColon,
";", line, column, currentLine));
206 state = State.Assign;
215 state = State.Multiply;
218 state = State.Divide;
221 state = State.Modulo;
236 state = State.Greater;
243 state = State.String;
250 state = State.BinaryNot;
254 if (
char.IsLetter(ch) || ch ==
'_')
256 state = State.Identifier;
259 else if (
char.IsDigit(ch))
262 state = State.Number;
265 InvalidCharacter(ch);
272 case State.BinaryNot:
275 tokenStream.Add(
new Token(
TokenType.AssignBinaryNot,
"~=", line, column, currentLine));
284 state = State.InlineComment;
287 state = State.BlockComment;
290 tokenStream.Add(
new Token(
TokenType.AssignDivide,
"/=", line, column, currentLine));
294 tokenStream.Add(
new Token(
TokenType.Divide,
"/", line, column, currentLine));
301 case State.InlineComment:
307 case State.BlockComment:
311 char next = ReadChar();
325 tokenStream.Add(
new Token(
TokenType.Equal,
"==", line, column, currentLine));
330 tokenStream.Add(
new Token(
TokenType.Assign,
"=", line, column, currentLine));
339 tokenStream.Add(
new Token(
TokenType.Increment,
"++", line, column, currentLine));
344 tokenStream.Add(
new Token(
TokenType.AssignPlus,
"+=", line, column, currentLine));
349 tokenStream.Add(
new Token(
TokenType.Plus,
"+", line, column, currentLine));
358 tokenStream.Add(
new Token(
TokenType.Decrement,
"--", line, column, currentLine));
363 tokenStream.Add(
new Token(
TokenType.AssignMinus,
"-=", line, column, currentLine));
368 tokenStream.Add(
new Token(
TokenType.Minus,
"-", line, column, currentLine));
377 tokenStream.Add(
new Token(
TokenType.AssignMultiply,
"*=", line, column, currentLine));
382 tokenStream.Add(
new Token(
TokenType.Multiply,
"*", line, column, currentLine));
391 tokenStream.Add(
new Token(
TokenType.AssignXor,
"^=", line, column, currentLine));
399 tokenStream.Add(
new Token(
TokenType.AssignModulo,
"%=", line, column, currentLine));
404 tokenStream.Add(
new Token(
TokenType.Modulo,
"%", line, column, currentLine));
413 tokenStream.Add(
new Token(
TokenType.And,
"&&", line, column, currentLine));
418 tokenStream.Add(
new Token(
TokenType.AssignBinaryAnd,
"&=", line, column, currentLine));
422 InvalidCharacter(ch);
428 tokenStream.Add(
new Token(
TokenType.Or,
"||", line, column, currentLine));
433 tokenStream.Add(
new Token(
TokenType.AssignBinaryOr,
"|=", line, column, currentLine));
437 InvalidCharacter(ch);
443 tokenStream.Add(
new Token(
TokenType.NotEqual,
"!=", line, column, currentLine));
448 tokenStream.Add(
new Token(
TokenType.Not,
"!", line, column, currentLine));
457 tokenStream.Add(
new Token(
TokenType.GreaterEqual,
">=", line, column, currentLine));
462 tokenStream.Add(
new Token(
TokenType.ShiftRight,
">>", line, column, currentLine));
467 tokenStream.Add(
new Token(
TokenType.Greater,
">", line, column, currentLine));
476 tokenStream.Add(
new Token(
TokenType.LessEqual,
"<=", line, column, currentLine));
481 tokenStream.Add(
new Token(
TokenType.ShiftLeft,
"<<", line, column, currentLine));
486 tokenStream.Add(
new Token(
TokenType.Less,
"<", line, column, currentLine));
492 case State.Identifier:
494 if (
char.IsLetterOrDigit(ch) || ch ==
'_')
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")
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")
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")
564 if (lexeme ==
"true")
567 tokenStream.Add(
new Token(tokenType, val, line, column, currentLine));
572 tokenStream.Add(
new Token(tokenType, lexeme, line, column, currentLine));
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);
600 tokenStream.Add(
new Token(
TokenType.Char, c, line, column, currentLine));
604 throw new LexerException(
"Ein 'Character' darf genau ein Zeichen lang sein - ausgenommen Steuerzeichen!", line, column, lines[line]);
610 tokenStream.Add(
new Token(
TokenType.String, lexeme, line, column, currentLine));
615 state = State.EscapeString;
617 else if (ch ==
'\r' || ch ==
'\n')
619 throw new LexerException(
"Ein String darf sich nicht auf mehrere Zeilen erstrecken.", line, column, lines[line]);
627 case State.EscapeString:
634 state = State.String;
639 state = State.String;
644 state = State.String;
649 state = State.String;
654 state = State.String;
659 state = State.String;
664 state = State.String;
667 throw new LexerException(
"Das Escapezeichen '\\" + ch +
"' kann in Strings nicht verarbeitet werden.", line, column, lines[line]);
676 if (
char.IsDigit(ch))
690 int intValue = Convert.ToInt32(lexeme, 2);
692 tokenStream.Add(
new Token(
TokenType.Integer, intValue, line, column, currentLine));
697 int intValue = Convert.ToInt32(lexeme, 8);
698 tokenStream.Add(
new Token(
TokenType.Integer, intValue, line, column, currentLine));
703 int intValue =
int.Parse(lexeme);
704 tokenStream.Add(
new Token(
TokenType.Integer, intValue, line, column, currentLine));
711 if (
char.IsDigit(ch))
715 float floatValue =
float.Parse(lexeme, System.Globalization.CultureInfo.InvariantCulture);
716 tokenStream.Add(
new Token(
TokenType.Float, floatValue, line, column, currentLine));
723 if (
char.IsDigit(ch) ||
char.IsLetter(ch))
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);
731 int intValue = Convert.ToInt32(lexeme, 16);
732 tokenStream.Add(
new Token(
TokenType.Integer, intValue, line, column, currentLine));
739 throw new LexerException(
"Unbekannter Lexer Status '" + state +
"'.");
745 if (state != State.None)