Exemplo n.º 1
0
        public void Load(string data)
        {
            var tokenizer = new KeyValuesTokenizer(data);
            var token     = tokenizer.NextToken();

            if (token == null || token.Item1 != TokenType.String)
            {
                throw new ParseException("Invalid token at " + tokenizer.Location());
            }

            var key = token.Item2;

            token = tokenizer.NextToken();
            if (token == null || token.Item1 != TokenType.BlockBegin)
            {
                throw new ParseException($"Invalid token: {token.Item1}, {token.Item2} at {tokenizer.Location()}");
            }

            var kv = new KeyValues();

            this[key] = kv;
            kv.Parse(tokenizer);

            token = tokenizer.NextToken();
            if (token != null)
            {
                throw new ParseException("Unexpected token at file end");
            }
        }
Exemplo n.º 2
0
        void Parse(KeyValuesTokenizer tokenizer)
        {
            string key = null;

            while (true)
            {
                var token = tokenizer.NextToken();
                if (token == null)
                {
                    throw new ParseException("Unexpected end of file");
                }

                if (key != null)
                {
                    if (token.Item1 == TokenType.BlockBegin)
                    {
                        var value = new KeyValues();
                        value.Parse(tokenizer);
                        this[key] = value;
                    }
                    else if (token.Item1 == TokenType.String)
                    {
                        this[key] = token.Item2;
                    }
                    else
                    {
                        throw new ParseException("Invalid token at " + tokenizer.Location());
                    }
                    key = null;
                }
                else
                {
                    if (token.Item1 == TokenType.BlockEnd)
                    {
                        break;
                    }
                    if (token.Item1 != TokenType.String)
                    {
                        throw new ParseException("Invalid token at " + tokenizer.Location());
                    }
                    key = token.Item2;
                }
            }
        }