public static void IgnoreWhiteSpaceAndCommentsToNextLine(this IPreprocessorReader rdr) { char ch; bool lineChangeFound; while (!rdr.EOF) { ch = rdr.Peek(); if (ch == '\n') { rdr.Ignore(1); break; } if (char.IsWhiteSpace(ch)) { rdr.Ignore(1); continue; } if (!rdr.IgnoreComments(out lineChangeFound)) { break; } if (lineChangeFound) { break; } } }
public static string ReadAndIgnoreNestableContent(this IPreprocessorReader rdr, string endToken) { var sb = new StringBuilder(); string str; rdr.IgnoreWhiteSpaceAndComments(false); while (!rdr.EOF) { str = rdr.PeekToken(false); if (string.IsNullOrEmpty(str)) { continue; } if (str == endToken) { rdr.Ignore(str.Length); break; } else if (str == "(") { sb.Append(str); rdr.Ignore(str.Length); sb.Append(rdr.ReadAndIgnoreNestableContent(")")); sb.Append(")"); } else if (str == "{") { sb.Append(str); rdr.Ignore(str.Length); sb.Append(rdr.ReadAndIgnoreNestableContent("}")); sb.Append("}"); } else if (str == "[") { sb.Append(str); rdr.Ignore(str.Length); sb.Append(rdr.ReadAndIgnoreNestableContent("]")); sb.Append("]"); } else { sb.Append(str); rdr.Ignore(str.Length); } } return(sb.ToString()); }
public static string ReadAndIgnoreStringLiteral(this IPreprocessorReader rdr) { // This function assumes the next character to read is either " or '. char quote = rdr.Peek(); rdr.Ignore(1); char ch; var sb = new StringBuilder(); sb.Append(quote); while (!rdr.EOF) { ch = rdr.Peek(); if (ch == quote) { sb.Append(ch); rdr.Ignore(1); if (quote == '"' && rdr.Peek() == '"') { // Double "" means keep going sb.Append('"'); rdr.Ignore(1); } else { break; } } else if (ch == '\\') { sb.Append('\\'); rdr.Ignore(1); if (!rdr.EOF) { sb.Append(rdr.Peek()); rdr.Ignore(1); } } else { sb.Append(ch); rdr.Ignore(1); } } return(sb.ToString()); }
public static bool IgnoreComments(this IPreprocessorReader rdr, out bool lineChangeFound, bool multiLineOnly) { lineChangeFound = false; if (rdr.Peek() == '/') { var str = rdr.Peek(2); if (str == "/*") { rdr.Ignore(2); //rdr.IgnoreUntil(c => c != '/' && c != '*' && c != '\r' && c != '\n'); rdr.IgnoreUntil(_multiLineCommentBreakChars); char ch; while (!rdr.EOF) { ch = rdr.Peek(); if (ch == '*') { if (rdr.Peek(2) == "*/") { rdr.Ignore(2); return(true); } else { rdr.Ignore(1); } } else if (ch == '/') { bool lineChange; if (rdr.IgnoreComments(out lineChange, true)) { if (lineChange) { lineChangeFound = true; } } else { rdr.Ignore(1); } } else if (ch == '\r' || ch == '\n') { lineChangeFound = true; rdr.Ignore(1); } //rdr.IgnoreUntil(c => c != '/' && c != '*' && c != '\r' && c != '\n'); rdr.IgnoreUntil(_multiLineCommentBreakChars); } return(true); } else if (str == "//" && multiLineOnly == false) { //rdr.IgnoreUntil(c => c != '\r' && c != '\n'); rdr.IgnoreUntil(LineEndChars); return(true); } } return(false); }