Compartilhar via


Sample Implementation of Expression Evaluation

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Important

In Visual Studio 2015, this way of implementing expression evaluators is deprecated. For information about implementing CLR expression evaluators, please see CLR Expression Evaluators and Managed Expression Evaluator Sample.

For a Watch window expression, Visual Studio calls ParseText to produce an IDebugExpression2 object. IDebugExpressionContext2::ParseText instantiates an expression evaluator (EE) and calls Parse to obtain an IDebugParsedExpression object.

This implementation of IDebugExpressionEvaluator::Parse performs the following tasks:

  1. [C++ only] Parses the expression to look for errors.

  2. Instantiates a class (called CParsedExpression in this example) that implements the IDebugParsedExpression interface and stores in the class the expression to be parsed.

  3. Returns the IDebugParsedExpression interface from the CParsedExpression object.

Note

In the examples that follow and in the MyCEE sample, the expression evaluator does not separate the parsing from the evaluation.

Managed Code

This is an implementation of IDebugExpressionEvaluator::Parse in managed code. Note that this version of the method defers the parsing to EvaluateSync as the code for parsing also evaluates at the same time (see Evaluating a Watch Expression).

namespace EEMC  
{  
    public class CParsedExpression : IDebugParsedExpression  
    {  
        public HRESULT Parse(  
                string                 expression,   
                uint                   parseFlags,  
                uint                   radix,  
            out string                 errorMessage,   
            out uint                   errorPosition,   
            out IDebugParsedExpression parsedExpression)  
        {   
            errorMessage = "";  
            errorPosition = 0;  
  
            parsedExpression =  
                new CParsedExpression(parseFlags, radix, expression);  
            return COM.S_OK;  
        }  
    }  
}  

Unmanaged Code

This is an implementation of IDebugExpressionEvaluator::Parse in unmanaged code. This method calls a helper function, Parse, to parse the expression and check for errors but this method ignores the resulting value. The formal evaluation is deferred to EvaluateSync where the expression is parsed while it is evaluated (see Evaluating a Watch Expression).

STDMETHODIMP CExpressionEvaluator::Parse(  
        in    LPCOLESTR                 pszExpression,  
        in    PARSEFLAGS                flags,  
        in    UINT                      radix,  
        out   BSTR                     *pbstrErrorMessages,  
        inout UINT                     *perrorCount,  
        out   IDebugParsedExpression  **ppparsedExpression  
    )  
{  
    if (pbstrErrorMessages == NULL)  
        return E_INVALIDARG;  
    else  
        *pbstrErrormessages = 0;  
  
    if (pparsedExpression == NULL)  
        return E_INVALIDARG;  
    else  
        *pparsedExpression = 0;  
  
    if (perrorCount == NULL)  
        return E_INVALIDARG;  
  
    HRESULT hr;  
    // Look for errors in the expression but ignore results  
    hr = ::Parse( pszExpression, pbstrErrorMessages );  
    if (hr != S_OK)  
        return hr;  
  
    CParsedExpression* pparsedExpr = new CParsedExpression( radix, flags, pszExpression );  
    if (!pparsedExpr)  
        return E_OUTOFMEMORY;  
  
    hr = pparsedExpr->QueryInterface( IID_IDebugParsedExpression,  
                                      reinterpret_cast<void**>(ppparsedExpression) );  
    pparsedExpr->Release();  
  
    return hr;  
}  

See Also

Evaluating a Watch Window Expression
Evaluating a Watch Expression