getWordPattern method

RegExp? getWordPattern(
  1. String? word
)

Returns RegExp pattern for word. Will cache RegExp instances.

Implementation

RegExp? getWordPattern(String? word) {
  if (word == null) return null;
  var regexp = _wordsPatterns[word];
  if (regexp != null) return regexp;

  var pattern = _dialect[word];
  if (pattern == null) return null;

  if (hasErrors) {
    if (_errors.containsKey(word)) {
      throw StateError("Can't use word with compilation error! Word: $word");
    }

    var patternWords = _patternWordPlaceholder
        .allMatches(pattern)
        .map((m) => m.group(2))
        .toList();

    var patternWordsWithError =
        patternWords.where((w) => _errors.containsKey(w)).toList();

    if (patternWordsWithError.isNotEmpty) {
      throw StateError(
          "Can't use word with compilation error! Words: $patternWordsWithError");
    }
  }

  regexp =
      RegExp(pattern, multiLine: multiLine, caseSensitive: caseSensitive);

  _wordsPatterns[word] = regexp;

  return regexp;
}