In high-demand enterprise environments, static grammars are often not enough. You might need a command syntax that adapts to your application's state—such as listing only the roles currently present in your database or commands available to a specific user.
The Intuitive DSL Engine solves this with Dynamic Macros: a late-binding mechanism that delegates syntax validation to your business logic at the exact moment of parsing.
Unlike traditional parsers that require a full re-compilation to update the grammar, iDSL macros are evaluated at runtime. This ensures your CLI or rule engine is always in sync with your data.
To create a macro, you provide a Function to the engine that returns a list of valid strings. This function has access to the DslRegistry, allowing it to inspect other registered commands if necessary.
// Example: Fetching roles from a repository
engine.registerMacro("db_roles", registry -> {
return roleRepository.findAllNames();
});
Once registered, use the ${macro_name} operator within your @DslCommand annotation. The engine will automatically invoke your provider to validate the user's input.
@DslCommand(
name = "ASSIGN ROLE",
syntax = "ASSIGN ${db_roles} TO username ;"
)
public class AssignRoleCommand implements Runnable {
@Bind("db_roles")
private String selectedRole;
@Bind("username")
private String targetUser;
@Override
public void run() {
// Logic to assign the role
}
}
The iDSL AstNavigator is sophisticated enough to handle multi-word macro results (e.g., "BACKUP DATABASE"). It automatically sorts choices by length to ensure the most specific match is prioritized during the recursive descent parsing.
Pro Tip: Macros are perfect for Semantic Firewalls in AI applications. Use them to restrict Large Language Model (LLM) outputs to a strictly defined set of valid business actions.