Bruce's Parsing Expression Grammar tool.
Go to file
2021-01-14 19:45:15 -08:00
grammars Added rule for hiding 2021-01-14 16:16:41 -08:00
.gitignore Fully purging "bpeg" from the source 2020-12-30 19:42:47 -08:00
bp.1 Simplified ... to ..%\n and $. to ./\n 2021-01-05 00:09:30 -08:00
bp.c Made all the heap garbage collection code optional, which saves some 2021-01-14 19:43:30 -08:00
compiler.c Replaced _exit()s with exits() 2021-01-13 19:01:49 -08:00
compiler.h Major overhaul of some of the memory tracking code, as well as some 2021-01-13 01:48:36 -08:00
file_loader.c Overhaul of memory tracking and left recursion. Added explanation doc 2021-01-14 19:21:31 -08:00
file_loader.h Overhaul of memory tracking and left recursion. Added explanation doc 2021-01-14 19:21:31 -08:00
grammar.c Replaced _exit()s with exits() 2021-01-13 19:01:49 -08:00
grammar.h Working towards zero memory leakage 2021-01-13 18:56:22 -08:00
json.c Added forward declarations for static functions 2021-01-12 22:33:28 -08:00
json.h Standardizing to line-comments instead of block comments 2021-01-12 21:04:43 -08:00
left-recursion.md Wording 2021-01-14 19:45:15 -08:00
LICENSE Fully purging "bpeg" from the source 2020-12-30 19:42:47 -08:00
Makefile Overhaul of memory tracking and left recursion. Added explanation doc 2021-01-14 19:21:31 -08:00
printing.c Added forward declarations for static functions 2021-01-12 22:33:28 -08:00
printing.h Some general cleanup, adding comments, adding more __attribute__s where 2021-01-12 22:22:38 -08:00
README.md Simplified ... to ..%\n and $. to ./\n 2021-01-05 00:09:30 -08:00
types.h Made all the heap garbage collection code optional, which saves some 2021-01-14 19:43:30 -08:00
utils.c Replaced _exit()s with exits() 2021-01-13 19:01:49 -08:00
utils.h Replaced _exit()s with exits() 2021-01-13 19:01:49 -08:00
vm.c Made all the heap garbage collection code optional, which saves some 2021-01-14 19:43:30 -08:00
vm.h Made all the heap garbage collection code optional, which saves some 2021-01-14 19:43:30 -08:00

BP - Bruce's PEG Tool

BP is a parsing expression grammar (PEG) tool for the command line. It's written in pure C with no dependencies.

Usage

bp [flags] <pattern> [<input files>...]

Flags

  • -h --help print the usage and quit
  • -v --verbose print verbose debugging info
  • -i --ignore-case perform a case-insensitive match
  • -I --inplace perform replacements or filtering in-place on files
  • -e --explain print an explanation of the matches
  • -j --json print matches as JSON objects
  • -l --list-files print only filenames containing matches
  • -d --define <name>:<def> define a grammar rule
  • -D --define-string <name>:<def> define a grammar rule (string-pattern)
  • -p --pattern <pat> provide a pattern (equivalent to bp '\(<pat>)')
  • -P --pattern-string <pat> provide a string pattern (equivalent to bp '<pat>', but may be useful if '<pat>' begins with a '-')
  • -r --replace <replacement> replace the input pattern with the given replacement
  • -m --mode <mode> set the behavior mode (defult: find-all)
  • -g --grammar <grammar file> use the specified file as a grammar

See man ./bp.1 for more details.

BP Patterns

BP patterns are a mixture of Parsing Expression Grammar and Regular Expression syntax, with a preference for prefix operators instead of suffix operators.

Pattern Meaning
pat1 pat2 pat1 followed by pat2
pat1 / pat2 pat1 if it matches, otherwise pat2
..pat Any text up to and including pat (except newlines)
..pat1 % pat2 Any text up to and including pat1 (except newlines), skipping over instances of pat2
. Any single character (except newline)
^^ The start of the input
^ The start of a line
$$ The end of the input
$ The end of a line
__ Zero or more whitespace characters (including newlines)
_ Zero or more whitespace characters (excluding newlines)
`c The literal character c
`a-z The character range a through z
`a,b The character a or the character b
\n, \033, \x0A, etc. An escape sequence character
\x00-xFF An escape sequence range (byte 0x00 through 0xFF here)
!pat pat does not match at the current position
[pat] or pat? Zero or one occurrences of pat (optional pattern)
5 pat Exactly 5 occurrences of pat
2-4 pat Between 2 and 4 occurrences of pat (inclusive)
5+ pat 5 or more occurrences of pat
5+ pat % sep 5 or more occurrences of pat, separated by sep (e.g. 0+ int % "," matches 1,2,3)
*pat 0 or more occurrences of pat (shorthand for 0+pat)
+pat 1 or more occurrences of pat (shorthand for 1+pat)
<pat pat matches just before the current position (backref)
>pat pat matches just in front of the current position (lookahead)
@pat Capture pat (used for text replacement and backreferences)
@foo=pat Let foo be the text of pat (used for text replacement and backreferences)
pat => "replacement" Match pat and replace it with replacement
(pat1 @keep=pat2) => "@keep" Match pat1 followed by pat2 and replace it with the text of pat2
pat1==pat2 pat1, assuming pat2 also matches with the same length
pat1!=pat2 pat1, unless pat2 also matches with the same length
#( block comment )# A block comment
# line comment A line comment

See man ./bp.1 for more details.

License

BP is provided under the MIT license with the Commons Clause (you can't sell this software without the developer's permission, but you're otherwise free to use, modify, and redistribute it free of charge). See LICENSE for details.