예제 #1
0
        public void TestRanges()
        {
            SeekableStringReader s = new SeekableStringReader("hello");

            Assert.Throws <ParseException>(() => s.Read(-1));
            Assert.AreEqual("hello", s.Read(999));
            Assert.Throws <ParseException>(() => s.Read(1));
            s.Rewind(int.MaxValue);
            Assert.IsTrue(s.HasMore());
            Assert.AreEqual("hello", s.Peek(999));
            Assert.IsTrue(s.HasMore());
        }
예제 #2
0
        public void TestStuff()
        {
            using (SeekableStringReader s = new SeekableStringReader("hello"))
            {
                Assert.AreEqual('h', s.Peek());
                Assert.AreEqual("hel", s.Peek(3));
                Assert.AreEqual('h', s.Read());
                Assert.AreEqual("ell", s.Read(3));
                Assert.AreEqual("o", s.Peek(999));
                Assert.AreEqual("o", s.Read(999));
            }

            using (SeekableStringReader s2 = new SeekableStringReader("    skip.\t\n\rwhitespace.  "))
            {
                s2.SkipWhitespace();
                Assert.AreEqual("skip", s2.ReadUntil('.'));
                s2.SkipWhitespace();
                Assert.AreEqual("whitespace", s2.ReadUntil('.'));
                s2.SkipWhitespace();
                Assert.IsFalse(s2.HasMore());
                Assert.Throws <IndexOutOfRangeException>(() => s2.Peek());
            }
        }
예제 #3
0
        public void TestReadUntil()
        {
            SeekableStringReader s = new SeekableStringReader("hello there");

            s.Read();
            Assert.AreEqual("ello", s.ReadUntil(' '));
            Assert.AreEqual('t', s.Peek());
            Assert.Throws <ParseException>(() => s.ReadUntil('x'));

            Assert.AreEqual("there", s.Rest());
            Assert.Throws <ParseException>(() => s.Rest());

            s.Rewind(int.MaxValue);
            Assert.AreEqual("hell", s.ReadUntil('x', 'y', 'z', ' ', 'o'));
            Assert.Throws <ParseException>(() => s.ReadUntil('x', 'y', '@'));
        }
예제 #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="rest"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        /// <exception cref="">InvalidSystemException</exception>
        public static string Parse(string rest, OutputFormat format)
        {
            SeekableReader reader  = new SeekableStringReader(rest);
            bool           success = System.Parse(reader, out System system);

            if (!success)
            {
                throw new InvalidSystemException("Could not parse as a system");
            }

            if (reader.Peek() != -1)
            {
                throw new InvalidSystemException($"Saw unexpected character: {(char)reader.Read()}");
            }

            return(system.ToString(format));
        }
예제 #5
0
            private VariableNode?ReadVariableNode(SeekableStringReader reader)
            {
                int start = reader.NextPosition;
                int c, len = 0;

                while ((c = reader.Peek()) > -1 && IsValidVariableChar(c, isFirstChar: len == 0))
                {
                    reader.Read();
                    len++;
                }

                if (len == 0)
                {
                    errorsSink.AddError(
                        "There is a variable with an empty or invalid name. The valid characters are 'a-zA-Z0-9_' and the first character cannot be a number.",
                        start);

                    return(null);
                }

                return(new VariableNode(pattern.Substring(start, len)));
            }