add-rule-to-mode
Add a lexer rule to a specific mode in an ANTLR4 grammar, enabling tokens for that lexical context and ensuring correct placement.
Instructions
Add a lexer rule to a specific mode in an ANTLR4 grammar.
When to use: Adding tokens that only apply in specific lexical contexts.
Features:
Places rule in the correct mode section
Validates mode exists
Supports all lexer rule options (skip, channel, fragment)
Auto-sorts within the mode
Example - Add rule to STRING_MODE: rule_name: "INTERPOLATION_START" pattern: "\{" mode_name: "STRING_MODE" write_to_file: true
Example - Add with pushMode action: rule_name: "INTERPOLATION_START" pattern: "\{" mode_name: "STRING_MODE" action: "pushMode(EXPRESSION_MODE)" write_to_file: true
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| skip | No | If true, adds "-> skip" directive | |
| action | No | Lexer action (e.g., "pushMode(MODE)", "popMode", "type(TYPE)") | |
| channel | No | Channel name to route tokens | |
| pattern | Yes | The lexer pattern | |
| fragment | No | If true, marks rule as fragment | |
| from_file | No | Optional: path to a grammar file to read. Required if using write_to_file. | |
| mode_name | Yes | Name of the mode to add the rule to | |
| rule_name | Yes | Name of the lexer rule (UPPER_CASE) | |
| output_mode | No | Output format: "full", "diff", or "none" | |
| write_to_file | No | If true, writes modified grammar back to from_file | |
| grammar_content | No | The ANTLR4 grammar file content |