aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-03-03 19:21:50 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-03-03 19:21:50 +0200
commite4f4f0bed9befbde2117af6f128d9323be3f1638 (patch)
tree6c41709cdc5fa51b0354b68897a8cc1651b8eb9c
parentca2bc859cf0001848224ff00a92fcc5d60a16e7f (diff)
Add union information to the semantics graph
New test: union.
-rw-r--r--tests/dump/driver.cxx60
-rw-r--r--tests/schema/annotation/test-000.std2
-rw-r--r--tests/schema/anonymous/test-000.std6
-rw-r--r--tests/schema/anonymous/test-001.std6
-rw-r--r--tests/schema/makefile2
-rw-r--r--tests/schema/union/makefile35
-rw-r--r--tests/schema/union/test-000.std37
-rw-r--r--tests/schema/union/test-000.xsd40
-rw-r--r--xsd-frontend/parser.cxx204
-rw-r--r--xsd-frontend/semantic-graph/elements.hxx55
-rw-r--r--xsd-frontend/semantic-graph/union.cxx2
-rw-r--r--xsd-frontend/semantic-graph/union.hxx2
-rw-r--r--xsd-frontend/traversal/union.cxx21
-rw-r--r--xsd-frontend/traversal/union.hxx6
14 files changed, 449 insertions, 29 deletions
diff --git a/tests/dump/driver.cxx b/tests/dump/driver.cxx
index d9917d3..ccfe5aa 100644
--- a/tests/dump/driver.cxx
+++ b/tests/dump/driver.cxx
@@ -55,8 +55,23 @@ namespace
<< endl;
wcout << ind << "list " <<
- (l.named () ? l.name () : String ("<anonymous>"))
- << " " << ref_name (l.argumented ().type ()) << endl;
+ (l.named () ? l.name () : String ("<anonymous>"));
+
+ SemanticGraph::Type& t (l.argumented ().type ());
+
+ if (t.named ())
+ wcout << " " << ref_name (t) << endl;
+ else
+ {
+ wcout << endl
+ << ind << "{" << endl;
+ indent++;
+
+ edge_traverser ().dispatch (l.argumented ());
+
+ indent--;
+ wcout << ind << "}" << endl;
+ }
}
};
@@ -70,7 +85,29 @@ namespace
<< endl;
wcout << ind << "union " <<
- (u.named () ? u.name () : String ("<anonymous>")) << endl;
+ (u.named () ? u.name () : String ("<anonymous>")) << " ";
+
+ for (Type::ArgumentedIterator i (u.argumented_begin ());
+ i != u.argumented_end (); ++i)
+ {
+ SemanticGraph::Type& t (i->type ());
+
+ if (t.named ())
+ wcout << ref_name (t) << " ";
+ else
+ {
+ wcout << endl
+ << ind << "{" << endl;
+ indent++;
+
+ edge_traverser ().dispatch (*i);
+
+ indent--;
+ wcout << ind << "}" << endl;
+ }
+ }
+
+ wcout << endl;
}
};
@@ -508,14 +545,12 @@ namespace
struct AnonymousNameTranslator: Transformations::AnonymousNameTranslator
{
virtual WideString
- translate (WideString const& file,
+ translate (WideString const& /*file*/,
WideString const& ns,
WideString const& name,
WideString const& xpath)
{
- wcout << "anonymous: " << file << " " << ns << " " << name << " " <<
- xpath << endl;
-
+ wcout << "anonymous: " << ns << " " << name << " " << xpath << endl;
return name;
}
};
@@ -647,6 +682,17 @@ main (Int argc, Char* argv[])
//
//
+ Traversal::Argumented argumented;
+ list >> argumented;
+ union_ >> argumented;
+
+ argumented >> list;
+ argumented >> union_;
+ argumented >> complex;
+ argumented >> enumeration;
+
+ //
+ //
Enumerator enumerator;
enumeration_names >> enumerator;
diff --git a/tests/schema/annotation/test-000.std b/tests/schema/annotation/test-000.std
index ed7f1e1..15f3e89 100644
--- a/tests/schema/annotation/test-000.std
+++ b/tests/schema/annotation/test-000.std
@@ -6,7 +6,7 @@ primary
<list type documentation>
list list http://www.w3.org/2001/XMLSchema#string
<union type documentation>
- union union
+ union union http://www.w3.org/2001/XMLSchema#int http://www.w3.org/2001/XMLSchema#string
<enumeration type documentation>
enumeration enum: http://www.w3.org/2001/XMLSchema#string
{
diff --git a/tests/schema/anonymous/test-000.std b/tests/schema/anonymous/test-000.std
index 7acb22b..1f33cb0 100644
--- a/tests/schema/anonymous/test-000.std
+++ b/tests/schema/anonymous/test-000.std
@@ -1,6 +1,6 @@
-anonymous: test anon_item anon
-anonymous: test anon_nested_item anon_nested
-anonymous: test anon_nested_item_base anon_nested_item
+anonymous: test anon_item anon
+anonymous: test anon_nested_item anon_nested
+anonymous: test anon_nested_item_base anon_nested_item
primary
{
namespace test
diff --git a/tests/schema/anonymous/test-001.std b/tests/schema/anonymous/test-001.std
index 11a2aed..cc3f2d1 100644
--- a/tests/schema/anonymous/test-001.std
+++ b/tests/schema/anonymous/test-001.std
@@ -1,6 +1,6 @@
-anonymous: test anon_base anon
-anonymous: test anon_nested_base anon_nested
-anonymous: test anon_nested_base_base anon_nested_base
+anonymous: test anon_base anon
+anonymous: test anon_nested_base anon_nested
+anonymous: test anon_nested_base_base anon_nested_base
primary
{
namespace test
diff --git a/tests/schema/makefile b/tests/schema/makefile
index 9b69ea7..b0fab92 100644
--- a/tests/schema/makefile
+++ b/tests/schema/makefile
@@ -5,7 +5,7 @@
include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make
-tests := annotation anonymous attribute-group element-group default
+tests := annotation anonymous attribute-group element-group default union
default := $(out_base)/
test := $(out_base)/.test
diff --git a/tests/schema/union/makefile b/tests/schema/union/makefile
new file mode 100644
index 0000000..498c3cc
--- /dev/null
+++ b/tests/schema/union/makefile
@@ -0,0 +1,35 @@
+# file : tests/schema/union/makefile
+# author : Boris Kolpackov <boris@codesynthesis.com>
+# copyright : Copyright (c) 2006-2010 Code Synthesis Tools CC
+# license : GNU GPL v2 + exceptions; see accompanying LICENSE file
+
+include $(dir $(lastword $(MAKEFILE_LIST)))../../../build/bootstrap.make
+
+tests := 000
+
+driver := $(out_root)/tests/dump/driver
+test := $(out_base)/.test
+clean := $(out_base)/.clean
+
+# Convenience alias for default target.
+#
+$(out_base)/: $(driver)
+
+# Test.
+#
+test_targets := $(addprefix $(out_base)/.test-,$(tests))
+
+$(test): $(test_targets)
+$(test_targets): driver := $(driver)
+
+.PHONY: $(out_base)/.test-%
+$(out_base)/.test-%: $(driver) $(src_base)/test-%.xsd $(src_base)/test-%.std
+ $(call message,test $(out_base)/$*,$(driver) $(src_base)/test-$*.xsd | diff -u $(src_base)/test-$*.std -)
+
+# Clean.
+#
+$(clean):
+
+# Dependencies.
+#
+$(call import,$(src_root)/tests/dump/makefile)
diff --git a/tests/schema/union/test-000.std b/tests/schema/union/test-000.std
new file mode 100644
index 0000000..6f95b0c
--- /dev/null
+++ b/tests/schema/union/test-000.std
@@ -0,0 +1,37 @@
+primary
+{
+ namespace test
+ {
+ union u1 http://www.w3.org/2001/XMLSchema#int http://www.w3.org/2001/XMLSchema#string
+ union u2
+ {
+ enumeration <anonymous>: http://www.w3.org/2001/XMLSchema#token
+ {
+ enumerator one
+ }
+ }
+
+ {
+ enumeration <anonymous>: http://www.w3.org/2001/XMLSchema#string
+ {
+ enumerator two
+ }
+ }
+
+ union u3 http://www.w3.org/2001/XMLSchema#int test#u1
+ {
+ enumeration <anonymous>: http://www.w3.org/2001/XMLSchema#token
+ {
+ enumerator one
+ }
+ }
+
+ {
+ enumeration <anonymous>: http://www.w3.org/2001/XMLSchema#string
+ {
+ enumerator two
+ }
+ }
+
+ }
+}
diff --git a/tests/schema/union/test-000.xsd b/tests/schema/union/test-000.xsd
new file mode 100644
index 0000000..99535de
--- /dev/null
+++ b/tests/schema/union/test-000.xsd
@@ -0,0 +1,40 @@
+<?xml version="1.0"?>
+<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:t="test" targetNamespace="test">
+
+ <simpleType name="u1">
+ <union memberTypes="int string"/>
+ </simpleType>
+
+ <simpleType name="u2">
+ <union>
+ <simpleType>
+ <restriction base="token">
+ <enumeration value="one"/>
+ </restriction>
+ </simpleType>
+ <simpleType>
+ <restriction base="string">
+ <enumeration value="two"/>
+ </restriction>
+ </simpleType>
+ </union>
+ </simpleType>
+
+ <simpleType name="u3">
+ <union memberTypes=" int
+
+t:u1 ">
+ <simpleType>
+ <restriction base="token">
+ <enumeration value="one"/>
+ </restriction>
+ </simpleType>
+ <simpleType>
+ <restriction base="string">
+ <enumeration value="two"/>
+ </restriction>
+ </simpleType>
+ </union>
+ </simpleType>
+
+</schema>
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<UnionMemberType> 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<UnionMemberTypes> ("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<Union, Union::ArgumentedIterator> na (
+ u, u.argumented_begin ());
+
+ s_.new_edge<Arguments> (
+ resolve<SemanticGraph::Type> (
+ 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<List> (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<Arguments> (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<Union> (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<UnionMemberTypes> ("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<Arguments> (*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<Names> (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<Arguments*>
+ Argumented;
+
public:
+ typedef
+ Bits::PointerIterator<Argumented::Iterator>
+ ArgumentedIterator;
+
+ typedef
+ Bits::PointerIterator<Argumented::ConstIterator>
+ 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<Node, Edge>;
- 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<Node, Edge>;
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