Comby Documentation

repository·master·Indexed 25 days ago

https://github.com/comby-tools/comby

Comby is a tool for structural code search and replace that understands nested expressions, comments, and strings for complex refactorings. The documentation covers installation across macOS, Linux, Windows, and Docker, building from source using OCaml, and detailed match semantics including weak delimiter matching and alphanumeric detection. It also includes details on the Comby rule language grammar and vendored libraries such as Patdiff (a patience diff implementation) and CamlZip.

Tokens
2K
Snippets
8
Records
17
Agent score
83%

What's inside Comby

  1. Understand alphanumeric delimiter detection

    master

    Comby can match alphanumeric delimiters (like for or def) at the character level. To prevent false positives (e.g., matching the for inside the word before), Comby uses look-ahead and prefix consumption:

    • Prefix: Comby expects whitespace, punctuation delimiters (like )), or the start of a line before an alphanumeric delimiter. The prefix is consumed to advance the state.
    • Suffix: Comby expects whitespace, punctuation (like ; or .), or the end of a line after the delimiter. The suffix is checked as a look-ahead but is not consumed, allowing for consecutive delimiters like begin begin separated by a single space.
  2. Use weak delimiter matching for unique delimiters

    master

    Comby supports weak delimiter matching for unique delimiters (e.g., (), []). In strict matching, delimiters must be balanced. In weak matching, a hole like (:[1]) can match any character except the closing delimiter (e.g., (]) would match). This can provide a performance benefit because Comby does not need to ensure well-balancedness for other delimiter types.

    Note: Weak delimiter matching only works for unique delimiters. It cannot be used for delimiters that share a closing token, such as Ruby's end which closes both class and def.

  3. Use the 'attempt' operator for backtracking in choice sequences

    master

    When using the choice operator <|> in a parse sequence, if the first branch succeeds in its initial steps but fails later, the entire parser fails and the second branch is never tried.

    To enable backtracking—allowing the parser to try the second branch if the first branch fails halfway through—wrap the branches in attempt @@.

    This is critical for:

    1. Disambiguating holes like :[1] and :[[1]].
    2. Handling alphanumeric sequences (like begin or struct) where you need to verify if a sequence initiates a balanced delimiter match.
  4. Build Comby from source

    master

    To build Comby from source, you must have opam (OCaml package manager) installed and an OCaml 5 switch configured.

    1. Setup OCaml environment

    opam init
    opam switch create 5.1.0 5.1.0
    eval $(opam env)

    2. Install OS dependencies

    • Linux: sudo apt install autoconf libpcre3-dev pkg-config zlib1g-dev m4 libgmp-dev libev4 libsqlite3-dev
    • Mac: brew install pkg-config gmp pcre libev

    3. Install library dependencies and build

    git clone https://github.com/comby-tools/comby
    cd comby 
    opam install . --deps-only
    make
    make test

    4. Install to PATH

    make install
  5. Install the CamlZip library

    master

    To install CamlZip, you must first ensure you have the following requirements:

    • Objective Caml 4.02 or higher.
    • The Findlib / ocamlfind library manager.
    • The Zlib C library (version 1.1.3 or higher). Ensure libz.a or libz.so is available on your system.

    Installation steps:

    1. Open the Makefile and edit the three variables at the beginning to match your system's Zlib installation location (defaults are typically sufficient for Linux).
    2. Run make all to build the library.
    3. If the Objective Caml native-code compiler (ocamlopt) is available, run make allopt.
    4. Run make install (using sudo if necessary) to install the library via ocamlfind.
    # Example installation sequence
    make all
    make allopt
    make install
  6. Install Comby on other Linux distributions

    master

    The Ubuntu binary is dynamically linked to the PCRE library. For other distributions, you may need to create a symbolic link for PCRE to work:

    • Arch Linux: sudo ln -s /usr/lib/libpcre.so /usr/lib/libpcre.so.3
    • Fedora: sudo ln -s /usr/lib64/libpcre.so /usr/lib64/libpcre.so.3