42 #include <visp3/core/vpConfig.h>
43 #include <visp3/core/vpException.h>
45 #include "vpKeyword.h"
56 #ifndef DOXYGEN_SHOULD_SKIP_THIS
58 static void count(
void);
59 static void next_source(
void);
61 void lexerr(
const char *path, ...);
106 #define isnult(c) (scantbl[c] == _NULT)
107 #define iscmtt(c) (scantbl[c] & _CMTT)
108 #define isfptt(c) (scantbl[c] & _FPTT)
109 #define isidnt(c) (scantbl[c] & _IDNT)
110 #define isintt(c) (scantbl[c] & _INTT)
111 #define issgnt(c) (scantbl[c] & _SGNT)
112 #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",
143 unsigned int mycolumno = 0;
149 static char *lastline;
152 static Byte *scantbl;
160 if ((chtbl = (Byte *)malloc(CHAR_NBR *
sizeof(Byte))) == NULL ||
161 (scantbl = (Byte *)malloc(CHAR_NBR *
sizeof(Byte))) == NULL) {
162 static char proc_name[] =
"open_lex";
171 for (
int i = 0; i < ASCII_NBR; i++) {
210 for (
int i = 0; i < ASCII_NBR; i++) {
212 scantbl[i] = _CMTT | _IDNT | _STGT;
214 scantbl[i] = _CMTT | _IDNT | _INTT | _STGT;
222 scantbl[i] = _CMTT | _SGNT | _STGT;
225 scantbl[i] = _CMTT | _FPTT | _STGT;
231 scantbl[i] = _CMTT | _IDNT | _STGT;
234 scantbl[i] = _CMTT | _STGT;
241 scantbl[EOB] = _NULT;
242 scantbl[EOF] = _NULT;
243 scantbl[EOL] = _NULT;
252 free((
char *)(chtbl - 2));
253 free((
char *)(scantbl - 2));
256 #define ECHO printf("%c", *(mysptr))
257 #define CURC (*((signed char *)mysptr))
258 #define NEXTC (*((signed char *)mysptr + 1))
259 #define PREVC (*((signed char *)mysptr - 1))
273 for (; chtbl[(int)CURC] == SPCT; mysptr++) {
276 switch (chtbl[(
int)CURC]) {
292 if (mysptr == lastline)
307 for (; iscmtt((
int)CURC); mysptr++) {
309 switch (chtbl[(
int)CURC]) {
315 lexerr(
"start", lex_errtbl[E_CMT_EOF], NULL);
319 if (mysptr == lastline)
340 for (; isidnt((
int)CURC); mysptr++) {
342 mylength = (int)(mysptr - mytext);
343 return (get_symbol(mytext, mylength));
348 myint = (int)(CURC -
'0');
350 for (; isintt((
int)CURC); mysptr++)
351 myint = myint * 10 + (
int)(CURC -
'0');
356 for (; isintt((
int)CURC); mysptr++) {
358 if (CURC !=
'E' && CURC !=
'e') {
359 myfloat = (float)atof(mytext);
369 if (isintt((
int)CURC))
371 else if (issgnt((
int)CURC) && isintt((
int)NEXTC))
375 myfloat = (float)atof(mytext);
378 for (; isintt((
int)CURC); mysptr++) {
380 myfloat = (float)atof(mytext);
393 if (!isintt((
int)CURC))
400 if (isintt((
int)CURC))
402 if (isfptt((
int)CURC) && isintt((
int)NEXTC))
410 for (; isstgt((
int)CURC); mysptr++) {
412 switch (chtbl[(
int)CURC]) {
418 lexerr(
"start", lex_errtbl[E_STG_EOF], NULL);
422 lexerr(
"start", lex_errtbl[E_STG_EOL], NULL);
428 mylength = (int)(mysptr - mytext);
460 int lexecho(FILE *f,
int token)
463 for (; chtbl[(int)CURC] == SPCT; mysptr++)
464 fwrite(mysptr, 1, 1, f);
466 switch (chtbl[(
int)CURC]) {
471 if (token != *mytext)
472 fwrite(mytext, 1, 1, f);
484 fwrite(mysptr, 1, 1, f);
485 if (mysptr == lastline)
494 fwrite(mysptr, 1, 1, f);
499 fwrite(mysptr, 1, 1, f);
502 for (; iscmtt((
int)CURC); mysptr++)
503 fwrite(mysptr, 1, 1, f);
504 switch (chtbl[(
int)CURC]) {
510 lexerr(
"start", lex_errtbl[E_CMT_EOF], NULL);
514 fwrite(mysptr, 1, 1, f);
515 if (mysptr == lastline)
524 fwrite(mysptr, 1, 1, f);
537 for (; isidnt((
int)CURC); mysptr++) {
539 mylength = (int)(mysptr - mytext);
540 if (token != get_symbol(mytext, mylength))
541 fwrite(mytext, (
size_t)mylength, 1, f);
542 return (get_symbol(mytext, mylength));
548 for (; isintt((
int)CURC); mysptr++) {
554 for (; isintt((
int)CURC); mysptr++) {
556 if (CURC !=
'E' && CURC !=
'e') {
557 if (token != T_FLOAT)
558 fwrite(mytext, (
size_t)(mysptr - mytext), 1, f);
565 if (isintt((
int)CURC))
567 else if (issgnt((
int)CURC) && isintt((
int)NEXTC))
571 if (token != T_FLOAT)
572 fwrite(mytext, (
size_t)(mysptr - mytext), 1, f);
575 for (; isintt((
int)CURC); mysptr++) {
577 if (token != T_FLOAT)
578 fwrite(mytext, (
size_t)(mysptr - mytext), 1, f);
583 fwrite(mytext, (
size_t)(mysptr - mytext), 1, f);
591 if (!isintt((
int)CURC)) {
592 if (token != *mytext)
593 fwrite(mytext, 1, 1, f);
601 if (isintt((
int)CURC))
603 if (isfptt((
int)CURC) && isintt((
int)NEXTC))
605 if (token != *mytext)
606 fwrite(mytext, 1, 1, f);
610 fwrite(mysptr, 1, 1, f);
614 for (; isstgt((
int)CURC); mysptr++)
615 fwrite(mysptr, 1, 1, f);
616 switch (chtbl[(
int)CURC]) {
622 lexerr(
"start", lex_errtbl[E_STG_EOF], NULL);
626 lexerr(
"start", lex_errtbl[E_STG_EOL], NULL);
630 fwrite(mysptr, 1, 1, f);
633 mylength = (int)(mysptr - mytext);
643 fwrite(mysptr, 1, 1, f);
655 #define BUFSIZE (BUFSIZ << 5)
656 #define LINESIZE (BUFSIZ - 1)
657 #define TEXTSIZE (1 + LINESIZE + BUFSIZE + 1)
668 void unlex(
void) { mysptr = mytext; }
677 void open_source(FILE *fd,
const char *str)
679 if ((source = (
char *)malloc((strlen(str) + 1) *
sizeof(
char))) == NULL) {
680 static char proc_name[] =
"open_source";
685 if ((botbuf = (
char *)malloc(TEXTSIZE *
sizeof(
char))) == NULL) {
686 static char proc_name[] =
"open_source";
691 buf = botbuf + 1 + LINESIZE;
701 void close_source(
void)
703 free((
char *)source);
704 free((
char *)botbuf);
710 static void next_source(
void)
719 while ((*--bot = *--top) != EOL) {
721 myline = mysptr = bot + 1;
723 size = fread(buf,
sizeof(
char), BUFSIZE, fds);
735 while (*--top != EOL) {
746 #define ERR_STACK_MAX 32
748 static const char *err_stack[ERR_STACK_MAX];
749 static int size_stack = 0;
755 static void count(
void)
760 for (str = myline; str <= mytext; str++) {
761 (*str ==
'\t') ? mycolumno += 8 - (mycolumno % 8) : mycolumno++;
778 void lexerr(
const char *path, ...)
788 fprintf(stderr,
"\n%*c\n\"%s\", line %d:\n", mycolumno,
'^', source, mylineno);
792 for (i = 0; i < size_stack; i++)
793 fprintf(stderr,
"%s", err_stack[i]);
798 while ((cp = (
char *)va_arg(ap,
char *)) != NULL)
799 fprintf(stderr,
"%s", cp);
800 fprintf(stderr,
"\n");
811 if (--size_stack < 0) {
812 static char proc_name[] =
"poperr";
813 fprintf(stderr,
"%s: error stack underflow\n", proc_name);
821 void popuperr(
const char *str)
823 if (size_stack <= 0) {
824 static const char proc_name[] =
"popuerr";
825 fprintf(stderr,
"%s: error stack underflow\n", proc_name);
828 err_stack[size_stack - 1] = str;
834 void pusherr(
const char *str)
836 if (size_stack >= ERR_STACK_MAX) {
837 static const char proc_name[] =
"pusherr";
838 fprintf(stderr,
"%s: error stack overflow\n", proc_name);
841 err_stack[size_stack++] = str;
error that can be emitted by ViSP classes.