Clean up the json parser a little

This commit is contained in:
Thomas Goyne 2014-07-05 07:53:13 -07:00
parent d9016cc8ea
commit a22dd0f9ca
1 changed files with 20 additions and 30 deletions

View File

@ -20,12 +20,6 @@ TODO:
*/ */
namespace json { namespace json {
std::istream& operator >> (std::istream& istr, UnknownElement& elementRoot) {
Reader::Read(elementRoot, istr);
return istr;
}
/// Wrapper around istream to keep track of document/line offsets /// Wrapper around istream to keep track of document/line offsets
class Reader::InputStream { class Reader::InputStream {
std::istream& m_iStr; std::istream& m_iStr;
@ -104,44 +98,41 @@ void Reader::Scan(Tokens& tokens, InputStream& inputStream) {
Token token; Token token;
token.locBegin = inputStream.GetLocation(); token.locBegin = inputStream.GetLocation();
// gives us null-terminated string char c = inputStream.Peek();
std::string sChar; switch (c) {
sChar.push_back(inputStream.Peek());
switch (sChar[0]) {
case '{': case '{':
token.sValue = sChar[0]; token.sValue = c;
MatchExpectedString(sChar, inputStream); inputStream.Get();
token.nType = Token::TOKEN_OBJECT_BEGIN; token.nType = Token::TOKEN_OBJECT_BEGIN;
break; break;
case '}': case '}':
token.sValue = sChar[0]; token.sValue = c;
MatchExpectedString(sChar, inputStream); inputStream.Get();
token.nType = Token::TOKEN_OBJECT_END; token.nType = Token::TOKEN_OBJECT_END;
break; break;
case '[': case '[':
token.sValue = sChar[0]; token.sValue = c;
MatchExpectedString(sChar, inputStream); inputStream.Get();
token.nType = Token::TOKEN_ARRAY_BEGIN; token.nType = Token::TOKEN_ARRAY_BEGIN;
break; break;
case ']': case ']':
token.sValue = sChar[0]; token.sValue = c;
MatchExpectedString(sChar, inputStream); inputStream.Get();
token.nType = Token::TOKEN_ARRAY_END; token.nType = Token::TOKEN_ARRAY_END;
break; break;
case ',': case ',':
token.sValue = sChar[0]; token.sValue = c;
MatchExpectedString(sChar, inputStream); inputStream.Get();
token.nType = Token::TOKEN_NEXT_ELEMENT; token.nType = Token::TOKEN_NEXT_ELEMENT;
break; break;
case ':': case ':':
token.sValue = sChar[0]; token.sValue = c;
MatchExpectedString(sChar, inputStream); inputStream.Get();
token.nType = Token::TOKEN_MEMBER_ASSIGN; token.nType = Token::TOKEN_MEMBER_ASSIGN;
break; break;
@ -183,8 +174,11 @@ void Reader::Scan(Tokens& tokens, InputStream& inputStream) {
token.nType = Token::TOKEN_NULL; token.nType = Token::TOKEN_NULL;
break; break;
case 0:
return;
default: default:
throw ScanException("Unexpected character in stream: " + sChar, inputStream.GetLocation()); throw ScanException(std::string("Unexpected character in stream: ") + c, inputStream.GetLocation());
} }
token.locEnd = inputStream.GetLocation(); token.locEnd = inputStream.GetLocation();
@ -198,13 +192,9 @@ void Reader::EatWhiteSpace(InputStream& inputStream) {
} }
void Reader::MatchExpectedString(std::string const& sExpected, InputStream& inputStream) { void Reader::MatchExpectedString(std::string const& sExpected, InputStream& inputStream) {
std::string::const_iterator it(sExpected.begin()), itEnd(sExpected.end()); for (char c : sExpected) {
for ( ; it != itEnd; ++it) { if (inputStream.EOS() || inputStream.Get() != c)
if (inputStream.EOS() || // did we reach the end before finding what we're looking for...
inputStream.Get() != *it) // ...or did we find something different?
{
throw ScanException("Expected string: " + sExpected, inputStream.GetLocation()); throw ScanException("Expected string: " + sExpected, inputStream.GetLocation());
}
} }
} }