/usr/include/cajun/json/reader.inl is in libcajun-dev 2.0.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 | /******************************************************************************
Copyright (c) 2009-2010, Terry Caton
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the projecct nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
#include <cassert>
#include <set>
#include <sstream>
/*
TODO:
* better documentation
*/
namespace json
{
inline std::istream& operator >> (std::istream& istr, UnknownElement& elementRoot) {
Reader::Read(elementRoot, istr);
return istr;
}
inline Reader::Location::Location() :
m_nLine(0),
m_nLineOffset(0),
m_nDocOffset(0)
{}
//////////////////////
// Reader::InputStream
class Reader::InputStream // would be cool if we could inherit from std::istream & override "get"
{
public:
InputStream(std::istream& iStr) :
m_iStr(iStr) {}
// protect access to the input stream, so we can keeep track of document/line offsets
char Get(); // big, define outside
char Peek() {
assert(m_iStr.eof() == false); // enforce reading of only valid stream data
return m_iStr.peek();
}
bool EOS() {
m_iStr.peek(); // apparently eof flag isn't set until a character read is attempted. whatever.
return m_iStr.eof();
}
const Location& GetLocation() const { return m_Location; }
private:
std::istream& m_iStr;
Location m_Location;
};
inline char Reader::InputStream::Get()
{
assert(m_iStr.eof() == false); // enforce reading of only valid stream data
char c = m_iStr.get();
++m_Location.m_nDocOffset;
if (c == '\n') {
++m_Location.m_nLine;
m_Location.m_nLineOffset = 0;
}
else {
++m_Location.m_nLineOffset;
}
return c;
}
//////////////////////
// Reader::TokenStream
class Reader::TokenStream
{
public:
TokenStream(const Tokens& tokens);
const Token& Peek();
const Token& Get();
bool EOS() const;
private:
const Tokens& m_Tokens;
Tokens::const_iterator m_itCurrent;
};
inline Reader::TokenStream::TokenStream(const Tokens& tokens) :
m_Tokens(tokens),
m_itCurrent(tokens.begin())
{}
inline const Reader::Token& Reader::TokenStream::Peek() {
if (EOS())
{
const Token& lastToken = *m_Tokens.rbegin();
std::string sMessage = "Unexpected end of token stream";
throw ParseException(sMessage, lastToken.locBegin, lastToken.locEnd); // nowhere to point to
}
return *(m_itCurrent);
}
inline const Reader::Token& Reader::TokenStream::Get() {
const Token& token = Peek();
++m_itCurrent;
return token;
}
inline bool Reader::TokenStream::EOS() const {
return m_itCurrent == m_Tokens.end();
}
///////////////////
// Reader (finally)
inline void Reader::Read(Object& object, std::istream& istr) { Read_i(object, istr); }
inline void Reader::Read(Array& array, std::istream& istr) { Read_i(array, istr); }
inline void Reader::Read(String& string, std::istream& istr) { Read_i(string, istr); }
inline void Reader::Read(Number& number, std::istream& istr) { Read_i(number, istr); }
inline void Reader::Read(Boolean& boolean, std::istream& istr) { Read_i(boolean, istr); }
inline void Reader::Read(Null& null, std::istream& istr) { Read_i(null, istr); }
inline void Reader::Read(UnknownElement& unknown, std::istream& istr) { Read_i(unknown, istr); }
template <typename ElementTypeT>
void Reader::Read_i(ElementTypeT& element, std::istream& istr)
{
Reader reader;
Tokens tokens;
InputStream inputStream(istr);
reader.Scan(tokens, inputStream);
TokenStream tokenStream(tokens);
reader.Parse(element, tokenStream);
if (tokenStream.EOS() == false)
{
const Token& token = tokenStream.Peek();
std::string sMessage = std::string("Expected End of token stream; found ") + token.sValue;
throw ParseException(sMessage, token.locBegin, token.locEnd);
}
}
inline void Reader::Scan(Tokens& tokens, InputStream& inputStream)
{
while (EatWhiteSpace(inputStream), // ignore any leading white space...
inputStream.EOS() == false) // ...before checking for EOS
{
// if all goes well, we'll create a token each pass
Token token;
token.locBegin = inputStream.GetLocation();
// gives us null-terminated string
char sChar = inputStream.Peek();
switch (sChar)
{
case '{':
token.sValue = MatchExpectedString(inputStream, "{");
token.nType = Token::TOKEN_OBJECT_BEGIN;
break;
case '}':
token.sValue = MatchExpectedString(inputStream, "}");
token.nType = Token::TOKEN_OBJECT_END;
break;
case '[':
token.sValue = MatchExpectedString(inputStream, "[");
token.nType = Token::TOKEN_ARRAY_BEGIN;
break;
case ']':
token.sValue = MatchExpectedString(inputStream, "]");
token.nType = Token::TOKEN_ARRAY_END;
break;
case ',':
token.sValue = MatchExpectedString(inputStream, ",");
token.nType = Token::TOKEN_NEXT_ELEMENT;
break;
case ':':
token.sValue = MatchExpectedString(inputStream, ":");
token.nType = Token::TOKEN_MEMBER_ASSIGN;
break;
case '"':
token.sValue = MatchString(inputStream);
token.nType = Token::TOKEN_STRING;
break;
case '-':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
token.sValue = MatchNumber(inputStream);
token.nType = Token::TOKEN_NUMBER;
break;
case 't':
token.sValue = MatchExpectedString(inputStream, "true");
token.nType = Token::TOKEN_BOOLEAN;
break;
case 'f':
token.sValue = MatchExpectedString(inputStream, "false");
token.nType = Token::TOKEN_BOOLEAN;
break;
case 'n':
token.sValue = MatchExpectedString(inputStream, "null");
token.nType = Token::TOKEN_NULL;
break;
default:
{
std::string sErrorMessage = std::string("Unexpected character in stream: ") + sChar;
throw ScanException(sErrorMessage, inputStream.GetLocation());
}
}
token.locEnd = inputStream.GetLocation();
tokens.push_back(token);
}
}
inline void Reader::EatWhiteSpace(InputStream& inputStream)
{
while (inputStream.EOS() == false &&
::isspace(inputStream.Peek()))
inputStream.Get();
}
inline std::string Reader::MatchExpectedString(InputStream& inputStream, const std::string& sExpected)
{
std::string::const_iterator it(sExpected.begin()),
itEnd(sExpected.end());
for ( ; it != itEnd; ++it) {
if (inputStream.EOS() || // did we reach the end before finding what we're looking for...
inputStream.Get() != *it) // ...or did we find something different?
{
std::string sMessage = std::string("Expected string: ") + sExpected;
throw ScanException(sMessage, inputStream.GetLocation());
}
}
// all's well if we made it here
return sExpected;
}
inline std::string Reader::MatchString(InputStream& inputStream)
{
MatchExpectedString(inputStream, "\"");
std::string string;
while (inputStream.EOS() == false &&
inputStream.Peek() != '"')
{
char c = inputStream.Get();
// escape?
if (c == '\\' &&
inputStream.EOS() == false) // shouldn't have reached the end yet
{
c = inputStream.Get();
switch (c) {
case '/': string.push_back('/'); break;
case '"': string.push_back('"'); break;
case '\\': string.push_back('\\'); break;
case 'b': string.push_back('\b'); break;
case 'f': string.push_back('\f'); break;
case 'n': string.push_back('\n'); break;
case 'r': string.push_back('\r'); break;
case 't': string.push_back('\t'); break;
case 'u': { // convert unicode to UTF-8
int x = 0, i;
// next four characters should be hex
for (i = 0; i < 4; ++i) {
c = inputStream.Get();
if (c >= '0' && c <= '9') {
x = (x << 4) | (c - '0');
} else if (c >= 'a' && c <= 'f') {
x = (x << 4) | (c - 'a' + 10);
} else if (c >= 'A' && c <= 'F') {
x = (x << 4) | (c - 'A' + 10);
} else {
std::string sMessage = std::string("Unrecognized hexadecimal character found in string: ") + c;
throw ScanException(sMessage, inputStream.GetLocation());
}
}
// encode as UTF-8
if (x < 0x80) {
string.push_back(x);
} else if (x < 0x800) {
string.push_back(0xc0 | (x >> 6));
string.push_back(0x80 | (x & 0x3f));
} else {
string.push_back(0xe0 | (x >> 12));
string.push_back(0x80 | ((x >> 6) & 0x3f));
string.push_back(0x80 | (x & 0x3f));
}
break;
}
default: {
std::string sMessage = std::string("Unrecognized escape sequence found in string: \\") + c;
throw ScanException(sMessage, inputStream.GetLocation());
}
}
}
else {
string.push_back(c);
}
}
// eat the last '"' that we just peeked
MatchExpectedString(inputStream, "\"");
// all's well if we made it here
return string;
}
inline std::string Reader::MatchNumber(InputStream& inputStream)
{
const char sNumericChars[] = "0123456789.eE-+";
std::set<char> numericChars;
numericChars.insert(sNumericChars, sNumericChars + sizeof(sNumericChars));
std::string sNumber;
while (inputStream.EOS() == false &&
numericChars.find(inputStream.Peek()) != numericChars.end())
{
sNumber.push_back(inputStream.Get());
}
return sNumber;
}
inline void Reader::Parse(UnknownElement& element, Reader::TokenStream& tokenStream)
{
const Token& token = tokenStream.Peek();
switch (token.nType) {
case Token::TOKEN_OBJECT_BEGIN:
{
// implicit non-const cast will perform conversion for us (if necessary)
Object& object = element;
Parse(object, tokenStream);
break;
}
case Token::TOKEN_ARRAY_BEGIN:
{
Array& array = element;
Parse(array, tokenStream);
break;
}
case Token::TOKEN_STRING:
{
String& string = element;
Parse(string, tokenStream);
break;
}
case Token::TOKEN_NUMBER:
{
Number& number = element;
Parse(number, tokenStream);
break;
}
case Token::TOKEN_BOOLEAN:
{
Boolean& boolean = element;
Parse(boolean, tokenStream);
break;
}
case Token::TOKEN_NULL:
{
Null& null = element;
Parse(null, tokenStream);
break;
}
default:
{
std::string sMessage = std::string("Unexpected token: ") + token.sValue;
throw ParseException(sMessage, token.locBegin, token.locEnd);
}
}
}
inline void Reader::Parse(Object& object, Reader::TokenStream& tokenStream)
{
MatchExpectedToken(Token::TOKEN_OBJECT_BEGIN, tokenStream);
bool bContinue = (tokenStream.EOS() == false &&
tokenStream.Peek().nType != Token::TOKEN_OBJECT_END);
while (bContinue)
{
Object::Member member;
// first the member name. save the token in case we have to throw an exception
const Token& tokenName = tokenStream.Peek();
member.name = MatchExpectedToken(Token::TOKEN_STRING, tokenStream);
// ...then the key/value separator...
MatchExpectedToken(Token::TOKEN_MEMBER_ASSIGN, tokenStream);
// ...then the value itself (can be anything).
Parse(member.element, tokenStream);
// try adding it to the object (this could throw)
try
{
object.Insert(member);
}
catch (Exception&)
{
// must be a duplicate name
std::string sMessage = std::string("Duplicate object member token: ") + member.name;
throw ParseException(sMessage, tokenName.locBegin, tokenName.locEnd);
}
bContinue = (tokenStream.EOS() == false &&
tokenStream.Peek().nType == Token::TOKEN_NEXT_ELEMENT);
if (bContinue)
MatchExpectedToken(Token::TOKEN_NEXT_ELEMENT, tokenStream);
}
MatchExpectedToken(Token::TOKEN_OBJECT_END, tokenStream);
}
inline void Reader::Parse(Array& array, Reader::TokenStream& tokenStream)
{
MatchExpectedToken(Token::TOKEN_ARRAY_BEGIN, tokenStream);
bool bContinue = (tokenStream.EOS() == false &&
tokenStream.Peek().nType != Token::TOKEN_ARRAY_END);
while (bContinue)
{
// ...what's next? could be anything
Array::iterator itElement = array.Insert(UnknownElement());
UnknownElement& element = *itElement;
Parse(element, tokenStream);
bContinue = (tokenStream.EOS() == false &&
tokenStream.Peek().nType == Token::TOKEN_NEXT_ELEMENT);
if (bContinue)
MatchExpectedToken(Token::TOKEN_NEXT_ELEMENT, tokenStream);
}
MatchExpectedToken(Token::TOKEN_ARRAY_END, tokenStream);
}
inline void Reader::Parse(String& string, Reader::TokenStream& tokenStream)
{
string = MatchExpectedToken(Token::TOKEN_STRING, tokenStream);
}
inline void Reader::Parse(Number& number, Reader::TokenStream& tokenStream)
{
const Token& currentToken = tokenStream.Peek(); // might need this later for throwing exception
const std::string& sValue = MatchExpectedToken(Token::TOKEN_NUMBER, tokenStream);
std::istringstream iStr(sValue);
double dValue;
iStr >> dValue;
// did we consume all characters in the token?
if (iStr.eof() == false)
{
char c = iStr.peek();
std::string sMessage = std::string("Unexpected character in NUMBER token: ") + c;
throw ParseException(sMessage, currentToken.locBegin, currentToken.locEnd);
}
number = dValue;
}
inline void Reader::Parse(Boolean& boolean, Reader::TokenStream& tokenStream)
{
const std::string& sValue = MatchExpectedToken(Token::TOKEN_BOOLEAN, tokenStream);
boolean = (sValue == "true" ? true : false);
}
inline void Reader::Parse(Null&, Reader::TokenStream& tokenStream)
{
MatchExpectedToken(Token::TOKEN_NULL, tokenStream);
}
inline const std::string& Reader::MatchExpectedToken(Token::Type nExpected, Reader::TokenStream& tokenStream)
{
const Token& token = tokenStream.Get();
if (token.nType != nExpected)
{
std::string sMessage = std::string("Unexpected token: ") + token.sValue;
throw ParseException(sMessage, token.locBegin, token.locEnd);
}
return token.sValue;
}
} // End namespace
|