44 #include <visp3/robot/vpMy.h>
45 #include <visp3/robot/vpToken.h>
46 #include <visp3/robot/vpKeyword.h>
60 #ifndef DOXYGEN_SHOULD_SKIP_THIS
63 static void count (
void);
64 static void next_source (
void);
66 void lexerr (
const char* path, ...);
111 #define isnult(c) (scantbl[c] == _NULT)
112 #define iscmtt(c) (scantbl[c] & _CMTT)
113 #define isfptt(c) (scantbl[c] & _FPTT)
114 #define isidnt(c) (scantbl[c] & _IDNT)
115 #define isintt(c) (scantbl[c] & _INTT)
116 #define issgnt(c) (scantbl[c] & _SGNT)
117 #define isstgt(c) (scantbl[c] & _STGT)
134 const char *lex_errtbl[] = {
137 "unexpected EOF in comment",
141 "unexpected EOF in string or char constant",
142 "newline in string or char constant",
150 unsigned int mycolumno = 0;
157 static char *lastline;
160 static Byte *scantbl;
169 static char proc_name[] =
"open_lex";
173 if ((chtbl = (Byte *) malloc (CHAR_NBR *
sizeof (Byte))) == NULL
174 || (scantbl = (Byte *) malloc (CHAR_NBR *
sizeof (Byte))) == NULL) {
183 for (i = 0; i < ASCII_NBR; i++) {
184 if (isalpha(i)) chtbl[i] = IDNT;
185 else if (isdigit(i)) chtbl[i] = INTT;
186 else if (isspace(i)) chtbl[i] = SPCT;
188 case '"' : chtbl[i] = STGT;
break;
190 case '-' : chtbl[i] = SGNT;
break;
191 case '.' : chtbl[i] = FPTT;
break;
192 case '/' : chtbl[i] = CMTT;
break;
193 case '_' : chtbl[i] = IDNT;
break;
194 default : chtbl[i] = NULT;
break;
206 for (i = 0; i < ASCII_NBR; i++) {
207 if (isalpha(i)) scantbl[i] = _CMTT|_IDNT|_STGT;
208 else if (isdigit(i)) scantbl[i] = _CMTT|_IDNT|_INTT|_STGT;
210 case '"' : scantbl[i] = _CMTT;
break;
212 case '-' : scantbl[i] = _CMTT|_SGNT|_STGT;
break;
213 case '.' : scantbl[i] = _CMTT|_FPTT|_STGT;
break;
214 case '/' : scantbl[i] = _STGT;
break;
215 case '_' : scantbl[i] = _CMTT|_IDNT|_STGT;
break;
216 default : scantbl[i] = _CMTT|_STGT;
break;
222 scantbl[EOB] = _NULT;
223 scantbl[EOF] = _NULT;
224 scantbl[EOL] = _NULT;
231 void close_lex (
void)
233 free ((
char *) (chtbl - 2));
234 free ((
char *) (scantbl - 2));
238 #define ECHO printf ("%c", *(mysptr))
239 #define CURC (*((signed char *)mysptr))
240 #define NEXTC (*((signed char *)mysptr+1))
241 #define PREVC (*((signed char *)mysptr-1))
257 for (; chtbl[(int)CURC] == SPCT; mysptr++) {};
259 switch (chtbl[(
int)CURC]) {
275 if (mysptr == lastline) next_source ();
288 for (; iscmtt((
int)CURC); mysptr++) {};
289 switch (chtbl[(
int)CURC]) {
295 lexerr (
"start", lex_errtbl[E_CMT_EOF], NULL);
299 if (mysptr == lastline) next_source ();
318 for (; isidnt((
int)CURC); mysptr++) {};
319 mylength = (int)(mysptr - mytext);
320 return (get_symbol (mytext, mylength));
325 myint = (int) (CURC -
'0');
327 for (; isintt((
int)CURC); mysptr++)
328 myint = myint * 10 + (
int) (CURC -
'0');
333 for (; isintt((
int)CURC); mysptr++) {};
334 if (CURC !=
'E' && CURC !=
'e') {
335 myfloat = (float) atof (mytext);
345 if (isintt((
int)CURC))
347 else if (issgnt((
int)CURC) && isintt((
int)NEXTC))
351 myfloat = (float) atof (mytext);
354 for (; isintt((
int)CURC); mysptr++) {};
355 myfloat = (float) atof (mytext);
368 if (! isintt((
int)CURC))
375 if (isintt((
int)CURC))
377 if (isfptt((
int)CURC) && isintt((
int)NEXTC))
385 for (; isstgt((
int)CURC); mysptr++) {};
386 switch (chtbl[(
int)CURC]) {
392 lexerr (
"start", lex_errtbl[E_STG_EOF], NULL);
396 lexerr (
"start", lex_errtbl[E_STG_EOL], NULL);
402 mylength = (int)(mysptr - mytext);
435 int lexecho (FILE *f,
int token)
438 for (; chtbl[(int)CURC] == SPCT; mysptr++)
439 fwrite (mysptr, 1, 1, f);
441 switch (chtbl[(
int)CURC]) {
446 if (token != *mytext)
447 fwrite (mytext, 1, 1, f);
459 fwrite (mysptr, 1, 1, f);
460 if (mysptr == lastline) next_source ();
467 fwrite (mysptr, 1, 1, f);
472 fwrite (mysptr, 1, 1, f);
475 for (; iscmtt((
int)CURC); mysptr++)
476 fwrite (mysptr, 1, 1, f);
477 switch (chtbl[(
int)CURC]) {
483 lexerr (
"start", lex_errtbl[E_CMT_EOF], NULL);
487 fwrite (mysptr, 1, 1, f);
488 if (mysptr == lastline) next_source ();
495 fwrite (mysptr, 1, 1, f);
508 for (; isidnt((
int)CURC); mysptr++) {};
509 mylength = (int)(mysptr - mytext);
510 if (token != get_symbol (mytext, mylength))
511 fwrite (mytext, (
size_t)mylength, 1, f);
512 return (get_symbol (mytext, mylength));
518 for (; isintt((
int)CURC); mysptr++) {};
523 for (; isintt((
int)CURC); mysptr++) {};
524 if (CURC !=
'E' && CURC !=
'e') {
525 if (token != T_FLOAT)
526 fwrite (mytext, (
size_t)(mysptr - mytext), 1, f);
533 if (isintt((
int)CURC)) mysptr++;
534 else if (issgnt((
int)CURC) && isintt((
int)NEXTC)) mysptr +=2;
537 if (token != T_FLOAT)
538 fwrite (mytext, (
size_t)(mysptr - mytext), 1, f);
541 for (; isintt((
int)CURC); mysptr++) {};
542 if (token != T_FLOAT)
543 fwrite (mytext, (
size_t)(mysptr - mytext), 1, f);
548 fwrite (mytext, (
size_t)(mysptr - mytext), 1, f);
556 if (! isintt((
int)CURC)) {
557 if (token != *mytext)
558 fwrite (mytext, 1, 1, f);
566 if (isintt((
int)CURC))
goto int_part;
567 if (isfptt((
int)CURC) && isintt((
int)NEXTC))
goto float_part;
568 if (token != *mytext)
569 fwrite (mytext, 1, 1, f);
573 fwrite (mysptr, 1, 1, f);
577 for (; isstgt((
int)CURC); mysptr++)
578 fwrite (mysptr, 1, 1, f);
579 switch (chtbl[(
int)CURC]) {
585 lexerr (
"start", lex_errtbl[E_STG_EOF], NULL);
589 lexerr (
"start", lex_errtbl[E_STG_EOL], NULL);
593 fwrite (mysptr, 1, 1, f);
596 mylength = (int)(mysptr - mytext);
606 fwrite (mysptr, 1, 1, f);
619 #define BUFSIZE (BUFSIZ << 5)
620 #define LINESIZE (BUFSIZ-1)
621 #define TEXTSIZE (1 + LINESIZE + BUFSIZE + 1)
646 void open_source (FILE *fd,
const char *str)
648 static char proc_name[] =
"open_source";
650 if ((source = (
char *) malloc((strlen(str)+1)*
sizeof(
char))) == NULL) {
654 strcpy (source, str);
655 if ((botbuf = (
char *) malloc (TEXTSIZE *
sizeof (
char))) == NULL) {
660 buf = botbuf + 1 + LINESIZE;
670 void close_source (
void)
672 free ((
char *) source);
673 free ((
char *) botbuf);
679 static void next_source (
void)
688 while ((*--bot = *--top) != EOL) {};
689 myline = mysptr = bot + 1;
691 size = fread (buf,
sizeof (
char), BUFSIZE,fds);
704 while (*--top != EOL) {};
715 #define ERR_STACK_MAX 32
718 static const char *err_stack[ERR_STACK_MAX];
719 static int size_stack = 0;
726 static void count (
void)
731 for (str = myline; str <= mytext; str++) {
732 (*str ==
'\t') ? mycolumno += 8 - (mycolumno % 8) : mycolumno++;
749 void lexerr (
const char* path, ...)
759 fprintf (stderr,
"\n%*c\n\"%s\", line %d:\n",
760 mycolumno,
'^', source, mylineno);
764 for (i = 0; i < size_stack; i++)
765 fprintf (stderr,
"%s", err_stack[i]);
770 while ((cp = (
char *) va_arg(ap,
char *)) != NULL)
771 fprintf (stderr,
"%s", cp);
772 fprintf (stderr,
"\n");
783 static char proc_name[] =
"poperr";
785 if (--size_stack < 0) {
786 fprintf (stderr,
"%s: error stack underflow\n", proc_name);
794 void popuperr (
const char *str)
796 static const char proc_name[] =
"popuerr";
798 if (size_stack <= 0) {
799 fprintf (stderr,
"%s: error stack underflow\n", proc_name);
802 err_stack[size_stack-1] = str;
808 void pusherr (
const char *str)
810 static const char proc_name[] =
"pusherr";
812 if (size_stack >= ERR_STACK_MAX) {
813 fprintf (stderr,
"%s: error stack overflow\n", proc_name);
816 err_stack[size_stack++] = str;