Compartir a través de


Sample Implementation of Expression Evaluation

For a Watch window expression, Visual Studio calls IDebugExpressionContext2::ParseText to produce an IDebugExpression2 object. IDebugExpressionContext2::ParseText instantiates an expression evaluator (EE) and calls IDebugExpressionEvaluator::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 IDebugParsedExpression::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 IDebugParsedExpression::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

Concepts

Evaluating a Watch Window Expression

Evaluating a Watch Expression