From c552070230c4ad9c92b516c28658b5c74c57e46b Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 11 Jun 2015 18:12:09 +0200 Subject: Outline of parsing and serialization for manifest object model --- bpkg/manifest | 23 ++++++++++++-- bpkg/manifest-parser | 38 +++++++++++------------ bpkg/manifest-parser.cxx | 2 +- bpkg/manifest.cxx | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 22 deletions(-) (limited to 'bpkg') diff --git a/bpkg/manifest b/bpkg/manifest index bf17a64..a02a4a0 100644 --- a/bpkg/manifest +++ b/bpkg/manifest @@ -12,6 +12,10 @@ namespace bpkg { + class manifest_parser; + class manifest_serializer; + class manifest_name_value; + using strings = std::vector; // priority @@ -101,8 +105,9 @@ namespace bpkg std::string comment; }; - struct manifest + class manifest { + public: using priority_type = bpkg::priority; using url_type = bpkg::url; using email_type = bpkg::email; @@ -121,9 +126,23 @@ namespace bpkg butl::optional package_email; std::vector dependencies; std::vector requirements; + + public: + manifest (manifest_parser&); + manifest (manifest_parser&, const manifest_name_value& start); + + void + serialize (manifest_serializer&) const; }; - using manifests = std::vector; + class manifests: public std::vector + { + public: + manifests (manifest_parser&); + + void + serialize (manifest_serializer&) const; + }; } #endif // BPKG_MANIFEST diff --git a/bpkg/manifest-parser b/bpkg/manifest-parser index 070cb77..6dc3ee3 100644 --- a/bpkg/manifest-parser +++ b/bpkg/manifest-parser @@ -26,6 +26,22 @@ namespace bpkg std::string description; }; + class manifest_name_value + { + public: + std::string name; + std::string value; + + std::uint64_t name_line; + std::uint64_t name_column; + + std::uint64_t value_line; + std::uint64_t value_column; + + bool + empty () const {return name.empty () && value.empty ();} + }; + class manifest_parser { public: @@ -35,22 +51,6 @@ namespace bpkg const std::string& name () const {return name_;} - class name_value_type - { - public: - std::string name; - std::string value; - - std::uint64_t name_line; - std::uint64_t name_column; - - std::uint64_t value_line; - std::uint64_t value_column; - - bool - empty () const {return name.empty () && value.empty ();} - }; - // The first returned pair is special "start-of-manifest" with // empty name and value being the format version: {"", ""}. // After that we have a sequence of ordinary pairs which are @@ -64,7 +64,7 @@ namespace bpkg // // ({"", ""} {"", ""}* {"", ""})* {"", ""} // - name_value_type + manifest_name_value next (); private: @@ -94,10 +94,10 @@ namespace bpkg private: void - parse_name (name_value_type&); + parse_name (manifest_name_value&); void - parse_value (name_value_type&); + parse_value (manifest_name_value&); // Skip spaces and return the first peeked non-space character. // diff --git a/bpkg/manifest-parser.cxx b/bpkg/manifest-parser.cxx index 037b874..f8a2e36 100644 --- a/bpkg/manifest-parser.cxx +++ b/bpkg/manifest-parser.cxx @@ -12,7 +12,7 @@ using namespace std; namespace bpkg { using parsing = manifest_parsing; - using name_value = manifest_parser::name_value_type; + using name_value = manifest_name_value; name_value manifest_parser:: next () diff --git a/bpkg/manifest.cxx b/bpkg/manifest.cxx index 40c7a32..befd3c0 100644 --- a/bpkg/manifest.cxx +++ b/bpkg/manifest.cxx @@ -4,8 +4,87 @@ #include +#include // move() + +#include +#include + using namespace std; namespace bpkg { + using parser = manifest_parser; + using parsing = manifest_parsing; + using serializer = manifest_serializer; + using name_value = manifest_name_value; + + // manifest + // + manifest:: + manifest (parser& p): manifest (p, p.next ()) // Delegate. + { + // Make sure this is the end. + // + name_value nv (p.next ()); + if (!nv.empty ()) + throw parsing (p.name (), nv.name_line, nv.name_column, + "single manifest expected"); + } + + manifest:: + manifest (parser& p, const name_value& s) + { + // Make sure this is the start and we support the version. + // + if (!s.name.empty ()) + throw parsing (p.name (), s.name_line, s.name_column, + "start of manifest expected"); + + if (s.value != "1") + throw parsing (p.name (), s.value_line, s.value_column, + "unsupported format version"); + + for (name_value nv (p.next ()); !nv.empty (); nv = p.next ()) + { + string& n (nv.name); + string& v (nv.value); + + if (n == "name") + name = move (v); + // ... + else + throw parsing (p.name (), nv.value_line, nv.value_column, + "unknown name " + n); + } + + // Verify all non-optional values were specified. + // + } + + void manifest:: + serialize (serializer& s) const + { + s.next ("", "1"); // Start of manifest. + s.next ("name", name); + // ... + s.next ("", ""); // End of manifest. + } + + // manifests + // + manifests:: + manifests (parser& p) + { + for (name_value nv (p.next ()); !nv.empty (); nv = p.next ()) + push_back (manifest (p, nv)); + } + + void manifests:: + serialize (serializer& s) const + { + for (const manifest& m: *this) + m.serialize (s); + + s.next ("", ""); // End of stream. + } } -- cgit v1.1