Exemplo n.º 1
0
        /// <summary>
        /// Creates a new scan token.
        /// </summary>
        /// <param name="type">Type of characters found.</param>
        /// <param name="text">Characters found.</param>
        /// <param name="start">Position of the first character found.</param>
        /// <exception cref="ArgumentException">text was empty or null; type was <see cref="Mark"/> but text was more than one character; type was not understood.</exception>
        public ScanToken(ScanTokenType type, string text, ScanPosition start)
        {
            Text  = NotNullOrEmpty(text, nameof(text));
            Type  = type;
            Start = start;
            switch (type)
            {
            case ScanTokenType.Mark:
                if (text.Length > 1)
                {
                    throw new ArgumentException("Mark tokens must be a single character", nameof(text));
                }
                End = start;
                break;

            case ScanTokenType.Word:
            case ScanTokenType.Space:
                End = start + (uint)text.Length;
                break;

            case ScanTokenType.Newline:
                End = new ScanPosition(start.Absolute + (uint)text.Length, start.Row + 1, 0);
                break;

            default:
                throw new ArgumentException($"{type} is not a supported scan token type", nameof(type));
            }
        }
Exemplo n.º 2
0
        public void New_StoresValues()
        {
            var subject = new ScanPosition(1, 2, 3);

            using (new AssertionScope())
            {
                subject.Absolute.Should().Be(1);
                subject.Row.Should().Be(2);
                subject.Column.Should().Be(3);
            }
        }
Exemplo n.º 3
0
        public void Plus_WithNonZero_ReturnsCorrectPosition()
        {
            var subject = new ScanPosition(100, 20, 30);

            (subject + 25).Should().Be(new ScanPosition(125, 20, 55));
        }
Exemplo n.º 4
0
        public void Plus_With0_ReturnsSamePosition()
        {
            var subject = new ScanPosition(100, 20, 30);

            (subject + 0).Should().Be(subject);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Creates a new mark token.
 /// </summary>
 /// <param name="mark">Mark character found.</param>
 /// <param name="position">Position of the mark.</param>
 /// <returns>The new token.</returns>
 public static ScanToken Mark(char mark, ScanPosition position) =>
 new ScanToken(ScanTokenType.Mark, mark.ToString(), position);
Exemplo n.º 6
0
 /// <summary>
 /// Creates a new newline token.
 /// </summary>
 /// <param name="newline">Newline character(s) found.</param>
 /// <param name="position">Position of the newline.</param>
 /// <returns>The new token.</returns>
 public static ScanToken Newline(string newline, ScanPosition position) =>
 new ScanToken(ScanTokenType.Newline, newline, position);
Exemplo n.º 7
0
 /// <summary>
 /// Creates a new space token.
 /// </summary>
 /// <param name="space">Space characters found.</param>
 /// <param name="position">Position of the space(s).</param>
 /// <returns>The new token.</returns>
 public static ScanToken Space(string space, ScanPosition position) =>
 new ScanToken(ScanTokenType.Space, space, position);
Exemplo n.º 8
0
 /// <summary>
 /// Creates a new word token.
 /// </summary>
 /// <param name="word">Word characters found.</param>
 /// <param name="position">Position of the word.</param>
 /// <returns>The new token.</returns>
 public static ScanToken Word(string word, ScanPosition position) =>
 new ScanToken(ScanTokenType.Word, word, position);