compilePatterns function

List<RegExp> compilePatterns(
  1. Iterable<String> patterns,
  2. String label
)

Compiles patterns as regular expressions, up front.

Compiling early means a malformed pattern fails with a message naming it and label, instead of a raw RegExp error thrown mid-scan — and means each pattern is compiled once rather than once per file. Throws a FormatException; callers report it and exit.

Implementation

List<RegExp> compilePatterns(Iterable<String> patterns, String label) {
  final matchers = <RegExp>[];
  for (final pattern in patterns) {
    try {
      matchers.add(RegExp(pattern));
    } on FormatException catch (e) {
      throw FormatException('invalid $label "$pattern": ${e.message}');
    }
  }
  return matchers;
}