Пример #1
0
        public KVToken ReadNextToken()
        {
            Require.NotDisposed(nameof(KVTokenReader), disposed);
            SwallowWhitespace();

            var nextChar = Peek();

            if (IsEndOfFile(nextChar))
            {
                return(new KVToken(KVTokenType.EndOfFile));
            }

            switch (nextChar)
            {
            case ObjectStart:
                return(ReadObjectStart());

            case ObjectEnd:
                return(ReadObjectEnd());

            case CommentBegin:
                return(ReadComment());

            case ConditionBegin:
                return(ReadCondition());

            case InclusionMark:
                return(ReadInclusion());

            case QuotationMark:
            default:
                return(ReadString());
            }
        }
Пример #2
0
        public KVObject ReadObject()
        {
            Require.NotDisposed(nameof(KVTextReader), disposed);

            var @object = default(KVObject);

            while (stateMachine.IsInObject)
            {
                KVToken token;

                try
                {
                    token = tokenReader.ReadNextToken();
                }
                catch (InvalidDataException ex)
                {
                    throw new KeyValueException(ex.Message, ex);
                }
                catch (EndOfStreamException ex)
                {
                    throw new KeyValueException("Found end of file while trying to read token.", ex);
                }

                switch (token.TokenType)
                {
                case KVTokenType.String:
                    ReadText(token.Value);
                    break;

                case KVTokenType.ObjectStart:
                    BeginNewObject();
                    break;

                case KVTokenType.ObjectEnd:
                    FinalizeCurrentObject();
                    break;

                case KVTokenType.Condition:
                    HandleCondition(token.Value);
                    break;

                case KVTokenType.EndOfFile:
                    try
                    {
                        @object = FinalizeDocument();
                    }
                    catch (InvalidOperationException ex)
                    {
                        throw new KeyValueException("Found end of file when another token type was expected.", ex);
                    }

                    break;

                case KVTokenType.Comment:
                    break;

                case KVTokenType.IncludeAndMerge:
                    HandleIncludeAndMerge(token.Value);
                    break;

                case KVTokenType.IncludeAndAppend:
                    HandleIncludeAndAppend(token.Value);
                    break;

                default:
                    throw new NotImplementedException("The developer forgot to handle a KVTokenType.");
                }
            }

            if (@object == null)
            {
                throw new InvalidOperationException(); // Should be unreachable.
            }

            return(@object);
        }