47 #include <visp/vpMy.h>
48 #include <visp/vpToken.h>
49 #include <visp/vpKeyword.h>
63 #ifndef DOXYGEN_SHOULD_SKIP_THIS
66 static void count (
void);
67 static void next_source (
void);
69 void lexerr (
const char* path, ...);
114 #define isnult(c) (scantbl[c] == _NULT)
115 #define iscmtt(c) (scantbl[c] & _CMTT)
116 #define isfptt(c) (scantbl[c] & _FPTT)
117 #define isidnt(c) (scantbl[c] & _IDNT)
118 #define isintt(c) (scantbl[c] & _INTT)
119 #define issgnt(c) (scantbl[c] & _SGNT)
120 #define isstgt(c) (scantbl[c] & _STGT)
137 const char *lex_errtbl[] = {
140 "unexpected EOF in comment",
144 "unexpected EOF in string or char constant",
145 "newline in string or char constant",
160 static char *lastline;
163 static Byte *scantbl;
172 static char proc_name[] =
"open_lex";
176 if ((chtbl = (Byte *) malloc (CHAR_NBR *
sizeof (Byte))) == NULL
177 || (scantbl = (Byte *) malloc (CHAR_NBR *
sizeof (Byte))) == NULL) {
186 for (i = 0; i < ASCII_NBR; i++) {
187 if (isalpha(i)) chtbl[i] = IDNT;
188 else if (isdigit(i)) chtbl[i] = INTT;
189 else if (isspace(i)) chtbl[i] = SPCT;
191 case '"' : chtbl[i] = STGT;
break;
193 case '-' : chtbl[i] = SGNT;
break;
194 case '.' : chtbl[i] = FPTT;
break;
195 case '/' : chtbl[i] = CMTT;
break;
196 case '_' : chtbl[i] = IDNT;
break;
197 default : chtbl[i] = NULT;
break;
209 for (i = 0; i < ASCII_NBR; i++) {
210 if (isalpha(i)) scantbl[i] = _CMTT|_IDNT|_STGT;
211 else if (isdigit(i)) scantbl[i] = _CMTT|_IDNT|_INTT|_STGT;
213 case '"' : scantbl[i] = _CMTT;
break;
215 case '-' : scantbl[i] = _CMTT|_SGNT|_STGT;
break;
216 case '.' : scantbl[i] = _CMTT|_FPTT|_STGT;
break;
217 case '/' : scantbl[i] = _STGT;
break;
218 case '_' : scantbl[i] = _CMTT|_IDNT|_STGT;
break;
219 default : scantbl[i] = _CMTT|_STGT;
break;
225 scantbl[EOB] = _NULT;
226 scantbl[EOF] = _NULT;
227 scantbl[EOL] = _NULT;
234 void close_lex (
void)
236 free ((
char *) (chtbl - 2));
237 free ((
char *) (scantbl - 2));
241 #define ECHO printf ("%c", *(mysptr))
242 #define CURC (*((signed char *)mysptr))
243 #define NEXTC (*((signed char *)mysptr+1))
244 #define PREVC (*((signed char *)mysptr-1))
260 for (; chtbl[(int)CURC] == SPCT; mysptr++) {};
262 switch (chtbl[(
int)CURC]) {
278 if (mysptr == lastline) next_source ();
291 for (; iscmtt((
int)CURC); mysptr++) {};
292 switch (chtbl[(
int)CURC]) {
298 lexerr (
"start", lex_errtbl[E_CMT_EOF], NULL);
302 if (mysptr == lastline) next_source ();
321 for (; isidnt((
int)CURC); mysptr++) {};
322 mylength = (int)(mysptr - mytext);
323 return (get_symbol (mytext, mylength));
328 myint = (int) (CURC -
'0');
330 for (; isintt((
int)CURC); mysptr++)
331 myint = myint * 10 + (
int) (CURC -
'0');
336 for (; isintt((
int)CURC); mysptr++) {};
337 if (CURC !=
'E' && CURC !=
'e') {
338 myfloat = (float) atof (mytext);
348 if (isintt((
int)CURC))
350 else if (issgnt((
int)CURC) && isintt((
int)NEXTC))
354 myfloat = (float) atof (mytext);
357 for (; isintt((
int)CURC); mysptr++) {};
358 myfloat = (float) atof (mytext);
371 if (! isintt((
int)CURC))
378 if (isintt((
int)CURC))
380 if (isfptt((
int)CURC) && isintt((
int)NEXTC))
388 for (; isstgt((
int)CURC); mysptr++) {};
389 switch (chtbl[(
int)CURC]) {
395 lexerr (
"start", lex_errtbl[E_STG_EOF], NULL);
399 lexerr (
"start", lex_errtbl[E_STG_EOL], NULL);
405 mylength = (int)(mysptr - mytext);
438 int lexecho (FILE *f,
int token)
441 for (; chtbl[(int)CURC] == SPCT; mysptr++)
442 fwrite (mysptr, 1, 1, f);
444 switch (chtbl[(
int)CURC]) {
449 if (token != *mytext)
450 fwrite (mytext, 1, 1, f);
462 fwrite (mysptr, 1, 1, f);
463 if (mysptr == lastline) next_source ();
470 fwrite (mysptr, 1, 1, f);
475 fwrite (mysptr, 1, 1, f);
478 for (; iscmtt((
int)CURC); mysptr++)
479 fwrite (mysptr, 1, 1, f);
480 switch (chtbl[(
int)CURC]) {
486 lexerr (
"start", lex_errtbl[E_CMT_EOF], NULL);
490 fwrite (mysptr, 1, 1, f);
491 if (mysptr == lastline) next_source ();
498 fwrite (mysptr, 1, 1, f);
511 for (; isidnt((
int)CURC); mysptr++) {};
512 mylength = (int)(mysptr - mytext);
513 if (token != get_symbol (mytext, mylength))
514 fwrite (mytext, mylength, 1, f);
515 return (get_symbol (mytext, mylength));
521 for (; isintt((
int)CURC); mysptr++) {};
526 for (; isintt((
int)CURC); mysptr++) {};
527 if (CURC !=
'E' && CURC !=
'e') {
528 if (token != T_FLOAT)
529 fwrite (mytext, mysptr - mytext, 1, f);
536 if (isintt((
int)CURC)) mysptr++;
537 else if (issgnt((
int)CURC) && isintt((
int)NEXTC)) mysptr +=2;
540 if (token != T_FLOAT)
541 fwrite (mytext, mysptr - mytext, 1, f);
544 for (; isintt((
int)CURC); mysptr++) {};
545 if (token != T_FLOAT)
546 fwrite (mytext, mysptr - mytext, 1, f);
551 fwrite (mytext, mysptr - mytext, 1, f);
559 if (! isintt((
int)CURC)) {
560 if (token != *mytext)
561 fwrite (mytext, 1, 1, f);
569 if (isintt((
int)CURC))
goto int_part;
570 if (isfptt((
int)CURC) && isintt((
int)NEXTC))
goto float_part;
571 if (token != *mytext)
572 fwrite (mytext, 1, 1, f);
576 fwrite (mysptr, 1, 1, f);
580 for (; isstgt((
int)CURC); mysptr++)
581 fwrite (mysptr, 1, 1, f);
582 switch (chtbl[(
int)CURC]) {
588 lexerr (
"start", lex_errtbl[E_STG_EOF], NULL);
592 lexerr (
"start", lex_errtbl[E_STG_EOL], NULL);
596 fwrite (mysptr, 1, 1, f);
599 mylength = (int)(mysptr - mytext);
609 fwrite (mysptr, 1, 1, f);
622 #define BUFSIZE (BUFSIZ << 5)
623 #define LINESIZE (BUFSIZ-1)
624 #define TEXTSIZE (1 + LINESIZE + BUFSIZE + 1)
649 void open_source (FILE *fd,
const char *str)
651 static char proc_name[] =
"open_source";
653 if ((source = (
char *) malloc((strlen(str)+1)*
sizeof(
char))) == NULL) {
657 strcpy (source, str);
658 if ((botbuf = (
char *) malloc (TEXTSIZE *
sizeof (
char))) == NULL) {
663 buf = botbuf + 1 + LINESIZE;
673 void close_source (
void)
675 free ((
char *) source);
676 free ((
char *) botbuf);
682 static void next_source (
void)
691 while ((*--bot = *--top) != EOL) {};
692 myline = mysptr = bot + 1;
694 size = fread (buf,
sizeof (
char), BUFSIZE,fds);
707 while (*--top != EOL) {};
718 #define ERR_STACK_MAX 32
721 static const char *err_stack[ERR_STACK_MAX];
722 static int size_stack = 0;
729 static void count (
void)
734 for (str = myline; str <= mytext; str++) {
735 (*str ==
'\t') ? mycolumno += 8 - (mycolumno % 8u) : mycolumno++;
752 void lexerr (
const char* path, ...)
762 fprintf (stderr,
"\n%*c\n\"%s\", line %d:\n",
763 mycolumno,
'^', source, mylineno);
767 for (i = 0; i < size_stack; i++)
768 fprintf (stderr,
"%s", err_stack[i]);
773 while ((cp = (
char *) va_arg(ap,
char *)) != NULL)
774 fprintf (stderr,
"%s", cp);
775 fprintf (stderr,
"\n");
786 static char proc_name[] =
"poperr";
788 if (--size_stack < 0) {
789 fprintf (stderr,
"%s: error stack underflow\n", proc_name);
797 void popuperr (
const char *str)
799 static const char proc_name[] =
"popuerr";
801 if (size_stack <= 0) {
802 fprintf (stderr,
"%s: error stack underflow\n", proc_name);
805 err_stack[size_stack-1] = str;
811 void pusherr (
const char *str)
813 static const char proc_name[] =
"pusherr";
815 if (size_stack >= ERR_STACK_MAX) {
816 fprintf (stderr,
"%s: error stack overflow\n", proc_name);
819 err_stack[size_stack++] = str;