Overview
The hints system provides intelligent suggestions when users make typos in their input. It uses the Levenshtein distance algorithm to find the closest valid alternatives and automatically includes them in error messages.Levenshtein Distance
The Levenshtein distance measures the minimum number of single-character edits (insertions, deletions, or substitutions) needed to change one string into another.How It Works
The algorithm uses dynamic programming to compute the edit distance:- Creates a matrix of size
(b.length + 1) × (a.length + 1) - Initializes first row and column with indices
- Fills matrix by comparing characters and taking minimum of:
- Deletion:
matrix[j][i-1] + 1 - Insertion:
matrix[j-1][i] + 1 - Substitution:
matrix[j-1][i-1] + indicator(0 if chars match, 1 if different)
- Deletion:
- Returns bottom-right cell value
Examples
Generating Hints
ThegenerateHints function finds the closest matches from a list of expected values.
Parameters
- found: The string the user actually typed
- expected: Array of valid/expected strings to compare against
- maxDistance: Maximum edit distance to consider (default: 2)
- Distance 1: Very close typos (single char error)
- Distance 2: Moderate typos (two char errors)
- Distance 3+: Distant matches (rarely useful)
- maxHints: Maximum number of hints to return (default: 3)
Return Value
Returns an array of suggested strings, sorted by edit distance (closest first).Examples
Parser Integration
The hints system integrates seamlessly with Parserator’s error handling.keywordWithHints
Creates a parser for a specific keyword with automatic hint generation.- Tries to match the expected keyword exactly
- If no match, extracts what the user typed (identifier pattern)
- Generates hints using
generateHints() - Returns an
Unexpectederror with hints attached
anyKeywordWithHints
Creates a parser that matches any keyword from a list with hint generation.stringWithHints
Creates a parser for string literals with hint generation for common mistakes.- Parses the quoted string
- Checks if content is in
validStrings - If not, generates hints for the content
- Returns hints with quotes preserved
Creating Custom Hint Parsers
You can create your own parsers with hint support:Tuning Hint Quality
Choosing maxDistance
- Use
maxDistance: 1for very strict matching (single typos only) - Use
maxDistance: 2for most cases (default, good balance) - Use
maxDistance: 3for forgiving matching (may give false positives)
Limiting Hint Count
- Use
maxHints: 1when you want a single best suggestion - Use
maxHints: 3for typical cases (default) - Use higher values only when you have many similar valid options
Error Message Display
Hints are automatically displayed by the error formatter:Best Practices
- Keep expected lists focused: Only include truly valid options
- Use reasonable maxDistance: Default of 2 works well for most cases
- Limit hint count: Too many suggestions overwhelm users
- Match the domain: For keywords use identifier patterns, for strings use quoted patterns
- Preserve formatting: When suggesting strings, keep the quotes
- Consider case sensitivity: Normalize case if your language is case-insensitive
Performance Considerations
The Levenshtein distance algorithm hasO(m × n) time complexity where m and n are string lengths:
- Keep expected lists reasonably sized (< 100 items)
- Use shorter strings when possible
- Consider filtering by first character before computing distance
- Cache hints for common typos if performance is critical
Related
- Errors - Core error types
- Error Formatter - Display formatted errors with hints