⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vs1.0_tokens.l

📁 使用stl技术,(还没看,是听说的)
💻 L
📖 第 1 页 / 共 5 页
字号:
		gCurFileName = gIncludeStack[gIncludeStackIndex].fileName;
//		yyin = gIncludeStack[gIncludeStackIndex].fileHandle;
		myin = gIncludeStack[gIncludeStackIndex].prevString;
		yylineno = gIncludeStack[gIncludeStackIndex].lineNo;
		gInvokeMacro = gIncludeStack[gIncludeStackIndex].lastInvokeMacro;
		gParseMacro = gIncludeStack[gIncludeStackIndex].lastParseMacro;
		gMacroLineParse = gIncludeStack[gIncludeStackIndex].lastMacroLineParse;
		gbInsideInclude = gIncludeStack[gIncludeStackIndex].lastbInsideInclude;
		gbInsideMacro = gIncludeStack[gIncludeStackIndex].lastbInsideMacro;
		gbProcessingIFDEF = gIncludeStack[gIncludeStackIndex].lastbProcessingIFDEF;

		if (!gbInsideMacro && !oneLiner)
		{
//			GenSwitchFileNames(gCurFileName);
			BEGIN(SAVELINE);
		}
		else
		{
			BEGIN(INITIAL);
		}

		// gSaveLine was last line saved, before macro invocation
		if (wasInMacro && !gbInsideMacro && !oneLiner)
		{
//			GenDebugLine();
//			GenListString();
			gLinesAssembled++;
			yylineno++;
		}

	}

}

%%


//=====================================================================
// Function:	FindNMacro
// Description:	Look through macros and see if it had been predefined
// Parameters:	findName = name to lookup
//				sLen = # characters valid in source (findName)
// Returns:		MACROENTRY * = pointer to macro entry if found
//=====================================================================
MACROENTRY *FindNMacro(char *findName, unsigned int sLen)
{
	MACROENTRY *curEntry;

	curEntry = gLastMacro;
	while (curEntry != NULL)
	{
		if (strlen(curEntry->macroName) == sLen)
		{
			if (!strncmp(curEntry->macroName, findName, sLen))
			{
				break;
			}
		}

		curEntry = curEntry->prev;
	}

	return curEntry;
	
}

//=====================================================================
// Function:	FindMacro
// Description:	Look through macros and see if it had been predefined
// Parameters:	findName = name to lookup
// Returns:		MACROENTRY * = pointer to macro entry if found
//=====================================================================
MACROENTRY *FindMacro(char *findName)
{
	MACROENTRY *curEntry;

	curEntry = gLastMacro;
	while (curEntry != NULL)
	{
		if (!strcmp(curEntry->macroName, findName))
		{
			break;
		}

		curEntry = curEntry->prev;
	}

	return curEntry;
	
}

//=====================================================================
// Function:	CleanUp
// Description:	Clean up the #define strings
// Parameters:	.
// Returns:		.
//=====================================================================
void CleanUp()
{
	void *tPtr;

	// free up the macros that were alloced
	while (gLastMacro != NULL)
	{

		FreeMacroEntry(gLastMacro);

		tPtr = gLastMacro;
		gLastMacro = gLastMacro->prev;
		SAFEFREE(tPtr);
	}

}

//=====================================================================
// Function:	FreeMacroEntry
// Description:	Frees up the macro entry data, (parms, lines of text)
// Parameters:	macEntry = pointer to the MACROENTRY structure
// Returns:		.
//=====================================================================
void FreeMacroEntry(MACROENTRY *macEntry)
{
	MACROTEXT *tText;
	MACROTEXT *tNext;

	SAFEFREE(macEntry->macroName);
	SAFEFREE(macEntry->fileName);
	// free the macro lines that were alloced
	tText = macEntry->lastMacroLines;
	while (tText != NULL)
	{
		tNext = tText->prev;
		SAFEFREE(tText);
		tText = tNext;
	}

	// free the text of the macro parms that were alloced
	tText = macEntry->lastMacroParms;
	while (tText != NULL)
	{
		tNext = tText->prev;
		SAFEFREE(tText);
		tText = tNext;
	}
}

//=====================================================================
// Function:	CheckMacroFunctions
// Description:	Find if this text is a builtin macro function
// Parameters:	lookString = non-null terminated string of possible
//				and if found set global macro function call
// Returns:		.
//=====================================================================
void CheckMacroFunctions(char *lookString, unsigned int *recognizedLen, char **invString)
{

	unsigned int i;
	unsigned int sLen;
	
	for (i=0; i< NUM_MACRO_FUNCTIONS; i++)
	{
		sLen = strlen(gMacroFunctions[i].name);
		if (!strncmp(gMacroFunctions[i].name, lookString, sLen))
		{
			gMacroCallFunction = gMacroFunctions[i].function;
			*recognizedLen = sLen;
			*invString = NULL;
			return;
		}
	}
}

