BaseTranslationRule'3

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

Class Definition

public abstract class BaseExtensibleTranslationRule<TInputAst, TOutputAst, TContext> :
        ReplaceAstDelegate<TInputAst, TOutputAst, TContext>, ISnowConvertExtensibleRule
        where TInputAst : class, IAst
        where TOutputAst : class, IAst
        where TContext : IReplaceContext, IExtensibleAstDelegateContext

This class allows the creation of additional translation rule classes using three 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.

  3. TContext, a context object with information about the current translation process.

With the BaseExtensibleTranslationRule overload of two arguments (without the context), the user can create simple translation rules that need nothing more than the input AST for creating the output AST. The difference with this overload of three arguments is the TContext object, which contains additional information about the current translation process. Some of the properties the user can use from the context include, but are not limited to:

  • Parent, the parent node of the current input AST. Sometimes, the same AST can be present in two different grammars, and the translation of the element depends on the context where is being used in. Asking the TContext object for the Parent AST property can help in these situations.

  • OriginalNode, the original AST without any changes that have already happened during the translation process. This can be helpful in situations in which a child of the current node has been already translated (because of the bottom-up traverse of the tree) and it is helpful to know the original node as it was recognized by the parser.

Also, there are multiple classes that implement the IReplaceContext interface, for example, there is a context class specific for translation rules from Teradata to Snowflake, there is another one for translating from Oracle to Snowflake, and so on for each of the supported languages. So the TContext object can have other specific properties depending on where it is being used.

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>
/// <param name="context">The context used for storing fields helpful during translation.</param>
/// <returns>True, if the current translation rule applies to the given node, false otherwise.</returns>
protected abstract bool IsApplicable(TInputAst inputNode, TContext context);

This IsApplicable overload has the same description as the one with a single argument, the only difference is the second parameter which represents the context.

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>
/// <param name="context">The context used for storing fields helpful during translation.</param>
protected abstract void Replace(TInputAst inputNode, out TOutputAst outputNode, TContext context);

This Replace overload has the same description as the one with two arguments, the only difference is the third parameter which represents the context.

Last updated