apple/pkl · error · VmException

invalidRegexSyntax

invalidRegexSyntax

Error message

invalidRegexSyntax

What it means

`VmUtils.compilePattern` compiles regexes with UNICODE_CHARACTER_CLASS | UNICODE_CASE via java.util.regex.Pattern; a syntactically invalid pattern throws PatternSyntaxException, which is translated into invalidRegexSyntax with the pattern and the underlying regex-engine message.

Source

Thrown at pkl-core/src/main/java/org/pkl/core/runtime/VmUtils.java:915

      throw new VmExceptionBuilder()
          .evalError("cannotInstantiateAbstractClass", parentClass)
          .withOptionalLocation(parentNode)
          .build();
    }

    assert parentClass.isExternal();
    throw new VmExceptionBuilder()
        .evalError("cannotInstantiateExternalClass", parentClass)
        .withOptionalLocation(parentNode)
        .build();
  }

  @TruffleBoundary
  public static Pattern compilePattern(String pattern, Node location) {
    try {
      return Pattern.compile(pattern, Pattern.UNICODE_CHARACTER_CLASS | Pattern.UNICODE_CASE);
    } catch (PatternSyntaxException e) {
      throw new VmExceptionBuilder()
          .withLocation(location)
          .evalError("invalidRegexSyntax", pattern, e.getMessage())
          .build();
    }
  }

  @TruffleBoundary
  public static <K, V> K getKey(Map.Entry<K, V> entry) {
    return entry.getKey();
  }

  @TruffleBoundary
  public static <K, V> V getValue(Map.Entry<K, V> entry) {
    return entry.getValue();
  }

  public static String getDisplayUri(SourceSection section, StackFrameTransformer transformer) {
    var sourceUri = section.getSource().getURI();

View on GitHub (pinned to f3efcbfc9b)

Solutions

  1. Read the included regex-engine message — it names the exact syntax error and index.
  2. Fix escaping: double-escape in string literals (`\\.`) and escape metacharacters like `(`, `[`, `+`, `*`.
  3. Validate the pattern against Java (not PCRE/JS) syntax; remove unsupported constructs.
  4. Test the pattern in a Java-regex-compatible tool before embedding it, and prefer raw literal concatenation over fragile interpolation.

Example fix

// before
val re = RegExp("data/(\\d+")
// after
val re = RegExp("data/(\\d+)")
Defensive patterns

Strategy: validation

Validate before calling

function isValidRegex(p) { try { new RegExp(p); return true; } catch { return false; } }

Type guard

function safeRegExp(p) { try { return new RegExp(p); } catch { return null; } }

Try / catch

try { Pattern.compile(pattern) } catch (e) { /* invalidRegexSyntax: inspect e message for position */ }

Prevention

When it happens

Trigger: Calling regex-using APIs (`RegExp(...)`, `find`, `replaceAll`, matchers in `is` type tests) with a pattern that has unbalanced parentheses/brackets, bad quantifiers, dangling escapes, or unsupported constructs.

Common situations: Hand-written regexes with escaping mistakes (Windows paths, dots, slashes), patterns built by string interpolation that break syntax, porting regexes from other flavors (PCRE/JS lookbehinds, named groups) into Java regex.

Related errors


AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08). Data as JSON: /api/errors/00f0f31df44a3882. Report an issue: GitHub.