/// If one of the current characters (or next non-whitespace character) is not the expected value, then an error is thrown
 public IStringScanner ExpectChar(IEnumerable <char> characters)
 {
     AssertNotFinished();
     CachePos();
     AutoSkipWhitespace();
     if (ExpectCharImpl(characters))
     {
         Match = NextChar.ToString();
         Next(1);
         NewPos();
     }
     else
     {
         ThrowUnexpectedCharacterException();
     }
     return(this);
 }
        /// <summary>
        /// If current character (or next non-whitespace character) is not the expected value, then an error is thrown
        /// </summary>
        /// <param name="character"></param>
        /// <returns></returns>
        ///
        public IStringScanner ExpectChar(char character)
        {
            AssertNotFinished();
            CachePos();
            AutoSkipWhitespace();

            if (NextChar == character)
            {
                Match = NextChar.ToString();
                Next(1);
                NewPos();
            }
            else
            {
                ThrowUnexpectedCharacterException();
            }
            return(this);
        }
Exemple #3
0
        public string Advance()
        {
            var old = Token;

            SkipSpaces();
            if (!HasNext)
            {
                Token = null;
            }
            else
            {
                switch (NextChar)
                {
                case '"':
                    Position++;
                    var  sb = new StringBuilder("\"");
                    bool escaped = false, done = false;
                    while (HasNext)
                    {
                        if (NextChar == '\\')
                        {
                            if (escaped)
                            {
                                sb.Append('\\');
                            }
                            else
                            {
                                escaped = true;
                            }
                        }
                        else if (escaped)
                        {
                            escaped = false;
                            switch (NextChar)
                            {
                            case 'n':
                                sb.Append("\n");
                                break;

                            case 'r':
                                sb.Append("\r");
                                break;

                            case 't':
                                sb.Append("\t");
                                break;

                            case '"':
                                sb.Append("\"");
                                break;

                            default:
                                throw new InvalidCharStringLiteralException();
                            }
                        }
                        else if (NextChar == '"')
                        {
                            Position++;
                            done = true;
                            break;
                        }
                        else
                        {
                            sb.Append(NextChar);
                        }

                        Position++;
                    }
                    Token = sb.ToString();
                    if (!done)
                    {
                        throw new InvalidCharStringLiteralException();
                    }
                    break;

                case '\'':
                    Position++;
                    if (!HasNext)
                    {
                        throw new InvalidCharStringLiteralException();
                    }

                    Token = "'" + NextChar;

                    Position++;
                    if (!HasNext || NextChar != '\'')
                    {
                        throw new InvalidCharStringLiteralException();
                    }
                    Position++;
                    break;

                default:
                    if (StandaloneChars.Contains(NextChar))
                    {
                        Token = NextChar.ToString();
                        Position++;
                    }
                    else if (char.IsLetter(NextChar) || NextChar == '_')
                    {
                        Token = TakeWhile(x => char.IsLetterOrDigit(x) || x == '_');
                    }
                    else if (char.IsNumber(NextChar))
                    {
                        Token = TakeWhile(char.IsNumber);
                    }
                    else if (IsSymbol(NextChar))
                    {
                        Token = TakeWhile(IsSymbol);
                    }
                    else
                    {
                        throw new IllegalCharacterException(NextChar);
                    }
                    break;
                }
            }

            return(old);
        }