From bc73e25a54ba4bd127ebb5000241253030dcfa2e Mon Sep 17 00:00:00 2001 From: John Kerl Date: Wed, 13 Jul 2016 21:22:54 -0400 Subject: [PATCH] lemon refactor iterate --- c/dsls/Makefile.am | 4 ++ c/dsls/Makefile.no-autoconfig | 4 +- c/dsls/lemon.c | 93 +---------------------------------- c/dsls/lemon_assert.c | 8 +++ c/dsls/lemon_assert.h | 11 +++++ c/dsls/lemon_error.c | 73 +++++++++++++++++++++++++++ c/dsls/lemon_error.h | 7 +++ c/dsls/lemon_option.c | 53 +++++++++----------- 8 files changed, 130 insertions(+), 123 deletions(-) create mode 100644 c/dsls/lemon_assert.c create mode 100644 c/dsls/lemon_assert.h create mode 100644 c/dsls/lemon_error.c create mode 100644 c/dsls/lemon_error.h diff --git a/c/dsls/Makefile.am b/c/dsls/Makefile.am index 32578df9c..8f2d7ea7e 100644 --- a/c/dsls/Makefile.am +++ b/c/dsls/Makefile.am @@ -3,6 +3,10 @@ AM_CFLAGS= -std=gnu99 noinst_PROGRAMS= lemon lemon_SOURCES= lemon.c \ + lemon_assert.c \ + lemon_assert.h \ + lemon_error.c \ + lemon_error.h \ lemon_option.c \ lemon_option.h diff --git a/c/dsls/Makefile.no-autoconfig b/c/dsls/Makefile.no-autoconfig index a398baf44..139480de0 100644 --- a/c/dsls/Makefile.no-autoconfig +++ b/c/dsls/Makefile.no-autoconfig @@ -105,8 +105,8 @@ ex2_lexer.c ex2_lexer.h: ex2_lexer.l ./ex_ast.h flex --prefix=ex2_lexer_ --outfile=ex2_lexer.c --header-file=ex2_lexer.h ex2_lexer.l # ---------------------------------------------------------------- -lemon: lemon.c lemon_option.c - $(DSLCC) -o lemon lemon.c lemon_option.c +lemon: lemon.c lemon_assert.c lemon_error.c lemon_option.c + $(DSLCC) -o lemon lemon.c lemon_assert.c lemon_error.c lemon_option.c # ---------------------------------------------------------------- clean: diff --git a/c/dsls/lemon.c b/c/dsls/lemon.c index bbce0c1ce..32a921395 100644 --- a/c/dsls/lemon.c +++ b/c/dsls/lemon.c @@ -13,6 +13,8 @@ #include #include +#include "lemon_assert.h" +#include "lemon_error.h" #include "lemon_option.h" #ifndef __WIN32__ @@ -37,14 +39,6 @@ extern void *malloc(); struct action *Action_new(); struct action *Action_sort(); -/********* From the file "assert.h" ************************************/ -void myassert(); -#ifndef NDEBUG -# define assert(X) if(!(X))myassert(__FILE__,__LINE__) -#else -# define assert(X) -#endif - /********** From the file "build.h" ************************************/ void FindRulePrecedences(); void FindFirstSets(); @@ -65,9 +59,6 @@ struct config *Configlist_basis(/* void */); void Configlist_eat(/* struct config * */); void Configlist_reset(/* void */); -/********* From the file "error.h" ***************************************/ -void ErrorMsg(const char *, int,const char *, ...); - /******** From the file "parse.h" *****************************************/ int Parse(/* struct lemon *lemp */); @@ -538,16 +529,6 @@ int acttab_insert(acttab *p) { return i - p->mnLookahead; } -/********************** From the file "assert.c" ****************************/ -/* -** A more efficient way of handling assertions. -*/ -void myassert(char *file, int line) -{ - fprintf(stderr,"Assertion failed on line %d of file \"%s\"\n",line,file); - exit(1); -} - /********************** From the file "build.c" *****************************/ /* ** Routines to construction the finite state machine for the LEMON @@ -1203,76 +1184,6 @@ void Configlist_eat(struct config *cfp) return; } -/***************** From the file "error.c" *********************************/ -/* -** Code for printing error message. -*/ - -/* Find a good place to break "msg" so that its length is at least "min" -** but no more than "max". Make the point as close to max as possible. -*/ -static int findbreak(char *msg, int min, int max) -{ - int i,spot; - char c; - for(i=spot=min; i<=max; i++){ - c = msg[i]; - if (c=='\t') msg[i] = ' '; - if (c=='\n') { msg[i] = ' '; spot = i; break; } - if (c==0) { spot = i; break; } - if (c=='-' && i0) { - sprintf(prefix,"%.*s:%d: ",PREFIXLIMIT-10,filename,lineno); - } else { - sprintf(prefix,"%.*s: ",PREFIXLIMIT-10,filename); - } - prefixsize = strlen(prefix); - availablewidth = LINEWIDTH - prefixsize; - - /* Generate the error message */ - vsprintf(errmsg,format,ap); - va_end(ap); - errmsgsize = strlen(errmsg); - /* Remove trailing '\n's from the error message. */ - while (errmsgsize>0 && errmsg[errmsgsize-1]=='\n') { - errmsg[--errmsgsize] = 0; - } - - /* Print the error message */ - base = 0; - while (errmsg[base]!=0) { - end = restart = findbreak(&errmsg[base],0,availablewidth); - restart += base; - while (errmsg[restart]==' ') restart++; - fprintf(stdout,"%s%.*s\n",prefix,end,&errmsg[base]); - base = restart; - } -} - /**************** From the file "main.c" ************************************/ /* ** Main program file for the LEMON parser generator. diff --git a/c/dsls/lemon_assert.c b/c/dsls/lemon_assert.c new file mode 100644 index 000000000..e3e1c6f99 --- /dev/null +++ b/c/dsls/lemon_assert.c @@ -0,0 +1,8 @@ +#include +#include +#include "lemon_assert.h" + +void lemon_assert(char *file, int line) { + fprintf(stderr,"Assertion failed on line %d of file \"%s\"\n", line, file); + exit(1); +} diff --git a/c/dsls/lemon_assert.h b/c/dsls/lemon_assert.h new file mode 100644 index 000000000..c522997f2 --- /dev/null +++ b/c/dsls/lemon_assert.h @@ -0,0 +1,11 @@ +#ifndef LEMON_ASSERT_H +#define LEMON_ASSERT_H + +void lemon_assert(); +#ifndef NDEBUG +# define assert(X) if(!(X))lemon_assert(__FILE__,__LINE__) +#else +# define assert(X) +#endif + +#endif // LEMON_ASSERT_H diff --git a/c/dsls/lemon_error.c b/c/dsls/lemon_error.c new file mode 100644 index 000000000..386a5e47e --- /dev/null +++ b/c/dsls/lemon_error.c @@ -0,0 +1,73 @@ +#include +#include +#include "lemon_error.h" + +/***************** From the file "error.c" *********************************/ +/* +** Code for printing error message. +*/ + +/* Find a good place to break "msg" so that its length is at least "min" +** but no more than "max". Make the point as close to max as possible. +*/ +static int findbreak(char *msg, int min, int max) +{ + int i,spot; + char c; + for(i=spot=min; i<=max; i++){ + c = msg[i]; + if (c=='\t') msg[i] = ' '; + if (c=='\n') { msg[i] = ' '; spot = i; break; } + if (c==0) { spot = i; break; } + if (c=='-' && i0) { + sprintf(prefix,"%.*s:%d: ",PREFIXLIMIT-10,filename,lineno); + } else { + sprintf(prefix,"%.*s: ",PREFIXLIMIT-10,filename); + } + prefixsize = strlen(prefix); + availablewidth = LINEWIDTH - prefixsize; + + /* Generate the error message */ + vsprintf(errmsg,format,ap); + va_end(ap); + errmsgsize = strlen(errmsg); + /* Remove trailing '\n's from the error message. */ + while (errmsgsize>0 && errmsg[errmsgsize-1]=='\n') { + errmsg[--errmsgsize] = 0; + } + + /* Print the error message */ + base = 0; + while (errmsg[base]!=0) { + end = restart = findbreak(&errmsg[base],0,availablewidth); + restart += base; + while (errmsg[restart]==' ') restart++; + fprintf(stdout,"%s%.*s\n",prefix,end,&errmsg[base]); + base = restart; + } +} diff --git a/c/dsls/lemon_error.h b/c/dsls/lemon_error.h new file mode 100644 index 000000000..3b47fe045 --- /dev/null +++ b/c/dsls/lemon_error.h @@ -0,0 +1,7 @@ +#ifndef LEMON_ERROR_H +#define LEMON_ERROR_H +#include "stdarg.h" + +void ErrorMsg(const char *, int,const char *, ...); + +#endif // LEMON_ERROR_H diff --git a/c/dsls/lemon_option.c b/c/dsls/lemon_option.c index 31cd6bf4c..9bb8dc3b9 100644 --- a/c/dsls/lemon_option.c +++ b/c/dsls/lemon_option.c @@ -10,12 +10,10 @@ static FILE *errstream; #define ISOPT(X) ((X)[0]=='-'||(X)[0]=='+'||strchr((X),'=')!=0) -/* -** Print the command line with a carrot pointing to the k-th character -** of the n-th field. -*/ -static void errline(int n, int k, FILE *err) -{ +// ---------------------------------------------------------------- +// Print the command line with a caret pointing to the k-th character +// of the nth field. +static void errline(int n, int k, FILE *err) { int spcnt, i; spcnt = 0; if (argv[0]) fprintf(err,"%s",argv[0]); @@ -33,12 +31,10 @@ static void errline(int n, int k, FILE *err) } } -/* -** Return the index of the N-th non-switch argument. Return -1 -** if N is out of range. -*/ -static int argindex(int n) -{ +// ---------------------------------------------------------------- +// Return the index of the N-th non-switch argument. Return -1 +// if N is out of range. +static int argindex(int n) { int i; int dashdash = 0; if (argv!=0 && *argv!=0) { @@ -55,11 +51,9 @@ static int argindex(int n) static char emsg[] = "Command line syntax error: "; -/* -** Process a flag command line argument. -*/ -static int handleflags(int i, FILE *err) -{ +// ---------------------------------------------------------------- +// Process a flag command-line argument. +static int handleflags(int i, FILE *err) { int v; int errcnt = 0; int j; @@ -89,11 +83,9 @@ static int handleflags(int i, FILE *err) return errcnt; } -/* -** Process a command line switch which has an argument. -*/ -static int handleswitch(int i, FILE *err) -{ +// ---------------------------------------------------------------- +// Process a command-line switch which has an argument. +static int handleswitch(int i, FILE *err) { int lv = 0; double dv = 0.0; char *sv = 0, *end; @@ -177,8 +169,8 @@ static int handleswitch(int i, FILE *err) return errcnt; } -int OptInit(char **a, struct s_options *o, FILE *err) -{ +// ---------------------------------------------------------------- +int OptInit(char **a, struct s_options *o, FILE *err) { int errcnt = 0; argv = a; op = o; @@ -201,6 +193,7 @@ int OptInit(char **a, struct s_options *o, FILE *err) return 0; } +// ---------------------------------------------------------------- int OptNArgs() { int cnt = 0; int dashdash = 0; @@ -214,21 +207,22 @@ int OptNArgs() { return cnt; } -char *OptArg(int n) -{ +// ---------------------------------------------------------------- +char *OptArg(int n) { int i; i = argindex(n); return i>=0 ? argv[i] : 0; } -void OptErr(int n) -{ +// ---------------------------------------------------------------- +void OptErr(int n) { int i; i = argindex(n); if (i>=0) errline(i,0,errstream); } -void OptPrint(){ +// ---------------------------------------------------------------- +void OptPrint() { int i; int max, len; max = 0; @@ -277,4 +271,3 @@ void OptPrint(){ } } } -