Skip to content

Instantly share code, notes, and snippets.

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