//=====================================================================
// Function:	FindAlphaNum
// Description:	Find a whole alpha numeric string, ie consists of
//				[A-Za-z0-9_] only
// Parameters:	srcStr = source string to search through.
//				sLen = unsinged int pointer to length of string found
// Returns:		pointer to found start of string.
//				NULL if none.
//=====================================================================
char *FindAlphaNum(char *srcStr, unsigned int *sLen)
{
	char curChar;
	char *foundStr;

	while (*srcStr != '\0')
	{
		curChar = toupper(*srcStr);
		if ((curChar >= 'A') && (curChar <= 'Z'))
			break;

		if ((curChar >= '0') && (curChar <='9'))
			break;

		if (curChar == '_')
			break;

		srcStr++;
	}

	if (*srcStr == '\0')
	{
		return NULL;
	}

	foundStr = srcStr;

	*sLen = 0;
	// now search for end of string of [A-Za-z0-9_]
	while (*srcStr != '\0')
	{
		curChar = toupper(*srcStr);
		if ((curChar < 'A') || (curChar > 'Z'))
		{
			if ((curChar < '0') || (curChar > '9'))
			{
				if (curChar != '_')
					break;
			}
		}

		(*sLen)++;
		srcStr++;
	}

	return foundStr;

}

//=====================================================================
// Function:	FindDefineParm
// Description:	Find if the MACROENTRY->macroText linked list contains
//				replaceable parameters.
// Parameters:	srcParms = pointer to MACROENTRY structure for source
//						parameters
//				invParms = MACROENTRY pointer to invocation parameters
//				lookString = non-null terminated string of possible
//							replaceable string
//				recognizedLen = replacement string matched length
//				invString = invocation string to replace with
// Returns:		pointer to first character found in lookstring
//=====================================================================
char *FindDefineParm(MACROENTRY *srcParms, MACROENTRY *invParms, 
						char *lookString, unsigned int *recognizedLen, char **invString)
{
	MACROTEXT *srcText;
	MACROTEXT *invText;
	char *checkStr;
	unsigned int checkLen;
	unsigned int sLen;

	checkStr = lookString;
	*invString = NULL;

	// first search for first [A-Za-z0-9_] only string
	checkStr = FindAlphaNum(lookString, &checkLen);

	while (checkStr != NULL)
	{
		// check all the #define parameters for match
		srcText = srcParms->firstMacroParms;
		invText = invParms->firstMacroParms;
		while (srcText)
		{
			sLen = strlen(srcText->macroText);
			// lengths should match
			if (sLen == checkLen)
			{
				if (!strncmp(checkStr, srcText->macroText, checkLen))
				{
					// it matched so return replacement text
					*invString = invText->macroText;
					// and length that we reconized
					*recognizedLen = checkLen;
					return checkStr;
				}
			}

			srcText = srcText->next;
			invText = invText->next;
		}

		// not found yet, so go to next string.
		checkStr = FindAlphaNum(checkStr+checkLen, &checkLen);
	}

	return NULL;
}

//=====================================================================
// Function:	FindReplaceParm
// Description:	Find if the MACROENTRY->macroText linked list contains
//				a replaceable parameters.
// Parameters:	srcParms = pointer to MACROENTRY structure for source
//						parameters
//				invParms = MACROENTRY pointer to invocation parameters
//				lookString = non-null terminated string of possible
//							replaceable string
//				recognizedLen = replacement string matched length
//				invString = invocation string to replace with
// Returns:		.
//=====================================================================
void FindReplaceParm(MACROENTRY *srcParms, MACROENTRY *invParms, 
						char *lookString, unsigned int *recognizedLen, char **invString)
{
	unsigned int sLen;
	MACROTEXT *srcText;
	MACROTEXT *invText;

	*recognizedLen = 0;
	*invString = NULL;

	srcText = srcParms->firstMacroParms;
	invText = invParms->firstMacroParms;

	if (srcText != NULL)
	{
		// go until srcText # strings ends
		while (srcText != NULL)
		{
			sLen = strlen(srcText->macroText);
			if (!strncmp(srcText->macroText, lookString, sLen))
			{
				// found it so return src, replacement string
				*recognizedLen = strlen(srcText->macroText);
				*invString = invText->macroText;
				// call function macro if it was invoked prior.
				if (gMacroCallFunction != NULL)
				{
					gMacroCallFunction(lookString, recognizedLen, invString);
					gMacroCallFunction = NULL;
				}
				return;
			}

			srcText = srcText->next;
			invText = invText->next;
		}
	}

	// ok, it wasn't found, look through builtin macro functions
	CheckMacroFunctions(lookString, recognizedLen, invString);
}

