46 #include "vpKeyword.h"
55 #ifndef DOXYGEN_SHOULD_SKIP_THIS
57 static void count (
void);
58 static void next_source (
void);
60 void lexerr (
const char* path, ...);
105 #define isnult(c) (scantbl[c] == _NULT)
106 #define iscmtt(c) (scantbl[c] & _CMTT)
107 #define isfptt(c) (scantbl[c] & _FPTT)
108 #define isidnt(c) (scantbl[c] & _IDNT)
109 #define isintt(c) (scantbl[c] & _INTT)
110 #define issgnt(c) (scantbl[c] & _SGNT)
111 #define isstgt(c) (scantbl[c] & _STGT)
128 const char *lex_errtbl[] = {
131 "unexpected EOF in comment",
135 "unexpected EOF in string or char constant",
136 "newline in string or char constant",
144 unsigned int mycolumno = 0;
151 static char *lastline;
154 static Byte *scantbl;
163 if ((chtbl = (Byte *) malloc (CHAR_NBR *
sizeof (Byte))) == NULL
164 || (scantbl = (Byte *) malloc (CHAR_NBR *
sizeof (Byte))) == NULL) {
165 static char proc_name[] =
"open_lex";
174 for (
int i = 0; i < ASCII_NBR; i++) {
175 if (isalpha(i)) chtbl[i] = IDNT;
176 else if (isdigit(i)) chtbl[i] = INTT;
177 else if (isspace(i)) chtbl[i] = SPCT;
179 case '"' : chtbl[i] = STGT;
break;
181 case '-' : chtbl[i] = SGNT;
break;
182 case '.' : chtbl[i] = FPTT;
break;
183 case '/' : chtbl[i] = CMTT;
break;
184 case '_' : chtbl[i] = IDNT;
break;
185 default : chtbl[i] = NULT;
break;
197 for (
int i = 0; i < ASCII_NBR; i++) {
198 if (isalpha(i)) scantbl[i] = _CMTT|_IDNT|_STGT;
199 else if (isdigit(i)) scantbl[i] = _CMTT|_IDNT|_INTT|_STGT;
201 case '"' : scantbl[i] = _CMTT;
break;
203 case '-' : scantbl[i] = _CMTT|_SGNT|_STGT;
break;
204 case '.' : scantbl[i] = _CMTT|_FPTT|_STGT;
break;
205 case '/' : scantbl[i] = _STGT;
break;
206 case '_' : scantbl[i] = _CMTT|_IDNT|_STGT;
break;
207 default : scantbl[i] = _CMTT|_STGT;
break;
213 scantbl[EOB] = _NULT;
214 scantbl[EOF] = _NULT;
215 scantbl[EOL] = _NULT;
222 void close_lex (
void)
224 free ((
char *) (chtbl - 2));
225 free ((
char *) (scantbl - 2));
229 #define ECHO printf ("%c", *(mysptr))
230 #define CURC (*((signed char *)mysptr))
231 #define NEXTC (*((signed char *)mysptr+1))
232 #define PREVC (*((signed char *)mysptr-1))
248 for (; chtbl[(int)CURC] == SPCT; mysptr++) {};
250 switch (chtbl[(
int)CURC]) {
266 if (mysptr == lastline) next_source ();
279 for (; iscmtt((
int)CURC); mysptr++) {};
280 switch (chtbl[(
int)CURC]) {
286 lexerr (
"start", lex_errtbl[E_CMT_EOF], NULL);
290 if (mysptr == lastline) next_source ();
309 for (; isidnt((
int)CURC); mysptr++) {};
310 mylength = (int)(mysptr - mytext);
311 return (get_symbol (mytext, mylength));
316 myint = (int) (CURC -
'0');
318 for (; isintt((
int)CURC); mysptr++)
319 myint = myint * 10 + (
int) (CURC -
'0');
324 for (; isintt((
int)CURC); mysptr++) {};
325 if (CURC !=
'E' && CURC !=
'e') {
326 myfloat = (float) atof (mytext);
336 if (isintt((
int)CURC))
338 else if (issgnt((
int)CURC) && isintt((
int)NEXTC))
342 myfloat = (float) atof (mytext);
345 for (; isintt((
int)CURC); mysptr++) {};
346 myfloat = (float) atof (mytext);
359 if (! isintt((
int)CURC))
366 if (isintt((
int)CURC))
368 if (isfptt((
int)CURC) && isintt((
int)NEXTC))
376 for (; isstgt((
int)CURC); mysptr++) {};
377 switch (chtbl[(
int)CURC]) {
383 lexerr (
"start", lex_errtbl[E_STG_EOF], NULL);
387 lexerr (
"start", lex_errtbl[E_STG_EOL], NULL);
393 mylength = (int)(mysptr - mytext);
426 int lexecho (FILE *f,
int token)
429 for (; chtbl[(int)CURC] == SPCT; mysptr++)
430 fwrite (mysptr, 1, 1, f);
432 switch (chtbl[(
int)CURC]) {
437 if (token != *mytext)
438 fwrite (mytext, 1, 1, f);
450 fwrite (mysptr, 1, 1, f);
451 if (mysptr == lastline) next_source ();
458 fwrite (mysptr, 1, 1, f);
463 fwrite (mysptr, 1, 1, f);
466 for (; iscmtt((
int)CURC); mysptr++)
467 fwrite (mysptr, 1, 1, f);
468 switch (chtbl[(
int)CURC]) {
474 lexerr (
"start", lex_errtbl[E_CMT_EOF], NULL);
478 fwrite (mysptr, 1, 1, f);
479 if (mysptr == lastline) next_source ();
486 fwrite (mysptr, 1, 1, f);
499 for (; isidnt((
int)CURC); mysptr++) {};
500 mylength = (int)(mysptr - mytext);
501 if (token != get_symbol (mytext, mylength))
502 fwrite (mytext, (
size_t)mylength, 1, f);
503 return (get_symbol (mytext, mylength));
509 for (; isintt((
int)CURC); mysptr++) {};
514 for (; isintt((
int)CURC); mysptr++) {};
515 if (CURC !=
'E' && CURC !=
'e') {
516 if (token != T_FLOAT)
517 fwrite (mytext, (
size_t)(mysptr - mytext), 1, f);
524 if (isintt((
int)CURC)) mysptr++;
525 else if (issgnt((
int)CURC) && isintt((
int)NEXTC)) mysptr +=2;
528 if (token != T_FLOAT)
529 fwrite (mytext, (
size_t)(mysptr - mytext), 1, f);
532 for (; isintt((
int)CURC); mysptr++) {};
533 if (token != T_FLOAT)
534 fwrite (mytext, (
size_t)(mysptr - mytext), 1, f);
539 fwrite (mytext, (
size_t)(mysptr - mytext), 1, f);
547 if (! isintt((
int)CURC)) {
548 if (token != *mytext)
549 fwrite (mytext, 1, 1, f);
557 if (isintt((
int)CURC))
goto int_part;
558 if (isfptt((
int)CURC) && isintt((
int)NEXTC))
goto float_part;
559 if (token != *mytext)
560 fwrite (mytext, 1, 1, f);
564 fwrite (mysptr, 1, 1, f);
568 for (; isstgt((
int)CURC); mysptr++)
569 fwrite (mysptr, 1, 1, f);
570 switch (chtbl[(
int)CURC]) {
576 lexerr (
"start", lex_errtbl[E_STG_EOF], NULL);
580 lexerr (
"start", lex_errtbl[E_STG_EOL], NULL);
584 fwrite (mysptr, 1, 1, f);
587 mylength = (int)(mysptr - mytext);
597 fwrite (mysptr, 1, 1, f);
610 #define BUFSIZE (BUFSIZ << 5)
611 #define LINESIZE (BUFSIZ-1)
612 #define TEXTSIZE (1 + LINESIZE + BUFSIZE + 1)
637 void open_source (FILE *fd,
const char *str)
639 if ((source = (
char *) malloc((strlen(str)+1)*
sizeof(
char))) == NULL) {
640 static char proc_name[] =
"open_source";
644 strcpy (source, str);
645 if ((botbuf = (
char *) malloc (TEXTSIZE *
sizeof (
char))) == NULL) {
646 static char proc_name[] =
"open_source";
651 buf = botbuf + 1 + LINESIZE;
661 void close_source (
void)
663 free ((
char *) source);
664 free ((
char *) botbuf);
670 static void next_source (
void)
679 while ((*--bot = *--top) != EOL) {};
680 myline = mysptr = bot + 1;
682 size = fread (buf,
sizeof (
char), BUFSIZE,fds);
695 while (*--top != EOL) {};
706 #define ERR_STACK_MAX 32
709 static const char *err_stack[ERR_STACK_MAX];
710 static int size_stack = 0;
717 static void count (
void)
722 for (str = myline; str <= mytext; str++) {
723 (*str ==
'\t') ? mycolumno += 8 - (mycolumno % 8) : mycolumno++;
740 void lexerr (
const char* path, ...)
750 fprintf (stderr,
"\n%*c\n\"%s\", line %d:\n",
751 mycolumno,
'^', source, mylineno);
755 for (i = 0; i < size_stack; i++)
756 fprintf (stderr,
"%s", err_stack[i]);
761 while ((cp = (
char *) va_arg(ap,
char *)) != NULL)
762 fprintf (stderr,
"%s", cp);
763 fprintf (stderr,
"\n");
774 if (--size_stack < 0) {
775 static char proc_name[] =
"poperr";
776 fprintf (stderr,
"%s: error stack underflow\n", proc_name);
784 void popuperr (
const char *str)
786 if (size_stack <= 0) {
787 static const char proc_name[] =
"popuerr";
788 fprintf (stderr,
"%s: error stack underflow\n", proc_name);
791 err_stack[size_stack-1] = str;
797 void pusherr (
const char *str)
799 if (size_stack >= ERR_STACK_MAX) {
800 static const char proc_name[] =
"pusherr";
801 fprintf (stderr,
"%s: error stack overflow\n", proc_name);
804 err_stack[size_stack++] = str;