Questions regarding IExpressionEvaluator

I would like an explanation of the IExpressionEvaluator interface. I gather it deals with dynamic evaluation of text in a parser, but I’m not sure the context(s) in which this will be called. I am keen to understand both EvaluateExpression() methods as well as the concept of undefined sections. TIA!

Hi Bob,

IExpressionEvaluator is used by advanced C#/Visual Basic parsers to evaluate ifdef sections and highlight them differently (i.e. with greyed color).

It can evaluate constants (defined with #define directive) and most binary and unary expressions.
You can look at NetEvaluator.cs source code for reference.

As an example, C# parser calls , ExpressionEvaluator.AddDefine when it parses #define directive, RemoveDefine when it parses #undef directive, and ExpressionEvaluator.EvaluateExpression when it parses #if directive. In case expression is not evaluated to true, then it skips code till #endif and adds whole part as an undefined section, which is later highlighted differently by the editor.

TextSource has its own property UndefinedSection, which is just gets assigned from the parser, when it finishes its work.

                    if (parser != null && (parser.Options & SyntaxOptions.EvaluateConditionals) != 0 && parser.ExpressionEvaluator != null)
                        UndefinedSections = parser.ExpressionEvaluator.UndefinedSections;
                    else
                        UndefinedSections = null;

Regards,
Dmitry

I don’t seem to have the source code for NetEvaluator.cs, or forgot where it is installed. If source code is a separate installation, I may not have put it on this machine. Where can I get it?

And just so I’m clear: the sole purpose of the IExpressionEvaluator is to determine if a given expression falls within an “undefined section” so the editor can gray it out instead of apply the normal syntax rules to it?

In Scheme there is something like this, the #! literal, which can be implementation defined. For example with Chez scheme, #!r6rs marker in the file disables chez-specific extensions, and #!chezscheme enables them explicitly. Implementations simply ignore #![symbol] markers for a symbol they don’t understand as meaningful. Most recognize #!r6rs, and some define their own (like Chez does). It’s one of those features of Scheme that was intended to make it more portable but had the opposite effect. Nor does it strictly speaking mean everything after a given marker should be blocked out like #ifndef does in C-based languages. So, it’s complicated, and probably a rabbit hole I should avoid until I’ve gotten all the other more important, less nuanced things implemented. LOL.

Hi Bob,

I’ve sent you email with the link to the source code, as it’s not included in the installation and needs to be downloaded separately.

And just so I’m clear: the sole purpose of the IExpressionEvaluator is to determine if a given expression falls within an “undefined section” so the editor can gray it out instead of apply the normal syntax rules to it?

Yes, basically the intent here is to skip parsing the whole block between #if and #endif (or #else - #endif), if the condition does not evaluate to true. Parsers that support expression evaluation do not try to parse anything inside such blocks (one of the reason that such sections could contain invalid syntax, and therefore should not be included in AST). Then editor marks these blocks with uniform color to provide a visual feedback that they have been excluded from parsing. However as lexical analysis is a separate process to syntax analysis, it should be possible to have full syntax highlighting for these blocks if needed.

Kind regards,
Dmitry