Exemplo n.º 1
0
 public void BackRewindsCounters()
 {
     BufferedCharReader reader = new BufferedCharReader(new StringReader("a"));
     reader.Next();
     reader.Back();
     AssertCounters(reader, 0, 0, 0);
 }
Exemplo n.º 2
0
 public void CountersOnReadingSecondChar()
 {
     BufferedCharReader reader = new BufferedCharReader(new StringReader("ab"));
     reader.Next();
     reader.Next();
     AssertCounters(reader, 2, 1, 2);
 }
Exemplo n.º 3
0
        public void CountersOnReadingFirstChar()
        {
            BufferedCharReader reader = new BufferedCharReader(new StringReader("a"));

            reader.Next();
            AssertCounters(reader, 1, 1, 1);
        }
Exemplo n.º 4
0
        public void CountersOnReadingSecondChar()
        {
            BufferedCharReader reader = new BufferedCharReader(new StringReader("ab"));

            reader.Next();
            reader.Next();
            AssertCounters(reader, 2, 1, 2);
        }
Exemplo n.º 5
0
        public void BackRewindsCounters()
        {
            BufferedCharReader reader = new BufferedCharReader(new StringReader("a"));

            reader.Next();
            reader.Back();
            AssertCounters(reader, 0, 0, 0);
        }
Exemplo n.º 6
0
        public JsonTextReader(TextReader reader)
        {
            if (reader == null)
                throw new ArgumentNullException("reader");

            _reader = new BufferedCharReader(reader);
            Push(ParseMethod);
        }
Exemplo n.º 7
0
        public void NextAfterBackRestoresCounters()
        {
            BufferedCharReader reader = new BufferedCharReader(new StringReader("a"));

            reader.Next();
            reader.Back();
            reader.Next();
            AssertCounters(reader, 1, 1, 1);
        }
 public JsonTextReader(TextReader reader)
 {
     if (reader == null)
     {
         throw new ArgumentNullException("reader");
     }
     _reader = new BufferedCharReader(reader);
     Push(ParseMethod);
 }
Exemplo n.º 9
0
        public void CountersWhenBackNextAroundLine()
        {
            BufferedCharReader reader = new BufferedCharReader(new StringReader("12\n34"));

            reader.Next(); AssertCounters(reader, 1, 1, 1);
            reader.Next(); AssertCounters(reader, 2, 1, 2);
            reader.Next(); AssertCounters(reader, 3, 1, 3);
            reader.Next(); AssertCounters(reader, 4, 2, 1);
            reader.Back(); AssertCounters(reader, 3, 1, 3);
            reader.Next(); AssertCounters(reader, 4, 2, 1);
        }
Exemplo n.º 10
0
 public void BacktrackAfterEnding()
 {
     BufferedCharReader reader = new BufferedCharReader(new StringReader(";"));
     Assert.IsTrue(reader.More());
     Assert.AreEqual(';', reader.Next());
     Assert.IsFalse(reader.More());
     reader.Back();
     Assert.IsTrue(reader.More());
     Assert.AreEqual(';', reader.Next());
     Assert.IsFalse(reader.More());
 }
Exemplo n.º 11
0
 private static char ParseHex(BufferedCharReader input, char[] hexDigits)
 {
     Debug.Assert(input != null);
     Debug.Assert(hexDigits != null);
     Debug.Assert(hexDigits.Length == 4);
     hexDigits[0] = input.Next();
     hexDigits[1] = input.Next();
     hexDigits[2] = input.Next();
     hexDigits[3] = input.Next();
     return((char)ushort.Parse(new string(hexDigits), NumberStyles.HexNumber));
 }
Exemplo n.º 12
0
        public void BacktrackAfterEnding()
        {
            BufferedCharReader reader = new BufferedCharReader(new StringReader(";"));

            Assert.IsTrue(reader.More());
            Assert.AreEqual(';', reader.Next());
            Assert.IsFalse(reader.More());
            reader.Back();
            Assert.IsTrue(reader.More());
            Assert.AreEqual(';', reader.Next());
            Assert.IsFalse(reader.More());
        }
Exemplo n.º 13
0
 public void CountersUnaffectedWhenReadingPastEOF()
 {
     BufferedCharReader reader = new BufferedCharReader(new StringReader("abc"));
     Assert.AreEqual('a', reader.Next());
     Assert.AreEqual('b', reader.Next());
     Assert.AreEqual('c', reader.Next());
     Assert.IsFalse(reader.More());
     Assert.AreEqual(0, reader.Next());
     Assert.AreEqual(3, reader.CharCount, "CharCount");
     Assert.AreEqual(1, reader.LineNumber, "LineNumber");
     Assert.AreEqual(3, reader.LinePosition, "LinePosition");
 }
