From 4135115229d27c54b70cd945e2211e652ab58d2f Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 13 Sep 2020 23:31:38 -0700 Subject: Spruced up a bunch of stuff, tweaked the grammar, added docs --- bpeg.c | 46 +++++++++------------------------------------- 1 file changed, 9 insertions(+), 37 deletions(-) (limited to 'bpeg.c') diff --git a/bpeg.c b/bpeg.c index 2245ff8..e78c9f6 100644 --- a/bpeg.c +++ b/bpeg.c @@ -1,36 +1,7 @@ /* * bpeg.c - Source code for the bpeg parser * - * Grammar: - * # comment - * .. any text up to the following pattern (if any); (multiline: ...) - * . any character (multiline: $.) - * ^ beginning of a line (^^: beginning of file) - * $ end of a line ($$: end of file) - * _ 0 or more spaces or tabs (__: include newlines and comments) - * ` character - * `- character between and - * \ escape sequence (e.g. \n, \033) - * \- escape sequence range (e.g. \x00-\xF0) - * ! no - * ~ any character as long as it doesn't match (multiline: ~~) - * + [% ] or more s (separated by ) - * * [% ] sugar for "0+ [% ]" - * - [% ] or fewer s (separated by ) - * ? sugar for "1- " - * - to (inclusive) s - * < after , ... - * > before , ... - * ( ) - * @ capture - * @ [ ] named - * { => } replaced with - * == iff matches at the same spot for the same length - * "@1" or "@[1]" first capture - * "@foo" or "@[foo]" capture named "foo" - * followed by - * / otherwise - * ; = is defined to be + * See `man ./bpeg.1` for more details */ #include #include @@ -53,8 +24,8 @@ static const char *usage = ( " -v --verbose\t print verbose debugging info\n" " -d --define =\t define a grammar rule\n" " -D --define-string =\t define a grammar rule (string-pattern)\n" - " -e --escaped \t provide an escaped pattern (equivalent to bpeg '\\()')\n" - " -s --string \t provide a string pattern (equivalent to bpeg '', but may be useful if '' begins with a '-')\n" + " -p --pattern \t provide a pattern (equivalent to bpeg '\\()')\n" + " -P --pattern-string \t provide a string pattern (equivalent to bpeg '', but may be useful if '' begins with a '-')\n" " -r --replace replace the input pattern with the given replacement\n" " -m --mode \t set the behavior mode (defult: find-all)\n" " -g --grammar use the specified file as a grammar\n"); @@ -104,14 +75,15 @@ int main(int argc, char *argv[]) { int verbose = 0; char *flag = NULL; + char path[PATH_MAX] = {0}; const char *rule = "find-all"; grammar_t *g = new_grammar(); + // Load builtins: int fd; if ((fd=open("/etc/xdg/bpeg/builtins.bpeg", O_RDONLY)) >= 0) load_grammar(g, readfile(fd)); // Keep in memory for debugging output - char path[PATH_MAX] = {0}; sprintf(path, "%s/.config/bpeg/builtins.bpeg", getenv("HOME")); if ((fd=open(path, O_RDONLY)) >= 0) load_grammar(g, readfile(fd)); // Keep in memory for debugging output @@ -121,10 +93,10 @@ int main(int argc, char *argv[]) if (streq(argv[i], "--")) { ++i; break; - } else if (FLAG("--help") || FLAG("-h")) { + } else if (streq(argv[i], "--help") || streq(argv[i], "-h")) { printf("%s\n", usage); return 0; - } else if (FLAG("--verbose") || FLAG("-v")) { + } else if (streq(argv[i], "--verbose") || streq(argv[i], "-v")) { verbose = 1; } else if (FLAG("--replace") || FLAG("-r")) { vm_op_t *p = bpeg_replacement(bpeg_pattern("pattern"), flag); @@ -166,13 +138,13 @@ int main(int argc, char *argv[]) vm_op_t *pat = bpeg_stringpattern(src); check(pat, "Failed to compile pattern"); add_def(g, src, def, pat); - } else if (FLAG("--escaped") || FLAG("-e")) { + } else if (FLAG("--pattern") || FLAG("-p")) { check(npatterns == 0, "Cannot define multiple patterns"); vm_op_t *p = bpeg_pattern(flag); check(p, "Pattern failed to compile: '%s'", flag); add_def(g, flag, "pattern", p); ++npatterns; - } else if (FLAG("--string") || FLAG("-s")) { + } else if (FLAG("--pattern-string") || FLAG("-P")) { vm_op_t *p = bpeg_stringpattern(flag); check(p, "Pattern failed to compile"); add_def(g, flag, "pattern", p); -- cgit v1.2.3