Skip to content

Replace PCRE with re, and fix regex hole bugs, performance and CI - #399

Merged
rvantonder merged 5 commits into
masterfrom
replace-pcre-with-re
Nov 6, 2026
Merged

rvantonder merged 5 commits into
masterfrom
replace-pcre-with-re

Conversation

@rvantonder

@rvantonder rvantonder commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Fixes #392. Comby no longer depends on PCRE, so it builds on distributions that dropped libpcre3 and without nixpkgs' ocaml_pcre, which is being removed.

Regex engine

Regex holes now run on ocaml-re, which is pure OCaml. Syntax is parsed by a new Vangstrom parser, lib/kernel/matchers/regex_syntax.ml, that builds a Re.t. I avoided Re.Perl because its classes are Latin-1 (\w+ on café splits the é), it parses [\W] as [\w], and it lacks inline flags, (?P<n>), \h, \v, \R and \cA.

Breaking changes

  • No longer supported: lookaround, backreferences, recursion, possessive quantifiers, atomic groups, \p{..} and (*VERB)s. The engine is an automaton, so these now give an error with a reason.
  • Unknown escapes like \q are now errors.
  • ^ and \A inside a pattern now mean the start of the source. They used to hold at every position, so (^a) on a\na matched both as. A leading ^ is still handled separately and also matches after a newline.
  • \b and \B see the character to their left, so \bo no longer matches inside foo.
  • -fast-offset-conversion is accepted but has no effect, because conversion is now always fast.

Bug fixes

  • Silent failures: a bad regex used to exit 0 with no output. It now prints a reason and exits 1, including for regexes inside rules. A regex that only becomes invalid after a rule substitutes into it is reported on stderr per file.
  • Hole parsing: :[x~b[^a]] and :[x~a\]] ended the hole at the first ], and now work.
  • Escaped dollar: foo\$ was treated as an end anchor.
  • Regex hole performance: every attempt copied the rest of the source, which was quadratic.
  • Match performance: each offset conversion folded over the whole source, costing about 1ms per match on a 90KB file.
180KB input Before After
:[x~\w+] 58.6s 2.4s
:[[x]] 53.6s 2.4s
:[x~nomatchanywhere] 4.3s 0.07s

CI and packaging

  • CI has been failing since the OCaml 5 change. The root Dockerfile now builds on ubuntu-24.04-ocaml-5.3 rather than the old base image.
  • CI also builds the Alpine and Ubuntu release images, which run the tests.
  • Alpine stays on 3.20 with OCaml 5.2: core_unix needs Alpine below 3.21, and opam couldn't solve on the 3.20 OCaml 5.3 image, probably because it's a frozen pre-release.
  • Removed dockerfiles/ubuntu/binary-release-18.04, which still built with OCaml 4.11 and has no OCaml 5 path.
  • Updated the opam files, README, install scripts and third-party licences. re was also missing from comby-kernel.opam.

Testing

  • CI passes on all three jobs. Locally, the full suite passes on macOS.
  • A comparison against the old PCRE build on 76 extra patterns differs only where listed above.
  • -json-lines output, including lines and columns, is byte-identical to the old build on the sampled sources.

Known gaps

  • \b next to non-ASCII text: re hardcodes a Latin-1 word category in category.ml, with no way to override it, so \b\w+\b leaves café alone where PCRE matched caf. A test pins this.
  • Not built by CI: dockerfiles/alpine/binary-release-plus-rg and dockerfiles/alpine/base-dependencies.
  • Formatting: the pinned ocamlformat 0.24.1 wasn't available. The regex files were formatted with 0.26.2 and the rest wasn't run.
  • Codecov: uploads fail with Token required. This predates the PR and doesn't fail the build.

libpcre3 is gone from Debian trixie and ocaml_pcre is being dropped from
nixpkgs, which currently prevents comby from building there (#392).

Regex holes now run on ocaml-re, removing the C dependency. Syntax is parsed
by a new Vangstrom parser (Regex_syntax) into a Re.t rather than Re.Perl,
because Re.Perl uses Latin-1 classes that split UTF-8 (\w+ on "café"), parses
[\W] as [\w], and lacks inline flags, (?P<n>), \h, \v, \R and \cA.

Breaking: lookaround, backreferences, recursion, possessive quantifiers,
atomic groups, \p{..} and (*VERB)s are no longer supported, as the engine is
an automaton. Unknown escapes like \q are now errors.

Invalid regexes used to exit 0 with no output, with the error visible only
under DEBUG_COMBY. They now report a reason and exit 1, or report to stderr
for each file for a regex nested in a rule.

Also fix hole parsing so that a class or escape after other text no longer
ends the hole at its first ]: :[x~b[^a]] and :[x~a\]] now work.
Regex holes now match in the whole source by position, instead of copying
the rest of the source at every attempt. That removes quadratic behavior and
gives \b and \B their left context. ^ and \A now mean the start of the source
rather than holding at every position.

Offset conversion folded over the whole source for each offset, costing about
1ms per match on a 90KB file. Convert with a binary search over line starts
instead. -fast-offset-conversion no longer has an effect.

A bad regex in a rule is now caught at startup and exits 1, and \$ at the end
of a pattern is no longer treated as an anchor.

Build Dockerfiles with OCaml 5.3 rather than from the stale 4.11 base image,
and drop the Ubuntu 18.04 one, which has no OCaml 5 path. Install scripts and
the README no longer ask for PCRE.
The Alpine 3.20 image for OCaml 5.3 is a frozen pre-release that opam cannot
solve for, so Alpine stays on 5.2. Build the release images in CI, since
nothing else would notice them breaking.
@rvantonder
rvantonder force-pushed the replace-pcre-with-re branch from 2da8177 to 52c08e5 Compare November 6, 2026 07:27
@rvantonder
rvantonder merged commit a49be8e into master Nov 6, 2026
3 checks passed
@rvantonder
rvantonder deleted the replace-pcre-with-re branch November 6, 2026 07:32
This was referenced Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cannot be built in debian trixie due to pcre3 being no more

1 participant