Skip to content

Instantly share code, notes, and snippets.

@thevar1able
Created September 24, 2015 16:37
Show Gist options
  • Save thevar1able/64a6c66e18f0f8c6e639 to your computer and use it in GitHub Desktop.
Save thevar1able/64a6c66e18f0f8c6e639 to your computer and use it in GitHub Desktop.
token tryInt(std::string str)
{
token result("INVALID", "");
int state = 0;
for(auto x : str)
{
switch(state)
{
case 0:
if(x == '0')
{
result.text.push_back(x);
state = 4;
result.type = "INT";
}
else if(x == '-')
{
result.text.push_back(x);
state = 1;
}
else if(x >= '1' && x <= '9')
{
result.text.push_back(x);
state = 2;
result.type = "INT";
} else return result;
break;
case 1:
if(x >= '1' && x <= '9')
{
result.text.push_back(x);
state = 2;
result.type = "INT";
} else return result;
break;
case 2:
if(x >= '0' && x <= '9')
{
result.text.push_back(x);
}
else if(x == 'l' || x == 'L')
{
result.text.push_back(x);
result.type = "INT";
state = 3;
}
else
{
result.type = "INVALID";
return result;
}
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment