Ejemplo n.º 1
0
        private static TokenType ReadLiteral(CharReader reader)
        {
            while (!reader.IsAtEnd)
            {
                switch (reader.Current)
                {
                case '{':
                    if (reader.Next == '{')
                    {
                        reader.MoveNext();
                        reader.MoveNext();
                        continue;
                    }

                    return(TokenType.CaptureName);

                case '}':
                    if (reader.Next == '}')
                    {
                        reader.MoveNext();
                        reader.MoveNext();
                        continue;
                    }
                    else
                    {
                        throw new FormatException($"At index {reader.Position}, there is an unescaped '}}' character.");
                    }
                }

                reader.MoveNext();
            }

            return(TokenType.Literal);
        }
Ejemplo n.º 2
0
        private static TokenType ReadCaptureName(CharReader reader)
        {
            var startPosition = reader.Position;

            reader.MoveNext();

            while (!reader.IsAtEnd)
            {
                switch (reader.Current)
                {
                case '{':
                    throw new FormatException($"At index {reader.Position}, there is an unexpected '{{' character.");

                case '}':
                    reader.MoveNext();
                    return(TokenType.Literal);
                }

                reader.MoveNext();
            }

            throw new FormatException($"There is a capture section starting at index {startPosition} without a terminating '}}' character.");
        }
Ejemplo n.º 3
0
        private static void TestCursorPosition(string text, CursorPosition position)
        {
            var reader = new CharReader(new StringReader(text));

            for (int i = 0; i < text.Length; i++)
            {
                reader.MoveNext();
            }

            string msg = $"'{text.Replace("\r", "\\r").Replace("\n", "\\n")}': current ";

            Assert.AreEqual(position.Row, reader.CurrentRow, msg + "row");
            Assert.AreEqual(position.Column, reader.CurrentColumn, msg + "column");
            Assert.AreEqual(text.Length, reader.CurrentIndex, msg + "index");
        }