[llvm-commits] [llvm] r146971 - in /llvm/trunk: include/llvm/Support/JSONParser.h lib/Support/JSONParser.cpp

Manuel Klimek klimek at google.com
Tue Dec 20 02:42:53 PST 2011


Author: klimek
Date: Tue Dec 20 04:42:52 2011
New Revision: 146971

URL: http://llvm.org/viewvc/llvm-project?rev=146971&view=rev
Log:
Pulls the implementation of skip() into JSONParser.

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

Modified:
    llvm/trunk/include/llvm/Support/JSONParser.h
    llvm/trunk/lib/Support/JSONParser.cpp

Modified: llvm/trunk/include/llvm/Support/JSONParser.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/JSONParser.h?rev=146971&r1=146970&r2=146971&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/JSONParser.h (original)
+++ llvm/trunk/include/llvm/Support/JSONParser.h Tue Dec 20 04:42:52 2011
@@ -48,10 +48,6 @@
   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 @@
   /// \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 @@
   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 @@
 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 @@
   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 @@
 /// \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 @@
     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);
   }
 

Modified: llvm/trunk/lib/Support/JSONParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/JSONParser.cpp?rev=146971&r1=146970&r2=146971&view=diff
==============================================================================
--- llvm/trunk/lib/Support/JSONParser.cpp (original)
+++ llvm/trunk/lib/Support/JSONParser.cpp Tue Dec 20 04:42:52 2011
@@ -40,7 +40,30 @@
 }
 
 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 @@
   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() {





More information about the llvm-commits mailing list