Exemplo n.º 14
0
        public void CountersUnaffectedWhenReadingPastEOF()
        {
            BufferedCharReader reader = new BufferedCharReader(new StringReader("abc"));

            Assert.AreEqual('a', reader.Next());
            Assert.AreEqual('b', reader.Next());
            Assert.AreEqual('c', reader.Next());
            Assert.IsFalse(reader.More());
            Assert.AreEqual(0, reader.Next());
            Assert.AreEqual(3, reader.CharCount, "CharCount");
            Assert.AreEqual(1, reader.LineNumber, "LineNumber");
            Assert.AreEqual(3, reader.LinePosition, "LinePosition");
        }
Exemplo n.º 15
0
 protected override JsonToken ReadTokenImpl()
 {
     if (_stack == null)
     {
         return(JsonToken.EOF());
     }
     if (_stack.Count == 0)
     {
         _stack  = null;
         _reader = null;
         return(JsonToken.EOF());
     }
     return(Pop()());
 }
Exemplo n.º 16
0
        public void LineNumberBumpsAndPositionResetsWithEachLF()
        {
            BufferedCharReader reader = new BufferedCharReader(new StringReader("\n1\n23\n456\n"));

            AssertReadLineNumPos(reader, '\n', 1, 1);
            AssertReadLineNumPos(reader, '1', 2, 1);
            AssertReadLineNumPos(reader, '\n', 2, 2);
            AssertReadLineNumPos(reader, '2', 3, 1);
            AssertReadLineNumPos(reader, '3', 3, 2);
            AssertReadLineNumPos(reader, '\n', 3, 3);
            AssertReadLineNumPos(reader, '4', 4, 1);
            AssertReadLineNumPos(reader, '5', 4, 2);
            AssertReadLineNumPos(reader, '6', 4, 3);
            AssertReadLineNumPos(reader, '\n', 4, 4);
            AssertReadLineNumPos(reader, '\0', 4, 4);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Reads the next token and returns it.
        /// </summary>

        protected override JsonToken ReadTokenImpl()
        {
            if (_stack == null)
            {
                return JsonToken.EOF();
            }
            else if (_stack.Count == 0)
            {
                _stack = null;
                _reader = null;
                return JsonToken.EOF();
            }
            else
            {
                return Pop()();
            }
        }
Exemplo n.º 18
0
        /// <summary>
        /// Reads the next token and returns it.
        /// </summary>

        protected override JsonToken ReadTokenImpl()
        {
            if (_stack == null)
            {
                return(JsonToken.EOF());
            }
            else if (_stack.Count == 0)
            {
                _stack           = null;
                _endLineNumber   = _reader.LineNumber;
                _endLinePosition = _reader.LinePosition;
                _endCharCount    = _reader.CharCount;
                _reader          = null;
                return(JsonToken.EOF());
            }
            else
            {
                return(Pop()());
            }
        }
Exemplo n.º 19
0
 public void CountersOnReadingFirstChar()
 {
     BufferedCharReader reader = new BufferedCharReader(new StringReader("a"));
     reader.Next();
     AssertCounters(reader, 1, 1, 1);
 }
Exemplo n.º 20
0
        /// <summary>
        /// Return the characters up to the next close quote character.
        /// Backslash processing is done. The formal JSON format does not
        /// allow strings in single quotes, but an implementation is allowed to
        /// accept them.
        /// </summary>
        /// <param name="quote">The quoting character, either " or '</param>
        /// <returns>A String.</returns>

        // TODO: Consider rendering Dequote public

        internal static string Dequote(BufferedCharReader input, char quote)
        {
            return(Dequote(input, quote, null).ToString());
        }
Exemplo n.º 21
0
        internal static StringBuilder Dequote(BufferedCharReader input, char quote, StringBuilder output)
        {
            Debug.Assert(input != null);

            if (output == null)
            {
                output = new StringBuilder();
            }

            char[] hexDigits = null;

            while (true)
            {
                char ch = input.Next();

                if (ch == BufferedCharReader.EOF)
                {
                    throw new FormatException("Unterminated string.");
                }

                if (ch == '\\')
                {
                    ch = input.Next();

                    switch (ch)
                    {
                    case 'b': output.Append('\b'); break;     // Backspace

                    case 't': output.Append('\t'); break;     // Horizontal tab

                    case 'n': output.Append('\n'); break;     // Newline

                    case 'f': output.Append('\f'); break;     // Form feed

                    case 'r': output.Append('\r'); break;     // Carriage return

                    case 'u':
                    {
                        if (hexDigits == null)
                        {
                            hexDigits = new char[4];
                        }

                        output.Append(ParseHex(input, hexDigits));
                        break;
                    }

                    default:
                        output.Append(ch);
                        break;
                    }
                }
                else
                {
                    if (ch == quote)
                    {
                        return(output);
                    }

                    output.Append(ch);
                }
            }
        }
Exemplo n.º 22
0
 private static void AssertReadLineNumPos(BufferedCharReader reader, char expected, int line, int pos)
 {
     Assert.AreEqual(expected, reader.Next(), "Read");
     AssertLineCounters(reader, line, pos);
 }
Exemplo n.º 23
0
 private static void AssertLineCounters(BufferedCharReader reader, int line, int pos)
 {
     Assert.AreEqual(line, reader.LineNumber, "LineNumber");
     Assert.AreEqual(pos, reader.LinePosition, "LinePosition");
 }
Exemplo n.º 24
0
        internal static StringBuilder Dequote(BufferedCharReader input, char quote, StringBuilder output)
        {
            Debug.Assert(input != null);

            if (output == null)
                output = new StringBuilder();
            
            char[] hexDigits = null;
            
            while (true)
            {
                char ch = input.Next();

                if ((ch == BufferedCharReader.EOF) || (ch == '\n') || (ch == '\r')) 
                    throw new FormatException("Unterminated string.");

                if (ch == '\\')
                {
                    ch = input.Next();

                    switch (ch)
                    {
                        case 'b': output.Append('\b'); break; // Backspace
                        case 't': output.Append('\t'); break; // Horizontal tab
                        case 'n': output.Append('\n'); break; // Newline
                        case 'f': output.Append('\f'); break; // Form feed
                        case 'r': output.Append('\r'); break; // Carriage return 
                            
                        case 'u':
                        {
                            if (hexDigits == null)
                                hexDigits = new char[4];
                            
                            output.Append(ParseHex(input, hexDigits)); 
                            break;
                        }
                            
                        default:
                            output.Append(ch);
                            break;
                    }
                }
                else
                {
                    if (ch == quote)
                        return output;

                    output.Append(ch);
                }
            }
        }
Exemplo n.º 25
0
 public void CountersWhenBackNextAroundLine()
 {
     BufferedCharReader reader = new BufferedCharReader(new StringReader("12\n34"));
     reader.Next(); AssertCounters(reader, 1, 1, 1);
     reader.Next(); AssertCounters(reader, 2, 1, 2);
     reader.Next(); AssertCounters(reader, 3, 1, 3);
     reader.Next(); AssertCounters(reader, 4, 2, 1);
     reader.Back(); AssertCounters(reader, 3, 1, 3);
     reader.Next(); AssertCounters(reader, 4, 2, 1);
 }
Exemplo n.º 26
0
 public void LineNumberBumpsAndPositionResetsWithEachLF()
 {
     BufferedCharReader reader = new BufferedCharReader(new StringReader("\n1\n23\n456\n"));
     AssertReadLineNumPos(reader, '\n',  1, 1);
     AssertReadLineNumPos(reader, '1',   2, 1);
     AssertReadLineNumPos(reader, '\n',  2, 2);
     AssertReadLineNumPos(reader, '2',   3, 1);
     AssertReadLineNumPos(reader, '3',   3, 2);
     AssertReadLineNumPos(reader, '\n',  3, 3);
     AssertReadLineNumPos(reader, '4',   4, 1);
     AssertReadLineNumPos(reader, '5',   4, 2);
     AssertReadLineNumPos(reader, '6',   4, 3);
     AssertReadLineNumPos(reader, '\n',  4, 4);
     AssertReadLineNumPos(reader, '\0',  4, 4);
 }
Exemplo n.º 27
0
 public void InitialCounters()
 {
     BufferedCharReader reader = new BufferedCharReader(TextReader.Null);
     AssertCounters(reader, 0, 0, 0);
 }
Exemplo n.º 28
0
 private static void AssertReadLineNumPos(BufferedCharReader reader, char expected, int line, int pos)
 {
     Assert.AreEqual(expected, reader.Next(), "Read");
     AssertLineCounters(reader, line, pos);
 }
Exemplo n.º 29
0
 private static void AssertCounters(BufferedCharReader reader, int chars, int line, int pos)
 {
     Assert.AreEqual(chars, reader.CharCount, "CharCount");
     AssertLineCounters(reader, line, pos);
 }
Exemplo n.º 30
0
        internal static StringBuilder Dequote(BufferedCharReader input, char quote, StringBuilder output)
        {
            Debug.Assert(input != null);
            if (output == null)
            {
                output = new StringBuilder();
            }
            char[] array = null;
            while (true)
            {
                char c = input.Next();
                switch (c)
                {
                case '\0':
                    throw new FormatException("Unterminated string.");

                case '\\':
                    c = input.Next();
                    switch (c)
                    {
                    case 'b':
                        output.Append('\b');
                        break;

                    case 't':
                        output.Append('\t');
                        break;

                    case 'n':
                        output.Append('\n');
                        break;

                    case 'f':
                        output.Append('\f');
                        break;

                    case 'r':
                        output.Append('\r');
                        break;

                    case 'u':
                        if (array == null)
                        {
                            array = new char[4];
                        }
                        output.Append(ParseHex(input, array));
                        break;

                    default:
                        output.Append(c);
                        break;
                    }
                    break;

                default:
                    if (c == quote)
                    {
                        return(output);
                    }
                    output.Append(c);
                    break;
                }
            }
        }
Exemplo n.º 31
0
 /// <summary>
 /// Reads the next token and returns it.
 /// </summary>
 protected override JsonToken ReadTokenImpl()
 {
     if (_stack == null)
     {
         return JsonToken.EOF();
     }
     else if (_stack.Count == 0)
     {
         _stack = null;
         _endLineNumber = _reader.LineNumber;
         _endLinePosition = _reader.LinePosition;
         _endCharCount = _reader.CharCount;
         _reader = null;
         return JsonToken.EOF();
     }
     else
     {
         return Pop()();
     }
 }
Exemplo n.º 32
0
 public void NextAfterBackRestoresCounters()
 {
     BufferedCharReader reader = new BufferedCharReader(new StringReader("a"));
     reader.Next();
     reader.Back();
     reader.Next();
     AssertCounters(reader, 1, 1, 1);
 }
Exemplo n.º 33
0
 private static void AssertCounters(BufferedCharReader reader, int chars, int line, int pos)
 {
     Assert.AreEqual(chars, reader.CharCount, "CharCount");
     AssertLineCounters(reader, line, pos);
 }
Exemplo n.º 34
0
 /// <summary>
 /// Return the characters up to the next close quote character.
 /// Backslash processing is done. The formal JSON format does not
 /// allow strings in single quotes, but an implementation is allowed to
 /// accept them.
 /// </summary>
 /// <param name="quote">The quoting character, either " or '</param>
 /// <returns>A String.</returns>
 
 // TODO: Consider rendering Dequote public
 
 internal static string Dequote(BufferedCharReader input, char quote)
 {
     return Dequote(input, quote, null).ToString();
 }
Exemplo n.º 35
0
 private static void AssertLineCounters(BufferedCharReader reader, int line, int pos)
 {
     Assert.AreEqual(line, reader.LineNumber, "LineNumber");
     Assert.AreEqual(pos, reader.LinePosition, "LinePosition");
 }
Exemplo n.º 36
0
        /// <summary>
        /// Eats the next four characters, assuming hex digits, and converts
        /// into the represented character value.
        /// </summary>
        /// <returns>The parsed character.</returns>

        private static char ParseHex(BufferedCharReader input, char[] hexDigits) 
        {
            Debug.Assert(input != null);
            Debug.Assert(hexDigits != null);
            Debug.Assert(hexDigits.Length == 4);
            
            hexDigits[0] = input.Next();
            hexDigits[1] = input.Next();
            hexDigits[2] = input.Next();
            hexDigits[3] = input.Next();
            
            return (char) ushort.Parse(new string(hexDigits), NumberStyles.HexNumber);
        }
Exemplo n.º 37
0
        public void InitialCounters()
        {
            BufferedCharReader reader = new BufferedCharReader(TextReader.Null);

            AssertCounters(reader, 0, 0, 0);
        }