Pulls the implementation of skip() into JSONParser.

This is the first step towards migrating more of the parser
implementation into the parser class.

llvm-svn: 146971
This commit is contained in:
Manuel Klimek 2011-12-20 10:42:52 +00:00
parent 860d978ef6
commit 47151c37b6
2 changed files with 35 additions and 36 deletions

View File

@ -48,10 +48,6 @@ protected:
JSONAtom(Kind MyKind) : MyKind(MyKind) {}
private:
/// \brief Parses to the end of the object and returns whether parsing
/// was successful.
bool skip() const;
Kind MyKind;
friend class JSONParser;
@ -76,8 +72,8 @@ public:
/// \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 either iterate over the
/// returned object or call 'skip' to trigger parsing.
/// 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();
@ -130,6 +126,13 @@ private:
bool errorIfNotAt(char C, StringRef Message);
/// }
/// \brief Skips all elements in the given container.
template <typename ContainerT>
bool skipContainer(const ContainerT &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;
@ -191,9 +194,6 @@ public:
private:
JSONString(StringRef RawText) : JSONValue(JK_String), RawText(RawText) {}
/// \brief Skips to the next position in the parse stream.
bool skip() const { return true; };
StringRef RawText;
friend class JSONAtom;
@ -223,9 +223,6 @@ private:
JSONKeyValuePair(const JSONString *Key, const JSONValue *Value)
: JSONAtom(JK_KeyValuePair), Key(Key), Value(Value) {}
/// \brief Skips to the next position in the parse stream.
bool skip() const { return Value->skip(); };
friend class JSONAtom;
friend class JSONParser;
template <typename, char, char, JSONAtom::Kind> friend class JSONContainer;
@ -243,8 +240,7 @@ public:
/// \brief Implementation of JSON containers (arrays and objects).
///
/// JSONContainers drive the lazy parsing of JSON arrays and objects via
/// forward iterators. Call 'skip' to validate parsing of all elements of the
/// container and to position the parse stream behind the container.
/// forward iterators.
template <typename AtomT, char StartChar, char EndChar,
JSONAtom::Kind ContainerKind>
class JSONContainer : public JSONValue {
@ -320,23 +316,13 @@ private:
return const_iterator(this);
}
/// \brief Skips to the next position in the parse stream.
bool skip() const {
for (const_iterator I = current(), E = end(); I != E; ++I) {
assert(*I != 0);
if (!(*I)->skip())
return false;
}
return !Parser->failed();
}
/// \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 {
Current->skip();
Parser->skip(*Current);
Position = Parser->parseNextElement<AtomT, EndChar>(Current);
}

View File

@ -40,7 +40,30 @@ JSONValue *JSONParser::parseRoot() {
}
bool JSONParser::validate() {
return parseRoot()->skip();
return skip(*parseRoot());
}
template <typename ContainerT>
bool JSONParser::skipContainer(const ContainerT &Container) {
for (typename ContainerT::const_iterator I = Container.current(),
E = Container.end();
I != E; ++I) {
assert(*I != 0);
if (!skip(**I))
return false;
}
return !failed();
}
bool JSONParser::skip(const JSONAtom &Atom) {
switch(Atom.getKind()) {
case JSONAtom::JK_Array: return skipContainer(*cast<JSONArray>(&Atom));
case JSONAtom::JK_Object: return skipContainer(*cast<JSONObject>(&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:
@ -159,16 +182,6 @@ std::string JSONParser::getErrorMessage() const {
return ErrorMessage;
}
bool JSONAtom::skip() const {
switch (MyKind) {
case JK_Array: return cast<JSONArray>(this)->skip();
case JK_Object: return cast<JSONObject>(this)->skip();
case JK_String: return cast<JSONString>(this)->skip();
case JK_KeyValuePair: return cast<JSONKeyValuePair>(this)->skip();
}
llvm_unreachable("Impossible enum value.");
}
// Parses a JSONValue, assuming that the current position is at the first
// character of the value.
JSONValue *JSONParser::parseValue() {