/// <summary> /// Skip until the first non-whitespace character where comments are /// also treated as whitespace. /// </summary> /// <param name="pbr"></param> /// <exception cref="System.IO.IOException"/> /// <exception cref="Gavaghan.JSON.JSONException"/> public override void SkipWhitespace(PushbackReader pbr) { for (; ;) { int c = pbr.Read(); if (c < 0) { break; // bail on EOF } if (!IsWhitespace(c)) { // it's not whitespace, so see if it's the start of a comment if (c == '/') { int next = pbr.Read(); // is it a line comment? if (next == '/') { SkipLineComment(pbr); continue; } // is it a block comment? else if (next == '*') { SkipBlockComment(pbr); continue; } // else, unread - it's the end of the whitespace pbr.Unread(c); } pbr.Unread(c); break; } } }
/// <summary> /// Skip to first non-whitespace character. /// </summary> /// <param name="pbr">a pushback reader</param> /// <exception cref="System.IO.IOException"/> public virtual void SkipWhitespace(PushbackReader pbr) { for (; ;) { int c = pbr.Read(); if (c < 0) { break; // bail on EOF } // if non-whitespace found, push it back and exit if (!IsWhitespace(c)) { pbr.Unread(c); break; } } }
/// <summary> /// Read the JSON value that comes after the whitespace (if any). /// </summary> /// <param name="reader"></param> /// <returns></returns> /// <exception cref="System.IO.IOException" /> /// <exception cref="Gavaghan.JSON.JSONException" /> public IJSONValue Read(TextReader reader) { PushbackReader pbr = new PushbackReader(reader, PushbackBufferSize); // look for start of value SkipWhitespace(pbr); int c = pbr.Read(); // bail out early if EOF if (c < 0) { return(null); } pbr.Unread(c); return(Read("$", pbr)); }
/// <summary> /// Throw away characters until the line comment is completely read. /// </summary> /// <param name="pbr">the PushbackReader to use</param> /// <exception cref="System.IO.IOException"/> /// <exception cref="Gavaghan.JSON.JSONException"/> private void SkipLineComment(PushbackReader pbr) { for (; ;) { int c = pbr.Read(); // out of data? quit if (c < 0) { break; } // end of line? quit if ((c == '\n') || (c == '\r')) { break; } } }
/// <summary> /// Throw away characters until the block comment is completely read. /// </summary> /// <param name="pbr">the PushbackReader to use</param> /// <exception cref="System.IO.IOException"/> /// <exception cref="Gavaghan.JSON.JSONException"/> private void SkipBlockComment(PushbackReader pbr) { bool star = false; for (; ;) { int c = pbr.Read(); // out of data? throw exception if (c < 0) { throw new JSONException("$", "Unterminated block comment at end of file"); } if (star && (c == '/')) { break; } star = (c == '*'); } }