42 #include "vpKeyword.h" 53 #ifndef DOXYGEN_SHOULD_SKIP_THIS 55 static void count(
void);
56 static void next_source(
void);
58 void lexerr(
const char *path, ...);
103 #define isnult(c) (scantbl[c] == _NULT) 104 #define iscmtt(c) (scantbl[c] & _CMTT) 105 #define isfptt(c) (scantbl[c] & _FPTT) 106 #define isidnt(c) (scantbl[c] & _IDNT) 107 #define isintt(c) (scantbl[c] & _INTT) 108 #define issgnt(c) (scantbl[c] & _SGNT) 109 #define isstgt(c) (scantbl[c] & _STGT) 125 const char *lex_errtbl[] = {
128 "unexpected EOF in comment",
132 "unexpected EOF in string or char constant",
133 "newline in string or char constant",
140 unsigned int mycolumno = 0;
146 static char *lastline;
149 static Byte *scantbl;
157 if ((chtbl = (Byte *)malloc(CHAR_NBR *
sizeof(Byte))) == NULL ||
158 (scantbl = (Byte *)malloc(CHAR_NBR *
sizeof(Byte))) == NULL) {
159 static char proc_name[] =
"open_lex";
168 for (
int i = 0; i < ASCII_NBR; i++) {
207 for (
int i = 0; i < ASCII_NBR; i++) {
209 scantbl[i] = _CMTT | _IDNT | _STGT;
211 scantbl[i] = _CMTT | _IDNT | _INTT | _STGT;
219 scantbl[i] = _CMTT | _SGNT | _STGT;
222 scantbl[i] = _CMTT | _FPTT | _STGT;
228 scantbl[i] = _CMTT | _IDNT | _STGT;
231 scantbl[i] = _CMTT | _STGT;
238 scantbl[EOB] = _NULT;
239 scantbl[EOF] = _NULT;
240 scantbl[EOL] = _NULT;
249 free((
char *)(chtbl - 2));
250 free((
char *)(scantbl - 2));
253 #define ECHO printf("%c", *(mysptr)) 254 #define CURC (*((signed char *)mysptr)) 255 #define NEXTC (*((signed char *)mysptr + 1)) 256 #define PREVC (*((signed char *)mysptr - 1)) 270 for (; chtbl[(int)CURC] == SPCT; mysptr++) {
273 switch (chtbl[(
int)CURC]) {
289 if (mysptr == lastline)
304 for (; iscmtt((
int)CURC); mysptr++) {
306 switch (chtbl[(
int)CURC]) {
312 lexerr(
"start", lex_errtbl[E_CMT_EOF], NULL);
316 if (mysptr == lastline)
337 for (; isidnt((
int)CURC); mysptr++) {
339 mylength = (int)(mysptr - mytext);
340 return (get_symbol(mytext, mylength));
345 myint = (int)(CURC -
'0');
347 for (; isintt((
int)CURC); mysptr++)
348 myint = myint * 10 + (
int)(CURC -
'0');
353 for (; isintt((
int)CURC); mysptr++) {
355 if (CURC !=
'E' && CURC !=
'e') {
356 myfloat = (float)atof(mytext);
366 if (isintt((
int)CURC))
368 else if (issgnt((
int)CURC) && isintt((
int)NEXTC))
372 myfloat = (float)atof(mytext);
375 for (; isintt((
int)CURC); mysptr++) {
377 myfloat = (float)atof(mytext);
390 if (!isintt((
int)CURC))
397 if (isintt((
int)CURC))
399 if (isfptt((
int)CURC) && isintt((
int)NEXTC))
407 for (; isstgt((
int)CURC); mysptr++) {
409 switch (chtbl[(
int)CURC]) {
415 lexerr(
"start", lex_errtbl[E_STG_EOF], NULL);
419 lexerr(
"start", lex_errtbl[E_STG_EOL], NULL);
425 mylength = (int)(mysptr - mytext);
457 int lexecho(FILE *f,
int token)
460 for (; chtbl[(int)CURC] == SPCT; mysptr++)
461 fwrite(mysptr, 1, 1, f);
463 switch (chtbl[(
int)CURC]) {
468 if (token != *mytext)
469 fwrite(mytext, 1, 1, f);
481 fwrite(mysptr, 1, 1, f);
482 if (mysptr == lastline)
491 fwrite(mysptr, 1, 1, f);
496 fwrite(mysptr, 1, 1, f);
499 for (; iscmtt((
int)CURC); mysptr++)
500 fwrite(mysptr, 1, 1, f);
501 switch (chtbl[(
int)CURC]) {
507 lexerr(
"start", lex_errtbl[E_CMT_EOF], NULL);
511 fwrite(mysptr, 1, 1, f);
512 if (mysptr == lastline)
521 fwrite(mysptr, 1, 1, f);
534 for (; isidnt((
int)CURC); mysptr++) {
536 mylength = (int)(mysptr - mytext);
537 if (token != get_symbol(mytext, mylength))
538 fwrite(mytext, (
size_t)mylength, 1, f);
539 return (get_symbol(mytext, mylength));
545 for (; isintt((
int)CURC); mysptr++) {
551 for (; isintt((
int)CURC); mysptr++) {
553 if (CURC !=
'E' && CURC !=
'e') {
554 if (token != T_FLOAT)
555 fwrite(mytext, (
size_t)(mysptr - mytext), 1, f);
562 if (isintt((
int)CURC))
564 else if (issgnt((
int)CURC) && isintt((
int)NEXTC))
568 if (token != T_FLOAT)
569 fwrite(mytext, (
size_t)(mysptr - mytext), 1, f);
572 for (; isintt((
int)CURC); mysptr++) {
574 if (token != T_FLOAT)
575 fwrite(mytext, (
size_t)(mysptr - mytext), 1, f);
580 fwrite(mytext, (
size_t)(mysptr - mytext), 1, f);
588 if (!isintt((
int)CURC)) {
589 if (token != *mytext)
590 fwrite(mytext, 1, 1, f);
598 if (isintt((
int)CURC))
600 if (isfptt((
int)CURC) && isintt((
int)NEXTC))
602 if (token != *mytext)
603 fwrite(mytext, 1, 1, f);
607 fwrite(mysptr, 1, 1, f);
611 for (; isstgt((
int)CURC); mysptr++)
612 fwrite(mysptr, 1, 1, f);
613 switch (chtbl[(
int)CURC]) {
619 lexerr(
"start", lex_errtbl[E_STG_EOF], NULL);
623 lexerr(
"start", lex_errtbl[E_STG_EOL], NULL);
627 fwrite(mysptr, 1, 1, f);
630 mylength = (int)(mysptr - mytext);
640 fwrite(mysptr, 1, 1, f);
652 #define BUFSIZE (BUFSIZ << 5) 653 #define LINESIZE (BUFSIZ - 1) 654 #define TEXTSIZE (1 + LINESIZE + BUFSIZE + 1) 665 void unlex(
void) { mysptr = mytext; }
674 void open_source(FILE *fd,
const char *str)
676 if ((source = (
char *)malloc((strlen(str) + 1) *
sizeof(
char))) == NULL) {
677 static char proc_name[] =
"open_source";
682 if ((botbuf = (
char *)malloc(TEXTSIZE *
sizeof(
char))) == NULL) {
683 static char proc_name[] =
"open_source";
688 buf = botbuf + 1 + LINESIZE;
698 void close_source(
void)
700 free((
char *)source);
701 free((
char *)botbuf);
707 static void next_source(
void)
716 while ((*--bot = *--top) != EOL) {
718 myline = mysptr = bot + 1;
720 size = fread(buf,
sizeof(
char), BUFSIZE, fds);
732 while (*--top != EOL) {
743 #define ERR_STACK_MAX 32 745 static const char *err_stack[ERR_STACK_MAX];
746 static int size_stack = 0;
752 static void count(
void)
757 for (str = myline; str <= mytext; str++) {
758 (*str ==
'\t') ? mycolumno += 8 - (mycolumno % 8) : mycolumno++;
775 void lexerr(
const char *path, ...)
785 fprintf(stderr,
"\n%*c\n\"%s\", line %d:\n", mycolumno,
'^', source, mylineno);
789 for (i = 0; i < size_stack; i++)
790 fprintf(stderr,
"%s", err_stack[i]);
795 while ((cp = (
char *)va_arg(ap,
char *)) != NULL)
796 fprintf(stderr,
"%s", cp);
797 fprintf(stderr,
"\n");
808 if (--size_stack < 0) {
809 static char proc_name[] =
"poperr";
810 fprintf(stderr,
"%s: error stack underflow\n", proc_name);
818 void popuperr(
const char *str)
820 if (size_stack <= 0) {
821 static const char proc_name[] =
"popuerr";
822 fprintf(stderr,
"%s: error stack underflow\n", proc_name);
825 err_stack[size_stack - 1] = str;
831 void pusherr(
const char *str)
833 if (size_stack >= ERR_STACK_MAX) {
834 static const char proc_name[] =
"pusherr";
835 fprintf(stderr,
"%s: error stack overflow\n", proc_name);
838 err_stack[size_stack++] = str;