Example #1
0
        private static GlobToken ReadScope(StringReader reader, GlobTokenKind kind, char first, char last)
        {
            char current = (char)reader.Read();

            Debug.Assert(current == first, "Unexpected token.");

            var accumulator = new StringBuilder();

            while (reader.Peek() != -1)
            {
                current = (char)reader.Peek();
                if (current == last)
                {
                    break;
                }
                accumulator.Append((char)reader.Read());
            }

            if (reader.Peek() == -1)
            {
                throw new InvalidOperationException($"Expected '{last}' but reached end of pattern.");
            }

            // Consume the last character.
            current = (char)reader.Read();
            Debug.Assert(current == last, "Unexpected token.");

            return(new GlobToken(kind, accumulator.ToString()));
        }
Example #2
0
        public GlobToken Scan()
        {
            _currentContent = string.Empty;
            _currentKind    = ScanToken();

            return(new GlobToken(_currentKind, _currentContent));
        }
Example #3
0
        /// <summary>
        /// Loads the tokens into the token queue.
        /// </summary>
        /// <returns>A queue of tokens representing the pattern.</returns>
        private Queue <GlobToken> QueueTokens()
        {
            var tokenQueue = new Queue <GlobToken>();

            while (_remainingPattern.Length > 0)
            {
                GlobTokenKind tokenKind = GetGlobTokenKindAndTrimRemainingPattern();

                if (tokenKind != GlobTokenKind.Identifier)
                {
                    tokenQueue.Enqueue(new GlobToken(tokenKind, string.Empty));
                    continue;
                }

                while (tokenKind == GlobTokenKind.Identifier)
                {
                    if (!string.IsNullOrEmpty(_remainingPattern))
                    {
                        tokenKind = GetGlobTokenKindAndTrimRemainingPattern();
                    }
                    else
                    {
                        break;
                    }
                }

                // We know we've got at least one Identifer, so queue it with the contents read
                tokenQueue.Enqueue(new GlobToken(GlobTokenKind.Identifier, _currentContent));
                _currentContent = string.Empty;

                // If we quit the while loop due to hitting a new token (rather than end of string), queue it.
                if (tokenKind != GlobTokenKind.Identifier)
                {
                    tokenQueue.Enqueue(new GlobToken(tokenKind, string.Empty));
                }
            }

            return(tokenQueue);
        }
Example #4
0
 public void Accept(GlobTokenKind kind)
 {
     Accept(new[] { kind });
 }
Example #5
0
 public GlobToken(GlobTokenKind kind, string value)
 {
     Kind  = kind;
     Value = value;
 }