Crate of the Week

I'm self-submitting srgn, which is most easily thought of as a mix of tr, sed, rip-grep and tree-sitter:

  • like tr and sed, it manipulates text,
  • supporting modern regex,
  • while providing low-barrier access to semantic programming language constructs, thanks to its grammar awareness through tree-sitter. It supports a number of languages with a number of possible queries each. The functionality essentially translates command-line options to pre-made tree-sitter queries.

For example, one can turn the following Python (more languages and other semantic queries [literal strings, comments, import names, ...] are supported):

"""GNU module."""

def GNU_says_moo():
    """The GNU -> say moo -> ✅"""

    GNU = """
      GNU
    """  # the GNU...

    print(GNU + " says moo")  # ...says moo

into

"""GNU 🐂 module."""

def GNU_says_moo():
    """The GNU 🐂 -> say moo -> ✅"""

    GNU = """
      GNU
    """  # the GNU...

    print(GNU + " says moo")  # ...says moo

through an invocation of

cat gnu.py | srgn --python doc-strings 'GNU' 'GNU 🐂'

where the edits are limited to documentation strings only. The diff highlights the effect:

- """GNU module."""
+ """GNU 🐂 module."""

  def GNU_says_moo():
-     """The GNU -> say moo -> ✅"""
+     """The GNU 🐂 -> say moo -> ✅"""

      GNU = """
        GNU
      """  # the GNU...

      print(GNU + " says moo")  # ...says moo

The crate provides both a native library and a CLI (cargo install srgn, a thin wrapper around the library).

5 Likes