[brlcad-commits] SF.net SVN: brlcad:[47693] brlcad/trunk/src/other/perplex
Open Source Solid Modeling CAD
Brought to you by:
brlcad
From: <n_...@us...> - 2011-11-29 20:05:34
|
Revision: 47693 http://brlcad.svn.sourceforge.net/brlcad/?rev=47693&view=rev Author: n_reed Date: 2011-11-29 20:05:24 +0000 (Tue, 29 Nov 2011) Log Message: ----------- add ability to generate a header file Modified Paths: -------------- brlcad/trunk/src/other/perplex/parser.y brlcad/trunk/src/other/perplex/perplex.cpp brlcad/trunk/src/other/perplex/perplex.h brlcad/trunk/src/other/perplex/scanner.re brlcad/trunk/src/other/perplex/scanner_template.c Modified: brlcad/trunk/src/other/perplex/parser.y =================================================================== --- brlcad/trunk/src/other/perplex/parser.y 2011-11-29 20:04:11 UTC (rev 47692) +++ brlcad/trunk/src/other/perplex/parser.y 2011-11-29 20:05:24 UTC (rev 47693) @@ -4,7 +4,24 @@ #include <stdlib.h> #include "perplex.h" #include "token_type.h" + +static void +writeHeader(FILE *templateFile, FILE *headerFile) +{ + char c; + while ((c = fgetc(templateFile)) != EOF) { + if (c == '%') { + if ((c = fgetc(templateFile)) == '%') { + /* found %%, end of header section */ + return; + } else { + fprintf(headerFile, "%c", '%'); + } + } + fprintf(headerFile, "%c", c); + } } +} %token_type {YYSTYPE} %extra_argument {appData_t *appData} @@ -17,12 +34,31 @@ definitions_section ::= definitions. { FILE *templateFile = appData->scanner_template; + FILE *headerFile = appData->header; + FILE *outFile = appData->out; char c; + /* write header file from template */ + if (headerFile != NULL) { + fprintf(headerFile, "#ifndef PERPLEX_H\n"); + fprintf(headerFile, "#define PERPLEX_H\n"); + writeHeader(templateFile, headerFile); + fprintf(headerFile, "#endif\n"); + } + + /* write implementation file from template */ while ((c = fgetc(templateFile)) != EOF) { - fprintf(appData->out, "%c", c); + if (c == '%') { + if ((c = fgetc(templateFile)) == '%') { + /* found %%, ignore */ + continue; + } else { + fprintf(outFile, "%c", '%'); + } + } + fprintf(outFile, "%c", c); } - fprintf(appData->out, "\n"); + fprintf(outFile, "\n"); fclose(templateFile); } Modified: brlcad/trunk/src/other/perplex/perplex.cpp =================================================================== --- brlcad/trunk/src/other/perplex/perplex.cpp 2011-11-29 20:04:11 UTC (rev 47692) +++ brlcad/trunk/src/other/perplex/perplex.cpp 2011-11-29 20:05:24 UTC (rev 47693) @@ -76,6 +76,7 @@ { mbo_opt_struct('?', 0, "help"), mbo_opt_struct('h', 0, "help"), + mbo_opt_struct('i', 1, "header"), mbo_opt_struct('o', 1, "output"), mbo_opt_struct('t', 1, "template"), mbo_opt_struct('-', 0, NULL) @@ -85,6 +86,7 @@ "Usage: perplex [options] input\n" " -?\n" " -h, --help\t\tprints this message\n" +" -i, --header PATH\tspecify path of header file\n" " -o, --output\t\tspecify path of output file\n" " -t, --template PATH\tspecify path to scanner template file\n" ; @@ -99,7 +101,10 @@ perplex_t scanner; appData_t *appData; char defaultTemplate[] = "scanner_template.c"; - FILE *inFile, *outFile = NULL, *templateFile = NULL; + FILE *inFile; + FILE *outFile = NULL; + FILE *templateFile = NULL; + FILE *headerFile = NULL; if (argc < 2) { puts(usage); @@ -112,6 +117,16 @@ case 'h': puts(usage); return 0; + case 'i': + if (opt_arg == NULL) { + fprintf(stderr, "Error: Header option requires path argument.\n"); + exit(1); + } + if ((headerFile = fopen(opt_arg, "w")) == NULL) { + fprintf(stderr, "Error: Couldn't open \"%s\" for writing.\n", opt_arg); + exit(1); + } + break; case 'o': if (opt_arg == NULL) { fprintf(stderr, "Error: Output option requires path argument.\n"); @@ -164,6 +179,7 @@ appData = scanner->appData; appData->in = inFile; appData->out = outFile; + appData->header = headerFile; appData->scanner_template = templateFile; /* parse */ Modified: brlcad/trunk/src/other/perplex/perplex.h =================================================================== --- brlcad/trunk/src/other/perplex/perplex.h 2011-11-29 20:04:11 UTC (rev 47692) +++ brlcad/trunk/src/other/perplex/perplex.h 2011-11-29 20:05:24 UTC (rev 47693) @@ -69,6 +69,7 @@ typedef struct appData_t { FILE *in; FILE *out; + FILE *header; FILE *scanner_template; YYSTYPE tokenData; } appData_t; Modified: brlcad/trunk/src/other/perplex/scanner.re =================================================================== --- brlcad/trunk/src/other/perplex/scanner.re 2011-11-29 20:04:11 UTC (rev 47692) +++ brlcad/trunk/src/other/perplex/scanner.re 2011-11-29 20:05:24 UTC (rev 47693) @@ -79,8 +79,6 @@ #include <stdlib.h> #include <string.h> -#define YYEOF -1 - /* --- from flex's flexdef.h --- */ void buf_init(struct Buf * buf, size_t elem_size); void buf_destroy(struct Buf * buf); Modified: brlcad/trunk/src/other/perplex/scanner_template.c =================================================================== --- brlcad/trunk/src/other/perplex/scanner_template.c 2011-11-29 20:04:11 UTC (rev 47692) +++ brlcad/trunk/src/other/perplex/scanner_template.c 2011-11-29 20:05:24 UTC (rev 47693) @@ -48,10 +48,14 @@ * template for generated scanners * */ - #include <stdio.h> #include <stdlib.h> +#define YYEOF -1 + +#define PERPLEX_APPDATA_TYPE void* +#define PERPLEX_CONDITION_TYPE int + struct Buf { void *elts; /* elements. */ int nelts; /* number of elements. */ @@ -72,17 +76,67 @@ char *tokenStart; struct Buf *buffer; char *tokenText; - void *appData; - condition_t condition; + PERPLEX_APPDATA_TYPE appData; + PERPLEX_CONDITION_TYPE condition; } *perplex_t; +perplex_t perplexFileScanner(FILE *input); +perplex_t perplexStringScanner(char *input); +void perplexFree(perplex_t scanner); + +int yylex(perplex_t scanner); + +%% +/* S C A N N E R _ T E M P L A T E . C + * BRL-CAD + * + * Copyright (c) 1990-2011 United States Government as represented by + * the U.S. Army Research Laboratory. + * + * Copyright (c) 1990 The Regents of the University of California. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/* Parts of this file are based on sources from the flex project. + * + * This code is derived from software contributed to Berkeley by + * Vern Paxson. + * + * The United States Government has rights in this work pursuant + * to contract no. DE-AC03-76SF00098 between the United States + * Department of Energy and the University of California. + */ #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#define YYEOF -1 - /* --- from flex's flexdef.h --- */ void buf_init(struct Buf * buf, size_t elem_size); void buf_destroy(struct Buf * buf); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |