private static void VerifyReadingSimpleText(IStringReader reader) { Assert.IsNotNull(reader); // read first line: var c = reader.ReadNext(); Assert.AreEqual('a', c); Assert.AreEqual('a', reader.CurrentChar); Assert.AreEqual(0, reader.Line); Assert.AreEqual(0, reader.LineOffset); Assert.IsFalse(reader.IsEmpty); c = reader.ReadNext(); Assert.AreEqual('\r', c); Assert.AreEqual(0, reader.Line); Assert.AreEqual(1, reader.LineOffset); Assert.IsFalse(reader.IsEmpty); c = reader.ReadNext(); Assert.AreEqual('\n', c); Assert.AreEqual('\n', reader.CurrentChar); Assert.AreEqual(0, reader.Line); Assert.AreEqual(2, reader.LineOffset); Assert.IsFalse(reader.IsEmpty); c = reader.ReadNext(); Assert.AreEqual('b', c); Assert.AreEqual('b', reader.CurrentChar); Assert.AreEqual(1, reader.Line); Assert.AreEqual(0, reader.LineOffset); Assert.IsFalse(reader.IsEmpty); c = reader.ReadNext(); Assert.AreEqual('\r', c); Assert.AreEqual('\r', reader.CurrentChar); Assert.AreEqual(1, reader.Line); Assert.AreEqual(1, reader.LineOffset); Assert.IsFalse(reader.IsEmpty); c = reader.ReadNext(); Assert.AreEqual('\n', c); Assert.AreEqual('\n', reader.CurrentChar); Assert.AreEqual(1, reader.Line); Assert.AreEqual(2, reader.LineOffset); Assert.IsFalse(reader.IsEof); Assert.IsFalse(reader.IsEmpty); c = reader.ReadNext(); Assert.AreEqual('\0', c); Assert.AreEqual('\0', reader.CurrentChar); Assert.AreEqual(2, reader.Line); Assert.AreEqual(-1, reader.LineOffset); Assert.IsTrue(reader.IsEof); Assert.IsFalse(reader.IsEmpty); }
internal static StringHelperStatusCode ReadCommentChars(IStringReader reader, bool multiline) { if (multiline) { char previousChar; char currentChar = '\0'; do { previousChar = currentChar; currentChar = reader.ReadNext(); if (reader.IsEof) { return(StringHelperStatusCode.UnexpectedEoF); } if (previousChar == '*' && currentChar == '/') { return(StringHelperStatusCode.Success); } }while (true); } else { do { var currentChar = reader.ReadNext(); if (reader.IsEof) { return(StringHelperStatusCode.UnexpectedEoF); } if (currentChar == '\r' || currentChar == '\n') { return(StringHelperStatusCode.Success); } }while (true); } }
/// <summary> /// Reads the keyword definition chars from given input. /// </summary> internal static StringHelperStatusCode ReadKeywordChars(IStringReader reader, StringBuilder output) { do { var currentChar = reader.ReadNext(); if (char.IsLetter(currentChar)) { output.Append(currentChar); } else { break; } }while (true); return(StringHelperStatusCode.Success); }
/// <summary> /// Reads characters that might be a number and copies them to given output. /// </summary> internal static StringHelperStatusCode ReadIntegerNumberChars(IStringReader reader, StringBuilder output) { do { var currentChar = reader.ReadNext(); if (char.IsDigit(currentChar) || currentChar == '-' || currentChar == '+') { output.Append(currentChar); } else { break; } }while (true); return(StringHelperStatusCode.Success); }
private static StringListToken ReadNextToken(IStringReader reader) { // skip white spaces here: StringHelper.ReadWhiteChars(reader); if (reader.IsEof) { return(StringListToken.Eof); } var currentChar = reader.CurrentChar; switch (currentChar) { case '/': currentChar = reader.ReadNext(); if (currentChar == '/') { return(StringListToken.CommentLine); } if (currentChar == '*') { return(StringListToken.CommentMultiline); } throw new FormatException("Invalid comment token at (" + reader.Line + ":" + (reader.LineOffset - 1) + ")"); case ';': return(StringListToken.Semicolon); case '"': return(StringListToken.String); case '=': return(StringListToken.Identity); } return(StringListToken.Invalid); }
/// <summary> /// Reads from the current input stream all the whitespaces. /// </summary> internal static StringHelperStatusCode ReadWhiteChars(IStringReader reader, out int count) { count = 0; do { char currentChar = reader.ReadNext(); if (reader.IsEof) { return(StringHelperStatusCode.UnexpectedEoF); } if (!char.IsWhiteSpace(currentChar)) { break; } count++; }while (true); return(StringHelperStatusCode.Success); }
/// <summary> /// Reads the string from given input stream. /// </summary> internal static StringHelperStatusCode ReadStringChars(IStringReader reader, StringBuilder output, StringBuilder escapedUnicodeNumberBuffer, bool errorOnNewLine, out int lastLine, out int lastOffset) { bool escape = false; bool unicodeNumber = false; if (escapedUnicodeNumberBuffer == null) { escapedUnicodeNumberBuffer = new StringBuilder(); } lastLine = reader.Line; lastOffset = reader.LineOffset; do { if (!unicodeNumber) { lastLine = reader.Line; lastOffset = reader.LineOffset; } var currentChar = reader.ReadNext(); // verify if not an invalid character was found in text: if (reader.IsEof) { return(StringHelperStatusCode.UnexpectedEoF); } if (errorOnNewLine && (currentChar == '\r' || currentChar == '\n')) { return(StringHelperStatusCode.UnexpectedNewLine); } if (unicodeNumber) { StringHelperStatusCode result = ReadStringUnicodeCharacter(currentChar, output, escapedUnicodeNumberBuffer, out unicodeNumber); // if parsing Unicode character failed, immediatelly stop! if (result != StringHelperStatusCode.Success) { return(result); } continue; } if (currentChar == '\\' && !escape) { escape = true; } else { if (escape) { switch (currentChar) { case 'n': output.Append('\n'); break; case 'r': output.Append('\r'); break; case 't': output.Append('\t'); break; case '/': output.Append('/'); break; case '\\': output.Append('\\'); break; case 'f': output.Append('\f'); break; case 'U': case 'u': unicodeNumber = true; break; case '"': output.Append('"'); break; case '\'': output.Append('\''); break; default: return(StringHelperStatusCode.UnknownEscapedChar); } escape = false; } else { if (currentChar == '"') { break; } output.Append(currentChar); } } }while (true); // as the string might finish with a Unicode character... if (unicodeNumber) { return(AddUnicodeChar(output, escapedUnicodeNumberBuffer, false)); } return(StringHelperStatusCode.Success); }
internal static StringHelperStatusCode ReadCommentChars(IStringReader reader, bool multiline) { if (multiline) { char previousChar; char currentChar = '\0'; do { previousChar = currentChar; currentChar = reader.ReadNext(); if (reader.IsEof) return StringHelperStatusCode.UnexpectedEoF; if (previousChar == '*' && currentChar == '/') return StringHelperStatusCode.Success; } while (true); } else { do { var currentChar = reader.ReadNext(); if (reader.IsEof) return StringHelperStatusCode.UnexpectedEoF; if (currentChar == '\r' || currentChar == '\n') return StringHelperStatusCode.Success; } while (true); } }
/// <summary> /// Reads the keyword definition chars from given input. /// </summary> internal static StringHelperStatusCode ReadKeywordChars(IStringReader reader, StringBuilder output) { do { var currentChar = reader.ReadNext(); if (char.IsLetter(currentChar)) { output.Append(currentChar); } else break; } while (true); return StringHelperStatusCode.Success; }
/// <summary> /// Reads the string from given input stream. /// </summary> internal static StringHelperStatusCode ReadStringChars(IStringReader reader, StringBuilder output, StringBuilder escapedUnicodeNumberBuffer, bool errorOnNewLine, out int lastLine, out int lastOffset) { bool escape = false; bool unicodeNumber = false; if (escapedUnicodeNumberBuffer == null) escapedUnicodeNumberBuffer = new StringBuilder(); lastLine = reader.Line; lastOffset = reader.LineOffset; do { if (!unicodeNumber) { lastLine = reader.Line; lastOffset = reader.LineOffset; } var currentChar = reader.ReadNext(); // verify if not an invalid character was found in text: if (reader.IsEof) return StringHelperStatusCode.UnexpectedEoF; if (errorOnNewLine && (currentChar == '\r' || currentChar == '\n')) return StringHelperStatusCode.UnexpectedNewLine; if (unicodeNumber) { StringHelperStatusCode result = ReadStringUnicodeCharacter(currentChar, output, escapedUnicodeNumberBuffer, out unicodeNumber); // if parsing Unicode character failed, immediatelly stop! if (result != StringHelperStatusCode.Success) return result; continue; } if (currentChar == '\\' && !escape) { escape = true; } else { if (escape) { switch (currentChar) { case 'n': output.Append('\n'); break; case 'r': output.Append('\r'); break; case 't': output.Append('\t'); break; case '/': output.Append('/'); break; case '\\': output.Append('\\'); break; case 'f': output.Append('\f'); break; case 'U': case 'u': unicodeNumber = true; break; case '"': output.Append('"'); break; case '\'': output.Append('\''); break; default: return StringHelperStatusCode.UnknownEscapedChar; } escape = false; } else { if (currentChar == '"') break; output.Append(currentChar); } } } while (true); // as the string might finish with a Unicode character... if (unicodeNumber) return AddUnicodeChar(output, escapedUnicodeNumberBuffer, false); return StringHelperStatusCode.Success; }
/// <summary> /// Reads characters that might be a number and copies them to given output. /// </summary> internal static StringHelperStatusCode ReadIntegerNumberChars(IStringReader reader, StringBuilder output) { do { var currentChar = reader.ReadNext(); if (char.IsDigit(currentChar) || currentChar == '-' || currentChar == '+') { output.Append(currentChar); } else break; } while (true); return StringHelperStatusCode.Success; }
/// <summary> /// Reads from the current input stream all the whitespaces. /// </summary> internal static StringHelperStatusCode ReadWhiteChars(IStringReader reader, out int count) { count = 0; do { char currentChar = reader.ReadNext(); if (reader.IsEof) return StringHelperStatusCode.UnexpectedEoF; if (!char.IsWhiteSpace(currentChar)) break; count++; } while (true); return StringHelperStatusCode.Success; }