Exemplo n.º 1
0
static void XaviLexerLoad(XaviLexer *lexer)
{
	XaviDfaState dfaState = DFA_START;
	const char *current = lexer->location;

	if (lexer->token == EOL || lexer->token == ERROR)
		return;

	while (dfaState != DFA_END)
	switch (dfaState)
	{
	case DFA_END:
		break;
	case DFA_START:
		if(isOperator(*current))
		{
			current++;
			dfaState = DFA_TERM_CHAR;
		}
		else if (*current == 'd')
		{
			current++;
			dfaState = DFA_DICE;
		}
		else if (*current == 'e')
		{
			current++;
			dfaState = DFA_E;
		}
		else if (*current == 'p')
		{
			current++;
			dfaState = DFA_PI_1;
		}
		else if (isdigit(*current))
		{
			current++;
			dfaState = DFA_INTEGER;
		}
		else if (isalpha(*current))
		{
			current++;
			dfaState = DFA_ID;
		}
		else if (isspace(*current))
		{
			lexer->location++;
			current++;
		}
		else if (*current == '\0')
		{
			dfaState = DFA_TERM_EOI;
		}
		else
		{
			dfaState = DFA_ERROR;
		}
		break;
	case DFA_DICE:
		if (isalpha(*current))
		{
			current++;
			dfaState = DFA_ID;
		}
		else
		{
			dfaState = DFA_TERM_CHAR;
		}
		break;
	case DFA_E:
		if (isalnum(*current))
		{
			current++;
			dfaState = DFA_ID;
		}
		else
		{
			dfaState = DFA_TERM_E;
		}
		break;
	case DFA_PI_1:
		if (*current == 'i')
		{
			current++;
			dfaState = DFA_PI_2;
		}
		else if (isIdCharacter(*current))
		{
			current++;
			dfaState = DFA_ID;
		}
		else
		{
			dfaState = DFA_TERM_STRING;
		}
		break;
	case DFA_PI_2:
		if (isIdCharacter(*current))
		{
			current++;
			dfaState = DFA_ID;
		}
		else
		{
			dfaState = DFA_TERM_PI;
		}
		break;
	case DFA_ID:
		if (isalnum(*current))
		{
			current++;
		}
		else
		{
			dfaState = DFA_TERM_STRING;
		}
		break;
	case DFA_INTEGER:
		if (*current == '.')
		{
			current++;
			dfaState = DFA_FLOAT;
		}
		else if (isdigit(*current))
		{
			current++;
		}
		else
		{
			dfaState = DFA_TERM_INTEGER;
		}
		break;
	case DFA_FLOAT:
		if (isdigit(*current))
		{
			current++;
		}
		else
		{
			dfaState = DFA_TERM_FLOAT;
		}
		break;
	case DFA_TERM_INTEGER:
		lexer->token = INTEGER;
		lexer->value.i = extractInteger(lexer->location, current);
		dfaState = DFA_END;
		break;
	case DFA_TERM_FLOAT:
		lexer->token = FLOAT;
		lexer->value.f = extractFloat(lexer->location, current);
		dfaState = DFA_END;
		break;
	case DFA_TERM_E:
		lexer->token = FLOAT;
		lexer->value.f = EULER;
		dfaState = DFA_END;
		break;
	case DFA_TERM_PI:
		lexer->token = FLOAT;
		lexer->value.f = PI;
		dfaState = DFA_END;
		break;
	case DFA_TERM_CHAR:
		lexer->token = *lexer->location;
		lexer->value.i = 0;
		dfaState = DFA_END;
		break;
	case DFA_TERM_STRING:
		lexer->token = ID;
		lexer->value.s = extractString(lexer->location, current);
		dfaState = DFA_END;
		break;
	case DFA_TERM_EOI:
		lexer->token = EOL;
		lexer->value.i = 0;
		dfaState = DFA_END;
		break;
	case DFA_ERROR:
		lexer->token = ERROR;
		lexer->value.i = 0;
		dfaState = DFA_END;
		break;
	}
	lexer->location = current;
}
Exemplo n.º 2
0
void SilikoLexerNext(SilikoLexer *Lexer)
{
	SilikoDfaState dfaState = DFA_START;
	Lexeme Lex = {NULL, 0, 4};

	if (Lexer->Token.Type == SILIKO_TOK_ID)
		free(Lexer->Token.Id);

	if (Lexer->Token.Type == SILIKO_TOK_EOL || Lexer->Token.Type == SILIKO_TOK_ERROR)
		return;

	if (!(Lex.Buffer = malloc(Lex.End)))
	{
		Lexer->Token.Type = SILIKO_TOK_ERROR;
		Lexer->Token.Integer = 0;
		return;
	}

	while (dfaState != DFA_END)
	switch (dfaState)
	{
	case DFA_END:
		break;
	case DFA_START:
		if (isOperator(SilikoDataSourceGet(Lexer->Source)))
		{
			Append(&Lex, SilikoDataSourceGet(Lexer->Source));
			Append(&Lex, '\0');
			SilikoDataSourceAdvance(Lexer->Source);
			dfaState = DFA_TERM_CHAR;
		}
		else if (SilikoDataSourceGet(Lexer->Source) == 'd')
		{
			Append(&Lex, SilikoDataSourceGet(Lexer->Source));
			SilikoDataSourceAdvance(Lexer->Source);
			dfaState = DFA_DICE;
		}
		else if (SilikoDataSourceGet(Lexer->Source) == 'e')
		{
			Append(&Lex, SilikoDataSourceGet(Lexer->Source));
			SilikoDataSourceAdvance(Lexer->Source);
			dfaState = DFA_E;
		}
		else if (SilikoDataSourceGet(Lexer->Source) == 'p')
		{
			Append(&Lex, SilikoDataSourceGet(Lexer->Source));
			SilikoDataSourceAdvance(Lexer->Source);
			dfaState = DFA_PI_1;
		}
		else if (isdigit(SilikoDataSourceGet(Lexer->Source)))
		{
			Append(&Lex, SilikoDataSourceGet(Lexer->Source));
			SilikoDataSourceAdvance(Lexer->Source);
			dfaState = DFA_INTEGER;
		}
		else if (isalpha(SilikoDataSourceGet(Lexer->Source)))
		{
			Append(&Lex, SilikoDataSourceGet(Lexer->Source));
			SilikoDataSourceAdvance(Lexer->Source);
			dfaState = DFA_ID;
		}
		else if (isspace(SilikoDataSourceGet(Lexer->Source)))
		{
			SilikoDataSourceAdvance(Lexer->Source);
		}
		else if (SilikoDataSourceGet(Lexer->Source) == '\0')
		{
			dfaState = DFA_TERM_EOL;
		}
		else
		{
			dfaState = DFA_ERROR;
		}
		break;
	case DFA_DICE:
		if (isalpha(SilikoDataSourceGet(Lexer->Source)))
		{
			Append(&Lex, SilikoDataSourceGet(Lexer->Source));
			SilikoDataSourceAdvance(Lexer->Source);
			dfaState = DFA_ID;
		}
		else
		{
			Append(&Lex, '\0');
			dfaState = DFA_TERM_CHAR;
		}
		break;
	case DFA_E:
		if (isalnum(SilikoDataSourceGet(Lexer->Source)))
		{
			Append(&Lex, SilikoDataSourceGet(Lexer->Source));
			SilikoDataSourceAdvance(Lexer->Source);
			dfaState = DFA_ID;
		}
		else
		{
			Append(&Lex, '\0');
			dfaState = DFA_TERM_E;
		}
		break;
	case DFA_PI_1:
		if (SilikoDataSourceGet(Lexer->Source) == 'i')
		{
			Append(&Lex, SilikoDataSourceGet(Lexer->Source));
			SilikoDataSourceAdvance(Lexer->Source);
			dfaState = DFA_PI_2;
		}
		else if (isIdCharacter(SilikoDataSourceGet(Lexer->Source)))
		{
			Append(&Lex, SilikoDataSourceGet(Lexer->Source));
			SilikoDataSourceAdvance(Lexer->Source);
			dfaState = DFA_ID;
		}
		else
		{
			Append(&Lex, '\0');
			dfaState = DFA_TERM_STRING;
		}
		break;
	case DFA_PI_2:
		if (isIdCharacter(SilikoDataSourceGet(Lexer->Source)))
		{
			Append(&Lex, SilikoDataSourceGet(Lexer->Source));
			SilikoDataSourceAdvance(Lexer->Source);
			dfaState = DFA_ID;
		}
		else
		{
			Append(&Lex, '\0');
			dfaState = DFA_TERM_PI;
		}
		break;
	case DFA_ID:
		if (isalnum(SilikoDataSourceGet(Lexer->Source)))
		{
			Append(&Lex, SilikoDataSourceGet(Lexer->Source));
			SilikoDataSourceAdvance(Lexer->Source);
		}
		else
		{
			Append(&Lex, '\0');
			dfaState = DFA_TERM_STRING;
		}
		break;
	case DFA_INTEGER:
		if (SilikoDataSourceGet(Lexer->Source) == '.')
		{
			Append(&Lex, SilikoDataSourceGet(Lexer->Source));
			SilikoDataSourceAdvance(Lexer->Source);
			dfaState = DFA_FLOAT;
		}
		else if (isdigit(SilikoDataSourceGet(Lexer->Source)))
		{
			Append(&Lex, SilikoDataSourceGet(Lexer->Source));
			SilikoDataSourceAdvance(Lexer->Source);
		}
		else
		{
			Append(&Lex, '\0');
			dfaState = DFA_TERM_INTEGER;
		}
		break;
	case DFA_FLOAT:
		if (isdigit(SilikoDataSourceGet(Lexer->Source)))
		{
			Append(&Lex, SilikoDataSourceGet(Lexer->Source));
			SilikoDataSourceAdvance(Lexer->Source);
		}
		else
		{
			Append(&Lex, '\0');
			dfaState = DFA_TERM_FLOAT;
		}
		break;
	case DFA_TERM_INTEGER:
		Lexer->Token.Type = SILIKO_TOK_INTEGER;
		Lexer->Token.Integer = strtoll(Lex.Buffer, NULL, 10);
		free(Lex.Buffer);
		dfaState = DFA_END;
		break;
	case DFA_TERM_FLOAT:
		Lexer->Token.Type = SILIKO_TOK_FLOAT;
		Lexer->Token.Float = atof(Lex.Buffer);
		free(Lex.Buffer);
		dfaState = DFA_END;
		break;
	case DFA_TERM_E:
		Lexer->Token.Type = SILIKO_TOK_FLOAT;
		Lexer->Token.Float = EULER;
		free(Lex.Buffer);
		dfaState = DFA_END;
		break;
	case DFA_TERM_PI:
		Lexer->Token.Type = SILIKO_TOK_FLOAT;
		Lexer->Token.Float = PI;
		free(Lex.Buffer);
		dfaState = DFA_END;
		break;
	case DFA_TERM_CHAR:
		Lexer->Token.Type = Lex.Buffer[0];
		Lexer->Token.Integer = 0;
		free(Lex.Buffer);
		dfaState = DFA_END;
		break;
	case DFA_TERM_STRING:
		Lexer->Token.Type = SILIKO_TOK_ID;
		Lexer->Token.Id = Lex.Buffer;
		dfaState = DFA_END;
		break;
	case DFA_TERM_EOL:
		Lexer->Token.Type = SILIKO_TOK_EOL;
		Lexer->Token.Integer = 0;
		free(Lex.Buffer);
		dfaState = DFA_END;
		break;
	case DFA_ERROR:
		Lexer->Token.Type = SILIKO_TOK_ERROR;
		Lexer->Token.Integer = 0;
		free(Lex.Buffer);
		dfaState = DFA_END;
		break;
	}
}