From 707cc94fe52463870a9c6c8e2e66eaaa389e601d Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 24 Feb 2009 15:16:26 +0200 Subject: Start tracking XSD/e with git after version 3.0.0 --- xsde/cxx/parser/attribute-validation-source.cxx | 468 +++++ xsde/cxx/parser/attribute-validation-source.hxx | 22 + xsde/cxx/parser/characters-validation-source.cxx | 76 + xsde/cxx/parser/characters-validation-source.hxx | 22 + xsde/cxx/parser/cli.hxx | 156 ++ xsde/cxx/parser/driver-source.cxx | 1139 ++++++++++++ xsde/cxx/parser/driver-source.hxx | 22 + xsde/cxx/parser/element-validation-source.cxx | 2120 ++++++++++++++++++++++ xsde/cxx/parser/element-validation-source.hxx | 22 + xsde/cxx/parser/elements.cxx | 258 +++ xsde/cxx/parser/elements.hxx | 431 +++++ xsde/cxx/parser/generator.cxx | 1615 ++++++++++++++++ xsde/cxx/parser/generator.hxx | 56 + xsde/cxx/parser/impl-header.cxx | 309 ++++ xsde/cxx/parser/impl-header.hxx | 22 + xsde/cxx/parser/impl-source.cxx | 543 ++++++ xsde/cxx/parser/impl-source.hxx | 22 + xsde/cxx/parser/name-processor.cxx | 1326 ++++++++++++++ xsde/cxx/parser/name-processor.hxx | 32 + xsde/cxx/parser/parser-forward.cxx | 112 ++ xsde/cxx/parser/parser-forward.hxx | 22 + xsde/cxx/parser/parser-header.cxx | 1818 +++++++++++++++++++ xsde/cxx/parser/parser-header.hxx | 22 + xsde/cxx/parser/parser-inline.cxx | 721 ++++++++ xsde/cxx/parser/parser-inline.hxx | 22 + xsde/cxx/parser/parser-source.cxx | 1626 +++++++++++++++++ xsde/cxx/parser/parser-source.hxx | 22 + xsde/cxx/parser/print-impl-common.hxx | 1063 +++++++++++ xsde/cxx/parser/state-processor.cxx | 319 ++++ xsde/cxx/parser/state-processor.hxx | 28 + xsde/cxx/parser/type-processor.cxx | 352 ++++ xsde/cxx/parser/type-processor.hxx | 34 + xsde/cxx/parser/validator.cxx | 704 +++++++ xsde/cxx/parser/validator.hxx | 36 + 34 files changed, 15562 insertions(+) create mode 100644 xsde/cxx/parser/attribute-validation-source.cxx create mode 100644 xsde/cxx/parser/attribute-validation-source.hxx create mode 100644 xsde/cxx/parser/characters-validation-source.cxx create mode 100644 xsde/cxx/parser/characters-validation-source.hxx create mode 100644 xsde/cxx/parser/cli.hxx create mode 100644 xsde/cxx/parser/driver-source.cxx create mode 100644 xsde/cxx/parser/driver-source.hxx create mode 100644 xsde/cxx/parser/element-validation-source.cxx create mode 100644 xsde/cxx/parser/element-validation-source.hxx create mode 100644 xsde/cxx/parser/elements.cxx create mode 100644 xsde/cxx/parser/elements.hxx create mode 100644 xsde/cxx/parser/generator.cxx create mode 100644 xsde/cxx/parser/generator.hxx create mode 100644 xsde/cxx/parser/impl-header.cxx create mode 100644 xsde/cxx/parser/impl-header.hxx create mode 100644 xsde/cxx/parser/impl-source.cxx create mode 100644 xsde/cxx/parser/impl-source.hxx create mode 100644 xsde/cxx/parser/name-processor.cxx create mode 100644 xsde/cxx/parser/name-processor.hxx create mode 100644 xsde/cxx/parser/parser-forward.cxx create mode 100644 xsde/cxx/parser/parser-forward.hxx create mode 100644 xsde/cxx/parser/parser-header.cxx create mode 100644 xsde/cxx/parser/parser-header.hxx create mode 100644 xsde/cxx/parser/parser-inline.cxx create mode 100644 xsde/cxx/parser/parser-inline.hxx create mode 100644 xsde/cxx/parser/parser-source.cxx create mode 100644 xsde/cxx/parser/parser-source.hxx create mode 100644 xsde/cxx/parser/print-impl-common.hxx create mode 100644 xsde/cxx/parser/state-processor.cxx create mode 100644 xsde/cxx/parser/state-processor.hxx create mode 100644 xsde/cxx/parser/type-processor.cxx create mode 100644 xsde/cxx/parser/type-processor.hxx create mode 100644 xsde/cxx/parser/validator.cxx create mode 100644 xsde/cxx/parser/validator.hxx (limited to 'xsde/cxx/parser') diff --git a/xsde/cxx/parser/attribute-validation-source.cxx b/xsde/cxx/parser/attribute-validation-source.cxx new file mode 100644 index 0000000..19c94fa --- /dev/null +++ b/xsde/cxx/parser/attribute-validation-source.cxx @@ -0,0 +1,468 @@ +// file : xsde/cxx/parser/attribute-validation-source.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include + +#include +#include + +namespace CXX +{ + namespace Parser + { + namespace + { + struct Test: Traversal::Attribute, + Traversal::AnyAttribute, + Context + { + Test (Context& c) + : Context (c) + { + } + + virtual Void + traverse (SemanticGraph::Attribute& a) + { + String const& name (a.name ()); + + if (a.qualified () && a.namespace_ ().name ()) + { + String const& ns (a.namespace_ ().name ()); + + os << "n == " << L << strlit (name) << " &&" << endl + << "ns == " << L << strlit (ns); + } + else + os << "n == " << L << strlit (name) << " && ns.empty ()"; + } + + virtual Void + traverse (SemanticGraph::AnyAttribute& a) + { + String const& ns (a.definition_namespace ().name ()); + + for (SemanticGraph::AnyAttribute::NamespaceIterator + i (a.namespace_begin ()), e (a.namespace_end ()); i != e;) + { + if (*i == L"##any") + { + os << "!n.empty ()"; + } + else if (*i == L"##other") + { + if (ns) + { + // Note that here I assume that ##other does not include + // unqualified names in a schema with target namespace. + // This is not what the spec says but that seems to be + // the consensus. + // + os << "(!ns.empty () && ns != " << L << strlit (ns) << ")"; + } + else + os << "!ns.empty ()"; + } + else if (*i == L"##local") + { + os << "(ns.empty () && !n.empty ())"; + } + else if (*i == L"##targetNamespace") + { + os << "ns == " << L << strlit (ns); + } + else + { + os << "ns == " << L << strlit (*i); + } + + if (++i != e) + os << " ||" << endl; + } + } + }; + + // + // + struct PhaseOne : Traversal::Attribute, Context + { + PhaseOne (Context& c) + : Context (c), test_ (c) + { + } + + virtual Void + traverse (Type& a) + { + String const& name (ename (a)); + String const& inst (emember (a)); + + SemanticGraph::Type& type (a.type ()); + String const& post (post_name (type)); + String const& ret (ret_type (type)); + + os << "if ("; + + test_.traverse (a); + + os << ")" + << "{" + << "if (this->" << inst << ")" + << "{" + << "this->" << inst << "->pre ();" + << endl; + + if (!exceptions) + os << "if (this->" << inst << "->_error_type ())" << endl + << "this->" << inst << "->_copy_error (ctx);" + << "else" << endl; + + os << "this->" << inst << "->_pre_impl (ctx);" + << endl + << "if (!ctx.error_type ())" << endl + << "this->" << inst << "->_characters (s);" + << endl + << "if (!ctx.error_type ())" << endl + << "this->" << inst << "->_post_impl ();" + << endl + << "if (!ctx.error_type ())" << endl; + + if (ret == L"void") + os << "this->" << inst << "->" << post << " ();" + << endl; + else + os << "{" + << arg_type (type) << " tmp = this->" << inst << "->" << + post << " ();" + << endl; + + if (!exceptions) + os << "if (this->" << inst << "->_error_type ())" << endl + << "this->" << inst << "->_copy_error (ctx);" + << "else" << endl; + + if (ret == L"void") + os << "this->" << name << " ();"; + else + os << "this->" << name << " (tmp);" + << "}"; + + os << "}"; + + if (!a.optional ()) + os << "static_cast< v_state_attr_* > (" << + "this->v_state_attr_stack_.top ())->" << name << " = true;"; + + os << "return true;" + << "}"; + } + + private: + Test test_; + }; + + + // + // + struct PhaseTwo : Traversal::AnyAttribute, Context + { + PhaseTwo (Context& c) + : Context (c), test_ (c) + { + } + + virtual Void + traverse (Type& a) + { + os << "if ("; + + test_.traverse (a); + + os << ")" << endl + << "{" + << "this->_any_attribute (ns, n, s);" + << "return true;" + << "}"; + } + + private: + Test test_; + }; + + + // + // + struct AttributeStateInit: Traversal::Attribute, Context + { + AttributeStateInit (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& a) + { + if (!a.optional ()) + os << "as." << ename (a) << " = false;"; + } + }; + + + // + // + struct AttributeStateCheck: Traversal::Attribute, Context + { + AttributeStateCheck (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& a) + { + if (!a.optional ()) + { + String ns (a.qualified () ? a.namespace_ ().name () : String ()); + + os << "if (!as." << ename (a) << ")" + << "{" + << "this->_schema_error (" << + "::xsde::cxx::schema_error::expected_attribute);" + << "return;" + << "}"; + } + } + }; + + // + // + struct Complex : Traversal::Complex, Context + { + Complex (Context& c) + : Context (c), + phase_one_ (c), + phase_two_ (c), + attribute_state_init_ (c), + attribute_state_check_ (c) + { + names_phase_one_ >> phase_one_; + names_phase_two_ >> phase_two_; + + names_attribute_state_init_ >> attribute_state_init_; + names_attribute_state_check_ >> attribute_state_check_; + } + + virtual Void + traverse (Type& c) + { + Boolean has_att (has (c)); + Boolean has_any (has (c)); + + if (!has_att && !has_any) + return; + + Boolean has_req_att (false); + if (has_att) + { + RequiredAttributeTest test (has_req_att); + Traversal::Names names_test (test); + names (c, names_test); + } + + String const& name (ename (c)); + + os <<"// Attribute validation and dispatch functions for " << + name << "." << endl + <<"//" << endl; + + if (has_att) + { + // _attribute_impl_phase_one + // + os << "bool " << name << "::" << endl + << "_attribute_impl_phase_one (const " << string_type << + "& ns," << endl + << "const " << string_type << "& n," << endl + << "const " << string_type << "& s)" << endl + << "{" + << "::xsde::cxx::parser::context& ctx = this->_context ();" + << endl; + + names (c, names_phase_one_); + + // Nothing matched - call our base (extension) or return false + // if there is no base (or restriction (even from anyType)). + // + if (c.inherits_p () && + !c.inherits ().is_a ()) + { + // We cannot use the fully-qualified base name directly + // because of some broken compilers (EVC 4.0). + // + String base (unclash (name, "base")); + + os << "typedef " << fq_name (c.inherits ().base ()) << " " << + base << ";" + << "return " << base << + "::_attribute_impl_phase_one (ns, n, s);"; + } + else + os << "return false;"; + + os << "}"; + } + + + if (has_any) + { + // _attribute_impl_phase_two + // + os << "bool " << name << "::" << endl + << "_attribute_impl_phase_two (const " << string_type << + "& ns," << endl + << "const " << string_type << "& n," << endl + << "const " << string_type << "& s)" + << "{"; + + names (c, names_phase_two_); + + // Nothing matched - call our base (extension) or return false + // if there is no base (or restriction (even from anyType)). + // + if (c.inherits_p () && + !c.inherits ().is_a ()) + { + // We cannot use the fully-qualified base name directly + // because of some broken compilers (EVC 4.0). + // + String base (unclash (name, "base")); + + os << "typedef " << fq_name (c.inherits ().base ()) << " " << + base << ";" + << "return " << base << + "::_attribute_impl_phase_two (ns, n, s);"; + } + else + os << "return false;"; + + os << "}"; + } + + if (has_req_att) + { + // _pre_a_validate + // + os << "void " << name << "::" << endl + << "_pre_a_validate ()" + << "{"; + + if (exceptions) + os << "this->v_state_attr_stack_.push ();"; + else + os << "if (this->v_state_attr_stack_.push ())" + << "{" + << "this->_sys_error (::xsde::cxx::sys_error::no_memory);" + << "return;" + << "}"; + + os << "v_state_attr_& as = *static_cast< v_state_attr_* > (" << + "this->v_state_attr_stack_.top ());" + << endl; + + names (c, names_attribute_state_init_); + + // Call our base (extension) last. + // + if (c.inherits_p () && + !c.inherits ().is_a ()) + { + // We cannot use the fully-qualified base name directly + // because of some broken compilers (EVC 4.0). + // + String base (unclash (name, "base")); + + os << "typedef " << fq_name (c.inherits ().base ()) << " " << + base << ";" + << base << "::_pre_a_validate ();"; + } + + os << "}"; + + + // _post_a_validate + // + os << "void " << name << "::" << endl + << "_post_a_validate ()" + << "{"; + + // Call our base (extension) first. + // + if (c.inherits_p () && + !c.inherits ().is_a ()) + { + // We cannot use the fully-qualified base name directly + // because of some broken compilers (EVC 4.0). + // + String base (unclash (name, "base")); + + os << "typedef " << fq_name (c.inherits ().base ()) << " " << + base << ";" + << base << "::_post_a_validate ();" + << endl; + + os << "if (this->_context ().error_type ())" << endl + << "return;" + << endl; + } + + os << "v_state_attr_& as = *static_cast< v_state_attr_* > (" << + "this->v_state_attr_stack_.top ());" + << endl; + + names (c, names_attribute_state_check_); + + os << endl + << "this->v_state_attr_stack_.pop ();" + << "}"; + } + } + + private: + PhaseOne phase_one_; + Traversal::Names names_phase_one_; + + PhaseTwo phase_two_; + Traversal::Names names_phase_two_; + + AttributeStateInit attribute_state_init_; + Traversal::Names names_attribute_state_init_; + + AttributeStateCheck attribute_state_check_; + Traversal::Names names_attribute_state_check_; + }; + } + + Void + generate_attribute_validation_source (Context& ctx) + { + Traversal::Schema schema; + + Traversal::Sources sources; + Traversal::Names schema_names; + + Namespace ns (ctx); + Traversal::Names names; + + schema >> sources >> schema; + schema >> schema_names >> ns >> names; + + Complex complex (ctx); + + names >> complex; + + schema.dispatch (ctx.schema_root); + } + } +} diff --git a/xsde/cxx/parser/attribute-validation-source.hxx b/xsde/cxx/parser/attribute-validation-source.hxx new file mode 100644 index 0000000..70101da --- /dev/null +++ b/xsde/cxx/parser/attribute-validation-source.hxx @@ -0,0 +1,22 @@ +// file : xsde/cxx/parser/attribute-validation-source.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef CXX_PARSER_ATTRIBUTE_VALIDATION_SOURCE_HXX +#define CXX_PARSER_ATTRIBUTE_VALIDATION_SOURCE_HXX + +#include + +#include + +namespace CXX +{ + namespace Parser + { + Void + generate_attribute_validation_source (Context&); + } +} + +#endif // CXX_PARSER_ATTRIBUTE_VALIDATION_SOURCE_HXX diff --git a/xsde/cxx/parser/characters-validation-source.cxx b/xsde/cxx/parser/characters-validation-source.cxx new file mode 100644 index 0000000..c97a859 --- /dev/null +++ b/xsde/cxx/parser/characters-validation-source.cxx @@ -0,0 +1,76 @@ +// file : xsde/cxx/parser/characters-validation-source.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include + +#include +#include + +namespace CXX +{ + namespace Parser + { + namespace + { + // + // + struct Complex : Traversal::Complex, Context + { + Complex (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& c) + { + if (!c.mixed ()) + return; + + String const& name (ename (c)); + + os <<"// Character validation functions for " << name << "." << endl + <<"//" << endl; + + // _characters_impl + // + os << "bool " << name << "::" << endl + << "_characters_impl (const " << string_type << "& s)" + << "{" + << "this->_any_characters (s);" + << "return true;" + << "}"; + } + }; + } + + Void + generate_characters_validation_source (Context& ctx) + { + //@@ Most of the time there is no mixed content type so + // we generate an empty namespace which looks ugly. Will + // need to implement smart namespace to handle this at + // some point. + // + Traversal::Schema schema; + + Traversal::Sources sources; + Traversal::Names schema_names; + + Namespace ns (ctx); + Traversal::Names names; + + schema >> sources >> schema; + schema >> schema_names >> ns >> names; + + Complex complex (ctx); + + names >> complex; + + schema.dispatch (ctx.schema_root); + } + } +} + diff --git a/xsde/cxx/parser/characters-validation-source.hxx b/xsde/cxx/parser/characters-validation-source.hxx new file mode 100644 index 0000000..5fab5cc --- /dev/null +++ b/xsde/cxx/parser/characters-validation-source.hxx @@ -0,0 +1,22 @@ +// file : xsde/cxx/parser/characters-validation-source.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef CXX_PARSER_CHARACTERS_VALIDATION_SOURCE_HXX +#define CXX_PARSER_CHARACTERS_VALIDATION_SOURCE_HXX + +#include + +#include + +namespace CXX +{ + namespace Parser + { + Void + generate_characters_validation_source (Context&); + } +} + +#endif // CXX_PARSER_CHARACTERS_VALIDATION_SOURCE_HXX diff --git a/xsde/cxx/parser/cli.hxx b/xsde/cxx/parser/cli.hxx new file mode 100644 index 0000000..485dd91 --- /dev/null +++ b/xsde/cxx/parser/cli.hxx @@ -0,0 +1,156 @@ +// file : xsde/cxx/parser/cli.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef CXX_PARSER_CLI_HXX +#define CXX_PARSER_CLI_HXX + +#include + +#include + +#include +#include + +namespace CXX +{ + namespace Parser + { + namespace CLI + { + using namespace Cult::Types; + + typedef Char const Key[]; + + extern Key type_map; + extern Key no_stl; + extern Key no_iostream; + extern Key no_exceptions; + extern Key no_long_long; + extern Key reuse_style_mixin; + extern Key reuse_style_none; + extern Key generate_inline; + extern Key suppress_validation; + extern Key generate_polymorphic; + extern Key runtime_polymorphic; + extern Key suppress_reset; + extern Key generate_noop_impl; + extern Key generate_print_impl; + extern Key generate_test_driver; + extern Key force_overwrite; + extern Key root_element_first; + extern Key root_element_last; + extern Key root_element; + extern Key generate_xml_schema; + extern Key extern_xml_schema; + extern Key output_dir; + extern Key skel_type_suffix; + extern Key skel_file_suffix; + extern Key impl_type_suffix; + extern Key impl_file_suffix; + extern Key namespace_map; + extern Key namespace_regex; + extern Key namespace_regex_trace; + extern Key reserved_name; + extern Key include_with_brackets; + extern Key include_prefix; + extern Key include_regex; + extern Key include_regex_trace; + extern Key guard_prefix; + extern Key hxx_suffix; + extern Key ixx_suffix; + extern Key cxx_suffix; + extern Key hxx_regex; + extern Key ixx_regex; + extern Key cxx_regex; + extern Key hxx_prologue; + extern Key ixx_prologue; + extern Key cxx_prologue; + extern Key prologue; + extern Key hxx_epilogue; + extern Key ixx_epilogue; + extern Key cxx_epilogue; + extern Key epilogue; + extern Key hxx_prologue_file; + extern Key ixx_prologue_file; + extern Key cxx_prologue_file; + extern Key prologue_file; + extern Key hxx_epilogue_file; + extern Key ixx_epilogue_file; + extern Key cxx_epilogue_file; + extern Key epilogue_file; + extern Key show_anonymous; + extern Key show_sloc; + extern Key proprietary_license; + + typedef Cult::CLI::Options< + type_map, Cult::Containers::Vector, + no_stl, Boolean, + no_iostream, Boolean, + no_exceptions, Boolean, + no_long_long, Boolean, + reuse_style_mixin, Boolean, + reuse_style_none, Boolean, + generate_inline, Boolean, + suppress_validation, Boolean, + generate_polymorphic, Boolean, + runtime_polymorphic, Boolean, + suppress_reset, Boolean, + generate_noop_impl, Boolean, + generate_print_impl, Boolean, + generate_test_driver, Boolean, + force_overwrite, Boolean, + root_element_first, Boolean, + root_element_last, Boolean, + root_element, NarrowString, + generate_xml_schema, Boolean, + extern_xml_schema, NarrowString, + output_dir, NarrowString, + skel_type_suffix, NarrowString, + skel_file_suffix, NarrowString, + impl_type_suffix, NarrowString, + impl_file_suffix, NarrowString, + namespace_map, Cult::Containers::Vector, + namespace_regex, Cult::Containers::Vector, + namespace_regex_trace, Boolean, + reserved_name, Cult::Containers::Vector, + include_with_brackets, Boolean, + include_prefix, NarrowString, + include_regex, Cult::Containers::Vector, + include_regex_trace, Boolean, + guard_prefix, NarrowString, + hxx_suffix, NarrowString, + ixx_suffix, NarrowString, + cxx_suffix, NarrowString, + hxx_regex, NarrowString, + ixx_regex, NarrowString, + cxx_regex, NarrowString, + hxx_prologue, Cult::Containers::Vector, + ixx_prologue, Cult::Containers::Vector, + cxx_prologue, Cult::Containers::Vector, + prologue, Cult::Containers::Vector, + hxx_epilogue, Cult::Containers::Vector, + ixx_epilogue, Cult::Containers::Vector, + cxx_epilogue, Cult::Containers::Vector, + epilogue, Cult::Containers::Vector, + hxx_prologue_file, NarrowString, + ixx_prologue_file, NarrowString, + cxx_prologue_file, NarrowString, + prologue_file, NarrowString, + hxx_epilogue_file, NarrowString, + ixx_epilogue_file, NarrowString, + cxx_epilogue_file, NarrowString, + epilogue_file, NarrowString, + show_anonymous, Boolean, + show_sloc, Boolean, + proprietary_license, Boolean + + > Options; + + struct OptionsSpec: Cult::CLI::OptionsSpec {}; + } + } +} + +#endif // CXX_PARSER_CLI_HXX diff --git a/xsde/cxx/parser/driver-source.cxx b/xsde/cxx/parser/driver-source.cxx new file mode 100644 index 0000000..7221741 --- /dev/null +++ b/xsde/cxx/parser/driver-source.cxx @@ -0,0 +1,1139 @@ +// file : xsde/cxx/parser/driver-source.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include +#include + +#include +#include + +#include +#include + +#include + +namespace CXX +{ + namespace Parser + { + namespace + { + typedef + Cult::Containers::Map + TypeInstanceMap; + + typedef Cult::Containers::Set InstanceSet; + + // For base types we only want member's types, but not the + // base itself. + // + struct BaseType: Traversal::Complex, Context + { + BaseType (Context& c) + : Context (c) + { + } + + virtual Void + traverse (SemanticGraph::Complex& c) + { + inherits (c); + + if (!restriction_p (c)) + { + names (c); + contains_compositor (c); + } + } + }; + + struct ParserDef: Traversal::Type, + Traversal::List, + Traversal::Complex, + + Traversal::AnyType, + Traversal::AnySimpleType, + + Traversal::Fundamental::Byte, + Traversal::Fundamental::UnsignedByte, + Traversal::Fundamental::Short, + Traversal::Fundamental::UnsignedShort, + Traversal::Fundamental::Int, + Traversal::Fundamental::UnsignedInt, + Traversal::Fundamental::Long, + Traversal::Fundamental::UnsignedLong, + Traversal::Fundamental::Integer, + Traversal::Fundamental::NonPositiveInteger, + Traversal::Fundamental::NonNegativeInteger, + Traversal::Fundamental::PositiveInteger, + Traversal::Fundamental::NegativeInteger, + + Traversal::Fundamental::Boolean, + + Traversal::Fundamental::Float, + Traversal::Fundamental::Double, + Traversal::Fundamental::Decimal, + + Traversal::Fundamental::String, + Traversal::Fundamental::NormalizedString, + Traversal::Fundamental::Token, + Traversal::Fundamental::Name, + Traversal::Fundamental::NameToken, + Traversal::Fundamental::NameTokens, + Traversal::Fundamental::NCName, + Traversal::Fundamental::Language, + + Traversal::Fundamental::QName, + + Traversal::Fundamental::Id, + Traversal::Fundamental::IdRef, + Traversal::Fundamental::IdRefs, + + Traversal::Fundamental::AnyURI, + + Traversal::Fundamental::Base64Binary, + Traversal::Fundamental::HexBinary, + + Traversal::Fundamental::Date, + Traversal::Fundamental::DateTime, + Traversal::Fundamental::Duration, + Traversal::Fundamental::Day, + Traversal::Fundamental::Month, + Traversal::Fundamental::MonthDay, + Traversal::Fundamental::Year, + Traversal::Fundamental::YearMonth, + Traversal::Fundamental::Time, + + Traversal::Fundamental::Entity, + Traversal::Fundamental::Entities, + + Context + { + ParserDef (Context& c, TypeInstanceMap& map, InstanceSet& set) + : Context (c), map_ (map), set_ (set), base_ (c) + { + *this >> inherits_ >> base_ >> inherits_; + + *this >> contains_compositor_; + base_ >> contains_compositor_; + + *this >> names_; + base_ >> names_; + + contains_compositor_ >> compositor_; + compositor_ >> contains_particle_; + contains_particle_ >> compositor_; + contains_particle_ >> particle_; + + names_ >> attribute_; + + particle_ >> belongs_; + attribute_ >> belongs_; + belongs_ >> *this; + } + + virtual Void + traverse (SemanticGraph::Type& t) + { + if (map_.find (&t) == map_.end ()) + { + String inst (find_instance_name (t)); + map_[&t] = inst; + + os << fq_name (t, "p:impl") << " " << inst << ";"; + } + } + + virtual Void + traverse (SemanticGraph::List& l) + { + if (map_.find (&l) == map_.end ()) + { + String inst (find_instance_name (l)); + map_[&l] = inst; + + os << fq_name (l, "p:impl") << " " << inst << ";"; + + dispatch (l.argumented ().type ()); + } + } + + virtual Void + traverse (SemanticGraph::Complex& c) + { + if (map_.find (&c) == map_.end ()) + { + String inst (find_instance_name (c)); + map_[&c] = inst; + + os << fq_name (c, "p:impl") << " " << inst << ";"; + + inherits (c); + + if (!restriction_p (c)) + { + names (c); + contains_compositor (c); + } + } + } + + // anyType & anySimpleType. + // + virtual Void + traverse (SemanticGraph::AnyType& t) + { + fund_type (t, "any_type"); + } + + virtual Void + traverse (SemanticGraph::AnySimpleType& t) + { + fund_type (t, "any_simple_type"); + } + + // Boolean. + // + virtual Void + traverse (SemanticGraph::Fundamental::Boolean& t) + { + fund_type (t, "boolean"); + } + + // Integral types. + // + virtual Void + traverse (SemanticGraph::Fundamental::Byte& t) + { + fund_type (t, "byte"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::UnsignedByte& t) + { + fund_type (t, "unsigned_byte"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Short& t) + { + fund_type (t, "short"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::UnsignedShort& t) + { + fund_type (t, "unsigned_short"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Int& t) + { + fund_type (t, "int"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::UnsignedInt& t) + { + fund_type (t, "unsigned_int"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Long& t) + { + fund_type (t, "long"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::UnsignedLong& t) + { + fund_type (t, "unsigned_long"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Integer& t) + { + fund_type (t, "integer"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NonPositiveInteger& t) + { + fund_type (t, "non_positive_integer"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NonNegativeInteger& t) + { + fund_type (t, "non_negative_integer"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::PositiveInteger& t) + { + fund_type (t, "positive_integer"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NegativeInteger& t) + { + fund_type (t, "negative_integer"); + } + + // Floats. + // + virtual Void + traverse (SemanticGraph::Fundamental::Float& t) + { + fund_type (t, "float"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Double& t) + { + fund_type (t, "double"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Decimal& t) + { + fund_type (t, "decimal"); + } + + // Strings. + // + virtual Void + traverse (SemanticGraph::Fundamental::String& t) + { + fund_type (t, "string"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NormalizedString& t) + { + fund_type (t, "normalized_string"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Token& t) + { + fund_type (t, "token"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NameToken& t) + { + fund_type (t, "nmtoken"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NameTokens& t) + { + fund_type (t, "nmtokens"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Name& t) + { + fund_type (t, "name"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NCName& t) + { + fund_type (t, "ncname"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Language& t) + { + fund_type (t, "language"); + } + + + // Qualified name. + // + virtual Void + traverse (SemanticGraph::Fundamental::QName& t) + { + fund_type (t, "qname"); + } + + + // ID/IDREF. + // + virtual Void + traverse (SemanticGraph::Fundamental::Id& t) + { + fund_type (t, "id"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::IdRef& t) + { + fund_type (t, "idref"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::IdRefs& t) + { + fund_type (t, "idrefs"); + } + + // URI. + // + virtual Void + traverse (SemanticGraph::Fundamental::AnyURI& t) + { + fund_type (t, "uri"); + } + + // Binary. + // + virtual Void + traverse (SemanticGraph::Fundamental::Base64Binary& t) + { + fund_type (t, "base64_binary"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::HexBinary& t) + { + fund_type (t, "hex_binary"); + } + + + // Date/time. + // + virtual Void + traverse (SemanticGraph::Fundamental::Date& t) + { + fund_type (t, "date"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::DateTime& t) + { + fund_type (t, "date_time"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Duration& t) + { + fund_type (t, "duration"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Day& t) + { + fund_type (t, "day"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Month& t) + { + fund_type (t, "month"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::MonthDay& t) + { + fund_type (t, "month_day"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Year& t) + { + fund_type (t, "year"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::YearMonth& t) + { + fund_type (t, "year_month"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Time& t) + { + fund_type (t, "time"); + } + + // Entity. + // + virtual Void + traverse (SemanticGraph::Fundamental::Entity& t) + { + fund_type (t, "entity"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Entities& t) + { + fund_type (t, "entities"); + } + + private: + virtual Void + fund_type (SemanticGraph::Type& t, String const& name) + { + if (map_.find (&t) == map_.end ()) + { + String inst (find_instance_name (name)); + map_[&t] = inst; + + os << fq_name (t, "p:impl") << " " << inst << ";"; + } + } + + String + find_instance_name (String const& raw_name) + { + String base_name (escape (raw_name + L"_p")); + String name (base_name); + + for (UnsignedLong i (1); set_.find (name) != set_.end (); ++i) + { + std::wostringstream os; + os << i; + name = base_name + os.str (); + } + + set_.insert (name); + return name; + } + + String + find_instance_name (SemanticGraph::Type& t) + { + return find_instance_name (t.name ()); + } + + TypeInstanceMap& map_; + InstanceSet& set_; + + BaseType base_; + Traversal::Inherits inherits_; + + Traversal::Compositor compositor_; + Traversal::Element particle_; + Traversal::ContainsCompositor contains_compositor_; + Traversal::ContainsParticle contains_particle_; + + Traversal::Names names_; + Traversal::Attribute attribute_; + + Traversal::Belongs belongs_; + }; + + // + // + struct ParticleArg: Traversal::Element, Context + { + ParticleArg (Context& c, TypeInstanceMap& map, Boolean& first) + : Context (c), map_ (map), first_ (first) + { + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + if (!first_) + os << "," << endl; + else + first_ = false; + + os << map_[&e.type ()]; + } + + private: + TypeInstanceMap& map_; + Boolean& first_; + }; + + struct AttributeArg: Traversal::Attribute, Context + { + AttributeArg (Context& c, TypeInstanceMap& map, Boolean& first) + : Context (c), map_ (map), first_ (first) + { + } + + virtual Void + traverse (Type& a) + { + if (!first_) + os << "," << endl; + else + first_ = false; + + os << map_[&a.type ()]; + } + + private: + TypeInstanceMap& map_; + Boolean& first_; + }; + + struct ArgList : Traversal::Complex, + Traversal::List, + Context + { + ArgList (Context& c, TypeInstanceMap& map) + : Context (c), + map_ (map), + particle_ (c, map, first_), + attribute_ (c, map, first_), + first_ (true) + { + inherits_ >> *this; + + contains_compositor_ >> compositor_; + compositor_ >> contains_particle_; + contains_particle_ >> compositor_; + contains_particle_ >> particle_; + + names_ >> attribute_; + } + + virtual Void + traverse (SemanticGraph::Complex& c) + { + inherits (c, inherits_); + + if (!restriction_p (c)) + { + names (c, names_); + contains_compositor (c, contains_compositor_); + } + } + + virtual Void + traverse (SemanticGraph::List& l) + { + if (!first_) + os << "," << endl; + else + first_ = false; + + os << map_[&l.argumented ().type ()]; + } + + private: + TypeInstanceMap& map_; + + Traversal::Inherits inherits_; + + Traversal::Compositor compositor_; + ParticleArg particle_; + Traversal::ContainsCompositor contains_compositor_; + Traversal::ContainsParticle contains_particle_; + + Traversal::Names names_; + AttributeArg attribute_; + + Boolean first_; + }; + + struct ParserConnect: Traversal::List, + Traversal::Complex, + Context + { + ParserConnect (Context& c, TypeInstanceMap& map) + : Context (c), map_ (map), base_ (c) + { + *this >> inherits_ >> base_ >> inherits_; + + *this >> contains_compositor_; + base_ >> contains_compositor_; + + *this >> names_; + base_ >> names_; + + contains_compositor_ >> compositor_; + compositor_ >> contains_particle_; + contains_particle_ >> compositor_; + contains_particle_ >> particle_; + + names_ >> attribute_; + + particle_ >> belongs_; + attribute_ >> belongs_; + belongs_ >> *this; + } + + virtual Void + traverse (SemanticGraph::List& l) + { + if (type_set_.find (&l) == type_set_.end ()) + { + os << map_[&l] << ".parsers (" << + map_[&l.argumented ().type ()] << ");" + << endl; + + type_set_.insert (&l); + } + } + + virtual Void + traverse (SemanticGraph::Complex& c) + { + if (type_set_.find (&c) == type_set_.end ()) + { + if (has_members (c)) + { + os << map_[&c] << ".parsers ("; + + ArgList args (*this, map_); + args.dispatch (c); + + os << ");" + << endl; + } + + type_set_.insert (&c); + + inherits (c); + + if (!restriction_p (c)) + { + names (c); + contains_compositor (c); + } + } + } + + private: + Boolean + has_members (SemanticGraph::Complex& c) + { + using SemanticGraph::Complex; + + if (has (c)) + return true; + + if (c.inherits_p ()) + { + SemanticGraph::Type& b (c.inherits ().base ()); + + if (Complex* cb = dynamic_cast (&b)) + return has_members (*cb); + + return b.is_a (); + } + + return false; + } + + private: + TypeInstanceMap& map_; + Cult::Containers::Set type_set_; + + BaseType base_; + Traversal::Inherits inherits_; + + Traversal::Compositor compositor_; + Traversal::Element particle_; + Traversal::ContainsCompositor contains_compositor_; + Traversal::ContainsParticle contains_particle_; + + Traversal::Names names_; + Traversal::Attribute attribute_; + + Traversal::Belongs belongs_; + }; + + struct GlobalElement: Traversal::Element, Context + { + GlobalElement (Context& c, SemanticGraph::Element*& element) + : Context (c), element_ (element) + { + } + + virtual Void + traverse (Type& e) + { + if (options.value ()) + { + if (element_ == 0) + element_ = &e; + } + else if (String name = options.value ()) + { + if (e.name () == name) + element_ = &e; + } + else + element_ = &e; // Cover root-element-last and no option. + } + + private: + SemanticGraph::Element*& element_; + }; + } + + Void + generate_driver_source (Context& ctx) + { + // Figure out the root element. Validator should have made sure + // it is unique. + // + SemanticGraph::Element* root (0); + { + Traversal::Schema schema; + Traversal::Sources sources; + + schema >> sources >> schema; + + Traversal::Names schema_names; + Traversal::Namespace ns; + Traversal::Names ns_names; + RootElement global_element (ctx.options, root); + + schema >> schema_names >> ns >> ns_names >> global_element; + + schema.dispatch (ctx.schema_root); + } + + String xs (ctx.xs_ns_name ()); + std::wostream& os (ctx.os); + + InstanceSet set; + TypeInstanceMap map; + SemanticGraph::Type& root_type (root->type ()); + + set.insert ("doc_p"); + + if (ctx.options.value ()) + os << "#include " << endl; + else + os << "#include " << endl; + + os << endl + << "int" << endl + << "main (int argc, char* argv[])" + << "{" + << "const char* input;" + << endl + << "if (argc < 2)" + << "{" + << "input = \"STDIN\";"; + + if (ctx.options.value ()) + os << "fprintf (stderr, \"XML file not specified, reading " << + "from STDIN\\n\");"; + else + os << "std::cerr << \"XML file not specified, reading " << + "from STDIN\" << std::endl;"; + + os << "}" + << "else" << endl + << "input = argv[1];" + << endl; + + if (ctx.options.value ()) + { + os << "FILE* f = argc > 1 ? fopen (argv[1], \"rb\") : stdin;" + << endl + << "if (f == 0)" + << "{" + << "fprintf (stderr, \"%s: unable to open\\n\", input);" + << "return 1;" + << "}"; + } + + if (ctx.options.value () || + ctx.options.value ()) + os << "int r = 0;" + << endl; + + if (!ctx.options.value ()) + os << "try" + << "{"; + + os << "// Instantiate individual parsers." << endl + << "//" << endl; + + { + ParserDef def (ctx, map, set); + def.dispatch (root_type); + } + + os << endl + << "// Connect the parsers together." << endl + << "//" << endl; + + { + // @@ I can simply iterate over the map instead of traversing + // the tree all over again. + // + ParserConnect connect (ctx, map); + connect.dispatch (root_type); + } + + String const& root_p (map[&root_type]); + + os << "// Parse the XML document." << endl + << "//" << endl; + + if (ctx.options.value ()) + os << "bool io_error = false;"; + + if (ctx.options.value ()) + os << xs << "::parser_error e;" + << endl + << "do" + << "{"; + + if (root->namespace_().name ()) + os << xs << "::document_pimpl doc_p (" << endl + << root_p << "," << endl + << ctx.strlit (root->namespace_().name ()) << "," << endl + << ctx.strlit (root->name ()) << ");" + << endl; + else + os << xs << "::document_pimpl doc_p (" << root_p << ", " << + ctx.strlit (root->name ()) << ");" + << endl; + + if (ctx.options.value ()) + os << "if (e = doc_p._error ())" << endl + << "break;" + << endl; + + os << root_p << ".pre ();"; + + if (ctx.options.value ()) + os << endl + << "if (e = " << root_p << "._error ())" << endl + << "break;"; + + + if (ctx.options.value ()) + { + os << endl + << "char buf[4096];" + << endl + << "do" + << "{" + << "size_t s = fread (buf, 1, sizeof (buf), f);" + << endl + << "if (s != sizeof (buf) && ferror (f))" + << "{" + << "io_error = true;" + << "break;" + << "}"; + + if (ctx.options.value ()) + os << "doc_p.parse (buf, s, feof (f) != 0);" + << "e = doc_p._error ();" + << "}" + << "while (!e && !feof (f));" + << endl + << "if (io_error || e)" << endl + << "break;" + << endl; + else + os << "doc_p.parse (buf, s, feof (f) != 0);" + << "}" + << "while (!feof (f));" + << endl + << "if (!io_error)" + << "{"; + } + else + { + os << endl + << "if (argc < 2)" << endl + << "doc_p.parse (std::cin);" + << "else" << endl + << "doc_p.parse (argv[1]);" + << endl; + + if (ctx.options.value ()) + os << "if (e = doc_p._error ())" << endl + << "break;" + << endl; + } + + String const& ret (Context::ret_type (root_type)); + String const& post (Context::post_name (root_type)); + + if (ret == L"void") + { + os << root_p << "." << post << " ();"; + + if (ctx.options.value ()) + os << "e = " << root_p << "._error ();"; + } + else + { + os << Context::arg_type (root_type) << " v = " << + root_p << "." << post << " ());" + << endl; + + if (ctx.options.value ()) + os << "if (e = " << root_p << "._error ())" << endl + << "break;" + << endl; + + if (ctx.options.value ()) + { + PrintCall t (ctx, root->name (), "v"); + t.dispatch (root_type); + } + else + { + os << "// TODO" << endl + << "//" << endl; + } + + { + DeleteCall t (ctx, "v"); + t.dispatch (root_type); + } + } + + if (ctx.options.value ()) + os << "}" + << "while (false);" + << endl; + else if (ctx.options.value ()) + os << "}" // if (!io_error) + << "else" + << "{" + << "fprintf (stderr, \"%s: read failure\\n\", input);" + << "r = 1;" + << "}"; + + // Error handling. + // + + if (ctx.options.value ()) + { + os << "// Handle errors." << endl + << "//" << endl; + + if (ctx.options.value ()) + { + os << "if (io_error)" + << "{"; + + if (ctx.options.value ()) + os << "fprintf (stderr, \"%s: read failure\\n\", input);"; + else + os << "std::cerr << input << \": read failure\" << std::endl;"; + + os << "r = 1;" + << "}" + << "else "; + } + + os << "if (e)" + << "{" + << "switch (e.type ())" + << "{" + << "case " << xs << "::parser_error::sys:" + << "{"; + + if (ctx.options.value ()) + os << "fprintf (stderr, \"%s: %s\\n\", input, e.sys_text ());"; + else + os << "std::cerr << input << \": \" << e.sys_text () << std::endl;"; + + os << "break;" + << "}" + << "case " << xs << "::parser_error::xml:" + << "{"; + + if (ctx.options.value ()) + os << "fprintf (stderr, \"%s:%lu:%lu: %s\\n\"," << endl + << "input, e.line (), e.column (), e.xml_text ());"; + else + os << "std::cerr << input << ':' << e.line () << ':'" << endl + << " << e.column () << \": \" << e.xml_text () << std::endl;"; + + os << "break;" + << "}"; + + if (!ctx.options.value ()) + { + os << "case " << xs << "::parser_error::schema:" + << "{"; + + if (ctx.options.value ()) + os << "fprintf (stderr, \"%s:%lu:%lu: %s\\n\"," << endl + << "input, e.line (), e.column (), e.schema_text ());"; + else + os << "std::cerr << input << ':' << e.line () << ':'" << endl + << " << e.column () << \": \" << e.schema_text () << std::endl;"; + + os << "break;" + << "}"; + } + + os << "case " << xs << "::parser_error::app:" + << "{"; + + if (ctx.options.value ()) + os << "fprintf (stderr, \"%s:%lu:%lu: application error %d\\n\"," << endl + << "input, e.line (), e.column (), e.app_code ());"; + else + os << "std::cerr << input << ':' << e.line () << ':'" << endl + << " << e.column () << \": application error \"" << endl + << " << e.app_code () << std::endl;"; + + os << "break;" + << "}" + << "default:" + << "{" + << "break;" + << "}" + << "}" //switch + << "r = 1;" + << "}"; // else if (e) + } + else + { + os << "}" // try + << "catch (const " << xs << "::parser_exception& e)" + << "{"; + + if (ctx.options.value ()) + os << "fprintf (stderr, \"%s:%lu:%lu: %s\\n\"," << endl + << "input, e.line (), e.column (), e.text ());" + << "r = 1;"; + else + os << "std::cerr << input << ':' << e.line () << ':'" << endl + << " << e.column () << \": \" << e.text () << std::endl;" + << "return 1;"; + + os << "}"; + + if (!ctx.options.value ()) + { + os << "catch (const std::ios_base::failure&)" + << "{" + << "std::cerr << input << \": unable to open or " << + "read failure\" << std::endl;" + << "return 1;" + << "}"; + } + } + + if (ctx.options.value ()) + os << "if (argc > 1)" << endl + << "fclose (f);" + << endl; + + if (ctx.options.value () || + ctx.options.value ()) + os << "return r;"; + else + os << "return 0;"; + + os << "}"; // main + } + } +} diff --git a/xsde/cxx/parser/driver-source.hxx b/xsde/cxx/parser/driver-source.hxx new file mode 100644 index 0000000..f829d41 --- /dev/null +++ b/xsde/cxx/parser/driver-source.hxx @@ -0,0 +1,22 @@ +// file : xsde/cxx/parser/driver-source.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef CXX_PARSER_DRIVER_SOURCE_HXX +#define CXX_PARSER_DRIVER_SOURCE_HXX + +#include + +#include + +namespace CXX +{ + namespace Parser + { + Void + generate_driver_source (Context&); + } +} + +#endif // CXX_PARSER_DRIVER_SOURCE_HXX diff --git a/xsde/cxx/parser/element-validation-source.cxx b/xsde/cxx/parser/element-validation-source.cxx new file mode 100644 index 0000000..e5d3589 --- /dev/null +++ b/xsde/cxx/parser/element-validation-source.cxx @@ -0,0 +1,2120 @@ +// file : xsde/cxx/parser/element-validation-source.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include + +#include +#include + +#include + +namespace CXX +{ + namespace Parser + { + namespace + { + typedef Cult::Containers::Vector Particles; + + // Find particle that can be absent. + // + struct OptionalParticleTest: Traversal::Choice + { + OptionalParticleTest (SemanticGraph::Particle*& result) + : is_optional_ (optional_), result_ (result) + { + } + + virtual Void + traverse (SemanticGraph::Choice& c) + { + using SemanticGraph::Choice; + result_ = 0; + optional_ = false; + + for (Choice::ContainsIterator i (c.contains_begin ()); + result_ == 0 && i != c.contains_end (); + ++i) + { + is_optional_.dispatch (i->particle ()); + if (optional_) + result_ = &i->particle (); + } + } + + struct IsOptional: Traversal::Choice, + Traversal::Sequence, + Traversal::Element, + Traversal::Any + { + IsOptional (Boolean& r) + : r_ (r) + { + } + + virtual Void + traverse (SemanticGraph::Choice& c) + { + if (!r_ && c.min () == 0) + r_ = true; + + // We need at least one particle to be optional for the whole + // choice to be optional. + // + using SemanticGraph::Choice; + + for (Choice::ContainsIterator i (c.contains_begin ()); + !r_ && i != c.contains_end (); + ++i) + { + dispatch (i->particle ()); + } + } + + virtual Void + traverse (SemanticGraph::Sequence& s) + { + if (!r_ && s.min () == 0) + r_ = true; + + // We need all particles to be optional for the whole sequence + // to be optional. + // + using SemanticGraph::Sequence; + + for (Sequence::ContainsIterator i (s.contains_begin ()); + !r_ && i != s.contains_end (); + ++i) + { + Boolean r (false); + IsOptional test (r); + test.dispatch (i->particle ()); + if (!r) + return; + } + + r_ = true; + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + if (!r_ && e.min () == 0) + r_ = true; + } + + virtual Void + traverse (SemanticGraph::Any& a) + { + if (!r_ && a.min () == 0) + r_ = true; + } + + private: + Boolean& r_; + }; + + private: + Boolean optional_; + IsOptional is_optional_; + SemanticGraph::Particle*& result_; + }; + + // + // + Void + choice_arm_call (SemanticGraph::Particle* p, + SemanticGraph::Choice* c, + Context* ctx) + { + using SemanticGraph::Choice; + + ctx->os << "this->" << Context::earm (*c) << " (" << + Context::etag (*p) << ");"; + + if ((c = dynamic_cast (p))) + { + OptionalParticleTest test (p); + test.traverse (*c); + + if (p) + choice_arm_call (p, c, ctx); + } + } + + // + // + struct ParticleTest: Traversal::Compositor, + Traversal::Element, + Traversal::Any, + Context + { + ParticleTest (Context& c) + : Context (c) + { + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + String const& name (e.name ()); + Boolean subst (poly_code && e.global ()); + + if (subst) + os << "("; + + if (e.qualified () && e.namespace_ ().name ()) + { + String const& ns (e.namespace_ ().name ()); + + os << "n == " << L << strlit (name) << " &&" << endl + << "ns == " << L << strlit (ns); + } + else + os << "n == " << L << strlit (name) << " && ns.empty ()"; + + + // Only a globally-defined element can be a subst-group root. + // + if (subst) + { + String root_id (e.name ()); + + if (String const& ns = e.namespace_ ().name ()) + { + root_id += L' '; + root_id += ns; + } + + os << ") ||" << endl + << "::xsde::cxx::parser::substitution_map_instance ()" << + ".check (" << endl + << "ns, n, " << strlit (root_id) << ", t)"; + } + + } + + virtual Void + traverse (SemanticGraph::Any& a) + { + String const& ns (a.definition_namespace ().name ()); + + // Note that we need to make sure the "flush" element (both name + // and namespace are empty) does not match any compositor. + // + for (SemanticGraph::Any::NamespaceIterator i (a.namespace_begin ()), + e (a.namespace_end ()); i != e;) + { + if (*i == L"##any") + { + os << "!n.empty ()"; + } + else if (*i == L"##other") + { + if (ns) + { + // Note that here I assume that ##other does not include + // unqualified names in a schema with target namespace. + // This is not what the spec says but that seems to be + // the consensus. + // + os << "(!ns.empty () && ns != " << L << strlit (ns) << ")"; + } + else + os << "!ns.empty ()"; + } + else if (*i == L"##local") + { + os << "(ns.empty () && !n.empty ())"; + } + else if (*i == L"##targetNamespace") + { + os << "ns == " << L << strlit (ns); + } + else + { + os << "ns == " << L << strlit (*i); + } + + if (++i != e) + os << " ||" << endl; + } + } + + virtual Void + traverse (SemanticGraph::Compositor& c) + { + // This compositor should already have been tested for + // triviality (empty). + // + Particles const& p (c.context ().get ("p:prefixes")); + + Boolean paren (p.size () != 1); + + for (Particles::ConstIterator i (p.begin ()), e (p.end ()); + i != e;) + { + if (paren) + os << "("; + + dispatch (**i); + + if (paren) + os << ")"; + + if (++i != e) + os << " ||" << endl; + } + } + }; + + + // Generates particle namespace-name pair. Used to generate + // the _expected_element call. + // + struct ParticleName: Traversal::Compositor, + Traversal::Element, + Traversal::Any, + Context + { + ParticleName (Context& c) + : Context (c) + { + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + String ns (e.qualified () ? e.namespace_ ().name () : String ()); + + os << L << strlit (ns) << ", " << L << strlit (e.name ()); + } + + virtual Void + traverse (SemanticGraph::Any& a) + { + String const& ns (*a.namespace_begin ()); + + os << L << strlit (ns) << ", " << L << "\"*\""; + } + + virtual Void + traverse (SemanticGraph::Compositor& c) + { + Particles const& p (c.context ().get ("p:prefixes")); + + dispatch (**p.begin ()); + } + }; + + + // Common base for the ParticleIn{All, Choice, Sequence} treversers. + // + struct ParticleInCompositor: protected Context + { + protected: + ParticleInCompositor (Context& c, SemanticGraph::Complex& type) + : Context (c), type_ (type), particle_test_ (c) + { + } + + + // Generate sub-parser setup code as well as the pre/post calls. + // + Void + pre_post_calls (SemanticGraph::Particle& p) + { + using SemanticGraph::Element; + using SemanticGraph::Complex; + + if (Element* e = dynamic_cast (&p)) + { + SemanticGraph::Type& type (e->type ()); + Boolean poly (poly_code && !anonymous (type)); + + String const& name (ename (*e)); + String inst (poly ? emember_cache (*e) : emember (*e)); + + String def_parser, map; + + if (poly) + { + def_parser = emember (*e); + map = emember_map (*e); + } + + if (poly) + { + String cast (mixin ? L"dynamic_cast" : L"static_cast"); + String fq_type (fq_name (type)); + + os << "if (t == 0 && this->" << def_parser << " != 0)" << endl + << "this->" << inst << " = this->" << def_parser << ";" + << "else" + << "{" + << "const char* ts = " << fq_type << "::_static_type ();" + << endl + << "if (t == 0)" << endl + << "t = ts;" + << endl + << "if (this->" << def_parser << " != 0 && " << + "strcmp (t, ts) == 0)" << endl + << "this->" << inst << " = this->" << def_parser << ";" + << "else" + << "{"; + + // Check that the types are related by inheritance. + // + os << "if (t != ts &&" << endl + << "!::xsde::cxx::parser::validating::" << + "inheritance_map_instance ().check (t, ts))" + << "{" + << "ctx.schema_error (::xsde::cxx::schema_error::not_derived);" + << "return;" + << "}"; + + os << "if (this->" << map << " != 0)" << endl + << "this->" << inst << " = " << cast << "< " << + fq_type << "* > (" << endl + << "this->" << map << "->find (t));" + << "else" << endl + << "this->" << inst << " = 0;" + << "}" + << "}"; + } + + String const& post (post_name (type)); + + os << "if (this->" << inst << ")" + << "{" + << "this->" << inst << "->pre ();"; + + if (!exceptions) + { + // Note that after pre() we need to check both parser and + // context error states because of the recursive parsing. + // + os << endl + << "if (this->" << inst << "->_error_type ())" << endl + << "this->" << inst << "->_copy_error (ctx);" + << endl + << "if (!ctx.error_type ())" << endl; + } + + os << "this->" << inst << "->_pre_impl (ctx);" + << "}" + << "else" << endl + << "ctx.current_.depth_++;" // Ignoring document fragment. + << endl + << "}" + << "else" // start + << "{" + << "if (this->" << inst << ")" + << "{"; + + String const& ret (ret_type (type)); + + if (ret == L"void") + os << "this->" << inst << "->" << post << " ();"; + else + os << arg_type (type) << " tmp = this->" << inst << "->" << + post << " ();"; + + if (!exceptions) + { + // Note that after post() we need to check both parser and + // context error states because of the recursive parsing. + // + os << endl + << "if (this->" << inst << "->_error_type ())" << endl + << "this->" << inst << "->_copy_error (ctx);" + << endl + << "if (!ctx.error_type ())" << endl; + } + + if (ret == L"void") + os << "this->" << name << " ();"; + else + os << "this->" << name << " (tmp);"; + + os << "}"; + } + else + { + os << "ctx.current_.any_ = true;" + << "ctx.current_.depth_++;" + << endl + << "this->_start_any_element (ns, n" << + (poly_runtime ? (poly_code ? ", t" : ", 0") : "") << ");" + << "}" + << "else" // start + << "{" + << "this->_end_any_element (ns, n);"; + } + } + + + protected: + SemanticGraph::Complex& type_; + ParticleTest particle_test_; + }; + + + + // The 'all' compositor can only contain elements with min={0,1}, max=1. + // + struct ParticleInAll: Traversal::Element, + ParticleInCompositor + { + ParticleInAll (Context& c, SemanticGraph::Complex& type) + : ParticleInCompositor (c, type) + { + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + UnsignedLong state (e.context ().get ("p:state")); + + SemanticGraph::Compositor& c ( + e.contained_particle ().compositor ()); + + if (state != 0) + os << "else "; + + os << "if ("; + + particle_test_.traverse (e); + + os << ")" + << "{" + << "if (count[" << state << "UL] == 0)" + << "{" + << "if (start)" + << "{"; + + if (c.min () == 0) + { + // Make the call to _present if we haven't seen any + // elements yet. + // + UnsignedLong state_count ( + c.context().get ("p:state-count")); + + if (state_count > 1) + { + os << "if ("; + + Boolean sub (false); + + for (UnsignedLong i (0); i < state_count; ++i) + { + if (i == state) + continue; + + if (sub) + os << " &&" << endl; + else + sub = true; + + os << "count[" << i << "UL] == 0"; + } + + os << ")" << endl + << "this->" << epresent (c) << " ();" + << endl; + } + } + + pre_post_calls (e); + + os << "count[" << state << "UL] = 1;" + << "}" + << "}" + << "else" // count != 0 + << "{" + << "assert (start);" // Assuming well-formed XML. + + // Since there is never more content after 'all', we could have + // as well thrown here. But instead we will let the code in + // start_element handle this along with other unexpected + // elements. + // + << "state = ~0UL;" + << "}" + << "}"; + } + }; + + + // + // + struct ParticleInChoice: Traversal::Particle, + Traversal::Compositor, + ParticleInCompositor + { + ParticleInChoice (Context& c, SemanticGraph::Complex& type) + : ParticleInCompositor (c, type), particle_name_ (c) + { + } + + virtual Void + traverse (SemanticGraph::Particle& p) + { + using SemanticGraph::Element; + + UnsignedLong state (p.context ().get ("p:state")); + + UnsignedLong min (p.min ()), max (p.max ()); + + os << "case " << state << "UL:" << endl + << "{"; + + if (max != 1) // We don't need the test if max == 1. + { + os << "if ("; + + particle_test_.dispatch (p); + + os << ")" + << "{"; + } + + os << "if (start)" + << "{"; + + pre_post_calls (p); + + switch (max) + { + case 0: + { + os << "count++;"; + break; + } + case 1: + { + // We do not need to increment count because min <= max and + // we do not generate min check for min <= 1 (see below). + // + os << "state = ~0UL;"; + break; + } + default: + { + os << "if (++count == " << max << "UL)" << endl + << "state = ~0UL;"; + } + }; + + os << "}"; // start + + // We've already moved to the final state if max == 1. + // + if (max != 1) + { + os << "}" + << "else" + << "{" + << "assert (start);"; // Assuming well-formed XML + + // Check if min cardinality requirements have been met. Since + // count is always >= 1, don't generate dead code if min <= 1. + // + if (min > 1) + { + os << "if (count < " << min << "UL)" << endl + << "this->_schema_error (" << + "::xsde::cxx::schema_error::expected_element);"; + + /* + << "this->_expected_element (" << endl; + particle_name_.dispatch (p); + os << "," << endl + << "ns, n);"; + */ + } + + + os << "state = ~0UL;" + << "}"; + } + + os << "break;" + << "}"; // case + } + + virtual Void + traverse (SemanticGraph::Compositor& c) + { + using SemanticGraph::Choice; + using SemanticGraph::Sequence; + using SemanticGraph::Compositor; + + Boolean choice (c.is_a ()); + SemanticGraph::Context& cc (c.context ()); + + UnsignedLong max (c.max ()); + UnsignedLong min (cc.get ("p:effective-min")); + UnsignedLong n (cc.get ("p:comp-number")); + UnsignedLong state (cc.get ("p:state")); + + String func (choice ? "choice_" : "sequence_"); + + os << "case " << state << "UL:" << endl + << "{" + << "unsigned long s = ~0UL;" + << endl; + + Boolean first (true); + + for (Compositor::ContainsIterator ci (c.contains_begin ()); + ci != c.contains_end (); ++ci) + { + SemanticGraph::Particle& p (ci->particle ()); + + if (p.is_a () && !cc.count ("p:comp-number")) + continue; // Empty compositor. + + if (!p.context ().count ("p:prefix")) + break; + + UnsignedLong state (p.context ().get ("p:state")); + + if (first) + first = false; + else + os << "else "; + + os << "if ("; + + particle_test_.dispatch (p); + + os << ")" << endl + << "s = " << state << "UL;"; + } + + if (!choice) + { + for (Compositor::ContainsIterator ci (c.contains_begin ()); + ci != c.contains_end (); ++ci) + { + Choice* pc = dynamic_cast (&ci->particle ()); + + if (pc && pc->min () == 1 && pc->max () == 1 && + pc->context ().get ("p:effective-min") == 0) + { + // This is a required choice with effective-min == 0 (i.e., + // it contains optional particle). We need to call the arm + // callback with that optional particle's tag. + // + + SemanticGraph::Particle* p (0); + OptionalParticleTest test (p); + test.traverse (*pc); + + if (p) + { + UnsignedLong state ( + pc->context ().get ("p:state")); + + os << endl + << "if (s > " << state << "UL)" << endl + << "{"; + choice_arm_call (p, pc, this); + os << "}"; + } + } + } + } + + // This compositor. + // + os << endl + << "if (s != ~0UL)" + << "{" + << "assert (start);"; // End is handled by the sub-machine. + + switch (max) + { + case 0: + { + os << "count++;"; + break; + } + case 1: + { + // We do not need to increment count because min <= max and + // we do not generate min check for min <= 1 (see below). + // + os << "state = ~0UL;"; + break; + } + default: + { + os << "if (++count == " << max << "UL)" << endl + << "state = ~0UL;"; + } + }; + + // Delegate to the sub-machine and call _arm, _present, or _next + // if necessary. + // + + os << endl + << "v_state_& vs = *static_cast< v_state_* > (" << + "this->v_state_stack_.top ());" + << "v_state_descr_& vd = vs.data[vs.size++];" // push + << endl + << "vd.func = &" << ename (type_) << "::" << func << n << ";" + << "vd.state = s;" + << "vd.count = 0;" + << endl; + + if (Choice* pc = dynamic_cast (&c)) + { + os << "this->" << earm (*pc) << " (static_cast< " << + earm_tag (*pc) << " > (s));"; + } + else + { + Sequence& s (dynamic_cast (c)); + + if (max != 1) + os << "this->" << enext (s) << " ();"; + else if (c.min () == 0) + os << "this->" << epresent (s) << " ();"; + } + + os << "this->" << func << n << " (vd.state, vd.count, ns, n, " << + (poly_runtime ? (poly_code ? "t, " : "0, ") : "") << "true);" + << "}"; + + + // Not this compositor. We've elready moved to the final state + // if max == 1. + // + if (max != 1) + { + os << "else" + << "{" + << "assert (start);"; // Assuming well-formed XML + + // Check if min cardinality requirements have been met. Since + // count is always >= 1, don't generate dead code if min <= 1. + // + if (min > 1) + { + os << "if (count < " << min << "UL)" << endl + << "this->_schema_error (" << + "::xsde::cxx::schema_error::expected_element);"; + + /* + << "this->_expected_element (" << endl; + particle_name_.dispatch (c); + os << "," << endl + << "ns, n);"; + */ + } + + os << "state = ~0UL;" + << "}"; + } + + os << "break;" + << "}"; // case + } + + private: + ParticleName particle_name_; + }; + + + // + // + struct ParticleInSequence: Traversal::Particle, + Traversal::Compositor, + ParticleInCompositor + { + ParticleInSequence (Context& c, + UnsignedLong state, + UnsignedLong next_state, + SemanticGraph::Complex& type) + : ParticleInCompositor (c, type), + state_ (state), particle_name_ (c) + { + // next_state == 0 indicates the terminal state (~0UL). + // + if (next_state != 0) + { + std::wostringstream ostr; + ostr << next_state; + next_state_ = ostr.str (); + } + else + next_state_ = L"~0"; + } + + virtual Void + traverse (SemanticGraph::Particle& p) + { + UnsignedLong min (p.min ()), max (p.max ()); + + os << "case " << state_ << "UL:" << endl + << "{" + << "if ("; + + particle_test_.dispatch (p); + + os << ")" + << "{"; + + // This element. + // + + os << "if (start)" + << "{"; + + pre_post_calls (p); + + switch (max) + { + case 0: + { + os << "count++;"; + break; + } + case 1: + { + os << "count = 0;" + << "state = " << next_state_ << "UL;"; + break; + } + default: + { + os << "if (++count == " << max << "UL)" + << "{" + << "count = 0;" + << "state = " << next_state_ << "UL;" + << "}"; + } + }; + + os << "}" // start + << "break;" + << "}"; + + // Not this element. + // + + os << "else" + << "{" + << "assert (start);"; // Assuming well-formed XML. + + // Check if min cardinality requirements have been met. Since + // count is always >= 0, don't generate dead code if min == 0. + // + if (min != 0) + { + os << "if (count < " << min << "UL)" + << "{" + << "this->_schema_error (" << + "::xsde::cxx::schema_error::expected_element);" + << "break;" + << "}"; + + /* + << "this->_expected_element (" << endl; + particle_name_.dispatch (p); + os << "," << endl + << "ns, n);"; + */ + } + + os << "count = 0;" + << "state = " << next_state_ << "UL;" + << "// Fall through." << endl + << "}" // else + << "}"; // case + } + + virtual Void + traverse (SemanticGraph::Compositor& c) + { + using SemanticGraph::Choice; + using SemanticGraph::Sequence; + using SemanticGraph::Compositor; + + Boolean choice (c.is_a ()); + SemanticGraph::Context& cc (c.context ()); + + UnsignedLong max (c.max ()); + UnsignedLong min (cc.get ("p:effective-min")); + UnsignedLong n (cc.get ("p:comp-number")); + + String func (choice ? "choice_" : "sequence_"); + + os << "case " << state_ << "UL:" << endl + << "{" + << "unsigned long s = ~0UL;" + << endl; + + Boolean first (true); + + for (Compositor::ContainsIterator ci (c.contains_begin ()); + ci != c.contains_end (); ++ci) + { + SemanticGraph::Particle& p (ci->particle ()); + + if (p.is_a () && !cc.count ("p:comp-number")) + continue; // Empty compositor. + + if (!p.context ().count ("p:prefix")) + break; + + UnsignedLong state (p.context ().get ("p:state")); + + if (first) + first = false; + else + os << "else "; + + os << "if ("; + + particle_test_.dispatch (p); + + os << ")" << endl + << "s = " << state << "UL;"; + } + + if (!choice) + { + for (Compositor::ContainsIterator ci (c.contains_begin ()); + ci != c.contains_end (); ++ci) + { + Choice* pc = dynamic_cast (&ci->particle ()); + + if (pc && pc->min () == 1 && pc->max () == 1 && + pc->context ().get ("p:effective-min") == 0) + { + // This is a required choice with effective-min == 0 (i.e., + // it contains optional particle). We need to call the arm + // callback with that optional particle's tag. + // + + SemanticGraph::Particle* p (0); + OptionalParticleTest test (p); + test.traverse (*pc); + + if (p) + { + UnsignedLong state ( + pc->context ().get ("p:state")); + + os << endl + << "if (s > " << state << "UL)" << endl + << "{"; + choice_arm_call (p, pc, this); + os << "}"; + } + } + } + } + + // This element. + // + + os << endl + << "if (s != ~0UL)" + << "{" + << "assert (start);"; // End is handled by the sub-machine. + + switch (max) + { + case 0: + { + os << "count++;" + << endl; + break; + } + case 1: + { + os << "count = 0;" + << "state = " << next_state_ << "UL;" + << endl; + break; + } + default: + { + os << "if (++count == " << max << "UL)" + << "{" + << "count = 0;" + << "state = " << next_state_ << "UL;" + << "}"; + } + }; + + // Delegate to the sub-machine and call _arm, _present, or _next + // if necessary. + // + + os << "v_state_& vs = *static_cast< v_state_* > (" << + "this->v_state_stack_.top ());" + << "v_state_descr_& vd = vs.data[vs.size++];" // push + << endl + << "vd.func = &" << ename (type_) << "::" << func << n << ";" + << "vd.state = s;" + << "vd.count = 0;" + << endl; + + Choice* pc = dynamic_cast (&c); + + if (pc) + { + os << "this->" << earm (*pc) << " (static_cast< " << + earm_tag (*pc) << " > (s));"; + } + else + { + Sequence& s (dynamic_cast (c)); + + if (max != 1) + os << "this->" << enext (s) << " ();"; + else if (c.min () == 0) + os << "this->" << epresent (s) << " ();"; + } + + os << "this->" << func << n << " (vd.state, vd.count, ns, n, " << + (poly_runtime ? (poly_code ? "t, " : "0, ") : "") << "true);" + << "break;" + << "}"; + + // Not this compositor. + // + + os << "else" + << "{" + << "assert (start);"; // Assuming well-formed XML + + if (pc && c.min () == 1 && min == 0 && max == 1) + { + // This is a required choice with effective-min == 0 (i.e., + // it contains optional particle). We need to call the arm + // callback with that optional particle's tag. + // + SemanticGraph::Particle* p (0); + OptionalParticleTest test (p); + test.traverse (*pc); + + if (p) + choice_arm_call (p, pc, this); + } + + // Check if min cardinality requirements have been met. Since + // count is always >= 0, don't generate dead code if min == 0. + // + if (min != 0) + { + os << "if (count < " << min << "UL)" + << "{" + << "this->_schema_error (" << + "::xsde::cxx::schema_error::expected_element);" + << "break;" + << "}"; + } + + os << "count = 0;" + << "state = " << next_state_ << "UL;" + << "// Fall through." << endl + << "}" // else + << "}"; // case + } + + private: + UnsignedLong state_; + String next_state_; + + ParticleName particle_name_; + }; + + + // + // + struct ParticleFunction: Traversal::All, + Traversal::Choice, + Traversal::Sequence, + Context + { + ParticleFunction (Context& c, SemanticGraph::Complex& type) + : Context (c), type_ (type) + { + *this >> contains_particle_ >> *this; + } + + + virtual Void + traverse (SemanticGraph::All& a) + { + if (!a.context().count ("p:comp-number")) // Empty compositor. + return; + + using SemanticGraph::Element; + using SemanticGraph::Compositor; + + os << "void " << ename (type_) << "::" << endl + << "all_0 (unsigned long& state," << endl + << "unsigned char* count," << endl + << "const " << string_type << "& ns," << endl + << "const " << string_type << "& n," << endl; + + if (poly_runtime) + os << "const char*" << (poly_code ? " t" : "") << "," << endl; + + os << "bool start)" + << "{"; + + if (poly_code) + os << "XSDE_UNUSED (t);" + << endl; + + os << "::xsde::cxx::parser::context& ctx = this->_context ();" + << endl; + + for (Compositor::ContainsIterator ci (a.contains_begin ()), + ce (a.contains_end ()); ci != ce; ++ci) + { + ParticleInAll t (*this, type_); + t.dispatch (ci->particle ()); + } + + // Handle the flush. + // + os << "else if (n.empty () && ns.empty ())" + << "{"; + + for (Compositor::ContainsIterator ci (a.contains_begin ()), + ce (a.contains_end ()); ci != ce; ++ci) + { + if (ci->min () == 0) + continue; + + Element& e (dynamic_cast (ci->particle ())); + String ns (e.qualified () ? e.namespace_ ().name () : String ()); + UnsignedLong state (e.context ().get ("p:state")); + + os << "if (count[" << state << "UL] == 0)" + << "{" + << "this->_schema_error (" << + "::xsde::cxx::schema_error::expected_element);" + << "return;" + << "}"; + + /* + << "this->_expected_element (" << endl + << L << "\"" << ns << "\", " << + L << "\"" << e.name () << "\");"; + */ + } + + // Error handling code relies on the fact that there is no + // code after the if-else block. + // + os << "state = ~0UL;" + << "}" + << "else" << endl + << "state = ~0UL;" + << "}"; + } + + virtual Void + traverse (SemanticGraph::Choice& c) + { + using SemanticGraph::Compositor; + + SemanticGraph::Context& cc (c.context ()); + + if (!cc.count ("p:comp-number")) // Empty compositor. + return; + + UnsignedLong n (cc.get ("p:comp-number")); + + os << "void " << ename (type_) << "::" << endl + << "choice_" << n << " (unsigned long& state," << endl + << "unsigned long& count," << endl + << "const " << string_type << "& ns," << endl + << "const " << string_type << "& n," << endl; + + if (poly_runtime) + os << "const char*" << (poly_code ? " t" : "") << "," << endl; + + os << "bool start)" + << "{" + << "::xsde::cxx::parser::context& ctx = this->_context ();" + << endl; + + os << "XSDE_UNUSED (count);" + << "XSDE_UNUSED (ns);" + << "XSDE_UNUSED (n);" + << "XSDE_UNUSED (ctx);"; + + if (poly_code) + os << "XSDE_UNUSED (t);"; + + + os << endl + << "switch (state)" + << "{"; + + for (Compositor::ContainsIterator ci (c.contains_begin ()), + ce (c.contains_end ()); ci != ce; ++ci) + { + SemanticGraph::Particle& p (ci->particle ()); + + if (p.is_a () && !p.context().count ("p:comp-number")) + continue; // Empty compositor. + + ParticleInChoice t (*this, type_); + t.dispatch (p); + } + + // Error handling code relies on the fact that there is no + // code after the switch statement. + // + os << "}" // switch + << "}"; + + + // Generate nested compositor functions. + // + Traversal::Choice::traverse (c); + } + + virtual Void + traverse (SemanticGraph::Sequence& s) + { + if (!s.context().count ("p:comp-number")) // Empty compositor. + return; + + using SemanticGraph::Compositor; + + UnsignedLong n (s.context ().get ("p:comp-number")); + + os << "void " << ename (type_) << "::" << endl + << "sequence_" << n << " (unsigned long& state," << endl + << "unsigned long& count," << endl + << "const " << string_type << "& ns," << endl + << "const " << string_type << "& n," << endl; + + if (poly_runtime) + os << "const char*" << (poly_code ? " t" : "") << "," << endl; + + os << "bool start)" + << "{" + << "::xsde::cxx::parser::context& ctx = this->_context ();" + << endl; + + if (poly_code) + os << "XSDE_UNUSED (t);"; + + os << "XSDE_UNUSED (ctx);" + << endl; + + os << "switch (state)" + << "{"; + + UnsignedLong state (0); + + for (Compositor::ContainsIterator ci (s.contains_begin ()), + ce (s.contains_end ()); ci != ce;) + { + SemanticGraph::Particle& p (ci->particle ()); + + if (p.is_a () && !p.context().count ("p:comp-number")) + { + // Empty compositor. + // + ++ci; + continue; + } + + // Find the next state. + // + do + ++ci; + while (ci != ce && + ci->particle ().is_a () && + !ci->particle ().context().count ("p:comp-number")); + + UnsignedLong next (ci == ce ? 0 : state + 1); + + ParticleInSequence t (*this, state++, next, type_); + t.dispatch (p); + } + + // Error handling code relies on the fact that there is no + // code after the switch statement. + // + os << "case ~0UL:" << endl + << "break;" + << "}" // switch + << "}"; + + // Generate nested compositor functions. + // + Traversal::Sequence::traverse (s); + } + + private: + SemanticGraph::Complex& type_; + Traversal::ContainsParticle contains_particle_; + }; + + // + // + struct CompositorPre: Traversal::All, + Traversal::Compositor, + Context + { + CompositorPre (Context& c, SemanticGraph::Complex& type) + : Context (c), type_ (type) + { + } + + virtual Void + traverse (SemanticGraph::All& a) + { + // Clear the counts and push the initial state. + // + os << "v_all_count_.push ();" + << endl; + + SemanticGraph::Compositor& c (a); + traverse (c); + } + + virtual Void + traverse (SemanticGraph::Compositor&) // Choice and sequence. + { + os << "v_state_& vs = *static_cast< v_state_* > (" << + "this->v_state_stack_.top ());" + << "v_state_descr_& vd = vs.data[vs.size++];" // push + << endl + << "vd.func = 0;" + << "vd.state = 0;" + << "vd.count = 0;"; + } + + private: + SemanticGraph::Complex& type_; + }; + + + // + // + struct CompositorStartElement: Traversal::All, + Traversal::Compositor, + Context + { + CompositorStartElement (Context& c, SemanticGraph::Complex& type) + : Context (c), type_ (type), + particle_test_ (c), particle_name_ (c) + { + } + + virtual Void + traverse (SemanticGraph::All&) + { + // The 'all' state machine reaches the final state only + // on an unknown element, in which case we won't get here + // again (it would be a validation error). Note that 'all' + // compositor cannot contain nested compositors so we don't + // need to re-set vd. + // + os << "all_0 (vd->state, v_all_count_.top (), ns, n, " << + (poly_runtime ? (poly_code ? "t, " : "0, ") : "") << "true);" + << endl + << "if (vd->state != ~0UL || ctx.error_type ())" << endl + << "vd->count++;" + << "else" << endl + << "return false;" // Let our parent handle this. + << endl; + } + + virtual Void + traverse (SemanticGraph::Compositor& c) // Choice and sequence. + { + using SemanticGraph::Choice; + using SemanticGraph::Sequence; + using SemanticGraph::Compositor; + + Boolean choice (c.is_a ()); + SemanticGraph::Context& cc (c.context ()); + + UnsignedLong max (c.max ()); + UnsignedLong min (cc.get ("p:effective-min")); + UnsignedLong n (cc.get ("p:comp-number")); + + String func (choice ? "choice_" : "sequence_"); + + // Invoke the current state machine. If it reaches its + // terminal state, pop it and invoke the next one until + // we reach the top, which requires special handling. + // + os << "while (vd->func != 0)" + << "{" + << "(this->*vd->func) (vd->state, vd->count, ns, n, " << + (poly_runtime ? (poly_code ? "t, " : "0, ") : "") << "true);" + << endl + << "vd = vs.data + (vs.size - 1);" // re-acquire + << endl + << "if (vd->state == ~0UL && !ctx.error_type ())" << endl + << "vd = vs.data + (--vs.size - 1);" // pop + << "else" << endl + << "break;" + << "}"; + + + // Check if we got to the top. This code is pretty much the + // same as the one found in ParticleInSequence. + // + os << "if (vd->func == 0)" + << "{" + << "if (vd->state != ~0UL)" + << "{" + << "unsigned long s = ~0UL;" + << endl; + + Boolean first (true); + + // Note that we don't need to worry about the compositor + // being empty - this case is handled by our caller. + // + for (Compositor::ContainsIterator ci (c.contains_begin ()); + ci != c.contains_end (); ++ci) + { + SemanticGraph::Particle& p (ci->particle ()); + + if (p.is_a () && !cc.count ("p:comp-number")) + continue; // Empty compositor. + + if (!p.context ().count ("p:prefix")) + break; + + UnsignedLong state (p.context ().get ("p:state")); + + if (first) + first = false; + else + os << "else "; + + os << "if ("; + + particle_test_.dispatch (p); + + os << ")" << endl + << "s = " << state << "UL;"; + } + + if (!choice) + { + for (Compositor::ContainsIterator ci (c.contains_begin ()); + ci != c.contains_end (); ++ci) + { + Choice* pc = dynamic_cast (&ci->particle ()); + + if (pc && pc->min () == 1 && pc->max () == 1 && + pc->context ().get ("p:effective-min") == 0) + { + // This is a required choice with effective-min == 0 (i.e., + // it contains optional particle). We need to call the arm + // callback with that optional particle's tag. + // + + SemanticGraph::Particle* p (0); + OptionalParticleTest test (p); + test.traverse (*pc); + + if (p) + { + UnsignedLong state ( + pc->context ().get ("p:state")); + + os << endl + << "if (s > " << state << "UL)" << endl + << "{"; + choice_arm_call (p, pc, this); + os << "}"; + } + } + } + } + + os << endl + << "if (s != ~0UL)" + << "{"; + + // This element is a prefix of the root compositor. + // + + switch (max) + { + case 0: + { + os << "vd->count++;"; + break; + } + case 1: + { + os << "vd->count++;" + << "vd->state = ~0UL;"; + break; + } + default: + { + os << "if (++vd->count == " << max << "UL)" << endl + << "vd->state = ~0UL;"; + } + }; + + // Delegate to the sub-machine and call _arm, _present, or _next + // if necessary. + // + + os << endl + << "vd = vs.data + vs.size++;" // push + << "vd->func = &" << ename (type_) << "::" << func << n << ";" + << "vd->state = s;" + << "vd->count = 0;" + << endl; + + Choice* pc (dynamic_cast (&c)); + + if (pc) + { + os << "this->" << earm (*pc) << " (static_cast< " << + earm_tag (*pc) << " > (s));"; + } + else + { + Sequence& s (dynamic_cast (c)); + + if (max != 1) + os << "this->" << enext (s) << " ();"; + else if (c.min () == 0) + os << "this->" << epresent (s) << " ();"; + } + + os << "this->" << func << n << " (vd->state, vd->count, ns, n, " << + (poly_runtime ? (poly_code ? "t, " : "0, ") : "") << "true);" + << "}"; + + // This element is not our prefix. + // + + os << "else" + << "{"; + + if (pc && c.min () == 1 && min == 0 && max == 1) + { + // This is a required choice with effective-min == 0 (i.e., + // it contains optional particle). We need to call the arm + // callback with that optional particle's tag. + // + SemanticGraph::Particle* p (0); + OptionalParticleTest test (p); + test.traverse (*pc); + + if (p) + choice_arm_call (p, pc, this); + } + + // Check if min cardinality requirements have been met. Since + // count is always >= 0, don't generate dead code if min == 0. + // + if (min != 0) + { + // Note that we are returning true in case of an error to + // indicate that no further search is needed. + // + os << "if (vd->count < " << min << "UL)" + << "{" + << "this->_schema_error (" << + "::xsde::cxx::schema_error::expected_element);" + << "return true;" + << "}"; + } + + // Return false to indicate that we are not handling this element. + // + os << "return false;" + << "}" + << "}" // if (state != ~0) + << "else" << endl + << "return false;" + << "}"; // if (function == 0) + } + + private: + SemanticGraph::Complex& type_; + ParticleTest particle_test_; + ParticleName particle_name_; + }; + + + // + // + struct CompositorEndElement: Traversal::All, + Traversal::Compositor, + Context + { + CompositorEndElement (Context& c, SemanticGraph::Complex& type) + : Context (c), type_ (type) + { + } + + virtual Void + traverse (SemanticGraph::All&) + { + os << "all_0 (vd.state, v_all_count_.top (), " << + "ns, n, " << (poly_runtime ? "0, " : "") << "false);" + << endl; + } + + virtual Void + traverse (SemanticGraph::Compositor&) // Choice and sequence. + { + os << "assert (vd.func != 0);" + << "(this->*vd.func) (vd.state, vd.count, ns, n, " << + (poly_runtime ? "0, " : "") << "false);" + << endl + << "if (vd.state == ~0UL)" << endl + << "vs.size--;" // pop + << endl; + } + + private: + SemanticGraph::Complex& type_; + }; + + + // + // + struct CompositorPost: Traversal::All, + Traversal::Compositor, + Context + { + CompositorPost (Context& c, SemanticGraph::Complex& type) + : Context (c), type_ (type), particle_name_ (c) + { + } + + virtual Void + traverse (SemanticGraph::All& a) + { + using SemanticGraph::Element; + + os << "v_state_& vs = *static_cast< v_state_* > (" << + "this->v_state_stack_.top ());" + << "v_state_descr_& vd = vs.data[vs.size - 1];" + << endl; + + // Flush the state machine with the empty element name. This + // allows us to detect missing content. + // + os << "if (vd.count != 0)" + << "{" + << string_type << " empty;" + << "all_0 (vd.state, v_all_count_.top (), empty, empty, " << + (poly_runtime ? "0, " : "") << "true);" + << "}"; + + if (a.context ().get ("p:effective-min") != 0) + { + os << "else" << endl + << "this->_schema_error (" << + "::xsde::cxx::schema_error::expected_element);"; + + /* + << "this->_expected_element (" << endl; + particle_name_.dispatch (a); + os << ");"; + */ + } + + os << endl + << "vs.size--;" // pop + << "v_all_count_.pop ();"; + } + + virtual Void + traverse (SemanticGraph::Compositor& c) // Choice and sequence. + { + using SemanticGraph::Choice; + + UnsignedLong min ( + c.context ().get ("p:effective-min")); + + os << "v_state_& vs = *static_cast< v_state_* > (" << + "this->v_state_stack_.top ());" + << "v_state_descr_* vd = vs.data + (vs.size - 1);" + << endl; + + + // Flush unfinished state machines with the empty element name. + // This allows us to detect missing content. Note that I am + // not re-setting vd since no new compositors are pushed on + // flush. + // + os << string_type << " empty;" + << "while (vd->func != 0)" + << "{" + << "(this->*vd->func) (vd->state, vd->count, empty, empty, " << + (poly_runtime ? "0, " : "") << "true);" + << endl + << "if (ctx.error_type ())" << endl + << "return;" + << endl + << "assert (vd->state == ~0UL);" + << "vd = vs.data + (--vs.size - 1);" // pop + << "}"; + + + Choice* pc (dynamic_cast (&c)); + + if (pc && c.min () == 1 && min == 0 && c.max () == 1) + { + // This is a required choice with effective-min == 0 (i.e., + // it contains optional particle). We need to call the arm + // callback with that optional particle's tag. + // + SemanticGraph::Particle* p (0); + OptionalParticleTest test (p); + test.traverse (*pc); + + if (p) + { + os << "if (vd->count == 0)" << endl + << "{"; + choice_arm_call (p, pc, this); + os << "}"; + } + } + + // Check if min cardinality requirements have been met. Since + // count is always >= 0, don't generate dead code if min == 0. + // + if (min != 0) + { + os << "if (vd->count < " << min << "UL)" << endl + << "this->_schema_error (" << + "::xsde::cxx::schema_error::expected_element);"; + } + } + + private: + SemanticGraph::Complex& type_; + ParticleName particle_name_; + }; + + + // + // + struct Complex : Traversal::Complex, Context + { + Complex (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& c) + { + // Nothing to generate if we don't have any elements and wildcards. + // + if (!has (c) && + !has_particle (c)) + return; + + using SemanticGraph::Compositor; + + String const& name (ename (c)); + Compositor& comp (c.contains_compositor ().compositor ()); + + // Don't use restriction_p here since we don't want special + // treatment of anyType. + // + Boolean restriction ( + c.inherits_p () && + c.inherits ().is_a ()); + + os <<"// Element validation and dispatch functions for " << + name << "." << endl + <<"//" << endl; + + // _start_element_impl + // + + os << "bool " << name << "::" << endl + << "_start_element_impl (const " << string_type << "& ns," << endl + << "const " << string_type << "& n"; + + if (poly_runtime) + os << "," << endl + << "const char*" << (poly_code ? " t" : ""); + + os << ")" + << "{"; + + if (poly_code) + os << "XSDE_UNUSED (t);" + << endl; + + os << "::xsde::cxx::parser::context& ctx = this->_context ();" + << endl; + + + os << "v_state_& vs = *static_cast< v_state_* > (" << + "this->v_state_stack_.top ());" + << "v_state_descr_* vd = vs.data + (vs.size - 1);" + << endl; + + //@@ OPT: I don't really need to call parser_base since it always + // returns false. + // + // In case of an inheritance-by-extension, call our base first. + // We don't need to generate this code for the 'all' compositor + // because it can only inherit from the empty content model. + // Sattes of the root machine for sequence and choice: + // + // 0 - calling base + // 1 - base returned false + // ~0 - terminal state + // + if (!restriction && !comp.is_a ()) + { + os << "if (vd->func == 0 && vd->state == 0)" + << "{"; + + // We cannot use the fully-qualified base name directly + // because of some broken compilers (EVC 4.0). + // + String base (unclash (name, "base")); + + os << "typedef "; + + if (c.inherits_p ()) + os << fq_name (c.inherits ().base ()); + else + os << complex_base; + + os << " " << base << ";" + << "if (" << base << "::"; + + if (poly_runtime) + os << "_start_element_impl (ns, n, " << + (poly_code ? "t" : "0") << "))" << endl; + else + os << "_start_element_impl (ns, n))" << endl; + + os << "return true;" + << "else" << endl + << "vd->state = 1;" + << "}"; + } + + { + CompositorStartElement t (*this, c); + t.dispatch (comp); + } + + os << "return true;" + << "}"; + + + // _end_element_impl + // + + os << "bool " << name << "::" << endl + << "_end_element_impl (const " << string_type << "& ns," << endl + << "const " << string_type << "& n)" + << "{"; + + os << "v_state_& vs = *static_cast< v_state_* > (" << + "this->v_state_stack_.top ());" + << "v_state_descr_& vd = vs.data[vs.size - 1];" + << endl; + + //@@ OPT: I don't really need to call parser_base since it always + // returns false. + // + // In case of an inheritance-by-extension, call our base first. + // We don't need to generate this code for the 'all' compositor + // because it can only inherit from the empty content model. + // + if (!restriction && !comp.is_a ()) + { + os << "if (vd.func == 0 && vd.state == 0)" + << "{"; + + // We cannot use the fully-qualified base name directly + // because of some broken compilers (EVC 4.0). + // + String base (unclash (name, "base")); + + os << "typedef "; + + if (c.inherits_p ()) + os << fq_name (c.inherits ().base ()); + else + os << complex_base; + + os << " " << base << ";" + << "if (!" << base << "::_end_element_impl (ns, n))" << endl + << "assert (false);" // Start and end should match. + << "return true;" + << "}"; + } + + { + CompositorEndElement t (*this, c); + t.dispatch (comp); + } + + os << "return true;" + << "}"; + + + // _pre_e_validate + // + os << "void " << name << "::" << endl + << "_pre_e_validate ()" + << "{"; + + if (exceptions) + os << "this->v_state_stack_.push ();"; + else + os << "if (this->v_state_stack_.push ())" + << "{" + << "this->_sys_error (::xsde::cxx::sys_error::no_memory);" + << "return;" + << "}"; + + os << "static_cast< v_state_* > (this->v_state_stack_.top ())->" << + "size = 0;" + << endl; + + { + // Assuming that this code cannot trigger an error. + // + CompositorPre t (*this, c); + t.dispatch (comp); + } + + // In case of an inheritance-by-extension, call our base + // _pre_e_validate. We don't need to generate this code for the + // 'all' compositor because it can only inherit from the empty + // content model. + // + if (!restriction && !comp.is_a ()) + { + // We don't need to call parser_base's implementation + // since it does nothing. + // + if (c.inherits_p ()) + { + // We cannot use the fully-qualified base name directly + // because of some broken compilers (EVC 4.0). + // + String base (unclash (name, "base")); + + os << "typedef " << fq_name (c.inherits ().base ()) << " " << + base << ";" + << base << "::_pre_e_validate ();"; + } + } + + os << "}"; + + + // _post_e_validate + // + os << "void " << name << "::" << endl + << "_post_e_validate ()" + << "{"; + + if (!comp.is_a ()) + os << "::xsde::cxx::parser::context& ctx = this->_context ();" + << endl; + + // In case of an inheritance-by-extension, call our base + // _post_e_validate. We don't need to generate this code for + // the 'all' compositor because it can only inherit from + // the empty content model. + // + if (!restriction && !comp.is_a ()) + { + // We don't need to call parser_base's implementation + // since it does nothing. + // + if (c.inherits_p ()) + { + // We cannot use the fully-qualified base name directly + // because of some broken compilers (EVC 4.0). + // + String base (unclash (name, "base")); + + os << "typedef " << fq_name (c.inherits ().base ()) << " " << + base << ";" + << base << "::_post_e_validate ();" + << endl; + + os << "if (ctx.error_type ())" << endl + << "return;" + << endl; + } + } + + { + CompositorPost t (*this, c); + t.dispatch (c.contains_compositor ().compositor ()); + } + + os << endl + << "this->v_state_stack_.pop ();" + << "}"; + + // + // + ParticleFunction t (*this, c); + t.dispatch (c.contains_compositor ().compositor ()); + } + }; + } + + Void + generate_element_validation_source (Context& ctx) + { + ctx.os << "#include " << endl + << endl; + + Traversal::Schema schema; + + Traversal::Sources sources; + Traversal::Names schema_names; + + Namespace ns (ctx); + Traversal::Names names; + + schema >> sources >> schema; + schema >> schema_names >> ns >> names; + + Complex complex (ctx); + + names >> complex; + + schema.dispatch (ctx.schema_root); + } + } +} diff --git a/xsde/cxx/parser/element-validation-source.hxx b/xsde/cxx/parser/element-validation-source.hxx new file mode 100644 index 0000000..4dbf5e3 --- /dev/null +++ b/xsde/cxx/parser/element-validation-source.hxx @@ -0,0 +1,22 @@ +// file : xsde/cxx/parser/element-validation-source.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef CXX_PARSER_ELEMENT_VALIDATION_SOURCE_HXX +#define CXX_PARSER_ELEMENT_VALIDATION_SOURCE_HXX + +#include + +#include + +namespace CXX +{ + namespace Parser + { + Void + generate_element_validation_source (Context&); + } +} + +#endif // CXX_PARSER_ELEMENT_VALIDATION_SOURCE_HXX diff --git a/xsde/cxx/parser/elements.cxx b/xsde/cxx/parser/elements.cxx new file mode 100644 index 0000000..e4d9cde --- /dev/null +++ b/xsde/cxx/parser/elements.cxx @@ -0,0 +1,258 @@ +// file : xsde/cxx/parser/elements.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include + +namespace CXX +{ + namespace Parser + { + Context:: + Context (std::wostream& o, + SemanticGraph::Schema& root, + CLI::Options const& ops, + Regex const* he, + Regex const* ie, + Regex const* hie) + : CXX::Context (o, + root, + "p:name", + "char", + ops.value (), + ops.value (), + "", // export symbol + ops.value (), + ops.value (), + ops.value (), + ops.value (), + ops.value (), + ops.value (), + ops.value ()), + options (ops), + xml_parser (xml_parser_), + simple_base (simple_base_), + complex_base (complex_base_), + list_base (list_base_), + parser_map (parser_map_), + validation (!ops.value ()), + exceptions (!ops.value ()), + poly_code (ops.value ()), + poly_runtime (poly_code || ops.value ()), + reset (!ops.value ()), + mixin (ops.value ()), + tiein (!mixin && !ops.value ()), + hxx_expr (he), + ixx_expr (ie), + hxx_impl_expr (hie), + xml_parser_ ("expat") + { + string_type = L"::xsde::cxx::ro_string"; + + simple_base_ = L"::xsde::cxx::parser::"; + simple_base_ += (validation ? L"validating" : L"non_validating"); + simple_base_ += L"::simple_content"; + + complex_base_ = L"::xsde::cxx::parser::"; + complex_base_ += (validation ? L"validating" : L"non_validating"); + complex_base_ += L"::complex_content"; + + list_base_ = L"::xsde::cxx::parser::"; + list_base_ += (validation ? L"validating" : L"non_validating"); + list_base_ += L"::list_base"; + + if (poly_code) + parser_map_ = xs_ns_name () + L"::parser_map"; + } + + String Context:: + real_fq_name (SemanticGraph::Nameable& n) + { + SemanticGraph::Context& c (n.context ()); + + if (c.count ("p:real-name")) + return c.get ("p:real-name"); + else + return fq_name (n); + } + + Content::Value Context:: + content (SemanticGraph::Complex& c) + { + using namespace SemanticGraph; + + if (c.mixed ()) + return Content::mixed; + + if (c.inherits_p ()) + { + Type& base (c.inherits ().base ()); + + if (Complex* cb = dynamic_cast (&base)) + return content (*cb); + + if (base.is_a ()) + return Content::complex; + + // Everyhting else (built-in type and AnySimpleType) is simple + // content. + // + return Content::simple; + } + else + return Content::complex; + } + + + String const& Context:: + ret_type (SemanticGraph::Type& t) + { + return t.context ().get ("p:ret-type"); + } + + String const& Context:: + arg_type (SemanticGraph::Type& t) + { + return t.context ().get ("p:arg-type"); + } + + String const& Context:: + post_name (SemanticGraph::Type& t) + { + return t.context ().get ("p:post"); + } + + String const& Context:: + epresent (SemanticGraph::Compositor& c) + { + return c.context ().get ("p:present"); + } + + String const& Context:: + enext (SemanticGraph::Sequence& s) + { + return s.context ().get ("p:next"); + } + + String const& Context:: + etag (SemanticGraph::Particle& p) + { + return p.context ().get ("p:tag"); + } + + String const& Context:: + earm (SemanticGraph::Choice& c) + { + return c.context ().get ("p:arm"); + } + + String const& Context:: + earm_tag (SemanticGraph::Choice& c) + { + return c.context ().get ("p:arm-tag"); + } + + String const& Context:: + eparser (SemanticGraph::Member& m) + { + return m.context ().get ("p:parser"); + } + + String const& Context:: + emember (SemanticGraph::Member& m) + { + return m.context ().get ("p:member"); + } + + String const& Context:: + emember_cache (SemanticGraph::Member& m) + { + return m.context ().get ("p:member-cache"); + } + + String const& Context:: + emember_map (SemanticGraph::Member& m) + { + return m.context ().get ("p:member-map"); + } + + String const& Context:: + etiein (SemanticGraph::Type& t) + { + return t.context ().get ("p:tiein"); + } + + String const& Context:: + eimpl (SemanticGraph::Type& t) + { + return t.context ().get ("p:impl"); + } + + // Includes + // + Void TypeForward:: + traverse (SemanticGraph::Type& t) + { + os << "class " << t.context ().get (name_key_) << ";"; + } + + Void Includes:: + traverse_ (SemanticGraph::Uses& u) + { + // Support for weak (forward) inclusion used in the file-per-type + // compilation model. + // + Boolean weak (u.context ().count ("weak")); + + if (weak && (type_ == header || type_ == impl_header)) + { + // Generate forward declarations. We don't really need them + // in the impl files. + // + if (type_ == header) + schema_.dispatch (u.schema ()); + + return; + } + + if (type_ == source && !weak) + return; + + SemanticGraph::Path path (u.path ()); + + // Try to use the portable representation of the path. If that + // fails, fall back to the native representation. + // + NarrowString path_str; + try + { + path_str = path.string (); + } + catch (SemanticGraph::InvalidPath const&) + { + path_str = path.native_file_string (); + } + + String inc_path; + + switch (type_) + { + case header: + case source: + { + inc_path = ctx_.hxx_expr->merge (path_str); + break; + } + case impl_header: + { + inc_path = ctx_.hxx_impl_expr->merge (path_str); + break; + } + } + + ctx_.os << "#include " << ctx_.process_include_path (inc_path) << endl + << endl; + } + } +} diff --git a/xsde/cxx/parser/elements.hxx b/xsde/cxx/parser/elements.hxx new file mode 100644 index 0000000..0bb226e --- /dev/null +++ b/xsde/cxx/parser/elements.hxx @@ -0,0 +1,431 @@ +// file : xsde/cxx/parser/elements.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef CXX_PARSER_ELEMENTS_HXX +#define CXX_PARSER_ELEMENTS_HXX + +#include + +#include + +#include + +#include + +namespace CXX +{ + namespace Parser + { + struct Content + { + enum Value + { + simple, + complex, + mixed + }; + }; + + // + // + class Context: public CXX::Context + { + public: + typedef BackendElements::Regex::Expression Regex; + + public: + Context (std::wostream&, + SemanticGraph::Schema&, + CLI::Options const&, + Regex const* hxx_expr, + Regex const* ixx_expr, + Regex const* hxx_impl_expr); + + protected: + Context (Context& c) + : CXX::Context (c), + options (c.options), + xml_parser (c.xml_parser), + simple_base (c.simple_base), + complex_base (c.complex_base), + list_base (c.list_base), + parser_map (c.parser_map), + validation (c.validation), + exceptions (c.exceptions), + poly_code (c.poly_code), + poly_runtime (c.poly_runtime), + reset (c.reset), + mixin (c.mixin), + tiein (c.tiein), + hxx_expr (c.hxx_expr), + ixx_expr (c.ixx_expr), + hxx_impl_expr (c.hxx_impl_expr) + { + } + + Context (Context& c, std::wostream& o) + : CXX::Context (c, o), + options (c.options), + xml_parser (c.xml_parser), + simple_base (c.simple_base), + complex_base (c.complex_base), + list_base (c.list_base), + parser_map (c.parser_map), + validation (c.validation), + exceptions (c.exceptions), + poly_code (c.poly_code), + poly_runtime (c.poly_runtime), + reset (c.reset), + mixin (c.mixin), + tiein (c.tiein), + hxx_expr (c.hxx_expr), + ixx_expr (c.ixx_expr), + hxx_impl_expr (c.hxx_impl_expr) + { + } + + public: + Boolean + restriction_p (SemanticGraph::Complex& c) const + { + if (c.inherits_p () && + c.inherits ().is_a ()) + { + // Restriction of anyType is a special case. + // + return !c.inherits ().base ().is_a (); + } + + return false; + } + + // Real (e.g., non-typedef) fq-name. + // + String + real_fq_name (SemanticGraph::Nameable& n); + + public: + static Content::Value + content (SemanticGraph::Complex&); + + public: + static String const& + ret_type (SemanticGraph::Type&); + + static String const& + arg_type (SemanticGraph::Type&); + + static String const& + post_name (SemanticGraph::Type&); + + public: + // Optional. + // + static String const& + epresent (SemanticGraph::Compositor&); + + // Sequence. + // + static String const& + enext (SemanticGraph::Sequence&); + + // Choice. + // + static String const& + etag (SemanticGraph::Particle&); + + static String const& + earm (SemanticGraph::Choice&); + + static String const& + earm_tag (SemanticGraph::Choice&); + + public: + static String const& + eparser (SemanticGraph::Member&); + + static String const& + emember (SemanticGraph::Member&); + + static String const& + emember_cache (SemanticGraph::Member&); + + static String const& + emember_map (SemanticGraph::Member&); + + static String const& + etiein (SemanticGraph::Type&); + + public: + static String const& + eimpl (SemanticGraph::Type&); + + public: + CLI::Options const& options; + String& xml_parser; + String& simple_base; + String& complex_base; + String& list_base; + String& parser_map; + + Boolean validation; + Boolean exceptions; + Boolean poly_code; + Boolean poly_runtime; + Boolean reset; + Boolean mixin; + Boolean tiein; + + Regex const* hxx_expr; + Regex const* ixx_expr; + Regex const* hxx_impl_expr; + + private: + String xml_parser_; + String simple_base_; + String complex_base_; + String list_base_; + String parser_map_; + }; + + // + // + struct RequiredAttributeTest: Traversal::Attribute + { + RequiredAttributeTest (Boolean& result) + : result_ (result) + { + } + + virtual Void + traverse (Type& a) + { + if (!result_ && !a.optional ()) + result_ = true; + } + + private: + Boolean& result_; + }; + + + // + // + struct ParticleParamDecl: Traversal::Element, Context + { + ParticleParamDecl (Context& c, Boolean& first, Boolean name_arg) + : Context (c), first_ (first), name_arg_ (name_arg) + { + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + if (!first_) + os << "," << endl; + else + first_ = false; + + os << fq_name (e.type ()) << "&"; + + if (name_arg_) + os << " " << ename (e); + else + os << " /* " << comment (e.name ()) << " */"; + } + + private: + Boolean& first_; + Boolean name_arg_; + }; + + struct AttributeParamDecl: Traversal::Attribute, Context + { + AttributeParamDecl (Context& c, Boolean& first, Boolean name_arg) + : Context (c), first_ (first), name_arg_ (name_arg) + { + } + + virtual Void + traverse (Type& a) + { + if (!first_) + os << "," << endl; + else + first_ = false; + + os << fq_name (a.type ()) << "&"; + + if (name_arg_) + os << " " << ename (a); + else + os << " /* " << comment (a.name ()) << " */"; + } + + private: + Boolean& first_; + Boolean name_arg_; + }; + + struct ParserParamDecl : Traversal::Complex, + Traversal::List, + Context + { + ParserParamDecl (Context& c, Boolean name_arg) + : Context (c), + particle_ (c, first_, name_arg), + attribute_ (c, first_, name_arg), + first_ (true), + name_arg_ (name_arg) + { + inherits_ >> *this; + + contains_compositor_ >> compositor_ >> contains_particle_; + contains_particle_ >> particle_; + contains_particle_ >> compositor_; + + names_ >> attribute_; + } + + virtual Void + traverse (SemanticGraph::Complex& c) + { + inherits (c, inherits_); + + if (!restriction_p (c)) + { + names (c, names_); + contains_compositor (c, contains_compositor_); + } + } + + virtual Void + traverse (SemanticGraph::List& l) + { + if (!first_) + os << "," << endl; + else + first_ = false; + + os << fq_name (l.argumented ().type ()) << "&"; + + if (name_arg_) + os << " " << ename (l) << "_item"; + else + os << " /* " << comment (l.name ()) << " item */"; + } + + private: + Traversal::Inherits inherits_; + + Traversal::Compositor compositor_; + ParticleParamDecl particle_; + Traversal::ContainsCompositor contains_compositor_; + Traversal::ContainsParticle contains_particle_; + + AttributeParamDecl attribute_; + Traversal::Names names_; + + Boolean first_; + Boolean name_arg_; + }; + + // + // + struct TypeForward: Traversal::Type, Context + { + TypeForward (Context& c, Char const* name_key) + : Context (c), name_key_ (name_key) + { + } + + virtual Void + traverse (SemanticGraph::Type& t); + + private: + Char const* name_key_; + }; + + struct Includes : Traversal::Imports, + Traversal::Includes + { + enum Type + { + header, + source, + impl_header + }; + + Includes (Context& c, Type t) + : ctx_ (c), + type_ (t), + namespace_ (c), + type_forward_ (c, t == header ? "p:name" : "p:impl") + { + schema_ >> schema_names_ >> namespace_ >> names_ >> type_forward_; + } + + virtual Void + traverse (SemanticGraph::Imports& i) + { + traverse_ (i); + } + + virtual Void + traverse (SemanticGraph::Includes& i) + { + traverse_ (i); + } + + private: + Void + traverse_ (SemanticGraph::Uses&); + + private: + Context& ctx_; + Type type_; + + Traversal::Schema schema_; + Traversal::Names schema_names_; + Namespace namespace_; + Traversal::Names names_; + TypeForward type_forward_; + }; + + // Find root element for the test driver. + // + struct RootElement: Traversal::Element + { + RootElement (CLI::Options const& options, + SemanticGraph::Element*& element) + : options_ (options), element_ (element) + { + } + + virtual Void + traverse (Type& e) + { + if (options_.value ()) + { + if (element_ == 0) + element_ = &e; + } + else if (String name = options_.value ()) + { + if (e.name () == name) + element_ = &e; + } + else + element_ = &e; // Cover root-element-last and no option. + } + + private: + CLI::Options const& options_; + SemanticGraph::Element*& element_; + }; + } +} + +#endif // CXX_PARSER_ELEMENTS_HXX diff --git a/xsde/cxx/parser/generator.cxx b/xsde/cxx/parser/generator.cxx new file mode 100644 index 0000000..e552c92 --- /dev/null +++ b/xsde/cxx/parser/generator.cxx @@ -0,0 +1,1615 @@ +// file : xsde/cxx/parser/generator.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include +#include + +#include + +#include + +#include + +#include "../../../libxsde/xsde/cxx/version.hxx" + +using std::endl; +using std::wcerr; + +using namespace XSDFrontend::SemanticGraph; + +// +// +typedef +boost::filesystem::wifstream +WideInputFileStream; + +typedef +boost::filesystem::wofstream +WideOutputFileStream; + +typedef +boost::filesystem::ifstream +NarrowInputFileStream; + +namespace CXX +{ + namespace + { + Char const copyright_gpl[] = + "// Copyright (C) 2005-2009 Code Synthesis Tools CC\n" + "//\n" + "// This program was generated by CodeSynthesis XSD/e, an XML Schema\n" + "// to C++ data binding compiler for embedded systems.\n" + "//\n" + "// This program is free software; you can redistribute it and/or modify\n" + "// it under the terms of the GNU General Public License version 2 as\n" + "// published by the Free Software Foundation.\n" + "//\n" + "// This program is distributed in the hope that it will be useful,\n" + "// but WITHOUT ANY WARRANTY; without even the implied warranty of\n" + "// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" + "// GNU General Public License for more details.\n" + "//\n" + "// You should have received a copy of the GNU General Public License\n" + "// along with this program; if not, write to the Free Software\n" + "// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n" + "//\n" + "//\n\n"; + + Char const copyright_proprietary[] = + "// Copyright (C) 2005-2009 Code Synthesis Tools CC\n" + "//\n" + "// This program was generated by CodeSynthesis XSD/e, an XML Schema to\n" + "// C++ data binding compiler for embedded systems, in the Proprietary\n" + "// License mode. You should have received a proprietary license from\n" + "// Code Synthesis Tools CC prior to generating this code. See the\n" + "// license text for conditions.\n" + "//\n\n"; + + Char const copyright_impl[] = + "// Not copyrighted - public domain.\n" + "//\n" + "// This sample parser implementation was generated by CodeSynthesis XSD/e,\n" + "// an XML Schema to C++ data binding compiler for embedded systems. You\n" + "// may use it in your programs without any restrictions.\n" + "//\n\n"; + } + + namespace Parser + { + namespace CLI + { + extern Key type_map = "type-map"; + extern Key no_stl = "no-stl"; + extern Key no_iostream = "no-iostream"; + extern Key no_exceptions = "no-exceptions"; + extern Key no_long_long = "no-long-long"; + extern Key reuse_style_mixin = "reuse-style-mixin"; + extern Key reuse_style_none = "reuse-style-none"; + extern Key generate_inline = "generate-inline"; + extern Key suppress_validation = "suppress-validation"; + extern Key generate_polymorphic = "generate-polymorphic"; + extern Key runtime_polymorphic = "runtime-polymorphic"; + extern Key suppress_reset = "suppress-reset"; + extern Key generate_noop_impl = "generate-noop-impl"; + extern Key generate_print_impl = "generate-print-impl"; + extern Key generate_test_driver = "generate-test-driver"; + extern Key force_overwrite = "force-overwrite"; + extern Key root_element_first = "root-element-first"; + extern Key root_element_last = "root-element-last"; + extern Key root_element = "root-element"; + extern Key generate_xml_schema = "generate-xml-schema"; + extern Key extern_xml_schema = "extern-xml-schema"; + extern Key output_dir = "output-dir"; + extern Key skel_type_suffix = "skel-type-suffix"; + extern Key skel_file_suffix = "skel-file-suffix"; + extern Key impl_type_suffix = "impl-type-suffix"; + extern Key impl_file_suffix = "impl-file-suffix"; + extern Key namespace_map = "namespace-map"; + extern Key namespace_regex = "namespace-regex"; + extern Key namespace_regex_trace = "namespace-regex-trace"; + extern Key reserved_name = "reserved-name"; + extern Key include_with_brackets = "include-with-brackets"; + extern Key include_prefix = "include-prefix"; + extern Key include_regex = "include-regex"; + extern Key include_regex_trace = "include-regex-trace"; + extern Key guard_prefix = "guard-prefix"; + extern Key hxx_suffix = "hxx-suffix"; + extern Key ixx_suffix = "ixx-suffix"; + extern Key cxx_suffix = "cxx-suffix"; + extern Key hxx_regex = "hxx-regex"; + extern Key ixx_regex = "ixx-regex"; + extern Key cxx_regex = "cxx-regex"; + extern Key hxx_prologue = "hxx-prologue"; + extern Key ixx_prologue = "ixx-prologue"; + extern Key cxx_prologue = "cxx-prologue"; + extern Key prologue = "prologue"; + extern Key hxx_epilogue = "hxx-epilogue"; + extern Key ixx_epilogue = "ixx-epilogue"; + extern Key cxx_epilogue = "cxx-epilogue"; + extern Key epilogue = "epilogue"; + extern Key hxx_prologue_file = "hxx-prologue-file"; + extern Key ixx_prologue_file = "ixx-prologue-file"; + extern Key cxx_prologue_file = "cxx-prologue-file"; + extern Key prologue_file = "prologue-file"; + extern Key hxx_epilogue_file = "hxx-epilogue-file"; + extern Key ixx_epilogue_file = "ixx-epilogue-file"; + extern Key cxx_epilogue_file = "cxx-epilogue-file"; + extern Key epilogue_file = "epilogue-file"; + extern Key show_anonymous = "show-anonymous"; + extern Key show_sloc = "show-sloc"; + extern Key proprietary_license = "proprietary-license"; + } + } + + Void Parser::Generator:: + usage () + { + std::wostream& e (wcerr); + ::CLI::Indent::Clip< ::CLI::OptionsUsage, WideChar> clip (e); + + e << "--type-map " << endl + << " Read XML Schema to C++ type mapping information\n" + << " from . Repeat this option to specify\n" + << " several type maps. Type maps are considered in\n" + << " order of appearance and the first match is used." + << endl; + + e << "--no-stl" << endl + << " Generate code that does not use STL." + << endl; + + e << "--no-iostream" << endl + << " Generate code that does not use the iostream\n" + << " library." + << endl; + + e << "--no-exceptions" << endl + << " Generate code that does not use C++ exceptions." + << endl; + + e << "--no-long-long" << endl + << " Generate code that does not use the long long\n" + << " and unsigned long long types." + << endl; + + e << "--reuse-style-mixin" << endl + << " Generate code that supports the mixin base parser\n" + << " implementation reuse style." + << endl; + + e << "--reuse-style-none" << endl + << " Do not generate any support for base parser\n" + << " implementation reuse." + << endl; + + e << "--generate-inline" << endl + << " Generate certain functions inline." + << endl; + + e << "--suppress-validation" << endl + << " Suppress the generation of validation code." + << endl; + + e << "--generate-polymorphic" << endl + << " Generate polymorphism-aware code. Specify this\n" + << " option if you use substitution groups or xsi:type." + << endl; + + e << "--runtime-polymorphic" << endl + << " Generate non-polymorphic code that uses the\n" + << " runtime library configured with polymorphism\n" + << " support." + << endl; + + e << "--suppress-reset" << endl + << " Suppress the generation of parser reset code." + << endl; + + e << "--generate-noop-impl" << endl + << " Generate a sample parser implementation that\n" + << " does nothing (no operation)." + << endl; + + e << "--generate-print-impl" << endl + << " Generate a sample parser implementation that\n" + << " prints the XML data to STDOUT." + << endl; + + e << "--generate-test-driver" << endl + << " Generate a test driver for the sample parser\n" + << " implementation." + << endl; + + e << "--force-overwrite" << endl + << " Force overwriting of the existing implementation\n" + << " and test driver files." + << endl; + + e << "--root-element-first" << endl + << " Indicate that the first global element is the\n" + << " document root." + << endl; + + e << "--root-element-last" << endl + << " Indicate that the last global element is the\n" + << " document root." + << endl; + + e << "--root-element " << endl + << " Indicate that is the document root." + << endl; + + e << "--generate-xml-schema" << endl + << " Generate a C++ header file as if the schema being\n" + << " compiled defines the XML Schema namespace." + << endl; + + e << "--extern-xml-schema " << endl + << " Generate code as if the XML Schema namespace was\n" + << " defined in and xsd:included in the schema\n" + << " being compiled." + << endl; + + e << "--output-dir " << endl + << " Write generated files to instead of the\n" + << " current directory." + << endl; + + e << "--skel-type-suffix " << endl + << " Use instead of the default '_pskel' to\n" + << " construct the names of generated parser skeletons." + << endl; + + e << "--skel-file-suffix " << endl + << " Use instead of the default '-pskel' to\n" + << " construct the names of generated parser skeleton\n" + << " files." + << endl; + + e << "--impl-type-suffix " << endl + << " Use instead of the default '_pimpl' to\n" + << " construct the names of parser implementations for\n" + << " the built-in XML Schema types and sample parser\n" + << " implementations." + << endl; + + e << "--impl-file-suffix " << endl + << " Use instead of the default '-pimpl' to\n" + << " construct the names of generated sample parser\n" + << " implementation files." + << endl; + + e << "--namespace-map =" << endl + << " Map XML Schema namespace to C++ namespace\n" + << " . Repeat this option to specify mapping for\n" + << " more than one XML Schema namespace." + << endl; + + e << "--namespace-regex " << endl + << " Add to the list of regular expressions\n" + << " used to translate XML Schema namespace names to\n" + << " C++ namespace names." + << endl; + + e << "--namespace-regex-trace" << endl + << " Trace the process of applying regular expressions\n" + << " specified with the --namespace-regex option." + << endl; + + e << "--reserved-name " << endl + << " Add to the list of names that should not\n" + << " be used as identifiers. The name can optionally\n" + << " be followed by '=' and the replacement name that\n" + << " should be used instead." + << endl; + + e << "--include-with-brackets" << endl + << " Use angle brackets (<>) instead of quotes (\"\") in\n" + << " generated #include directives." + << endl; + + e << "--include-prefix " << endl + << " Add to generated #include directive\n" + << " paths." + << endl; + + e << "--include-regex " << endl + << " Add to the list of regular expressions\n" + << " used to transform #include directive paths." + << endl; + + e << "--include-regex-trace" << endl + << " Trace the process of applying regular expressions\n" + << " specified with the --include-regex option." + << endl; + + e << "--guard-prefix " << endl + << " Add to generated header inclusion guards." + << endl; + + e << "--hxx-suffix " << endl + << " Use instead of the default '.hxx' to\n" + << " construct the name of the header file." + << endl; + + e << "--ixx-suffix " << endl + << " Use instead of the default '.ixx' to\n" + << " construct the name of the inline file." + << endl; + + e << "--cxx-suffix " << endl + << " Use instead of the default '.cxx' to\n" + << " construct the name of the source file." + << endl; + + e << "--hxx-regex " << endl + << " Use to construct the name of the header\n" + << " file." + << endl; + + e << "--ixx-regex " << endl + << " Use to construct the name of the inline\n" + << " file." + << endl; + + e << "--cxx-regex " << endl + << " Use to construct the name of the source\n" + << " file." + << endl; + + + // Prologues. + // + e << "--hxx-prologue " << endl + << " Insert at the beginning of the header file." + << endl; + + e << "--ixx-prologue " << endl + << " Insert at the beginning of the inline file." + << endl; + + e << "--cxx-prologue " << endl + << " Insert at the beginning of the source file." + << endl; + + e << "--prologue " << endl + << " Insert at the beginning of each generated\n" + << " file for which there is no file-specific prologue." + << endl; + + + // Epilogues. + // + e << "--hxx-epilogue " << endl + << " Insert at the end of the header file." + << endl; + + e << "--ixx-epilogue " << endl + << " Insert at the end of the inline file." + << endl; + + e << "--cxx-epilogue " << endl + << " Insert at the end of the source file." + << endl; + + e << "--epilogue " << endl + << " Insert at the end of each generated file\n" + << " for which there is no file-specific epilogue." + << endl; + + + // Prologue files. + // + e << "--hxx-prologue-file " << endl + << " Insert the content of the at the beginning\n" + << " of the header file." + << endl; + + e << "--ixx-prologue-file " << endl + << " Insert the content of the at the beginning\n" + << " of the inline file." + << endl; + + e << "--cxx-prologue-file " << endl + << " Insert the content of the at the beginning\n" + << " of the source file." + << endl; + + e << "--prologue-file " << endl + << " Insert the content of the at the beginning\n" + << " of each generated file for which there is no file-\n" + << " specific prologue file." + << endl; + + + // Epilogue files. + // + e << "--hxx-epilogue-file " << endl + << " Insert the content of the at the end of\n" + << " the header file." + << endl; + + e << "--ixx-epilogue-file " << endl + << " Insert the content of the at the end of\n" + << " the inline file." + << endl; + + e << "--cxx-epilogue-file " << endl + << " Insert the content of the at the end of\n" + << " the source file." + << endl; + + e << "--epilogue-file " << endl + << " Insert the content of the at the end of\n" + << " each generated file for which there is no file-\n" + << " specific epilogue file." + << endl; + + + // Misc. + // + e << "--show-anonymous" << endl + << " Show elements and attributes that are of anonymous\n" + << " types." + << endl; + + e << "--show-sloc" << endl + << " Show the number of generated physical source lines\n" + << " of code (SLOC)." + << endl; + + e << "--sloc-limit " << endl + << " Check that the number of generated physical source\n" + << " lines of code (SLOC) does not exceed ." + << endl; + + e << "--options-file " << endl + << " Read additional options from . Each option\n" + << " should appear on a separate line optionally\n" + << " followed by space and an argument." + << endl; + + e << "--proprietary-license" << endl + << " Indicate that the generated code is licensed under\n" + << " a proprietary license instead of the GPL." + << endl; + } + + Parser::CLI::OptionsSpec Parser::Generator:: + options_spec () + { + CLI::OptionsSpec spec; + + spec.option ().default_value ("-pskel"); + spec.option ().default_value ("_pskel"); + spec.option ().default_value ("-pimpl"); + spec.option ().default_value ("_pimpl"); + + spec.option ().default_value (".hxx"); + spec.option ().default_value (".ixx"); + spec.option ().default_value (".cxx"); + + return spec; + } + + + namespace + { + template + Void + open (S& ifs, NarrowString const& path) + { + try + { + Path fs_path (path, boost::filesystem::native); + ifs.open (fs_path, std::ios_base::in | std::ios_base::binary); + + if (!ifs.is_open ()) + { + wcerr << path.c_str () << ": error: unable to open in read mode" + << endl; + + throw Parser::Generator::Failed (); + } + } + catch (InvalidPath const&) + { + wcerr << "error: '" << path.c_str () << "' is not a valid " + << "filesystem path" << endl; + + throw Parser::Generator::Failed (); + } + } + + Void + append (WideOutputFileStream& os, + NarrowString const& path, + WideInputFileStream& default_is) + { + using std::ios_base; + + if (path) + { + WideInputFileStream is; + open (is, path); + os << is.rdbuf (); + } + else if (default_is.is_open ()) + { + os << default_is.rdbuf (); + default_is.seekg (0, ios_base::beg); + } + } + + Void + append (WideOutputFileStream& os, + Cult::Containers::Vector const& primary, + Cult::Containers::Vector const& def) + { + Cult::Containers::Vector const& v ( + primary.empty () ? def : primary); + + for (Containers::Vector::ConstIterator + i (v.begin ()), e (v.end ()); i != e; ++i) + { + os << i->c_str () << endl; + } + } + } + + + UnsignedLong Parser::Generator:: + generate (Parser::CLI::Options const& ops, + Schema& schema, + Path const& file_path, + TypeMap::Namespaces& type_map, + Boolean gen_driver, + const WarningSet& disabled_warnings, + FileList& file_list, + AutoUnlinks& unlinks) + { + using std::ios_base; + namespace Indentation = BackendElements::Indentation; + + typedef BackendElements::Regex::Expression Regex; + + try + { + Boolean generate_xml_schema (ops.value ()); + + // We could be compiling several schemas at once in which case + // handling of the --generate-xml-schema option gets tricky: we + // will need to rely on the presence of the --extern-xml-schema + // to tell us which (fake) schema file corresponds to XML Schema. + // + if (generate_xml_schema) + { + if (NarrowString name = ops.value ()) + { + if (file_path.native_file_string () != name) + generate_xml_schema = false; + } + } + + Boolean impl (!generate_xml_schema && + (ops.value () || + ops.value ())); + + Boolean driver (gen_driver && !generate_xml_schema && + ops.value ()); + + + // Evaluate the graph for possibility of generating something useful. + // + { + Validator validator; + if (!validator.validate ( + ops, schema, file_path, driver, disabled_warnings)) + throw Failed (); + } + + // Process names. + // + { + NameProcessor proc; + proc.process (ops, schema, file_path); + } + + Boolean validation (!ops.value ()); + + // Compute state machine info. + // + if (validation) + { + StateProcessor proc; + proc.process (schema, file_path); + } + + // Read-in type maps. + // + { + using namespace TypeMap; + typedef Containers::Vector Files; + + Files const& files (ops.value ()); + + for (Files::ConstIterator f (files.begin ()); f != files.end (); ++f ) + { + NarrowInputFileStream ifs; + open (ifs, *f); + + Lexer l (ifs, *f); + TypeMap::Parser p (l, *f); + + if (!p.parse (type_map)) + throw Failed (); + } + + // Add the built-in mappings at the end. + // + String xns; + { + Context ctx (std::wcerr, schema, ops, 0, 0, 0); + xns = ctx.xs_ns_name (); + } + + if (ops.value ()) + { + TypeMap::Namespace xsd_std ("http://www\\.w3\\.org/2001/XMLSchema"); + + String qname (xns + L"::qname*"); + String string_seq (xns + L"::string_sequence*"); + + xsd_std.types_push_back ("string", "char*", "char*"); + xsd_std.types_push_back ("normalizedString", "char*", "char*"); + xsd_std.types_push_back ("token", "char*", "char*"); + xsd_std.types_push_back ("Name", "char*", "char*"); + xsd_std.types_push_back ("NMTOKEN", "char*", "char*"); + xsd_std.types_push_back ("NMTOKENS", string_seq, string_seq); + xsd_std.types_push_back ("NCName", "char*", "char*"); + + xsd_std.types_push_back ("ID", "char*", "char*"); + xsd_std.types_push_back ("IDREF", "char*", "char*"); + xsd_std.types_push_back ("IDREFS", string_seq, string_seq); + + xsd_std.types_push_back ("language", "char*", "char*"); + xsd_std.types_push_back ("anyURI", "char*", "char*"); + xsd_std.types_push_back ("QName", qname, qname); + + type_map.push_back (xsd_std); + } + else + { + TypeMap::Namespace xsd_std ("http://www\\.w3\\.org/2001/XMLSchema"); + + String qname (xns + L"::qname"); + String string_seq (xns + L"::string_sequence*"); + + xsd_std.types_push_back ("string", "::std::string"); + xsd_std.types_push_back ("normalizedString", "::std::string"); + xsd_std.types_push_back ("token", "::std::string"); + xsd_std.types_push_back ("Name", "::std::string"); + xsd_std.types_push_back ("NMTOKEN", "::std::string"); + xsd_std.types_push_back ("NMTOKENS", string_seq, string_seq); + xsd_std.types_push_back ("NCName", "::std::string"); + + xsd_std.types_push_back ("ID", "::std::string"); + xsd_std.types_push_back ("IDREF", "::std::string"); + xsd_std.types_push_back ("IDREFS", string_seq, string_seq); + + xsd_std.types_push_back ("language", "::std::string"); + xsd_std.types_push_back ("anyURI", "::std::string"); + xsd_std.types_push_back ("QName", qname); + + type_map.push_back (xsd_std); + } + + String buffer (xns + L"::buffer*"); + + TypeMap::Namespace xsd ("http://www\\.w3\\.org/2001/XMLSchema"); + + xsd.types_push_back ("boolean", "bool", "bool"); + + xsd.types_push_back ("byte", "signed char", "signed char"); + xsd.types_push_back ("unsignedByte", "unsigned char", "unsigned char"); + + xsd.types_push_back ("short", "short", "short"); + xsd.types_push_back ("unsignedShort", "unsigned short", "unsigned short"); + + xsd.types_push_back ("int", "int", "int"); + xsd.types_push_back ("unsignedInt", "unsigned int", "unsigned int"); + + if (ops.value ()) + { + xsd.types_push_back ("long", "long", "long"); + xsd.types_push_back ("unsignedLong", "unsigned long", "unsigned long"); + } + else + { + xsd.types_push_back ("long", "long long", "long long"); + xsd.types_push_back ("unsignedLong", "unsigned long long", "unsigned long long"); + } + + xsd.types_push_back ("integer", "long", "long"); + + xsd.types_push_back ("negativeInteger", "long", "long"); + xsd.types_push_back ("nonPositiveInteger", "long", "long"); + + xsd.types_push_back ("positiveInteger", "unsigned long", "unsigned long"); + xsd.types_push_back ("nonNegativeInteger", "unsigned long", "unsigned long"); + + xsd.types_push_back ("float", "float", "float"); + xsd.types_push_back ("double", "double", "double"); + xsd.types_push_back ("decimal", "double", "double"); + + xsd.types_push_back ("base64Binary", buffer, buffer); + xsd.types_push_back ("hexBinary", buffer, buffer); + + xsd.types_push_back ("gDay", xns + L"::gday"); + xsd.types_push_back ("gMonth", xns + L"::gmonth"); + xsd.types_push_back ("gYear", xns + L"::gyear"); + xsd.types_push_back ("gMonthDay", xns + L"::gmonth_day"); + xsd.types_push_back ("gYearMonth", xns + L"::gyear_month"); + xsd.types_push_back ("date", xns + L"::date"); + xsd.types_push_back ("time", xns + L"::time"); + xsd.types_push_back ("dateTime", xns + L"::date_time"); + xsd.types_push_back ("duration", xns + L"::duration"); + + type_map.push_back (xsd); + + // Everyhting else maps to void. + // + TypeMap::Namespace rest (".*"); + rest.types_push_back (".*", "void", "void"); + type_map.push_back (rest); + } + + // Process types. + // + { + TypeProcessor proc; + proc.process (ops, schema, type_map); + } + + // Generate code. + // + Boolean inline_ (ops.value () && + !generate_xml_schema); + + Boolean source (!generate_xml_schema); + + NarrowString name (file_path.leaf ()); + NarrowString skel_suffix (ops.value ()); + NarrowString impl_suffix (ops.value ()); + + NarrowString hxx_suffix (ops.value ()); + NarrowString ixx_suffix (ops.value ()); + NarrowString cxx_suffix (ops.value ()); + + Regex hxx_expr ( + ops.value ().empty () + ? "#^(.+?)(\\.[^./\\\\]+)?$#$1" + skel_suffix + hxx_suffix + "#" + : ops.value ()); + + Regex ixx_expr ( + ops.value ().empty () + ? "#^(.+?)(\\.[^./\\\\]+)?$#$1" + skel_suffix + ixx_suffix + "#" + : ops.value ()); + + Regex cxx_expr ( + ops.value ().empty () + ? "#^(.+?)(\\.[^./\\\\]+)?$#$1" + skel_suffix + cxx_suffix + "#" + : ops.value ()); + + Regex hxx_impl_expr; + Regex cxx_impl_expr; + Regex cxx_driver_expr; + + if (impl || driver) + { + hxx_impl_expr = + "#^(.+?)(\\.[^./\\\\]+)?$#$1" + impl_suffix + hxx_suffix + "#"; + + cxx_impl_expr = + "#^(.+?)(\\.[^./\\\\]+)?$#$1" + impl_suffix + cxx_suffix + "#"; + + cxx_driver_expr = + "#^(.+?)(\\.[^./\\\\]+)?$#$1-pdriver" + cxx_suffix + "#"; + } + + if (!hxx_expr.match (name)) + { + wcerr << "error: header expression '" << + hxx_expr.pattern () << "' does not match '" << + name.c_str () << "'" << endl; + throw Failed (); + } + + if (inline_ && !ixx_expr.match (name)) + { + wcerr << "error: inline expression '" << + ixx_expr.pattern () << "' does not match '" << + name.c_str () << "'" << endl; + throw Failed (); + } + + if (source && !cxx_expr.match (name)) + { + wcerr << "error: source expression '" << + cxx_expr.pattern () << "' does not match '" << + name.c_str () << "'" << endl; + throw Failed (); + } + + if (impl || driver) + { + if (!hxx_impl_expr.match (name)) + { + wcerr << "error: implementation header expression '" << + hxx_impl_expr.pattern () << "' does not match '" << + name.c_str () << "'" << endl; + throw Failed (); + } + + if (!cxx_impl_expr.match (name)) + { + wcerr << "error: implementation source expression '" << + cxx_impl_expr.pattern () << "' does not match '" << + name.c_str () << "'" << endl; + throw Failed (); + } + + if (!cxx_driver_expr.match (name)) + { + wcerr << "error: driver source expression '" << + cxx_driver_expr.pattern () << "' does not match '" << + name.c_str () << "'" << endl; + throw Failed (); + } + } + + NarrowString hxx_name (hxx_expr.merge (name)); + NarrowString ixx_name (inline_ ? ixx_expr.merge (name) : NarrowString ()); + NarrowString cxx_name (source ? cxx_expr.merge (name) : NarrowString ()); + + NarrowString hxx_impl_name; + NarrowString cxx_impl_name; + NarrowString cxx_driver_name; + + if (impl || driver) + { + hxx_impl_name = hxx_impl_expr.merge (name); + cxx_impl_name = cxx_impl_expr.merge (name); + cxx_driver_name = cxx_driver_expr.merge (name); + } + + Path hxx_path (hxx_name, boost::filesystem::native); + Path ixx_path (ixx_name, boost::filesystem::native); + Path cxx_path (cxx_name, boost::filesystem::native); + + Path hxx_impl_path; + Path cxx_impl_path; + Path cxx_driver_path; + + if (impl || driver) + { + hxx_impl_path = Path (hxx_impl_name, boost::filesystem::native); + cxx_impl_path = Path (cxx_impl_name, boost::filesystem::native); + cxx_driver_path = Path (cxx_driver_name, boost::filesystem::native); + } + + if (NarrowString dir = ops.value ()) + { + try + { + Path path (dir, boost::filesystem::native); + + hxx_path = path / hxx_path; + ixx_path = path / ixx_path; + cxx_path = path / cxx_path; + + if (impl || driver) + { + hxx_impl_path = path / hxx_impl_path; + cxx_impl_path = path / cxx_impl_path; + cxx_driver_path = path /cxx_driver_path; + } + + } + catch (InvalidPath const&) + { + wcerr << dir.c_str () << ": error: invalid path" << endl; + throw Failed (); + } + } + + // Open the impl files first so that if open fails, the skel files + // are not deleted. + // + WideOutputFileStream hxx_impl; + WideOutputFileStream cxx_impl; + WideOutputFileStream cxx_driver; + + if (impl) + { + if (!ops.value ()) + { + WideInputFileStream tmp (hxx_impl_path, ios_base::in); + + if (tmp.is_open ()) + { + wcerr << hxx_impl_path << ": error: cowardly refusing to " << + "overwrite an existing file" << endl; + throw Failed (); + } + + tmp.close (); + } + + hxx_impl.open (hxx_impl_path, ios_base::out); + + if (!hxx_impl.is_open ()) + { + wcerr << hxx_impl_path << ": error: unable to open in write mode" + << endl; + throw Failed (); + } + + unlinks.add (hxx_impl_path); + file_list.push_back (hxx_impl_path.native_file_string ()); + + if (!ops.value ()) + { + WideInputFileStream tmp (cxx_impl_path, ios_base::in); + + if (tmp.is_open ()) + { + wcerr << cxx_impl_path << ": error: cowardly refusing to " << + "overwrite an existing file" << endl; + throw Failed (); + } + + tmp.close (); + } + + cxx_impl.open (cxx_impl_path, ios_base::out); + + if (!cxx_impl.is_open ()) + { + wcerr << cxx_impl_path << ": error: unable to open in write mode" + << endl; + throw Failed (); + } + + unlinks.add (cxx_impl_path); + file_list.push_back (cxx_impl_path.native_file_string ()); + } + + if (driver) + { + if (!ops.value ()) + { + WideInputFileStream tmp (cxx_driver_path, ios_base::in); + + if (tmp.is_open ()) + { + wcerr << cxx_driver_path << ": error: cowardly refusing to " << + "overwrite an existing file" << endl; + throw Failed (); + } + + tmp.close (); + } + + cxx_driver.open (cxx_driver_path, ios_base::out); + + if (!cxx_driver.is_open ()) + { + wcerr << cxx_driver_path << ": error: unable to open in write " << + "mode" << endl; + throw Failed (); + } + + unlinks.add (cxx_driver_path); + file_list.push_back (cxx_driver_path.native_file_string ()); + } + + // Open the skel files. + // + WideOutputFileStream hxx (hxx_path, ios_base::out); + WideOutputFileStream ixx; + WideOutputFileStream cxx; + + if (!hxx.is_open ()) + { + wcerr << hxx_path << ": error: unable to open in write mode" << endl; + throw Failed (); + } + + unlinks.add (hxx_path); + file_list.push_back (hxx_path.native_file_string ()); + + if (inline_) + { + ixx.open (ixx_path, ios_base::out); + + if (!ixx.is_open ()) + { + wcerr << ixx_path << ": error: unable to open in write mode" << endl; + throw Failed (); + } + + unlinks.add (ixx_path); + file_list.push_back (ixx_path.native_file_string ()); + } + + if (source) + { + cxx.open (cxx_path, ios_base::out); + + if (!cxx.is_open ()) + { + wcerr << cxx_path << ": error: unable to open in write mode" << endl; + throw Failed (); + } + + unlinks.add (cxx_path); + file_list.push_back (cxx_path.native_file_string ()); + } + + // Print copyright and license. + // + Char const* copyright ( + ops.value () + ? copyright_proprietary + : copyright_gpl); + + hxx << copyright; + + if (inline_) + ixx << copyright; + + if (source) + cxx << copyright; + + if (impl) + { + hxx_impl << copyright_impl; + cxx_impl << copyright_impl; + } + + if (driver) + cxx_driver << copyright_impl; + + // Prologue. + // + WideInputFileStream prologue; + { + NarrowString name (ops.value ()); + + if (name) + open (prologue, name); + } + + // Epilogue. + // + WideInputFileStream epilogue; + { + NarrowString name (ops.value ()); + + if (name) + open (epilogue, name); + } + + + // SLOC counter. + // + UnsignedLong sloc (0); + Boolean show_sloc (ops.value ()); + + + // + // + Regex guard_expr ("/([a-z])([A-Z])/$1_$2/"); // Split words. + + NarrowString guard_prefix (ops.value ()); + + if (!guard_prefix) + guard_prefix = file_path.branch_path ().native_directory_string (); + + if (guard_prefix) + guard_prefix += '_'; + + + // HXX + // + { + Context ctx (hxx, schema, ops, &hxx_expr, &ixx_expr, &hxx_impl_expr); + + Indentation::Clip hxx_sloc (hxx); + + String guard (guard_expr.merge (guard_prefix + hxx_name)); + guard = ctx.escape (guard); // Make it a C++ id. + std::transform (guard.begin (), guard.end(), guard.begin (), upcase); + + hxx << "#ifndef " << guard << endl + << "#define " << guard << endl + << endl; + + // Copy prologue. + // + hxx << "// Begin prologue." << endl + << "//" << endl; + + append ( + hxx, ops.value (), ops.value ()); + append (hxx, ops.value (), prologue); + + hxx << "//" << endl + << "// End prologue." << endl + << endl; + + { + // Version check. + // + hxx << "#include " << endl + << endl + << "#if (XSDE_INT_VERSION != " << XSDE_INT_VERSION << "L)" << endl + << "#error XSD/e runtime version mismatch" << endl + << "#endif" << endl + << endl; + + // Runtime/generated code compatibility checks. + // + + hxx << "#include " << endl + << endl; + + if (ops.value ()) + { + hxx << "#ifdef XSDE_STL" << endl + << "#error the XSD/e runtime uses STL while the " << + "generated code does not (reconfigure the runtime or " << + "remove --no-stl)" << endl + << "#endif" << endl + << endl; + } + else + { + hxx << "#ifndef XSDE_STL" << endl + << "#error the generated code uses STL while the " << + "XSD/e runtime does not (reconfigure the runtime or " << + "add --no-stl)" << endl + << "#endif" << endl + << endl; + } + + if (ops.value ()) + { + hxx << "#ifdef XSDE_IOSTREAM" << endl + << "#error the XSD/e runtime uses iostream while the " << + "generated code does not (reconfigure the runtime or " << + "remove --no-iostream)" << endl + << "#endif" << endl + << endl; + } + else + { + hxx << "#ifndef XSDE_IOSTREAM" << endl + << "#error the generated code uses iostream while the " << + "XSD/e runtime does not (reconfigure the runtime or " << + "add --no-iostream)" << endl + << "#endif" << endl + << endl; + } + + if (ops.value ()) + { + hxx << "#ifdef XSDE_EXCEPTIONS" << endl + << "#error the XSD/e runtime uses exceptions while the " << + "generated code does not (reconfigure the runtime or " << + "remove --no-exceptions)" << endl + << "#endif" << endl + << endl; + } + else + { + hxx << "#ifndef XSDE_EXCEPTIONS" << endl + << "#error the generated code uses exceptions while the " << + "XSD/e runtime does not (reconfigure the runtime or " << + "add --no-exceptions)" << endl + << "#endif" << endl + << endl; + } + + if (ops.value ()) + { + hxx << "#ifdef XSDE_LONGLONG" << endl + << "#error the XSD/e runtime uses long long while the " << + "generated code does not (reconfigure the runtime or " << + "remove --no-long-long)" << endl + << "#endif" << endl + << endl; + } + else + { + hxx << "#ifndef XSDE_LONGLONG" << endl + << "#error the generated code uses long long while the " << + "XSD/e runtime does not (reconfigure the runtime or " << + "add --no-long-long)" << endl + << "#endif" << endl + << endl; + } + + if (ops.value ()) + { + hxx << "#ifdef XSDE_PARSER_VALIDATION" << endl + << "#error the XSD/e runtime uses validation while the " << + "generated code does not (reconfigure the runtime or " << + "remove --suppress-validation)" << endl + << "#endif" << endl + << endl; + } + else + { + hxx << "#ifndef XSDE_PARSER_VALIDATION" << endl + << "#error the generated code uses validation while the " << + "XSD/e runtime does not (reconfigure the runtime or " << + "add --suppress-validation)" << endl + << "#endif" << endl + << endl; + } + + if (ops.value () || + ops.value ()) + { + hxx << "#ifndef XSDE_POLYMORPHIC" << endl + << "#error the generated code expects XSD/e runtime with " << + "polymorphism support (reconfigure the runtime or remove " << + "--generate-polymorphic/--runtime-polymorphic)" << endl + << "#endif" << endl + << endl; + } + else + { + hxx << "#ifdef XSDE_POLYMORPHIC" << endl + << "#error the generated code expects XSD/e runtime " << + "without polymorphism support (reconfigure the runtime or " << + "add --generate-polymorphic/--runtime-polymorphic)" << endl + << "#endif" << endl + << endl; + } + + if (ops.value ()) + { + hxx << "#ifndef XSDE_REUSE_STYLE_MIXIN" << endl + << "#error the generated code uses the mixin reuse style " << + "while the XSD/e runtime does not (reconfigure the runtime " << + "or remove --reuse-style-mixin)" << endl + << "#endif" << endl + << endl; + } + else if (ops.value ()) + { + hxx << "#ifndef XSDE_REUSE_STYLE_NONE" << endl + << "#error the generated code does not provide support " << + "for parser reuse while the XSD/e runtime does (reconfigure " << + "the runtime or remove --reuse-style-none)" << endl + << "#endif" << endl + << endl; + } + else + { + hxx << "#ifndef XSDE_REUSE_STYLE_TIEIN" << endl + << "#error the generated code uses the tiein reuse style " << + "while the XSD/e runtime does not (reconfigure the runtime " << + "or add --reuse-style-mixin or --reuse-style-none)" << endl + << "#endif" << endl + << endl; + } + + // + // + + hxx << "#include " << endl + << endl; + + // Set auto-indentation. + // + Indentation::Clip hxx_clip (hxx); + + // Generate. + // + if (!generate_xml_schema) + generate_parser_forward (ctx); + + generate_parser_header (ctx, generate_xml_schema); + + if (inline_) + hxx << "#include " << ctx.process_include_path (ixx_name) << endl + << endl; + + hxx << "#include " << endl + << endl; + } + + // Copy epilogue. + // + hxx << "// Begin epilogue." << endl + << "//" << endl; + + append (hxx, ops.value (), epilogue); + append ( + hxx, ops.value (), ops.value ()); + + hxx << "//" << endl + << "// End epilogue." << endl + << endl; + + hxx << "#endif // " << guard << endl; + + if (show_sloc) + { + wcerr << hxx_path << ": " + << hxx_sloc.buffer ().count () << endl; + + sloc += hxx_sloc.buffer ().count (); + } + } + + + // IXX + // + if (inline_) + { + Context ctx (ixx, schema, ops, &hxx_expr, &ixx_expr, &hxx_impl_expr); + + Indentation::Clip ixx_sloc (ixx); + + + // Copy prologue. + // + ixx << "// Begin prologue." << endl + << "//" << endl; + + append ( + ixx, ops.value (), ops.value ()); + append (ixx, ops.value (), prologue); + + ixx << "//" << endl + << "// End prologue." << endl + << endl; + + { + // Set auto-indentation. + // + Indentation::Clip ixx_clip (ixx); + + + // Generate. + // + generate_parser_inline (ctx); + } + + // Copy epilogue. + // + ixx << "// Begin epilogue." << endl + << "//" << endl; + + append (ixx, ops.value (), epilogue); + append ( + ixx, ops.value (), ops.value ()); + + ixx << "//" << endl + << "// End epilogue." << endl + << endl; + + if (show_sloc) + { + wcerr << ixx_path << ": " + << ixx_sloc.buffer ().count () << endl; + + sloc += ixx_sloc.buffer ().count (); + } + } + + + // CXX + // + if (source) + { + Context ctx (cxx, schema, ops, &hxx_expr, &ixx_expr, &hxx_impl_expr); + + Indentation::Clip cxx_sloc (cxx); + + // Copy prologue. + // + cxx << "// Begin prologue." << endl + << "//" << endl; + + append ( + cxx, ops.value (), ops.value ()); + append (cxx, ops.value (), prologue); + + cxx << "//" << endl + << "// End prologue." << endl + << endl; + + { + // Set auto-indentation. + // + Indentation::Clip cxx_clip (cxx); + + cxx << "#include " << ctx.process_include_path (hxx_name) << endl + << endl; + + if (!inline_) + generate_parser_inline (ctx); + + generate_parser_source (ctx); + + if (validation) + { + generate_element_validation_source (ctx); + generate_attribute_validation_source (ctx); + generate_characters_validation_source (ctx); + } + } + + // Copy epilogue. + // + cxx << "// Begin epilogue." << endl + << "//" << endl; + + append (cxx, ops.value (), epilogue); + append ( + cxx, ops.value (), ops.value ()); + + cxx << "//" << endl + << "// End epilogue." << endl + << endl; + + if (show_sloc) + { + wcerr << cxx_path << ": " + << cxx_sloc.buffer ().count () << endl; + + sloc += cxx_sloc.buffer ().count (); + } + } + + // HXX impl + // + if (impl) + { + Context ctx (hxx_impl, schema, ops, + &hxx_expr, &ixx_expr, &hxx_impl_expr); + + String guard (guard_expr.merge (guard_prefix + hxx_impl_name)); + guard = ctx.escape (guard); // Make it a C++ id. + std::transform (guard.begin (), guard.end(), guard.begin (), upcase); + + hxx_impl << "#ifndef " << guard << endl + << "#define " << guard << endl + << endl; + + { + // Set auto-indentation. + // + Indentation::Clip clip (hxx_impl); + + hxx_impl << "#include " << ctx.process_include_path (hxx_name) + << endl << endl; + + generate_impl_header (ctx); + } + + hxx_impl << "#endif // " << guard << endl; + } + + // CXX impl + // + if (impl) + { + Context ctx (cxx_impl, schema, ops, + &hxx_expr, &ixx_expr, &hxx_impl_expr); + + // Set auto-indentation. + // + Indentation::Clip clip (cxx_impl); + + cxx_impl << "#include " << ctx.process_include_path (hxx_impl_name) + << endl << endl; + + generate_impl_source (ctx); + } + + // CXX driver + // + if (driver) + { + Context ctx (cxx_driver, schema, ops, + &hxx_expr, &ixx_expr, &hxx_impl_expr); + + // Set auto-indentation. + // + Indentation::Clip clip (cxx_driver); + + cxx_driver << "#include " << ctx.process_include_path (hxx_impl_name) + << endl << endl; + + generate_driver_source (ctx); + } + + return sloc; + } + catch (NoNamespaceMapping const& e) + { + wcerr << e.file () << ":" << e.line () << ":" << e.column () + << ": error: unable to map XML Schema namespace '" << e.ns () + << "' to C++ namespace" << endl; + + wcerr << e.file () << ":" << e.line () << ":" << e.column () + << ": info: use the --namespace-map or --namespace-regex option " + << "to provide custom mapping" << endl; + + throw Failed (); + } + catch (InvalidNamespaceMapping const& e) + { + wcerr << "error: invalid XML to C++ namespace mapping specified: " + << "'" << e.mapping () << "': " << e.reason () << endl; + + throw Failed (); + } + catch (BackendElements::Regex::Format const& e) + { + wcerr << "error: invalid regex: '" << + e.expression ().c_str () << "': " << + e.description ().c_str () << endl; + + throw Failed (); + } + catch (BackendElements::Regex::Format const& e) + { + wcerr << "error: invalid regex: '" << + e.expression () << "': " << e.description () << endl; + + throw Failed (); + } + } +} diff --git a/xsde/cxx/parser/generator.hxx b/xsde/cxx/parser/generator.hxx new file mode 100644 index 0000000..a261eca --- /dev/null +++ b/xsde/cxx/parser/generator.hxx @@ -0,0 +1,56 @@ +// file : xsde/cxx/parser/generator.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef CXX_PARSER_GENERATOR_HXX +#define CXX_PARSER_GENERATOR_HXX + +#include + +#include + +#include +#include + +#include // Path +#include + +#include +#include +#include + +namespace CXX +{ + namespace Parser + { + using namespace Cult::Types; + + class Generator + { + public: + static Void + usage (); + + static CLI::OptionsSpec + options_spec (); + + struct Failed {}; + + static UnsignedLong + generate (CLI::Options const& options, + XSDFrontend::SemanticGraph::Schema&, + XSDFrontend::SemanticGraph::Path const& file, + TypeMap::Namespaces& type_map, + Boolean gen_driver, + const WarningSet& disabled_warnings, + FileList& file_list, + AutoUnlinks& unlinks); + + private: + Generator (); + }; + } +} + +#endif // CXX_PARSER_GENERATOR_HXX diff --git a/xsde/cxx/parser/impl-header.cxx b/xsde/cxx/parser/impl-header.cxx new file mode 100644 index 0000000..5d15ed2 --- /dev/null +++ b/xsde/cxx/parser/impl-header.cxx @@ -0,0 +1,309 @@ +// file : xsde/cxx/parser/impl-header.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include + +#include +#include + +namespace CXX +{ + namespace Parser + { + namespace + { + struct Enumeration: Traversal::Enumeration, Context + { + Enumeration (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& e) + { + String const& name (eimpl (e)); + String const& ret (ret_type (e)); + SemanticGraph::Type& base (e.inherits ().base ()); + + os << "class " << name << ": " << "public " << + (mixin ? "virtual " : "") << ename (e); + + if (mixin) + os << "," << endl + << " public " << fq_name (base, "p:impl"); + + os << "{" + << "public:" << endl; + + if (tiein) + os << name << " ();" + << endl; + + os << "virtual void" << endl + << "pre ();" + << endl + << "virtual " << ret << endl + << post_name (e) << " ();"; + + if (tiein) + os << endl + << "private:" << endl + << fq_name (base, "p:impl") << " base_impl_;"; + + os << "};"; + } + }; + + // + // + struct List: Traversal::List, Context + { + List (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& l) + { + String const& name (eimpl (l)); + SemanticGraph::Type& t (l.argumented ().type ()); + + String item (unclash (ename (l), "item")); + + os << "class " << name << ": public " << + (mixin ? "virtual " : "") << ename (l) + << "{" + << "public:" << endl + << "virtual void" << endl + << "pre ();" + << endl; + + // item + // + String const& arg (arg_type (t)); + + os << "virtual void" << endl + << item; + + if (arg == L"void") + os << " ();"; + else + os << " (" << arg << ");"; + + os << endl; + + // post + // + String const& ret (ret_type (l)); + + os << "virtual " << ret << endl + << post_name (l) << " ();" + << "};"; + } + }; + + struct Union: Traversal::Union, Context + { + Union (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& u) + { + String const& name (eimpl (u)); + String const& ret (ret_type (u)); + + os << "class " << name << ": public " << + (mixin ? "virtual " : "") << ename (u) + << "{" + << "public:" << endl + << "virtual void" << endl + << "pre ();" + << endl + << "virtual void" << endl + << "_characters (const " << string_type << "&);" + << endl + << "virtual " << ret << endl + << post_name (u) << " ();" + << "};"; + } + }; + + + // + // + struct ParticleCallback: Traversal::Element, Context + { + ParticleCallback (Context& c) + : Context (c) + { + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + String const& arg (arg_type (e.type ())); + + os << "virtual void" << endl + << ename (e); + + if (arg == L"void") + os << " ();"; + else + os << " (" << arg << ");"; + + os << endl; + } + }; + + struct AttributeCallback: Traversal::Attribute, Context + { + AttributeCallback (Context& c) + : Context (c) + { + } + + virtual Void + traverse (SemanticGraph::Attribute& a) + { + String const& arg (arg_type (a.type ())); + + os << "virtual void" << endl + << ename (a); + + if (arg == L"void") + os << " ();"; + else + os << " (" << arg << ");"; + + os << endl; + } + }; + + + // + // + struct Complex : Traversal::Complex, Context + { + Complex (Context& c) + : Context (c), + particle_callback_ (c), + attribute_callback_ (c) + { + contains_compositor_callback_ >> compositor_callback_; + compositor_callback_ >> contains_particle_callback_; + contains_particle_callback_ >> compositor_callback_; + contains_particle_callback_ >> particle_callback_; + + names_attribute_callback_ >> attribute_callback_; + } + + virtual Void + traverse (Type& c) + { + Boolean hb (c.inherits_p ()); + Boolean he (has (c)); + Boolean ha (has (c)); + + String const& name (eimpl (c)); + String const& ret (ret_type (c)); + + os << "class " << name << ": public " << + (mixin ? "virtual " : "") << ename (c); + + if (mixin && hb) + os << "," << endl + << " public " << fq_name (c.inherits ().base (), "p:impl"); + + os << "{" + << "public:" << endl; + + if (tiein && hb) + os << name << " ();" + << endl; + + os << "virtual void" << endl + << "pre ();" + << endl; + + // In case of an inheritance-by-restriction, we don't need to + // generate parser callbacks, etc. since they are the same as + // in the base. + // + if (!restriction_p (c)) + { + if (ha) + { + os << "// Attributes." << endl + << "//" << endl; + + names (c, names_attribute_callback_); + } + + if (he) + { + os << "// Elements." << endl + << "//" << endl; + + contains_compositor (c, contains_compositor_callback_); + } + } + + os << "virtual " << ret << endl + << post_name (c) << " ();"; + + if (tiein && hb) + os << endl + << "private:" << endl + << fq_name (c.inherits ().base (), "p:impl") << " base_impl_;"; + + os << "};"; + } + + private: + Traversal::Compositor compositor_callback_; + ParticleCallback particle_callback_; + Traversal::ContainsCompositor contains_compositor_callback_; + Traversal::ContainsParticle contains_particle_callback_; + + AttributeCallback attribute_callback_; + Traversal::Names names_attribute_callback_; + }; + } + + Void + generate_impl_header (Context& ctx) + { + Traversal::Schema schema; + + Traversal::Sources sources; + Includes includes (ctx, Includes::impl_header); + Traversal::Names schema_names; + + Namespace ns (ctx); + Traversal::Names names; + + schema >> includes; + schema >> sources >> schema; + schema >> schema_names >> ns >> names; + + List list (ctx); + Union union_ (ctx); + Complex complex (ctx); + Enumeration enumeration (ctx); + + names >> list; + names >> union_; + names >> complex; + names >> enumeration; + + schema.dispatch (ctx.schema_root); + } + } +} diff --git a/xsde/cxx/parser/impl-header.hxx b/xsde/cxx/parser/impl-header.hxx new file mode 100644 index 0000000..43a7584 --- /dev/null +++ b/xsde/cxx/parser/impl-header.hxx @@ -0,0 +1,22 @@ +// file : xsde/cxx/parser/impl-header.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef CXX_PARSER_IMPL_HEADER_HXX +#define CXX_PARSER_IMPL_HEADER_HXX + +#include + +#include + +namespace CXX +{ + namespace Parser + { + Void + generate_impl_header (Context&); + } +} + +#endif // CXX_PARSER_IMPL_HEADER_HXX diff --git a/xsde/cxx/parser/impl-source.cxx b/xsde/cxx/parser/impl-source.cxx new file mode 100644 index 0000000..6b02425 --- /dev/null +++ b/xsde/cxx/parser/impl-source.cxx @@ -0,0 +1,543 @@ +// file : xsde/cxx/parser/impl-source.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include +#include + +#include +#include + +namespace CXX +{ + namespace Parser + { + namespace + { + struct Enumeration: Traversal::Enumeration, Context + { + Enumeration (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& e) + { + String const& name (eimpl (e)); + String const& ret (ret_type (e)); + SemanticGraph::Type& base (e.inherits ().base ()); + String const& base_ret (ret_type (base)); + + os << "// " << name << endl + << "//" << endl + << endl; + + // c-tor + // + if (tiein) + os << name << "::" << endl + << name << " ()" << endl + << ": " << ename (e) << " (&base_impl_)" + << "{" + << "}"; + + // pre + // + os << "void " << name << "::" << endl + << "pre ()" + << "{" + << "}"; + + // post + // + os << ret << " " << name << "::" << endl + << post_name (e) << " ()" + << "{"; + + if (ret == base_ret) + { + os << (ret != L"void" ? "return " : "") << + post_name (base) << " ();"; + } + else if (ret == L"void") + { + os << arg_type (base) << " v = " << post_name (base) << " ();" + << endl; + + if (options.value ()) + os << "if (!_error ())" + << "{"; + + if (options.value ()) + { + PrintCall t (*this, e.name (), "v"); + t.dispatch (base); + } + else + os << "// TODO" << endl + << "//" << endl; + + { + DeleteCall t (*this, "v"); + t.dispatch (base); + } + + if (options.value ()) + os << "}"; + } + else + { + if (base_ret == L"void") + os << post_name (base) << " ();"; + else + { + os << arg_type (base) << " v = " << post_name (base) << " ();" + << endl; + + if (options.value ()) + os << "if (!_error ())" + << "{"; + + os << "// TODO" << endl + << "//" << endl + << "// return ... ;" << endl; + + { + DeleteCall t (*this, "v"); + t.dispatch (base); + } + + if (options.value ()) + os << "}"; + } + } + + os << "}"; + } + }; + + // + // + struct List: Traversal::List, Context + { + List (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& l) + { + String const& name (eimpl (l)); + SemanticGraph::Type& type (l.argumented ().type ()); + + String item (unclash (ename (l), "item")); + + os << "// " << name << endl + << "//" << endl + << endl; + + // pre + // + os << "void " << name << "::" << endl + << "pre ()" + << "{" + << "}"; + + // item + // + String const& arg (arg_type (type)); + + os << "void " << name << "::" << endl + << item; + + if (arg == L"void") + os << " ()"; + else + os << " (" << arg << " " << item << ")"; + + os << "{"; + + if (arg != L"void") + { + if (options.value ()) + { + PrintCall t (*this, type.name (), item); + t.dispatch (type); + } + else + os << "// TODO" << endl + << "//" << endl; + + { + DeleteCall t (*this, item); + t.dispatch (type); + } + } + + os << "}"; + + // post + // + String const& ret (ret_type (l)); + + os << ret << " " << name << "::" << endl + << post_name (l) << " ()" + << "{"; + + if (ret != L"void") + os << "// TODO" << endl + << "//" << endl + << "// return ... ;" << endl; + + os << "}"; + } + }; + + // + // + struct Union: Traversal::Union, Context + { + Union (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& u) + { + String const& name (eimpl (u)); + + os << "// " << name << endl + << "//" << endl + << endl; + + // pre + // + os << "void " << name << "::" << endl + << "pre ()" + << "{" + << "}"; + + // _characters + // + os << "void " << name << "::" << endl + << "_characters (const " << string_type << "& s)" + << "{"; + + if (options.value ()) + { + if (options.value ()) + os << "if (s.size () != 0)" + << "{" + << "printf (" << strlit (u.name () + L": ") << ");" + << "fwrite (s.data (), s.size (), 1, stdout);" + << "printf (\"\\n\");" + << "}"; + else + os << "std::cout << " << strlit (u.name () + L": ") << + " << s << std::endl;"; + } + else + os << "// TODO" << endl + << "//" << endl; + + os << "}"; + + // post + // + String const& ret (ret_type (u)); + + os << ret << " " << name << "::" << endl + << post_name (u) << " ()" + << "{"; + + if (ret != L"void") + os << "// TODO" << endl + << "//" << endl + << "// return ... ;" << endl; + + os << "}"; + } + }; + + // + // + struct ParticleCallback: Traversal::Element, Context + { + ParticleCallback (Context& c) + : Context (c) + { + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + using SemanticGraph::Complex; + + String const& name (ename (e)); + String const& arg (arg_type (e.type ())); + Complex& c (dynamic_cast (e.scope ())); + + os << "void " << eimpl (c) << "::" << endl + << name; + + if (arg == L"void") + os << " ()"; + else + os << " (" << arg << " " << name << ")"; + + os << "{"; + + if (arg != L"void") + { + if (options.value ()) + { + PrintCall t (*this, e.name (), name); + t.dispatch (e.type ()); + } + else + os << "// TODO" << endl + << "//" << endl; + + { + DeleteCall t (*this, name); + t.dispatch (e.type ()); + } + } + + os << "}"; + } + }; + + struct AttributeCallback: Traversal::Attribute, Context + { + AttributeCallback (Context& c) + : Context (c) + { + } + + virtual Void + traverse (SemanticGraph::Attribute& a) + { + using SemanticGraph::Complex; + + String const& name (ename (a)); + String const& arg (arg_type (a.type ())); + Complex& c (dynamic_cast (a.scope ())); + + os << "void " << eimpl (c) << "::" << endl + << name; + + if (arg == L"void") + os << " ()"; + else + os << " (" << arg << " " << name << ")"; + + os << "{"; + + if (arg != L"void") + { + if (options.value ()) + { + PrintCall t (*this, a.name (), name); + t.dispatch (a.type ()); + } + else + os << "// TODO" << endl + << "//" << endl; + + { + DeleteCall t (*this, name); + t.dispatch (a.type ()); + } + } + + os << "}"; + } + }; + + // + // + struct Complex : Traversal::Complex, Context + { + Complex (Context& c) + : Context (c), + particle_callback_ (c), + attribute_callback_ (c) + { + contains_compositor_callback_ >> compositor_callback_; + compositor_callback_ >> contains_particle_callback_; + contains_particle_callback_ >> compositor_callback_; + contains_particle_callback_ >> particle_callback_; + + names_attribute_callback_ >> attribute_callback_; + } + + virtual Void + traverse (Type& c) + { + Boolean hb (c.inherits_p ()); + + String const& name (eimpl (c)); + String const& ret (ret_type (c)); + + os << "// " << name << endl + << "//" << endl + << endl; + + // c-tor + // + if (tiein && hb) + os << name << "::" << endl + << name << " ()" << endl + << ": " << ename (c) << " (&base_impl_)" + << "{" + << "}"; + + // pre + // + os << "void " << name << "::" << endl + << "pre ()" + << "{" + << "}"; + + // Parser callbacks. + // + if (!restriction_p (c)) + { + names (c, names_attribute_callback_); + contains_compositor (c, contains_compositor_callback_); + } + + // post + // + os << ret << " " << name << "::" << endl + << post_name (c) << " ()" + << "{"; + + if (hb) + { + SemanticGraph::Type& base (c.inherits ().base ()); + String const& base_ret (ret_type (base)); + + if (ret == base_ret) + { + os << (ret != L"void" ? "return " : "") << + post_name (base) << " ();"; + } + else if (ret == L"void") + { + os << arg_type (base) << " v = " << post_name (base) << " ();" + << endl; + + if (options.value ()) + os << "if (!_error ())" + << "{"; + + if (options.value ()) + { + PrintCall t (*this, c.name (), "v"); + t.dispatch (base); + } + else + os << "// TODO" << endl + << "//" << endl; + + { + DeleteCall t (*this, "v"); + t.dispatch (base); + } + + if (options.value ()) + os << "}"; + } + else + { + if (base_ret == L"void") + os << post_name (base) << " ();"; + else + { + os << arg_type (base) << " v = " << post_name (base) << " ();" + << endl; + + if (options.value ()) + os << "if (!_error ())" + << "{"; + + os << "// TODO" << endl + << "//" << endl + << "// return ... ;" << endl; + + { + DeleteCall t (*this, "v"); + t.dispatch (base); + } + + if (options.value ()) + os << "}"; + } + } + } + else + { + if (ret != L"void") + os << "// TODO" << endl + << "//" << endl + << "// return ... ;" << endl; + } + + os << "}"; + } + + private: + Traversal::Compositor compositor_callback_; + ParticleCallback particle_callback_; + Traversal::ContainsCompositor contains_compositor_callback_; + Traversal::ContainsParticle contains_particle_callback_; + + AttributeCallback attribute_callback_; + Traversal::Names names_attribute_callback_; + }; + } + + Void + generate_impl_source (Context& ctx) + { + if (ctx.options.value ()) + { + if (ctx.options.value ()) + ctx.os << "#include " << endl + << endl; + else + ctx.os << "#include " << endl + << endl; + } + + Traversal::Schema schema; + + Traversal::Sources sources; + Traversal::Names schema_names; + + Namespace ns (ctx); + Traversal::Names names; + + schema >> sources >> schema; + schema >> schema_names >> ns >> names; + + List list (ctx); + Union union_ (ctx); + Complex complex (ctx); + Enumeration enumeration (ctx); + + names >> list; + names >> union_; + names >> complex; + names >> enumeration; + + schema.dispatch (ctx.schema_root); + } + } +} diff --git a/xsde/cxx/parser/impl-source.hxx b/xsde/cxx/parser/impl-source.hxx new file mode 100644 index 0000000..98c7961 --- /dev/null +++ b/xsde/cxx/parser/impl-source.hxx @@ -0,0 +1,22 @@ +// file : xsde/cxx/parser/impl-source.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef CXX_PARSER_IMPL_SOURCE_HXX +#define CXX_PARSER_IMPL_SOURCE_HXX + +#include + +#include + +namespace CXX +{ + namespace Parser + { + Void + generate_impl_source (Context&); + } +} + +#endif // CXX_PARSER_IMPL_SOURCE_HXX diff --git a/xsde/cxx/parser/name-processor.cxx b/xsde/cxx/parser/name-processor.cxx new file mode 100644 index 0000000..bc0e7a5 --- /dev/null +++ b/xsde/cxx/parser/name-processor.cxx @@ -0,0 +1,1326 @@ +// file : xsde/cxx/parser/name-processor.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2006-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include +#include + +#include +#include + +#include + +#include +#include + +namespace CXX +{ + namespace Parser + { + namespace + { + // + // + typedef Cult::Containers::Set NameSet; + + class Context: public CXX::Context + { + public: + Context (CLI::Options const& ops, + SemanticGraph::Schema& root, + SemanticGraph::Path const& file) + : CXX::Context (std::wcerr, + root, + "p:name", + "char", + ops.value (), + ops.value (), + "", // export symbol + ops.value (), + ops.value (), + ops.value (), + ops.value (), + ops.value (), + ops.value (), + ops.value ()), + schema_path_ (file), + skel_suffix_ (ops.value ()), + impl_suffix_ (ops.value ()), + schema (root), + schema_path (schema_path_), + impl (ops.value () || + ops.value () || + ops.value ()), + tiein (!ops.value () && + !ops.value ()), + skel_suffix (skel_suffix_), + impl_suffix (impl_suffix_), + global_type_names (global_type_names_), + validation (!ops.value ()), + polymorphic (ops.value ()) + { + } + + protected: + Context (Context& c) + : CXX::Context (c), + schema (c.schema), + schema_path (c.schema_path), + impl (c.impl), + tiein (c.tiein), + skel_suffix (c.skel_suffix), + impl_suffix (c.impl_suffix), + global_type_names (c.global_type_names), + validation (c.validation), + polymorphic (c.polymorphic) + { + } + + public: + String + find_name (String const& n, String const& suffix, NameSet& set) + { + String name (escape (n + suffix)); + + for (UnsignedLong i (1); set.find (name) != set.end (); ++i) + { + std::wostringstream os; + os << i; + name = escape (n + os.str () + suffix); + } + + set.insert (name); + return name; + } + + String + find_name (String const& n, NameSet& set) + { + return find_name (n, L"", set); + } + + private: + SemanticGraph::Path const schema_path_; + String const skel_suffix_; + String const impl_suffix_; + + Cult::Containers::Map global_type_names_; + + public: + SemanticGraph::Schema& schema; + SemanticGraph::Path const& schema_path; + Boolean const impl; + Boolean const tiein; + String const& skel_suffix; + String const& impl_suffix; + + Cult::Containers::Map& global_type_names; + + Boolean validation; + Boolean polymorphic; + }; + + // Primary names. + // + struct PrimaryParticle: Traversal::Element, Context + { + PrimaryParticle (Context& c, NameSet& set, Boolean restriction) + : Context (c), set_ (set), restriction_ (restriction) + { + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + using SemanticGraph::Element; + + SemanticGraph::Context& ec (e.context ()); + + if (!restriction_) + { + ec.set ("p:name", find_name (e.name (), set_)); + } + else + { + Element* prot ( + ec.get ("xsd-frontend-restriction-correspondence")); + + ec.set ("p:name", prot->context ().get ("p:name")); + } + } + + private: + NameSet& set_; + Boolean restriction_; + }; + + struct PrimaryAttribute: Traversal::Attribute, Context + { + PrimaryAttribute (Context& c, NameSet& set, Boolean restriction) + : Context (c), set_ (set), restriction_ (restriction) + { + } + + virtual Void + traverse (SemanticGraph::Attribute& a) + { + using SemanticGraph::Attribute; + + SemanticGraph::Context& ac (a.context ()); + + if (!restriction_) + { + ac.set ("p:name", find_name (a.name (), set_)); + } + else + { + Attribute* prot ( + ac.get ("xsd-frontend-restriction-correspondence")); + + ac.set ("p:name", prot->context ().get ("p:name")); + } + } + + private: + NameSet& set_; + Boolean restriction_; + }; + + + // + // Secondary names. + // + + struct ParticleTag: Traversal::Element, + Traversal::Any, + Traversal::Choice, + Traversal::Sequence, + Context + { + ParticleTag (Context& c, NameSet& set, Boolean restriction) + : Context (c), set_ (set), restriction_ (restriction) + { + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + using SemanticGraph::Element; + + SemanticGraph::Context& ec (e.context ()); + + if (!restriction_) + { + String const base (ec.get ("p:name")); + ec.set ("p:tag", find_name (base, L"_tag", set_)); + } + else + { + Element* prot = ec.get ( + "xsd-frontend-restriction-correspondence"); + + ec.set ("p:tag", prot->context ().get ("p:tag")); + } + } + + virtual Void + traverse (SemanticGraph::Any& a) + { + using SemanticGraph::Any; + + SemanticGraph::Context& ac (a.context ()); + + if (!restriction_) + ac.set ("p:tag", find_name (L"any", L"_tag", set_)); + else + { + Any* prot = ac.get ( + "xsd-frontend-restriction-correspondence"); + + ac.set ("p:tag", prot->context ().get ("p:tag")); + } + } + + virtual Void + traverse (SemanticGraph::Choice& c) + { + using SemanticGraph::Compositor; + + SemanticGraph::Context& cc (c.context ()); + + if (!restriction_) + { + cc.set ("p:tag", find_name (L"choice", L"_tag", set_)); + } + else + { + Compositor* prot = cc.get ( + "xsd-frontend-restriction-correspondence"); + + cc.set ("p:tag", prot->context ().get ("p:tag")); + } + } + + virtual Void + traverse (SemanticGraph::Sequence& s) + { + using SemanticGraph::Compositor; + + SemanticGraph::Context& sc (s.context ()); + + if (!restriction_) + { + sc.set ("p:tag", find_name (L"sequence", L"_tag", set_)); + } + else + { + Compositor* prot = sc.get ( + "xsd-frontend-restriction-correspondence"); + + sc.set ("p:tag", prot->context ().get ("p:tag")); + } + } + + private: + NameSet& set_; + Boolean restriction_; + }; + + struct SecondaryCompositor: Traversal::All, + Traversal::Choice, + Traversal::Sequence, + Context + { + SecondaryCompositor (Context& c, NameSet& set, Boolean restriction) + : Context (c), + set_ (set), + restriction_ (restriction), + particle_tag_ (c, set, restriction) + { + contain_particle_tag_ >> particle_tag_; + } + + virtual Void + traverse (SemanticGraph::All& a) + { + // For the all compositor, maxOccurs=1 and minOccurs={0,1} + // and it cannot be used in restriction. + // + if (a.min () == 0) + { + a.context ().set ( + "p:present", find_name (L"all", L"_present", set_)); + } + + Traversal::All::traverse (a); + } + + virtual Void + traverse (SemanticGraph::Choice& c) + { + if (c.contains_begin () == c.contains_end ()) + return; + + SemanticGraph::Context& cc (c.context ()); + + if (!restriction_) + { + cc.set ("p:arm-tag", find_name (L"choice", L"_arm_tag", set_)); + Traversal::Choice::contains (c, contain_particle_tag_); + cc.set ("p:arm", find_name (L"choice", L"_arm", set_)); + } + else + { + SemanticGraph::Compositor& b ( + *cc.get ( + "xsd-frontend-restriction-correspondence")); + + SemanticGraph::Context& bc (b.context ()); + + cc.set ("p:arm-tag", bc.get ("p:arm-tag")); + Traversal::Choice::contains (c, contain_particle_tag_); + cc.set ("p:arm", bc.get ("p:arm")); + } + + Traversal::Choice::traverse (c); + } + + virtual Void + traverse (SemanticGraph::Sequence& s) + { + SemanticGraph::Context& sc (s.context ()); + + if (!restriction_) + { + if (s.max () != 1) + sc.set ("p:next", find_name (L"sequence", L"_next", set_)); + else if (s.min () == 0) + sc.set ("p:present", find_name (L"sequence", L"_present", set_)); + } + else + { + // Root compositor that models inheritance by extension + // may not have an association. + // + if (sc.count ("xsd-frontend-restriction-correspondence")) + { + SemanticGraph::Compositor& b ( + *sc.get ( + "xsd-frontend-restriction-correspondence")); + + SemanticGraph::Context& bc (b.context ()); + + if (b.max () != 1) + { + sc.set ("p:next", bc.get ("p:next")); + + // Add the *_present callback if this is a restriction + // of sequence to optional. + // + if (s.min () == 0) + sc.set ("p:present", + find_name (L"sequence", L"_present", set_)); + } + else if (b.min () == 0) + sc.set ("p:present", bc.get ("p:present")); + } + } + + Traversal::Sequence::traverse (s); + } + + private: + NameSet& set_; + Boolean restriction_; + + ParticleTag particle_tag_; + Traversal::ContainsParticle contain_particle_tag_; + }; + + struct SecondaryParticle: Traversal::Element, Context + { + SecondaryParticle (Context& c, NameSet& set, Boolean restriction) + : Context (c), set_ (set), restriction_ (restriction) + { + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + Boolean poly ( + polymorphic && !e.type ().context ().count ("anonymous")); + + SemanticGraph::Context& ec (e.context ()); + + if (!restriction_) + { + String const& base (ec.get ("p:name")); + + ec.set ("p:parser", find_name (base + L"_parser", set_)); + ec.set ("p:member", find_name (base + L"_parser_", set_)); + + if (poly) + { + ec.set ( + "p:member-cache", find_name (base + L"_parser_cache_", set_)); + + ec.set ( + "p:member-map", find_name (base + L"_parser_map_", set_)); + } + } + else + { + SemanticGraph::Context& bc ( + ec.get ( + "xsd-frontend-restriction-correspondence")->context ()); + + ec.set ("p:parser", bc.get ("p:parser")); + ec.set ("p:member", bc.get ("p:member")); + + if (poly) + { + ec.set ("p:member-cache", bc.get ("p:member-cache")); + ec.set ("p:member-map", bc.get ("p:member-map")); + } + } + } + + private: + NameSet& set_; + Boolean restriction_; + }; + + struct SecondaryAttribute: Traversal::Attribute, Context + { + SecondaryAttribute (Context& c, NameSet& set, Boolean restriction) + : Context (c), set_ (set), restriction_ (restriction) + { + } + + virtual Void + traverse (SemanticGraph::Attribute& a) + { + using SemanticGraph::Attribute; + + SemanticGraph::Context& ac (a.context ()); + + if (!restriction_) + { + String const& base (ac.get ("p:name")); + + a.context ().set ( + "p:parser", find_name (base + L"_parser", set_)); + + a.context ().set ( + "p:member", find_name (base + L"_parser_", set_)); + } + else + { + SemanticGraph::Context& bc ( + ac.get ( + "xsd-frontend-restriction-correspondence")->context ()); + + ac.set ("p:parser", bc.get ("p:parser")); + ac.set ("p:member", bc.get ("p:member")); + } + } + + private: + NameSet& set_; + Boolean restriction_; + }; + + + // + // + struct Complex: Traversal::Complex, Context + { + Complex (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& c) + { + SemanticGraph::Context& cc (c.context ()); + + // Use processed name. + // + String const& name (cc.get ("p:name")); + + // We leave this set around to allow other mappings to use + // this information. + // + cc.set ("cxx-parser-name-processor-member-set", NameSet ()); + NameSet& member_set ( + cc.get ("cxx-parser-name-processor-member-set")); + + member_set.insert (name); + + // Add our base's members to the initial list unless we are + // inheriting by restriction in which case we need to have + // the same names as our base. + // + Boolean restriction (false); + + if (c.inherits_p ()) + { + // @@ What if this types name is the same as one of base's + // members? + // + SemanticGraph::Type& base (c.inherits ().base ()); + + if (base.is_a ()) + { + if (!base.context ().count ( + "cxx-parser-name-processor-member-set")) + { + dispatch (base); + } + + NameSet const& base_set ( + base.context ().get ( + "cxx-parser-name-processor-member-set")); + + member_set.insert (base_set.begin (), base_set.end ()); + + // Inheritance by restriction from anyType is a special case. + // + restriction = c.inherits ().is_a () && + !c.inherits ().base ().is_a (); + } + } + + // First assign the "primary" names. + // + { + if (c.contains_compositor_p ()) + { + PrimaryParticle particle (*this, member_set, restriction); + Traversal::Compositor compositor; + Traversal::ContainsCompositor contains_compositor; + Traversal::ContainsParticle contains_particle; + + contains_compositor >> compositor >> contains_particle; + + contains_particle >> compositor; + contains_particle >> particle; + + Complex::contains_compositor (c, contains_compositor); + } + + // + // + PrimaryAttribute attribute (*this, member_set, restriction); + Traversal::Names names (attribute); + + Complex::names (c, names); + } + + // Assign "secondary" names. + // + { + if (c.contains_compositor_p ()) + { + // In case of validation we generate some extra callbacks + // for compositors. + // + if (validation) + { + SecondaryParticle particle (*this, member_set, restriction); + SecondaryCompositor compositor (*this, member_set, restriction); + Traversal::ContainsCompositor contains_compositor; + Traversal::ContainsParticle contains_particle; + + contains_compositor >> compositor >> contains_particle; + + contains_particle >> compositor; + contains_particle >> particle; + + Complex::contains_compositor (c, contains_compositor); + } + else + { + SecondaryParticle particle (*this, member_set, restriction); + Traversal::Compositor compositor; + Traversal::ContainsCompositor contains_compositor; + Traversal::ContainsParticle contains_particle; + + contains_compositor >> compositor >> contains_particle; + + contains_particle >> compositor; + contains_particle >> particle; + + Complex::contains_compositor (c, contains_compositor); + } + } + + // + // + SecondaryAttribute attribute (*this, member_set, restriction); + Traversal::Names names (attribute); + + Complex::names (c, names); + } + } + }; + + + // + // + struct GlobalType: Traversal::Type, Context + { + GlobalType (Context& c, NameSet& set) + : Context (c), set_ (set) + { + } + + virtual Void + traverse (SemanticGraph::Type& t) + { + SemanticGraph::Context& c (t.context ()); + String const& n (t.name ()); + + String name (find_name (n + skel_suffix, set_)); + c.set ("p:name", name); + + // Assign the post_* name. + // + c.set ("p:post", find_post_name (t)); + + if (tiein) + c.set ("p:tiein", escape (n + L"_impl_")); + + // Note that we do not add this name to the set so that it + // does not influence other names. + // + if (impl) + c.set ("p:impl", escape (n + impl_suffix)); + } + + private: + String + find_post_name (SemanticGraph::Type& t) + { + String const& n (t.name ()); + + // It is possible that our base has the same type name (just + // in a different namespaces). Avoid name clash in this case. + // + using SemanticGraph::Complex; + + Complex* c = dynamic_cast (&t); + + if (c == 0 || !c->inherits_p ()) + { + return escape (L"post_" + n); + } + else + { + NameSet set; + + // Collect all base's post_*. In some mutual inclusion cases it + // is possible that our base won't have the post name assigned + // yet. In this situation will will have to figure it out + // ourselves (we can do it since we use the "raw" type name). + // + SemanticGraph::Type* b (&c->inherits ().base ()); + + while (true) + { + if (b->context ().count ("p:post")) + set.insert (b->context ().get ("p:post")); + else + set.insert (find_post_name (*b)); + + Complex* cb (dynamic_cast (b)); + + if (cb != 0 && cb->inherits_p ()) + { + b = &cb->inherits ().base (); + continue; + } + + break; + } + + String base_name (escape (L"post_" + n)); + String post (base_name); + + for (UnsignedLong i (1); set.find (post) != set.end (); ++i) + { + std::wostringstream os; + os << i; + post = base_name + os.str (); + } + + return post; + } + } + + private: + NameSet& set_; + }; + + + struct Namespace: Traversal::Namespace, Context + { + Namespace (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& ns) + { + SemanticGraph::Context& nsc (ns.context ()); + String const& name (ns.name ()); + + // Use a name set associated with this namespace if present. + // This will make sure that we don't get any conflicts in the + // multi-mapping translation case. Note that here we assume + // that all mappings traverse schemas in the same order which + // is currently the case. + // + if (global_type_names.find (name) == global_type_names.end ()) + { + if (!nsc.count ("name-set")) + nsc.set ("name-set", NameSet ()); + + NameSet& s (nsc.get ("name-set")); + global_type_names[name] = &s; + } + + NameSet& type_set (*global_type_names[name]); + + GlobalType type (*this, type_set); + Traversal::Names names (type); + + Traversal::Namespace::names (ns, names); + } + }; + + struct FundType : Traversal::AnyType, + Traversal::AnySimpleType, + + Traversal::Fundamental::Byte, + Traversal::Fundamental::UnsignedByte, + Traversal::Fundamental::Short, + Traversal::Fundamental::UnsignedShort, + Traversal::Fundamental::Int, + Traversal::Fundamental::UnsignedInt, + Traversal::Fundamental::Long, + Traversal::Fundamental::UnsignedLong, + Traversal::Fundamental::Integer, + Traversal::Fundamental::NonPositiveInteger, + Traversal::Fundamental::NonNegativeInteger, + Traversal::Fundamental::PositiveInteger, + Traversal::Fundamental::NegativeInteger, + + Traversal::Fundamental::Boolean, + + Traversal::Fundamental::Float, + Traversal::Fundamental::Double, + Traversal::Fundamental::Decimal, + + Traversal::Fundamental::String, + Traversal::Fundamental::NormalizedString, + Traversal::Fundamental::Token, + Traversal::Fundamental::Name, + Traversal::Fundamental::NameToken, + Traversal::Fundamental::NameTokens, + Traversal::Fundamental::NCName, + Traversal::Fundamental::Language, + + Traversal::Fundamental::QName, + + Traversal::Fundamental::Id, + Traversal::Fundamental::IdRef, + Traversal::Fundamental::IdRefs, + + Traversal::Fundamental::AnyURI, + + Traversal::Fundamental::Base64Binary, + Traversal::Fundamental::HexBinary, + + Traversal::Fundamental::Date, + Traversal::Fundamental::DateTime, + Traversal::Fundamental::Duration, + Traversal::Fundamental::Day, + Traversal::Fundamental::Month, + Traversal::Fundamental::MonthDay, + Traversal::Fundamental::Year, + Traversal::Fundamental::YearMonth, + Traversal::Fundamental::Time, + + Traversal::Fundamental::Entity, + Traversal::Fundamental::Entities, + + Context + + { + FundType (Context& c) + : Context (c) + { + } + + // anyType & anySimpleType. + // + virtual Void + traverse (SemanticGraph::AnyType& t) + { + set_names (t, "any_type"); + } + + virtual Void + traverse (SemanticGraph::AnySimpleType& t) + { + set_names (t, "any_simple_type"); + } + + // Boolean. + // + virtual Void + traverse (SemanticGraph::Fundamental::Boolean& t) + { + set_names (t, "boolean"); + } + + // Integral types. + // + virtual Void + traverse (SemanticGraph::Fundamental::Byte& t) + { + set_names (t, "byte"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::UnsignedByte& t) + { + set_names (t, "unsigned_byte"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Short& t) + { + set_names (t, "short"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::UnsignedShort& t) + { + set_names (t, "unsigned_short"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Int& t) + { + set_names (t, "int"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::UnsignedInt& t) + { + set_names (t, "unsigned_int"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Long& t) + { + set_names (t, "long"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::UnsignedLong& t) + { + set_names (t, "unsigned_long"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Integer& t) + { + set_names (t, "integer"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NonPositiveInteger& t) + { + set_names (t, "non_positive_integer"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NonNegativeInteger& t) + { + set_names (t, "non_negative_integer"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::PositiveInteger& t) + { + set_names (t, "positive_integer"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NegativeInteger& t) + { + set_names (t, "negative_integer"); + } + + // Floats. + // + virtual Void + traverse (SemanticGraph::Fundamental::Float& t) + { + set_names (t, "float"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Double& t) + { + set_names (t, "double"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Decimal& t) + { + set_names (t, "decimal"); + } + + // Strings. + // + virtual Void + traverse (SemanticGraph::Fundamental::String& t) + { + set_names (t, "string"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NormalizedString& t) + { + set_names (t, "normalized_string"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Token& t) + { + set_names (t, "token"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NameToken& t) + { + set_names (t, "nmtoken"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NameTokens& t) + { + set_names (t, "nmtokens"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Name& t) + { + set_names (t, "name"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NCName& t) + { + set_names (t, "ncname"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Language& t) + { + set_names (t, "language"); + } + + + // Qualified name. + // + virtual Void + traverse (SemanticGraph::Fundamental::QName& t) + { + set_names (t, "qname"); + } + + + // ID/IDREF. + // + virtual Void + traverse (SemanticGraph::Fundamental::Id& t) + { + set_names (t, "id"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::IdRef& t) + { + set_names (t, "idref"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::IdRefs& t) + { + set_names (t, "idrefs"); + } + + // URI. + // + virtual Void + traverse (SemanticGraph::Fundamental::AnyURI& t) + { + set_names (t, "uri"); + } + + // Binary. + // + virtual Void + traverse (SemanticGraph::Fundamental::Base64Binary& t) + { + set_names (t, "base64_binary"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::HexBinary& t) + { + set_names (t, "hex_binary"); + } + + + // Date/time. + // + virtual Void + traverse (SemanticGraph::Fundamental::Date& t) + { + set_names (t, "date"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::DateTime& t) + { + set_names (t, "date_time"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Duration& t) + { + set_names (t, "duration"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Day& t) + { + set_names (t, "gday"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Month& t) + { + set_names (t, "gmonth"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::MonthDay& t) + { + set_names (t, "gmonth_day"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Year& t) + { + set_names (t, "gyear");; + } + + virtual Void + traverse (SemanticGraph::Fundamental::YearMonth& t) + { + set_names (t, "gyear_month"); + + } + + virtual Void + traverse (SemanticGraph::Fundamental::Time& t) + { + set_names (t, "time"); + } + + // Entity. + // + virtual Void + traverse (SemanticGraph::Fundamental::Entity& t) + { + set_names (t, "entity"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Entities& t) + { + set_names (t, "entities"); + } + + private: + String + make_skel_name (String const& base) + { + return escape (base + skel_suffix); + } + + String + make_impl_name (String const& base) + { + return escape (base + impl_suffix); + } + + Void + set_names (SemanticGraph::Type& t, String const& name) + { + SemanticGraph::Context& c (t.context ()); + + WideChar const* ns = validation + ? L"::xsde::cxx::parser::validating::" + : L"::xsde::cxx::parser::non_validating::"; + + String skel (make_skel_name (name)); + c.set ("p:name", skel); + c.set ("p:real-name", ns + skel); + + String impl (make_impl_name (name)); + c.set ("p:impl", impl); + c.set ("p:real-impl", ns + impl); + + c.set ("p:post", L"post_" + name); + + if (tiein) + c.set ("p:tiein", name + L"_impl_"); + } + }; + + // Go into sourced/included/imported schemas while making sure + // we don't process the same stuff more than once. + // + struct Uses: Traversal::Sources, + Traversal::Includes, + Traversal::Imports + { + virtual Void + traverse (SemanticGraph::Sources& sr) + { + SemanticGraph::Schema& s (sr.schema ()); + + if (!s.context ().count ("cxx-parser-name-processor-seen")) + { + s.context ().set ("cxx-parser-name-processor-seen", true); + Traversal::Sources::traverse (sr); + } + } + + virtual Void + traverse (SemanticGraph::Includes& i) + { + SemanticGraph::Schema& s (i.schema ()); + + if (!s.context ().count ("cxx-parser-name-processor-seen")) + { + s.context ().set ("cxx-parser-name-processor-seen", true); + Traversal::Includes::traverse (i); + } + } + + virtual Void + traverse (SemanticGraph::Imports& i) + { + SemanticGraph::Schema& s (i.schema ()); + + if (!s.context ().count ("cxx-parser-name-processor-seen")) + { + s.context ().set ("cxx-parser-name-processor-seen", true); + Traversal::Imports::traverse (i); + } + } + }; + + // Go into implied schemas while making sure we don't process + // the same stuff more than once. + // + struct Implies: Traversal::Implies + { + virtual Void + traverse (SemanticGraph::Implies& i) + { + SemanticGraph::Schema& s (i.schema ()); + + if (!s.context ().count ("cxx-parser-name-processor-seen")) + { + s.context ().set ("cxx-parser-name-processor-seen", true); + Traversal::Implies::traverse (i); + } + } + }; + + Void + process_impl (CLI::Options const& ops, + SemanticGraph::Schema& tu, + SemanticGraph::Path const& file) + { + Context ctx (ops, tu, file); + + if (tu.names_begin ()->named ().name () == + L"http://www.w3.org/2001/XMLSchema") + { + // XML Schema namespace. + // + Traversal::Schema schema; + + Traversal::Names schema_names; + Traversal::Namespace ns; + Traversal::Names ns_names; + FundType fund_type (ctx); + + schema >> schema_names >> ns >> ns_names >> fund_type; + + schema.dispatch (tu); + } + else + { + // Pass one - assign names to fundamental types. + // + { + Traversal::Schema schema; + Implies implies; + Traversal::Schema xs_schema; + + schema >> implies >> xs_schema; + + Traversal::Names xs_schema_names; + Traversal::Namespace ns; + Traversal::Names ns_names; + FundType fund_type (ctx); + + xs_schema >> xs_schema_names >> ns >> ns_names >> fund_type; + + schema.dispatch (tu); + } + + // Pass two - assign names to global types. This pass cannot + // be combined with pass three because of possible recursive + // schema inclusions. Also note that we check first if this + // schema has already been processed which may happen in the + // file-per-type compilation mode. + // + if (!tu.context ().count ("cxx-parser-name-processor-seen")) + { + Traversal::Schema schema; + Uses uses; + + schema >> uses >> schema; + + Traversal::Names schema_names; + Namespace ns (ctx); + + schema >> schema_names >> ns; + + // Some twisted schemas do recusive self-inclusion. + // + tu.context ().set ("cxx-parser-name-processor-seen", true); + + schema.dispatch (tu); + } + + // Pass three - assign names inside complex types. Here we don't + // need to go into included/imported schemas. + // + { + Traversal::Schema schema; + Traversal::Sources sources; + + schema >> sources >> schema; + + Traversal::Names schema_names; + Traversal::Namespace ns; + Traversal::Names ns_names; + + schema >> schema_names >> ns >> ns_names; + + Complex complex (ctx); + + ns_names >> complex; + + schema.dispatch (tu); + } + } + } + } + + Void NameProcessor:: + process (CLI::Options const& ops, + SemanticGraph::Schema& tu, + SemanticGraph::Path const& file) + { + process_impl (ops, tu, file); + } + } +} diff --git a/xsde/cxx/parser/name-processor.hxx b/xsde/cxx/parser/name-processor.hxx new file mode 100644 index 0000000..8d55f30 --- /dev/null +++ b/xsde/cxx/parser/name-processor.hxx @@ -0,0 +1,32 @@ +// file : xsde/cxx/parser/name-processor.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2006-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef CXX_PARSER_NAME_PROCESSOR_HXX +#define CXX_PARSER_NAME_PROCESSOR_HXX + +#include + +#include + +#include + +namespace CXX +{ + namespace Parser + { + using namespace Cult::Types; + + class NameProcessor + { + public: + Void + process (CLI::Options const& options, + XSDFrontend::SemanticGraph::Schema&, + XSDFrontend::SemanticGraph::Path const& file); + }; + } +} + +#endif // CXX_PARSER_NAME_PROCESSOR_HXX diff --git a/xsde/cxx/parser/parser-forward.cxx b/xsde/cxx/parser/parser-forward.cxx new file mode 100644 index 0000000..3ce73de --- /dev/null +++ b/xsde/cxx/parser/parser-forward.cxx @@ -0,0 +1,112 @@ +// file : xsde/cxx/parser/parser-forward.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include + +#include +#include + +namespace CXX +{ + namespace Parser + { + namespace + { + struct Enumeration: Traversal::Enumeration, Context + { + Enumeration (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& e) + { + os << "class " << ename (e) << ";"; + } + }; + + // + // + struct List: Traversal::List, Context + { + List (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& l) + { + os << "class " << ename (l) << ";"; + } + }; + + // + // + struct Union: Traversal::Union, Context + { + Union (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& u) + { + os << "class " << ename (u) << ";"; + } + }; + + // + // + struct Complex : Traversal::Complex, Context + { + Complex (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& c) + { + os << "class " << ename (c) << ";"; + } + }; + } + + Void + generate_parser_forward (Context& ctx) + { + ctx.os << "// Forward declarations" << endl + << "//" << endl; + + Traversal::Schema schema; + + Traversal::Sources sources; + Traversal::Names schema_names; + + Namespace ns (ctx); + Traversal::Names names; + + schema >> sources >> schema; + schema >> schema_names >> ns >> names; + + List list (ctx); + Union union_ (ctx); + Complex complex (ctx); + Enumeration enumeration (ctx); + + names >> list; + names >> union_; + names >> complex; + names >> enumeration; + + schema.dispatch (ctx.schema_root); + + ctx.os << endl; + } + } +} diff --git a/xsde/cxx/parser/parser-forward.hxx b/xsde/cxx/parser/parser-forward.hxx new file mode 100644 index 0000000..1ddeb39 --- /dev/null +++ b/xsde/cxx/parser/parser-forward.hxx @@ -0,0 +1,22 @@ +// file : xsde/cxx/parser/parser-forward.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef CXX_PARSER_PARSER_FORWARD_HXX +#define CXX_PARSER_PARSER_FORWARD_HXX + +#include + +#include + +namespace CXX +{ + namespace Parser + { + Void + generate_parser_forward (Context&); + } +} + +#endif // CXX_PARSER_PARSER_FORWARD_HXX diff --git a/xsde/cxx/parser/parser-header.cxx b/xsde/cxx/parser/parser-header.cxx new file mode 100644 index 0000000..95642ab --- /dev/null +++ b/xsde/cxx/parser/parser-header.cxx @@ -0,0 +1,1818 @@ +// file : xsde/cxx/parser/parser-header.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include + +#include +#include + +namespace CXX +{ + namespace Parser + { + namespace + { + struct Enumeration: Traversal::Enumeration, Context + { + Enumeration (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& e) + { + String const& name (ename (e)); + SemanticGraph::Type& base (e.inherits ().base ()); + String fq_base (fq_name (base)); + + os << "class " << name << ": public " << + (mixin ? "virtual " : "") << fq_base + << "{" + << "public:" << endl + << "// Parser callbacks. Override them in your " << + "implementation." << endl + << "//" << endl; + + os << "// virtual void" << endl + << "// pre ();" << endl + << endl; + + String const& ret (ret_type (e)); + String const& base_ret (ret_type (base)); + + Boolean same (ret == base_ret); + + os << "virtual " << ret << endl + << post_name (e) << " ()" << + (same || ret == L"void" ? ";" : " = 0;"); + + if (poly_code) + { + os << endl + << "public:" << endl + << "static const char*" << endl + << "_static_type ();" + << endl + << "virtual const char*" << endl + << "_dynamic_type () const;"; + } + + if (tiein) + { + os << endl + << "// Constructor." << endl + << "//" << endl + << name << " (" << fq_base << "* tiein);" + << endl; + + os << "// Implementation details." << endl + << "//" << endl; + + // If our base has pure virtual post, override it here. + // + Boolean base_same ( + base.inherits_p () && + base_ret == ret_type (base.inherits ().base ())); + + if (!(base_same || base_ret == L"void")) + os << "virtual " << base_ret << endl + << post_name (base) << " ();" + << endl; + + os << "protected:" << endl + << name << "* " << etiein (e) << ";" + << name << " (" << name << "*, void*);"; + } + + os << "};"; + } + }; + + + // + // + struct List: Traversal::List, Context + { + List (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& l) + { + String const& name (ename (l)); + SemanticGraph::Type& t (l.argumented ().type ()); + String item_type (fq_name (t)); + + String item (unclash (name, "item")); + + os << "class " << name << ": public " << list_base + << "{" + << "public:" << endl + << "// Parser callbacks. Override them in your " << + "implementation." << endl + << "//" << endl; + + // pre + // + os << "// virtual void" << endl + << "// pre ();" << endl + << endl; + + // item + // + String const& arg (arg_type (t)); + + os << "virtual void" << endl + << item; + + if (arg == L"void") + os << " ();"; + else + os << " (" << arg << ");"; + + os << endl; + + // post + // + String const& ret (ret_type (l)); + + os << "virtual " << ret << endl + << post_name (l) << " ()" << (ret == L"void" ? ";" : " = 0;") + << endl; + + + // + // + os << "// Parser construction API." << endl + << "//" << endl; + + // item_parser + // + os << "void" << endl + << unclash (name, "item_parser") << " (" << item_type << "&);" + << endl; + + // parsers + // + os << "void" << endl + << "parsers (" << item_type << "& /* item */);" + << endl; + + if (reset) + os << "virtual void" << endl + << "_reset ();" + << endl; + + // c-tor + // + os << "// Constructor." << endl + << "//" << endl + << name << " ();" + << endl; + + if (poly_code) + { + os << "public:" << endl + << "static const char*" << endl + << "_static_type ();" + << endl + << "virtual const char*" << endl + << "_dynamic_type () const;" + << endl; + } + + // + // + os << "// Implementation details." << endl + << "//" << endl + << "protected:" << endl; + + if (tiein) + { + os << name << "* " << etiein (l) << ";" + << name << " (" << name << "*, void*);" + << endl; + } + + os << "virtual void" << endl + << "_xsde_parse_item (const " << string_type << "&);" + << endl; + + os << "protected:" << endl + << item_type << "* _xsde_" << item << "_;" + << "};"; + } + }; + + + struct Union: Traversal::Union, Context + { + Union (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& u) + { + String const& name (ename (u)); + + os << "class " << name << ": public " << simple_base + << "{" + << "public:" << endl + << "// Parser callbacks. Override them in your " << + "implementation." << endl + << "//" << endl; + + os << "// virtual void" << endl + << "// pre ();" << endl + << "//" << endl + << "// virtual void" << endl + << "// _characters (const " << string_type << "&);" << endl + << endl; + + String const& ret (ret_type (u)); + + os << "virtual " << ret << endl + << post_name (u) << " ()" << (ret == L"void" ? ";" : " = 0;"); + + if (poly_code) + { + os << endl + << "public:" << endl + << "static const char*" << endl + << "_static_type ();" + << endl + << "virtual const char*" << endl + << "_dynamic_type () const;"; + } + + if (tiein) + { + // c-tor + // + os << endl + << "// Constructor." << endl + << "//" << endl + << name << " ();" + << endl; + + // + // + os << "// Implementation details." << endl + << "//" << endl + << "protected:" << endl + << name << "* " << etiein (u) << ";" + << name << " (" << name << "*, void*);"; + } + + os << "};"; + } + }; + + // + // Callbacks. + // + + struct ParticleTag: Traversal::Particle, Context + { + ParticleTag (Context& c) + : Context (c), first_ (true) + { + } + + virtual Void + traverse (Type& p) + { + if (first_) + first_ = false; + else + os << "," << endl; + + os << etag (p); + } + + private: + Boolean first_; + }; + + struct CompositorCallback: Traversal::All, + Traversal::Choice, + Traversal::Sequence, + Context + { + CompositorCallback (Context& c) + : Context (c) + { + } + + virtual Void + traverse (SemanticGraph::All& a) + { + if (correspondent (a) == 0) + { + // For the all compositor, maxOccurs=1 and minOccurs={0,1}. + // + if (a.min () == 0) + { + os << "virtual void" << endl + << epresent (a) << " ();" + << endl; + } + } + + Traversal::All::traverse (a); + } + + virtual Void + traverse (SemanticGraph::Choice& c) + { + if (c.contains_begin () == c.contains_end ()) + return; + + if (correspondent (c) == 0) + { + os << "enum " << earm_tag (c) + << "{"; + + { + ParticleTag particle (*this); + Traversal::ContainsParticle contain_particle (particle); + Traversal::Choice::contains (c, contain_particle); + } + + os << "};"; + + os << "virtual void" << endl + << earm (c) << " (" << earm_tag (c) << ");" + << endl; + } + + Traversal::Choice::traverse (c); + } + + virtual Void + traverse (SemanticGraph::Sequence& s) + { + // Root compositor that models inheritance by extension + // may not have an association so we may fall through + // in to the 'if' case even though this is a restriction. + // This is ok since such a compositor always has max == + // min == 1 and so nothing is generated. + // + if (SemanticGraph::Compositor* b = correspondent (s)) + { + // Add the *_present callback if this is a restriction + // of sequence to optional. + // + if (b->max () != 1 && s.min () == 0) + { + os << "virtual void" << endl + << epresent (s) << " ();" + << endl; + } + } + else + { + if (s.max () != 1) + { + os << "virtual void" << endl + << enext (s) << " ();" + << endl; + } + else if (s.min () == 0) + { + os << "virtual void" << endl + << epresent (s) << " ();" + << endl; + } + } + + Traversal::Sequence::traverse (s); + } + }; + + struct ParticleCallback: Traversal::Element, Context + { + ParticleCallback (Context& c) + : Context (c) + { + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + if (correspondent (e) == 0) + { + String const& arg (arg_type (e.type ())); + + os << "virtual void" << endl + << ename (e); + + if (arg == L"void") + os << " ();"; + else + os << " (" << arg << ");"; + + os << endl; + } + } + }; + + struct AttributeCallback: Traversal::Attribute, Context + { + AttributeCallback (Context& c) + : Context (c) + { + } + + virtual Void + traverse (SemanticGraph::Attribute& a) + { + String const& arg (arg_type (a.type ())); + + os << "virtual void" << endl + << ename (a); + + if (arg == L"void") + os << " ();"; + else + os << " (" << arg << ");"; + + os << endl; + } + }; + + + // + // + struct ParticleAccessor: Traversal::Element, Context + { + ParticleAccessor (Context& c) + : Context (c) + { + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + String const& parser (eparser (e)); + + os << "void" << endl + << parser << " (" << fq_name (e.type ()) << "&);" + << endl; + + if (poly_code && !anonymous (e.type ())) + { + os << "void" << endl + << parser << " (" << parser_map << "&);" + << endl; + } + } + }; + + struct AttributeAccessor: Traversal::Attribute, Context + { + AttributeAccessor (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& a) + { + os << "void" << endl + << eparser (a) << " (" << fq_name (a.type ()) << "&);" + << endl; + } + }; + + + // + // + struct ParticleMember: Traversal::Element, Context + { + ParticleMember (Context& c) + : Context (c) + { + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + String type (fq_name (e.type ())); + + os << type << "* " << emember (e) << ";"; + + if (poly_code && !anonymous (e.type ())) + { + os << type << "* " << emember_cache (e) << ";" + << parser_map << "* " << emember_map (e) << ";" + << endl; + } + } + }; + + struct AttributeMember: Traversal::Attribute, Context + { + AttributeMember (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& a) + { + os << fq_name (a.type ()) << "* " << emember (a) << ";"; + } + }; + + // + // + struct Particle: Traversal::All, + Traversal::Choice, + Traversal::Sequence, + Context + { + Particle (Context& c) + : Context (c) + { + *this >> contains_particle_ >> *this; + } + + + virtual Void + traverse (SemanticGraph::All& a) + { + if (!a.context().count ("p:comp-number")) + return; + + UnsignedLong state_count ( + a.context().get ("p:state-count")); + + os << "void" << endl + << "all_0 (unsigned long&," << endl + << "unsigned char*," << endl + << "const " << string_type << "&," << endl + << "const " << string_type << "&," << endl; + + if (poly_runtime) + os << "const char*," << endl; + + os << "bool);" + << endl + << "unsigned char v_all_first_[" << state_count << "UL];" + << "::xsde::cxx::parser::validating::all_stack v_all_count_;" + << endl; + } + + virtual Void + traverse (SemanticGraph::Choice& c) + { + if (!c.context().count ("p:comp-number")) + return; + + UnsignedLong n (c.context ().get ("p:comp-number")); + + os << "void" << endl + << "choice_" << n << " (unsigned long&," << endl + << "unsigned long&," << endl + << "const " << string_type << "&," << endl + << "const " << string_type << "&," << endl; + + if (poly_runtime) + os << "const char*," << endl; + + os << "bool);" + << endl; + + Traversal::Choice::traverse (c); + } + + virtual Void + traverse (SemanticGraph::Sequence& s) + { + if (!s.context().count ("p:comp-number")) + return; + + UnsignedLong n (s.context ().get ("p:comp-number")); + + os << "void" << endl + << "sequence_" << n << " (unsigned long&," << endl + << "unsigned long&," << endl + << "const " << string_type << "&," << endl + << "const " << string_type << "&," << endl; + + if (poly_runtime) + os << "const char*," << endl; + + os << "bool);" + << endl; + + Traversal::Sequence::traverse (s); + } + + private: + Traversal::ContainsParticle contains_particle_; + }; + + + // + // + struct AttributeValidationState: Traversal::Attribute, Context + { + AttributeValidationState (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& a) + { + if (!a.optional ()) + { + os << "bool " << ename (a) << ";"; + } + } + }; + + // + // + struct Complex : Traversal::Complex, Context + { + Complex (Context& c) + : Context (c), + compositor_callback_val_ (c), + particle_callback_ (c), + attribute_callback_ (c), + particle_accessor_ (c), + attribute_accessor_ (c), + particle_member_ (c), + attribute_member_ (c), + attribute_validation_state_ (c) + { + // Callback. + // + if (validation) + { + contains_compositor_callback_ >> compositor_callback_val_; + compositor_callback_val_ >> contains_particle_callback_; + contains_particle_callback_ >> compositor_callback_val_; + } + else + { + contains_compositor_callback_ >> compositor_callback_non_val_; + compositor_callback_non_val_ >> contains_particle_callback_; + contains_particle_callback_ >> compositor_callback_non_val_; + } + + contains_particle_callback_ >> particle_callback_; + + names_attribute_callback_ >> attribute_callback_; + + // Accessor. + // + contains_compositor_accessor_ >> compositor_accessor_; + compositor_accessor_ >> contains_particle_accessor_; + contains_particle_accessor_ >> compositor_accessor_; + contains_particle_accessor_ >> particle_accessor_; + + names_attribute_accessor_ >> attribute_accessor_; + + // Member. + // + contains_compositor_member_ >> compositor_member_; + compositor_member_ >> contains_particle_member_; + contains_particle_member_ >> compositor_member_; + contains_particle_member_ >> particle_member_; + + names_attribute_member_ >> attribute_member_; + + // + // + names_attribute_validation_state_ >> attribute_validation_state_; + } + + virtual Void + traverse (Type& c) + { + String const& name (ename (c)); + + // In case of an inheritance-by-restriction, we don't need to + // generate parser callbacks, etc. since they are the same as + // in the base. We only need the parsing/validation code. + // + Boolean hb (c.inherits_p ()); + Boolean restriction (restriction_p (c)); + + Boolean he (has (c)); + Boolean ha (has (c)); + + Boolean hae (has_particle (c)); + Boolean haa (has (c)); + + Boolean hra (false); // Has required attribute. + if (ha) + { + RequiredAttributeTest test (hra); + Traversal::Names names_test (test); + names (c, names_test); + } + + // + // + os << "class " << name << ": public "; + + if (hb) + os << (mixin ? "virtual " : "") << fq_name (c.inherits ().base ()); + else + os << complex_base; + + os << "{" + << "public:" << endl + << "// Parser callbacks. Override them in your " << + "implementation." << endl + << "//" << endl; + + // pre + // + os << "// virtual void" << endl + << "// pre ();" << endl + << endl; + + // Member callbacks. + // + if (!restriction) + { + if (ha) + { + os << "// Attributes." << endl + << "//" << endl; + + names (c, names_attribute_callback_); + } + } + + if (!restriction || validation) + { + if (he || hae) + { + if (!restriction) + os << "// Elements." << endl + << "//" << endl; + + contains_compositor (c, contains_compositor_callback_); + } + } + + // post + // + String const& ret (ret_type (c)); + Boolean same (hb && ret == ret_type (c.inherits ().base ())); + + os << "virtual " << ret << endl + << post_name (c) << " ()" << + (same || ret == L"void" ? ";" : " = 0;") + << endl; + + // + // + if (!restriction && (he || ha)) + { + os << "// Parser construction API." << endl + << "//" << endl; + + os << "void" << endl + << "parsers ("; + + { + ParserParamDecl decl (*this, false); + decl.traverse (c); + } + + os << ");" + << endl; + + if (ha) + { + os << "// Individual attribute parsers." << endl + << "//" << endl; + + names (c, names_attribute_accessor_); + } + + if (he) + { + os << "// Individual element parsers." << endl + << "//" << endl; + + contains_compositor (c, contains_compositor_accessor_); + } + } + + if (!restriction && (he || ha) && reset) + { + os << "virtual void" << endl + << "_reset ();" + << endl; + } + + // Default c-tor. + // + if (tiein || + (!restriction && (he || ha)) || + (validation && (he || hae || hra))) + { + os << "// Constructor." << endl + << "//" << endl; + + if (hb && tiein) + os << name << " (" << fq_name (c.inherits ().base ()) << + "* tiein);" + << endl; + else + os << name << " ();" + << endl; + } + + if (poly_code) + { + os << "public:" << endl + << "static const char*" << endl + << "_static_type ();" + << endl + << "virtual const char*" << endl + << "_dynamic_type () const;" + << endl; + } + + // Implementation. + // + if (tiein || he || ha || (validation && (hae || haa))) + { + os << "// Implementation details." << endl + << "//" << endl; + } + + if (tiein) + { + if (hb) + { + // If our base has pure virtual post, override it here. + // + SemanticGraph::Type& base (c.inherits ().base ()); + + String const& base_ret (ret_type (base)); + + Boolean base_same ( + base.inherits_p () && + base_ret == ret_type (base.inherits ().base ())); + + if (!(base_same || base_ret == L"void")) + os << "virtual " << base_ret << endl + << post_name (base) << " ();" + << endl; + } + + os << "protected:" << endl + << name << "* " << etiein (c) << ";" + << name << " (" << name << "*, void*);" + << endl; + } + + // element + // + if (he || (validation && hae)) + { + os << "protected:" << endl; + + // _start_element_impl + // + os << "virtual bool" << endl + << "_start_element_impl (const " << string_type << "&," << endl + << "const " << string_type << "&"; + + if (poly_runtime) + os << "," << endl + << "const char*"; + + os << ");" + << endl; + + // end_element + // + os << "virtual bool" << endl + << "_end_element_impl (const " << string_type << "&," << endl + << "const " << string_type << "&);" + << endl; + } + + // attribute + // + if (validation) + { + if (ha || haa) + os << "protected:" << endl; + + if (ha) + { + os << "virtual bool" << endl + << "_attribute_impl_phase_one (const " << string_type << + "&," << endl + << "const " << string_type << "&," << endl + << "const " << string_type << "&);" << endl + << endl; + } + + if (haa) + { + os << "virtual bool" << endl + << "_attribute_impl_phase_two (const " << string_type << + "&," << endl + << "const " << string_type << "&," << endl + << "const " << string_type << "&);" + << endl; + } + } + else + { + if (ha) + { + os << "protected:" << endl + << "virtual bool" << endl + << "_attribute_impl (const " << string_type << "&," << endl + << "const " << string_type << "&," << endl + << "const " << string_type << "&);" + << endl; + } + } + + // characters + // + if (validation && c.mixed ()) + { + os << "protected:" << endl + << "virtual bool" << endl + << "_characters_impl (const " << string_type << "&);" + << endl; + } + + if (!restriction && (he || ha)) + { + os << "protected:" << endl; + + if (ha) + names (c, names_attribute_member_); + + if (he) + contains_compositor (c, contains_compositor_member_); + + os << endl; + } + + if (validation && (he || hae)) + { + UnsignedLong depth (c.context ().get ("p:depth")); + + os << "protected:" << endl; + + os << "struct v_state_descr_" + << "{" + << "void (" << fq_name (c) << "::*func) (" << endl + << "unsigned long&," << endl + << "unsigned long&," << endl + << "const " << string_type << "&," << endl + << "const " << string_type << "&," << endl; + + if (poly_runtime) + os << "const char*," << endl; + + os << "bool);" + << "unsigned long state;" + << "unsigned long count;" + << "};"; + + // Allocate one extra slot for the special state. + // + os << "struct v_state_" + << "{" + << "v_state_descr_ data[" << depth + 1 << "UL];" + << "unsigned long size;" + << "};"; + + os << "v_state_ v_state_first_;" + << "::xsde::cxx::parser::stack v_state_stack_;" + << endl; + + os << "virtual void" << endl + << "_pre_e_validate ();" + << endl; + + os << "virtual void" << endl + << "_post_e_validate ();" + << endl; + + Particle t (*this); + t.dispatch (c.contains_compositor ().compositor ()); + } + + if (validation && hra) + { + os << "protected:" << endl; + + os << "struct v_state_attr_" + << "{"; + + names (c, names_attribute_validation_state_); + + os << "};"; + + os << "v_state_attr_ v_state_attr_first_;" + << "::xsde::cxx::parser::stack v_state_attr_stack_;" + << endl; + + os << "virtual void" << endl + << "_pre_a_validate ();" + << endl; + + os << "virtual void" << endl + << "_post_a_validate ();" + << endl; + } + + os << "};"; + } + + private: + // + // + CompositorCallback compositor_callback_val_; + Traversal::Compositor compositor_callback_non_val_; + ParticleCallback particle_callback_; + Traversal::ContainsCompositor contains_compositor_callback_; + Traversal::ContainsParticle contains_particle_callback_; + + AttributeCallback attribute_callback_; + Traversal::Names names_attribute_callback_; + + // + // + Traversal::Compositor compositor_accessor_; + ParticleAccessor particle_accessor_; + Traversal::ContainsCompositor contains_compositor_accessor_; + Traversal::ContainsParticle contains_particle_accessor_; + + AttributeAccessor attribute_accessor_; + Traversal::Names names_attribute_accessor_; + + // + // + Traversal::Compositor compositor_member_; + ParticleMember particle_member_; + Traversal::ContainsCompositor contains_compositor_member_; + Traversal::ContainsParticle contains_particle_member_; + + AttributeMember attribute_member_; + Traversal::Names names_attribute_member_; + + // + // + AttributeValidationState attribute_validation_state_; + Traversal::Names names_attribute_validation_state_; + }; + + struct FundType : Context, + + Traversal::AnyType, + Traversal::AnySimpleType, + + Traversal::Fundamental::Byte, + Traversal::Fundamental::UnsignedByte, + Traversal::Fundamental::Short, + Traversal::Fundamental::UnsignedShort, + Traversal::Fundamental::Int, + Traversal::Fundamental::UnsignedInt, + Traversal::Fundamental::Long, + Traversal::Fundamental::UnsignedLong, + Traversal::Fundamental::Integer, + Traversal::Fundamental::NonPositiveInteger, + Traversal::Fundamental::NonNegativeInteger, + Traversal::Fundamental::PositiveInteger, + Traversal::Fundamental::NegativeInteger, + + Traversal::Fundamental::Boolean, + + Traversal::Fundamental::Float, + Traversal::Fundamental::Double, + Traversal::Fundamental::Decimal, + + Traversal::Fundamental::String, + Traversal::Fundamental::NormalizedString, + Traversal::Fundamental::Token, + Traversal::Fundamental::Name, + Traversal::Fundamental::NameToken, + Traversal::Fundamental::NameTokens, + Traversal::Fundamental::NCName, + Traversal::Fundamental::Language, + + Traversal::Fundamental::QName, + + Traversal::Fundamental::Id, + Traversal::Fundamental::IdRef, + Traversal::Fundamental::IdRefs, + + Traversal::Fundamental::AnyURI, + + Traversal::Fundamental::Base64Binary, + Traversal::Fundamental::HexBinary, + + Traversal::Fundamental::Date, + Traversal::Fundamental::DateTime, + Traversal::Fundamental::Duration, + Traversal::Fundamental::Day, + Traversal::Fundamental::Month, + Traversal::Fundamental::MonthDay, + Traversal::Fundamental::Year, + Traversal::Fundamental::YearMonth, + Traversal::Fundamental::Time, + + Traversal::Fundamental::Entity, + Traversal::Fundamental::Entities + { + FundType (Context& c) + : Context (c), xs_ns_ (xs_ns_name ()) + { + impl_ns_ = "::xsde::cxx::parser::"; + impl_ns_ += (validation ? L"validating" : L"non_validating"); + + if (options.value ()) + { + qname_type_ = xs_ns_ + L"::qname*"; + string_type_ = L"char*"; + } + else + { + qname_type_ = xs_ns_ + L"::qname"; + string_type_ = L"::std::string"; + } + + string_seq_type_ = xs_ns_ + L"::string_sequence*"; + buffer_type_ = xs_ns_ + L"::buffer*"; + + if (options.value ()) + { + long_type_ = L"long"; + unsigned_long_type_ = L"unsigned long"; + } + else + { + long_type_ = L"long long"; + unsigned_long_type_ = L"unsigned long long"; + } + } + + // anyType & anySimpleType. + // + virtual Void + traverse (SemanticGraph::AnyType& t) + { + gen_typedef (t, "void"); + } + + virtual Void + traverse (SemanticGraph::AnySimpleType& t) + { + gen_typedef (t, "void"); + } + + // Boolean. + // + virtual Void + traverse (SemanticGraph::Fundamental::Boolean& t) + { + gen_typedef (t, "bool"); + } + + // Integral types. + // + virtual Void + traverse (SemanticGraph::Fundamental::Byte& t) + { + gen_typedef (t, "signed char"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::UnsignedByte& t) + { + gen_typedef (t, "unsigned char"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Short& t) + { + gen_typedef (t, "short"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::UnsignedShort& t) + { + gen_typedef (t, "unsigned short"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Int& t) + { + gen_typedef (t, "int"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::UnsignedInt& t) + { + gen_typedef (t, "unsigned int"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Long& t) + { + gen_typedef (t, long_type_); + } + + virtual Void + traverse (SemanticGraph::Fundamental::UnsignedLong& t) + { + gen_typedef (t, unsigned_long_type_); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Integer& t) + { + gen_typedef (t, "long"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NegativeInteger& t) + { + gen_typedef (t, "long"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NonPositiveInteger& t) + { + gen_typedef (t, "long"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::PositiveInteger& t) + { + gen_typedef (t, "unsigned long"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NonNegativeInteger& t) + { + gen_typedef (t, "unsigned long"); + } + + // Floats. + // + virtual Void + traverse (SemanticGraph::Fundamental::Float& t) + { + gen_typedef (t, "float"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Double& t) + { + gen_typedef (t, "double"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Decimal& t) + { + gen_typedef (t, "double"); + } + + // Strings. + // + virtual Void + traverse (SemanticGraph::Fundamental::String& t) + { + gen_typedef (t, string_type_); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NormalizedString& t) + { + gen_typedef (t, string_type_); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Token& t) + { + gen_typedef (t, string_type_); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NameToken& t) + { + nmtoken_ = gen_typedef (t, string_type_); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NameTokens& t) + { + // NMTOKENS uses NMTOKEN implementation to parse individual items. + // As a result, we don't generate NMTOKENS if we didn't generate + // NMTOKEN. Here we assume NMTOKEN is handled before NMTOKENS. + // + if(nmtoken_) + gen_typedef (t, string_seq_type_); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Name& t) + { + gen_typedef (t, string_type_); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NCName& t) + { + gen_typedef (t, string_type_); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Language& t) + { + gen_typedef (t, string_type_); + } + + // Qualified name. + // + virtual Void + traverse (SemanticGraph::Fundamental::QName& t) + { + gen_typedef (t, qname_type_); + } + + // ID/IDREF. + // + virtual Void + traverse (SemanticGraph::Fundamental::Id& t) + { + gen_typedef (t, string_type_); + } + + virtual Void + traverse (SemanticGraph::Fundamental::IdRef& t) + { + idref_ = gen_typedef (t, string_type_); + } + + virtual Void + traverse (SemanticGraph::Fundamental::IdRefs& t) + { + // IDREFS uses IDREF implementation to parse individual items. + // As a result, we don't generate IDREFS if we didn't generate + // IDREF. Here we assume IDREF is handled before IDREFS. + // + if (idref_) + gen_typedef (t, string_seq_type_); + } + + // URI. + // + virtual Void + traverse (SemanticGraph::Fundamental::AnyURI& t) + { + gen_typedef (t, string_type_); + } + + // Binary. + // + virtual Void + traverse (SemanticGraph::Fundamental::Base64Binary& t) + { + gen_typedef (t, buffer_type_); + } + + virtual Void + traverse (SemanticGraph::Fundamental::HexBinary& t) + { + gen_typedef (t, buffer_type_); + } + + + // Date/time. + // + virtual Void + traverse (SemanticGraph::Fundamental::Date& t) + { + gen_typedef (t, xs_ns_ + L"::date"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::DateTime& t) + { + gen_typedef (t, xs_ns_ + L"::date_time"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Duration& t) + { + gen_typedef (t, xs_ns_ + L"::duration"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Day& t) + { + gen_typedef (t, xs_ns_ + L"::gday"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Month& t) + { + gen_typedef (t, xs_ns_ + L"::gmonth"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::MonthDay& t) + { + gen_typedef (t, xs_ns_ + L"::gmonth_day"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Year& t) + { + gen_typedef (t, xs_ns_ + L"::gyear"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::YearMonth& t) + { + gen_typedef (t, xs_ns_ + L"::gyear_month"); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Time& t) + { + gen_typedef (t, xs_ns_ + L"::time"); + } + + // Entity. + // + virtual Void + traverse (SemanticGraph::Fundamental::Entity&) + { + } + + virtual Void + traverse (SemanticGraph::Fundamental::Entities&) + { + } + + private: + Boolean + gen_typedef (SemanticGraph::Type& t, String const& type) + { + if (ret_type (t) == type) + { + SemanticGraph::Context& c (t.context ()); + + String const& real_name (c.get ("p:real-name")); + String const& name (c.get ("p:name")); + + os << "typedef " << real_name << " " << name << ";"; + + String const& real_impl (c.get ("p:real-impl")); + String const& impl (c.get ("p:impl")); + + os << "typedef " << real_impl << " " << impl << ";" + << endl; + + return true; + } + + return false; + } + + String xs_ns_; + String impl_ns_; + String qname_type_; + String string_type_; + String buffer_type_; + String string_seq_type_; + String long_type_; + String unsigned_long_type_; + + Boolean idref_; + Boolean nmtoken_; + }; + + struct FundNamespace : Namespace, Context + { + FundNamespace (Context& c) + : Namespace (c), Context (c) + { + } + + void + traverse (Type& ns) + { + pre (ns); + + String impl ("::xsde::cxx::parser::"); + impl += (validation ? L"validating" : L"non_validating"); + + String const c (char_type); + + os << "// Built-in XML Schema types mapping." << endl + << "//" << endl + << "using ::xsde::cxx::string_sequence;" + << "using ::xsde::cxx::qname;" + << "using ::xsde::cxx::buffer;" + << "using ::xsde::cxx::time_zone;" + << "using ::xsde::cxx::gday;" + << "using ::xsde::cxx::gmonth;" + << "using ::xsde::cxx::gyear;" + << "using ::xsde::cxx::gmonth_day;" + << "using ::xsde::cxx::gyear_month;" + << "using ::xsde::cxx::date;" + << "using ::xsde::cxx::time;" + << "using ::xsde::cxx::date_time;" + << "using ::xsde::cxx::duration;" + << endl; + + os << "// Base parser skeletons." << endl + << "//" << endl + << "using ::xsde::cxx::parser::parser_base;" + << "typedef " << impl << "::empty_content " << + "parser_empty_content;" + << "typedef " << impl << "::simple_content " << + "parser_simple_content;" + << "typedef " << impl << "::complex_content " << + "parser_complex_content;" + << "typedef " << impl << "::list_base parser_list_base;" + << endl; + + if (poly_code) + { + os << "// Parser map interface and default implementation." << endl + << "//" << endl + << "using ::xsde::cxx::parser::parser_map;" + << "using ::xsde::cxx::parser::parser_map_impl;" + << endl; + + os << "// Substitution and inheritance hashmaps load querying." << endl + << "//" << endl + << "using ::xsde::cxx::parser::parser_smap_buckets;" + << "using ::xsde::cxx::parser::parser_smap_elements;"; + + if (validation) + os << "using ::xsde::cxx::parser::validating::parser_imap_buckets;" + << "using ::xsde::cxx::parser::validating::parser_imap_elements;"; + + os << endl; + } + + os << "// Parser skeletons and implementations for the XML Schema" << endl + << "// built-in types." << endl + << "//" << endl; + + names (ns); + + os << "// Read-only string." << endl + << "//" << endl + << "using ::xsde::cxx::ro_string;" + << endl; + + os << "// Error codes." << endl + << "//" << endl; + + if (!exceptions) + os << "using xsde::cxx::sys_error;"; + + os << "typedef xsde::cxx::parser::expat::xml_error " << + "parser_xml_error;"; + + if (validation) + os << "typedef xsde::cxx::schema_error parser_schema_error;"; + + os << endl; + + if (exceptions) + { + os << "// Exceptions." << endl + << "//" << endl + << "typedef xsde::cxx::parser::exception parser_exception;" + << "typedef xsde::cxx::parser::xml parser_xml;"; + + if (validation) + os << "typedef xsde::cxx::parser::schema parser_schema;"; + + os << endl; + } + else + os << "// Error object." << endl + << "//" << endl + << "typedef xsde::cxx::parser::error parser_error;" + << endl; + + os << "// Document parser." << endl + << "//" << endl + << "using xsde::cxx::parser::expat::document_pimpl;" + << endl; + + os << "// Parser context." << endl + << "//" << endl + << "typedef xsde::cxx::parser::context parser_context;" + << endl; + + post (ns); + } + }; + } + + Void + generate_parser_header (Context& ctx, Boolean generate_xml_schema) + { + NarrowString extern_xml_schema; + + if (!generate_xml_schema) + extern_xml_schema = ctx.options.value (); + + if (extern_xml_schema) + { + String name (ctx.hxx_expr->merge (extern_xml_schema)); + + ctx.os << "#include " << ctx.process_include_path (name) << endl + << endl; + + // Generate includes that came from the type map. + // + if (ctx.schema_root.context ().count ("p:includes")) + { + typedef Cult::Containers::Set Includes; + + Includes& is ( + ctx.schema_root.context ().get ("p:includes")); + + for (Includes::ConstReverseIterator i (is.rbegin ()); + i != is.rend (); ++i) + { + ctx.os << "#include " << *i << endl; + } + + ctx.os << endl; + } + } + else + { + ctx.os << "#include " << endl + << "#include " << endl + << endl; + + // Data types. + // + ctx.os << "#include " << endl + << endl; + + // Error handling. + // + if (ctx.exceptions) + ctx.os << "#include " << endl + << endl; + else + { + ctx.os << "#include " << endl; + + if (ctx.validation) + ctx.os << "#include " << endl; + + ctx.os << "#include " << endl + << "#include " << endl + << endl; + } + + // Polymorphism support. + // + if (ctx.poly_code) + { + ctx.os << "#include " << endl + << "#include " << endl; + + if (ctx.validation) + ctx.os << "#include " << endl; + + ctx.os << endl; + } + + // Parsers. + // + if (ctx.validation) + ctx.os << "#include " << endl + << "#include " << endl + << "#include " << endl + << endl; + else + ctx.os << "#include " << endl + << "#include " << endl + << "#include " << endl + << endl; + + // Document. + // + ctx.os << "#include " << endl + << endl; + + // Generate includes that came from the type map. + // + if (ctx.schema_root.context ().count ("p:includes")) + { + typedef Cult::Containers::Set Includes; + + Includes& is ( + ctx.schema_root.context ().get ("p:includes")); + + for (Includes::ConstReverseIterator i (is.rbegin ()); + i != is.rend (); ++i) + { + ctx.os << "#include " << *i << endl; + } + + ctx.os << endl; + } + + // Generate fundamental types. + // + if (generate_xml_schema) + { + Traversal::Schema schema; + Traversal::Names names; + FundNamespace ns (ctx); + + schema >> names >> ns; + + Traversal::Names ns_names; + FundType type (ctx); + + ns >> ns_names >> type; + + schema.dispatch (ctx.schema_root); + } + else + { + Traversal::Schema schema, xsd; + Traversal::Implies implies; + Traversal::Names names; + FundNamespace ns (ctx); + + schema >> implies >> xsd >> names >> ns; + + Traversal::Names ns_names; + FundType type (ctx); + + ns >> ns_names >> type; + + schema.dispatch (ctx.schema_root); + } + } + + // Generate user type mapping. + // + if (!generate_xml_schema) + { + Traversal::Schema schema; + + Traversal::Sources sources; + Includes includes (ctx, Includes::header); + Traversal::Names schema_names; + + Namespace ns (ctx); + Traversal::Names names; + + schema >> includes; + schema >> sources >> schema; + schema >> schema_names >> ns >> names; + + List list (ctx); + Union union_ (ctx); + Complex complex (ctx); + Enumeration enumeration (ctx); + + names >> list; + names >> union_; + names >> complex; + names >> enumeration; + + schema.dispatch (ctx.schema_root); + } + } + } +} diff --git a/xsde/cxx/parser/parser-header.hxx b/xsde/cxx/parser/parser-header.hxx new file mode 100644 index 0000000..0baf7bc --- /dev/null +++ b/xsde/cxx/parser/parser-header.hxx @@ -0,0 +1,22 @@ +// file : xsde/cxx/parser/parser-header.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef CXX_PARSER_PARSER_HEADER_HXX +#define CXX_PARSER_PARSER_HEADER_HXX + +#include + +#include + +namespace CXX +{ + namespace Parser + { + Void + generate_parser_header (Context&, Boolean generate_xml_schema); + } +} + +#endif // CXX_PARSER_PARSER_HEADER_HXX diff --git a/xsde/cxx/parser/parser-inline.cxx b/xsde/cxx/parser/parser-inline.cxx new file mode 100644 index 0000000..689f95f --- /dev/null +++ b/xsde/cxx/parser/parser-inline.cxx @@ -0,0 +1,721 @@ +// file : xsde/cxx/parser/parser-inline.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include + +#include +#include + +namespace CXX +{ + namespace Parser + { + namespace + { + struct Enumeration: Traversal::Enumeration, Context + { + Enumeration (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& e) + { + if (tiein) + { + String const& name (ename (e)); + String const& impl (etiein (e)); + + // We have to use "real" (non-typedef) base name in base + // initializer because of some broken compilers (EVC 4.0). + // + SemanticGraph::Type& base (e.inherits ().base ()); + String fq_base (fq_name (base)); + String real_fq_base (real_fq_name (base)); + + os << "// " << name << endl + << "//" << endl + << endl; + + os << inl + << name << "::" << endl + << name << " (" << fq_base << "* tiein)" << endl + << ": " << real_fq_base << " (tiein, 0)," << endl + << " " << impl << " (0)" + << "{" + << "}"; + + os << inl + << name << "::" << endl + << name << " (" << name << "* impl, void*)" << endl + << ": " << real_fq_base << " (impl, 0)," << endl + << " " << impl << " (impl)" + << "{" + << "}"; + } + } + }; + + // + // + struct List: Traversal::List, Context + { + List (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& l) + { + String const& name (ename (l)); + SemanticGraph::Type& t (l.argumented ().type ()); + String item_type (fq_name (t)); + + String item (unclash (name, "item")); + + os << "// " << name << endl + << "//" << endl + << endl; + + // item_parser + // + os << inl + << "void " << name << "::" << endl + << unclash (name, "item_parser") << " (" << + item_type << "& " << item << ")" + << "{" + << "this->_xsde_" << item << "_ = &" << item << ";" + << "}"; + + // parsers + // + os << inl + << "void " << name << "::" << endl + << "parsers (" << item_type << "& " << item << ")" + << "{" + << "this->_xsde_" << item << "_ = &" << item << ";" + << "}"; + + // c-tor + // + os << inl + << name << "::" << endl + << name << " ()" << endl + << ": "; + + if (tiein) + os << etiein (l) << " (0)," << endl + << " "; + + os << "_xsde_" << item << "_ (0)" + << "{" + << "}"; + + if (tiein) + { + os << inl + << name << "::" << endl + << name << " (" << name << "* impl, void*)" << endl + << ": " << list_base << " (impl, 0)," << endl + << " " << etiein (l) << " (impl)," << endl + << " _xsde_" << item << "_ (0)" + << "{" + << "}"; + } + } + }; + + struct Union: Traversal::Union, Context + { + Union (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& u) + { + if (tiein) + { + String const& name (ename (u)); + String const& impl (etiein (u)); + + os << "// " << name << endl + << "//" << endl + << endl; + + // + // + os << inl + << name << "::" << endl + << name << " ()" << endl + << ": " << impl << " (0)" + << "{" + << "}"; + + // + // + os << inl + << name << "::" << endl + << name << " (" << name << "* impl, void*)" << endl + << ": " << simple_base << " (impl, 0)," << endl + << " " << impl << " (impl)" + << "{" + << "}"; + } + } + }; + + // + // + struct ParticleAccessor: Traversal::Element, Context + { + ParticleAccessor (Context& c) + : Context (c) + { + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + String const& scope (ename (e.scope ())); + String const& parser (eparser (e)); + + os << inl + << "void " << scope << "::" << endl + << parser << " (" << fq_name (e.type ()) << "& p)" + << "{" + << "this->" << emember (e) << " = &p;" + << "}"; + + if (poly_code && !anonymous (e.type ())) + { + os << inl + << "void " << scope << "::" << endl + << parser << " (" << parser_map << "& m)" + << "{" + << "this->" << emember_map (e) << " = &m;" + << "}"; + } + } + }; + + struct AttributeAccessor: Traversal::Attribute, Context + { + AttributeAccessor (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& a) + { + os << inl + << "void " << ename (a.scope ()) << "::" << endl + << eparser (a) << " (" << fq_name (a.type ()) << "& p)" + << "{" + << "this->" << emember (a) << " = &p;" + << "}"; + } + }; + + // + // + struct ParticleMemberSet: Traversal::Element, Context + { + ParticleMemberSet (Context& c) + : Context (c) + { + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + os << "this->" << emember (e) << " = &" << ename (e) << ";"; + } + }; + + struct AttributeMemberSet: Traversal::Attribute, Context + { + AttributeMemberSet (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& a) + { + os << "this->" << emember (a) << " = &" << ename (a) << ";"; + } + }; + + struct BaseMemberSet: Traversal::Complex, + Traversal::List, + Context + { + BaseMemberSet (Context& c) + : Context (c) + { + inherits_ >> *this; + } + + virtual Void + traverse (SemanticGraph::Complex& c) + { + inherits (c, inherits_); + + if (!restriction_p (c)) + { + names (c); + contains_compositor (c); + } + } + + virtual Void + traverse (SemanticGraph::List& l) + { + String const& name (ename (l)); + String item (unclash (name, "item")); + + os << "this->_xsde_" << item << "_ = &" << name << "_item;"; + } + + private: + Traversal::Inherits inherits_; + }; + + // + // + struct ParticleMemberInit: Traversal::Element, Context + { + ParticleMemberInit (Context& c, Boolean comma) + : Context (c), first_ (!comma) + { + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + if (first_) + first_ = false; + else + os << "," << endl << " "; + + os << emember (e) << " (0)"; + + if (poly_code && !anonymous (e.type ())) + { + os << "," << endl + << " " << emember_map (e) << " (0)"; + } + } + + Boolean + comma () const + { + return !first_; + } + + private: + Boolean first_; + }; + + struct AttributeMemberInit: Traversal::Attribute, Context + { + AttributeMemberInit (Context& c, Boolean comma) + : Context (c), first_ (!comma) + { + } + + virtual Void + traverse (Type& a) + { + if (first_) + first_ = false; + else + os << "," << endl << " "; + + os << emember (a) << " (0)"; + } + + Boolean + comma () const + { + return !first_; + } + + private: + Boolean first_; + }; + + // + // + struct Particle: Traversal::All, Context + { + Particle (Context& c) + : Context (c) + { + } + + virtual Void + traverse (SemanticGraph::All& a) + { + if (!a.context().count ("p:comp-number")) + return; + + UnsignedLong state_count ( + a.context().get ("p:state-count")); + + os << "," << endl + << " v_all_count_ (" << state_count << "UL, v_all_first_)"; + } + }; + + + // + // + struct Complex: Traversal::Complex, Context + { + Complex (Context& c) + : Context (c), + particle_accessor_ (c), + attribute_accessor_ (c), + base_set_ (c), + particle_set_ (c), + attribute_set_ (c), + particle_ (c) + { + // Accessor. + // + contains_compositor_accessor_ >> compositor_accessor_; + compositor_accessor_ >> contains_particle_accessor_; + contains_particle_accessor_ >> compositor_accessor_; + contains_particle_accessor_ >> particle_accessor_; + + names_attribute_accessor_ >> attribute_accessor_; + + // Member set. + // + inherits_base_set_ >> base_set_; + base_set_ >> contains_compositor_set_; + base_set_ >> names_attribute_set_; + + contains_compositor_set_ >> compositor_set_; + compositor_set_ >> contains_particle_set_; + contains_particle_set_ >> compositor_set_; + contains_particle_set_ >> particle_set_; + + names_attribute_set_ >> attribute_set_; + } + + virtual Void + traverse (Type& c) + { + Boolean hb (c.inherits_p ()); + Boolean he (has (c)); + Boolean ha (has (c)); + + Boolean hae (has_particle (c)); + + Boolean hra (false); // Has required attribute. + if (ha) + { + RequiredAttributeTest test (hra); + Traversal::Names names_test (test); + names (c, names_test); + } + + Boolean restriction (restriction_p (c)); + + if (!(tiein || + (!restriction && (he || ha)) || + (validation && (he || hae || hra)))) + return; + + String const& name (ename (c)); + + os << "// " << name << endl + << "//" << endl + << endl; + + if (!restriction && (he || ha)) + { + // _parser () + // + if (ha) + names (c, names_attribute_accessor_); + + if (he) + contains_compositor (c, contains_compositor_accessor_); + + + // parsers () + // + + os << inl + << "void " << name << "::" << endl + << "parsers ("; + + { + ParserParamDecl decl (*this, true); + decl.traverse (c); + } + + os << ")" + << "{"; + + inherits (c, inherits_base_set_); + + if (ha) + names (c, names_attribute_set_); + + if (he) + contains_compositor (c, contains_compositor_set_); + + os << "}"; + } + + // We have to use "real" (non-typedef) base name in base + // initializer because of some broken compilers (EVC 4.0). + // + String real_fq_base; + + if (hb && tiein) + real_fq_base = real_fq_name (c.inherits ().base ()); + + // Default c-tor. + // + os << inl + << name << "::" << endl; + + if (hb && tiein) + os << name << " (" << fq_name (c.inherits ().base ()) << + "* tiein)" << endl; + else + os << name << " ()" << endl; + + os << ": "; + + Boolean comma (false); + + if (hb && tiein) + { + os << real_fq_base << " (tiein, 0)"; + comma = true; + } + + if (tiein) + { + if (comma) + os << "," << endl << " "; + + os << etiein (c) << " (0)"; + comma = true; + } + + if (!restriction && (he || ha)) + { + if (ha) + { + AttributeMemberInit attribute_init (*this, comma); + Traversal::Names names_attribute_init; + + names_attribute_init >> attribute_init; + + names (c, names_attribute_init); + + comma = attribute_init.comma (); + } + + if (he) + { + Traversal::Compositor compositor_init; + ParticleMemberInit particle_init (*this, comma); + Traversal::ContainsCompositor contains_compositor_init; + Traversal::ContainsParticle contains_particle_init; + + contains_compositor_init >> compositor_init; + compositor_init >> contains_particle_init; + contains_particle_init >> compositor_init; + contains_particle_init >> particle_init; + + contains_compositor (c, contains_compositor_init); + + comma = particle_init.comma (); + } + } + + if (validation && (he || hae)) + { + if (comma) + os << "," << endl << " "; + + os << "v_state_stack_ (sizeof (v_state_), &v_state_first_)"; + + particle_.dispatch (c.contains_compositor ().compositor ()); + + comma = true; + } + + if (validation && (hra)) + { + if (comma) + os << "," << endl << " "; + + os << "v_state_attr_stack_ (sizeof (v_state_attr_), " << + "&v_state_attr_first_)"; + } + + os << "{" + << "}"; + + // Tiein c-tor. + // + if (tiein) + { + os << inl + << name << "::" << endl + << name << " (" << name << "* impl, void*)" << endl + << ": "; + + if (hb) + os << real_fq_base << " (impl, 0)," << endl; + else + os << complex_base << " (impl, 0)," << endl; + + os << " " << etiein (c) << " (impl)"; + + Boolean comma (true); + + if (!restriction && (he || ha)) + { + if (ha) + { + AttributeMemberInit attribute_init (*this, comma); + Traversal::Names names_attribute_init; + + names_attribute_init >> attribute_init; + + names (c, names_attribute_init); + + comma = attribute_init.comma (); + } + + if (he) + { + Traversal::Compositor compositor_init; + ParticleMemberInit particle_init (*this, comma); + Traversal::ContainsCompositor contains_compositor_init; + Traversal::ContainsParticle contains_particle_init; + + contains_compositor_init >> compositor_init; + compositor_init >> contains_particle_init; + contains_particle_init >> compositor_init; + contains_particle_init >> particle_init; + + contains_compositor (c, contains_compositor_init); + + comma = particle_init.comma (); + } + } + + if (validation && (he || hae)) + { + if (comma) + os << "," << endl << " "; + + os << "v_state_stack_ (sizeof (v_state_), &v_state_first_)"; + + particle_.dispatch (c.contains_compositor ().compositor ()); + + comma = true; + } + + if (validation && (hra)) + { + if (comma) + os << "," << endl << " "; + + os << "v_state_attr_stack_ (sizeof (v_state_attr_), " << + "&v_state_attr_first_)"; + } + + os << "{" + << "}"; + } + } + + private: + // + // + Traversal::Compositor compositor_accessor_; + ParticleAccessor particle_accessor_; + Traversal::ContainsCompositor contains_compositor_accessor_; + Traversal::ContainsParticle contains_particle_accessor_; + + AttributeAccessor attribute_accessor_; + Traversal::Names names_attribute_accessor_; + + // + // + BaseMemberSet base_set_; + Traversal::Inherits inherits_base_set_; + + Traversal::Compositor compositor_set_; + ParticleMemberSet particle_set_; + Traversal::ContainsCompositor contains_compositor_set_; + Traversal::ContainsParticle contains_particle_set_; + + AttributeMemberSet attribute_set_; + Traversal::Names names_attribute_set_; + + // + // + Particle particle_; + }; + } + + Void + generate_parser_inline (Context& ctx) + { + // Emit "weak" header includes that are used in the file-per-type + // compilation model. + // + if (!ctx.options.value ()) + { + Traversal::Schema schema; + Includes includes (ctx, Includes::source); + + schema >> includes; + schema.dispatch (ctx.schema_root); + } + + Traversal::Schema schema; + + Traversal::Sources sources; + Traversal::Names schema_names; + + Namespace ns (ctx); + Traversal::Names names; + + schema >> sources >> schema; + schema >> schema_names >> ns >> names; + + Enumeration enumeration (ctx); + List list (ctx); + Union union_ (ctx); + Complex complex (ctx); + + names >> enumeration; + names >> list; + names >> union_; + names >> complex; + + schema.dispatch (ctx.schema_root); + } + } +} diff --git a/xsde/cxx/parser/parser-inline.hxx b/xsde/cxx/parser/parser-inline.hxx new file mode 100644 index 0000000..c54f537 --- /dev/null +++ b/xsde/cxx/parser/parser-inline.hxx @@ -0,0 +1,22 @@ +// file : xsde/cxx/parser/parser-inline.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef CXX_PARSER_PARSER_INLINE_HXX +#define CXX_PARSER_PARSER_INLINE_HXX + +#include + +#include + +namespace CXX +{ + namespace Parser + { + Void + generate_parser_inline (Context&); + } +} + +#endif // CXX_PARSER_PARSER_INLINE_HXX diff --git a/xsde/cxx/parser/parser-source.cxx b/xsde/cxx/parser/parser-source.cxx new file mode 100644 index 0000000..5e1e9bd --- /dev/null +++ b/xsde/cxx/parser/parser-source.cxx @@ -0,0 +1,1626 @@ +// file : xsde/cxx/parser/parser-source.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include + +#include +#include + +namespace CXX +{ + namespace Parser + { + namespace + { + struct Enumeration: Traversal::Enumeration, Context + { + Enumeration (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& e) + { + String const& name (ename (e)); + String const& ret (ret_type (e)); + SemanticGraph::Type& base (e.inherits ().base ()); + String const& base_ret (ret_type (base)); + + Boolean same (ret == base_ret); + Boolean base_same ( + base.inherits_p () && + base_ret == ret_type (base.inherits ().base ())); + + if (same || ret == L"void" || poly_code || + (tiein && !(base_same || base_ret == L"void"))) + { + os << "// " << name << endl + << "//" << endl + << endl; + } + + if (same || ret == L"void") + { + String const& post (post_name (e)); + + os << ret << " " << name << "::" << endl + << post << " ()" + << "{"; + + if (tiein) + { + String const& impl (etiein (e)); + + os << "if (this->" << impl << ")" << endl + << (ret != L"void" ? "return " : "") << "this->" << + impl << "->" << post << " ();"; + } + + if (same) + { + if (tiein) + os << "else" << endl; + + if (ret == L"void") + os << post_name (base) << " ();"; + else + os << "return " << post_name (base) << " ();"; + } + + os << "}"; + } + + if (poly_code) + { + String id (e.name ()); + + if (String ns = xml_ns_name (e)) + { + id += L' '; + id += ns; + } + + os << "const char* " << name << "::" << endl + << "_static_type ()" + << "{" + << "return " << strlit (id) << ";" + << "}"; + + os << "const char* " << name << "::" << endl + << "_dynamic_type () const" + << "{" + << "return _static_type ();" + << "}"; + + if (validation) + { + Boolean gen (!anonymous (e)); + + // We normally don't need to enter anonymous types into + // the inheritance map. The only exception is when an + // anonymous types is defined inside an element that + // is a member of a substitution group. + // + if (!gen) + { + // The first instance that this anonymous type classifies + // is the prototype for others if any. + // + SemanticGraph::Instance& i ( + e.classifies_begin ()->instance ()); + + if (SemanticGraph::Element* e = + dynamic_cast (&i)) + { + if (e->substitutes_p ()) + gen = true; + } + } + + if (gen) + { + os << "static" << endl + << "const ::xsde::cxx::parser::validating::" << + "inheritance_map_entry" << endl + << "_xsde_" << name << "_inheritance_map_entry_ (" << endl + << name << "::_static_type ()," << endl + << fq_name (base) << "::_static_type ());" + << endl; + } + } + } + + if (tiein && !(base_same || base_ret == L"void")) + { + String const& impl (etiein (base)); + String const& base_post (post_name (base)); + + os << base_ret << " " << name << "::" << endl + << base_post << " ()" + << "{" + << "assert (this->" << impl << ");" + << "return this->" << impl << "->" << base_post << " ();" + << "}"; + } + } + }; + + // + // + struct List: Traversal::List, Context + { + List (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& l) + { + String const& name (ename (l)); + SemanticGraph::Type& t (l.argumented ().type ()); + + String item (unclash (name, "item")); + String inst (L"_xsde_" + item + L"_"); + String const& post (post_name (t)); + + os << "// " << name << endl + << "//" << endl + << endl; + + // item + // + String const& arg (arg_type (t)); + + os << "void " << name << "::" << endl + << item; + + if (arg == L"void") + os << " ()"; + else + os << " (" << arg << (tiein ? " x" : "") << ")"; + + os << "{"; + + if (tiein) + { + String const& impl (etiein (l)); + + os << "if (this->" << impl << ")" << endl + << "this->" << impl << "->" << item << " (" << + (arg != L"void" ? "x" : "") << ");"; + } + + os << "}"; + + // post + // + if (ret_type (l) == L"void") + { + String const& post (post_name (l)); + + os << "void " << name << "::" << endl + << post << " ()" + << "{"; + + if (tiein) + { + String const& impl (etiein (l)); + + os << "if (this->" << impl << ")" << endl + << "this->" << impl << "->" << post << " ();"; + } + + os << "}"; + } + + // reset + // + if (reset) + { + os << "void " << name << "::" << endl + << "_reset ()" + << "{" + << list_base << "::_reset ();" + << endl + << "if (this->" << inst << ")" << endl + << "this->" << inst << "->_reset ();" + << "}"; + } + + // parse_item + // + os << "void " << name << "::" << endl + << "_xsde_parse_item (const " << string_type << "& v)" + << "{" + << "if (this->" << inst << ")" + << "{" + << "::xsde::cxx::parser::context& ctx = this->_context ();" + << endl; + + // This implementation should work for both validating + // and non-validating cases. + // + if (!exceptions || validation) + { + String const& ret (ret_type (t)); + + os << "this->" << inst << "->pre ();"; + + if (!exceptions) + os << endl + << "if (this->" << inst << "->_error_type ())" << endl + << "this->" << inst << "->_copy_error (ctx);" + << "else" << endl; + + os << "this->" << inst << "->_pre_impl (ctx);" + << endl + << "if (!ctx.error_type ())" << endl + << "this->" << inst << "->_characters (v);" + << endl + << "if (!ctx.error_type ())" << endl + << "this->" << inst << "->_post_impl ();" + << endl + << "if (!ctx.error_type ())" << endl; + + if (ret == L"void") + os << "this->" << inst << "->" << post << " ();" + << endl; + else + os << "{" + << arg_type (t) << " tmp = this->" << inst << "->" << + post << " ();" + << endl; + + if (!exceptions) + os << "if (this->" << inst << "->_error_type ())" << endl + << "this->" << inst << "->_copy_error (ctx);" + << "else" << endl; + + if (ret == L"void") + os << "this->" << item << " ();"; + else + os << "this->" << item << " (tmp);" + << "}"; + } + else + { + os << "this->" << inst << "->pre ();" + << "this->" << inst << "->_pre_impl (ctx);" + << "this->" << inst << "->_characters (v);" + << "this->" << inst << "->_post_impl ();"; + + if (ret_type (t) == L"void") + os << "this->" << inst << "->" << post << " ();" + << "this->" << item << " ();"; + else + os << "this->" << item << " (this->" << inst << "->" << + post << " ());"; + } + + os << "}" + << "}"; + + // + // + if (poly_code) + { + String id (l.name ()); + + if (String ns = xml_ns_name (l)) + { + id += L' '; + id += ns; + } + + os << "const char* " << name << "::" << endl + << "_static_type ()" + << "{" + << "return " << strlit (id) << ";" + << "}"; + + os << "const char* " << name << "::" << endl + << "_dynamic_type () const" + << "{" + << "return _static_type ();" + << "}"; + } + } + }; + + + // + // + struct Union: Traversal::Union, Context + { + Union (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& u) + { + String const& name (ename (u)); + String const& ret (ret_type (u)); + + if (ret == L"void" || poly_code) + { + os << "// " << name << endl + << "//" << endl + << endl; + } + + if (ret == L"void") + { + String const& post (post_name (u)); + + os << "void " << name << "::" << endl + << post << " ()" + << "{"; + + if (tiein) + { + String const& impl (etiein (u)); + + os << "if (this->" << impl << ")" << endl + << "this->" << impl << "->" << post << " ();"; + } + + os << "}"; + } + + if (poly_code) + { + String id (u.name ()); + + if (String ns = xml_ns_name (u)) + { + id += L' '; + id += ns; + } + + os << "const char* " << name << "::" << endl + << "_static_type ()" + << "{" + << "return " << strlit (id) << ";" + << "}"; + + os << "const char* " << name << "::" << endl + << "_dynamic_type () const" + << "{" + << "return _static_type ();" + << "}"; + } + } + }; + + // + // + struct ParticleReset: Traversal::Element, Context + { + ParticleReset (Context& c) + : Context (c) + { + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + String const& m (emember (e)); + + os << "if (this->" << m << ")" << endl + << "this->" << m << "->_reset ();" + << endl; + + if (poly_code && !anonymous (e.type ())) + { + String const& map (emember_map (e)); + + os << "if (this->" << map << ")" << endl + << "this->" << map << "->reset ();" + << endl; + } + } + }; + + struct AttributeReset: Traversal::Attribute, Context + { + AttributeReset (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& a) + { + String const& m (emember (a)); + + os << "if (this->" << m << ")" << endl + << "this->" << m << "->_reset ();" + << endl; + } + }; + + // + // + struct StartElement : Traversal::Element, Context + { + StartElement (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& e) + { + Boolean poly (poly_code && !anonymous (e.type ())); + Boolean subst (poly && e.global ()); + String const& inst (poly ? emember_cache (e) : emember (e)); + + if (e.qualified () && e.namespace_ ().name ()) + { + os << "if (" << (subst ? "(" : "") << + "n == " << strlit (e.name ()) << " &&" << endl + << "ns == " << strlit (e.namespace_ ().name ()); + } + else + { + os << "if (" << (subst ? "(" : "") << + "n == " << L << strlit (e.name ()) << " && ns.empty ()"; + } + + // Only a globally-defined element can be a subst-group root. + // + if (subst) + { + String root_id (e.name ()); + + if (String const& ns = e.namespace_ ().name ()) + { + root_id += L' '; + root_id += ns; + } + + os << ") ||" << endl + << "::xsde::cxx::parser::substitution_map_instance ()" << + ".check (" << endl + << "ns, n, " << strlit (root_id) << ", t)"; + } + + os << ")" + << "{"; + + if (poly) + { + // In case of mixin we use virtual inheritance and only + // dynamic_cast can be used. + // + String cast (mixin ? L"dynamic_cast" : L"static_cast"); + String fq_type (fq_name (e.type ())); + String const& member (emember (e)); + String const& member_map (emember_map (e)); + + os << "if (t == 0 && this->" << member << " != 0)" << endl + << "this->" << inst << " = this->" << member << ";" + << "else" + << "{" + << "const char* ts = " << fq_type << "::_static_type ();" + << endl + << "if (t == 0)" << endl + << "t = ts;" + << endl + << "if (this->" << member << " != 0 && " << + "strcmp (t, ts) == 0)" << endl + << "this->" << inst << " = this->" << member << ";" + << "else if (this->" << member_map << " != 0)" << endl + << "this->" << inst << " = " << cast << "< " << fq_type << + "* > (" << endl + << "this->" << member_map << "->find (t));" + << "else" << endl + << "this->" << inst << " = 0;" + << "}"; + } + + os << "if (this->" << inst << ")" + << "{"; + + if (exceptions) + { + os << "this->" << inst << "->pre ();" + << "this->" << inst << "->_pre_impl (ctx);"; + } + else + { + // Note that after pre() we need to check both parser and + // context error states because of the recursive parsing. + // + os << "this->" << inst << "->pre ();" + << endl + << "if (this->" << inst << "->_error_type ())" << endl + << "this->" << inst << "->_copy_error (ctx);" + << endl + << "if (!ctx.error_type ())" << endl + << "this->" << inst << "->_pre_impl (ctx);"; + } + + os << "}" + << "else" << endl + << "ctx.current_.depth_++;" // Ignoring document fragment. + << endl + << "return true;" + << "}"; + } + }; + + + // + // + struct EndElement : Traversal::Element, Context + { + EndElement (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& e) + { + String const& name (ename (e)); + Boolean poly (poly_code && !anonymous (e.type ())); + Boolean subst (poly && e.global ()); + String const& inst (poly ? emember_cache (e) : emember (e)); + + if (e.qualified () && e.namespace_ ().name ()) + { + os << "if (" << (subst ? "(" : "") << + "n == " << strlit (e.name ()) << " &&" << endl + << "ns == " << strlit (e.namespace_ ().name ()); + } + else + { + os << "if (" << (subst ? "(" : "") << + "n == " << strlit (e.name ()) << " && ns.empty ()"; + } + + // Only a globally-defined element can be a subst-group root. + // + if (subst) + { + String root_id (e.name ()); + + if (String const& ns = e.namespace_ ().name ()) + { + root_id += L' '; + root_id += ns; + } + + os << ") ||" << endl + << "::xsde::cxx::parser::substitution_map_instance ()" << + ".check (" << endl + << "ns, n, " << strlit (root_id) << ")"; + } + + os << ")" + << "{"; + + SemanticGraph::Type& type (e.type ()); + String const& post (post_name (type)); + + os << "if (this->" << inst << ")" + << "{"; + + if (exceptions) + { + if (ret_type (type) == L"void") + os << "this->" << inst << "->" << post << " ();" + << "this->" << name << " ();"; + else + os << "this->" << name << " (this->" << inst << "->" << + post << " ());"; + } + else + { + // Note that after post() we need to check both parser and + // context error states because of the recursive parsing. + // + if (ret_type (type) == L"void") + { + os << "this->" << inst << "->" << post << " ();" + << endl + << "if (this->" << inst << "->_error_type ())" << endl + << "this->" << inst << "->_copy_error (ctx);" + << endl + << "if (!ctx.error_type ())" << endl + << "this->" << name << " ();"; + } + else + { + os << arg_type (type) << " tmp = this->" << inst << "->" << + post << " ();" + << endl + << "if (this->" << inst << "->_error_type ())" << endl + << "this->" << inst << "->_copy_error (ctx);" + << endl + << "if (!ctx.error_type ())" << endl + << "this->" << name << " (tmp);"; + } + } + + os << "}" + << "return true;" + << "}"; + } + }; + + + // + // + struct Attribute : Traversal::Attribute, Context + { + Attribute (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& a) + { + String const& name (ename (a)); + String const& inst (emember (a)); + + if (a.qualified () && a.namespace_ ().name ()) + { + os << "if (n == " << L << strlit (a.name ()) << " &&" << endl + << "ns == " << L << strlit (a.namespace_ ().name ()) << ")" + << "{"; + } + else + { + os << "if (n == " << L << strlit (a.name ()) << " && ns.empty ())" + << "{"; + } + + SemanticGraph::Type& type (a.type ()); + String const& post (post_name (type)); + String const& ret (ret_type (type)); + + os << "if (this->" << inst << ")" + << "{"; + + if (exceptions) + { + os << "this->" << inst << "->pre ();" + << "this->" << inst << "->_pre_impl (ctx);" + << "this->" << inst << "->_characters (v);" + << "this->" << inst << "->_post_impl ();"; + + if (ret == L"void") + os << "this->" << inst << "->" << post << " ();" + << "this->" << name << " ();"; + else + os << "this->" << name << " (this->" << inst << "->" << + post << " ());"; + } + else + { + os << "this->" << inst << "->pre ();" + << endl + << "if (!this->" << inst << "->_error_type ())" << endl + << "this->" << inst << "->_pre_impl (ctx);" + << "else" << endl + << "this->" << inst << "->_copy_error (ctx);" + << endl + << "if (!ctx.error_type ())" << endl + << "this->" << inst << "->_characters (v);" + << endl + << "if (!ctx.error_type ())" << endl + << "this->" << inst << "->_post_impl ();" + << endl + << "if (!ctx.error_type ())" << endl; + + if (ret == L"void") + os << "this->" << inst << "->" << post << " ();" + << endl; + else + os << "{" + << arg_type (type) << " tmp = this->" << inst << "->" << + post << " ();" + << endl; + + os << "if (this->" << inst << "->_error_type ())" << endl + << "this->" << inst << "->_copy_error (ctx);" + << "else" << endl; + + if (ret == L"void") + os << "this->" << name << " ();"; + else + os << "this->" << name << " (tmp);" + << "}"; + } + + os << "}" + << "return true;" + << "}"; + } + }; + + // + // Callbacks. + // + + struct CompositorCallback: Traversal::All, + Traversal::Choice, + Traversal::Sequence, + Context + { + CompositorCallback (Context& c) + : Context (c) + { + } + + virtual Void + traverse (SemanticGraph::All& a) + { + if (correspondent (a) == 0) + { + // For the all compositor, maxOccurs=1 and minOccurs={0,1}. + // + if (a.min () == 0) + { + SemanticGraph::Scope& s (scope (a)); + String const& present (epresent (a)); + + os << "void " << ename (s) << "::" << endl + << present << " ()" + << "{"; + + if (tiein) + { + String const& impl ( + etiein (dynamic_cast (s))); + + os << "if (this->" << impl << ")" << endl + << "this->" << impl << "->" << present << " ();"; + } + + os << "}"; + } + } + + Traversal::All::traverse (a); + } + + virtual Void + traverse (SemanticGraph::Choice& c) + { + if (c.contains_begin () == c.contains_end ()) + return; + + if (correspondent (c) == 0) + { + SemanticGraph::Scope& s (scope (c)); + String const& arm (earm (c)); + + os << "void " << ename (s) << "::" << endl + << arm << " (" << earm_tag (c) << (tiein ? " x" : "") << ")" + << "{"; + + if (tiein) + { + String const& impl ( + etiein (dynamic_cast (s))); + + os << "if (this->" << impl << ")" << endl + << "this->" << impl << "->" << arm << " (x);"; + } + + os << "}"; + + } + + Traversal::Choice::traverse (c); + } + + virtual Void + traverse (SemanticGraph::Sequence& s) + { + // Root compositor that models inheritance by extension + // may not have an association so we may fall through + // in to the 'if' case even though this is a restriction. + // This is ok since such a compositor always has max == + // min == 1 and so nothing is generated. + // + if (SemanticGraph::Compositor* b = correspondent (s)) + { + // Add the *_present callback if this is a restriction + // of sequence to optional. + // + if (b->max () != 1 && s.min () == 0) + { + SemanticGraph::Scope& ss (scope (s)); + + os << "void " << ename (ss) << "::" << endl + << epresent (s) << " ()" + << "{"; + + if (tiein) + { + String const& impl ( + etiein (dynamic_cast (ss))); + + os << "if (this->" << impl << ")" << endl + << "this->" << impl << "->" << enext (s) << " ();"; + } + else + os << "this->" << enext (s) << " ();"; + + os << "}"; + } + } + else + { + if (s.max () != 1) + { + SemanticGraph::Scope& ss (scope (s)); + String const& next (enext (s)); + + os << "void " << ename (ss) << "::" << endl + << next << " ()" + << "{"; + + if (tiein) + { + String const& impl ( + etiein (dynamic_cast (ss))); + + os << "if (this->" << impl << ")" << endl + << "this->" << impl << "->" << next << " ();"; + } + + os << "}"; + } + else if (s.min () == 0) + { + SemanticGraph::Scope& ss (scope (s)); + String const& present (epresent (s)); + + os << "void " << ename (ss) << "::" << endl + << present << " ()" + << "{"; + + if (tiein) + { + String const& impl ( + etiein (dynamic_cast (ss))); + + os << "if (this->" << impl << ")" << endl + << "this->" << impl << "->" << present << " ();"; + } + + os << "}"; + } + } + + Traversal::Sequence::traverse (s); + } + + private: + SemanticGraph::Scope& + scope (SemanticGraph::Compositor& c) + { + SemanticGraph::Compositor* root (&c); + + while (root->contained_particle_p ()) + root = &root->contained_particle ().compositor (); + + return dynamic_cast ( + root->contained_compositor ().container ()); + } + }; + + struct ParticleCallback: Traversal::Element, Context + { + ParticleCallback (Context& c) + : Context (c) + { + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + if (correspondent (e) == 0) + { + String const& name (ename (e)); + String const& arg (arg_type (e.type ())); + + os << "void " << ename (e.scope ()) << "::" << endl + << name; + + if (arg == L"void") + os << " ()"; + else + os << " (" << arg << (tiein ? " x" : "") << ")"; + + os << "{"; + + if (tiein) + { + String const& impl ( + etiein (dynamic_cast (e.scope ()))); + + os << "if (this->" << impl << ")" << endl + << "this->" << impl << "->" << name << " (" << + (arg != L"void" ? "x" : "") << ");"; + } + + os << "}"; + } + } + }; + + struct AttributeCallback: Traversal::Attribute, Context + { + AttributeCallback (Context& c) + : Context (c) + { + } + + virtual Void + traverse (SemanticGraph::Attribute& a) + { + String const& name (ename (a)); + String const& arg (arg_type (a.type ())); + + os << "void " << ename (a.scope ()) << "::" << endl + << name; + + if (arg == L"void") + os << " ()"; + else + os << " (" << arg << (tiein ? " x" : "") << ")"; + + os << "{"; + + if (tiein) + { + String const& impl ( + etiein (dynamic_cast (a.scope ()))); + + os << "if (this->" << impl << ")" << endl + << "this->" << impl << "->" << name << " (" << + (arg != L"void" ? "x" : "") << ");"; + } + + os << "}"; + } + }; + + // + // + struct Complex : Traversal::Complex, Context + { + Complex (Context& c) + : Context (c), + compositor_callback_val_ (c), + particle_callback_ (c), + attribute_callback_ (c), + particle_reset_ (c), + attribute_reset_ (c), + start_element_ (c), + end_element_ (c), + attribute_ (c) + { + // Callback. + // + if (validation) + { + contains_compositor_callback_ >> compositor_callback_val_; + compositor_callback_val_ >> contains_particle_callback_; + contains_particle_callback_ >> compositor_callback_val_; + } + else + { + contains_compositor_callback_ >> compositor_callback_non_val_; + compositor_callback_non_val_ >> contains_particle_callback_; + contains_particle_callback_ >> compositor_callback_non_val_; + } + + contains_particle_callback_ >> particle_callback_; + + names_attribute_callback_ >> attribute_callback_; + + // Reset. + // + contains_compositor_reset_ >> compositor_reset_; + compositor_reset_ >> contains_particle_reset_; + contains_particle_reset_ >> compositor_reset_; + contains_particle_reset_ >> particle_reset_; + + names_attribute_reset_ >> attribute_reset_; + + // + // + contains_compositor_start_ >> start_compositor_; + start_compositor_ >> contains_particle_start_; + contains_particle_start_ >> start_compositor_; + contains_particle_start_ >> start_element_; + + // + // + contains_compositor_end_ >> end_compositor_; + end_compositor_ >> contains_particle_end_; + contains_particle_end_ >> end_compositor_; + contains_particle_end_ >> end_element_; + + // + // + names_attribute_ >> attribute_; + } + + virtual Void + traverse (Type& c) + { + Boolean hb (c.inherits_p ()); + Boolean restriction (restriction_p (c)); + Boolean he (has (c)); + Boolean ha (has (c)); + + Boolean hae (has_particle (c)); + + Boolean hra (false); // Has required attribute. + if (ha) + { + RequiredAttributeTest test (hra); + Traversal::Names names_test (test); + names (c, names_test); + } + + String const& name (ename (c)); + String const& ret (ret_type (c)); + Boolean same (hb && ret == ret_type (c.inherits ().base ())); + + String base_ret; + Boolean base_same (true); + + if (tiein && hb) + { + SemanticGraph::Type& base (c.inherits ().base ()); + + base_ret = ret_type (base); + base_same = base.inherits_p () && + base_ret == ret_type (base.inherits ().base ()); + } + + if (he || ha || same || ret == L"void" || poly_code || + (tiein && !(base_same || base_ret == L"void"))) + { + os << "// " << name << endl + << "//" << endl + << endl; + } + + // Member callbacks. + // + if (!restriction) + { + if (ha) + names (c, names_attribute_callback_); + } + + if (!restriction || validation) + { + if (he || hae) + contains_compositor (c, contains_compositor_callback_); + } + + // post + // + if (same || ret == L"void") + { + String const& post (post_name (c)); + + os << ret << " " << name << "::" << endl + << post << " ()" + << "{"; + + if (tiein) + { + String const& impl (etiein (c)); + + os << "if (this->" << impl << ")" << endl + << (ret != L"void" ? "return " : "") << "this->" << + impl << "->" << post << " ();"; + } + + if (same) + { + if (tiein) + os << "else" << endl; + + SemanticGraph::Type& base (c.inherits ().base ()); + + if (ret == L"void") + os << post_name (base) << " ();"; + else + os << "return " << post_name (base) << " ();"; + } + + os << "}"; + } + + // reset + // + if (!restriction && (he || ha) && reset) + { + os << "void " << name << "::" << endl + << "_reset ()" + << "{"; + + // Avoid recursion in case of recursive parsing. + // + if (he) + os << "if (this->resetting_)" << endl + << "return;" + << endl; + + // Reset the base. We cannot use the fully-qualified base name + // directly because of some broken compilers (EVC 4.0). + // + String base (unclash (name, "base")); + + os << "typedef "; + + if (hb) + os << fq_name (c.inherits ().base ()); + else + os << complex_base; + + os << " " << base << ";" + << base << "::_reset ();" + << endl; + + // Reset validation state. + // + if (validation) + { + if (he || hae) + { + os << "this->v_state_stack_.clear ();" + << endl; + + SemanticGraph::Compositor& comp ( + c.contains_compositor ().compositor ()); + + if (comp.is_a () && + comp.context().count ("p:comp-number")) + { + os << "this->v_all_count_.clear ();" + << endl; + } + } + + if (hra) + os << "this->v_state_attr_stack_.clear ();" + << endl; + } + + // Reset member parsers. + // + + if (ha) + names (c, names_attribute_reset_); + + if (he) + { + os << "this->resetting_ = true;" + << endl; + + contains_compositor (c, contains_compositor_reset_); + + os << "this->resetting_ = false;" + << endl; + } + + os << "}"; + } + + // + // + if (poly_code) + { + String id (c.name ()); + + if (String ns = xml_ns_name (c)) + { + id += L' '; + id += ns; + } + + os << "const char* " << name << "::" << endl + << "_static_type ()" + << "{" + << "return " << strlit (id) << ";" + << "}"; + + os << "const char* " << name << "::" << endl + << "_dynamic_type () const" + << "{" + << "return _static_type ();" + << "}"; + + if (hb && validation) + { + Boolean gen (!anonymous (c)); + + // We normally don't need to enter anonymous types into + // the inheritance map. The only exception is when an + // anonymous types is defined inside an element that + // is a member of a substitution group. + // + if (!gen) + { + // The first instance that this anonymous type classifies + // is the prototype for others if any. + // + SemanticGraph::Instance& i ( + c.classifies_begin ()->instance ()); + + if (SemanticGraph::Element* e = + dynamic_cast (&i)) + { + if (e->substitutes_p ()) + gen = true; + } + } + + if (gen) + { + SemanticGraph::Type& base (c.inherits ().base ()); + + os << "static" << endl + << "const ::xsde::cxx::parser::validating::" << + "inheritance_map_entry" << endl + << "_xsde_" << name << "_inheritance_map_entry_ (" << endl + << name << "::_static_type ()," << endl + << fq_name (base) << "::_static_type ());" + << endl; + } + } + } + + // Base post + // + if (tiein && !(base_same || base_ret == L"void")) + { + SemanticGraph::Type& base (c.inherits ().base ()); + + String const& impl (etiein (base)); + String const& base_post (post_name (base)); + + os << base_ret << " " << name << "::" << endl + << base_post << " ()" + << "{" + << "assert (this->" << impl << ");" + << "return this->" << impl << "->" << base_post << " ();" + << "}"; + } + + // The rest is parsing/validation code which is generated in + // *-validation-source.cxx. + // + if (validation) + return; + + // Don't use the restriction_p result from here since we don't + // want special treatment of anyType. + // + restriction = hb && c.inherits ().is_a (); + + // _start_element_impl & _end_element_impl + // + if (he) + { + // _start_element_impl + // + + os << "bool " << name << "::" << endl + << "_start_element_impl (const " << string_type << "& ns," << endl + << "const " << string_type << "& n"; + + if (poly_runtime) + os << "," << endl + << "const char*" << (poly_code ? " t" : ""); + + os << ")" + << "{"; + + if (poly_code) + os << "XSDE_UNUSED (t);" + << endl; + + if (!restriction) + { + // We cannot use the fully-qualified base name directly + // because of some broken compilers (EVC 4.0). + // + String base (unclash (name, "base")); + + os << "typedef "; + + if (hb) + os << fq_name (c.inherits ().base ()); + else + os << complex_base; + + os << " " << base << ";" + << "if (" << base << "::"; + + if (poly_runtime) + os << "_start_element_impl (ns, n, " << + (poly_code ? "t" : "0") << "))" << endl; + else + os << "_start_element_impl (ns, n))" << endl; + + os << "return true;" + << endl; + } + + os << "::xsde::cxx::parser::context& ctx = this->_context ();" + << endl; + + contains_compositor (c, contains_compositor_start_); + + os << "return false;" + << "}"; + + + // _end_element_impl + // + os << "bool " << name << "::" << endl + << "_end_element_impl (const " << string_type << "& ns," << endl + << "const " << string_type << "& n)" + << "{"; + + + if (!restriction) + { + // We cannot use the fully-qualified base name directly + // because of some broken compilers (EVC 4.0). + // + String base (unclash (name, "base")); + + os << "typedef "; + + if (hb) + os << fq_name (c.inherits ().base ()); + else + os << complex_base; + + os << " " << base << ";" + << "if (" << base << "::_end_element_impl (ns, n))" << endl + << "return true;" + << endl; + } + + if (!exceptions) + os << "::xsde::cxx::parser::context& ctx = this->_context ();" + << endl; + + contains_compositor (c, contains_compositor_end_); + + os << "return false;" + << "}"; + } + + + if (ha) + { + // _attribute_impl + // + + os << "bool " << name << "::" << endl + << "_attribute_impl (const " << string_type << "& ns," << endl + << "const " << string_type << "& n," << endl + << "const " << string_type << "& v)" + << "{"; + + if (!restriction) + { + // We cannot use the fully-qualified base name directly + // because of some broken compilers (EVC 4.0). + // + String base (unclash (name, "base")); + + os << "typedef "; + + if (hb) + os << fq_name (c.inherits ().base ()); + else + os << complex_base; + + os << " " << base << ";" + << "if (" << base << "::_attribute_impl (ns, n, v))" + << "{" + << "return true;" + << "}"; + } + + os << "::xsde::cxx::parser::context& ctx = this->_context ();" + << endl; + + names (c, names_attribute_); + + os << "return false;" + << "}"; + } + } + + private: + // + // + CompositorCallback compositor_callback_val_; + Traversal::Compositor compositor_callback_non_val_; + ParticleCallback particle_callback_; + Traversal::ContainsCompositor contains_compositor_callback_; + Traversal::ContainsParticle contains_particle_callback_; + + AttributeCallback attribute_callback_; + Traversal::Names names_attribute_callback_; + + // + // + Traversal::Compositor compositor_reset_; + ParticleReset particle_reset_; + Traversal::ContainsCompositor contains_compositor_reset_; + Traversal::ContainsParticle contains_particle_reset_; + + AttributeReset attribute_reset_; + Traversal::Names names_attribute_reset_; + + // + // + Traversal::Compositor start_compositor_; + StartElement start_element_; + Traversal::ContainsCompositor contains_compositor_start_; + Traversal::ContainsParticle contains_particle_start_; + + // + // + Traversal::Compositor end_compositor_; + EndElement end_element_; + Traversal::ContainsCompositor contains_compositor_end_; + Traversal::ContainsParticle contains_particle_end_; + + // + // + Attribute attribute_; + Traversal::Names names_attribute_; + }; + + // Generate substitution group map entries. + // + struct GlobalElement: Traversal::Element, Context + { + GlobalElement (Context& c) + : Context (c) + { + } + + virtual Void + traverse (Type& e) + { + if (e.substitutes_p ()) + { + String name (escape (e.name ())); + Type& r (e.substitutes ().root ()); + + SemanticGraph::Type& type (e.type ()); + + String m_id (e.name ()); + String r_id (r.name ()); + + if (String const& ns = e.namespace_ ().name ()) + { + m_id += L' '; + m_id += ns; + } + + if (String const& ns = r.namespace_ ().name ()) + { + r_id += L' '; + r_id += ns; + } + + os << "// Substitution map entry for " << comment (e.name ()) << + "." << endl + << "//" << endl + << "static" << endl + << "const ::xsde::cxx::parser::substitution_map_entry" << endl + << "_xsde_" << name << "_substitution_map_entry_ (" << endl + << strlit (m_id) << "," << endl + << strlit (r_id) << "," << endl + << fq_name (type) << "::_static_type ());" + << endl; + } + } + }; + } + + Void + generate_parser_source (Context& ctx) + { + if (ctx.tiein) + ctx.os << "#include " << endl + << endl; + + if (ctx.poly_code) + { + ctx.os << "#include " << endl + << "#include " << endl; + + if (ctx.validation) + ctx.os << "#include " << endl + << endl; + else + ctx.os << endl; + + ctx.os << "static" << endl + << "const ::xsde::cxx::parser::substitution_map_init" << endl + << "_xsde_substitution_map_init_;" + << endl; + + if (ctx.validation) + { + ctx.os << "static" << endl + << "const ::xsde::cxx::parser::validating::" << + "inheritance_map_init" << endl + << "_xsde_inheritance_map_init_;" + << endl; + } + } + + // Emit "weak" header includes that are used in the file-per-type + // compilation model. + // + if (ctx.options.value ()) + { + Traversal::Schema schema; + Includes includes (ctx, Includes::source); + + schema >> includes; + schema.dispatch (ctx.schema_root); + } + + Traversal::Schema schema; + + Traversal::Sources sources; + Traversal::Names schema_names; + + Namespace ns (ctx); + Traversal::Names names; + + schema >> sources >> schema; + schema >> schema_names >> ns >> names; + + List list (ctx); + Union union_ (ctx); + Complex complex (ctx); + Enumeration enumeration (ctx); + GlobalElement global_element (ctx); + + names >> list; + names >> union_; + names >> complex; + names >> enumeration; + + if (ctx.poly_code) + names >> global_element; + + schema.dispatch (ctx.schema_root); + } + } +} diff --git a/xsde/cxx/parser/parser-source.hxx b/xsde/cxx/parser/parser-source.hxx new file mode 100644 index 0000000..dcf07d6 --- /dev/null +++ b/xsde/cxx/parser/parser-source.hxx @@ -0,0 +1,22 @@ +// file : xsde/cxx/parser/parser-source.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef CXX_PARSER_PARSER_SOURCE_HXX +#define CXX_PARSER_PARSER_SOURCE_HXX + +#include + +#include + +namespace CXX +{ + namespace Parser + { + Void + generate_parser_source (Context&); + } +} + +#endif // CXX_PARSER_PARSER_SOURCE_HXX diff --git a/xsde/cxx/parser/print-impl-common.hxx b/xsde/cxx/parser/print-impl-common.hxx new file mode 100644 index 0000000..1815bb7 --- /dev/null +++ b/xsde/cxx/parser/print-impl-common.hxx @@ -0,0 +1,1063 @@ +// file : xsde/cxx/parser/print-impl-common.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef CXX_PARSER_PRINT_IMPL_COMMON_HXX +#define CXX_PARSER_PRINT_IMPL_COMMON_HXX + +#include +#include + +#include + +namespace CXX +{ + namespace Parser + { + struct PrintCall: Traversal::Type, + + Traversal::Fundamental::Boolean, + + Traversal::Fundamental::Byte, + Traversal::Fundamental::UnsignedByte, + Traversal::Fundamental::Short, + Traversal::Fundamental::UnsignedShort, + Traversal::Fundamental::Int, + Traversal::Fundamental::UnsignedInt, + Traversal::Fundamental::Long, + Traversal::Fundamental::UnsignedLong, + Traversal::Fundamental::Integer, + Traversal::Fundamental::NonPositiveInteger, + Traversal::Fundamental::NonNegativeInteger, + Traversal::Fundamental::PositiveInteger, + Traversal::Fundamental::NegativeInteger, + + Traversal::Fundamental::Float, + Traversal::Fundamental::Double, + Traversal::Fundamental::Decimal, + + Traversal::Fundamental::String, + Traversal::Fundamental::NormalizedString, + Traversal::Fundamental::Token, + Traversal::Fundamental::Name, + Traversal::Fundamental::NameToken, + Traversal::Fundamental::NameTokens, + Traversal::Fundamental::NCName, + Traversal::Fundamental::Language, + + Traversal::Fundamental::QName, + + Traversal::Fundamental::Id, + Traversal::Fundamental::IdRef, + Traversal::Fundamental::IdRefs, + + Traversal::Fundamental::AnyURI, + + Traversal::Fundamental::Base64Binary, + Traversal::Fundamental::HexBinary, + + Traversal::Fundamental::Date, + Traversal::Fundamental::DateTime, + Traversal::Fundamental::Duration, + Traversal::Fundamental::Day, + Traversal::Fundamental::Month, + Traversal::Fundamental::MonthDay, + Traversal::Fundamental::Year, + Traversal::Fundamental::YearMonth, + Traversal::Fundamental::Time, + + Context + { + PrintCall (Context& c, String const& tag, String const& arg) + : Context (c), tag_ (tag), arg_ (arg) + { + } + + virtual Void + traverse (SemanticGraph::Type&) + { + gen_user_type (); + } + + // Boolean. + // + virtual Void + traverse (SemanticGraph::Fundamental::Boolean& t) + { + if (default_type (t, "bool")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %u\n") << ", " << + arg_ << ");"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << " << std::endl;"; + } + else + gen_user_type (); + } + + // Integral types. + // + virtual Void + traverse (SemanticGraph::Fundamental::Byte& t) + { + if (default_type (t, "signed char")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %d\n") << ", " << + arg_ << ");"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + "static_cast (" << arg_ << ") << std::endl;"; + } + else + gen_user_type (); + } + + virtual Void + traverse (SemanticGraph::Fundamental::UnsignedByte& t) + { + if (default_type (t, "unsigned char")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %u\n") << ", " << + arg_ << ");"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + "static_cast (" << arg_ << ") << std::endl;"; + } + else + gen_user_type (); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Short& t) + { + if (default_type (t, "short")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %d\n") << ", " << + arg_ << ");"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << " << std::endl;"; + } + else + gen_user_type (); + } + + virtual Void + traverse (SemanticGraph::Fundamental::UnsignedShort& t) + { + if (default_type (t, "unsigned short")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %u\n") << ", " << + arg_ << ");"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << " << std::endl;"; + } + else + gen_user_type (); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Int& t) + { + if (default_type (t, "int")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %d\n") << ", " << + arg_ << ");"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << " << std::endl;"; + } + else + gen_user_type (); + } + + virtual Void + traverse (SemanticGraph::Fundamental::UnsignedInt& t) + { + if (default_type (t, "unsigned int")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %u\n") << ", " << + arg_ << ");"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << " << std::endl;"; + } + else + gen_user_type (); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Long& t) + { + if (options.value ()) + { + if (default_type (t, "long")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %ld\n") << ", " << + arg_ << ");"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << " << std::endl;"; + } + else + gen_user_type (); + } + else + { + if (default_type (t, "long long")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %lld\n") << ", " << + arg_ << ");"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << " << std::endl;"; + } + else + gen_user_type (); + } + } + + virtual Void + traverse (SemanticGraph::Fundamental::UnsignedLong& t) + { + if (options.value ()) + { + if (default_type (t, "unsigned long")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %lu\n") << ", " << + arg_ << ");"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << " << std::endl;"; + } + else + gen_user_type (); + } + else + { + if (default_type (t, "unsigned long long")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %llu\n") << ", " << + arg_ << ");"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << " << std::endl;"; + } + else + gen_user_type (); + } + } + + virtual Void + traverse (SemanticGraph::Fundamental::Integer& t) + { + if (default_type (t, "long")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %ld\n") << ", " << + arg_ << ");"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << " << std::endl;"; + } + else + gen_user_type (); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NegativeInteger& t) + { + if (default_type (t, "long")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %ld\n") << ", " << + arg_ << ");"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << " << std::endl;"; + } + else + gen_user_type (); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NonPositiveInteger& t) + { + if (default_type (t, "long")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %ld\n") << ", " << + arg_ << ");"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << " << std::endl;"; + } + else + gen_user_type (); + } + + virtual Void + traverse (SemanticGraph::Fundamental::PositiveInteger& t) + { + if (default_type (t, "unsigned long")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %lu\n") << ", " << + arg_ << ");"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << " << std::endl;"; + } + else + gen_user_type (); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NonNegativeInteger& t) + { + if (default_type (t, "unsigned long")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %lu\n") << ", " << + arg_ << ");"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << " << std::endl;"; + } + else + gen_user_type (); + } + + // Floats. + // + virtual Void + traverse (SemanticGraph::Fundamental::Float& t) + { + if (default_type (t, "float")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %g\n") << ", " << + arg_ << ");"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << " << std::endl;"; + } + else + gen_user_type (); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Double& t) + { + if (default_type (t, "double")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %g\n") << ", " << + arg_ << ");"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << " << std::endl;"; + } + else + gen_user_type (); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Decimal& t) + { + if (default_type (t, "double")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %g\n") << ", " << + arg_ << ");"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << " << std::endl;"; + } + else + gen_user_type (); + } + + // Strings. + // + virtual Void + traverse (SemanticGraph::Fundamental::String& t) + { + gen_string (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NormalizedString& t) + { + gen_string (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Token& t) + { + gen_string (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NameToken& t) + { + gen_string (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Name& t) + { + gen_string (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NCName& t) + { + gen_string (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Language& t) + { + gen_string (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Id& t) + { + gen_string (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::IdRef& t) + { + gen_string (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::AnyURI& t) + { + gen_string (t); + } + + // String sequences. + // + + virtual Void + traverse (SemanticGraph::Fundamental::NameTokens& t) + { + gen_sequence (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::IdRefs& t) + { + gen_sequence (t); + } + + // QName + // + + virtual Void + traverse (SemanticGraph::Fundamental::QName& t) + { + if (options.value ()) + { + if (default_type (t, xs_ns_name () + L"::qname*")) + { + if (options.value ()) + os << "if (" << arg_ << "->prefix ()[0] == '\\0')" << endl + << "printf (" << strlit (tag_ + L": %s\n") << ", " << + arg_ << "->name ());" + << "else" << endl + << "printf (" << strlit (tag_ + L": %s:%s\n") << "," << endl + << arg_ << "->prefix ()," << endl + << arg_ << "->name ());"; + else + os << "if (" << arg_ << "->prefix ()[0] == '\\0')" << endl + << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << "->name () << std::endl;" + << "else" << endl + << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << "->prefix ()" << endl + << " << ':' << " << arg_ << "->name () << std::endl;"; + } + else + gen_user_type (); + } + else + { + if (default_type (t, xs_ns_name () + L"::qname")) + { + if (options.value ()) + os << "if (" << arg_ << ".prefix ().empty ())" << endl + << "printf (" << strlit (tag_ + L": %s\n") << ", " << + arg_ << ".name ().c_str ());" + << "else" << endl + << "printf (" << strlit (tag_ + L": %s:%s\n") << "," << endl + << arg_ << ".prefix ().c_str ()," << endl + << arg_ << ".name ().c_str ());"; + else + os << "if (" << arg_ << ".prefix ().empty ())" << endl + << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << ".name () << std::endl;" + << "else" << endl + << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << ".prefix ()" << endl + << " << ':' << " << arg_ << ".name () << std::endl;"; + } + else + gen_user_type (); + } + } + + // Binary. + // + virtual Void + traverse (SemanticGraph::Fundamental::Base64Binary& t) + { + gen_buffer (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::HexBinary& t) + { + gen_buffer (t); + } + + // Date/time. + // + virtual Void + traverse (SemanticGraph::Fundamental::Date& t) + { + if (default_type (t, xs_ns_name () + L"::date")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %d-%u-%u") << "," << endl + << arg_ << ".year ()," << endl + << arg_ << ".month ()," << endl + << arg_ << ".day ());" << endl; + else + os << "std::cout << " << strlit (tag_ + L": ") << endl + << " << " << arg_ << ".year () << '-'" << endl + << " << " << arg_ << ".month () << '-'" << endl + << " << " << arg_ << ".day ();"; + + gen_time_zone (); + } + else + gen_user_type (); + } + + virtual Void + traverse (SemanticGraph::Fundamental::DateTime& t) + { + if (default_type (t, xs_ns_name () + L"::date_time")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %d-%u-%uT%u:%u:%g") << + "," << endl + << arg_ << ".year ()," << endl + << arg_ << ".month ()," << endl + << arg_ << ".day ()," << endl + << arg_ << ".hours ()," << endl + << arg_ << ".minutes ()," << endl + << arg_ << ".seconds ());"; + else + os << "std::cout << " << strlit (tag_ + L": ") << endl + << " << " << arg_ << ".year () << '-'" << endl + << " << " << arg_ << ".month () << '-'" << endl + << " << " << arg_ << ".day () << 'T'" << endl + << " << " << arg_ << ".hours () << ':'" << endl + << " << " << arg_ << ".minutes () << ':'" << endl + << " << " << arg_ << ".seconds ();"; + + gen_time_zone (); + } + else + gen_user_type (); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Duration& t) + { + if (default_type (t, xs_ns_name () + L"::duration")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": ") << ");" + << endl + << "if (" << arg_ << ".negative ())" << endl + << "printf (\"-\");" + << endl + << "printf (\"P%uY%uM%uDT%uH%uM%gS\\n\"," << endl + << arg_ << ".years ()," << endl + << arg_ << ".months ()," << endl + << arg_ << ".days ()," << endl + << arg_ << ".hours ()," << endl + << arg_ << ".minutes ()," << endl + << arg_ << ".seconds ());"; + else + os << "std::cout << " << strlit (tag_ + L": ") << ";" + << endl + << "if (" << arg_ << ".negative ())" << endl + << "std::cout << '-';" + << endl + << "std::cout << 'P'" << endl + << " << " << arg_ << ".years () << 'Y'" << endl + << " << " << arg_ << ".months () << 'M'" << endl + << " << " << arg_ << ".days () << \"DT\"" << endl + << " << " << arg_ << ".hours () << 'H'" << endl + << " << " << arg_ << ".minutes () << 'M'" << endl + << " << " << arg_ << ".seconds () << 'S'" + << " << std::endl;"; + } + else + gen_user_type (); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Day& t) + { + if (default_type (t, xs_ns_name () + L"::gday")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": ---%u") << ", " << + arg_ << ".day ());"; + else + os << "std::cout << " << strlit (tag_ + L": ---") << + " << " << arg_ << ".day ();"; + + gen_time_zone (); + } + else + gen_user_type (); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Month& t) + { + if (default_type (t, xs_ns_name () + L"::gmonth")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": --%u") << ", " << + arg_ << ".month ());"; + else + os << "std::cout << " << strlit (tag_ + L": --") << + " << " << arg_ << ".month ();"; + + gen_time_zone (); + } + else + gen_user_type (); + } + + virtual Void + traverse (SemanticGraph::Fundamental::MonthDay& t) + { + if (default_type (t, xs_ns_name () + L"::gmonth_day")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": --%u-%u") << "," << endl + << arg_ << ".month ()," << endl + << arg_ << ".day ());"; + else + os << "std::cout << " << strlit (tag_ + L": --") << endl + << " << " << arg_ << ".month () << '-'" << endl + << " << " << arg_ << ".day ();"; + + gen_time_zone (); + } + else + gen_user_type (); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Year& t) + { + if (default_type (t, xs_ns_name () + L"::gyear")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %d") << ", " << + arg_ << ".year ());"; + else + os << "std::cout << " << strlit (tag_ + L": ") << + " << " << arg_ << ".year ();"; + + gen_time_zone (); + } + else + gen_user_type (); + } + + virtual Void + traverse (SemanticGraph::Fundamental::YearMonth& t) + { + if (default_type (t, xs_ns_name () + L"::gyear_month")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %d-%u") << "," << endl + << arg_ << ".year ()," << endl + << arg_ << ".month ());"; + else + os << "std::cout << " << strlit (tag_ + L": ") << endl + << " << " << arg_ << ".year () << '-'" << endl + << " << " << arg_ << ".month ();"; + + gen_time_zone (); + } + else + gen_user_type (); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Time& t) + { + if (default_type (t, xs_ns_name () + L"::time")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %u:%u:%g") << "," << endl + << arg_ << ".hours ()," << endl + << arg_ << ".minutes ()," << endl + << arg_ << ".seconds ());"; + else + os << "std::cout << " << strlit (tag_ + L": ") << endl + << " << " << arg_ << ".hours () << ':'" << endl + << " << " << arg_ << ".minutes () << ':'" << endl + << " << " << arg_ << ".seconds ();"; + + gen_time_zone (); + } + else + gen_user_type (); + } + + private: + bool + default_type (SemanticGraph::Type& t, String const& def_type) + { + return ret_type (t) == def_type; + } + + void + gen_user_type () + { + os << "// TODO" << endl + << "//" << endl; + } + + void + gen_string (SemanticGraph::Type& t) + { + if (options.value ()) + { + if (default_type (t, "char*")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %s\n") << ", " << + arg_ << ");"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << " << std::endl;"; + } + else + gen_user_type (); + } + else + { + if (default_type (t, "::std::string")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %s\n") << ", " << + arg_ << ".c_str ());"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << " << std::endl;"; + } + else + gen_user_type (); + } + } + + void + gen_sequence (SemanticGraph::Type& t) + { + String type (xs_ns_name () + L"::string_sequence"); + + if (default_type (t, type + L"*")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": ") << ");" + << endl; + else + os << "std::cout << " << strlit (tag_ + L": ") << ";" + << endl; + + os << "for (" << type << "::const_iterator i (" << arg_ << + "->begin ()), e (" << arg_ << "->end ());" << endl + << "i != e;)" + << "{"; + + if (options.value ()) + { + if (options.value ()) + os << "printf (\"%s\", *i++);"; + else + os << "printf (\"%s\", (i++)->c_str ());"; + + os << "if (i != e)" << endl + << "printf (\" \");" + << "}" + << "printf (\"\\n\");"; + } + else + os << "std::cout << *i++;" + << "if (i != e)" << endl + << "std::cout << ' ';" + << "}" + << "std::cout << std::endl;"; + } + else + gen_user_type (); + } + + void + gen_buffer (SemanticGraph::Type& t) + { + if (default_type (t, xs_ns_name () + L"::buffer*")) + { + if (options.value ()) + os << "printf (" << strlit (tag_ + L": %zu bytes\n") << ", " << + arg_ << "->size ());"; + else + os << "std::cout << " << strlit (tag_ + L": ") << " << " << + arg_ << "->size () << \" bytes\" << std::endl;"; + } + else + gen_user_type (); + } + + void + gen_time_zone () + { + os << endl + << "if (" << arg_ << ".zone_present ())" + << "{"; + + if (options.value ()) + os << "if (" << arg_ << ".zone_hours () < 0)" << endl + << "printf (\"%d:%d\", " << arg_ << ".zone_hours (), -" << + arg_ << ".zone_minutes ());" + << "else" << endl + << "printf (\"+%d:%d\", " << arg_ << ".zone_hours (), " << + arg_ << ".zone_minutes ());"; + else + os << "if (" << arg_ << ".zone_hours () < 0)" << endl + << "std::cout << " << arg_ << ".zone_hours () << ':' << -" << + arg_ << ".zone_minutes ();" + << "else" << endl + << "std::cout << '+' << " << arg_ << ".zone_hours () << " << + "':' << " << arg_ << ".zone_minutes ();"; + + os << "}"; + + if (options.value ()) + os << "printf (\"\\n\");"; + else + os << "std::cout << std::endl;"; + } + + private: + String tag_; + String arg_; + }; + + struct DeleteCall: Traversal::Fundamental::String, + Traversal::Fundamental::NormalizedString, + Traversal::Fundamental::Token, + Traversal::Fundamental::Name, + Traversal::Fundamental::NameToken, + Traversal::Fundamental::NameTokens, + Traversal::Fundamental::NCName, + Traversal::Fundamental::Language, + + Traversal::Fundamental::QName, + + Traversal::Fundamental::Id, + Traversal::Fundamental::IdRef, + Traversal::Fundamental::IdRefs, + + Traversal::Fundamental::AnyURI, + + Traversal::Fundamental::Base64Binary, + Traversal::Fundamental::HexBinary, + + Context + { + DeleteCall (Context& c, String const& arg) + : Context (c), arg_ (arg) + { + } + + // Strings. + // + virtual Void + traverse (SemanticGraph::Fundamental::String& t) + { + gen_string (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NormalizedString& t) + { + gen_string (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Token& t) + { + gen_string (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NameToken& t) + { + gen_string (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Name& t) + { + gen_string (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::NCName& t) + { + gen_string (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Language& t) + { + gen_string (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::Id& t) + { + gen_string (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::IdRef& t) + { + gen_string (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::AnyURI& t) + { + gen_string (t); + } + + // String sequences. + // + + virtual Void + traverse (SemanticGraph::Fundamental::NameTokens& t) + { + gen_sequence (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::IdRefs& t) + { + gen_sequence (t); + } + + // QName + // + + virtual Void + traverse (SemanticGraph::Fundamental::QName& t) + { + if (options.value () && + default_type (t, xs_ns_name () + L"::qname*")) + { + os << endl + << "delete " << arg_ << ";"; + } + } + + // Binary. + // + virtual Void + traverse (SemanticGraph::Fundamental::Base64Binary& t) + { + gen_buffer (t); + } + + virtual Void + traverse (SemanticGraph::Fundamental::HexBinary& t) + { + gen_buffer (t); + } + + private: + bool + default_type (SemanticGraph::Type& t, String const& def_type) + { + return ret_type (t) == def_type; + } + + void + gen_string (SemanticGraph::Type& t) + { + if (options.value () && default_type (t, "char*")) + { + os << endl + << "delete[] " << arg_ << ";"; + } + } + + void + gen_sequence (SemanticGraph::Type& t) + { + if (default_type (t, xs_ns_name () + L"::string_sequence*")) + { + os << endl + << "delete " << arg_ << ";"; + } + } + + void + gen_buffer (SemanticGraph::Type& t) + { + if (default_type (t, xs_ns_name () + L"::buffer*")) + { + os << endl + << "delete " << arg_ << ";"; + } + } + + private: + String arg_; + }; + } +} + +#endif // CXX_PARSER_PRINT_IMPL_COMMON_HXX diff --git a/xsde/cxx/parser/state-processor.cxx b/xsde/cxx/parser/state-processor.cxx new file mode 100644 index 0000000..3ecc77a --- /dev/null +++ b/xsde/cxx/parser/state-processor.cxx @@ -0,0 +1,319 @@ +// file : xsde/cxx/parser/state-processor.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2006-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include + +#include + +#include +#include + +#include + +#include + +namespace CXX +{ + namespace Parser + { + namespace + { + typedef Cult::Containers::Vector Particles; + + void + print (Particles const& p) + { + using std::wcerr; + using std::endl; + + wcerr << "prefixes: " << endl; + + for (Particles::ConstIterator i (p.begin ()); i != p.end (); ++i) + { + if (SemanticGraph::Element* e = + dynamic_cast (*i)) + { + wcerr << e->name () << endl; + } + else + { + wcerr << "" << endl; + } + } + + wcerr << endl; + } + + // + // + struct Particle: Traversal::All, + Traversal::Choice, + Traversal::Sequence + { + Particle (UnsignedLong& all, + UnsignedLong& choice, + UnsignedLong& sequence, + UnsignedLong& depth) + : all_ (all), + choice_ (choice), + sequence_ (sequence), + depth_ (depth) + { + } + + virtual Void + traverse (SemanticGraph::All& a) + { + using SemanticGraph::Compositor; + + // Go over particles, collecting "prefix" particles in prefixes_, + // assigning state numbers and calculating effective minOccurs. + // If all prefixes of this compositor have minOccurs = 0, then + // the compositor itself effectively has minOccurs = 0 regardless + // of the actual value specified in the schema. + // + // Note that we don't need to care about depth since the 'all' + // compositor cannot contain any nested compositors. + // + + UnsignedLong state (0); + UnsignedLong min (0); + + for (Compositor::ContainsIterator ci (a.contains_begin ()); + ci != a.contains_end (); ++ci) + { + SemanticGraph::Particle& p (ci->particle ()); + + // The 'all' compositor can only include elements. + // + prefixes_.push_back (&p); + + if (min == 0 && ci->min () != 0) + min = 1; + + p.context ().set ("p:prefix", true); + p.context ().set ("p:state", state++); + } + + if (!prefixes_.empty ()) + { + a.context ().set ("p:comp-number", choice_++); + a.context ().set ("p:prefixes", prefixes_); + a.context ().set ("p:state-count", + UnsignedLong (prefixes_.size ())); + + // effective-min = min * actual-min + // + if (min == 1) + min = a.min (); + + a.context ().set ("p:effective-min", min); + + // print (prefixes_); + } + } + + virtual Void + traverse (SemanticGraph::Choice& c) + { + using SemanticGraph::Compositor; + + // Go over particles, collecting "prefix" particles in prefixes_, + // assigning state numbers and calculating effective minOccurs. + // If any prefix of this compositor have minOccurs = 0, then the + // compositor itself effectively has minOccurs = 0 regardless of + // the actual value specified in the schema. + // + + UnsignedLong state (0); + UnsignedLong min (1); + + for (Compositor::ContainsIterator ci (c.contains_begin ()); + ci != c.contains_end (); ++ci) + { + SemanticGraph::Particle& p (ci->particle ()); + + if (p.is_a () || + p.is_a ()) + { + prefixes_.push_back (&p); + + if (min == 1 && ci->min () == 0) + min = 0; + } + else + { + UnsignedLong depth (0); + Particle t (all_, choice_, sequence_, depth); + t.dispatch (p); + + if (t.prefixes_.empty ()) + continue; // Skip empty compositors. + + if (++depth > depth_) // One for this compositor. + depth_ = depth; + + prefixes_.insert (prefixes_.end (), + t.prefixes_.begin ().base (), + t.prefixes_.end ().base ()); + + if (min == 1 && + p.context ().get ("p:effective-min") == 0) + min = 0; + } + + p.context ().set ("p:prefix", true); + p.context ().set ("p:state", state++); + } + + if (!prefixes_.empty ()) + { + c.context ().set ("p:comp-number", choice_++); + c.context ().set ("p:prefixes", prefixes_); + + // effective-min = min * actual-min + // + if (min == 1) + min = c.min (); + + c.context ().set ("p:effective-min", min); + + // print (prefixes_); + } + } + + virtual Void + traverse (SemanticGraph::Sequence& s) + { + using SemanticGraph::Compositor; + + // Go over particles, collecting "prefix" particles in prefixes_, + // assigning state numbers and calculating effective minOccurs. + // If all prefixes of this compositor have minOccurs = 0, then + // the compositor itself effectively has minOccurs = 0 regardless + // of the actual value specified in the schema. + // + + Boolean prefix (true); + UnsignedLong state (0); + UnsignedLong min (0); + + for (Compositor::ContainsIterator ci (s.contains_begin ()); + ci != s.contains_end (); ++ci) + { + SemanticGraph::Particle& p (ci->particle ()); + + if (p.is_a () || + p.is_a ()) + { + if (prefix) + { + prefixes_.push_back (&p); + + if (ci->min () != 0) + min = 1; + } + } + else + { + UnsignedLong depth (0); + Particle t (all_, choice_, sequence_, depth); + t.dispatch (p); + + if (t.prefixes_.empty ()) + continue; // Skip empty compositors. + + if (++depth > depth_) // One for this compositor. + depth_ = depth; + + if (prefix) + { + prefixes_.insert (prefixes_.end (), + t.prefixes_.begin ().base (), + t.prefixes_.end ().base ()); + + if (p.context ().get ("p:effective-min") != 0) + min = 1; + } + } + + p.context ().set ("p:state", state++); + + if (prefix) + p.context ().set ("p:prefix", true); + + if (prefix && min != 0) + prefix = false; + } + + if (!prefixes_.empty ()) + { + s.context ().set ("p:comp-number", sequence_++); + s.context ().set ("p:prefixes", prefixes_); + + // effective-min = min * actual-min + // + if (min == 1) + min = s.min (); + + s.context ().set ("p:effective-min", min); + + // print (prefixes_); + } + } + + private: + Particles prefixes_; + + UnsignedLong& all_; + UnsignedLong& choice_; + UnsignedLong& sequence_; + + UnsignedLong& depth_; + }; + + + // + // + struct Complex: Traversal::Complex + { + virtual Void + traverse (Type& c) + { + if (c.contains_compositor_p ()) + { + UnsignedLong all (0), choice (0), sequence (0), depth (0); + Particle t (all, choice, sequence, depth); + t.dispatch (c.contains_compositor ().compositor ()); + + // Set the maximum stack depth for this type. Used to + // allocate fixed-size state stack. + // + c.context ().set ("p:depth", depth + 1); + } + } + }; + } + + Void StateProcessor:: + process (SemanticGraph::Schema& tu, SemanticGraph::Path const&) + { + Traversal::Schema schema; + Traversal::Sources sources; + Traversal::Names schema_names; + Traversal::Namespace ns; + Traversal::Names ns_names; + + schema >> sources >> schema; + schema >> schema_names >> ns >> ns_names; + + Complex complex_type; + + ns_names >> complex_type; + + schema.dispatch (tu); + } + } +} diff --git a/xsde/cxx/parser/state-processor.hxx b/xsde/cxx/parser/state-processor.hxx new file mode 100644 index 0000000..c276d54 --- /dev/null +++ b/xsde/cxx/parser/state-processor.hxx @@ -0,0 +1,28 @@ +// file : xsde/cxx/parser/state-processor.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2006-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef CXX_PARSER_STATE_PROCESSOR_HXX +#define CXX_PARSER_STATE_PROCESSOR_HXX + +#include +#include + +namespace CXX +{ + namespace Parser + { + using namespace Cult::Types; + + class StateProcessor + { + public: + Void + process (XSDFrontend::SemanticGraph::Schema&, + XSDFrontend::SemanticGraph::Path const& file); + }; + } +} + +#endif // CXX_PARSER_STATE_PROCESSOR_HXX diff --git a/xsde/cxx/parser/type-processor.cxx b/xsde/cxx/parser/type-processor.cxx new file mode 100644 index 0000000..4e7f65a --- /dev/null +++ b/xsde/cxx/parser/type-processor.cxx @@ -0,0 +1,352 @@ +// file : xsde/cxx/parser/type-processor.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2006-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include + +#include +#include + +#include +#include + +namespace CXX +{ + namespace Parser + { + namespace + { + // + // + struct Type: Traversal::Type + { + Type (SemanticGraph::Schema& schema, + TypeMap::Namespaces& type_map, + Boolean add_includes) + : schema_ (schema), + type_map_ (type_map), + add_includes_ (add_includes) + { + } + + virtual Void + traverse (SemanticGraph::Type& type) + { + using TypeMap::Namespace; + using TypeMap::Namespaces; + + SemanticGraph::Context& tc (type.context ()); + + // There are two situations where we may try to process the + // same type more than once. The first is when the type is + // used in several element declarations in the same schema. + // The second situation only occurs when we are in the file- + // per-type mode. In this case the type was processed as part + // of another root schema. In the second case, while the ret + // and arg types are assumed to be the same, we need to re- + // match the type in order to add include directives to the + // new root schema. + // + Boolean set (true); + + if (tc.count ("p:ret-type")) + { + SemanticGraph::Schema* s ( + tc.get ("p:root-schema")); + + if (&schema_ == s) + return; + + set = false; + } + + SemanticGraph::Namespace& ns ( + dynamic_cast (type.scope ())); + + String const& ns_name (ns.name ()); + String const& t_name (type.name ()); + + // std::wcerr << "traversing: " << ns_name << "#" << t_name << endl; + + for (Namespaces::ConstIterator n (type_map_.begin ()); + n != type_map_.end (); ++n) + { + // Check if the namespace matches. + // + Boolean ns_match; + + if (!n->xsd_name ().empty ()) + { + ns_match = n->xsd_name ().match (ns_name); + } + else + ns_match = ns_name.empty (); + + // std::wcerr << "considering ns expr: " << n->xsd_name () << endl; + + if (ns_match) + { + // Namespace matched. See if there is a type that matches. + // + for (Namespace::TypesIterator t (n->types_begin ()); + t != n->types_end (); ++t) + { + if (t->xsd_name ().match (t_name)) + { + if (set) + { + // Got a match. See if the namespace has the C++ + // namespace mapping. + // + String cxx_ns; + + if (n->has_cxx_name ()) + { + if (!n->xsd_name ().empty ()) + { + cxx_ns = n->xsd_name ().merge ( + n->cxx_name (), ns_name, true); + } + else + cxx_ns = n->cxx_name (); + + cxx_ns += L"::"; + } + + // Figure out ret and arg type names. + // + String ret_type (cxx_ns); + + ret_type += t->xsd_name ().merge ( + t->cxx_ret_name (), t_name, true); + + String arg_type; + + if (t->cxx_arg_name ()) + { + arg_type = cxx_ns; + arg_type += t->xsd_name ().merge ( + t->cxx_arg_name (), t_name, true); + } + else + { + if (ret_type == L"void") + arg_type = ret_type; + else + { + WideChar last (ret_type[ret_type.size () - 1]); + + // If it is already a pointer or reference then use + // it as is. + // + if (last == L'*' || last == L'&') + arg_type = ret_type; + else + arg_type = L"const " + ret_type + L"&"; + } + } + + tc.set ("p:ret-type", ret_type); + tc.set ("p:arg-type", arg_type); + } + + tc.set ("p:root-schema", &schema_); + + //std::wcerr << t_name << " -> " << ret_type << endl; + + // See of we need to add any includes to the translations + // unit. + // + if (add_includes_) + { + if (n->includes_begin () != n->includes_end ()) + { + typedef Cult::Containers::Set Includes; + + SemanticGraph::Context& sc (schema_.context ()); + + if (!sc.count ("p:includes")) + sc.set ("p:includes", Includes ()); + + Includes& is (sc.get ("p:includes")); + + for (Namespace::IncludesIterator i (n->includes_begin ()); + i != n->includes_end (); ++i) + { + is.insert (*i); + } + } + } + + return; + } + } + } + } + } + + private: + SemanticGraph::Schema& schema_; + TypeMap::Namespaces& type_map_; + Boolean add_includes_; + }; + + // + // + struct BaseType: Traversal::Complex + { + virtual Void + traverse (SemanticGraph::Complex& c) + { + Complex::inherits (c); + } + }; + + // + // + struct GlobalType: Traversal::Type, + Traversal::List, + Traversal::Complex, + Traversal::Enumeration + { + GlobalType (SemanticGraph::Schema& schema, + TypeMap::Namespaces& type_map, + Boolean add_includes, + Boolean tiein) + : type_ (schema, type_map, add_includes) + { + inherits_ >> type_; + names_ >> instance_ >> belongs_ >> type_; + argumented_ >> type_; + + if (tiein) + { + // In case of a tiein support, we also need to process base's + // base type. + // + inherits_base_ >> base_type_ >> inherits_level_2_ >> type_; + } + } + + virtual Void + traverse (SemanticGraph::Type& t) + { + type_.traverse (t); + } + + virtual Void + traverse (SemanticGraph::List& l) + { + type_.traverse (l); + Traversal::List::argumented (l, argumented_); + } + + virtual Void + traverse (SemanticGraph::Complex& c) + { + type_.traverse (c); + Complex::inherits (c, inherits_); + Complex::inherits (c, inherits_base_); + Complex::names (c, names_); + } + + virtual Void + traverse (SemanticGraph::Enumeration& e) + { + type_.traverse (e); + Complex::inherits (e, inherits_); + } + + private: + Parser::Type type_; + BaseType base_type_; + Traversal::Names names_; + Traversal::Instance instance_; + Traversal::Inherits inherits_; + Traversal::Inherits inherits_base_; + Traversal::Inherits inherits_level_2_; + Traversal::Belongs belongs_; + Traversal::Argumented argumented_; + }; + + Void + process_impl (CLI::Options const& options, + XSDFrontend::SemanticGraph::Schema& tu, + TypeMap::Namespaces& type_map) + { + Boolean tiein (!options.value () && + !options.value ()); + + if (tu.names_begin ()->named ().name () == + L"http://www.w3.org/2001/XMLSchema") + { + // XML Schema namespace. + // + Traversal::Schema schema; + + Traversal::Names schema_names; + Traversal::Namespace ns; + Traversal::Names ns_names; + GlobalType global_type (tu, type_map, true, tiein); + + schema >> schema_names >> ns >> ns_names >> global_type; + + schema.dispatch (tu); + } + else + { + // If --extern-xml-schema is specified, then we don't want + // includes from the XML Schema type map. + // + Boolean extern_xml_schema ( + options.value ()); + + // Besides types defined in this schema, also process those + // referenced by global elements in case we are generating + // something for them. + // + Traversal::Schema schema; + Traversal::Schema xs_schema; + Traversal::Sources sources; + Traversal::Implies implies; + + schema >> sources >> schema; + schema >> implies >> xs_schema; + + Traversal::Names schema_names; + Traversal::Namespace ns; + Traversal::Names ns_names; + Traversal::Element global_element; + GlobalType global_type (tu, type_map, true, tiein); + + schema >> schema_names >> ns >> ns_names; + + ns_names >> global_element; + ns_names >> global_type; + + Traversal::Belongs element_belongs; + global_element >> element_belongs >> global_type; + + Traversal::Names xs_schema_names; + Traversal::Namespace xs_ns; + Traversal::Names xs_ns_names; + GlobalType xs_global_type (tu, type_map, !extern_xml_schema, tiein); + + xs_schema >> xs_schema_names >> xs_ns >> xs_ns_names >> + xs_global_type; + + schema.dispatch (tu); + } + } + } + + Void TypeProcessor:: + process (CLI::Options const& options, + XSDFrontend::SemanticGraph::Schema& s, + TypeMap::Namespaces& tm) + { + process_impl (options, s, tm); + } + } +} diff --git a/xsde/cxx/parser/type-processor.hxx b/xsde/cxx/parser/type-processor.hxx new file mode 100644 index 0000000..5855569 --- /dev/null +++ b/xsde/cxx/parser/type-processor.hxx @@ -0,0 +1,34 @@ +// file : xsde/cxx/parser/type-processor.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2006-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef CXX_PARSER_TYPE_PROCESSOR_HXX +#define CXX_PARSER_TYPE_PROCESSOR_HXX + +#include + +#include + +#include + +#include + +namespace CXX +{ + namespace Parser + { + using namespace Cult::Types; + + class TypeProcessor + { + public: + Void + process (CLI::Options const& options, + XSDFrontend::SemanticGraph::Schema&, + TypeMap::Namespaces&); + }; + } +} + +#endif // CXX_PARSER_TYPE_PROCESSOR_HXX diff --git a/xsde/cxx/parser/validator.cxx b/xsde/cxx/parser/validator.cxx new file mode 100644 index 0000000..570a0bf --- /dev/null +++ b/xsde/cxx/parser/validator.cxx @@ -0,0 +1,704 @@ +// file : xsde/cxx/parser/validator.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include + +#include +#include + +#include + +#include + +using std::wcerr; + +namespace CXX +{ + namespace Parser + { + namespace + { + class ValidationContext: public Context + { + public: + ValidationContext (SemanticGraph::Schema& root, + CLI::Options const& options, + const WarningSet& disabled_warnings, + Boolean& valid_) + : Context (std::wcerr, root, options, 0, 0, 0), + disabled_warnings_ (disabled_warnings), + disabled_warnings_all_ (false), + valid (valid_), + subst_group_warning_issued (subst_group_warning_issued_), + subst_group_warning_issued_ (false) + { + } + + public: + Boolean + is_disabled (Char const* w) + { + return disabled_warnings_all_ || + disabled_warnings_.find (w) != disabled_warnings_.end (); + } + + public: + String + xpath (SemanticGraph::Nameable& n) + { + if (n.is_a ()) + return L""; // There is a bug if you see this. + + assert (n.named ()); + + SemanticGraph::Scope& scope (n.scope ()); + + if (scope.is_a ()) + return n.name (); + + return xpath (scope) + L"/" + n.name (); + } + + protected: + ValidationContext (ValidationContext& c) + : Context (c), + disabled_warnings_ (c.disabled_warnings_), + disabled_warnings_all_ (c.disabled_warnings_all_), + valid (c.valid), + subst_group_warning_issued (c.subst_group_warning_issued) + { + } + + protected: + const WarningSet& disabled_warnings_; + Boolean disabled_warnings_all_; + Boolean& valid; + Boolean& subst_group_warning_issued; + Boolean subst_group_warning_issued_; + }; + + // + // + struct Any : Traversal::Any, ValidationContext + { + Any (ValidationContext& c) + : ValidationContext (c) + { + } + + struct Element: Traversal::Element, ValidationContext + { + Element (ValidationContext& c, SemanticGraph::Any& any) + : ValidationContext (c), + any_ (any), + ns_ (any.definition_namespace ().name ()) + { + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + using SemanticGraph::Any; + + Boolean q (e.qualified ()); + String ns (q ? e.namespace_ ().name () : ""); + + for (Any::NamespaceIterator i (any_.namespace_begin ()); + i != any_.namespace_end (); ++i) + { + Boolean failed (false); + + if (*i == L"##any") + { + failed = true; + } + else if (*i == L"##other") + { + if (ns_) + { + // Note that here I assume that ##other does not + // include names without target namespace. This + // is not what the spec says but that seems to be + // the consensus. + // + failed = q && ns != ns_; + } + else + { + // No target namespace. + // + failed = q && ns != L""; + } + } + else if (*i == L"##local") + { + failed = !q || ns == L""; + } + else if (*i == L"##targetNamespace") + { + failed = (q && ns_ == ns) || (!q && ns_ == L""); + } + else + { + failed = q && *i == ns; + } + + if (failed) + { + Any& a (any_); + + os << a.file () << ":" << a.line () << ":" << a.column () + << ": warning P001: namespace '" << *i << "' allows for " + << "element '" << e.name () << "'" << endl; + + os << a.file () << ":" << a.line () << ":" << a.column () + << ": warning P001: generated code may not associate " + << "element '" << e.name () << "' correctly if it appears " + << "in place of this wildcard" << endl; + + os << e.file () << ":" << e.line () << ":" << e.column () + << ": info: element '" << e.name () << "' is defined " + << "here" << endl; + + os << a.file () << ":" << a.line () << ":" << a.column () + << ": info: turn on validation to ensure correct " + << "association" << endl; + } + } + } + + private: + SemanticGraph::Any& any_; + String ns_; + }; + + struct Complex: Traversal::Complex + { + Complex () + : up_ (true), down_ (true) + { + } + + virtual Void + post (Type& c) + { + // Go down the inheritance hierarchy. + // + if (down_) + { + Boolean up = up_; + up_ = false; + + if (c.inherits_p ()) + dispatch (c.inherits ().base ()); + + up_ = up; + } + + // Go up the inheritance hierarchy. + // + if (up_) + { + Boolean down = down_; + down_ = false; + + for (Type::BegetsIterator i (c.begets_begin ()); + i != c.begets_end (); ++i) + { + dispatch (i->derived ()); + } + + down_ = down; + } + } + + private: + Boolean up_, down_; + }; + + virtual Void + traverse (SemanticGraph::Any& a) + { + using SemanticGraph::Compositor; + + // Find our complex type. + // + Compositor* c (&a.contained_particle ().compositor ()); + + while(!c->contained_compositor_p ()) + c = &c->contained_particle ().compositor (); + + SemanticGraph::Complex& type ( + dynamic_cast ( + c->contained_compositor ().container ())); + + Complex complex; + Traversal::Compositor compositor; + Element element (*this, a); + Traversal::ContainsCompositor contains_compositor; + Traversal::ContainsParticle contains_particle; + + complex >> contains_compositor; + contains_compositor >> compositor; + compositor >> contains_particle; + contains_particle >> compositor; + contains_particle >> element; + + complex.dispatch (type); + } + }; + + + // + // + struct Traverser : Traversal::Schema, + Traversal::Complex, + Traversal::Type, + Traversal::Element, + ValidationContext + { + Traverser (ValidationContext& c) + : ValidationContext (c), + any_ (c) + { + *this >> sources_ >> *this; + *this >> schema_names_ >> ns_ >> names_ >> *this; + + // Any + // + if (!validation && !is_disabled ("P001")) + { + *this >> contains_compositor_ >> compositor_ >> contains_particle_; + contains_particle_ >> compositor_; + contains_particle_ >> any_; + } + } + + virtual Void + traverse (SemanticGraph::Complex& c) + { + using SemanticGraph::Schema; + + traverse (static_cast (c)); + + if (c.inherits_p ()) + { + SemanticGraph::Type& t (c.inherits ().base ()); + + if (t.named () && + types_.find ( + t.scope ().name () + L"#" + t.name ()) == types_.end ()) + { + // Don't worry about types that are in included/imported + // schemas. + // + Schema& s (dynamic_cast (t.scope ().scope ())); + + if (&s == &schema_root || sources_p (schema_root, s)) + { + valid = false; + + wcerr << c.file () << ":" << c.line () << ":" << c.column () + << ": error: type '" << xpath (c) << "' inherits from " + << "yet undefined type '" << xpath (t) << "'" << endl; + + wcerr << t.file () << ":" << t.line () << ":" << t.column () + << ": info: '" << xpath (t) << "' is defined here" + << endl; + + wcerr << c.file () << ":" << c.line () << ":" << c.column () + << ": info: inheritance from a yet-undefined type is " + << "not supported" << endl; + + wcerr << c.file () << ":" << c.line () << ":" << c.column () + << ": info: re-arrange your schema and try again" + << endl; + } + } + } + + Complex::traverse (c); + } + + virtual Void + traverse (SemanticGraph::Type& t) + { + if (t.named ()) + { + types_.insert (t.scope ().name () + L"#" + t.name ()); + } + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + if (is_disabled ("P002")) + return; + + if (e.substitutes_p () && + !options.value () && + !subst_group_warning_issued) + { + subst_group_warning_issued = true; + + os << e.file () << ":" << e.line () << ":" << e.column () + << ": warning P002: substitution groups are used but " + << "--generate-polymorphic was not specified" << endl; + + os << e.file () << ":" << e.line () << ":" << e.column () + << ": info: generated code may not be able to parse " + << "some conforming instances" << endl; + } + } + + // Return true if root sources s. + // + Boolean + sources_p (SemanticGraph::Schema& root, SemanticGraph::Schema& s) + { + using SemanticGraph::Schema; + using SemanticGraph::Sources; + + for (Schema::UsesIterator i (root.uses_begin ()); + i != root.uses_end (); ++i) + { + if (i->is_a ()) + { + if (&i->schema () == &s || sources_p (i->schema (), s)) + return true; + } + } + + return false; + } + + private: + Containers::Set types_; + + Traversal::Sources sources_; + + Traversal::Names schema_names_; + Traversal::Namespace ns_; + + Traversal::Names names_; + + // Any. + // + Any any_; + Traversal::Compositor compositor_; + Traversal::ContainsParticle contains_particle_; + Traversal::ContainsCompositor contains_compositor_; + }; + + // + // + struct AnonymousMember: protected ValidationContext + { + AnonymousMember (ValidationContext& c, Boolean& error_issued) + : ValidationContext (c), error_issued_ (error_issued) + { + } + + Boolean + traverse_common (SemanticGraph::Member& m) + { + SemanticGraph::Type& t (m.type ()); + + if (!t.named () + && !t.is_a () + && !t.is_a ()) + { + if (!error_issued_) + { + valid = false; + error_issued_ = true; + + wcerr << t.file () + << ": error: anonymous types detected" + << endl; + + wcerr << t.file () + << ": info: " + << "anonymous types are not supported in this mapping" + << endl; + + wcerr << t.file () + << ": info: consider explicitly naming these types or " + << "remove the --preserve-anonymous option to " + << "automatically name them" + << endl; + + if (!options.value ()) + wcerr << t.file () + << ": info: use --show-anonymous option to see these " + << "types" << endl; + } + + return true; + } + + return false; + } + + private: + Boolean& error_issued_; + }; + + struct AnonymousElement: Traversal::Element, + AnonymousMember + { + AnonymousElement (ValidationContext& c, Boolean& error_issued) + : AnonymousMember (c, error_issued) + { + } + + virtual Void + traverse (SemanticGraph::Element& e) + { + if (traverse_common (e)) + { + if (options.value ()) + { + wcerr << e.file () << ":" << e.line () << ":" << e.column () + << ": error: element '" << xpath (e) << "' " + << "is of anonymous type" << endl; + } + } + else + Traversal::Element::traverse (e); + } + }; + + struct AnonymousAttribute: Traversal::Attribute, + AnonymousMember + { + AnonymousAttribute (ValidationContext& c, Boolean& error_issued) + : AnonymousMember (c, error_issued) + { + } + + virtual Void + traverse (Type& a) + { + if (traverse_common (a)) + { + if (options.value ()) + { + wcerr << a.file () << ":" << a.line () << ":" << a.column () + << ": error: attribute '" << xpath (a) << "' " + << "is of anonymous type" << endl; + } + } + else + Traversal::Attribute::traverse (a); + } + }; + + struct AnonymousType : Traversal::Schema, + Traversal::Complex, + ValidationContext + { + AnonymousType (ValidationContext& c) + : ValidationContext (c), + error_issued_ (false), + element_ (c, error_issued_), + attribute_ (c, error_issued_) + { + *this >> sources_ >> *this; + *this >> schema_names_ >> ns_ >> names_ >> *this; + + *this >> contains_compositor_ >> compositor_; + compositor_ >> contains_particle_; + contains_particle_ >> compositor_; + contains_particle_ >> element_; + + *this >> names_attribute_ >> attribute_; + } + + private: + Boolean error_issued_; + + Containers::Set types_; + + Traversal::Sources sources_; + + Traversal::Names schema_names_; + Traversal::Namespace ns_; + Traversal::Names names_; + + Traversal::Compositor compositor_; + AnonymousElement element_; + Traversal::ContainsCompositor contains_compositor_; + Traversal::ContainsParticle contains_particle_; + + AnonymousAttribute attribute_; + Traversal::Names names_attribute_; + }; + + struct GlobalElement: Traversal::Element, ValidationContext + { + GlobalElement (ValidationContext& c, SemanticGraph::Element*& element) + : ValidationContext (c), element_ (element) + { + } + + virtual Void + traverse (Type& e) + { + if (!valid) + return; + + if (options.value ()) + { + if (element_ == 0) + element_ = &e; + } + else if (options.value ()) + { + element_ = &e; + } + else if (String name = options.value ()) + { + if (e.name () == name) + element_ = &e; + } + else + { + if (element_ == 0) + element_ = &e; + else + { + wcerr << schema_root.file () << ": error: unable to generate " + << "the test driver without a unique document root" + << endl; + + wcerr << schema_root.file () << ": info: use --root-element-* " + << "options to specify the document root" << endl; + + valid = false; + } + } + } + + private: + SemanticGraph::Element*& element_; + }; + } + + Boolean Validator:: + validate (CLI::Options const& options, + SemanticGraph::Schema& root, + SemanticGraph::Path const&, + Boolean gen_driver, + const WarningSet& disabled_warnings) + { + Boolean valid (true); + ValidationContext ctx (root, options, disabled_warnings, valid); + + // + // + if (options.value () && + options.value ()) + { + wcerr << "error: mutually exclusive options specified: " + << "--generate-noop-impl and --generate-print-impl" + << endl; + + return false; + } + + // + // + { + Boolean ref (options.value ()); + Boolean rel (options.value ()); + Boolean re (options.value ()); + + if ((ref && rel) || (ref && re) || (rel && re)) + { + wcerr << "error: mutually exclusive options specified: " + << "--root-element-last, --root-element-first, and " + << "--root-element" + << endl; + + return false; + } + } + + // + // + if (options.value () && + options.value ()) + { + wcerr << "error: mutually exclusive options specified: " + << "--reuse-style-mixin and --reuse-style-none" + << endl; + + return false; + } + + // + // + if (options.value () && + (options.value () || + options.value ()) && + !ctx.is_disabled ("P003")) + { + wcerr << "warning P003: generating sample implementation without " + << "parser reuse support: the resulting code may not compile" + << endl; + + return false; + } + + // Test for anonymout types. + // + { + AnonymousType traverser (ctx); + traverser.dispatch (root); + } + + // Test the rest. + // + if (valid) + { + Traverser traverser (ctx); + traverser.dispatch (root); + } + + // Test that the document root is unique. + // + if (valid && gen_driver) + { + SemanticGraph::Element* element (0); + + Traversal::Schema schema; + Traversal::Sources sources; + + schema >> sources >> schema; + + Traversal::Names schema_names; + Traversal::Namespace ns; + Traversal::Names ns_names; + GlobalElement global_element (ctx, element); + + schema >> schema_names >> ns >> ns_names >> global_element; + + schema.dispatch (root); + + if (valid && element == 0) + { + wcerr << root.file () << ": error: unable to generate the " + << "test driver without a global element (document root)" + << endl; + + valid = false; + } + } + + return valid; + } + } +} diff --git a/xsde/cxx/parser/validator.hxx b/xsde/cxx/parser/validator.hxx new file mode 100644 index 0000000..d602aae --- /dev/null +++ b/xsde/cxx/parser/validator.hxx @@ -0,0 +1,36 @@ +// file : xsde/cxx/parser/validator.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef CXX_PARSER_VALIDATOR_HXX +#define CXX_PARSER_VALIDATOR_HXX + +#include + +#include + +#include + +#include + +namespace CXX +{ + namespace Parser + { + using namespace Cult::Types; + + class Validator + { + public: + Boolean + validate (CLI::Options const& options, + XSDFrontend::SemanticGraph::Schema&, + XSDFrontend::SemanticGraph::Path const& tu, + Boolean gen_driver, + const WarningSet& disabled_warnings); + }; + } +} + +#endif // CXX_PARSER_VALIDATOR_HXX -- cgit v1.1