BaseTranslationRule'2

Class for Extensibility Translation Rules with two generic arguments. Forces the 'IsApplicable' and 'Replace' methods to be implemented.

Class Definition

public abstract class BaseExtensibleTranslationRule<TInputAst, TOutputAst> :
        BaseExtensibleTranslationRule<TInputAst, TOutputAst, ReplaceContext>, 
        ISnowConvertExtensibleRule
        where TInputAst : class, IAst
        where TOutputAst : class, IAst

This class allows the creation of additional translation rule classes using two generic arguments:

  1. TInputAst, the input AST type, i.e, the type of AST that this translation rule will work with.

  2. TOutputAst, the output (result) AST type, i.e, the type of AST that this translation rule will return. Most likely, it must be the same type as TInputAST.

With this BaseExtensibleTranslationRule overload, the user can create simple additional translation rule classes that need no more than the input AST for creating the output AST. If more information is needed to do this, the BaseTranslationRule'3 class is available.

Class Members to Override

IsApplicable

/// <summary>
/// Returns if the current translation rule must apply to the given node. If this method 
/// returns true, the Replace method will be called with the given node. 
/// </summary>
/// <param name="inputNode">The node to test if the current translation rule applies.</param>
/// <returns>True, if the current translation rule applies to the given node, false otherwise.</returns>
protected abstract bool IsApplicable(TInputAst inputNode);

The IsApplicable method filters out the instances of the TInputAst type that should be excluded from this translation rule class. Sometimes it is necessary to do this because the translation only applies to a subset of all instances of the TInputAst type; such criteria must be specified in the IsApplicable method. If the translation rule needs to apply to all instances, the user can just return true.

Replace

/// <summary>
/// Main method of the translation rule, this is where the resulting node must be generated. 
/// The method must assign 'resultNode' because this is the resulting node of the translation rule. 
/// </summary>
/// <param name="inputNode">The input node.</param>
/// <param name="outputNode">The resulting node of the translation rule.</param>
protected abstract void Replace(TInputAst inputNode, out TOutputAst outputNode);

The Replace method is the one in charge of defining how the given nodes must be transformed into Snowflake equivalent code. Notice the outputNode parameter is an out parameter that must be assigned before the end of the method. This argument represents the code in Snowflake that should be functionally equivalent to the inputNode AST. Here are some common ways to assign this parameter:

  • If the input node is already fully supported in Snowflake, the user can you assign outputNode = inputNode, to mark the node as supported.

  • If the input node should be removed from the output code, maybe because it is not applicable in Snowflake due to architectural reasons, the user can assign outputNode = null, to remove the element.

  • If the input node needs to be transformed somehow to make it compatible with Snowflake, then a new node must be emitted with such modifications. As mentioned before, by design choice, the ASTs are immutable, so there is no way to modify an existing node, so a new one should be created by using Emitters. You can read more about it here.

Last updated