Ejemplo n.º 1
0
ULONG yylex(void)
{
	ULONG hash, maxlen;
	char *s;
	struct sLexString *pLongestFixed = NULL;
	ULONG nFloatMask, nOldFloatMask, nFloatLen;
	ULONG linestart = AtLineStart;

	switch (lexerstate) {
	case LEX_STATE_NORMAL:
		AtLineStart = 0;

 scanagain:

		while (*pLexBuffer == ' ' || *pLexBuffer == '\t') {
			linestart = 0;
			pLexBuffer += 1;
		}

		if (*pLexBuffer == 0) {
			if (yywrap() == 0) {
				linestart = AtLineStart;
				AtLineStart = 0;
				goto scanagain;
			}
		}

		s = pLexBuffer;
		nOldFloatMask = nFloatLen = 0;
		nFloatMask = tFloatingFirstChar[(int) *s++];
		while (nFloatMask && nFloatLen < nLexBufferLeng) {
			nFloatLen += 1;
			nOldFloatMask = nFloatMask;
			if (nFloatLen == 1)
				nFloatMask &= tFloatingSecondChar[(int) *s++];
			else
				nFloatMask &= tFloatingChars[(int) *s++];
		}

		maxlen = nLexBufferLeng;
		if (nLexMaxLeng < maxlen)
			maxlen = nLexMaxLeng;

		yyleng = 0;
		hash = 0;
		s = pLexBuffer;
		while (yyleng < nLexMaxLeng) {
			yyleng += 1;
			hash = ((hash << 1) + (toupper(*s))) % LEXHASHSIZE;
			s += 1;
			if (tLexHash[hash]) {
				struct sLexString *lex;

				lex = tLexHash[hash];
				while (lex) {
					if (lex->nNameLength == yyleng) {
						if (strnicmp
						    (pLexBuffer, lex->tzName,
						     yyleng) == 0) {
							pLongestFixed = lex;
						}
					}
					lex = lex->pNext;
				}
			}

		}

		if (nFloatLen == 0 && pLongestFixed == NULL) {
			if (*pLexBuffer == '"') {
				ULONG index = 0;

				pLexBuffer += 1;
				while ((*pLexBuffer != '"')
				       && (*pLexBuffer != '\n')) {
					char ch, *marg;

					if ((ch = *pLexBuffer++) == '\\') {
						switch (ch = (*pLexBuffer++)) {
						case 'n':
							ch = '\n';
							break;
						case 't':
							ch = '\t';
							break;
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							if ((marg =
							     sym_FindMacroArg(ch
									      -
									      '0'))
							    != NULL) {
								while (*marg)
									yylval.
									    tzString
									    [index++]
									    =
									    *marg++;
								ch = 0;
							}
							break;
						case '@':
							if ((marg =
							     sym_FindMacroArg
							     (-1)) != NULL) {
								while (*marg)
									yylval.
									    tzString
									    [index++]
									    =
									    *marg++;
								ch = 0;
							}
							break;
						}
					} else if (ch == '{') {
						char sym[MAXSYMLEN];
						int i = 0;

						while ((*pLexBuffer != '}')
						       && (*pLexBuffer != '"')
						       && (*pLexBuffer !=
							   '\n')) {
							if ((ch =
							     *pLexBuffer++) ==
							    '\\') {
								switch (ch =
									(*pLexBuffer++))
								{
								case '0':
								case '1':
								case '2':
								case '3':
								case '4':
								case '5':
								case '6':
								case '7':
								case '8':
								case '9':
									if ((marg = sym_FindMacroArg(ch - '0')) != NULL) {
										while
										    (*marg)
											sym[i++] = *marg++;
										ch = 0;
									}
									break;
								case '@':
									if ((marg = sym_FindMacroArg(-1)) != NULL) {
										while
										    (*marg)
											sym[i++] = *marg++;
										ch = 0;
									}
									break;
								}
							} else
								sym[i++] = ch;
						}

						sym[i] = 0;
						index +=
						    symvaluetostring(&yylval.
								     tzString
								     [index],
								     sym);
						if (*pLexBuffer == '}')
							pLexBuffer += 1;
						else
							yyerror("Missing }");
						ch = 0;
					}

					if (ch)
						yylval.tzString[index++] = ch;
				}

				yylval.tzString[index++] = 0;

				if (*pLexBuffer == '\n')
					yyerror("Unterminated string");
				else
					pLexBuffer += 1;

				return (T_STRING);
			} else if (*pLexBuffer == '{') {
				char sym[MAXSYMLEN], ch, *marg;
				int i = 0;

				pLexBuffer += 1;

				while ((*pLexBuffer != '}')
				       && (*pLexBuffer != '\n')) {
					if ((ch = *pLexBuffer++) == '\\') {
						switch (ch = (*pLexBuffer++)) {
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							if ((marg =
							     sym_FindMacroArg(ch
									      -
									      '0'))
							    != NULL) {
								while (*marg)
									sym[i++]
									    =
									    *marg++;
								ch = 0;
							}
							break;
						case '@':
							if ((marg =
							     sym_FindMacroArg
							     (-1)) != NULL) {
								while (*marg)
									sym[i++]
									    =
									    *marg++;
								ch = 0;
							}
							break;
						}
					} else
						sym[i++] = ch;
				}
				sym[i] = 0;
				symvaluetostring(yylval.tzString, sym);
				if (*pLexBuffer == '}')
					pLexBuffer += 1;
				else
					yyerror("Missing }");

				return (T_STRING);
			} else {
				if (*pLexBuffer == '\n')
					AtLineStart = 1;

				yyleng = 1;
				return (*pLexBuffer++);
			}
		}

		if (nFloatLen == 0) {
			yyleng = pLongestFixed->nNameLength;
			pLexBuffer += yyleng;
			return (pLongestFixed->nToken);
		}

		if (pLongestFixed == NULL) {
			struct sLexFloat *tok;

			tok = lexgetfloat(nOldFloatMask);
			yyleng = nFloatLen;
			if (tok->Callback) {
				if (tok->Callback(pLexBuffer, yyleng) == 0)
					goto scanagain;
			}

			if (tok->nToken == T_ID && linestart) {
				pLexBuffer += yyleng;
				return (T_LABEL);
			} else {
				pLexBuffer += yyleng;
				return (tok->nToken);
			}
		}

		if (nFloatLen > pLongestFixed->nNameLength) {
			struct sLexFloat *tok;

			tok = lexgetfloat(nOldFloatMask);
			yyleng = nFloatLen;
			if (tok->Callback) {
				if (tok->Callback(pLexBuffer, yyleng) == 0)
					goto scanagain;
			}

			if (tok->nToken == T_ID && linestart) {
				pLexBuffer += yyleng;
				return (T_LABEL);
			} else {
				pLexBuffer += yyleng;
				return (tok->nToken);
			}
		} else {
			yyleng = pLongestFixed->nNameLength;
			pLexBuffer += yyleng;
			return (pLongestFixed->nToken);
		}
		break;

	case LEX_STATE_MACROARGS:
		{
			ULONG index = 0;

			while (*pLexBuffer == ' ' || *pLexBuffer == '\t') {
				linestart = 0;
				pLexBuffer += 1;
			}

			while ((*pLexBuffer != ',')
			       && (*pLexBuffer != '\n')) {
				char ch, *marg;

				if ((ch = *pLexBuffer++) == '\\') {
					switch (ch = (*pLexBuffer++)) {
					case 'n':
						ch = '\n';
						break;
					case 't':
						ch = '\t';
						break;
					case '0':
					case '1':
					case '2':
					case '3':
					case '4':
					case '5':
					case '6':
					case '7':
					case '8':
					case '9':
						if ((marg =
						     sym_FindMacroArg(ch -
								      '0')) !=
						    NULL) {
							while (*marg)
								yylval.
								    tzString
								    [index++] =
								    *marg++;
							ch = 0;
						}
						break;
					case '@':
						if ((marg =
						     sym_FindMacroArg(-1)) !=
						    NULL) {
							while (*marg)
								yylval.
								    tzString
								    [index++] =
								    *marg++;
							ch = 0;
						}
						break;
					}
				} else if (ch == '{') {
					char sym[MAXSYMLEN];
					int i = 0;

					while ((*pLexBuffer != '}')
					       && (*pLexBuffer != '"')
					       && (*pLexBuffer != '\n')) {
						if ((ch =
						     *pLexBuffer++) == '\\') {
							switch (ch =
								(*pLexBuffer++))
							{
							case '0':
							case '1':
							case '2':
							case '3':
							case '4':
							case '5':
							case '6':
							case '7':
							case '8':
							case '9':
								if ((marg =
								     sym_FindMacroArg
								     (ch -
								      '0')) !=
								    NULL) {
									while
									    (*marg)
										sym[i++] = *marg++;
									ch = 0;
								}
								break;
							case '@':
								if ((marg =
								     sym_FindMacroArg
								     (-1)) !=
								    NULL) {
									while
									    (*marg)
										sym[i++] = *marg++;
									ch = 0;
								}
								break;
							}
						} else
							sym[i++] = ch;
					}
					sym[i] = 0;
					index +=
					    symvaluetostring(&yylval.
							     tzString[index],
							     sym);
					if (*pLexBuffer == '}')
						pLexBuffer += 1;
					else
						yyerror("Missing }");
					ch = 0;
				}

				if (ch)
					yylval.tzString[index++] = ch;
			}

			if (index) {
				yyleng = index;
				yylval.tzString[index] = 0;
				if (*pLexBuffer == '\n') {
					while (yylval.tzString[--index] == ' ') {
						yylval.tzString[index] = 0;
						yyleng -= 1;
					}
				}
				return (T_STRING);
			} else if (*pLexBuffer == '\n') {
				pLexBuffer += 1;
				AtLineStart = 1;
				yyleng = 1;
				return ('\n');
			} else if (*pLexBuffer == ',') {
				pLexBuffer += 1;
				yyleng = 1;
				return (',');
			} else {
				yyerror("INTERNAL ERROR IN YYLEX");
				return (0);
			}
		}

		break;
	}

	yyerror("INTERNAL ERROR IN YYLEX");
	return (0);
}
Ejemplo n.º 2
0
ULONG
yylex_NORMAL()
{
	struct sLexString *pLongestFixed = NULL;
	ULONG nFloatMask, nFloatLen;
	ULONG linestart = AtLineStart;

	AtLineStart = 0;

scanagain:
	while (*pLexBuffer == ' ' || *pLexBuffer == '\t') {
		linestart = 0;
		pLexBuffer++;
	}

	if (*pLexBuffer == 0) {
		// Reached the end of a file, macro, or rept.
		if (yywrap() == 0) {
			linestart = AtLineStart;
			AtLineStart = 0;
			goto scanagain;
		}
	}

	// Try to match an identifier, macro argument (e.g. \1),
	// or numeric literal.
	yylex_GetFloatMaskAndFloatLen(&nFloatMask, &nFloatLen);

	// Try to match a keyword or operator.
	pLongestFixed = yylex_GetLongestFixed();

	if (nFloatLen == 0 && pLongestFixed == NULL) {
		// No keyword, identifier, operator, or numerical literal matches.

		if (*pLexBuffer == '"') {
			pLexBuffer++;
			yylex_ReadQuotedString();
			return T_STRING;
		} else if (*pLexBuffer == '{') {
			pLexBuffer++;
			yylex_ReadBracketedSymbol(yylval.tzString, 0);
			return T_STRING;
		} else {
			// It's not a keyword, operator, identifier, macro argument,
			// numeric literal, string, or bracketed symbol, so just return
			// the ASCII character.
			if (*pLexBuffer == '\n')
				AtLineStart = 1;

			return *pLexBuffer++;
		}
	}

	if (pLongestFixed == NULL || nFloatLen > pLongestFixed->nNameLength) {
		// Longest match was an identifier, macro argument, or numeric literal.
		struct sLexFloat *token = lexgetfloat(nFloatMask);

		if (token->Callback) {
			int done = token->Callback(pLexBuffer, nFloatLen);
			if (!done)
				goto scanagain;
		}

		pLexBuffer += nFloatLen;

		if (token->nToken == T_ID && linestart) {
			return T_LABEL;
		} else {
			return token->nToken;
		}
	}

	// Longest match was a keyword or operator.
	pLexBuffer += pLongestFixed->nNameLength;
	return pLongestFixed->nToken;
}