Ejemplo n.º 1
0
 public ParseException(Token token, string error)
     : this(token?.File ?? string.Empty, token?.Line ?? -1, token?.Column ?? -1, error)
 {
 }
Ejemplo n.º 2
0
        private void MoveToNextToken()
        {
            // If any tokens were pushed back to the lexer, move trough these first.
            if (_pushedTokens.Count > 0)
            {
                Current = _pushedTokens.Pop();
                return;
            }

            // Find the first matching token type.
            foreach (var tokenData in _tokenTypes)
            {
                var match = tokenData.Pattern.Match(_input, _caretPosition);

                if (match.Success)
                {
                    Current = new Token(tokenData.Type, match.Groups[tokenData.ContentGroup].Value, _file, _line,
                        _column);

                    MoveCaret(match.Groups[0].Length);
                    SkipWhitespace();
                    return;
                }
            }

            // If no token type was found, the character at the caret is a token.
            var token = _input.Substring(_caretPosition, 1);
            Current = new Token(TokenType.Token, token, _file, _line, _column);
            MoveCaret(1);
            SkipWhitespace();
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Pushes the specified token.
        /// </summary>
        /// <param name="token">The token.</param>
        public void Push(Token token)
        {
            if (token == null) throw new ArgumentNullException(nameof(token));

            // Push the current token to the stack and set the current token to the pushed token.
            if (Current != null)
                _pushedTokens.Push(Current);

            Current = token;
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Sets the enumerator to its initial position, which is before the first element in the collection.
        /// </summary>
        public void Reset()
        {
            _column = 1;
            _line = 1;
            _caretPosition = 0;

            _pushedTokens.Clear();
            Current = null;

            SkipWhitespace();
        }
Ejemplo n.º 5
0
        /// <summary>
        ///     Advances the enumerator to the next element of the collection.
        /// </summary>
        /// <returns>
        ///     true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of
        ///     the collection.
        /// </returns>
        public bool MoveNext()
        {
            if (_caretPosition >= _input.Length && _pushedTokens.Count == 0)
            {
                Current = null;
                return false;
            }

            MoveToNextToken();
            return true;
        }