Click here to Skip to main content
12,250,546 members (52,051 online)
Click here to Skip to main content

Stats

36.4K views
1.9K downloads
16 bookmarked
Posted

Expression Control for TFS Work Items

, 5 Oct 2010 CPOL
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)

Share

About the Author

konamiman.MSX
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!

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160426.1 | Last Updated 6 Oct 2010
Article Copyright 2010 by konamiman.MSX
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid