示例#1
0
        public Element(Token keyToken, Parser parser)
        {
            this.keyToken = keyToken;

            Token n;
            do
            {
                n = parser.AdvanceToNextToken();
                if (n == null)
                {
                    throw (new Exception("unexpected end of file, expected closing bracket"));
                }
                if (n.Type == TokenType.Data)
                {
                    tokens.Add(n);
                    n = parser.AdvanceToNextToken();
                    if (n == null)
                    {
                        throw (new Exception("unexpected end of file, expected bracket, comma or key"));
                    }
                    TokenType ty = n.Type;
                    if (ty != TokenType.OpenBracket && ty != TokenType.CloseBracket && ty != TokenType.Comma && ty != TokenType.Key)
                    {
                        throw (new Exception("unexpected token; expected bracket, comma or key"));
                    }
                }
                if (n.Type == TokenType.OpenBracket)
                {
                    compound = new Scope(parser);

                    // current token should be a TOK_CLOSE_BRACKET
                    n = parser.CurrentToken;
                    Debug.Assert(n != null);

                    if (n.Type != TokenType.CloseBracket)
                    {
                        throw (new Exception("expected closing bracket"));
                    }
                    parser.AdvanceToNextToken();
                    return;
                }
            }
            while (n.Type != TokenType.Key && n.Type != TokenType.CloseBracket);
        }
示例#2
0
        public Scope(Parser parser, bool topLevel = false)
        {
            if (!topLevel)
            {
                Token t = parser.CurrentToken;
                if (t.Type != TokenType.OpenBracket)
                {
                    throw (new Exception("expected open bracket"));
                }
            }

            Token n = parser.AdvanceToNextToken();
            if (n == null)
            {
                throw( new Exception("unexpected end of file"));
            }
            while ( n.Type != TokenType.CloseBracket)
            {
                if (n.Type != TokenType.Key)
                {
                    throw (new Exception("unexpected token, expected TOK_KEY"));
                }

                string str = n.StringContents;
                if (!elements.ContainsKey(str))
                {
                    elements.Add(str, new List<Element>());
                }
                elements[str].Add(new Element(n, parser));

                n = parser.CurrentToken;
                if (n == null)
                {
                    if (topLevel)
                    {
                        return;
                    }
                    throw (new Exception("unexpected end of file"));
                }
            }
        }