From ff2ef9504159665c30b16b398f1e755665353bc3 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 30 Dec 2020 19:42:47 -0800 Subject: Fully purging "bpeg" from the source --- .gitignore | 2 +- LICENSE | 2 +- Makefile | 2 +- README.md | 10 +- bp.c | 288 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ bpeg.c | 288 ------------------------------------------------------- compiler.c | 50 +++++----- compiler.h | 10 +- grammar.c | 4 +- grammars/bpeg.bp | 4 +- types.h | 20 ++-- utils.c | 2 +- vm.c | 10 +- vm.h | 2 +- 14 files changed, 347 insertions(+), 347 deletions(-) create mode 100644 bp.c delete mode 100644 bpeg.c diff --git a/.gitignore b/.gitignore index 73725c9..679010a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ *.o tags -bpeg +bp .* !.gitignore diff --git a/LICENSE b/LICENSE index e6a922d..6f01923 100644 --- a/LICENSE +++ b/LICENSE @@ -17,7 +17,7 @@ whose value derives, entirely or substantially, from the functionality of the Software. Any license notice or attribution required by the License must also include this Commons Clause License Condition notice. -Software: BPEG +Software: BP License: MIT Licensor: Bruce Hill diff --git a/Makefile b/Makefile index d419241..39d15de 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ all: $(NAME) .c.o: $(CC) -c $(CFLAGS) $(CWARN) $(G) $(O) -o $@ $< -$(NAME): $(OBJFILES) bpeg.c +$(NAME): $(OBJFILES) bp.c $(CC) $(CFLAGS) $(CWARN) $(G) $(O) -o $@ $^ clean: diff --git a/README.md b/README.md index cd290e1..04bfba4 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# BPEG +# BP - Bruce's PEG Tool -BPEG is a parsing expression grammar tool for the command line. +BP is a parsing expression grammar (PEG) tool for the command line. It's written in pure C with no dependencies. ## Usage @@ -24,8 +24,8 @@ It's written in pure C with no dependencies. See `man ./bp.1` for more details. -## BPEG Patterns -BPEG patterns are a mixture of Parsing Expression Grammar and Regular +## 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. @@ -70,7 +70,7 @@ Pattern | Meaning See `man ./bp.1` for more details. ## License -BPEG is provided under the MIT license with the [Commons Clause](https://commonsclause.com/) +BP is provided under the MIT license with the [Commons Clause](https://commonsclause.com/) (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. diff --git a/bp.c b/bp.c new file mode 100644 index 0000000..41acbcc --- /dev/null +++ b/bp.c @@ -0,0 +1,288 @@ +/* + * bp.c - Source code for the bp parser + * + * See `man ./bp.1` for more details + */ +#include +#include +#include +#include +#include +#include +#include + +#include "compiler.h" +#include "file_loader.h" +#include "grammar.h" +#include "json.h" +#include "utils.h" +#include "viz.h" +#include "vm.h" + +static const char *usage = ( + "BP - a Parsing Expression Grammar command line tool\n\n" + "Usage:\n" + " bp [flags] [...]\n\n" + "Flags:\n" + " -h --help print the usage and quit\n" + " -v --verbose print verbose debugging info\n" + " -e --explain explain the matches\n" + " -j --json print matches as a list of JSON objects\n" + " -I --inplace modify a file in-place\n" + " -i --ignore-case preform matching case-insensitively\n" + " -l --list-files list filenames only\n" + " -d --define : define a grammar rule\n" + " -D --define-string : define a grammar rule (string-pattern)\n" + " -p --pattern provide a pattern (equivalent to bp '\\()')\n" + " -P --pattern-string provide a string pattern (may be useful if '' begins with a '-')\n" + " -r --replace replace the input pattern with the given replacement\n" + " -m --mode set the behavior mode (defult: find-all)\n" + " -g --grammar use the specified file as a grammar\n"); + +static print_options_t print_options = 0; + +static char *getflag(const char *flag, char *argv[], int *i) +{ + size_t n = strlen(flag); + check(argv[*i], "Attempt to get flag from NULL argument"); + if (strncmp(argv[*i], flag, n) == 0) { + if (argv[*i][n] == '=') { + return &argv[*i][n+1]; + } else if (argv[*i][n] == '\0') { + check(argv[*i+1], "Expected argument after '%s'\n\n%s", flag, usage); + ++(*i); + return argv[*i]; + } + } + return NULL; +} + +static int print_errors(file_t *f, match_t *m) +{ + int ret = 0; + if (m->op->op == VM_CAPTURE && m->op->args.capture.name && streq(m->op->args.capture.name, "!")) { + printf("\033[31;1m"); + print_match(stdout, f, m, print_options); + printf("\033[0m\n"); + fprint_line(stdout, f, m->start, m->end, " "); + return 1; + } + if (m->child) ret += print_errors(f, m->child); + if (m->nextsibling) ret += print_errors(f, m->nextsibling); + return ret; +} + +static int run_match(grammar_t *g, const char *filename, vm_op_t *pattern, unsigned int flags) +{ + static int printed_matches = 0; + file_t *f = load_file(filename); + check(f, "Could not open file: %s", filename); + if (flags & BP_INPLACE) // Need to do this before matching + intern_file(f); + match_t *m = match(g, f, f->contents, pattern, flags); + if (m && print_errors(f, m) > 0) + _exit(1); + if (m != NULL && m->end > m->start + 1) { + ++printed_matches; + + if (flags & BP_EXPLAIN) { + if (filename) + printf("\033[1;4m%s\033[0m\n", filename); + visualize_match(m); + } else if (flags & BP_LISTFILES) { + printf("%s\n", filename); + } else if (flags & BP_JSON) { + if (printed_matches > 1) + printf(",\n"); + printf("{\"filename\":\"%s\",", filename ? filename : "-"); + printf("\"tree\":{\"rule\":\"text\",\"start\":%d,\"end\":%ld,\"children\":[", + 0, f->end - f->contents); + json_match(f->contents, m, (flags & BP_VERBOSE) ? 1 : 0); + printf("]}}\n"); + } else if (flags & BP_INPLACE && filename) { + FILE *out = fopen(filename, "w"); + print_match(out, f, m, 0); + fclose(out); + printf("%s\n", filename); + } else { + if (printed_matches > 1) + fputc('\n', stdout); + if (filename) { + if (print_options & PRINT_COLOR) + printf("\033[1;4;33m%s\033[0m\n", filename); + else + printf("%s:\n", filename); + } + print_match(stdout, f, m, + filename ? print_options : print_options & (print_options_t)~PRINT_LINE_NUMBERS); + } + destroy_file(&f); + return 0; + } else { + destroy_file(&f); + return 1; + } +} + +#define FLAG(f) (flag=getflag((f), argv, &i)) + +int main(int argc, char *argv[]) +{ + unsigned int flags = 0; + char *flag = NULL; + char path[PATH_MAX] = {0}; + const char *rule = "find-all"; + + grammar_t *g = new_grammar(); + + // Load builtins: + if (access("/etc/xdg/bp/builtins.bp", R_OK) != -1) + load_grammar(g, load_file("/etc/xdg/bp/builtins.bp")); // Keep in memory for debugging output + sprintf(path, "%s/.config/bp/builtins.bp", getenv("HOME")); + if (access(path, R_OK) != -1) + load_grammar(g, load_file(path)); // Keep in memory for debugging output + + int i, npatterns = 0; + check(argc > 1, "%s", usage); + for (i = 1; i < argc; i++) { + if (streq(argv[i], "--")) { + ++i; + break; + } else if (streq(argv[i], "--help")) { + flag_help: + printf("%s\n", usage); + return 0; + } else if (streq(argv[i], "--verbose")) { + flags |= BP_VERBOSE; + } else if (streq(argv[i], "--explain")) { + flags |= BP_EXPLAIN; + } else if (streq(argv[i], "--json")) { + flags |= BP_JSON; + } else if (streq(argv[i], "--inplace")) { + flags |= BP_INPLACE; + } else if (streq(argv[i], "--ignore-case")) { + flags |= BP_IGNORECASE; + } else if (streq(argv[i], "--list-files")) { + flags |= BP_LISTFILES; + } else if (FLAG("--replace") || FLAG("-r")) { + file_t *pat_file = spoof_file("", "pattern"); + vm_op_t *patref = bp_pattern(pat_file, pat_file->contents); + file_t *replace_file = spoof_file("", flag); + vm_op_t *rep = bp_replacement(replace_file, patref, replace_file->contents); + check(rep, "Replacement failed to compile: %s", flag); + add_def(g, replace_file, replace_file->contents, "replacement", rep); + rule = "replace-all"; + } else if (FLAG("--grammar") || FLAG("-g")) { + file_t *f = load_file(flag); + if (f == NULL) { + sprintf(path, "%s/.config/bp/%s.bp", getenv("HOME"), flag); + f = load_file(path); + } + if (f == NULL) { + sprintf(path, "/etc/xdg/bp/%s.bp", flag); + f = load_file(path); + } + check(f != NULL, "Couldn't find grammar: %s", flag); + load_grammar(g, f); // Keep in memory for debug output + } else if (FLAG("--define") || FLAG("-d")) { + char *def = flag; + char *eq = strchr(def, ':'); + check(eq, "Rule definitions must include an ':'\n\n%s", usage); + *eq = '\0'; + char *src = ++eq; + file_t *def_file = spoof_file(def, src); + vm_op_t *pat = bp_pattern(def_file, def_file->contents); + check(pat, "Failed to compile pattern: %s", flag); + add_def(g, def_file, src, def, pat); + } else if (FLAG("--define-string") || FLAG("-D")) { + char *def = flag; + char *eq = strchr(def, ':'); + check(eq, "Rule definitions must include an ':'\n\n%s", usage); + *eq = '\0'; + char *src = ++eq; + file_t *def_file = spoof_file(def, flag); + vm_op_t *pat = bp_stringpattern(def_file, def_file->contents); + check(pat, "Failed to compile pattern: %s", flag); + add_def(g, def_file, src, def, pat); + } else if (FLAG("--pattern") || FLAG("-p")) { + check(npatterns == 0, "Cannot define multiple patterns"); + file_t *arg_file = spoof_file("", flag); + vm_op_t *p = bp_pattern(arg_file, arg_file->contents); + check(p, "Pattern failed to compile: %s", flag); + add_def(g, arg_file, flag, "pattern", p); + ++npatterns; + } else if (FLAG("--pattern-string") || FLAG("-P")) { + file_t *arg_file = spoof_file("", flag); + vm_op_t *p = bp_stringpattern(arg_file, arg_file->contents); + check(p, "Pattern failed to compile: %s", flag); + add_def(g, arg_file, flag, "pattern", p); + ++npatterns; + } else if (FLAG("--mode") || FLAG("-m")) { + rule = flag; + } else if (argv[i][0] == '-' && argv[i][1] && argv[i][1] != '-') { // single-char flags + for (char *c = &argv[i][1]; *c; ++c) { + switch (*c) { + case 'h': goto flag_help; // -h + case 'v': flags |= BP_VERBOSE; break; // -v + case 'e': flags |= BP_EXPLAIN; break; // -e + case 'j': flags |= BP_JSON; break; // -j + case 'I': flags |= BP_INPLACE; break; // -I + case 'i': flags |= BP_IGNORECASE; break; // -i + case 'l': flags |= BP_LISTFILES; break; // -l + default: + printf("Unrecognized flag: -%c\n\n%s\n", *c, usage); + return 1; + } + } + } else if (argv[i][0] != '-') { + if (npatterns > 0) break; + file_t *arg_file = spoof_file("", argv[i]); + vm_op_t *p = bp_stringpattern(arg_file, arg_file->contents); + check(p, "Pattern failed to compile: %s", argv[i]); + add_def(g, arg_file, argv[i], "pattern", p); + ++npatterns; + } else { + printf("Unrecognized flag: %s\n\n%s\n", argv[i], usage); + return 1; + } + } + + if (((flags & BP_JSON) != 0) + ((flags & BP_EXPLAIN) != 0) + ((flags & BP_LISTFILES) != 0) > 1) { + printf("Please choose no more than one of the flags: -j/--json, -e/--explain, and -l/--list-files.\n" + "They are mutually contradictory.\n"); + return 1; + } + + if (isatty(STDOUT_FILENO)) { + print_options |= PRINT_COLOR | PRINT_LINE_NUMBERS; + } + + vm_op_t *pattern = lookup(g, rule); + check(pattern != NULL, "No such rule: '%s'", rule); + + int ret = 1; + if (flags & BP_JSON) printf("["); + if (i < argc) { + // Files pass in as command line args: + for (int nfiles = 0; i < argc; nfiles++, i++) { + ret &= run_match(g, argv[i], pattern, flags); + } + } else if (isatty(STDIN_FILENO)) { + // No files, no piped in input, so use * **/*: + glob_t globbuf; + glob("*", 0, NULL, &globbuf); + glob("**/*", GLOB_APPEND, NULL, &globbuf); + for (size_t i = 0; i < globbuf.gl_pathc; i++) { + ret &= run_match(g, globbuf.gl_pathv[i], pattern, flags); + } + globfree(&globbuf); + } else { + // Piped in input: + ret &= run_match(g, NULL, pattern, flags); + } + if (flags & BP_JSON) printf("]\n"); + + return ret; +} + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1 diff --git a/bpeg.c b/bpeg.c deleted file mode 100644 index 438c302..0000000 --- a/bpeg.c +++ /dev/null @@ -1,288 +0,0 @@ -/* - * bpeg.c - Source code for the bpeg parser - * - * See `man ./bp.1` for more details - */ -#include -#include -#include -#include -#include -#include -#include - -#include "compiler.h" -#include "file_loader.h" -#include "grammar.h" -#include "json.h" -#include "utils.h" -#include "viz.h" -#include "vm.h" - -static const char *usage = ( - "BP - a Parsing Expression Grammar command line tool\n\n" - "Usage:\n" - " bp [flags] [...]\n\n" - "Flags:\n" - " -h --help print the usage and quit\n" - " -v --verbose print verbose debugging info\n" - " -e --explain explain the matches\n" - " -j --json print matches as a list of JSON objects\n" - " -I --inplace modify a file in-place\n" - " -i --ignore-case preform matching case-insensitively\n" - " -l --list-files list filenames only\n" - " -d --define : define a grammar rule\n" - " -D --define-string : define a grammar rule (string-pattern)\n" - " -p --pattern provide a pattern (equivalent to bp '\\()')\n" - " -P --pattern-string provide a string pattern (may be useful if '' begins with a '-')\n" - " -r --replace replace the input pattern with the given replacement\n" - " -m --mode set the behavior mode (defult: find-all)\n" - " -g --grammar use the specified file as a grammar\n"); - -static print_options_t print_options = 0; - -static char *getflag(const char *flag, char *argv[], int *i) -{ - size_t n = strlen(flag); - check(argv[*i], "Attempt to get flag from NULL argument"); - if (strncmp(argv[*i], flag, n) == 0) { - if (argv[*i][n] == '=') { - return &argv[*i][n+1]; - } else if (argv[*i][n] == '\0') { - check(argv[*i+1], "Expected argument after '%s'\n\n%s", flag, usage); - ++(*i); - return argv[*i]; - } - } - return NULL; -} - -static int print_errors(file_t *f, match_t *m) -{ - int ret = 0; - if (m->op->op == VM_CAPTURE && m->op->args.capture.name && streq(m->op->args.capture.name, "!")) { - printf("\033[31;1m"); - print_match(stdout, f, m, print_options); - printf("\033[0m\n"); - fprint_line(stdout, f, m->start, m->end, " "); - return 1; - } - if (m->child) ret += print_errors(f, m->child); - if (m->nextsibling) ret += print_errors(f, m->nextsibling); - return ret; -} - -static int run_match(grammar_t *g, const char *filename, vm_op_t *pattern, unsigned int flags) -{ - static int printed_matches = 0; - file_t *f = load_file(filename); - check(f, "Could not open file: %s", filename); - if (flags & BPEG_INPLACE) // Need to do this before matching - intern_file(f); - match_t *m = match(g, f, f->contents, pattern, flags); - if (m && print_errors(f, m) > 0) - _exit(1); - if (m != NULL && m->end > m->start + 1) { - ++printed_matches; - - if (flags & BPEG_EXPLAIN) { - if (filename) - printf("\033[1;4m%s\033[0m\n", filename); - visualize_match(m); - } else if (flags & BPEG_LISTFILES) { - printf("%s\n", filename); - } else if (flags & BPEG_JSON) { - if (printed_matches > 1) - printf(",\n"); - printf("{\"filename\":\"%s\",", filename ? filename : "-"); - printf("\"tree\":{\"rule\":\"text\",\"start\":%d,\"end\":%ld,\"children\":[", - 0, f->end - f->contents); - json_match(f->contents, m, (flags & BPEG_VERBOSE) ? 1 : 0); - printf("]}}\n"); - } else if (flags & BPEG_INPLACE && filename) { - FILE *out = fopen(filename, "w"); - print_match(out, f, m, 0); - fclose(out); - printf("%s\n", filename); - } else { - if (printed_matches > 1) - fputc('\n', stdout); - if (filename) { - if (print_options & PRINT_COLOR) - printf("\033[1;4;33m%s\033[0m\n", filename); - else - printf("%s:\n", filename); - } - print_match(stdout, f, m, - filename ? print_options : print_options & (print_options_t)~PRINT_LINE_NUMBERS); - } - destroy_file(&f); - return 0; - } else { - destroy_file(&f); - return 1; - } -} - -#define FLAG(f) (flag=getflag((f), argv, &i)) - -int main(int argc, char *argv[]) -{ - unsigned int flags = 0; - char *flag = NULL; - char path[PATH_MAX] = {0}; - const char *rule = "find-all"; - - grammar_t *g = new_grammar(); - - // Load builtins: - if (access("/etc/xdg/bp/builtins.bp", R_OK) != -1) - load_grammar(g, load_file("/etc/xdg/bp/builtins.bp")); // Keep in memory for debugging output - sprintf(path, "%s/.config/bp/builtins.bp", getenv("HOME")); - if (access(path, R_OK) != -1) - load_grammar(g, load_file(path)); // Keep in memory for debugging output - - int i, npatterns = 0; - check(argc > 1, "%s", usage); - for (i = 1; i < argc; i++) { - if (streq(argv[i], "--")) { - ++i; - break; - } else if (streq(argv[i], "--help")) { - flag_help: - printf("%s\n", usage); - return 0; - } else if (streq(argv[i], "--verbose")) { - flags |= BPEG_VERBOSE; - } else if (streq(argv[i], "--explain")) { - flags |= BPEG_EXPLAIN; - } else if (streq(argv[i], "--json")) { - flags |= BPEG_JSON; - } else if (streq(argv[i], "--inplace")) { - flags |= BPEG_INPLACE; - } else if (streq(argv[i], "--ignore-case")) { - flags |= BPEG_IGNORECASE; - } else if (streq(argv[i], "--list-files")) { - flags |= BPEG_LISTFILES; - } else if (FLAG("--replace") || FLAG("-r")) { - file_t *pat_file = spoof_file("", "pattern"); - vm_op_t *patref = bpeg_pattern(pat_file, pat_file->contents); - file_t *replace_file = spoof_file("", flag); - vm_op_t *rep = bpeg_replacement(replace_file, patref, replace_file->contents); - check(rep, "Replacement failed to compile: %s", flag); - add_def(g, replace_file, replace_file->contents, "replacement", rep); - rule = "replace-all"; - } else if (FLAG("--grammar") || FLAG("-g")) { - file_t *f = load_file(flag); - if (f == NULL) { - sprintf(path, "%s/.config/bp/%s.bp", getenv("HOME"), flag); - f = load_file(path); - } - if (f == NULL) { - sprintf(path, "/etc/xdg/bp/%s.bp", flag); - f = load_file(path); - } - check(f != NULL, "Couldn't find grammar: %s", flag); - load_grammar(g, f); // Keep in memory for debug output - } else if (FLAG("--define") || FLAG("-d")) { - char *def = flag; - char *eq = strchr(def, ':'); - check(eq, "Rule definitions must include an ':'\n\n%s", usage); - *eq = '\0'; - char *src = ++eq; - file_t *def_file = spoof_file(def, src); - vm_op_t *pat = bpeg_pattern(def_file, def_file->contents); - check(pat, "Failed to compile pattern: %s", flag); - add_def(g, def_file, src, def, pat); - } else if (FLAG("--define-string") || FLAG("-D")) { - char *def = flag; - char *eq = strchr(def, ':'); - check(eq, "Rule definitions must include an ':'\n\n%s", usage); - *eq = '\0'; - char *src = ++eq; - file_t *def_file = spoof_file(def, flag); - vm_op_t *pat = bpeg_stringpattern(def_file, def_file->contents); - check(pat, "Failed to compile pattern: %s", flag); - add_def(g, def_file, src, def, pat); - } else if (FLAG("--pattern") || FLAG("-p")) { - check(npatterns == 0, "Cannot define multiple patterns"); - file_t *arg_file = spoof_file("", flag); - vm_op_t *p = bpeg_pattern(arg_file, arg_file->contents); - check(p, "Pattern failed to compile: %s", flag); - add_def(g, arg_file, flag, "pattern", p); - ++npatterns; - } else if (FLAG("--pattern-string") || FLAG("-P")) { - file_t *arg_file = spoof_file("", flag); - vm_op_t *p = bpeg_stringpattern(arg_file, arg_file->contents); - check(p, "Pattern failed to compile: %s", flag); - add_def(g, arg_file, flag, "pattern", p); - ++npatterns; - } else if (FLAG("--mode") || FLAG("-m")) { - rule = flag; - } else if (argv[i][0] == '-' && argv[i][1] && argv[i][1] != '-') { // single-char flags - for (char *c = &argv[i][1]; *c; ++c) { - switch (*c) { - case 'h': goto flag_help; // -h - case 'v': flags |= BPEG_VERBOSE; break; // -v - case 'e': flags |= BPEG_EXPLAIN; break; // -e - case 'j': flags |= BPEG_JSON; break; // -j - case 'I': flags |= BPEG_INPLACE; break; // -I - case 'i': flags |= BPEG_IGNORECASE; break; // -i - case 'l': flags |= BPEG_LISTFILES; break; // -l - default: - printf("Unrecognized flag: -%c\n\n%s\n", *c, usage); - return 1; - } - } - } else if (argv[i][0] != '-') { - if (npatterns > 0) break; - file_t *arg_file = spoof_file("", argv[i]); - vm_op_t *p = bpeg_stringpattern(arg_file, arg_file->contents); - check(p, "Pattern failed to compile: %s", argv[i]); - add_def(g, arg_file, argv[i], "pattern", p); - ++npatterns; - } else { - printf("Unrecognized flag: %s\n\n%s\n", argv[i], usage); - return 1; - } - } - - if (((flags & BPEG_JSON) != 0) + ((flags & BPEG_EXPLAIN) != 0) + ((flags & BPEG_LISTFILES) != 0) > 1) { - printf("Please choose no more than one of the flags: -j/--json, -e/--explain, and -l/--list-files.\n" - "They are mutually contradictory.\n"); - return 1; - } - - if (isatty(STDOUT_FILENO)) { - print_options |= PRINT_COLOR | PRINT_LINE_NUMBERS; - } - - vm_op_t *pattern = lookup(g, rule); - check(pattern != NULL, "No such rule: '%s'", rule); - - int ret = 1; - if (flags & BPEG_JSON) printf("["); - if (i < argc) { - // Files pass in as command line args: - for (int nfiles = 0; i < argc; nfiles++, i++) { - ret &= run_match(g, argv[i], pattern, flags); - } - } else if (isatty(STDIN_FILENO)) { - // No files, no piped in input, so use * **/*: - glob_t globbuf; - glob("*", 0, NULL, &globbuf); - glob("**/*", GLOB_APPEND, NULL, &globbuf); - for (size_t i = 0; i < globbuf.gl_pathc; i++) { - ret &= run_match(g, globbuf.gl_pathv[i], pattern, flags); - } - globfree(&globbuf); - } else { - // Piped in input: - ret &= run_match(g, NULL, pattern, flags); - } - if (flags & BPEG_JSON) printf("]\n"); - - return ret; -} - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1 diff --git a/compiler.c b/compiler.c index 2ad0e32..7ee5fbe 100644 --- a/compiler.c +++ b/compiler.c @@ -1,5 +1,5 @@ /* - * compiler.c - Compile strings into BPEG virtual machine code. + * compiler.c - Compile strings into BP virtual machine code. */ #include @@ -46,7 +46,7 @@ static void set_range(vm_op_t *op, ssize_t min, ssize_t max, vm_op_t *pat, vm_op */ static vm_op_t *expand_chain(file_t *f, vm_op_t *first) { - vm_op_t *second = bpeg_simplepattern(f, first->end); + vm_op_t *second = bp_simplepattern(f, first->end); if (second == NULL) return first; second = expand_chain(f, second); if (second->end <= first->end) @@ -65,7 +65,7 @@ static vm_op_t *expand_choices(file_t *f, vm_op_t *first) first = expand_chain(f, first); const char *str = first->end; if (!matchchar(&str, '/')) return first; - vm_op_t *second = bpeg_simplepattern(f, str); + vm_op_t *second = bp_simplepattern(f, str); if (!second) file_err(f, str, str, "There should be a pattern here after a '/'"); second = expand_choices(f, second); @@ -98,9 +98,9 @@ static vm_op_t *chain_together(vm_op_t *first, vm_op_t *second) } /* - * Compile a string of BPEG code into virtual machine opcodes + * Compile a string of BP code into virtual machine opcodes */ -vm_op_t *bpeg_simplepattern(file_t *f, const char *str) +vm_op_t *bp_simplepattern(file_t *f, const char *str) { str = after_spaces(str); if (!*str) return NULL; @@ -119,14 +119,14 @@ vm_op_t *bpeg_simplepattern(file_t *f, const char *str) ++str; op->multiline = 1; } - vm_op_t *till = bpeg_simplepattern(f, str); + vm_op_t *till = bp_simplepattern(f, str); op->op = VM_UPTO_AND; op->len = -1; op->args.multiple.first = till; if (till) str = till->end; if (matchchar(&str, '%')) { - vm_op_t *skip = bpeg_simplepattern(f, str); + vm_op_t *skip = bp_simplepattern(f, str); if (!skip) file_err(f, str, str, "There should be a pattern to skip here after the '%%'"); op->args.multiple.second = skip; @@ -252,7 +252,7 @@ vm_op_t *bpeg_simplepattern(file_t *f, const char *str) } // Not case '!': { - vm_op_t *p = bpeg_simplepattern(f, str); + vm_op_t *p = bp_simplepattern(f, str); if (!p) file_err(f, str, str, "There should be a pattern after this '!'"); str = p->end; op->op = VM_NOT; @@ -277,14 +277,14 @@ vm_op_t *bpeg_simplepattern(file_t *f, const char *str) } else { min = n1, max = n1; } - vm_op_t *pat = bpeg_simplepattern(f, str); + vm_op_t *pat = bp_simplepattern(f, str); if (!pat) file_err(f, str, str, "There should be a pattern after this repetition count."); str = pat->end; str = after_spaces(str); vm_op_t *sep = NULL; if (matchchar(&str, '%')) { - sep = bpeg_simplepattern(f, str); + sep = bp_simplepattern(f, str); if (!sep) file_err(f, str, str, "There should be a separator pattern after this '%%'"); str = sep->end; @@ -296,7 +296,7 @@ vm_op_t *bpeg_simplepattern(file_t *f, const char *str) } // Lookbehind case '<': { - vm_op_t *pat = bpeg_simplepattern(f, str); + vm_op_t *pat = bp_simplepattern(f, str); if (!pat) file_err(f, str, str, "There should be a pattern after this '<'"); str = pat->end; @@ -312,7 +312,7 @@ vm_op_t *bpeg_simplepattern(file_t *f, const char *str) } // Lookahead case '>': { - vm_op_t *pat = bpeg_simplepattern(f, str); + vm_op_t *pat = bp_simplepattern(f, str); if (!pat) file_err(f, str, str, "There should be a pattern after this '>'"); str = pat->end; @@ -325,7 +325,7 @@ vm_op_t *bpeg_simplepattern(file_t *f, const char *str) case '(': case '{': { char closing = c == '(' ? ')' : '}'; free(op); - op = bpeg_simplepattern(f, str); + op = bp_simplepattern(f, str); if (!op) file_err(f, str, str, "There should be a valid pattern after this parenthesis."); op = expand_choices(f, op); @@ -339,7 +339,7 @@ vm_op_t *bpeg_simplepattern(file_t *f, const char *str) } // Square brackets case '[': { - vm_op_t *pat = bpeg_simplepattern(f, str); + vm_op_t *pat = bp_simplepattern(f, str); if (!pat) file_err(f, str, str, "There should be a valid pattern after this square bracket."); pat = expand_choices(f, pat); @@ -353,14 +353,14 @@ vm_op_t *bpeg_simplepattern(file_t *f, const char *str) // Repeating case '*': case '+': { ssize_t min = c == '*' ? 0 : 1; - vm_op_t *pat = bpeg_simplepattern(f, str); + vm_op_t *pat = bp_simplepattern(f, str); if (!pat) file_err(f, str, str, "There should be a valid pattern here after the '%c'", c); str = pat->end; str = after_spaces(str); vm_op_t *sep = NULL; if (matchchar(&str, '%')) { - sep = bpeg_simplepattern(f, str); + sep = bp_simplepattern(f, str); if (!sep) file_err(f, str, str, "There should be a separator pattern after the '%%' here."); str = sep->end; @@ -376,7 +376,7 @@ vm_op_t *bpeg_simplepattern(file_t *f, const char *str) op->args.capture.name = strndup(str, (size_t)(a-str)); str = after_spaces(a) + 1; } - vm_op_t *pat = bpeg_simplepattern(f, str); + vm_op_t *pat = bp_simplepattern(f, str); if (!pat) file_err(f, str, str, "There should be a valid pattern here to capture after the '@'"); str = pat->end; @@ -386,7 +386,7 @@ vm_op_t *bpeg_simplepattern(file_t *f, const char *str) } // Hide case '~': { - vm_op_t *pat = bpeg_simplepattern(f, str); + vm_op_t *pat = bp_simplepattern(f, str); if (!pat) file_err(f, str, str, "There should be a pattern after this '~'"); str = pat->end; @@ -473,7 +473,7 @@ vm_op_t *bpeg_simplepattern(file_t *f, const char *str) } else if (str+2 < f->end && (matchstr(&str, "!=") || matchstr(&str, "=="))) { // Equality == and inequality != int equal = str[-2] == '='; vm_op_t *first = op; - vm_op_t *second = bpeg_simplepattern(f, str); + vm_op_t *second = bp_simplepattern(f, str); if (!second) file_err(f, str, str, "The '%c=' operator expects a pattern before and after.", equal?'=':'!'); if (equal) { @@ -497,9 +497,9 @@ vm_op_t *bpeg_simplepattern(file_t *f, const char *str) } /* - * Similar to bpeg_simplepattern, except that the pattern begins with an implicit, unclosable quote. + * Similar to bp_simplepattern, except that the pattern begins with an implicit, unclosable quote. */ -vm_op_t *bpeg_stringpattern(file_t *f, const char *str) +vm_op_t *bp_stringpattern(file_t *f, const char *str) { vm_op_t *ret = NULL; while (*str) { @@ -530,7 +530,7 @@ vm_op_t *bpeg_stringpattern(file_t *f, const char *str) ++str; continue; } - interp = bpeg_simplepattern(f, str + 1); + interp = bp_simplepattern(f, str + 1); if (interp == NULL) file_err(f, str, str+1, "This isn't a valid escape sequence or pattern."); break; @@ -565,7 +565,7 @@ vm_op_t *bpeg_stringpattern(file_t *f, const char *str) * Given a pattern and a replacement string, compile the two into a replacement * VM opcode. */ -vm_op_t *bpeg_replacement(file_t *f, vm_op_t *pat, const char *replacement) +vm_op_t *bp_replacement(file_t *f, vm_op_t *pat, const char *replacement) { vm_op_t *op = new(vm_op_t); op->op = VM_REPLACE; @@ -588,9 +588,9 @@ vm_op_t *bpeg_replacement(file_t *f, vm_op_t *pat, const char *replacement) return op; } -vm_op_t *bpeg_pattern(file_t *f, const char *str) +vm_op_t *bp_pattern(file_t *f, const char *str) { - vm_op_t *op = bpeg_simplepattern(f, str); + vm_op_t *op = bp_simplepattern(f, str); if (op != NULL) op = expand_choices(f, op); return op; } diff --git a/compiler.h b/compiler.h index f7e8b43..bae222f 100644 --- a/compiler.h +++ b/compiler.h @@ -1,5 +1,5 @@ /* - * compiler.h - Header file for BPEG compiler. + * compiler.h - Header file for BP compiler. */ #ifndef COMPILER__H #define COMPILER__H @@ -8,13 +8,13 @@ #include "types.h" __attribute__((nonnull(1,2))) -vm_op_t *bpeg_simplepattern(file_t *f, const char *str); +vm_op_t *bp_simplepattern(file_t *f, const char *str); __attribute__((nonnull(1,2))) -vm_op_t *bpeg_stringpattern(file_t *f, const char *str); +vm_op_t *bp_stringpattern(file_t *f, const char *str); __attribute__((nonnull(1,2))) -vm_op_t *bpeg_replacement(file_t *f, vm_op_t *pat, const char *replacement); +vm_op_t *bp_replacement(file_t *f, vm_op_t *pat, const char *replacement); __attribute__((nonnull(1,2))) -vm_op_t *bpeg_pattern(file_t *f, const char *str); +vm_op_t *bp_pattern(file_t *f, const char *str); #endif // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1 diff --git a/grammar.c b/grammar.c index 08fe37b..229fb0e 100644 --- a/grammar.c +++ b/grammar.c @@ -46,7 +46,7 @@ vm_op_t *load_grammar(grammar_t *g, file_t *f) name = strndup(name, (size_t)(name_end-name)); src = after_spaces(name_end); check(matchchar(&src, ':'), "Expected ':' in definition"); - vm_op_t *op = bpeg_pattern(f, src); + vm_op_t *op = bp_pattern(f, src); if (op == NULL) break; //check(op, "Couldn't load definition"); add_def(g, f, src, name, op); @@ -59,7 +59,7 @@ vm_op_t *load_grammar(grammar_t *g, file_t *f) src = after_spaces(src); } if (src < f->end) { - fprint_line(stderr, f, src, NULL, "Invalid BPEG pattern"); + fprint_line(stderr, f, src, NULL, "Invalid BP pattern"); _exit(1); } return ret; diff --git a/grammars/bpeg.bp b/grammars/bpeg.bp index c7960dd..007c209 100644 --- a/grammars/bpeg.bp +++ b/grammars/bpeg.bp @@ -1,4 +1,4 @@ -# This is a file defining the BPEG grammar using BPEG syntax +# This is a file defining the BP grammar using BP syntax Grammar: __ *(Def [__`;])%__ __ ($$ / @!=(... => "Could not parse this code")) Def: @name=id _ `: __ ( @@ -78,5 +78,5 @@ id: "^^" / "^" / "__" / "_" / "$$" / "$" / "|" / `a-z,A-Z *`a-z,A-Z,0-9,- line-comment: `# .. $ block-comment: "#("..")#" % block-comment -# Note: comments are undefined by default in regular BPEG +# Note: comments are undefined by default in regular BP comment: block-comment / line-comment diff --git a/types.h b/types.h index 0cbcc93..24b66a9 100644 --- a/types.h +++ b/types.h @@ -1,5 +1,5 @@ /* - * types.h - Datatypes used by BPEG + * types.h - Datatypes used by BP */ #ifndef TYPES__H #define TYPES__H @@ -8,17 +8,17 @@ #include "file_loader.h" -enum BPEGFlag { - BPEG_VERBOSE = 1 << 0, - BPEG_IGNORECASE = 1 << 1, - BPEG_EXPLAIN = 1 << 2, - BPEG_JSON = 1 << 3, - BPEG_LISTFILES = 1 << 4, - BPEG_INPLACE = 1 << 5, +enum BPFlag { + BP_VERBOSE = 1 << 0, + BP_IGNORECASE = 1 << 1, + BP_EXPLAIN = 1 << 2, + BP_JSON = 1 << 3, + BP_LISTFILES = 1 << 4, + BP_INPLACE = 1 << 5, }; /* - * BPEG virtual machine opcodes (these must be kept in sync with the names in vm.c) + * BP virtual machine opcodes (these must be kept in sync with the names in vm.c) */ enum VMOpcode { VM_ANYCHAR = 1, @@ -42,7 +42,7 @@ enum VMOpcode { }; /* - * A struct reperesenting a BPEG virtual machine operation + * A struct reperesenting a BP virtual machine operation */ typedef struct vm_op_s { enum VMOpcode op; diff --git a/utils.c b/utils.c index c2473d2..2572b33 100644 --- a/utils.c +++ b/utils.c @@ -43,7 +43,7 @@ const char *after_spaces(const char *str) } /* - * Return the first character after a valid BPEG name, or NULL if none is + * Return the first character after a valid BP name, or NULL if none is * found. */ const char *after_name(const char *str) diff --git a/vm.c b/vm.c index 40c783b..9412b0c 100644 --- a/vm.c +++ b/vm.c @@ -1,5 +1,5 @@ /* - * vm.c - Code for the BPEG virtual machine that performs the matching. + * vm.c - Code for the BP virtual machine that performs the matching. */ #include @@ -116,7 +116,7 @@ static match_t *_match(grammar_t *g, file_t *f, const char *str, vm_op_t *op, un } case VM_STRING: { if (&str[op->len] > f->end) return NULL; - if ((flags & BPEG_IGNORECASE) ? memicmp(str, op->args.s, (size_t)op->len) != 0 + if ((flags & BP_IGNORECASE) ? memicmp(str, op->args.s, (size_t)op->len) != 0 : memcmp(str, op->args.s, (size_t)op->len) != 0) return NULL; match_t *m = new(match_t); @@ -215,7 +215,7 @@ static match_t *_match(grammar_t *g, file_t *f, const char *str, vm_op_t *op, un } if (p->end == start && reps > 0) { // Since no forward progress was made on either `pat` or - // `sep` and BPEG does not have mutable state, it's + // `sep` and BP does not have mutable state, it's // guaranteed that no progress will be made on the next // loop either. We know that this will continue to loop // until reps==max, so let's just cut to the chase instead @@ -662,7 +662,7 @@ static match_t *match_backref(const char *str, vm_op_t *op, match_t *cap, unsign for (match_t *child = cap->child; child; child = child->nextsibling) { if (child->start > prev) { size_t len = (size_t)(child->start - prev); - if ((flags & BPEG_IGNORECASE) ? memicmp(str, prev, len) != 0 + if ((flags & BP_IGNORECASE) ? memicmp(str, prev, len) != 0 : memcmp(str, prev, len) != 0) { destroy_match(&ret); return NULL; @@ -682,7 +682,7 @@ static match_t *match_backref(const char *str, vm_op_t *op, match_t *cap, unsign } if (cap->end > prev) { size_t len = (size_t)(cap->end - prev); - if ((flags & BPEG_IGNORECASE) ? memicmp(str, prev, len) != 0 + if ((flags & BP_IGNORECASE) ? memicmp(str, prev, len) != 0 : memcmp(str, prev, len) != 0) { destroy_match(&ret); return NULL; diff --git a/vm.h b/vm.h index 842b782..758b9d5 100644 --- a/vm.h +++ b/vm.h @@ -1,5 +1,5 @@ /* - * vm.h - Header file for BPEG virtual machine. + * vm.h - Header file for BP virtual machine. */ #ifndef VM__H #define VM__H -- cgit v1.2.3