Skip to content

Commit

Permalink
Squashed 'misc/packcc/' changes from 8fad6d2f..03402b79
Browse files Browse the repository at this point in the history
03402b79 Merge pull request #4 from universal-ctags/introduce--i-option
e2f11828 Introduce -i option for putting an #include directive earlier
3fb1a196 Add .editorconfig

git-subtree-dir: misc/packcc
git-subtree-split: 03402b79505dc0024f90d5bebfd7e5d3fb62da9a
  • Loading branch information
masatake committed Jun 28, 2020
1 parent d66b256 commit d1c4b9a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true

[packcc.c]
indent_size = 4
indent_style = space
trim_trailing_whitespace = true
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ The base name of the parser source files can be changed by -o option.
`packcc -o parser example.peg`

By running this, the parser source parser.h and parser.c are generated.

-i option is for putting an #include directive at the head of example.c.

`packcc -i '<early_cdef.h>' example.peg`

If you want to confirm the version of the packcc command, execute the below.

`packcc -v`
Expand Down Expand Up @@ -196,7 +201,7 @@ The specified C source code is copied verbatim to the C header file before the g

- `%source { c source code }`

The specified C source code is copied verbatim to the C source file before the generated parser implementation code.
The specified C source code is copied verbatim to the C source file before the generated parser implementation code but after including basic header files like stdio.h. Use -i option if you want to put your code at the head of the C source file.

- `%common { c source code }`

Expand Down
38 changes: 36 additions & 2 deletions packcc.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ typedef struct context_tag {
char_array_t buffer;
node_array_t rules;
node_hash_table_t rulehash;
char *earlyinclude;
} context_t;

typedef struct generate_tag {
Expand Down Expand Up @@ -764,7 +765,7 @@ static void node_const_array__term(node_const_array_t *array) {
free((node_t **)array->buf);
}

static context_t *create_context(const char *iname, const char *oname, bool debug) {
static context_t *create_context(const char *iname, const char *earlyinclude, const char *oname, bool debug) {
context_t *ctx = (context_t *)malloc_e(sizeof(context_t));
ctx->iname = strdup_e((iname && iname[0]) ? iname : "-");
ctx->sname = (oname && oname[0]) ? add_fileext(oname, "c") : replace_fileext(ctx->iname, "c");
Expand All @@ -786,6 +787,7 @@ static context_t *create_context(const char *iname, const char *oname, bool debu
ctx->rulehash.mod = 0;
ctx->rulehash.max = 0;
ctx->rulehash.buf = NULL;
ctx->earlyinclude = earlyinclude? strdup_e(earlyinclude): NULL;
return ctx;
}

Expand Down Expand Up @@ -932,6 +934,7 @@ static void destroy_context(context_t *ctx) {
free(ctx->hname);
free(ctx->sname);
free(ctx->iname);
free(ctx->earlyinclude);
free(ctx);
}

Expand Down Expand Up @@ -1974,6 +1977,11 @@ static bool parse_directive_string_(context_t *ctx, const char *name, char **out
static bool parse(context_t *ctx) {
fprintf(ctx->sfile, "/* A packrat parser generated by PackCC %s */\n\n", VERSION);
fprintf(ctx->hfile, "/* A packrat parser generated by PackCC %s */\n\n", VERSION);
if (ctx->earlyinclude) {
fputs("#include ", ctx->sfile);
fputs(ctx->earlyinclude, ctx->sfile);
fputc('\n', ctx->sfile);
}
{
fputs(
"#ifdef _MSC_VER\n"
Expand Down Expand Up @@ -3967,6 +3975,7 @@ static void print_usage(FILE *output) {
fprintf(output, "Generates a packrat parser for C.\n");
fprintf(output, "\n");
fprintf(output, " -o BASENAME specify a base name of output source and header files\n");
fprintf(output, " -i INCLUDE put \"#include INCLUDE\" at the head of the source file\n");
fprintf(output, " -d with debug information\n");
fprintf(output, " -h print this help message and exit\n");
fprintf(output, " -v print the version and exit\n");
Expand All @@ -3975,6 +3984,7 @@ static void print_usage(FILE *output) {
int main(int argc, char **argv) {
const char *iname = NULL;
const char *oname = NULL;
const char *earlyinclude = NULL;
bool debug = false;
#ifdef _MSC_VER
#ifdef _DEBUG
Expand All @@ -3987,6 +3997,7 @@ int main(int argc, char **argv) {
{
const char *fname = NULL;
const char *opt_o = NULL;
const char *opt_i = NULL;
bool opt_d = false;
bool opt_h = false;
bool opt_v = false;
Expand Down Expand Up @@ -4014,6 +4025,28 @@ int main(int argc, char **argv) {
}
opt_o = o;
}
else if (strcmp(argv[i] + 1, "i") == 0) {
const char *e = (argv[i][2] != '\0') ? argv[i] + 2 : (++i < argc) ? argv[i] : NULL;
if (e == NULL) {
print_error("Argument of #include missing\n");
fprintf(stderr, "\n");
print_usage(stderr);
exit(1);
}
if (opt_i != NULL) {
print_error("Extra argument of #include '%s'\n", e);
fprintf(stderr, "\n");
print_usage(stderr);
exit(1);
}
size_t l = strlen(e);
if ((e[0] != '"' && e[0] != '<') || (e[l - 1] != '"' && e[l - 1] != '>')) {
print_error("-i option expects \"FILENAME\" or <FILENAME>: '%s'\n", e);
fprintf(stderr, "\n");
exit(1);
}
opt_i = e;
}
else if (strcmp(argv[i] + 1, "d") == 0) {
opt_d = true;
}
Expand Down Expand Up @@ -4050,10 +4083,11 @@ int main(int argc, char **argv) {
}
iname = (fname != NULL && fname[0] != '\0') ? fname : NULL;
oname = (opt_o != NULL && opt_o[0] != '\0') ? opt_o : NULL;
earlyinclude = opt_i;
debug = opt_d;
}
{
context_t *ctx = create_context(iname, oname, debug);
context_t *ctx = create_context(iname, earlyinclude, oname, debug);
int b = parse(ctx) && generate(ctx);
destroy_context(ctx);
if (!b) exit(10);
Expand Down

0 comments on commit d1c4b9a

Please sign in to comment.