Ejemplo n.º 1
0
        public VdfNode LoadString(string filename, string input)
        {
            this.input  = input;
            currentFile = filename;
            line        = 1;
            col         = 1;

            var rootNode = new VdfNode();

            rootNode.Children = new List <VdfNode>();

            var tokenEnumerator = Tokenise().GetEnumerator();

            while (tokenEnumerator.MoveNext())
            {
                var tok = tokenEnumerator.Current;
                if (tok == null)
                {
                    throw new Exception("Enumerator behaving nonsensically");
                }

                if (tok.Type == TokenType.Unquoted && (tok.Payload == "#include" || tok.Payload == "#base"))
                {
                    Throw("VDF inclusions are unimplemented");
                }
                else
                {
                    rootNode.Children.Add(LoadStringNode(tokenEnumerator));
                }
            }

            return(rootNode);
        }
Ejemplo n.º 2
0
        VdfNode LoadStringNode(IEnumerator <Token> tokenEnumerator)
        {
            var node = new VdfNode();

            if (tokenEnumerator.Current.Type == TokenType.Conditional)
            {
                node.Condition = tokenEnumerator.Current.Payload.TrimStart('[').TrimEnd(']');
                if (!tokenEnumerator.MoveNext())
                {
                    Throw("Unexpected EOF after conditional");
                }
            }

            if (tokenEnumerator.Current.Type != TokenType.Quoted && tokenEnumerator.Current.Type != TokenType.Unquoted)
            {
                Throw("Expected a key");
            }

            node.Name = tokenEnumerator.Current.Payload;

            if (!tokenEnumerator.MoveNext())
            {
                Throw("Unexpected EOF after key");
            }

            if (tokenEnumerator.Current.Type == TokenType.Quoted || tokenEnumerator.Current.Type == TokenType.Unquoted)
            {
                node.Value = tokenEnumerator.Current.Payload;
            }
            else if (tokenEnumerator.Current.Type == TokenType.LeftBrace)
            {
                while (true)
                {
                    if (!tokenEnumerator.MoveNext())
                    {
                        Throw("Unexpected EOF inside section");
                    }
                    else if (tokenEnumerator.Current.Type == TokenType.RightBrace)
                    {
                        return(node);
                    }
                    else
                    {
                        node.Children.Add(LoadStringNode(tokenEnumerator));
                    }
                }
            }
            else
            {
                Throw("Unexpected token type");
            }
            return(node);
        }