Exemple #1
0
        internal virtual bool TryReadCharacterLiteral(ILookaroundEnumerator <char> enumerator, [NotNullWhen(true)] out Token?token)
        {
            token = default;
            var escaped = false;

            if (enumerator.Current != '\'')
            {
                return(false);
            }
            enumerator.MoveNext(); //Eat the quote.
            if (enumerator.TryGetNext(out var nextChar) == false)
            {
                throw new LexingException(ExpressionParserStrings.ResourceManager.GetString("LexerError_NewlineInConstant", CultureInfo.InvariantCulture));
            }
            if (enumerator.Current == '\'')
            {
                throw new LexingException(ExpressionParserStrings.ResourceManager.GetString("LexerError_EmptyCharacterLiteral", CultureInfo.InvariantCulture));
            }
            if (enumerator.Current == '\\')
            {
                if (nextChar.IsValidEscapedChar() == false)
                {
                    throw new LexingException(ExpressionParserStrings.ResourceManager.GetString("LexerError_UnrecognizedEscape", CultureInfo.InvariantCulture));
                }
                enumerator.MoveNext(); // Pass the backslash
                escaped = true;
            }
            if (enumerator.TryGetNext(out nextChar) == false)
            {
                throw new LexingException(ExpressionParserStrings.ResourceManager.GetString("LexerError_NewlineInConstant", CultureInfo.InvariantCulture));
            }
            if (nextChar != '\'')
            {
                throw new LexingException(ExpressionParserStrings.ResourceManager.GetString("LexerError_TooManyCharsInCharLiteral", CultureInfo.InvariantCulture));
            }
            token = new Token(TokenType.CharacterLiteral, null, escaped ? enumerator.Current.Escape() : enumerator.Current);
            enumerator.MoveNext(); //Move to quote
            return(true);
        }