From e4f4f0bed9befbde2117af6f128d9323be3f1638 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 3 Mar 2010 19:21:50 +0200 Subject: Add union information to the semantics graph New test: union. --- xsd-frontend/parser.cxx | 204 ++++++++++++++++++++++++++++++- xsd-frontend/semantic-graph/elements.hxx | 55 ++++++++- xsd-frontend/semantic-graph/union.cxx | 2 +- xsd-frontend/semantic-graph/union.hxx | 2 +- xsd-frontend/traversal/union.cxx | 21 +++- xsd-frontend/traversal/union.hxx | 6 + 6 files changed, 276 insertions(+), 14 deletions(-) (limited to 'xsd-frontend') diff --git a/xsd-frontend/parser.cxx b/xsd-frontend/parser.cxx index 9137725..d40cda2 100644 --- a/xsd-frontend/parser.cxx +++ b/xsd-frontend/parser.cxx @@ -62,6 +62,7 @@ namespace XSDFrontend namespace { + // // Exceptions. // @@ -198,6 +199,21 @@ namespace XSDFrontend // // + struct UnionMemberType + { + UnionMemberType (String const& ns, String const& uq) + : ns_name (ns), uq_name (uq) + { + } + + String ns_name; + String uq_name; + }; + + typedef Cult::Containers::Vector UnionMemberTypes; + + // + // struct ElementGroupRef { ElementGroupRef (String const& uq_name_, String const& ns_name_, @@ -300,6 +316,7 @@ namespace XSDFrontend Traversal::Fundamental::IdRef, Traversal::Fundamental::IdRefs, Traversal::List, + Traversal::Union, Traversal::Complex, Traversal::Enumeration, Traversal::ElementGroup, @@ -585,6 +602,45 @@ namespace XSDFrontend } Void + traverse (SemanticGraph::Union& u) + { + using SemanticGraph::Union; + + if (u.context ().count ("union-member-types")) + { + UnionMemberTypes const& m ( + u.context ().get ("union-member-types")); + + // Process it backwards so that we can just insert each + // edge in the front. + // + for (UnionMemberTypes::ConstReverseIterator i (m.rbegin ()); + i != m.rend (); i++) + { + try + { + NodeArgs na ( + u, u.argumented_begin ()); + + s_.new_edge ( + resolve ( + i->ns_name, i->uq_name, s_, cache_), na); + } + catch (NotName const& ex) + { + wcerr << u.file () << ":" << u.line () << ":" << u.column () << ": " + << "error: unable to resolve item type '" << i->uq_name << "' " + << "in namespace '" << i->ns_name << "'" << endl; + + valid_ = false; + } + } + + u.context ().remove ("union-member-types"); + } + } + + Void traverse (SemanticGraph::Complex& c) { // Avoid traversing complex type more than once. @@ -2662,12 +2718,15 @@ namespace XSDFrontend SemanticGraph::Type* Parser::Impl:: list (XML::Element const& l, XML::Element const& t) { + if (trace_) + wcout << "list" << endl; + List& node (s_->new_node (file (), t.line (), t.column ())); if (String item_type = l["itemType"]) { if (trace_) - wcout << "list item type: " << fq_name (l, item_type) << endl; + wcout << "item type: " << fq_name (l, item_type) << endl; set_type (item_type, l, node); } @@ -2720,14 +2779,153 @@ namespace XSDFrontend return &node; } + namespace + { + // + // List parsing utility functions. + // + + // Find first non-space character. + // + Size + find_ns (const WideChar* s, Size size, Size pos) + { + while (pos < size && + (s[pos] == 0x20 || // space + s[pos] == 0x0D || // carriage return + s[pos] == 0x09 || // tab + s[pos] == 0x0A)) + ++pos; + + return pos < size ? pos : String::npos; + } + + // Find first space character. + // + Size + find_s (const WideChar* s, Size size, Size pos) + { + while (pos < size && + s[pos] != 0x20 && // space + s[pos] != 0x0D && // carriage return + s[pos] != 0x09 && // tab + s[pos] != 0x0A) + ++pos; + + return pos < size ? pos : String::npos; + } + } + SemanticGraph::Type* Parser::Impl:: - union_ (XML::Element const&, XML::Element const& t) + union_ (XML::Element const& u, XML::Element const& t) { if (trace_) wcout << "union" << endl; Union& node (s_->new_node (file (), t.line (), t.column ())); + Boolean has_members (false); + + if (String members = u["memberTypes"]) + { + // Don't bother trying to resolve member types at this point + // since the order is important so we would have to insert + // the late resolutions into specific places. It is simpler + // to just do the whole resolution later. + // + const WideChar* data (members.c_str ()); + Size size (members.size ()); + + UnionMemberTypes* m (0); + + // Traverse the type list while logically collapsing spaces. + // + for (Size i (find_ns (data, size, 0)); i != String::npos;) + { + String s; + Size j (find_s (data, size, i)); + + if (j != String::npos) + { + s = String (data + i, j - i); + i = find_ns (data, size, j); + } + else + { + // Last item. + // + s = String (data + i, size - i); + i = String::npos; + } + + if (trace_) + wcout << "member type: " << fq_name (u, s) << endl; + + if (m == 0) + { + node.context ().set ("union-member-types", UnionMemberTypes ()); + m = &node.context ().get ("union-member-types"); + } + + try + { + m->push_back ( + UnionMemberType ( + namespace_name (u, s), unqualified_name (s))); + } + catch (XML::NoMapping const& ex) + { + wcerr << file () << ":" << u.line () << ":" << u.column () << ": " + << "error: unable to resolve namespace prefix " + << "'" << ex.prefix () << "' in '" << s << "'" << endl; + + valid_ = false; + } + } + + has_members = (m != 0); + } + + // Handle anonymous members. + // + push (u); + + annotation (false); + + while (more ()) + { + XML::Element e (next ()); + String name (e.name ()); + + if (trace_) + wcout << name << endl; + + Type* t (0); + + if (name == L"simpleType") t = simple_type (e); else + { + wcerr << file () << ":" << e.line () << ":" << e.column () << ": " + << "error: expected 'simpleType' instead of " + << "'" << e.name () << "'" << endl; + + valid_ = false; + } + + if (t) + s_->new_edge (*t, node); + } + + pop (); + + if (node.argumented_begin () == node.argumented_end () && !has_members) + { + wcerr << file () << ":" << u.line () << ":" << u.column () << ": " + << "error: expected 'memberTypes' attribute or 'simpleType' " + << "nested element" << endl; + + valid_ = false; + } + if (String name = t["name"]) s_->new_edge (scope (), node, name); @@ -4323,7 +4521,7 @@ namespace XSDFrontend // each refType. Instead we could lookup the target type // and then navigate through Arguments edges to see if this // type already arguments specialization that we are intersted - // in. But for now I will simply the logic by creating a new + // in. But for now I will simplify the logic by creating a new // specialization every time. // diff --git a/xsd-frontend/semantic-graph/elements.hxx b/xsd-frontend/semantic-graph/elements.hxx index 58779fa..3d8991e 100644 --- a/xsd-frontend/semantic-graph/elements.hxx +++ b/xsd-frontend/semantic-graph/elements.hxx @@ -1081,26 +1081,71 @@ namespace XSDFrontend class Specialization: public virtual Type { + typedef + Cult::Containers::Vector + Argumented; + public: + typedef + Bits::PointerIterator + ArgumentedIterator; + + typedef + Bits::PointerIterator + ArgumentedConstIterator; + + ArgumentedIterator + argumented_begin () + { + return argumented_.begin (); + } + + ArgumentedConstIterator + argumented_begin () const + { + return argumented_.begin (); + } + + ArgumentedIterator + argumented_end () + { + return argumented_.end (); + } + + ArgumentedConstIterator + argumented_end () const + { + return argumented_.end (); + } + + // Shortcut for one-argument specializations. + // Arguments& argumented () const { - return *argumented_; + return *argumented_[0]; } protected: friend class Bits::Graph; - void + using Type::add_edge_right; + + Void add_edge_right (Arguments& a) { - argumented_ = &a; + argumented_.push_back (&a); } - using Type::add_edge_right; + public: + Void + add_edge_right (Arguments& a, ArgumentedIterator const& pos) + { + argumented_.insert (pos.base (), &a); + } private: - Arguments* argumented_; + Argumented argumented_; }; diff --git a/xsd-frontend/semantic-graph/union.cxx b/xsd-frontend/semantic-graph/union.cxx index 44f2ed3..b4b4cf0 100644 --- a/xsd-frontend/semantic-graph/union.cxx +++ b/xsd-frontend/semantic-graph/union.cxx @@ -21,7 +21,7 @@ namespace XSDFrontend UnionInit () { TypeInfo ti (typeid (Union)); - ti.add_base (Access::public_, true, typeid (Type)); + ti.add_base (Access::public_, true, typeid (Specialization)); RTTI::insert (ti); } diff --git a/xsd-frontend/semantic-graph/union.hxx b/xsd-frontend/semantic-graph/union.hxx index 42115d6..62df730 100644 --- a/xsd-frontend/semantic-graph/union.hxx +++ b/xsd-frontend/semantic-graph/union.hxx @@ -12,7 +12,7 @@ namespace XSDFrontend { namespace SemanticGraph { - class Union: public virtual Type + class Union: public virtual Specialization { protected: friend class Bits::Graph; diff --git a/xsd-frontend/traversal/union.cxx b/xsd-frontend/traversal/union.cxx index f1c979d..acf419a 100644 --- a/xsd-frontend/traversal/union.cxx +++ b/xsd-frontend/traversal/union.cxx @@ -10,11 +10,12 @@ namespace XSDFrontend namespace Traversal { Void Union:: - traverse (Type& l) + traverse (Type& u) { - pre (l); - name (l); - post (l); + pre (u); + argumented (u); + name (u); + post (u); } Void Union:: @@ -23,6 +24,18 @@ namespace XSDFrontend } Void Union:: + argumented (Type& u) + { + argumented (u, *this); + } + + Void Union:: + argumented (Type& u, EdgeDispatcherBase& d) + { + iterate_and_dispatch (u.argumented_begin (), u.argumented_end (), d); + } + + Void Union:: name (Type&) { } diff --git a/xsd-frontend/traversal/union.hxx b/xsd-frontend/traversal/union.hxx index 22201c9..e3d31bd 100644 --- a/xsd-frontend/traversal/union.hxx +++ b/xsd-frontend/traversal/union.hxx @@ -22,6 +22,12 @@ namespace XSDFrontend pre (Type&); virtual Void + argumented (Type&); + + virtual Void + argumented (Type&, EdgeDispatcherBase& d); + + virtual Void name (Type&); virtual Void -- cgit v1.1