Beispiel #1
0
static void readToken (tokenInfo *const token)
{
	int c;

	token->type    = TOKEN_UNDEFINED;
	token->keyword = KEYWORD_NONE;
	vStringClear (token->string);

getNextChar:

	do
		c = getcFromInputFile ();
	while (c == '\t'  ||  c == ' '  ||  c == '\n');

	switch (c)
	{
		case EOF:  token->type = TOKEN_EOF;                break;
		case ';':  token->type = TOKEN_SEMICOLON;          break;
		case '!':  token->type = TOKEN_BANG;               break;
		case '}':  token->type = TOKEN_CLOSE_BRACE;        break;
		case ']':  token->type = TOKEN_CLOSE_BRACKET;      break;
		case ')':  token->type = TOKEN_CLOSE_PAREN;        break;
		case ',':  token->type = TOKEN_COMMA;              break;
		case '$':  token->type = TOKEN_DOLLAR;             break;
		case '.':  token->type = TOKEN_DOT;                break;
		case '{':  token->type = TOKEN_OPEN_BRACE;         break;
		case '[':  token->type = TOKEN_OPEN_BRACKET;       break;
		case '(':  token->type = TOKEN_OPEN_PAREN;         break;
		case '~':  token->type = TOKEN_TILDE;              break;


		case '+':
		case '*':
		case '^':
		case '=':  token->type = TOKEN_OPERATOR;           break;

		case '-':
			c = getcFromInputFile ();
			if (c == '>')
				token->type = TOKEN_CONSTRAINT;
			else if (c == '-')  /* is this the start of a comment? */
			{
				skipToCharacter ('\n');
				goto getNextChar;
			}
			else
			{
				if (!isspace (c))
					ungetcToInputFile (c);
				token->type = TOKEN_OPERATOR;
			}
			break;

		case '?':
		case ':':
		{
			int c2 = getcFromInputFile ();
			if (c2 == '=')
				token->type = TOKEN_OPERATOR;
			else
			{
				if (!isspace (c2))
					ungetcToInputFile (c2);
				if (c == ':')
					token->type = TOKEN_COLON;
				else
					token->type = TOKEN_QUESTION;
			}
			break;
		}

		case '<':
			c = getcFromInputFile ();
			if (c != '='  &&  c != '>'  &&  !isspace (c))
				ungetcToInputFile (c);
			token->type = TOKEN_OPERATOR;
			break;

		case '>':
			c = getcFromInputFile ();
			if (c != '='  &&  c != '>'  &&  !isspace (c))
				ungetcToInputFile (c);
			token->type = TOKEN_OPERATOR;
			break;

		case '/':
			c = getcFromInputFile ();
			if (c != '/'  &&  c != '='  &&  !isspace (c))
				ungetcToInputFile (c);
			token->type = TOKEN_OPERATOR;
			break;

		case '\\':
			c = getcFromInputFile ();
			if (c != '\\'  &&  !isspace (c))
				ungetcToInputFile (c);
			token->type = TOKEN_OPERATOR;
			break;

		case '"':
			token->type = TOKEN_STRING;
			parseString (token->string);
			break;

		case '\'':
			token->type = TOKEN_CHARACTER;
			parseCharacter ();
			break;

		default:
			if (isalpha (c))
			{
				parseIdentifier (token->string, c);
				token->keyword = analyzeToken (token->string, Lang_eiffel);
				if (isKeyword (token, KEYWORD_NONE))
					token->type = TOKEN_IDENTIFIER;
				else
					token->type = TOKEN_KEYWORD;
			}
			else if (isdigit (c))
			{
				vString* numeric = parseNumeric (c);
				vStringCat (token->string, numeric);
				vStringDelete (numeric);
				token->type = TOKEN_NUMERIC;
			}
			else if (isFreeOperatorChar (c))
			{
				parseFreeOperator (token->string, c);
				token->type = TOKEN_OPERATOR;
			}
			else
				token->type = TOKEN_UNDEFINED;
			break;
	}
}
Beispiel #2
0
static void readToken (tokenInfo *const token)
{
    int c;

    token->type    = TOKEN_UNDEFINED;
    token->keyword = KEYWORD_NONE;
    vStringClear (token->string);

getNextChar:

    do
        c = fileGetc ();
    while (c == '\t'  ||  c == ' '  ||  c == '\n');

    switch (c)
    {
    case EOF:
        longjmp (Exception, (int)ExceptionEOF);
        break;
    case '!':
        token->type = TOKEN_BANG;
        break;
    case '$':
        token->type = TOKEN_DOLLAR;
        break;
    case '(':
        token->type = TOKEN_OPEN_PAREN;
        break;
    case ')':
        token->type = TOKEN_CLOSE_PAREN;
        break;
    case ',':
        token->type = TOKEN_COMMA;
        break;
    case '.':
        token->type = TOKEN_DOT;
        break;
    case ';':
        goto getNextChar;
    case '[':
        token->type = TOKEN_OPEN_BRACKET;
        break;
    case ']':
        token->type = TOKEN_CLOSE_BRACKET;
        break;
    case '{':
        token->type = TOKEN_OPEN_BRACE;
        break;
    case '}':
        token->type = TOKEN_CLOSE_BRACE;
        break;
    case '~':
        token->type = TOKEN_TILDE;
        break;


    case '+':
    case '*':
    case '^':
    case '=':
        token->type = TOKEN_OPERATOR;
        break;

    case '-':
        c = fileGetc ();
        if (c == '>')
            token->type = TOKEN_CONSTRAINT;
        else if (c == '-')  /* is this the start of a comment? */
        {
            skipToCharacter ('\n');
            goto getNextChar;
        }
        else
        {
            if (!isspace (c))
                fileUngetc (c);
            token->type = TOKEN_OPERATOR;
        }
        break;

    case '?':
    case ':':
        c = fileGetc ();
        if (c == '=')
            token->type = TOKEN_OPERATOR;
        else
        {
            token->type = TOKEN_COLON;
            if (!isspace (c))
                fileUngetc (c);
        }
        break;

    case '<':
        c = fileGetc ();
        if (c != '='  &&  c != '>'  &&  !isspace (c))
            fileUngetc (c);
        token->type = TOKEN_OPERATOR;
        break;

    case '>':
        c = fileGetc ();
        if (c != '='  &&  c != '>'  &&  !isspace (c))
            fileUngetc (c);
        token->type = TOKEN_OPERATOR;
        break;

    case '/':
        c = fileGetc ();
        if (c != '/'  &&  c != '='  &&  !isspace (c))
            fileUngetc (c);
        token->type = TOKEN_OPERATOR;
        break;

    case '\\':
        c = fileGetc ();
        if (c != '\\'  &&  !isspace (c))
            fileUngetc (c);
        token->type = TOKEN_OPERATOR;
        break;

    case '"':
        token->type = TOKEN_STRING;
        parseString (token->string);
        break;

    case '\'':
        token->type = TOKEN_CHARACTER;
        parseCharacter ();
        break;

    default:
        if (isalpha (c))
        {
            parseIdentifier (token->string, c);
            token->keyword = analyzeToken (token->string);
            if (isKeyword (token, KEYWORD_NONE))
                token->type = TOKEN_IDENTIFIER;
            else
                token->type = TOKEN_KEYWORD;
        }
        else if (isdigit (c))
        {
            vStringCat (token->string, parseNumeric (c));
            token->type = TOKEN_NUMERIC;
        }
        else if (isFreeOperatorChar (c))
        {
            parseFreeOperator (token->string, c);
            token->type = TOKEN_OPERATOR;
        }
        else
        {
            token->type = TOKEN_UNDEFINED;
            Assert (! isType (token, TOKEN_UNDEFINED));
        }
        break;
    }
}