MyNixOS website logo
Description

grep-like CLI using Parsec parsers instead of regex.

screp is a grep-like command-line tool that uses Parsec parser combinators instead of regular expressions for pattern matching. Write patterns using a familiar Haskell-like DSL with combinators like 'some digit', 'string TODO', or 'manyTill anyChar (string END)'.

screp

A grep-like CLI that uses Parsec parser combinators instead of regex.

Installation

cabal build screp
cabal install screp

Make sure ~/.local/bin is in your PATH:

export PATH="$HOME/.local/bin:$PATH"

Or install from Hackage:

cabal install screp

Usage

screp PATTERN FILE...
screp [OPTIONS] PATTERN FILE...

DSL Primitives

PrimitiveExampleDescription
charchar 'x'Single character
stringstring "abc"Literal string
anyCharanyCharAny character
digitdigit0-9
letterlettera-z, A-Z
alphaNumalphaNumLetter or digit
spacespaceSingle whitespace
spacesspacesZero or more whitespace
newlinenewlineNewline character
oneOfoneOf "abc"Match one of chars
noneOfnoneOf "xyz"Match none of chars

DSL Combinators

CombinatorSyntaxDescription
Sequencep1 >> p2Run p1 then p2, return p2's result
Concatp1 <+> p2Run both, concatenate results
Alternativep1 <|> p2Try p1, else p2
Manymany pZero or more
Somesome pOne or more
Optionaloptional pZero or one
Trytry pBacktracking
Betweenbetween '(' ')' pParse between delimiters
Countcount 3 pExactly n repetitions
ManyTillmanyTill p endNon-greedy: match p until end
Refref "name"Named parser from import file

Examples

# Find digits
screp 'some digit' file.txt

# Find email patterns (inline)
screp 'some alphaNum <+> char '\''@'\'' <+> some alphaNum <+> char '\''.'\'' <+> some letter' contacts.txt

# Find TODO comments
screp 'string "TODO"' -r ./src/

# Search recursively with extension filter
screp -r -e .hs 'string "import"' ./src/

# Count matches
screp -c 'digit' data.txt

# JSON output
screp --json 'some digit' file.txt

# Non-greedy matching: find everything between X and Y
screp 'string "START" <+> manyTill anyChar (string "END")' file.txt

Using Custom Parsers (--import)

For complex patterns, define parsers in a Haskell file and reference them with ref:

Parsers.hs:

module Parsers (parsers) where

import Text.Parsec
import Text.Parsec.String
import Data.Map (Map)
import qualified Data.Map as Map

parsers :: Map String (Parser String)
parsers = Map.fromList
  [ ("email", email)
  , ("phone", phone)
  ]

email :: Parser String
email = do
  user <- many1 alphaNum
  char '@'
  domain <- many1 alphaNum
  char '.'
  tld <- many1 letter
  pure $ user ++ "@" ++ domain ++ "." ++ tld

phone :: Parser String
phone = do
  a <- count 3 digit
  char '-'
  b <- count 3 digit
  char '-'
  c <- count 4 digit
  pure $ a ++ "-" ++ b ++ "-" ++ c

Usage:

# Find emails using custom parser
screp --import Parsers.hs 'ref "email"' contacts.txt

# Find phone numbers
screp --import Parsers.hs 'ref "phone"' contacts.txt

Requirements for custom parsers:

  • Module must export parsers :: Map String (Parser String)
  • Requires parsec and containers packages installed

Options

-r, --recursive      Search directories recursively
-e, --extension EXT  Only search files with extension (e.g., .hs, .txt)
-i, --import FILE    Haskell file with named parsers (for 'ref "name"')
-v, --verbose        Verbose output with full match text
-c, --count          Only print count of matches
-q, --quiet          Quiet mode (exit code only)
-m, --max-results N  Maximum number of results to show
--json               Output results as JSON

Output Format

Default output is grep-like:

file.txt:1:28:123
file.txt:2:5:[email protected]

Format: filepath:line:column:matched_text

Library API

The Scrappy.Grep.* modules expose:

  • Scrappy.Grep.DSL - AST types (ParserExpr, MatchResult)
  • Scrappy.Grep.DSL.Parser - Parse DSL strings to AST
  • Scrappy.Grep.DSL.Interpreter - Convert AST to Parsec parsers
  • Scrappy.Grep.Search - File/directory search functions
  • Scrappy.Grep.Config - External parser execution via runghc
  • Scrappy.Grep.Output - Result formatting.
Metadata

Version

0.3.0.0

Platforms (80)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    uefi
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-uefi
  • aarch64-windows
  • aarch64_be-none
  • arc-linux
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-linux
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • sh4-linux
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-uefi
  • x86_64-windows