WicErrors getNextToken(pToken tok) { pTokTab tokTabPtr; WicErrors retVal = ERR_NONE; static long tokAfterDefine = 2; // used to flag '(' in #define x( as a // special parentheses int temp; assert(currTokF >= 0); currTokLen = 0; currTok[currTokLen] = 0; TOK_NUM_AFTER_NEW_LINE++; /* Used for #preprocessor directives */ tokAfterDefine++; /* Used for #preprocessor directives */ g_currLineNum = LINE_NUM; g_currColNum = COL_NUM; /* When getNextToken gets called, STATE may be one of: TS_START, TS_COMMENT. */ temp = skipBlank(); if (STATE == TS_START) { setTokPos( tok->pos, TOK_FILE_NAME, currTokF, LINE_NUM, COL_NUM, LINES_BEFORE, temp, orderLineNum ); while (NEXT_CHAR == '') { getNextChar(); tok->pos->spacesBefore = skipBlank(); } if (isalpha(NEXT_CHAR) || NEXT_CHAR == '_') { if (!scanId()) { char saveChar = NEXT_CHAR; currTokLen = 0; currTok[currTokLen] = 0; getNextChar(); if (saveChar == '"') { retVal = scanStr(tok->data); } else { retVal = scanChar(tok->data); } goto Return; } } else if (isdigit(NEXT_CHAR)) { retVal = scanNum(tok->data); goto Return; } else switch (NEXT_CHAR) { case '\'': getNextChar(); retVal = scanChar(tok->data); goto Return; break; case '"': if (currLineIsInclude) { getNextChar(); retVal = scanIncludeFileName(tok->data, '"'); goto Return; } else { getNextChar(); retVal = scanStr(tok->data); goto Return; break; } case '\n': pushGetNextChar(); currLineIsInclude = 0; break; case '!': pushGetNextChar(); if (NEXT_CHAR == '=') { pushGetNextChar(); } break; case '#': pushGetNextChar(); if (TOK_NUM_AFTER_NEW_LINE == 1) { skipBlank(); if (isalpha(NEXT_CHAR) || NEXT_CHAR == '_') { scanId(); } else { tok->data->code = Y_PRE_NULL; retVal = ERR_NONE; goto Return; } } else { if (NEXT_CHAR == '#') { pushGetNextChar(); } } break; case '%': pushGetNextChar(); if (NEXT_CHAR == '=') { pushGetNextChar(); } break; case '&': pushGetNextChar(); if (NEXT_CHAR == '&') { pushGetNextChar(); if (NEXT_CHAR == '=') { pushGetNextChar(); } } break; case '(': pushGetNextChar(); break; case ')': pushGetNextChar(); break; case '*': pushGetNextChar(); if (NEXT_CHAR == '=') { pushGetNextChar(); } break; case '+': pushGetNextChar(); if (NEXT_CHAR == '+') { pushGetNextChar(); } else if (NEXT_CHAR == '=') { pushGetNextChar(); } break; case ',': pushGetNextChar(); break; case '-': pushGetNextChar(); if (NEXT_CHAR == '-') { pushGetNextChar(); } else if (NEXT_CHAR == '=') { pushGetNextChar(); } else if (NEXT_CHAR == '>') { pushGetNextChar(); } break; case '.': pushGetNextChar(); if (NEXT_CHAR == '.') { pushGetNextChar(); if (NEXT_CHAR == '.') { pushGetNextChar(); } else { retVal = RERR_INV_CHAR; goto Return; } } else if (isdigit(NEXT_CHAR)) { if (pushFloatDotExp(tok->data, 1)) { retVal = convStr2Const(tok->data); goto Return; } else { retVal = RERR_INV_CHAR; goto Return; } } break; case '/': pushGetNextChar(); if (NEXT_CHAR == '=') { pushGetNextChar(); } else if (NEXT_CHAR == '*') { /* comment begin */ popChars(1); STATE = TS_COMMENT; getNextChar(); retVal = scanComment(tok->data); goto Return; } else if (NEXT_CHAR == '/') { popChars(1); STATE = TS_COMMENT; getNextChar(); retVal = scanCPlusPlusComment(tok->data); goto Return; } break; case ':': pushGetNextChar(); if (NEXT_CHAR == '>') { pushGetNextChar(); } break; case ';': pushGetNextChar(); break; case '<': if (currLineIsInclude) { getNextChar(); retVal = scanIncludeFileName(tok->data, '>'); goto Return; } else { pushGetNextChar(); if (NEXT_CHAR == '<') { pushGetNextChar(); if (NEXT_CHAR == '=') { pushGetNextChar(); } } else if (NEXT_CHAR == '=') { pushGetNextChar(); } } break; case '=': pushGetNextChar(); if (NEXT_CHAR == '=') { pushGetNextChar(); } break; case '>': pushGetNextChar(); if (NEXT_CHAR == '>') { pushGetNextChar(); if (NEXT_CHAR == '=') { pushGetNextChar(); } } else if (NEXT_CHAR == '=') { pushGetNextChar(); } break; case '?': pushGetNextChar(); break; case '[': pushGetNextChar(); break; case ']': pushGetNextChar(); break; case '^': pushGetNextChar(); if (NEXT_CHAR == '=') pushGetNextChar(); break; case '{': pushGetNextChar(); break; case '|': pushGetNextChar(); if (NEXT_CHAR == '=') { pushGetNextChar(); } else if (NEXT_CHAR == '|') { pushGetNextChar(); } break; case '}': pushGetNextChar(); break; case '~': pushGetNextChar(); break; case (char) EOF: tok->data->code = Y_EOF; retVal = ERR_NONE; goto Return; break; default: /* Eat up an ivalid character */ getNextChar(); retVal = RERR_INV_CHAR; goto Return; } tokTabPtr = tabLookup(currTok); if (tokTabPtr != NULL) { tok->data->code = tokTabPtr->code; if (tok->data->code == Y_PRE_INCLUDE) { currLineIsInclude = 1; } if (tok->data->code == Y_PRE_DEFINE) { tokAfterDefine = 0; } if (tok->data->code == Y_LEFT_PAREN && tokAfterDefine == 2) { // the case of #define x(... if (tok->pos->spacesBefore == 0) { tok->data->code = Y_PRE_SPECIAL_LEFT_PAREN; } } tok->data->repr.string = registerString(tokTabPtr->name, !FREE_STRING); } else { if (currTok[0] == '#') { retVal = RERR_INV_PREPROCESSOR; goto Return; } else { tok->data->code = Y_ID; tok->data->repr.string = registerString(wicStrdup(currTok), FREE_STRING); } } } else if (STATE == TS_COMMENT) { setTokPos(tok->pos, TOK_FILE_NAME, currTokF, LINE_NUM, COL_NUM, LINES_BEFORE, 0, orderLineNum); retVal = scanComment(tok->data); goto Return; } else { assert(0); } Return: if (tok->data->code != Y_PRE_NEWLINE) { tok->pos->linesBefore = tok->pos->lineNum - PREV_TOK_LINE_NUM; PREV_TOK_LINE_NUM = tok->pos->lineNum; } else { tok->pos->linesBefore = 0; } zapTokPos(g_currPos); g_currPos = dupTokPos(tok->pos, NULL); return retVal; }
char *getTokDataStr(pTokData t, int useTargetLang, char *s, int spaceLeft) { TargetLangType lang; spaceLeft--; if (useTargetLang) { lang = g_opt.targetLang; } else { lang = TLT_C; } if (t == NULL) { reportError(FATAL_INTERNAL, "in getTokenStr: Null token passed"); } else { switch (t->code) { case Y_PRE_NEWLINE: sprintf(s, "<NL>"); break; case Y_NUMBER: _convertConstant(t, lang, s, spaceLeft); break; case Y_EOF: sprintf(s, "END-OF-FILE"); break; case Y_PRE_COMMENT: sprintf(s, "%c%.*s", g_tlang.commentChar, spaceLeft-1, t->repr.string); break; case Y_STRING: switch(lang) { case TLT_C: _getCString(s, spaceLeft, t->repr.s.s, t->repr.s.strLen); break; case TLT_FORTRAN: _getFortranString(s, spaceLeft, t->repr.s.s, t->repr.s.strLen); break; case TLT_ASM: _getAsmString(s, spaceLeft, t->repr.s.s, t->repr.s.strLen); break; default: assert(0); } break; case Y_INCLUDE_FILE_NAME: sprintf(s, "%.*s", spaceLeft, getTokDataIdName(t)); setNewFileExt(s, s, g_tlang.extension[0]); break; case Y_ID: case Y_TYPEDEF_NAME: sprintf(s, "%.*s", spaceLeft, getTokDataIdName(t)); break; default: { pTokTab elem = tabLookup(t->repr.string); char *tokStr; if (elem == NULL) { printf("DEBUG: t->code = %d", t->code); assert(elem != NULL); } if (lang == TLT_C) { tokStr = elem->name; } else { tokStr = elem->tname[lang]; } sprintf(s, "%.*s", spaceLeft, tokStr); } } } return s; }