AddStatement now passes runtime context for custom handlers

This commit is contained in:
Stone_Red
2026-03-06 00:04:47 +01:00
parent 491eedf77e
commit 246e05dcd9
5 changed files with 203 additions and 3 deletions
@@ -111,6 +111,20 @@ public class YesNtInterpreter
PreScanLines();
}
/// <summary>
/// Registers a custom statement using a pre-built <see cref="StatementAttribute"/>,
/// with access to the script's <see cref="IStatementContext"/> (variables, line number, output, etc.).
/// </summary>
/// <param name="attribute">The attribute describing the keyword, search mode, and priority.</param>
/// <param name="handler">
/// The delegate invoked when the statement matches. Receives the argument text and the current
/// <see cref="IStatementContext"/> for reading/writing script state.
/// </param>
public void AddStatement(StatementAttribute attribute, Action<string, IStatementContext> handler)
{
AddStatement(attribute, args => handler(args, runtimeInfo));
}
/// <summary>
/// Registers a simple custom statement with default settings.
/// </summary>
@@ -123,6 +137,22 @@ public class YesNtInterpreter
AddStatement(new StatementAttribute(name, searchMode, spaceAround), handler);
}
/// <summary>
/// Registers a simple custom statement with default settings,
/// with access to the script's <see cref="IStatementContext"/> (variables, line number, output, etc.).
/// </summary>
/// <param name="name">The keyword to match.</param>
/// <param name="searchMode">Where in the line the keyword is searched for.</param>
/// <param name="spaceAround">Which sides of the keyword must be padded with a space.</param>
/// <param name="handler">
/// The delegate invoked when the statement matches. Receives the argument text and the current
/// <see cref="IStatementContext"/> for reading/writing script state.
/// </param>
public void AddStatement(string name, SearchMode searchMode, SpaceAround spaceAround, Action<string, IStatementContext> handler)
{
AddStatement(new StatementAttribute(name, searchMode, spaceAround), handler);
}
/// <summary>
/// Registers a simple custom statement with a specific syntax-highlight color.
/// </summary>
@@ -136,6 +166,23 @@ public class YesNtInterpreter
AddStatement(new StatementAttribute(name, searchMode, spaceAround, consoleColor), handler);
}
/// <summary>
/// Registers a simple custom statement with a specific syntax-highlight color,
/// with access to the script's <see cref="IStatementContext"/> (variables, line number, output, etc.).
/// </summary>
/// <param name="name">The keyword to match.</param>
/// <param name="searchMode">Where in the line the keyword is searched for.</param>
/// <param name="spaceAround">Which sides of the keyword must be padded with a space.</param>
/// <param name="consoleColor">The color used for syntax highlighting.</param>
/// <param name="handler">
/// The delegate invoked when the statement matches. Receives the argument text and the current
/// <see cref="IStatementContext"/> for reading/writing script state.
/// </param>
public void AddStatement(string name, SearchMode searchMode, SpaceAround spaceAround, ConsoleColor consoleColor, Action<string, IStatementContext> handler)
{
AddStatement(new StatementAttribute(name, searchMode, spaceAround, consoleColor), handler);
}
/// <summary>
/// Unregisters all handlers matching the specified keyword <paramref name="name"/>.
/// </summary>