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
|
//===---------------------StructuredData.cpp ---------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lldb/Core/StructuredData.h"
#include <errno.h>
#include <stdlib.h>
#include <inttypes.h>
using namespace lldb_private;
static StructuredData::ObjectSP read_json_object (const char **ch);
static StructuredData::ObjectSP read_json_array (const char **ch);
static StructuredData::ObjectSP
read_json_number (const char **ch)
{
StructuredData::ObjectSP object_sp;
while (isspace (**ch))
(*ch)++;
const char *start_of_number = *ch;
bool is_integer = true;
bool is_float = false;
while (isdigit(**ch) || **ch == '-' || **ch == '.' || **ch == '+' || **ch == 'e' || **ch == 'E')
{
if (isdigit(**ch) == false && **ch != '-')
{
is_integer = false;
is_float = true;
}
(*ch)++;
}
while (isspace (**ch))
(*ch)++;
if (**ch == ',' || **ch == ']' || **ch == '}')
{
if (is_integer)
{
errno = 0;
uint64_t val = strtoul (start_of_number, NULL, 10);
if (errno == 0)
{
object_sp.reset(new StructuredData::Integer());
object_sp->GetAsInteger()->SetValue (val);
}
}
if (is_float)
{
char *end_of_number = NULL;
errno = 0;
double val = strtod (start_of_number, &end_of_number);
if (errno == 0 && end_of_number != start_of_number && end_of_number != NULL)
{
object_sp.reset(new StructuredData::Float());
object_sp->GetAsFloat()->SetValue (val);
}
}
}
return object_sp;
}
static std::string
read_json_string (const char **ch)
{
std::string string;
if (**ch == '"')
{
(*ch)++;
while (**ch != '\0')
{
if (**ch == '"')
{
(*ch)++;
while (isspace (**ch))
(*ch)++;
break;
}
else if (**ch == '\\')
{
switch (**ch)
{
case '"':
string.push_back('"');
*ch += 2;
break;
case '\\':
string.push_back('\\');
*ch += 2;
break;
case '/':
string.push_back('/');
*ch += 2;
break;
case 'b':
string.push_back('\b');
*ch += 2;
break;
case 'f':
string.push_back('\f');
*ch += 2;
break;
case 'n':
string.push_back('\n');
*ch += 2;
break;
case 'r':
string.push_back('\r');
*ch += 2;
break;
case 't':
string.push_back('\t');
*ch += 2;
break;
case 'u':
// FIXME handle four-hex-digits
*ch += 10;
break;
default:
*ch += 1;
}
}
else
{
string.push_back (**ch);
}
(*ch)++;
}
}
return string;
}
static StructuredData::ObjectSP
read_json_value (const char **ch)
{
StructuredData::ObjectSP object_sp;
while (isspace (**ch))
(*ch)++;
if (**ch == '{')
{
object_sp = read_json_object (ch);
}
else if (**ch == '[')
{
object_sp = read_json_array (ch);
}
else if (**ch == '"')
{
std::string string = read_json_string (ch);
object_sp.reset(new StructuredData::String());
object_sp->GetAsString()->SetValue(string);
}
else
{
if (strncmp (*ch, "true", 4) == 0)
{
object_sp.reset(new StructuredData::Boolean());
object_sp->GetAsBoolean()->SetValue(true);
*ch += 4;
}
else if (strncmp (*ch, "false", 5) == 0)
{
object_sp.reset(new StructuredData::Boolean());
object_sp->GetAsBoolean()->SetValue(false);
*ch += 5;
}
else if (strncmp (*ch, "null", 4) == 0)
{
object_sp.reset(new StructuredData::Null());
*ch += 4;
}
else
{
object_sp = read_json_number (ch);
}
}
return object_sp;
}
static StructuredData::ObjectSP
read_json_array (const char **ch)
{
StructuredData::ObjectSP object_sp;
if (**ch == '[')
{
(*ch)++;
while (isspace (**ch))
(*ch)++;
bool first_value = true;
while (**ch != '\0' && (first_value || **ch == ','))
{
if (**ch == ',')
(*ch)++;
first_value = false;
while (isspace (**ch))
(*ch)++;
lldb_private::StructuredData::ObjectSP value_sp = read_json_value (ch);
if (value_sp)
{
if (object_sp.get() == NULL)
{
object_sp.reset(new StructuredData::Array());
}
object_sp->GetAsArray()->Push (value_sp);
}
while (isspace (**ch))
(*ch)++;
}
if (**ch == ']')
{
// FIXME should throw an error if we don't see a } to close out the JSON object
(*ch)++;
while (isspace (**ch))
(*ch)++;
}
}
return object_sp;
}
static StructuredData::ObjectSP
read_json_object (const char **ch)
{
StructuredData::ObjectSP object_sp;
if (**ch == '{')
{
(*ch)++;
while (isspace (**ch))
(*ch)++;
bool first_pair = true;
while (**ch != '\0' && (first_pair || **ch == ','))
{
first_pair = false;
if (**ch == ',')
(*ch)++;
while (isspace (**ch))
(*ch)++;
if (**ch != '"')
break;
std::string key_string = read_json_string (ch);
while (isspace (**ch))
(*ch)++;
if (key_string.size() > 0 && **ch == ':')
{
(*ch)++;
while (isspace (**ch))
(*ch)++;
lldb_private::StructuredData::ObjectSP value_sp = read_json_value (ch);
if (value_sp.get())
{
if (object_sp.get() == NULL)
{
object_sp.reset(new StructuredData::Dictionary());
}
object_sp->GetAsDictionary()->AddItem (key_string.c_str(), value_sp);
}
}
while (isspace (**ch))
(*ch)++;
}
if (**ch == '}')
{
// FIXME should throw an error if we don't see a } to close out the JSON object
(*ch)++;
while (isspace (**ch))
(*ch)++;
}
}
return object_sp;
}
StructuredData::ObjectSP
StructuredData::ParseJSON (std::string json_text)
{
StructuredData::ObjectSP object_sp;
const size_t json_text_size = json_text.size();
if (json_text_size > 0)
{
const char *start_of_json_text = json_text.c_str();
const char *c = json_text.c_str();
while (*c != '\0' &&
static_cast<size_t>(c - start_of_json_text) <= json_text_size)
{
while (isspace (*c) &&
static_cast<size_t>(c - start_of_json_text) < json_text_size)
c++;
if (*c == '{')
{
object_sp = read_json_object (&c);
}
else
{
// We have bad characters here, this is likely an illegal JSON string.
return object_sp;
}
}
}
return object_sp;
}
StructuredData::ObjectSP
StructuredData::Object::GetObjectForDotSeparatedPath (llvm::StringRef path)
{
if (this->GetType() == Type::eTypeDictionary)
{
std::pair<llvm::StringRef, llvm::StringRef> match = path.split('.');
std::string key = match.first.str();
ObjectSP value = this->GetAsDictionary()->GetValueForKey (key.c_str());
if (value.get())
{
// Do we have additional words to descend? If not, return the
// value we're at right now.
if (match.second.empty())
{
return value;
}
else
{
return value->GetObjectForDotSeparatedPath (match.second);
}
}
return ObjectSP();
}
if (this->GetType() == Type::eTypeArray)
{
std::pair<llvm::StringRef, llvm::StringRef> match = path.split('[');
if (match.second.size() == 0)
{
return this->shared_from_this();
}
errno = 0;
uint64_t val = strtoul (match.second.str().c_str(), NULL, 10);
if (errno == 0)
{
return this->GetAsArray()->GetItemAtIndex(val);
}
return ObjectSP();
}
return this->shared_from_this();
}
void
StructuredData::Array::Dump (Stream &s) const
{
s << "[";
const size_t arrsize = m_items.size();
for (size_t i = 0; i < arrsize; ++i)
{
m_items[i]->Dump(s);
if (i + 1 < arrsize)
s << ",";
}
s << "]";
}
void
StructuredData::Integer::Dump (Stream &s) const
{
s.Printf ("%" PRIu64, m_value);
}
void
StructuredData::Float::Dump (Stream &s) const
{
s.Printf ("%lf", m_value);
}
void
StructuredData::Boolean::Dump (Stream &s) const
{
if (m_value == true)
s.PutCString ("true");
else
s.PutCString ("false");
}
void
StructuredData::String::Dump (Stream &s) const
{
std::string quoted;
const size_t strsize = m_value.size();
for (size_t i = 0; i < strsize ; ++i)
{
char ch = m_value[i];
if (ch == '"')
quoted.push_back ('\\');
quoted.push_back (ch);
}
s.Printf ("\"%s\"", quoted.c_str());
}
void
StructuredData::Dictionary::Dump (Stream &s) const
{
bool have_printed_one_elem = false;
s << "{";
for (collection::const_iterator iter = m_dict.begin(); iter != m_dict.end(); ++iter)
{
if (have_printed_one_elem == false)
{
have_printed_one_elem = true;
}
else
{
s << ",";
}
s << "\"" << iter->first.AsCString() << "\":";
iter->second->Dump(s);
}
s << "}";
}
void
StructuredData::Null::Dump (Stream &s) const
{
s << "null";
}
|