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
|
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* JsonLexer.cpp
* A Json Lexer.
* See http://www.json.org/
* Copyright (C) 2014 Simon Newton
*/
#define __STDC_LIMIT_MACROS // for UINT8_MAX & friends
#include "ola/web/JsonLexer.h"
#include <math.h>
#include <stdint.h>
#include <string.h>
#include <memory>
#include <string>
#include "ola/Logging.h"
#include "ola/web/Json.h"
#include "ola/base/Macro.h"
namespace ola {
namespace web {
using std::auto_ptr;
using std::string;
static bool ParseTrimmedInput(const char **input,
JsonParserInterface *parser);
/**
* @brief Trim leading whitespace from a string.
* @param input A pointer to a pointer with the data. This is updated to point
* to the first non-whitespace character.
* @returns true if more data remains, false if the string is now empty.
* @note This is static within JsonLexer.cpp because we should be using
* strings rather than char[]s. The only reason this doesn't use a string is
* because I think we'll need a wchar on Windows.
*/
static bool TrimWhitespace(const char **input) {
while (**input != 0 &&
(**input == ' ' || **input == '\t' || **input == '\r' ||
**input == '\n')) {
(*input)++;
}
return **input != 0;
}
/**
* @brief Extract a string token from the input.
* @param input A pointer to a pointer with the data. This should point to the
* first character after the quote (") character.
* @param str A string object to store the extracted string.
* @param parser the JsonParserInterface to pass tokens to.
* @returns true if the string was extracted correctly, false otherwise.
*/
static bool ParseString(const char **input, string* str,
JsonParserInterface *parser) {
while (true) {
size_t size = strcspn(*input, "\"\\");
char c = (*input)[size];
if (c == 0) {
parser->SetError("Unterminated string");
str->clear();
return false;
}
str->append(*input, size);
*input += size + 1;
if (c == '"') {
return true;
}
if (c == '\\') {
// escape character
char append_char = 0;
switch (**input) {
case '"':
case '\\':
case '/':
append_char = **input;
break;
case 'b':
append_char = '\b';
break;
case 'f':
append_char = '\f';
break;
case 'n':
append_char = '\n';
break;
case 'r':
append_char = '\r';
break;
case 't':
append_char = '\t';
break;
case 'u':
// TODO(simonn): handle unicode
OLA_INFO << "unicode character found";
break;
default:
OLA_WARN << "Invalid escape character: \\" << **input;
parser->SetError("Invalid string escape sequence");
return false;
}
str->push_back(append_char);
(*input)++;
}
}
return true;
}
bool ExtractDigits(const char **input, uint64_t *i,
unsigned int *leading_zeros = NULL) {
*i = 0;
bool at_start = true;
unsigned int zeros = 0;
while (isdigit(**input)) {
if (at_start && **input == '0') {
zeros++;
} else if (at_start) {
at_start = false;
}
*i *= 10;
*i += **input - '0';
(*input)++;
}
if (leading_zeros) {
*leading_zeros = zeros;
}
return true;
}
/**
* Parse a JsonNumber
*/
static bool ParseNumber(const char **input, JsonParserInterface *parser) {
// The number is in the form <full>.<fractional>e<exponent>
// full and exponent are signed but we track the sign separate from the
// value.
// Only full is required.
uint64_t full = 0;
uint64_t fractional = 0;
int64_t signed_exponent = 0;
uint32_t leading_fractional_zeros = 0;
bool has_fractional = false;
bool has_exponent = false;
bool is_negative = (**input == '-');
if (is_negative) {
(*input)++;
if (**input == 0) {
return false;
}
}
if (**input == '0') {
(*input)++;
} else if (isdigit(**input)) {
ExtractDigits(input, &full);
} else {
return false;
}
if (**input == '.') {
(*input)++;
ExtractDigits(input, &fractional, &leading_fractional_zeros);
has_fractional = true;
}
if (**input == 'e' || **input == 'E') {
bool negative_exponent = false;
// Handle the exponent
(*input)++;
switch (**input) {
case '-':
negative_exponent = true;
// fall through
OLA_FALLTHROUGH
case '+':
(*input)++;
break;
default:
{};
}
if (**input == 0) {
return false;
}
uint64_t exponent;
if (isdigit(**input)) {
ExtractDigits(input, &exponent);
} else {
return false;
}
signed_exponent = (negative_exponent ? -1 : 1) * exponent;
has_exponent = true;
}
// Now we have all the components, run the correct callback.
if (has_fractional || has_exponent) {
JsonDouble::DoubleRepresentation double_rep;
double_rep.is_negative = is_negative;
double_rep.full = full;
double_rep.leading_fractional_zeros = leading_fractional_zeros;
double_rep.fractional = fractional;
double_rep.exponent = signed_exponent;
parser->Number(double_rep);
return true;
}
if (is_negative) {
int64_t value = -1 * full;
if (value < INT32_MIN || value > INT32_MAX) {
parser->Number(value);
} else {
parser->Number(static_cast<int32_t>(value));
}
} else {
if (full > UINT32_MAX) {
parser->Number(full);
} else {
parser->Number(static_cast<uint32_t>(full));
}
}
return true;
}
/**
* Starts from the first character after the '['.
*/
static bool ParseArray(const char **input, JsonParserInterface *parser) {
if (!TrimWhitespace(input)) {
parser->SetError("Unterminated array");
return false;
}
parser->OpenArray();
if (**input == ']') {
(*input)++;
parser->CloseArray();
return true;
}
while (true) {
if (!TrimWhitespace(input)) {
parser->SetError("Unterminated array");
return false;
}
bool result = ParseTrimmedInput(input, parser);
if (!result) {
OLA_INFO << "Invalid input";
return false;
}
if (!TrimWhitespace(input)) {
parser->SetError("Unterminated array");
return false;
}
switch (**input) {
case ']':
(*input)++; // move past the ]
parser->CloseArray();
return true;
case ',':
break;
default:
parser->SetError("Expected either , or ] after an array element");
return false;
}
(*input)++; // move past the ,
}
}
/**
* Starts from the first character after the '{'.
*/
static bool ParseObject(const char **input, JsonParserInterface *parser) {
if (!TrimWhitespace(input)) {
parser->SetError("Unterminated object");
return false;
}
parser->OpenObject();
if (**input == '}') {
(*input)++;
parser->CloseObject();
return true;
}
while (true) {
if (!TrimWhitespace(input)) {
parser->SetError("Unterminated object");
return false;
}
if (**input != '"') {
parser->SetError("Expected key for object");
OLA_INFO << "Expected string";
return false;
}
(*input)++;
string key;
if (!ParseString(input, &key, parser)) {
return false;
}
parser->ObjectKey(key);
if (!TrimWhitespace(input)) {
parser->SetError("Missing : after key");
return false;
}
if (**input != ':') {
parser->SetError("Incorrect character after key, should be :");
return false;
}
(*input)++;
if (!TrimWhitespace(input)) {
parser->SetError("Unterminated object");
return false;
}
bool result = ParseTrimmedInput(input, parser);
if (!result) {
return false;
}
if (!TrimWhitespace(input)) {
parser->SetError("Unterminated object");
return false;
}
switch (**input) {
case '}':
(*input)++; // move past the }
parser->CloseObject();
return true;
case ',':
break;
default:
parser->SetError("Expected either , or } after an object value");
return false;
}
(*input)++; // move past the ,
}
}
static bool ParseTrimmedInput(const char **input,
JsonParserInterface *parser) {
static const char TRUE_STR[] = "true";
static const char FALSE_STR[] = "false";
static const char NULL_STR[] = "null";
if (**input == '"') {
(*input)++;
string str;
if (ParseString(input, &str, parser)) {
parser->String(str);
return true;
}
return false;
} else if (strncmp(*input, TRUE_STR, sizeof(TRUE_STR) - 1) == 0) {
*input += sizeof(TRUE_STR) - 1;
parser->Bool(true);
return true;
} else if (strncmp(*input, FALSE_STR, sizeof(FALSE_STR) - 1) == 0) {
*input += sizeof(FALSE_STR) - 1;
parser->Bool(false);
return true;
} else if (strncmp(*input, NULL_STR, sizeof(NULL_STR) - 1) == 0) {
*input += sizeof(NULL_STR) - 1;
parser->Null();
return true;
} else if (**input == '-' || isdigit(**input)) {
return ParseNumber(input, parser);
} else if (**input == '[') {
(*input)++;
return ParseArray(input, parser);
} else if (**input == '{') {
(*input)++;
return ParseObject(input, parser);
}
parser->SetError("Invalid JSON value");
return false;
}
bool ParseRaw(const char *input, JsonParserInterface *parser) {
if (!TrimWhitespace(&input)) {
parser->SetError("No JSON data found");
return false;
}
parser->Begin();
bool result = ParseTrimmedInput(&input, parser);
if (!result) {
return false;
}
parser->End();
return !TrimWhitespace(&input);
}
bool JsonLexer::Parse(const string &input,
JsonParserInterface *parser) {
// TODO(simon): Do we need to convert to unicode here? I think this may be
// an issue on Windows. Consider mbstowcs.
// Copying the input sucks though, so we should use input.c_str() if we can.
char* input_data = new char[input.size() + 1];
strncpy(input_data, input.c_str(), input.size() + 1);
bool result = ParseRaw(input_data, parser);
delete[] input_data;
return result;
}
} // namespace web
} // namespace ola
|