add-rule
Add lexer or parser rules to ANTLR4 grammars with auto-detection from naming case and options for skip, channel, fragment, return type, and positioning.
Instructions
Add a new lexer or parser rule with automatic type detection and positioning.
Auto-detection: Rule type is determined by naming convention:
UPPERCASE = lexer rule (e.g., ID, STRING, NUMBER)
lowercase = parser rule (e.g., expression, statement, term)
When to use: Add any rule to your grammar - lexer tokens or parser rules.
Example - Add lexer rule (UPPERCASE): rule_name: "ID" pattern: "[a-zA-Z_][a-zA-Z0-9_]*"
Example - Add parser rule (lowercase): rule_name: "expression" definition: "term ((PLUS | MINUS) term)*"
Example - Add lexer with skip: rule_name: "WS" pattern: "[ \t\n\r]+" skip: true
Example - Add lexer with channel: rule_name: "COMMENT" pattern: "//.*?\n" channel: "COMMENTS"
Example - Add fragment: rule_name: "DIGIT" pattern: "[0-9]" fragment: true
Example - Add with positioning: rule_name: "STRING" pattern: "".*?"" insert_after: "ID"
Example - Add parser with return type: rule_name: "intLiteral" definition: "INT" return_type: "int value"
Features:
Auto-detects lexer vs parser from rule name case
Default: Alphabetical sorting within rule type
Optional: Custom positioning with insert_after/insert_before
Lexer-specific: skip, channel, fragment options
Parser-specific: return_type option
Prevents duplicate rule names
Optional file persistence with write_to_file: true
Diff output mode for large grammars
Returns: Modified grammar with new rule inserted, success message, position description, file write confirmation if applicable.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| skip | No | Lexer only: If true, adds "-> skip" directive (common for whitespace) | |
| channel | No | Lexer only: Channel name to route tokens (e.g., "COMMENTS") | |
| pattern | No | For lexer rules: The lexer pattern. Examples: [0-9]+, \".*?\", [a-zA-Z_][a-zA-Z0-9_]* | |
| fragment | No | Lexer only: If true, marks rule as fragment (reusable pattern, not a token) | |
| from_file | No | Optional: path to a grammar file to read. Required if using write_to_file. | |
| rule_name | Yes | Name of the rule. UPPERCASE for lexer rules (ID, STRING), lowercase for parser rules (expression, term) | |
| definition | No | For parser rules: The rule definition. Examples: "ID ASSIGN expr", "term (PLUS term)*" | |
| 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) | |
| return_type | No | Parser only: Return type specification (e.g., "String value", "int result") | |
| insert_after | No | Optional: Insert this rule immediately after the specified rule name. Overrides alphabetical sorting. | |
| insert_before | No | Optional: Insert this rule immediately before the specified rule name. Overrides alphabetical sorting. | |
| write_to_file | No | If true, writes modified grammar back to from_file (requires from_file to be set) | |
| grammar_content | No | The ANTLR4 grammar file content |