bp/README.md

77 lines
4.0 KiB
Markdown
Raw Normal View History

2020-09-14 11:00:03 -07:00
# BPEG
BPEG is a parsing expression grammar tool for the command line.
It's written in pure C with no dependencies.
## Usage
`bp [flags] <pattern> [<input files>...]`
2020-09-14 11:00:03 -07:00
### Flags
* `-h` `--help` print the usage and quit
* `-v` `--verbose` print verbose debugging info
2020-09-14 12:16:15 -07:00
* `-i` `--ignore-case` perform a case-insensitive match
2020-12-27 19:48:52 -08:00
* `-I` `--inplace` perform replacements or filtering in-place on files
2020-12-14 22:32:47 -08:00
* `-e` `--explain` print an explanation of the matches
* `-j` `--json` print matches as JSON objects
2020-12-17 16:23:45 -08:00
* `-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 '-')
2020-09-14 11:00:03 -07:00
* `-r` `--replace <replacement>` replace the input pattern with the given replacement
* `-m` `--mode <mode>` set the behavior mode (defult: `find-all`)
2020-09-14 11:00:03 -07:00
* `-g` `--grammar <grammar file>` use the specified file as a grammar
See `man ./bp.1` for more details.
2020-09-14 11:00:03 -07:00
## BPEG Patterns
BPEG patterns are a mixture of Parsing Expression Grammar and Regular
Expression syntax, with a preference for prefix operators instead of
suffix operators.
2020-09-28 16:54:17 -07:00
Pattern | Meaning
-------------------|---------------------
`pat1 pat2` | `pat1` followed by `pat2`
`pat1 / pat2` | `pat1` if it matches, otherwise `pat2`
`...pat` | Any text up to and including `pat` (including newlines)
`..pat` | Any text up to and including `pat` (except newlines)
`.` | Any single character (except newline)
`$.` | Any single character (including 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`
2020-12-19 18:53:51 -08:00
`` `a,b `` | The character `a` or the character `b`
2020-09-28 16:54:17 -07:00
`\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
2020-09-28 17:42:38 -07:00
`[pat]` or `pat?` | Zero or one occurrences of `pat` (optional pattern)
2020-09-28 16:54:17 -07:00
`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`
2020-09-28 17:42:38 -07:00
`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`)
2020-09-28 16:54:17 -07:00
`<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`
2020-09-28 16:54:17 -07:00
`pat1==pat2` | `pat1`, assuming `pat2` also matches with the same length
2020-09-28 17:56:02 -07:00
`pat1!=pat2` | `pat1`, unless `pat2` also matches with the same length
2020-09-28 16:54:17 -07:00
`#( block comment )#` | A block comment
`# line comment` | A line comment
See `man ./bp.1` for more details.
2020-09-14 11:00:03 -07:00
## License
2020-09-28 17:02:34 -07:00
BPEG is provided under the MIT license with the [Commons Clause](https://commonsclause.com/)
2020-09-28 17:01:53 -07:00
(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](LICENSE) for details.