test-parser-rule
Test if input text matches an ANTLR4 parser rule, providing match result, parse tree, and errors. Uses native runtime, falls back to simulation if unavailable.
Instructions
Test if input text matches a specific parser rule. Now with native ANTLR4 support!
π Native ANTLR4 Support: Automatically uses native ANTLR4 runtime if available for 100% accurate parsing including:
β Lexer modes (pushMode, popMode)
β Semantic predicates ({...?})
β Actions ({...})
β All complex parser patterns
β Multi-file grammars with imports
Falls back to simulation if ANTLR4 is not installed.
When to use:
Rapid iteration on rule syntax during development
Verify if text matches a parser rule structure
Test rules with complex features (modes, predicates)
Test rules that reference imported tokens/rules
Debug parsing issues in complex grammars
Setup for 100% accuracy (optional):
Install Java: brew install openjdk
Install ANTLR4: wget https://www.antlr.org/download/antlr-4.13.1-complete.jar
Set env: export ANTLR4_JAR=/path/to/antlr-4.13.1-complete.jar
How it works:
Native mode: Compiles and executes actual ANTLR4 parser (100% accurate)
Simulation mode: Best-effort matching (~70-90% accuracy)
Example - Test expression rule: rule_name: "expression" input: "x + y * 2"
Example - Test with multi-file grammar: from_file: "/path/to/MyParser.g4" base_path: "/path/to/grammar/dir" load_imports: true rule_name: "expression" input: "x + y"
Example - Show parse tree: rule_name: "statement" input: "if (x) y = 1;" show_tree: true
Returns:
Match result (β matches or β doesn't match)
Parse tree (if show_tree enabled)
Parse errors with line/column information
Mode indicator (π Native or β οΈ Simulation)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| input | Yes | Input text to test against the rule | |
| base_path | No | Optional: base directory for resolving imports and tokenVocab. Required for multi-file grammars. | |
| from_file | No | Optional: path to a grammar file to read | |
| rule_name | Yes | Name of the parser rule to test (e.g., "expression", "statement") | |
| show_tree | No | Optional: if true, displays the parse tree (native mode only). Default: false. | |
| load_imports | No | Optional: if true, automatically load imported grammars and lexer vocabulary. Default: true. | |
| grammar_content | No | The ANTLR4 grammar file content |