private static CommentToken ResolveDelphiComment(LexerContext context) { var begin = context.GetIndex(); context.IncIndex(); context.IncIndex(); var inners = new List <CommentToken>(); while (!context.IsEnded() && !context.IsNewLineNow()) { if (CommentToken.IsOldStyleCommentBegin(context)) { inners.Add(ResolveOldStyleComment(context)); continue; } if (CommentToken.IsTurboPascalCommentBegin(context)) { inners.Add(ResolveTurboPascalComment(context)); continue; } context.IncIndex(); } var isSingleLine = inners.All(c => c.IsSingleLineComment); if (!isSingleLine) { throw new SyntaxErrorException(); } return(new CommentToken(true, inners, begin, context.GetIndex())); }
private static void ResolveWhitespace(LexerContext context) { while (!context.IsEnded() && (context.GetCurrentChar().Equals(' ') || ResolveNewLine(context))) { context.IncIndex(); } }
private static NumberToken ResolveNumber(LexerContext context) { var begin = context.GetIndex(); var builder = new StringBuilder(); if (NumberToken.IsDecimalNumberBegin(context)) { while (!context.IsEnded() && NumberToken.IsDecimalDigit(context)) { builder.Append(context.GetCurrentChar()); context.IncIndex(); } } else if (NumberToken.IsHexNumberBegin(context)) { context.IncIndex(); while (!context.IsEnded() && NumberToken.IsHexadecimalDigit(context)) { builder.Append(context.GetCurrentChar()); context.IncIndex(); } } else if (NumberToken.IsOctalNumberBegin(context)) { context.IncIndex(); while (!context.IsEnded() && NumberToken.IsOctalDigit(context)) { builder.Append(context.GetCurrentChar()); context.IncIndex(); } } else { context.IncIndex(); while (!context.IsEnded() && NumberToken.IsBinaryDigit(context)) { builder.Append(context.GetCurrentChar()); context.IncIndex(); } } return(new NumberToken(builder.ToString(), begin, context.GetIndex())); }
public List <IToken> Run(string str) { var context = new LexerContext(str); var res = new List <IToken>(); ResolveWhitespace(context); while (!context.IsEnded()) { res.Add(Resolve(context)); ResolveWhitespace(context); } return(res); }
private static CommentToken ResolveTurboPascalComment(LexerContext context) { var begin = context.GetIndex(); context.IncIndex(); var inners = new List <CommentToken>(); var isOneLine = true; while (!context.IsEnded()) { if (CommentToken.IsOldStyleCommentBegin(context)) { inners.Add(ResolveOldStyleComment(context)); continue; } if (CommentToken.IsDelphiCommentBegin(context)) { inners.Add(ResolveDelphiComment(context)); continue; } if (context.IsNewLineNow()) { isOneLine = false; for (var i = 0; i < Environment.NewLine.Length; i++) { context.IncIndex(); } } if (context.IsNewLineNow()) { isOneLine = false; for (var i = 0; i < Environment.NewLine.Length; i++) { context.IncIndex(); } } if (CommentToken.IsTurboPascalCommentEnd(context)) { context.IncIndex(); break; } context.IncIndex(); } return(new CommentToken(isOneLine && inners.All(c => c.IsSingleLineComment), inners, begin, context.GetIndex())); }
private static void ResolveControlString(LexerContext context, StringBuilder builder) { context.IncIndex(); var numberBuilder = new StringBuilder(); while (!context.IsEnded() && Char.IsDigit(context.GetCurrentChar())) { numberBuilder.Append(context.GetCurrentChar()); context.IncIndex(); } var num = Int32.Parse(numberBuilder.ToString()); builder.Append((char)num); }
private static IdentifierToken ResolveIdentifier(LexerContext context) { var builder = new StringBuilder(); var begin = context.GetIndex(); builder.Append(context.GetCurrentChar()); context.IncIndex(); while (!context.IsEnded() && (Char.IsDigit(context.GetCurrentChar()) || Char.IsLetter(context.GetCurrentChar()))) { builder.Append(context.GetCurrentChar()); context.IncIndex(); } return(new IdentifierToken(builder.ToString(), begin, context.GetIndex())); }
private static void ResolveQuotedString(LexerContext context, StringBuilder builder) { context.IncIndex(); while (!context.IsEnded() && StringToken.IsStringSymbol(context.GetCurrentChar())) { if (context.GetCurrentChar().Equals('\'')) { if (StringToken.IsDoubleQuote(context)) { builder.Append('\''); context.IncIndex(); context.IncIndex(); } else { context.IncIndex(); return; } } builder.Append(context.GetCurrentChar()); context.IncIndex(); } }