Click here to Skip to main content
15,894,330 members
Articles / Programming Languages / C#

Expression Control for TFS Work Items

Rate me:
Please Sign up or sign in to vote.
4.55/5 (8 votes)
5 Oct 2010CPOL16 min read 66.2K   2.1K   17  
Custom control for TFS Work Items that shows the result of calculating an expression based on the work item fields contents
using System;
using System.Collections;
using System.Text;
using Antlr.Runtime.Tree;
using Evaluant.Calculator.Domain;
using Antlr.Runtime;

namespace Evaluant.Calculator
{
    public class Expression
    {
        protected string expression;

        public Expression(string expression)
        {
            if (expression == null || expression == String.Empty)
                throw new 
                    ArgumentException("Expression can't be empty", "expression");

            this.expression = expression;
        }

        protected CommonTree Parse(string expression)
        {
            ECalcLexer lexer = new ECalcLexer(new ANTLRStringStream(expression));
            ECalcParser parser = new ECalcParser(new CommonTokenStream(lexer));

            try
            {
                RuleReturnScope rule = parser.expression();
                if (parser.HasError)
                {
                    throw new EvaluationException(parser.ErrorMessage + " " + parser.ErrorPosition);
                }

                return rule.Tree as CommonTree;
            }
            catch (EvaluationException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new EvaluationException(e.Message, e);
            }
        }

        public object Evaluate()
        {
            EvaluationVisitor visitor = new EvaluationVisitor();
            visitor.EvaluateFunction += EvaluateFunction;
            visitor.EvaluateParameter += EvaluateParameter;
            visitor.Parameters = parameters;

            LogicalExpression.Create(Parse(expression)).Accept(visitor);
            return visitor.Result;
        }

        public event EvaluateFunctionHandler EvaluateFunction;
        public event EvaluateParameterHandler EvaluateParameter;

        private Hashtable parameters = new Hashtable();

        public Hashtable Parameters
        {
            get { return parameters; }
            set { parameters = value; }
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer SunHotels
Spain Spain
Under the secret identity of a C# programmer, a freaky guy who loves MSX computers and japanese culture is hidden. Beware!

Comments and Discussions