mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-22 18:54:02 +01:00
Goodbye, JSONParser...
llvm-svn: 154930
This commit is contained in:
parent
70cac8dbd0
commit
47de8bd0ef
@ -396,7 +396,6 @@ add_subdirectory(utils/FileUpdate)
|
||||
add_subdirectory(utils/count)
|
||||
add_subdirectory(utils/not)
|
||||
add_subdirectory(utils/llvm-lit)
|
||||
add_subdirectory(utils/json-bench)
|
||||
add_subdirectory(utils/yaml-bench)
|
||||
|
||||
add_subdirectory(projects)
|
||||
|
@ -1,448 +0,0 @@
|
||||
//===--- JSONParser.h - Simple JSON parser ----------------------*- C++ -*-===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This file implements a JSON parser.
|
||||
//
|
||||
// See http://www.json.org/ for an overview.
|
||||
// See http://www.ietf.org/rfc/rfc4627.txt for the full standard.
|
||||
//
|
||||
// FIXME: Currently this supports a subset of JSON. Specifically, support
|
||||
// for numbers, booleans and null for values is missing.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef LLVM_SUPPORT_JSON_PARSER_H
|
||||
#define LLVM_SUPPORT_JSON_PARSER_H
|
||||
|
||||
#include "llvm/ADT/StringRef.h"
|
||||
#include "llvm/Support/Allocator.h"
|
||||
#include "llvm/Support/Casting.h"
|
||||
#include "llvm/Support/ErrorHandling.h"
|
||||
#include "llvm/Support/SourceMgr.h"
|
||||
|
||||
namespace llvm {
|
||||
|
||||
class JSONContainer;
|
||||
class JSONString;
|
||||
class JSONValue;
|
||||
class JSONKeyValuePair;
|
||||
|
||||
/// \brief Base class for a parsable JSON atom.
|
||||
///
|
||||
/// This class has no semantics other than being a unit of JSON data which can
|
||||
/// be parsed out of a JSON document.
|
||||
class JSONAtom {
|
||||
public:
|
||||
/// \brief Possible types of JSON objects.
|
||||
enum Kind { JK_KeyValuePair, JK_Array, JK_Object, JK_String };
|
||||
|
||||
/// \brief Returns the type of this value.
|
||||
Kind getKind() const { return MyKind; }
|
||||
|
||||
static bool classof(const JSONAtom *Atom) { return true; }
|
||||
|
||||
protected:
|
||||
JSONAtom(Kind MyKind) : MyKind(MyKind) {}
|
||||
|
||||
private:
|
||||
Kind MyKind;
|
||||
};
|
||||
|
||||
/// \brief A parser for JSON text.
|
||||
///
|
||||
/// Use an object of JSONParser to iterate over the values of a JSON text.
|
||||
/// All objects are parsed during the iteration, so you can only iterate once
|
||||
/// over the JSON text, but the cost of partial iteration is minimized.
|
||||
/// Create a new JSONParser if you want to iterate multiple times.
|
||||
class JSONParser {
|
||||
public:
|
||||
/// \brief Create a JSONParser for the given input.
|
||||
///
|
||||
/// Parsing is started via parseRoot(). Access to the object returned from
|
||||
/// parseRoot() will parse the input lazily.
|
||||
JSONParser(StringRef Input, SourceMgr *SM);
|
||||
|
||||
/// \brief Returns the outermost JSON value (either an array or an object).
|
||||
///
|
||||
/// Can return NULL if the input does not start with an array or an object.
|
||||
/// The object is not parsed yet - the caller must iterate over the
|
||||
/// returned object to trigger parsing.
|
||||
///
|
||||
/// A JSONValue can be either a JSONString, JSONObject or JSONArray.
|
||||
JSONValue *parseRoot();
|
||||
|
||||
/// \brief Parses the JSON text and returns whether it is valid JSON.
|
||||
///
|
||||
/// In case validate() return false, failed() will return true and
|
||||
/// getErrorMessage() will return the parsing error.
|
||||
bool validate();
|
||||
|
||||
/// \brief Returns true if an error occurs during parsing.
|
||||
///
|
||||
/// If there was an error while parsing an object that was created by
|
||||
/// iterating over the result of 'parseRoot', 'failed' will return true.
|
||||
bool failed() const;
|
||||
|
||||
private:
|
||||
/// \brief These methods manage the implementation details of parsing new JSON
|
||||
/// atoms.
|
||||
/// @{
|
||||
JSONString *parseString();
|
||||
JSONValue *parseValue();
|
||||
JSONKeyValuePair *parseKeyValuePair();
|
||||
/// @}
|
||||
|
||||
/// \brief Helpers to parse the elements out of both forms of containers.
|
||||
/// @{
|
||||
const JSONAtom *parseElement(JSONAtom::Kind ContainerKind);
|
||||
StringRef::iterator parseFirstElement(JSONAtom::Kind ContainerKind,
|
||||
char StartChar, char EndChar,
|
||||
const JSONAtom *&Element);
|
||||
StringRef::iterator parseNextElement(JSONAtom::Kind ContainerKind,
|
||||
char EndChar,
|
||||
const JSONAtom *&Element);
|
||||
/// @}
|
||||
|
||||
/// \brief Whitespace parsing.
|
||||
/// @{
|
||||
void nextNonWhitespace();
|
||||
bool isWhitespace();
|
||||
/// @}
|
||||
|
||||
/// \brief These methods are used for error handling.
|
||||
/// {
|
||||
void setExpectedError(StringRef Expected, StringRef Found);
|
||||
void setExpectedError(StringRef Expected, char Found);
|
||||
bool errorIfAtEndOfFile(StringRef Message);
|
||||
bool errorIfNotAt(char C, StringRef Message);
|
||||
/// }
|
||||
|
||||
/// \brief Skips all elements in the given container.
|
||||
bool skipContainer(const JSONContainer &Container);
|
||||
|
||||
/// \brief Skips to the next position behind the given JSON atom.
|
||||
bool skip(const JSONAtom &Atom);
|
||||
|
||||
/// All nodes are allocated by the parser and will be deallocated when the
|
||||
/// parser is destroyed.
|
||||
BumpPtrAllocator ValueAllocator;
|
||||
|
||||
/// \brief The original input to the parser.
|
||||
MemoryBuffer *InputBuffer;
|
||||
|
||||
/// \brief The source manager used for diagnostics and buffer management.
|
||||
SourceMgr *SM;
|
||||
|
||||
/// \brief The current position in the parse stream.
|
||||
StringRef::iterator Position;
|
||||
|
||||
/// \brief The end position for fast EOF checks without introducing
|
||||
/// unnecessary dereferences.
|
||||
StringRef::iterator End;
|
||||
|
||||
/// \brief If true, an error has occurred.
|
||||
bool Failed;
|
||||
|
||||
friend class JSONContainer;
|
||||
};
|
||||
|
||||
|
||||
/// \brief Base class for JSON value objects.
|
||||
///
|
||||
/// This object represents an abstract JSON value. It is the root node behind
|
||||
/// the group of JSON entities that can represent top-level values in a JSON
|
||||
/// document. It has no API, and is just a placeholder in the type hierarchy of
|
||||
/// nodes.
|
||||
class JSONValue : public JSONAtom {
|
||||
protected:
|
||||
JSONValue(Kind MyKind) : JSONAtom(MyKind) {}
|
||||
|
||||
public:
|
||||
/// \brief dyn_cast helpers
|
||||
///@{
|
||||
static bool classof(const JSONAtom *Atom) {
|
||||
switch (Atom->getKind()) {
|
||||
case JK_Array:
|
||||
case JK_Object:
|
||||
case JK_String:
|
||||
return true;
|
||||
case JK_KeyValuePair:
|
||||
return false;
|
||||
}
|
||||
llvm_unreachable("Invalid JSONAtom kind");
|
||||
}
|
||||
static bool classof(const JSONValue *Value) { return true; }
|
||||
///@}
|
||||
};
|
||||
|
||||
/// \brief Gives access to the text of a JSON string.
|
||||
///
|
||||
/// FIXME: Implement a method to return the unescaped text.
|
||||
class JSONString : public JSONValue {
|
||||
public:
|
||||
/// \brief Returns the underlying parsed text of the string.
|
||||
///
|
||||
/// This is the unescaped content of the JSON text.
|
||||
/// See http://www.ietf.org/rfc/rfc4627.txt for details.
|
||||
StringRef getRawText() const { return RawText; }
|
||||
|
||||
private:
|
||||
JSONString(StringRef RawText) : JSONValue(JK_String), RawText(RawText) {}
|
||||
|
||||
StringRef RawText;
|
||||
|
||||
friend class JSONParser;
|
||||
|
||||
public:
|
||||
/// \brief dyn_cast helpers
|
||||
///@{
|
||||
static bool classof(const JSONAtom *Atom) {
|
||||
return Atom->getKind() == JK_String;
|
||||
}
|
||||
static bool classof(const JSONString *String) { return true; }
|
||||
///@}
|
||||
};
|
||||
|
||||
/// \brief A (key, value) tuple of type (JSONString *, JSONValue *).
|
||||
///
|
||||
/// Note that JSONKeyValuePair is not a JSONValue, it is a bare JSONAtom.
|
||||
/// JSONKeyValuePairs can be elements of a JSONObject, but not of a JSONArray.
|
||||
/// They are not viable as top-level values either.
|
||||
class JSONKeyValuePair : public JSONAtom {
|
||||
public:
|
||||
const JSONString * const Key;
|
||||
const JSONValue * const Value;
|
||||
|
||||
private:
|
||||
JSONKeyValuePair(const JSONString *Key, const JSONValue *Value)
|
||||
: JSONAtom(JK_KeyValuePair), Key(Key), Value(Value) {}
|
||||
|
||||
friend class JSONParser;
|
||||
|
||||
public:
|
||||
/// \brief dyn_cast helpers
|
||||
///@{
|
||||
static bool classof(const JSONAtom *Atom) {
|
||||
return Atom->getKind() == JK_KeyValuePair;
|
||||
}
|
||||
static bool classof(const JSONKeyValuePair *KeyValuePair) { return true; }
|
||||
///@}
|
||||
};
|
||||
|
||||
/// \brief Implementation of JSON containers (arrays and objects).
|
||||
///
|
||||
/// JSONContainers drive the lazy parsing of JSON arrays and objects via
|
||||
/// forward iterators.
|
||||
class JSONContainer : public JSONValue {
|
||||
private:
|
||||
/// \brief An iterator that parses the underlying container during iteration.
|
||||
///
|
||||
/// Iterators on the same collection use shared state, so when multiple copies
|
||||
/// of an iterator exist, only one is allowed to be used for iteration;
|
||||
/// iterating multiple copies of an iterator of the same collection will lead
|
||||
/// to undefined behavior.
|
||||
class AtomIterator {
|
||||
public:
|
||||
AtomIterator(const AtomIterator &I) : Container(I.Container) {}
|
||||
|
||||
/// \brief Iterator interface.
|
||||
///@{
|
||||
bool operator==(const AtomIterator &I) const {
|
||||
if (isEnd() || I.isEnd())
|
||||
return isEnd() == I.isEnd();
|
||||
return Container->Position == I.Container->Position;
|
||||
}
|
||||
bool operator!=(const AtomIterator &I) const {
|
||||
return !(*this == I);
|
||||
}
|
||||
AtomIterator &operator++() {
|
||||
Container->parseNextElement();
|
||||
return *this;
|
||||
}
|
||||
const JSONAtom *operator*() {
|
||||
return Container->Current;
|
||||
}
|
||||
///@}
|
||||
|
||||
private:
|
||||
/// \brief Create an iterator for which 'isEnd' returns true.
|
||||
AtomIterator() : Container(0) {}
|
||||
|
||||
/// \brief Create an iterator for the given container.
|
||||
AtomIterator(const JSONContainer *Container) : Container(Container) {}
|
||||
|
||||
bool isEnd() const {
|
||||
return Container == 0 || Container->Position == StringRef::iterator();
|
||||
}
|
||||
|
||||
const JSONContainer * const Container;
|
||||
|
||||
friend class JSONContainer;
|
||||
};
|
||||
|
||||
protected:
|
||||
/// \brief An iterator for the specified AtomT.
|
||||
///
|
||||
/// Used for the implementation of iterators for JSONArray and JSONObject.
|
||||
template <typename AtomT>
|
||||
class IteratorTemplate : public std::iterator<std::forward_iterator_tag,
|
||||
const AtomT*> {
|
||||
public:
|
||||
explicit IteratorTemplate(const AtomIterator& AtomI)
|
||||
: AtomI(AtomI) {}
|
||||
|
||||
bool operator==(const IteratorTemplate &I) const {
|
||||
return AtomI == I.AtomI;
|
||||
}
|
||||
bool operator!=(const IteratorTemplate &I) const { return !(*this == I); }
|
||||
|
||||
IteratorTemplate &operator++() {
|
||||
++AtomI;
|
||||
return *this;
|
||||
}
|
||||
|
||||
const AtomT *operator*() { return dyn_cast<AtomT>(*AtomI); }
|
||||
|
||||
private:
|
||||
AtomIterator AtomI;
|
||||
};
|
||||
|
||||
JSONContainer(JSONParser *Parser, char StartChar, char EndChar,
|
||||
JSONAtom::Kind ContainerKind)
|
||||
: JSONValue(ContainerKind), Parser(Parser),
|
||||
Position(), Current(0), Started(false),
|
||||
StartChar(StartChar), EndChar(EndChar) {}
|
||||
|
||||
/// \brief Returns a lazy parsing iterator over the container.
|
||||
///
|
||||
/// As the iterator drives the parse stream, begin() must only be called
|
||||
/// once per container.
|
||||
AtomIterator atom_begin() const {
|
||||
if (Started)
|
||||
report_fatal_error("Cannot parse container twice.");
|
||||
Started = true;
|
||||
// Set up the position and current element when we begin iterating over the
|
||||
// container.
|
||||
Position = Parser->parseFirstElement(getKind(), StartChar, EndChar, Current);
|
||||
return AtomIterator(this);
|
||||
}
|
||||
AtomIterator atom_end() const {
|
||||
return AtomIterator();
|
||||
}
|
||||
|
||||
private:
|
||||
AtomIterator atom_current() const {
|
||||
if (!Started)
|
||||
return atom_begin();
|
||||
|
||||
return AtomIterator(this);
|
||||
}
|
||||
|
||||
/// \brief Parse the next element in the container into the Current element.
|
||||
///
|
||||
/// This routine is called as an iterator into this container walks through
|
||||
/// its elements. It mutates the container's internal current node to point to
|
||||
/// the next atom of the container.
|
||||
void parseNextElement() const {
|
||||
Parser->skip(*Current);
|
||||
Position = Parser->parseNextElement(getKind(), EndChar, Current);
|
||||
}
|
||||
|
||||
// For parsing, JSONContainers call back into the JSONParser.
|
||||
JSONParser * const Parser;
|
||||
|
||||
// 'Position', 'Current' and 'Started' store the state of the parse stream
|
||||
// for iterators on the container, they don't change the container's elements
|
||||
// and are thus marked as mutable.
|
||||
mutable StringRef::iterator Position;
|
||||
mutable const JSONAtom *Current;
|
||||
mutable bool Started;
|
||||
|
||||
const char StartChar;
|
||||
const char EndChar;
|
||||
|
||||
friend class JSONParser;
|
||||
|
||||
public:
|
||||
/// \brief dyn_cast helpers
|
||||
///@{
|
||||
static bool classof(const JSONAtom *Atom) {
|
||||
switch (Atom->getKind()) {
|
||||
case JK_Array:
|
||||
case JK_Object:
|
||||
return true;
|
||||
case JK_KeyValuePair:
|
||||
case JK_String:
|
||||
return false;
|
||||
}
|
||||
llvm_unreachable("Invalid JSONAtom kind");
|
||||
}
|
||||
static bool classof(const JSONContainer *Container) { return true; }
|
||||
///@}
|
||||
};
|
||||
|
||||
/// \brief A simple JSON array.
|
||||
class JSONArray : public JSONContainer {
|
||||
public:
|
||||
typedef IteratorTemplate<JSONValue> const_iterator;
|
||||
|
||||
/// \brief Returns a lazy parsing iterator over the container.
|
||||
///
|
||||
/// As the iterator drives the parse stream, begin() must only be called
|
||||
/// once per container.
|
||||
const_iterator begin() const { return const_iterator(atom_begin()); }
|
||||
const_iterator end() const { return const_iterator(atom_end()); }
|
||||
|
||||
private:
|
||||
JSONArray(JSONParser *Parser)
|
||||
: JSONContainer(Parser, '[', ']', JSONAtom::JK_Array) {}
|
||||
|
||||
public:
|
||||
/// \brief dyn_cast helpers
|
||||
///@{
|
||||
static bool classof(const JSONAtom *Atom) {
|
||||
return Atom->getKind() == JSONAtom::JK_Array;
|
||||
}
|
||||
static bool classof(const JSONArray *Array) { return true; }
|
||||
///@}
|
||||
|
||||
friend class JSONParser;
|
||||
};
|
||||
|
||||
/// \brief A JSON object: an iterable list of JSON key-value pairs.
|
||||
class JSONObject : public JSONContainer {
|
||||
public:
|
||||
typedef IteratorTemplate<JSONKeyValuePair> const_iterator;
|
||||
|
||||
/// \brief Returns a lazy parsing iterator over the container.
|
||||
///
|
||||
/// As the iterator drives the parse stream, begin() must only be called
|
||||
/// once per container.
|
||||
const_iterator begin() const { return const_iterator(atom_begin()); }
|
||||
const_iterator end() const { return const_iterator(atom_end()); }
|
||||
|
||||
private:
|
||||
JSONObject(JSONParser *Parser)
|
||||
: JSONContainer(Parser, '{', '}', JSONAtom::JK_Object) {}
|
||||
|
||||
public:
|
||||
/// \brief dyn_cast helpers
|
||||
///@{
|
||||
static bool classof(const JSONAtom *Atom) {
|
||||
return Atom->getKind() == JSONAtom::JK_Object;
|
||||
}
|
||||
static bool classof(const JSONObject *Object) { return true; }
|
||||
///@}
|
||||
|
||||
friend class JSONParser;
|
||||
};
|
||||
|
||||
} // end namespace llvm
|
||||
|
||||
#endif // LLVM_SUPPORT_JSON_PARSER_H
|
@ -32,7 +32,6 @@ add_llvm_library(LLVMSupport
|
||||
IntrusiveRefCntPtr.cpp
|
||||
IsInf.cpp
|
||||
IsNAN.cpp
|
||||
JSONParser.cpp
|
||||
LockFileManager.cpp
|
||||
ManagedStatic.cpp
|
||||
MemoryBuffer.cpp
|
||||
|
@ -1,302 +0,0 @@
|
||||
//===--- JSONParser.cpp - Simple JSON parser ------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This file implements a JSON parser.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "llvm/Support/JSONParser.h"
|
||||
|
||||
#include "llvm/ADT/Twine.h"
|
||||
#include "llvm/Support/Casting.h"
|
||||
#include "llvm/Support/MemoryBuffer.h"
|
||||
|
||||
using namespace llvm;
|
||||
|
||||
JSONParser::JSONParser(StringRef Input, SourceMgr *SM)
|
||||
: SM(SM), Failed(false) {
|
||||
InputBuffer = MemoryBuffer::getMemBuffer(Input, "JSON");
|
||||
SM->AddNewSourceBuffer(InputBuffer, SMLoc());
|
||||
End = InputBuffer->getBuffer().end();
|
||||
Position = InputBuffer->getBuffer().begin();
|
||||
}
|
||||
|
||||
JSONValue *JSONParser::parseRoot() {
|
||||
if (Position != InputBuffer->getBuffer().begin())
|
||||
report_fatal_error("Cannot reuse JSONParser.");
|
||||
if (isWhitespace())
|
||||
nextNonWhitespace();
|
||||
if (errorIfAtEndOfFile("'[' or '{' at start of JSON text"))
|
||||
return 0;
|
||||
switch (*Position) {
|
||||
case '[':
|
||||
return new (ValueAllocator.Allocate<JSONArray>(1)) JSONArray(this);
|
||||
case '{':
|
||||
return new (ValueAllocator.Allocate<JSONObject>(1)) JSONObject(this);
|
||||
default:
|
||||
setExpectedError("'[' or '{' at start of JSON text", *Position);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool JSONParser::validate() {
|
||||
JSONValue *Root = parseRoot();
|
||||
if (Root == NULL) {
|
||||
return false;
|
||||
}
|
||||
return skip(*Root);
|
||||
}
|
||||
|
||||
bool JSONParser::skip(const JSONAtom &Atom) {
|
||||
switch(Atom.getKind()) {
|
||||
case JSONAtom::JK_Array:
|
||||
case JSONAtom::JK_Object:
|
||||
return skipContainer(*cast<JSONContainer>(&Atom));
|
||||
case JSONAtom::JK_String:
|
||||
return true;
|
||||
case JSONAtom::JK_KeyValuePair:
|
||||
return skip(*cast<JSONKeyValuePair>(&Atom)->Value);
|
||||
}
|
||||
llvm_unreachable("Impossible enum value.");
|
||||
}
|
||||
|
||||
// Sets the current error to:
|
||||
// "expected <Expected>, but found <Found>".
|
||||
void JSONParser::setExpectedError(StringRef Expected, StringRef Found) {
|
||||
SM->PrintMessage(SMLoc::getFromPointer(Position), SourceMgr::DK_Error,
|
||||
"expected " + Expected + ", but found " + Found + ".", ArrayRef<SMRange>());
|
||||
Failed = true;
|
||||
}
|
||||
|
||||
// Sets the current error to:
|
||||
// "expected <Expected>, but found <Found>".
|
||||
void JSONParser::setExpectedError(StringRef Expected, char Found) {
|
||||
setExpectedError(Expected, ("'" + StringRef(&Found, 1) + "'").str());
|
||||
}
|
||||
|
||||
// If there is no character available, returns true and sets the current error
|
||||
// to: "expected <Expected>, but found EOF.".
|
||||
bool JSONParser::errorIfAtEndOfFile(StringRef Expected) {
|
||||
if (Position == End) {
|
||||
setExpectedError(Expected, "EOF");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Sets the current error if the current character is not C to:
|
||||
// "expected 'C', but got <current character>".
|
||||
bool JSONParser::errorIfNotAt(char C, StringRef Message) {
|
||||
if (*Position != C) {
|
||||
std::string Expected =
|
||||
("'" + StringRef(&C, 1) + "' " + Message).str();
|
||||
if (Position == End)
|
||||
setExpectedError(Expected, "EOF");
|
||||
else
|
||||
setExpectedError(Expected, *Position);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Forbidding inlining improves performance by roughly 20%.
|
||||
// FIXME: Remove once llvm optimizes this to the faster version without hints.
|
||||
LLVM_ATTRIBUTE_NOINLINE static bool
|
||||
wasEscaped(StringRef::iterator First, StringRef::iterator Position);
|
||||
|
||||
// Returns whether a character at 'Position' was escaped with a leading '\'.
|
||||
// 'First' specifies the position of the first character in the string.
|
||||
static bool wasEscaped(StringRef::iterator First,
|
||||
StringRef::iterator Position) {
|
||||
assert(Position - 1 >= First);
|
||||
StringRef::iterator I = Position - 1;
|
||||
// We calulate the number of consecutive '\'s before the current position
|
||||
// by iterating backwards through our string.
|
||||
while (I >= First && *I == '\\') --I;
|
||||
// (Position - 1 - I) now contains the number of '\'s before the current
|
||||
// position. If it is odd, the character at 'Positon' was escaped.
|
||||
return (Position - 1 - I) % 2 == 1;
|
||||
}
|
||||
|
||||
// Parses a JSONString, assuming that the current position is on a quote.
|
||||
JSONString *JSONParser::parseString() {
|
||||
assert(Position != End);
|
||||
assert(!isWhitespace());
|
||||
if (errorIfNotAt('"', "at start of string"))
|
||||
return 0;
|
||||
StringRef::iterator First = Position + 1;
|
||||
|
||||
// Benchmarking shows that this loop is the hot path of the application with
|
||||
// about 2/3rd of the runtime cycles. Since escaped quotes are not the common
|
||||
// case, and multiple escaped backslashes before escaped quotes are very rare,
|
||||
// we pessimize this case to achieve a smaller inner loop in the common case.
|
||||
// We're doing that by having a quick inner loop that just scans for the next
|
||||
// quote. Once we find the quote we check the last character to see whether
|
||||
// the quote might have been escaped. If the last character is not a '\', we
|
||||
// know the quote was not escaped and have thus found the end of the string.
|
||||
// If the immediately preceding character was a '\', we have to scan backwards
|
||||
// to see whether the previous character was actually an escaped backslash, or
|
||||
// an escape character for the quote. If we find that the current quote was
|
||||
// escaped, we continue parsing for the next quote and repeat.
|
||||
// This optimization brings around 30% performance improvements.
|
||||
do {
|
||||
// Step over the current quote.
|
||||
++Position;
|
||||
// Find the next quote.
|
||||
while (Position != End && *Position != '"')
|
||||
++Position;
|
||||
if (errorIfAtEndOfFile("'\"' at end of string"))
|
||||
return 0;
|
||||
// Repeat until the previous character was not a '\' or was an escaped
|
||||
// backslash.
|
||||
} while (*(Position - 1) == '\\' && wasEscaped(First, Position));
|
||||
|
||||
return new (ValueAllocator.Allocate<JSONString>())
|
||||
JSONString(StringRef(First, Position - First));
|
||||
}
|
||||
|
||||
|
||||
// Advances the position to the next non-whitespace position.
|
||||
void JSONParser::nextNonWhitespace() {
|
||||
do {
|
||||
++Position;
|
||||
} while (isWhitespace());
|
||||
}
|
||||
|
||||
// Checks if there is a whitespace character at the current position.
|
||||
bool JSONParser::isWhitespace() {
|
||||
return *Position == ' ' || *Position == '\t' ||
|
||||
*Position == '\n' || *Position == '\r';
|
||||
}
|
||||
|
||||
bool JSONParser::failed() const {
|
||||
return Failed;
|
||||
}
|
||||
|
||||
// Parses a JSONValue, assuming that the current position is at the first
|
||||
// character of the value.
|
||||
JSONValue *JSONParser::parseValue() {
|
||||
assert(Position != End);
|
||||
assert(!isWhitespace());
|
||||
switch (*Position) {
|
||||
case '[':
|
||||
return new (ValueAllocator.Allocate<JSONArray>(1)) JSONArray(this);
|
||||
case '{':
|
||||
return new (ValueAllocator.Allocate<JSONObject>(1)) JSONObject(this);
|
||||
case '"':
|
||||
return parseString();
|
||||
default:
|
||||
setExpectedError("'[', '{' or '\"' at start of value", *Position);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Parses a JSONKeyValuePair, assuming that the current position is at the first
|
||||
// character of the key, value pair.
|
||||
JSONKeyValuePair *JSONParser::parseKeyValuePair() {
|
||||
assert(Position != End);
|
||||
assert(!isWhitespace());
|
||||
|
||||
JSONString *Key = parseString();
|
||||
if (Key == 0)
|
||||
return 0;
|
||||
|
||||
nextNonWhitespace();
|
||||
if (errorIfNotAt(':', "between key and value"))
|
||||
return 0;
|
||||
|
||||
nextNonWhitespace();
|
||||
const JSONValue *Value = parseValue();
|
||||
if (Value == 0)
|
||||
return 0;
|
||||
|
||||
return new (ValueAllocator.Allocate<JSONKeyValuePair>(1))
|
||||
JSONKeyValuePair(Key, Value);
|
||||
}
|
||||
|
||||
/// \brief Parses the first element of a JSON array or object, or closes the
|
||||
/// array.
|
||||
///
|
||||
/// The method assumes that the current position is before the first character
|
||||
/// of the element, with possible white space in between. When successful, it
|
||||
/// returns the new position after parsing the element. Otherwise, if there is
|
||||
/// no next value, it returns a default constructed StringRef::iterator.
|
||||
StringRef::iterator JSONParser::parseFirstElement(JSONAtom::Kind ContainerKind,
|
||||
char StartChar, char EndChar,
|
||||
const JSONAtom *&Element) {
|
||||
assert(*Position == StartChar);
|
||||
Element = 0;
|
||||
nextNonWhitespace();
|
||||
if (errorIfAtEndOfFile("value or end of container at start of container"))
|
||||
return StringRef::iterator();
|
||||
|
||||
if (*Position == EndChar)
|
||||
return StringRef::iterator();
|
||||
|
||||
Element = parseElement(ContainerKind);
|
||||
if (Element == 0)
|
||||
return StringRef::iterator();
|
||||
|
||||
return Position;
|
||||
}
|
||||
|
||||
/// \brief Parses the next element of a JSON array or object, or closes the
|
||||
/// array.
|
||||
///
|
||||
/// The method assumes that the current position is before the ',' which
|
||||
/// separates the next element from the current element. When successful, it
|
||||
/// returns the new position after parsing the element. Otherwise, if there is
|
||||
/// no next value, it returns a default constructed StringRef::iterator.
|
||||
StringRef::iterator JSONParser::parseNextElement(JSONAtom::Kind ContainerKind,
|
||||
char EndChar,
|
||||
const JSONAtom *&Element) {
|
||||
Element = 0;
|
||||
nextNonWhitespace();
|
||||
if (errorIfAtEndOfFile("',' or end of container for next element"))
|
||||
return 0;
|
||||
|
||||
if (*Position == ',') {
|
||||
nextNonWhitespace();
|
||||
if (errorIfAtEndOfFile("element in container"))
|
||||
return StringRef::iterator();
|
||||
|
||||
Element = parseElement(ContainerKind);
|
||||
if (Element == 0)
|
||||
return StringRef::iterator();
|
||||
|
||||
return Position;
|
||||
} else if (*Position == EndChar) {
|
||||
return StringRef::iterator();
|
||||
} else {
|
||||
setExpectedError("',' or end of container for next element", *Position);
|
||||
return StringRef::iterator();
|
||||
}
|
||||
}
|
||||
|
||||
const JSONAtom *JSONParser::parseElement(JSONAtom::Kind ContainerKind) {
|
||||
switch (ContainerKind) {
|
||||
case JSONAtom::JK_Array:
|
||||
return parseValue();
|
||||
case JSONAtom::JK_Object:
|
||||
return parseKeyValuePair();
|
||||
default:
|
||||
llvm_unreachable("Impossible code path");
|
||||
}
|
||||
}
|
||||
|
||||
bool JSONParser::skipContainer(const JSONContainer &Container) {
|
||||
for (JSONContainer::AtomIterator I = Container.atom_current(),
|
||||
E = Container.atom_end();
|
||||
I != E; ++I) {
|
||||
assert(*I != 0);
|
||||
if (!skip(**I))
|
||||
return false;
|
||||
}
|
||||
return !failed();
|
||||
}
|
@ -165,7 +165,6 @@ add_llvm_unittest(Support
|
||||
Support/CommandLineTest.cpp
|
||||
Support/ConstantRangeTest.cpp
|
||||
Support/EndianTest.cpp
|
||||
Support/JSONParserTest.cpp
|
||||
Support/LeakDetectorTest.cpp
|
||||
Support/MathExtrasTest.cpp
|
||||
Support/Path.cpp
|
||||
|
@ -1,191 +0,0 @@
|
||||
//===- unittest/Tooling/JSONParserTest ------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "llvm/Support/Casting.h"
|
||||
#include "llvm/Support/JSONParser.h"
|
||||
#include "llvm/ADT/Twine.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace llvm {
|
||||
|
||||
// Checks that the given input gives a parse error. Makes sure that an error
|
||||
// text is available and the parse fails.
|
||||
static void ExpectParseError(StringRef Message, StringRef Input) {
|
||||
SourceMgr SM;
|
||||
JSONParser Parser(Input, &SM);
|
||||
EXPECT_FALSE(Parser.validate()) << Message << ": " << Input;
|
||||
EXPECT_TRUE(Parser.failed()) << Message << ": " << Input;
|
||||
}
|
||||
|
||||
// Checks that the given input can be parsed without error.
|
||||
static void ExpectParseSuccess(StringRef Message, StringRef Input) {
|
||||
SourceMgr SM;
|
||||
JSONParser Parser(Input, &SM);
|
||||
EXPECT_TRUE(Parser.validate()) << Message << ": " << Input;
|
||||
}
|
||||
|
||||
TEST(JSONParser, FailsOnEmptyString) {
|
||||
ExpectParseError("Empty JSON text", "");
|
||||
}
|
||||
|
||||
TEST(JSONParser, FailsIfStartsWithString) {
|
||||
ExpectParseError("Top-level string", "\"x\"");
|
||||
}
|
||||
|
||||
TEST(JSONParser, ParsesEmptyArray) {
|
||||
ExpectParseSuccess("Empty array", "[]");
|
||||
}
|
||||
|
||||
TEST(JSONParser, FailsIfNotClosingArray) {
|
||||
ExpectParseError("Not closing array", "[");
|
||||
ExpectParseError("Not closing array", " [ ");
|
||||
ExpectParseError("Not closing array", " [x");
|
||||
}
|
||||
|
||||
TEST(JSONParser, ParsesEmptyArrayWithWhitespace) {
|
||||
ExpectParseSuccess("Array with spaces", " [ ] ");
|
||||
ExpectParseSuccess("All whitespaces", "\t\r\n[\t\n \t\r ]\t\r \n\n");
|
||||
}
|
||||
|
||||
TEST(JSONParser, ParsesEmptyObject) {
|
||||
ExpectParseSuccess("Empty object", "[{}]");
|
||||
}
|
||||
|
||||
TEST(JSONParser, ParsesObject) {
|
||||
ExpectParseSuccess("Object with an entry", "[{\"a\":\"/b\"}]");
|
||||
}
|
||||
|
||||
TEST(JSONParser, ParsesMultipleKeyValuePairsInObject) {
|
||||
ExpectParseSuccess("Multiple key, value pairs",
|
||||
"[{\"a\":\"/b\",\"c\":\"d\",\"e\":\"f\"}]");
|
||||
}
|
||||
|
||||
TEST(JSONParser, FailsIfNotClosingObject) {
|
||||
ExpectParseError("Missing close on empty", "[{]");
|
||||
ExpectParseError("Missing close after pair", "[{\"a\":\"b\"]");
|
||||
}
|
||||
|
||||
TEST(JSONParser, FailsIfMissingColon) {
|
||||
ExpectParseError("Missing colon between key and value", "[{\"a\"\"/b\"}]");
|
||||
ExpectParseError("Missing colon between key and value", "[{\"a\" \"b\"}]");
|
||||
}
|
||||
|
||||
TEST(JSONParser, FailsOnMissingQuote) {
|
||||
ExpectParseError("Missing open quote", "[{a\":\"b\"}]");
|
||||
ExpectParseError("Missing closing quote", "[{\"a\":\"b}]");
|
||||
}
|
||||
|
||||
TEST(JSONParser, ParsesEscapedQuotes) {
|
||||
ExpectParseSuccess("Parses escaped string in key and value",
|
||||
"[{\"a\":\"\\\"b\\\" \\\" \\\"\"}]");
|
||||
}
|
||||
|
||||
TEST(JSONParser, ParsesEmptyString) {
|
||||
ExpectParseSuccess("Parses empty string in value", "[{\"a\":\"\"}]");
|
||||
}
|
||||
|
||||
TEST(JSONParser, FailsOnMissingString) {
|
||||
ExpectParseError("Missing value", "[{\"a\":}]");
|
||||
ExpectParseError("Missing key", "[{:\"b\"}]");
|
||||
}
|
||||
|
||||
TEST(JSONParser, ParsesMultipleObjects) {
|
||||
ExpectParseSuccess(
|
||||
"Multiple objects in array",
|
||||
"["
|
||||
" { \"a\" : \"b\" },"
|
||||
" { \"a\" : \"b\" },"
|
||||
" { \"a\" : \"b\" }"
|
||||
"]");
|
||||
}
|
||||
|
||||
TEST(JSONParser, FailsOnMissingComma) {
|
||||
ExpectParseError(
|
||||
"Missing comma",
|
||||
"["
|
||||
" { \"a\" : \"b\" }"
|
||||
" { \"a\" : \"b\" }"
|
||||
"]");
|
||||
}
|
||||
|
||||
TEST(JSONParser, FailsOnSuperfluousComma) {
|
||||
ExpectParseError("Superfluous comma in array", "[ { \"a\" : \"b\" }, ]");
|
||||
ExpectParseError("Superfluous comma in object", "{ \"a\" : \"b\", }");
|
||||
}
|
||||
|
||||
TEST(JSONParser, ParsesSpacesInBetweenTokens) {
|
||||
ExpectParseSuccess(
|
||||
"Various whitespace between tokens",
|
||||
" \t \n\n \r [ \t \n\n \r"
|
||||
" \t \n\n \r { \t \n\n \r\"a\"\t \n\n \r :"
|
||||
" \t \n\n \r \"b\"\t \n\n \r } \t \n\n \r,\t \n\n \r"
|
||||
" \t \n\n \r { \t \n\n \r\"a\"\t \n\n \r :"
|
||||
" \t \n\n \r \"b\"\t \n\n \r } \t \n\n \r]\t \n\n \r");
|
||||
}
|
||||
|
||||
TEST(JSONParser, ParsesArrayOfArrays) {
|
||||
ExpectParseSuccess("Array of arrays", "[[]]");
|
||||
}
|
||||
|
||||
TEST(JSONParser, HandlesEndOfFileGracefully) {
|
||||
ExpectParseError("In string starting with EOF", "[\"");
|
||||
ExpectParseError("In string hitting EOF", "[\" ");
|
||||
ExpectParseError("In string escaping EOF", "[\" \\");
|
||||
ExpectParseError("In array starting with EOF", "[");
|
||||
ExpectParseError("In array element starting with EOF", "[[], ");
|
||||
ExpectParseError("In array hitting EOF", "[[] ");
|
||||
ExpectParseError("In array hitting EOF", "[[]");
|
||||
ExpectParseError("In object hitting EOF", "{\"\"");
|
||||
}
|
||||
|
||||
// Checks that the given string can be parsed into an identical string inside
|
||||
// of an array.
|
||||
static void ExpectCanParseString(StringRef String) {
|
||||
std::string StringInArray = (llvm::Twine("[\"") + String + "\"]").str();
|
||||
SourceMgr SM;
|
||||
JSONParser Parser(StringInArray, &SM);
|
||||
const JSONArray *ParsedArray = dyn_cast<JSONArray>(Parser.parseRoot());
|
||||
StringRef ParsedString =
|
||||
dyn_cast<JSONString>(*ParsedArray->begin())->getRawText();
|
||||
EXPECT_EQ(String, ParsedString.str());
|
||||
}
|
||||
|
||||
// Checks that parsing the given string inside an array fails.
|
||||
static void ExpectCannotParseString(StringRef String) {
|
||||
std::string StringInArray = (llvm::Twine("[\"") + String + "\"]").str();
|
||||
ExpectParseError((Twine("When parsing string \"") + String + "\"").str(),
|
||||
StringInArray);
|
||||
}
|
||||
|
||||
TEST(JSONParser, ParsesStrings) {
|
||||
ExpectCanParseString("");
|
||||
ExpectCannotParseString("\\");
|
||||
ExpectCannotParseString("\"");
|
||||
ExpectCanParseString(" ");
|
||||
ExpectCanParseString("\\ ");
|
||||
ExpectCanParseString("\\\"");
|
||||
ExpectCannotParseString("\"\\");
|
||||
ExpectCannotParseString(" \\");
|
||||
ExpectCanParseString("\\\\");
|
||||
ExpectCannotParseString("\\\\\\");
|
||||
ExpectCanParseString("\\\\\\\\");
|
||||
ExpectCanParseString("\\\" ");
|
||||
ExpectCannotParseString("\\\\\" ");
|
||||
ExpectCanParseString("\\\\\\\" ");
|
||||
ExpectCanParseString(" \\\\ \\\" \\\\\\\" ");
|
||||
}
|
||||
|
||||
TEST(JSONParser, WorksWithIteratorAlgorithms) {
|
||||
SourceMgr SM;
|
||||
JSONParser Parser("[\"1\", \"2\", \"3\", \"4\", \"5\", \"6\"]", &SM);
|
||||
const JSONArray *Array = dyn_cast<JSONArray>(Parser.parseRoot());
|
||||
EXPECT_EQ(6, std::distance(Array->begin(), Array->end()));
|
||||
}
|
||||
|
||||
} // end namespace llvm
|
@ -9,7 +9,7 @@
|
||||
|
||||
LEVEL = ..
|
||||
PARALLEL_DIRS := FileCheck FileUpdate TableGen PerfectShuffle \
|
||||
count fpcmp llvm-lit not unittest json-bench
|
||||
count fpcmp llvm-lit not unittest
|
||||
|
||||
EXTRA_DIST := check-each-file codegen-diff countloc.sh \
|
||||
DSAclean.py DSAextract.py emacs findsym.pl GenLibDeps.pl \
|
||||
|
@ -1,5 +0,0 @@
|
||||
add_llvm_utility(json-bench
|
||||
JSONBench.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(json-bench LLVMSupport)
|
@ -1,85 +0,0 @@
|
||||
//===- JSONBench - Benchmark the JSONParser implementation ----------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This program executes the JSONParser on differntly sized JSON texts and
|
||||
// outputs the run time.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "llvm/ADT/Twine.h"
|
||||
#include "llvm/Support/CommandLine.h"
|
||||
#include "llvm/Support/JSONParser.h"
|
||||
#include "llvm/Support/Timer.h"
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
|
||||
static llvm::cl::opt<bool>
|
||||
Verify("verify", llvm::cl::desc(
|
||||
"Run a quick verification useful for regression testing"),
|
||||
llvm::cl::init(false));
|
||||
|
||||
static llvm::cl::opt<unsigned>
|
||||
MemoryLimitMB("memory-limit", llvm::cl::desc(
|
||||
"Do not use more megabytes of memory"),
|
||||
llvm::cl::init(1000));
|
||||
|
||||
void benchmark(llvm::TimerGroup &Group, llvm::StringRef Name,
|
||||
llvm::StringRef JSONText) {
|
||||
llvm::Timer BaseLine((Name + ": Loop").str(), Group);
|
||||
BaseLine.startTimer();
|
||||
char C = 0;
|
||||
for (llvm::StringRef::iterator I = JSONText.begin(),
|
||||
E = JSONText.end();
|
||||
I != E; ++I) { C += *I; }
|
||||
BaseLine.stopTimer();
|
||||
volatile char DontOptimizeOut = C; (void)DontOptimizeOut;
|
||||
|
||||
llvm::Timer Parsing((Name + ": Parsing").str(), Group);
|
||||
Parsing.startTimer();
|
||||
llvm::SourceMgr SM;
|
||||
llvm::JSONParser Parser(JSONText, &SM);
|
||||
if (!Parser.validate()) {
|
||||
llvm::errs() << "Parsing error in JSON parser benchmark.\n";
|
||||
exit(1);
|
||||
}
|
||||
Parsing.stopTimer();
|
||||
}
|
||||
|
||||
std::string createJSONText(size_t MemoryMB, unsigned ValueSize) {
|
||||
std::string JSONText;
|
||||
llvm::raw_string_ostream Stream(JSONText);
|
||||
Stream << "[\n";
|
||||
size_t MemoryBytes = MemoryMB * 1024 * 1024;
|
||||
while (JSONText.size() < MemoryBytes) {
|
||||
Stream << " {\n"
|
||||
<< " \"key1\": \"" << std::string(ValueSize, '*') << "\",\n"
|
||||
<< " \"key2\": \"" << std::string(ValueSize, '*') << "\",\n"
|
||||
<< " \"key3\": \"" << std::string(ValueSize, '*') << "\"\n"
|
||||
<< " }";
|
||||
Stream.flush();
|
||||
if (JSONText.size() < MemoryBytes) Stream << ",";
|
||||
Stream << "\n";
|
||||
}
|
||||
Stream << "]\n";
|
||||
Stream.flush();
|
||||
return JSONText;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
llvm::cl::ParseCommandLineOptions(argc, argv);
|
||||
llvm::TimerGroup Group("JSON parser benchmark");
|
||||
if (Verify) {
|
||||
benchmark(Group, "Fast", createJSONText(10, 500));
|
||||
} else {
|
||||
benchmark(Group, "Small Values", createJSONText(MemoryLimitMB, 5));
|
||||
benchmark(Group, "Medium Values", createJSONText(MemoryLimitMB, 500));
|
||||
benchmark(Group, "Large Values", createJSONText(MemoryLimitMB, 50000));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,21 +0,0 @@
|
||||
##===- utils/FileCheck/Makefile ----------------------------*- Makefile -*-===##
|
||||
#
|
||||
# The LLVM Compiler Infrastructure
|
||||
#
|
||||
# This file is distributed under the University of Illinois Open Source
|
||||
# License. See LICENSE.TXT for details.
|
||||
#
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
LEVEL = ../..
|
||||
TOOLNAME = json-bench
|
||||
USEDLIBS = LLVMSupport.a
|
||||
|
||||
# This tool has no plugins, optimize startup time.
|
||||
TOOL_NO_EXPORTS = 1
|
||||
|
||||
# Don't install this utility
|
||||
NO_INSTALL = 1
|
||||
|
||||
include $(LEVEL)/Makefile.common
|
||||
|
Loading…
Reference in New Issue
Block a user