Beispiel #1
0
        private IExpression ParseSimpleTermExpression()
        {
            Token token = this.lexer.NextToken();

            if (token == null)
                return null;

            if (token.TokenType == TokenType.Name && token.Value == "function")
                return ParseFunctionExpression();

            switch (token.TokenType)
            {
                case TokenType.Separator:
                    if (token.Value == "(")
                    {
                        IExpression expression = this.ParseExpression();
                        this.Parse(TokenType.Separator, ")");
                        return expression;
                    }

                    if (token.Value == "{")
                        return this.ParseObjectExpression();

                    break;
                case TokenType.Boolean:
                    bool booleanValue = Convert.ToBoolean(token.Value);
                    return new ConstantExpression(booleanValue);
                case TokenType.Integer:
                    int intValue = Int32.Parse(token.Value, System.Globalization.CultureInfo.InvariantCulture);
                    return new ConstantExpression(intValue);
                case TokenType.Real:
                    double realValue = Double.Parse(token.Value, System.Globalization.CultureInfo.InvariantCulture);
                    return new ConstantExpression(realValue);
                case TokenType.String:
                    return new ConstantExpression(token.Value);
                case TokenType.Object:
                    if (token.Value == "null")
                        return new ConstantExpression(null);

                    if (token.Value == "undefined")
                        return new ConstantExpression(Undefined.Instance);

                    break;
                case TokenType.Name:
                    IExpression expr = null;

                    expr = new VariableExpression(token.Value);

                    if (this.TryParse(TokenType.Separator, "("))
                    {
                        IList<IExpression> arguments = this.ParseArgumentList();
                        expr = new InvokeExpression(expr, arguments);
                    }

                    return expr;
            }

            throw new UnexpectedTokenException(token);
        }
Beispiel #2
0
        private IExpression ParseTermExpression()
        {
            if (this.TryParse(TokenType.Name, "new"))
                return ParseNewExpression();

            IExpression expression = this.ParseSimpleTermExpression();

            while (this.TryParse(TokenType.Operator, ".") || this.TryParse(TokenType.Separator, "[", "("))
            {
                if (this.TryParse(TokenType.Operator, "."))
                {
                    this.lexer.NextToken();
                    string name = this.ParseName();
                    IList<IExpression> arguments = null;

                    if (this.TryParse(TokenType.Separator, "("))
                        arguments = this.ParseArgumentList();

                    expression = new DotExpression(expression, name, arguments);
                    continue;
                }

                if (this.TryParse(TokenType.Separator, "("))
                {
                    IList<IExpression> arguments = this.ParseArgumentList();
                    expression = new InvokeExpression(expression, arguments);
                    continue;
                }

                expression = new ArrayExpression(expression, this.ParseArrayArgumentList());
            }

            return expression;
        }
Beispiel #3
0
        private IExpression ParseSimpleTermExpression()
        {
            Token token = this.lexer.NextToken();

            if (token == null)
                return null;

            if (token.TokenType == TokenType.Name && token.Value == "function")
                return this.ParseFunctionExpression();

            if (token.TokenType == TokenType.Name && token.Value == "typeof")
                return new TypeOfExpression(this.ParseExpression());

            switch (token.TokenType)
            {
                case TokenType.Delimiter:
                    if (token.Value == "(")
                    {
                        IExpression expression = this.ParseExpression();
                        this.Parse(TokenType.Delimiter, ")");
                        return expression;
                    }

                    if (token.Value == "{")
                        return this.ParseObjectExpression();

                    if (token.Value == "[")
                        return this.ParseArrayExpression();

                    break;
                case TokenType.Integer:
                    int intValue = Int32.Parse(token.Value, System.Globalization.CultureInfo.InvariantCulture);
                    return new ConstantExpression(intValue);
                case TokenType.Real:
                    double realValue = Double.Parse(token.Value, System.Globalization.CultureInfo.InvariantCulture);
                    return new ConstantExpression(realValue);
                case TokenType.String:
                    return new ConstantExpression(token.Value);
                case TokenType.Name:
                    if (token.Value == "null")
                        return new ConstantExpression(null);
                    if (token.Value == "undefined")
                        return new ConstantExpression(Undefined.Instance);
                    if (token.Value == "true")
                        return new ConstantExpression(true);
                    if (token.Value == "false")
                        return new ConstantExpression(false);

                    IExpression expr = null;

                    expr = new VariableExpression(token.Value);

                    if (this.TryParse(TokenType.Delimiter, "("))
                    {
                        IList<IExpression> arguments = this.ParseArgumentList();
                        expr = new InvokeExpression(expr, arguments);
                    }

                    return expr;
            }

            throw new ParserException(string.Format("Unexpected '{0}'", token.Value));
        }