42 #include <visp3/core/vpConfig.h>
43 #include <visp3/core/vpException.h>
45 #include "vpKeyword.h"
56 #ifndef DOXYGEN_SHOULD_SKIP_THIS
59 static void count(
void);
60 static void next_source(
void);
62 void lexerr(
const char *path, ...);
107 #define isnult(c) (scantbl[c] == _NULT)
108 #define iscmtt(c) (scantbl[c] & _CMTT)
109 #define isfptt(c) (scantbl[c] & _FPTT)
110 #define isidnt(c) (scantbl[c] & _IDNT)
111 #define isintt(c) (scantbl[c] & _INTT)
112 #define issgnt(c) (scantbl[c] & _SGNT)
113 #define isstgt(c) (scantbl[c] & _STGT)
129 const char *lex_errtbl[] = {
132 "unexpected EOF in comment",
136 "unexpected EOF in string or char constant",
137 "newline in string or char constant",
144 unsigned int mycolumno = 0;
150 static char *lastline;
153 static Byte *scantbl;
161 if ((chtbl = (Byte *)malloc(CHAR_NBR *
sizeof(Byte))) == NULL ||
162 (scantbl = (Byte *)malloc(CHAR_NBR *
sizeof(Byte))) == NULL) {
163 static char proc_name[] =
"open_lex";
172 for (
int i = 0; i < ASCII_NBR; i++) {
211 for (
int i = 0; i < ASCII_NBR; i++) {
213 scantbl[i] = _CMTT | _IDNT | _STGT;
215 scantbl[i] = _CMTT | _IDNT | _INTT | _STGT;
223 scantbl[i] = _CMTT | _SGNT | _STGT;
226 scantbl[i] = _CMTT | _FPTT | _STGT;
232 scantbl[i] = _CMTT | _IDNT | _STGT;
235 scantbl[i] = _CMTT | _STGT;
242 scantbl[EOB] = _NULT;
243 scantbl[EOF] = _NULT;
244 scantbl[EOL] = _NULT;
253 free((
char *)(chtbl - 2));
254 free((
char *)(scantbl - 2));
257 #define ECHO printf("%c", *(mysptr))
258 #define CURC (*((signed char *)mysptr))
259 #define NEXTC (*((signed char *)mysptr + 1))
260 #define PREVC (*((signed char *)mysptr - 1))
274 for (; chtbl[(int)CURC] == SPCT; mysptr++) {
277 switch (chtbl[(
int)CURC]) {
293 if (mysptr == lastline)
308 for (; iscmtt((
int)CURC); mysptr++) {
310 switch (chtbl[(
int)CURC]) {
316 lexerr(
"start", lex_errtbl[E_CMT_EOF], NULL);
320 if (mysptr == lastline)
341 for (; isidnt((
int)CURC); mysptr++) {
343 mylength = (int)(mysptr - mytext);
344 return (get_symbol(mytext, mylength));
349 myint = (int)(CURC -
'0');
351 for (; isintt((
int)CURC); mysptr++)
352 myint = myint * 10 + (
int)(CURC -
'0');
357 for (; isintt((
int)CURC); mysptr++) {
359 if (CURC !=
'E' && CURC !=
'e') {
360 myfloat = (float)atof(mytext);
370 if (isintt((
int)CURC))
372 else if (issgnt((
int)CURC) && isintt((
int)NEXTC))
376 myfloat = (float)atof(mytext);
379 for (; isintt((
int)CURC); mysptr++) {
381 myfloat = (float)atof(mytext);
394 if (!isintt((
int)CURC))
401 if (isintt((
int)CURC))
403 if (isfptt((
int)CURC) && isintt((
int)NEXTC))
411 for (; isstgt((
int)CURC); mysptr++) {
413 switch (chtbl[(
int)CURC]) {
419 lexerr(
"start", lex_errtbl[E_STG_EOF], NULL);
423 lexerr(
"start", lex_errtbl[E_STG_EOL], NULL);
429 mylength = (int)(mysptr - mytext);
461 int lexecho(FILE *f,
int token)
464 for (; chtbl[(int)CURC] == SPCT; mysptr++)
465 fwrite(mysptr, 1, 1, f);
467 switch (chtbl[(
int)CURC]) {
472 if (token != *mytext)
473 fwrite(mytext, 1, 1, f);
485 fwrite(mysptr, 1, 1, f);
486 if (mysptr == lastline)
495 fwrite(mysptr, 1, 1, f);
500 fwrite(mysptr, 1, 1, f);
503 for (; iscmtt((
int)CURC); mysptr++)
504 fwrite(mysptr, 1, 1, f);
505 switch (chtbl[(
int)CURC]) {
511 lexerr(
"start", lex_errtbl[E_CMT_EOF], NULL);
515 fwrite(mysptr, 1, 1, f);
516 if (mysptr == lastline)
525 fwrite(mysptr, 1, 1, f);
538 for (; isidnt((
int)CURC); mysptr++) {
540 mylength = (int)(mysptr - mytext);
541 if (token != get_symbol(mytext, mylength))
542 fwrite(mytext, (
size_t)mylength, 1, f);
543 return (get_symbol(mytext, mylength));
549 for (; isintt((
int)CURC); mysptr++) {
555 for (; isintt((
int)CURC); mysptr++) {
557 if (CURC !=
'E' && CURC !=
'e') {
558 if (token != T_FLOAT)
559 fwrite(mytext, (
size_t)(mysptr - mytext), 1, f);
566 if (isintt((
int)CURC))
568 else if (issgnt((
int)CURC) && isintt((
int)NEXTC))
572 if (token != T_FLOAT)
573 fwrite(mytext, (
size_t)(mysptr - mytext), 1, f);
576 for (; isintt((
int)CURC); mysptr++) {
578 if (token != T_FLOAT)
579 fwrite(mytext, (
size_t)(mysptr - mytext), 1, f);
584 fwrite(mytext, (
size_t)(mysptr - mytext), 1, f);
592 if (!isintt((
int)CURC)) {
593 if (token != *mytext)
594 fwrite(mytext, 1, 1, f);
602 if (isintt((
int)CURC))
604 if (isfptt((
int)CURC) && isintt((
int)NEXTC))
606 if (token != *mytext)
607 fwrite(mytext, 1, 1, f);
611 fwrite(mysptr, 1, 1, f);
615 for (; isstgt((
int)CURC); mysptr++)
616 fwrite(mysptr, 1, 1, f);
617 switch (chtbl[(
int)CURC]) {
623 lexerr(
"start", lex_errtbl[E_STG_EOF], NULL);
627 lexerr(
"start", lex_errtbl[E_STG_EOL], NULL);
631 fwrite(mysptr, 1, 1, f);
634 mylength = (int)(mysptr - mytext);
644 fwrite(mysptr, 1, 1, f);
656 #define BUFSIZE (BUFSIZ << 5)
657 #define LINESIZE (BUFSIZ - 1)
658 #define TEXTSIZE (1 + LINESIZE + BUFSIZE + 1)
669 void unlex(
void) { mysptr = mytext; }
678 void open_source(FILE *fd,
const char *str)
680 if ((source = (
char *)malloc((strlen(str) + 1) *
sizeof(
char))) == NULL) {
681 static char proc_name[] =
"open_source";
686 if ((botbuf = (
char *)malloc(TEXTSIZE *
sizeof(
char))) == NULL) {
687 static char proc_name[] =
"open_source";
692 buf = botbuf + 1 + LINESIZE;
702 void close_source(
void)
704 free((
char *)source);
705 free((
char *)botbuf);
711 static void next_source(
void)
720 while ((*--bot = *--top) != EOL) {
722 myline = mysptr = bot + 1;
724 size = fread(buf,
sizeof(
char), BUFSIZE, fds);
737 while (*--top != EOL) {
748 #define ERR_STACK_MAX 32
750 static const char *err_stack[ERR_STACK_MAX];
751 static int size_stack = 0;
757 static void count(
void)
762 for (str = myline; str <= mytext; str++) {
763 (*str ==
'\t') ? mycolumno += 8 - (mycolumno % 8) : mycolumno++;
780 void lexerr(
const char *path, ...)
790 fprintf(stderr,
"\n%*c\n\"%s\", line %d:\n", mycolumno,
'^', source, mylineno);
794 for (i = 0; i < size_stack; i++)
795 fprintf(stderr,
"%s", err_stack[i]);
800 while ((cp = (
char *)va_arg(ap,
char *)) != NULL)
801 fprintf(stderr,
"%s", cp);
802 fprintf(stderr,
"\n");
813 if (--size_stack < 0) {
814 static char proc_name[] =
"poperr";
815 fprintf(stderr,
"%s: error stack underflow\n", proc_name);
823 void popuperr(
const char *str)
825 if (size_stack <= 0) {
826 static const char proc_name[] =
"popuerr";
827 fprintf(stderr,
"%s: error stack underflow\n", proc_name);
830 err_stack[size_stack - 1] = str;
836 void pusherr(
const char *str)
838 if (size_stack >= ERR_STACK_MAX) {
839 static const char proc_name[] =
"pusherr";
840 fprintf(stderr,
"%s: error stack overflow\n", proc_name);
843 err_stack[size_stack++] = str;
error that can be emitted by ViSP classes.