Dynamic Macros (${...}) & Late-Binding
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.
1. The Power of Late-Binding
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.
- Zero Reflection: Macros replace the need for slow, unsafe dynamic reflection.
- Real-time Accuracy: If a role is added to your database, it is immediately recognized by the iBNF grammar without a restart.
2. Registering a Dynamic Macro
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();
});
3. Using Macros in iBNF Syntax
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
}
}
4. Multi-word Resolution
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.
Next Steps
Explore our Spring Boot & Quarkus Integration guide to see how to automatically register macros and commands using your favorite IoC container.