Ejemplo n.º 1
0
        public object Evaluate()
        {
            if (expression == null)
            {
                throw new ExpressionEvaluatorException("the expression should not be empty or null");
            }

            object result;

            try
            {
                ExpressionLexer  lexer  = new ExpressionLexer(expression);
                ExpressionParser parser = new ExpressionParser(
                    new TokenStream(lexer),
                    context);

                ExpressionNode    tree    = parser.Parse();
                ExpressionVisitor visitor = new ExpressionVisitor(context);
                IExpression       expr    = ExpressionBuilder.Build(tree);
                expr.Accept(visitor);

                result = visitor.EvaluationResult;
            }
            catch (Exception e) // reports all unexpected exceptions
            {
                context.ReportError(
                    String.Format(
                        CultureInfo.InvariantCulture,
                        "failed to evaluate expression '{0}' : '{1}'",
                        expression,
                        e.Message));
                result = expression;
            }

            return(result);
        }
Ejemplo n.º 2
0
 public TokenStream(ExpressionLexer lexer)
 {
     this.lexer = lexer;
 }
 public TokenStream(ExpressionLexer lexer)
 {
     this.lexer = lexer;
 }
        public object Evaluate()
        {
            if (expression == null)
            {
                throw new ExpressionEvaluatorException("the expression should not be empty or null");
            }

            object result;
            try
            {
                ExpressionLexer lexer = new ExpressionLexer(expression);
                ExpressionParser parser = new ExpressionParser(
                    new TokenStream(lexer),
                    context);

                ExpressionNode tree = parser.Parse();
                ExpressionVisitor visitor = new ExpressionVisitor(context);
                IExpression expr = ExpressionBuilder.Build(tree);
                expr.Accept(visitor);

                result = visitor.EvaluationResult;
            }
            catch (Exception e) // reports all unexpected exceptions
            {
                context.ReportError(
                    String.Format(
                    CultureInfo.InvariantCulture,
                    "failed to evaluate expression '{0}' : '{1}'",
                    expression,
                    e.Message));
                result = expression;
            }

            return result;
        }