//=====================================================================
// Function:	ReplaceMacroParms
// Description:	Replace macro parameters when macro was defined, with
//				those specified on the macro invocation line
// Parameters:	srcLine = source line to replace src macro parms with
//				destLine = destination line save to.
//				invocation macro parameters.
//				parseMacro = currently parsing macro entry
//				invParms = invocation macro entry
// Returns:		.
//=====================================================================
void ReplaceMacroParms(char *srcLine, char *destLine, 
							MACROENTRY *srcParms, MACROENTRY *invParms)
{
	char *findReplace;
	char *invString;
	unsigned int sLen;
	unsigned int dLen;
	unsigned int copyLen;
	unsigned int subLen;
	unsigned int recognizedLen;

	destLine[0]= '\0';

	sLen = strlen(srcLine);
	dLen = 0;

	while (sLen > 0)
	{
		// strtok might work better except it modifies the string, so
		// kind of do my own....
		if (!srcParms->bIsDefine)
		{
			findReplace = strchr(srcLine, '%');
			if (findReplace != NULL)
			{
				// bypass % sign in findReplacement
				findReplace++;
				// figure out length of source before %
				copyLen = (findReplace - srcLine)-1;
				// check if there is a replacement string
				FindReplaceParm(srcParms, invParms, findReplace, &recognizedLen, &invString);
			}
			else
			{
				strcat(destLine, srcLine);
				return;
			}
		}
		else
		{
			findReplace = FindDefineParm(srcParms, invParms, srcLine, &recognizedLen, &invString);
			if (findReplace != NULL)
			{
				// figure out length of source before %
				copyLen = findReplace - srcLine;
			}
			else
			{
				strcat(destLine, srcLine);
				return;
			}
		}


		if (invString != NULL)
		{
			// figure out how much we are going to substitute
			subLen = strlen(invString);
		}
		else
		{
			subLen = 0;
		}

		if ((dLen + copyLen + subLen) > MAXSAVELINE)
		{
			LexError("Macro string overrun.\n");
			CleanUp();
			exit(ERROR_MACRO_OVERRUN);
		}

		if (copyLen > 0)
		{
			strncat(destLine, srcLine, copyLen);
			dLen += copyLen;
		}

		srcLine += copyLen;
		sLen -= copyLen;
		// in macro so skip % part of variable
		if (!srcParms->bIsDefine)
		{
			// skip %, also
			srcLine++;
			sLen--;
		}

		if (invString != NULL)
		{
			strcat(destLine, invString);
			dLen += strlen(invString);
		}

		srcLine += recognizedLen;
		sLen -= recognizedLen;
	}

}

//=====================================================================
// Function:	SaveMacroText
// Description:	Adds a string to a linked list of MACROTEXT structures
// Parameters:	srcText = pointer to source text to save
//				lastMacroText = last allocated, or NULL
// Returns:		newly allocated MACROTEXT structure, or NULL
//=====================================================================
MACROTEXT *SaveMacroText(char *srcText, MACROTEXT *lastMacroText)
{
	MACROTEXT *curMacroText;

	curMacroText = (MACROTEXT *)malloc(sizeof(MACROTEXT));
	if (curMacroText == NULL)
	{
		return NULL;
	}
	else
	{
		// no next entry but set up previous with previously alloced macro parameter
		curMacroText->next = NULL;
		curMacroText->prev = lastMacroText;

		// if the macroParm pointer is null then we are the first allocated
		// so if not set the last one allocate next pointer to newly allocated structure
		if (lastMacroText != NULL)
		{
			lastMacroText->next = curMacroText;
		}

		/* %%%%% this should be set up in memory pools. */
		curMacroText->macroText = strdup(srcText);
		if (curMacroText->macroText == NULL)
		{
			SAFEFREE(curMacroText);
			return NULL;
		}
	}

	return curMacroText;
}

//=====================================================================
// Function:	ParseBuiltInMacroParms
// Description:	parse parameters of string and fill in MACROENTRY
//				structure.
// Parameters:	parsedMacro = pointer to MACROENTRY structure that gets
//				filled in with parameter pointers and count
//				parmStr = string to parse parameters from
// Returns:		false if error
//=====================================================================
bool ParseBuiltInMacroParms(MACROENTRY *parsedMacro, char *parmStr)
{
	char *endStr;
	char *foundParm;
	MACROTEXT *prevMT;
	MACROTEXT *curMT;

	parsedMacro->numParms = 0;
	parsedMacro->firstMacroParms = NULL;
	
	foundParm = strdup(parmStr);
	if (foundParm == NULL)
	{
		LexError("Out of memory parsing bultin macro parameters.\n");
		return false;
	}

	// assume a ')' is on the end.
	endStr = strrchr(foundParm, ')');
	if (endStr == NULL)
	{
		LexWarning("Ending parenthesis not found for macro %s.\n", parsedMacro->macroName);
		endStr = foundParm + strlen(foundParm);
	}

	prevMT = NULL;
	// strip out and seperate parameters
	while (foundParm < endStr)
	{
		// allocate a macro text structure
		curMT = (MACROTEXT *)malloc(sizeof(MACROTEXT));
		if (curMT == NULL)
		{
			free(parmStr);
			LexError("Out of memory parsing bultin macro parameters.\n");
			return false;
		} 
		curMT->next = NULL;
		curMT->prev = prevMT;
		parsedMacro->numParms++;

		if (prevMT != NULL)
		{
			prevMT->next = curMT;
		}
		else
		{
			parsedMacro->firstMacroParms = curMT;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -