I'm self-submitting srgn, which is most easily thought of as a mix of tr, sed, rip-grep and tree-sitter:
- like
trandsed, 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-madetree-sitterqueries.
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).