rename-rule
Rename an ANTLR4 grammar rule and automatically update all references throughout the grammar, including imported files, to keep the grammar consistent.
Instructions
Safely rename a rule and automatically update ALL references throughout the grammar.
When to use: Refactor rule names for clarity, fix naming conventions, or improve code readability.
Example usage: old_name: "expr" new_name: "expression" write_to_file: true
Multi-file example: from_file: "MyGrammar.g4" base_path: "/path/to/grammars" load_imports: true write_to_file: true
Features:
Uses whole-word matching (prevents "expr" from matching "subexpr")
Updates rule definition and ALL references in other rules
Preserves rule position in grammar
Reports number of references updated
Multi-file support: Set load_imports=true to rename across imported grammars
Recommended workflow:
find-rule-usages with load_imports=true to see full impact
rename-rule with load_imports=true to perform refactoring
validate-grammar to verify correctness
Returns: Modified grammar with rule and all references renamed, update count, file write confirmation if applicable.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| new_name | Yes | New name for the rule (must follow ANTLR4 naming: uppercase for lexer, lowercase for parser) | |
| old_name | Yes | Current name of the rule (case-sensitive) | |
| base_path | No | Optional: base directory for resolving imports. Required for multi-file grammars. | |
| from_file | No | Optional: path to a grammar file to read. Required if using write_to_file or load_imports. | |
| output_mode | No | Output format: "full" returns entire modified grammar, "diff" returns git-style unified diff (default for modification tools), "none" returns no content (useful for write-only operations) | |
| load_imports | No | If true, loads all imported grammar files and renames the rule across all files. Requires from_file to be set. | |
| write_to_file | No | If true, writes modified grammar back to from_file (and all imported files if load_imports is true) | |
| grammar_content | No | The ANTLR4 grammar file content (ignored if from_file and load_imports are set) |