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 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 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 StringToken ResolveString(LexerContext context) { var begin = context.GetIndex(); var builder = new StringBuilder(); while (StringToken.IsStringBegin(context)) { if (context.GetCurrentChar().Equals('\'')) { ResolveQuotedString(context, builder); } else { ResolveControlString(context, builder); } } return(new StringToken(builder.ToString(), begin, context.GetIndex())); }
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())); }
private static SpecialSymbolToken ResolveSpecial(LexerContext context) { var begin = context.GetIndex(); if (!context.IsLast() && SpecialSymbolToken.IsSpecialPair(context)) { var pair = context.GetCharPair(); context.IncIndex(); context.IncIndex(); return(new SpecialSymbolToken(pair, begin)); } else { var ch = context.GetCurrentChar(); context.IncIndex(); return(new SpecialSymbolToken(ch, begin)); } }