示例#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));
            }
        }
示例#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);
            }
        }
示例#3
0
        public void Plus_WithNonZero_ReturnsCorrectPosition()
        {
            var subject = new ScanPosition(100, 20, 30);

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

            (subject + 0).Should().Be(subject);
        }
示例#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);
示例#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);
示例#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);
示例#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);