/comby

A tool for changing code across many languages.

Primary LanguageOCamlApache License 2.0Apache-2.0

comby

Apache-2.0 Build Status Coverage Status Downloads Commit Gitter

A short example below shows how comby simplifies matching and rewriting compared to regex approaches like sed.

Install

Binary (Mac OS X and Ubuntu Linux) bash <(curl -sL get.comby.dev)

  • Arch and other Linux: The PCRE library is dynamically linked in the Ubuntu binary. For other distributions, like Arch, a fixup is needed: ln -s /usr/lib/libpcre.so /usr/lib/libpcre.so.3. Alternatively, consider building from source.

  • Mac OS X: If you run into dyld: Library not loaded: /usr/local/opt/pcre/lib/libpcre.1.dylib, try run this fixup:

click to expand
install_name_tool -change /usr/local/opt/pcre/lib/libpcre.1.dylib /usr/local/brew/lib/libpcre.1.dylib /usr/local/bin/comby`

With docker: docker pull comby/comby

Running with docker on stdin:

echo '(👋 hi)' | docker run -a stdin -a stdout -i comby/comby '(:[emoji] hi)' 'bye :[emoji]' lisp -stdin

Need help writing patterns or have other problems? Consider posting in Gitter.

Aren't regex approaches like sed good enough?

Sometimes, yes. But often, small changes and refactorings are complicated by nested expressions, comments, or strings. Consider the following C-like snippet. Say the challenge is to rewrite the two if conditions to the value 1. Can you write a regular expression that matches the contents of the two if condition expressions, and only those two? Feel free to share your pattern with @rvtond on Twitter.

if (fgets(line, 128, file_pointer) == Null) // 1) if (...) returns 0
      return 0;
...
if (scanf("%d) %d", &x, &y) == 2) // 2) if (scanf("%d) %d", &x, &y) == 2) returns 0
      return 0; 

To match these with comby, all you need to write is if (:[condition]), and specify one flag that this language is C-like. The replacement is if (1). See the live example.

Build from source

  • Install opam

  • Create a new switch if you don't have OCaml installed:

opam init
opam switch create 4.07.0 4.07.0 
  • Install OS dependencies:

    • Linux: sudo apt-get install pkg-config libpcre3-dev

    • Mac: brew install pkg-config pcre

  • Then install the library dependencies:

git clone https://github.com/comby-tools/comby
cd comby && opam install . --deps-only -y
  • Build and test
make
make test
  • If you want to install comby on your PATH, run
make install