Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r63616 - in branches/release/libs/spirit/example/scheme: . detail input output test
From: hartmut.kaiser_at_[hidden]
Date: 2010-07-04 14:04:43


Author: hkaiser
Date: 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
New Revision: 63616
URL: http://svn.boost.org/trac/boost/changeset/63616

Log:
Spirit: restored Scheme example to the point where it has been deleted from release branch
Added:
   branches/release/libs/spirit/example/scheme/
   branches/release/libs/spirit/example/scheme/detail/
   branches/release/libs/spirit/example/scheme/detail/utree_detail1.hpp (contents, props changed)
   branches/release/libs/spirit/example/scheme/detail/utree_detail2.hpp (contents, props changed)
   branches/release/libs/spirit/example/scheme/detail/utree_detail3.hpp (contents, props changed)
   branches/release/libs/spirit/example/scheme/input/
   branches/release/libs/spirit/example/scheme/input/parse_sexpr.hpp (contents, props changed)
   branches/release/libs/spirit/example/scheme/input/parse_sexpr_impl.hpp (contents, props changed)
   branches/release/libs/spirit/example/scheme/input/sexpr.hpp (contents, props changed)
   branches/release/libs/spirit/example/scheme/out.txt (contents, props changed)
   branches/release/libs/spirit/example/scheme/output/
   branches/release/libs/spirit/example/scheme/output/generate_sexpr.hpp (contents, props changed)
   branches/release/libs/spirit/example/scheme/output/generate_sexpr_impl.hpp (contents, props changed)
   branches/release/libs/spirit/example/scheme/output/sexpr.hpp (contents, props changed)
   branches/release/libs/spirit/example/scheme/sexpr.hpp (contents, props changed)
   branches/release/libs/spirit/example/scheme/sexpr_test.cpp (contents, props changed)
   branches/release/libs/spirit/example/scheme/sexpr_test.txt (contents, props changed)
   branches/release/libs/spirit/example/scheme/simple_print.hpp (contents, props changed)
   branches/release/libs/spirit/example/scheme/test/
   branches/release/libs/spirit/example/scheme/test/out.txt (contents, props changed)
   branches/release/libs/spirit/example/scheme/test/scheme.cpp (contents, props changed)
   branches/release/libs/spirit/example/scheme/test/sexpr_input_test.cpp (contents, props changed)
   branches/release/libs/spirit/example/scheme/test/sexpr_output_test.cpp (contents, props changed)
   branches/release/libs/spirit/example/scheme/test/sexpr_test.txt (contents, props changed)
   branches/release/libs/spirit/example/scheme/test/utree_test.cpp (contents, props changed)
   branches/release/libs/spirit/example/scheme/utree.hpp (contents, props changed)
   branches/release/libs/spirit/example/scheme/utree_operators.hpp (contents, props changed)
   branches/release/libs/spirit/example/scheme/utree_test.cpp (contents, props changed)

Added: branches/release/libs/spirit/example/scheme/detail/utree_detail1.hpp
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/detail/utree_detail1.hpp 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,123 @@
+/*=============================================================================
+ Copyright (c) 2001-2010 Joel de Guzman
+
+ Distributed under the Boost Software License, Version 1.0. (See accompanying
+ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+#if !defined(BOOST_SPIRIT_UTREE_DETAIL1)
+#define BOOST_SPIRIT_UTREE_DETAIL1
+
+#if defined(BOOST_MSVC)
+# pragma warning(push)
+# pragma warning(disable: 4804)
+# pragma warning(disable: 4805)
+# pragma warning(disable: 4244)
+#endif
+
+#include <boost/type_traits/alignment_of.hpp>
+
+namespace scheme { namespace detail
+{
+ template <typename UTreeX, typename UTreeY>
+ struct visit_impl;
+ struct index_impl;
+ template <typename T>
+ struct get_impl;
+
+ ///////////////////////////////////////////////////////////////////////////
+ // Our POD double linked list. Straightforward implementation.
+ // This implementation is very primitive and is not meant to be
+ // used stand-alone. This is the internal data representation
+ // of lists in our utree.
+ ///////////////////////////////////////////////////////////////////////////
+ struct list // keep this a POD!
+ {
+ struct node;
+
+ template <typename Value>
+ class node_iterator;
+
+ void free();
+ void copy(list const& other);
+ void default_construct();
+
+ template <typename T>
+ void insert_before(T const& val, node* node);
+
+ template <typename T>
+ void insert_after(T const& val, node* node);
+
+ template <typename T>
+ void push_front(T const& val);
+
+ template <typename T>
+ void push_back(T const& val);
+
+ void pop_front();
+ void pop_back();
+ node* erase(node* pos);
+
+ node* first;
+ node* last;
+ std::size_t size;
+ };
+
+ ///////////////////////////////////////////////////////////////////////////
+ // Our POD fast string. This implementation is very primitive and is not
+ // meant to be used stand-alone. This is the internal data representation
+ // of strings in our utree. This is deliberately a POD to allow it to be
+ // placed in a union. This POD fast string specifically utilizes
+ // (sizeof(list) * alignment_of(list)) - (2 * sizeof(char)). In a 32 bit
+ // system, this is 14 bytes. The two extra bytes are used by utree to store
+ // management info.
+ //
+ // It is a const string (i.e. immutable). It stores the characters directly
+ // if possible and only uses the heap if the string does not fit. Null
+ // characters are allowed, making it suitable to encode raw binary. The
+ // string length is encoded in the first byte if the string is placed in-situ,
+ // else, the length plus a pointer to the string in the heap are stored.
+ ///////////////////////////////////////////////////////////////////////////
+ struct fast_string // Keep this a POD!
+ {
+ static std::size_t const
+ buff_size = (sizeof(list) + boost::alignment_of<list>::value)
+ / sizeof(char);
+
+ static std::size_t const
+ small_string_size = buff_size-sizeof(char);
+
+ static std::size_t const
+ max_string_len = small_string_size - 1;
+
+ struct heap_store
+ {
+ char* str;
+ std::size_t size;
+ };
+
+ union
+ {
+ char buff[buff_size];
+ heap_store heap;
+ };
+
+ int get_type() const;
+ void set_type(int t);
+ bool is_heap_allocated() const;
+
+ std::size_t size() const;
+ char const* str() const;
+
+ template <typename Iterator>
+ void construct(Iterator f, Iterator l);
+
+ void swap(fast_string& other);
+ void free();
+ void copy(fast_string const& other);
+
+ char& info();
+ char info() const;
+ };
+}}
+
+#endif

Added: branches/release/libs/spirit/example/scheme/detail/utree_detail2.hpp
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/detail/utree_detail2.hpp 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,944 @@
+/*=============================================================================
+ Copyright (c) 2001-2010 Joel de Guzman
+
+ Distributed under the Boost Software License, Version 1.0. (See accompanying
+ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+#if !defined(BOOST_SPIRIT_UTREE_DETAIL2)
+#define BOOST_SPIRIT_UTREE_DETAIL2
+
+namespace scheme { namespace detail
+{
+ inline char& fast_string::info()
+ {
+ return buff[small_string_size];
+ }
+
+ inline char fast_string::info() const
+ {
+ return buff[small_string_size];
+ }
+
+ inline int fast_string::get_type() const
+ {
+ return info() >> 1;
+ }
+
+ inline void fast_string::set_type(int t)
+ {
+ info() = (t << 1) | (info() & 1);
+ }
+
+ inline bool fast_string::is_heap_allocated() const
+ {
+ return info() & 1;
+ }
+
+ inline std::size_t fast_string::size() const
+ {
+ if (is_heap_allocated())
+ return heap.size;
+ else
+ return max_string_len - buff[small_string_size - 1];
+ }
+
+ inline char const* fast_string::str() const
+ {
+ if (is_heap_allocated())
+ return heap.str;
+ else
+ return buff;
+ }
+
+ template <typename Iterator>
+ inline void fast_string::construct(Iterator f, Iterator l)
+ {
+ unsigned const size = l-f;
+ char* str;
+ if (size < small_string_size)
+ {
+ // if it fits, store it in-situ; small_string_size minus the length
+ // of the string is placed in buff[small_string_size - 1]
+ str = buff;
+ buff[small_string_size - 1] = max_string_len - size;
+ info() &= ~0x1;
+ }
+ else
+ {
+ // else, store it in the heap
+ str = new char[size + 1]; // add one for the null char
+ heap.str = str;
+ heap.size = size;
+ info() |= 0x1;
+ }
+ for (std::size_t i = 0; i != size; ++i)
+ {
+ *str++ = *f++;
+ }
+ *str = '\0'; // add the null char
+ }
+
+ inline void fast_string::swap(fast_string& other)
+ {
+ std::swap(*this, other);
+ }
+
+ inline void fast_string::free()
+ {
+ if (is_heap_allocated())
+ {
+ delete [] heap.str;
+ heap.str = 0;
+ }
+ }
+
+ inline void fast_string::copy(fast_string const& other)
+ {
+ construct(other.str(), other.str() + other.size());
+ }
+
+ struct list::node : boost::noncopyable
+ {
+ template <typename T>
+ node(T const& val, node* next, node* prev)
+ : val(val), next(next), prev(prev) {}
+
+ void unlink()
+ {
+ prev->next = next;
+ next->prev = prev;
+ }
+
+ utree val;
+ node* next;
+ node* prev;
+ };
+
+ template <typename Value>
+ class list::node_iterator
+ : public boost::iterator_facade<
+ node_iterator<Value>
+ , Value
+ , boost::bidirectional_traversal_tag
+ >
+ {
+ public:
+
+ node_iterator()
+ : node(0) {}
+
+ explicit node_iterator(list::node* p)
+ : node(p) {}
+
+ private:
+
+ friend class boost::iterator_core_access;
+ friend class scheme::utree;
+
+ void increment()
+ {
+ node = node->next;
+ }
+
+ void decrement()
+ {
+ node = node->prev;
+ }
+
+ bool equal(node_iterator const& other) const
+ {
+ return node == other.node;
+ }
+
+ utree& dereference() const
+ {
+ return node->val;
+ }
+
+ list::node* node;
+ };
+
+ inline void list::free()
+ {
+ node* p = first;
+ while (p != last)
+ {
+ node* next = p->next;
+ delete p;
+ p = next;
+ }
+ first = last = 0;
+ size = 0;
+ }
+
+ inline void list::copy(list const& other)
+ {
+ first = last = 0;
+ node* p = other.first;
+ while (p != 0)
+ {
+ push_back(p->val);
+ p = p->next;
+ }
+ }
+
+ inline void list::default_construct()
+ {
+ first = last = 0;
+ size = 0;
+ }
+
+ template <typename T>
+ inline void list::insert_before(T const& val, node* np)
+ {
+ BOOST_ASSERT(np != 0);
+ node* new_node = new node(val, np, np->prev);
+ if (np->prev)
+ np->prev->next = new_node;
+ else
+ first = new_node;
+ np->prev = new_node;
+ ++size;
+ }
+
+ template <typename T>
+ inline void list::insert_after(T const& val, node* np)
+ {
+ BOOST_ASSERT(np != 0);
+ node* new_node = new node(val, np->next, np);
+ if (np->next)
+ np->next->prev = new_node;
+ else
+ last = new_node;
+ np->next = new_node;
+ ++size;
+ }
+
+ template <typename T>
+ inline void list::push_front(T const& val)
+ {
+ detail::list::node* new_node;
+ if (first == 0)
+ {
+ new_node = new detail::list::node(val, 0, 0);
+ first = last = new_node;
+ ++size;
+ }
+ else
+ {
+ insert_before(val, first);
+ }
+ }
+
+ template <typename T>
+ inline void list::push_back(T const& val)
+ {
+ if (last == 0)
+ push_front(val);
+ else
+ insert_after(val, last);
+ }
+
+ inline void list::pop_front()
+ {
+ BOOST_ASSERT(size != 0);
+ node* np = first;
+ first = first->next;
+ first->prev = 0;
+ delete np;
+ --size;
+ }
+
+ inline void list::pop_back()
+ {
+ BOOST_ASSERT(size != 0);
+ node* np = last;
+ last = last->prev;
+ last->next = 0;
+ delete np;
+ --size;
+ }
+
+ inline list::node* list::erase(node* pos)
+ {
+ BOOST_ASSERT(pos != 0);
+ if (pos == first)
+ {
+ pop_front();
+ return first;
+ }
+ else if (pos == last)
+ {
+ pop_back();
+ return 0;
+ }
+ else
+ {
+ node* next(pos->next);
+ pos->unlink();
+ delete pos;
+ --size;
+ return next;
+ }
+ }
+
+ template <typename F, typename X>
+ struct bind_impl // simple binder for binary visitation (we don't want to bring in the big guns)
+ {
+ typedef typename F::result_type result_type;
+ X& x; // always by reference
+ F f;
+ bind_impl(F f, X& x) : x(x), f(f) {}
+
+ template <typename Y>
+ typename F::result_type operator()(Y& y) const
+ {
+ return f(x, y);
+ }
+
+ template <typename Y>
+ typename F::result_type operator()(Y const& y) const
+ {
+ return f(x, y);
+ }
+ };
+
+ template <typename F, typename X>
+ bind_impl<F, X const> bind(F f, X const& x)
+ {
+ return bind_impl<F, X const>(f, x);
+ }
+
+ template <typename F, typename X>
+ bind_impl<F, X> bind(F f, X& x)
+ {
+ return bind_impl<F, X>(f, x);
+ }
+
+ template <typename UTreeX, typename UTreeY = UTreeX>
+ struct visit_impl
+ {
+ template <typename F>
+ typename F::result_type
+ static apply(UTreeX& x, F f) // single dispatch
+ {
+ typedef typename
+ boost::mpl::if_<boost::is_const<UTreeX>,
+ typename UTreeX::const_iterator,
+ typename UTreeX::iterator>::type
+ iterator;
+
+ typedef boost::iterator_range<iterator> list_range;
+ typedef utree_type type;
+
+ switch (x.get_type())
+ {
+ default:
+ BOOST_ASSERT(false); // can't happen
+
+ case type::nil_type:
+ nil arg;
+ return f(arg);
+
+ case type::bool_type:
+ return f(x.b);
+
+ case type::int_type:
+ return f(x.i);
+
+ case type::double_type:
+ return f(x.d);
+
+ case type::list_type:
+ return f(list_range(iterator(x.l.first), iterator(0)));
+
+ case type::string_type:
+ return f(utf8_string_range(x.s.str(), x.s.size()));
+
+ case type::symbol_type:
+ return f(utf8_symbol_range(x.s.str(), x.s.size()));
+
+ case type::binary_type:
+ return f(binary_range(x.s.str(), x.s.size()));
+
+ case type::reference_type:
+ return apply(*x.p, f);
+
+ case type::function_type:
+ return f(x.f);
+ }
+ }
+
+ template <typename F>
+ typename F::result_type
+ static apply(UTreeX& x, UTreeY& y, F f) // double dispatch
+ {
+ typedef typename
+ boost::mpl::if_<boost::is_const<UTreeX>,
+ typename UTreeX::const_iterator,
+ typename UTreeX::iterator>::type
+ iterator;
+
+ typedef boost::iterator_range<iterator> list_range;
+ typedef utree_type type;
+
+ switch (x.get_type())
+ {
+ default:
+ BOOST_ASSERT(false); // can't happen
+
+ case type::nil_type:
+ nil x_;
+ return visit_impl::apply(y, detail::bind(f, x_));
+
+ case type::bool_type:
+ return visit_impl::apply(y, detail::bind(f, x.b));
+
+ case type::int_type:
+ return visit_impl::apply(y, detail::bind(f, x.i));
+
+ case type::double_type:
+ return visit_impl::apply(y, detail::bind(f, x.d));
+
+ case type::list_type:
+ return visit_impl::apply(
+ y, detail::bind<F, list_range>(f,
+ list_range(iterator(x.l.first), iterator(0))));
+
+ case type::string_type:
+ return visit_impl::apply(y, detail::bind(
+ f, utf8_string_range(x.s.str(), x.s.size())));
+
+ case type::symbol_type:
+ return visit_impl::apply(y, detail::bind(
+ f, utf8_symbol_range(x.s.str(), x.s.size())));
+
+ case type::binary_type:
+ return visit_impl::apply(y, detail::bind(
+ f, binary_range(x.s.str(), x.s.size())));
+
+ case type::reference_type:
+ return apply(*x.p, y, f);
+
+ case type::function_type:
+ return visit_impl::apply(y, detail::bind(f, x.f));
+ }
+ }
+ };
+
+ struct index_impl
+ {
+ static utree& apply(list::node* node, std::size_t i)
+ {
+ for (; i > 0; --i)
+ node = node->next;
+ return node->val;
+ }
+
+ static utree const& apply(list::node const* node, std::size_t i)
+ {
+ for (; i > 0; --i)
+ node = node->next;
+ return node->val;
+ }
+ };
+}}
+
+namespace scheme
+{
+ inline utree::utree()
+ {
+ set_type(type::nil_type);
+ }
+
+ inline utree::utree(bool b) : b(b)
+ {
+ set_type(type::bool_type);
+ }
+
+ inline utree::utree(unsigned int i) : i(i)
+ {
+ set_type(type::int_type);
+ }
+
+ inline utree::utree(int i) : i(i)
+ {
+ set_type(type::int_type);
+ }
+
+ inline utree::utree(double d) : d(d)
+ {
+ set_type(type::double_type);
+ }
+
+ inline utree::utree(char const* str)
+ {
+ s.construct(str, str + strlen(str));
+ set_type(type::string_type);
+ }
+
+ inline utree::utree(char const* str, std::size_t len)
+ {
+ s.construct(str, str + len);
+ set_type(type::string_type);
+ }
+
+ inline utree::utree(std::string const& str)
+ {
+ s.construct(str.begin(), str.end());
+ set_type(type::string_type);
+ }
+
+ template <typename Base, utree_type::info type_>
+ inline utree::utree(basic_string<Base, type_> const& bin)
+ {
+ s.construct(bin.begin(), bin.end());
+ set_type(type_);
+ }
+
+ inline utree::utree(boost::reference_wrapper<utree> ref)
+ : p(ref.get_pointer())
+ {
+ set_type(type::reference_type);
+ }
+
+ inline utree::utree(function_ptr fptr)
+ : f(fptr)
+ {
+ set_type(type::function_type);
+ }
+
+ inline utree::utree(utree const& other)
+ {
+ copy(other);
+ }
+
+ inline utree::~utree()
+ {
+ free();
+ }
+
+ inline utree& utree::operator=(utree const& other)
+ {
+ if (this != &other)
+ {
+ free();
+ copy(other);
+ }
+ return *this;
+ }
+
+ inline utree& utree::operator=(bool b_)
+ {
+ free();
+ b = b_;
+ set_type(type::bool_type);
+ return *this;
+ }
+
+ inline utree& utree::operator=(unsigned int i_)
+ {
+ free();
+ i = i_;
+ set_type(type::int_type);
+ return *this;
+ }
+
+ inline utree& utree::operator=(int i_)
+ {
+ free();
+ i = i_;
+ set_type(type::int_type);
+ return *this;
+ }
+
+ inline utree& utree::operator=(double d_)
+ {
+ free();
+ d = d_;
+ set_type(type::double_type);
+ return *this;
+ }
+
+ inline utree& utree::operator=(char const* s_)
+ {
+ free();
+ s.construct(s_, s_ + strlen(s_));
+ set_type(type::string_type);
+ return *this;
+ }
+
+ inline utree& utree::operator=(std::string const& s_)
+ {
+ free();
+ s.construct(s_.begin(), s_.end());
+ set_type(type::string_type);
+ return *this;
+ }
+
+ template <typename Base, utree_type::info type_>
+ inline utree& utree::operator=(basic_string<Base, type_> const& bin)
+ {
+ free();
+ s.construct(bin.begin(), bin.end());
+ set_type(type_);
+ return *this;
+ }
+
+ inline utree& utree::operator=(boost::reference_wrapper<utree> ref)
+ {
+ free();
+ p = ref.get_pointer();
+ set_type(type::reference_type);
+ return *this;
+ }
+
+ inline utree& utree::operator=(function_ptr fptr)
+ {
+ free();
+ f = fptr;
+ set_type(type::function_type);
+ return *this;
+ }
+
+ template <typename F>
+ typename F::result_type
+ inline utree::visit(utree const& x, F f)
+ {
+ return detail::visit_impl<utree const>::apply(x, f);
+ }
+
+ template <typename F>
+ typename F::result_type
+ inline utree::visit(utree& x, F f)
+ {
+ return detail::visit_impl<utree>::apply(x, f);
+ }
+
+ template <typename F>
+ typename F::result_type
+ inline utree::visit(utree const& x, utree const& y, F f)
+ {
+ return detail::visit_impl<utree const, utree const>::apply(x, y, f);
+ }
+
+ template <typename F>
+ typename F::result_type
+ inline utree::visit(utree const& x, utree& y, F f)
+ {
+ return detail::visit_impl<utree const, utree>::apply(x, y, f);
+ }
+
+ template <typename F>
+ typename F::result_type
+ inline utree::visit(utree& x, utree const& y, F f)
+ {
+ return detail::visit_impl<utree, utree const>::apply(x, y, f);
+ }
+
+ template <typename F>
+ typename F::result_type
+ inline utree::visit(utree& x, utree& y, F f)
+ {
+ return detail::visit_impl<utree, utree>::apply(x, y, f);
+ }
+
+ inline utree& utree::operator[](std::size_t i)
+ {
+ if (get_type() == type::reference_type)
+ return (*p)[i];
+ BOOST_ASSERT(get_type() == type::list_type && size() > i);
+ return detail::index_impl::apply(l.first, i);
+ }
+
+ inline utree const& utree::operator[](std::size_t i) const
+ {
+ if (get_type() == type::reference_type)
+ return (*(utree const*)p)[i];
+ BOOST_ASSERT(get_type() == type::list_type && size() > i);
+ return detail::index_impl::apply(l.first, i);
+ }
+
+ template <typename T>
+ inline void utree::push_front(T const& val)
+ {
+ if (get_type() == type::reference_type)
+ return p->push_front(val);
+ ensure_list_type();
+ l.push_front(val);
+ }
+
+ template <typename T>
+ inline void utree::push_back(T const& val)
+ {
+ if (get_type() == type::reference_type)
+ return p->push_back(val);
+ ensure_list_type();
+ l.push_back(val);
+ }
+
+ template <typename T>
+ inline utree::iterator utree::insert(iterator pos, T const& val)
+ {
+ if (get_type() == type::reference_type)
+ return p->insert(pos, val);
+ ensure_list_type();
+ if (pos.node == l.last)
+ {
+ push_back(val);
+ return begin();
+ }
+ else
+ {
+ l.insert_before(val, pos.node);
+ return utree::iterator(pos.node->prev);
+ }
+ }
+
+
+ template <typename T>
+ inline void utree::insert(iterator pos, std::size_t n, T const& val)
+ {
+ if (get_type() == type::reference_type)
+ return p->insert(pos, n, val);
+ for (std::size_t i = 0; i != n; ++i)
+ insert(pos, val);
+ }
+
+ template <typename Iter>
+ inline void utree::insert(iterator pos, Iter first, Iter last)
+ {
+ if (get_type() == type::reference_type)
+ return p->insert(pos, first, last);
+ ensure_list_type();
+ while (first != last)
+ insert(pos, *first++);
+ }
+
+ template <typename Iter>
+ inline void utree::assign(Iter first, Iter last)
+ {
+ if (get_type() == type::reference_type)
+ return p->assign(first, last);
+ ensure_list_type();
+ clear();
+ while (first != last)
+ push_back(*first++);
+ }
+
+ inline void utree::clear()
+ {
+ if (get_type() == type::reference_type)
+ return p->clear();
+ // clear will always make this a nil type
+ free();
+ set_type(type::nil_type);
+ }
+
+ inline void utree::pop_front()
+ {
+ if (get_type() == type::reference_type)
+ return p->pop_front();
+ BOOST_ASSERT(get_type() == type::list_type);
+ l.pop_front();
+ }
+
+ inline void utree::pop_back()
+ {
+ if (get_type() == type::reference_type)
+ return p->pop_back();
+ BOOST_ASSERT(get_type() == type::list_type);
+ l.pop_back();
+ }
+
+ inline utree::iterator utree::erase(iterator pos)
+ {
+ if (get_type() == type::reference_type)
+ return p->erase(pos);
+ BOOST_ASSERT(get_type() == type::list_type);
+ return iterator(l.erase(pos.node));
+ }
+
+ inline utree::iterator utree::erase(iterator first, iterator last)
+ {
+ if (get_type() == type::reference_type)
+ return p->erase(first, last);
+ while (first != last)
+ erase(first++);
+ return last;
+ }
+
+ inline utree::iterator utree::begin()
+ {
+ if (get_type() == type::reference_type)
+ return p->begin();
+ ensure_list_type();
+ return iterator(l.first);
+ }
+
+ inline utree::iterator utree::end()
+ {
+ if (get_type() == type::reference_type)
+ return p->end();
+ ensure_list_type();
+ return iterator(l.last);
+ }
+
+ inline utree::const_iterator utree::begin() const
+ {
+ if (get_type() == type::reference_type)
+ return ((utree const*)p)->begin();
+ BOOST_ASSERT(get_type() == type::list_type);
+ return const_iterator(l.first);
+ }
+
+ inline utree::const_iterator utree::end() const
+ {
+ if (get_type() == type::reference_type)
+ return ((utree const*)p)->end();
+ BOOST_ASSERT(get_type() == type::list_type);
+ return const_iterator(l.last);
+ }
+
+ inline bool utree::empty() const
+ {
+ if (get_type() == type::reference_type)
+ return ((utree const*)p)->empty();
+ if (get_type() == type::list_type)
+ return l.size == 0;
+ BOOST_ASSERT(get_type() == type::nil_type);
+ return true;
+ }
+
+ inline std::size_t utree::size() const
+ {
+ if (get_type() == type::reference_type)
+ return ((utree const*)p)->size();
+ if (get_type() == type::list_type)
+ return l.size;
+ BOOST_ASSERT(get_type() == type::nil_type);
+ return 0;
+ }
+
+ inline int utree::which() const
+ {
+ return get_type();
+ }
+
+ inline utree& utree::front()
+ {
+ if (get_type() == type::reference_type)
+ return p->front();
+ BOOST_ASSERT(get_type() == type::list_type && l.first != 0);
+ return l.first->val;
+ }
+
+ inline utree& utree::back()
+ {
+ if (get_type() == type::reference_type)
+ return p->back();
+ BOOST_ASSERT(get_type() == type::list_type && l.last != 0);
+ return l.last->val;
+ }
+
+ inline utree const& utree::front() const
+ {
+ if (get_type() == type::reference_type)
+ return ((utree const*)p)->front();
+ BOOST_ASSERT(get_type() == type::list_type && l.first != 0);
+ return l.first->val;
+ }
+
+ inline utree const& utree::back() const
+ {
+ if (get_type() == type::reference_type)
+ return ((utree const*)p)->back();
+ BOOST_ASSERT(get_type() == type::list_type && l.last != 0);
+ return l.last->val;
+ }
+
+ inline void utree::swap(utree& other)
+ {
+ s.swap(other.s);
+ }
+
+ inline utree::type::info utree::get_type() const
+ {
+ // the fast string holds the type info
+ return static_cast<utree::type::info>(s.get_type());
+ }
+
+ inline void utree::set_type(type::info t)
+ {
+ // the fast string holds the type info
+ s.set_type(t);
+ }
+
+ inline void utree::ensure_list_type()
+ {
+ if (get_type() == type::nil_type)
+ {
+ set_type(type::list_type);
+ l.default_construct();
+ }
+ else
+ {
+ BOOST_ASSERT(get_type() == type::list_type);
+ }
+ }
+
+ inline void utree::free()
+ {
+ switch (get_type())
+ {
+ case type::binary_type:
+ case type::symbol_type:
+ case type::string_type:
+ s.free();
+ break;
+ case type::list_type:
+ l.free();
+ break;
+ default:
+ break;
+ };
+ }
+
+ inline void utree::copy(utree const& other)
+ {
+ set_type(other.get_type());
+ switch (other.get_type())
+ {
+ case type::nil_type:
+ break;
+ case type::bool_type:
+ b = other.b;
+ break;
+ case type::int_type:
+ i = other.i;
+ break;
+ case type::double_type:
+ d = other.d;
+ break;
+ case type::reference_type:
+ p = other.p;
+ break;
+ case type::string_type:
+ case type::symbol_type:
+ case type::binary_type:
+ s.copy(other.s);
+ break;
+ case type::list_type:
+ l.copy(other.l);
+ break;
+ }
+ }
+
+ inline utree::utree(construct_list)
+ {
+ l.default_construct();
+ set_type(type::list_type);
+ }
+}
+
+#endif

Added: branches/release/libs/spirit/example/scheme/detail/utree_detail3.hpp
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/detail/utree_detail3.hpp 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,132 @@
+/*=============================================================================
+ Copyright (c) 2001-2010 Joel de Guzman
+
+ Distributed under the Boost Software License, Version 1.0. (See accompanying
+ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+#if !defined(BOOST_SPIRIT_UTREE_DETAIL3)
+#define BOOST_SPIRIT_UTREE_DETAIL3
+
+#include <boost/variant/get.hpp> // boost::bad_get
+
+namespace scheme
+{
+ namespace detail
+ {
+ ///////////////////////////////////////////////////////////////////////
+ template <typename T>
+ struct get_utree_type;
+
+#define SCHEME_GET_UTREE_TYPE(t, v) \
+ template <> struct get_utree_type<t> { enum { value = v }; } \
+ /**/
+
+ SCHEME_GET_UTREE_TYPE(nil, utree_type::nil_type);
+ SCHEME_GET_UTREE_TYPE(bool, utree_type::bool_type);
+ SCHEME_GET_UTREE_TYPE(int, utree_type::int_type);
+ SCHEME_GET_UTREE_TYPE(double, utree_type::double_type);
+ SCHEME_GET_UTREE_TYPE(utf8_string_range, utree_type::string_type);
+ SCHEME_GET_UTREE_TYPE(utf8_symbol_range, utree_type::symbol_type);
+ SCHEME_GET_UTREE_TYPE(binary_range, utree_type::binary_type);
+ SCHEME_GET_UTREE_TYPE(boost::iterator_range<utree::iterator>,
+ utree_type::list_type);
+ SCHEME_GET_UTREE_TYPE(boost::iterator_range<utree::const_iterator>,
+ utree_type::list_type);
+
+#undef SCHEME_GET_UTREE_TYPE
+
+ ///////////////////////////////////////////////////////////////////////
+ template <typename T>
+ struct get_impl;
+
+ template <>
+ struct get_impl<nil>
+ {
+ typedef nil type;
+ static type call(utree const&) { return nil(); }
+ };
+
+ template <>
+ struct get_impl<bool>
+ {
+ typedef bool type;
+ static type call(utree const& x) { return x.b; }
+ };
+
+ template <>
+ struct get_impl<int>
+ {
+ typedef int type;
+ static type call(utree const& x) { return x.i; }
+ };
+
+ template <>
+ struct get_impl<double>
+ {
+ typedef double type;
+ static type call(utree const& x) { return x.d; }
+ };
+
+ template <>
+ struct get_impl<boost::iterator_range<utree::iterator> >
+ {
+ typedef boost::iterator_range<utree::iterator> type;
+ static type call(utree const& x)
+ {
+ return type(utree::iterator(x.l.first), utree::iterator(0));
+ }
+ };
+
+ template <>
+ struct get_impl<boost::iterator_range<utree::const_iterator> >
+ {
+ typedef boost::iterator_range<utree::const_iterator> type;
+ static type call(utree const& x)
+ {
+ return type(utree::const_iterator(x.l.first), utree::const_iterator(0));
+ }
+ };
+
+ template <>
+ struct get_impl<utf8_string_range>
+ {
+ typedef utf8_string_range type;
+ static type call(utree const& x)
+ {
+ return type(x.s.str(), x.s.size());
+ }
+ };
+
+ template <>
+ struct get_impl<utf8_symbol_range>
+ {
+ typedef utf8_symbol_range type;
+ static type call(utree const& x)
+ {
+ return type(x.s.str(), x.s.size());
+ }
+ };
+
+ template <>
+ struct get_impl<binary_range>
+ {
+ typedef binary_range type;
+ static type call(utree const& x)
+ {
+ return type(x.s.str(), x.s.size());
+ }
+ };
+ }
+
+ template <typename T>
+ typename detail::get_impl<T>::type
+ get(utree const& x)
+ {
+ if (x.which() != detail::get_utree_type<T>::value)
+ throw boost::bad_get();
+
+ return detail::get_impl<T>::call(x);
+ }
+}
+
+#endif

Added: branches/release/libs/spirit/example/scheme/input/parse_sexpr.hpp
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/input/parse_sexpr.hpp 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,23 @@
+// Copyright (c) 2001-2010 Hartmut Kaiser
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+#if !defined(BOOST_SPIRIT_PARSE_SEXPR)
+#define BOOST_SPIRIT_PARSE_SEXPR
+
+#include "../utree.hpp"
+#include "../input/sexpr.hpp"
+
+namespace scheme { namespace input
+{
+ template <typename InputStream>
+ bool parse_sexpr(InputStream& is, utree& result);
+
+ template <typename InputStream>
+ bool parse_sexpr_list(InputStream& is, utree& result);
+}}
+
+#endif
+
+

Added: branches/release/libs/spirit/example/scheme/input/parse_sexpr_impl.hpp
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/input/parse_sexpr_impl.hpp 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,55 @@
+// Copyright (c) 2001-2010 Hartmut Kaiser
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+#if !defined(BOOST_SPIRIT_PARSE_SEXPR_IMPL)
+#define BOOST_SPIRIT_PARSE_SEXPR_IMPL
+
+#include <iostream>
+#include <boost/spirit/include/support_istream_iterator.hpp>
+#include <boost/spirit/include/qi_parse.hpp>
+
+#include "../input/sexpr.hpp"
+#include "../input/parse_sexpr.hpp"
+
+namespace scheme { namespace input
+{
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename InputStream>
+ bool parse_sexpr(InputStream& is, utree& result)
+ {
+ // no white space skipping in the stream!
+ is.unsetf(std::ios::skipws);
+
+ typedef boost::spirit::basic_istream_iterator<char> iterator_type;
+ iterator_type first(is);
+ iterator_type last;
+
+ scheme::input::sexpr<iterator_type> p;
+ scheme::input::white_space<iterator_type> ws;
+
+ return phrase_parse(first, last, p, ws, result);
+ }
+
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename InputStream>
+ bool parse_sexpr_list(InputStream& is, utree& result)
+ {
+ // no white space skipping in the stream!
+ is.unsetf(std::ios::skipws);
+
+ typedef boost::spirit::basic_istream_iterator<char> iterator_type;
+ iterator_type first(is);
+ iterator_type last;
+
+ scheme::input::sexpr<iterator_type> p;
+ scheme::input::white_space<iterator_type> ws;
+
+ return phrase_parse(first, last, +p, ws, result);
+ }
+}}
+
+#endif
+
+

Added: branches/release/libs/spirit/example/scheme/input/sexpr.hpp
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/input/sexpr.hpp 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,167 @@
+/*=============================================================================
+ Copyright (c) 2001-2010 Joel de Guzman
+
+ Distributed under the Boost Software License, Version 1.0. (See accompanying
+ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+#if !defined(BOOST_SPIRIT_SEXPR)
+#define BOOST_SPIRIT_SEXPR
+
+#include <string>
+
+#include <boost/cstdint.hpp>
+#include <boost/spirit/include/qi.hpp>
+#include <boost/spirit/include/phoenix_core.hpp>
+#include <boost/spirit/include/phoenix_container.hpp>
+#include <boost/spirit/include/phoenix_statement.hpp>
+#include <boost/spirit/include/phoenix_operator.hpp>
+#include <boost/regex/pending/unicode_iterator.hpp>
+
+#include "../utree.hpp"
+
+namespace scheme { namespace input
+{
+ using boost::spirit::ascii::char_;
+ using boost::spirit::ascii::space;
+ using boost::spirit::qi::grammar;
+ using boost::spirit::qi::rule;
+ using boost::spirit::qi::eol;
+ using boost::spirit::qi::_val;
+ using boost::spirit::qi::_r1;
+ using boost::spirit::qi::_1;
+ using boost::spirit::qi::uint_parser;
+ using boost::spirit::qi::real_parser;
+ using boost::spirit::qi::strict_real_policies;
+ using boost::spirit::qi::char_set;
+ using boost::spirit::qi::int_;
+ using boost::spirit::qi::hex;
+ using boost::spirit::qi::oct;
+ using boost::spirit::qi::bool_;
+ using boost::spirit::qi::no_case;
+ using boost::spirit::qi::lexeme;
+ using boost::spirit::qi::lit;
+ using boost::phoenix::function;
+
+ typedef boost::uint32_t uchar; // a unicode code point
+
+ template <typename Iterator>
+ struct white_space : grammar<Iterator>
+ {
+ white_space() : white_space::base_type(start)
+ {
+ start =
+ space // tab/space/cr/lf
+ | ';' >> *(char_ - eol) >> eol // comments
+ ;
+ }
+
+ rule<Iterator> start;
+ };
+
+ namespace detail
+ {
+ struct push_utf8
+ {
+ template <typename S, typename C>
+ struct result { typedef void type; };
+
+ void operator()(std::string& utf8, uchar code_point) const
+ {
+ typedef std::back_insert_iterator<std::string> insert_iter;
+ insert_iter out_iter(utf8);
+ boost::utf8_output_iterator<insert_iter> utf8_iter(out_iter);
+ *utf8_iter++ = code_point;
+ }
+ };
+
+ struct push_esc
+ {
+ template <typename S, typename C>
+ struct result { typedef void type; };
+
+ void operator()(std::string& utf8, uchar c) const
+ {
+ switch (c)
+ {
+ case 'b': utf8 += '\b'; break;
+ case 't': utf8 += '\t'; break;
+ case 'n': utf8 += '\n'; break;
+ case 'f': utf8 += '\f'; break;
+ case 'r': utf8 += '\r'; break;
+ case '"': utf8 += '"'; break;
+ case '\\': utf8 += '\\'; break;
+ }
+ }
+ };
+ }
+
+ template <typename Iterator>
+ struct string : grammar<Iterator, std::string()>
+ {
+ string() : string::base_type(start)
+ {
+ uint_parser<uchar, 16, 4, 4> hex4;
+ uint_parser<uchar, 16, 8, 8> hex8;
+ function<detail::push_utf8> push_utf8;
+ function<detail::push_esc> push_esc;
+
+ str_esc
+ = '\\'
+ >> ( ('u' >> hex4) [push_utf8(_r1, _1)]
+ | ('U' >> hex8) [push_utf8(_r1, _1)]
+ | char_("btnfr\\\"'") [push_esc(_r1, _1)]
+ )
+ ;
+
+ start
+ = '"'
+ >> *(str_esc(_val) | (~char_('"')) [_val += _1])
+ >> '"'
+ ;
+ }
+
+ rule<Iterator, void(std::string&)> str_esc;
+ rule<Iterator, std::string()> start;
+ };
+
+ template <typename Iterator>
+ struct sexpr : grammar<Iterator, white_space<Iterator>, utree()>
+ {
+ sexpr() : sexpr::base_type(start)
+ {
+ real_parser<double, strict_real_policies<double> > strict_double;
+ uint_parser<unsigned char, 16, 2, 2> hex2;
+
+ start = atom | list;
+
+ list = '(' >> *start >> ')';
+
+ atom = strict_double
+ | integer
+ | bool_
+ | string
+ | byte_str
+ | symbol
+ ;
+
+ char const* exclude = " ();\"\0-\31\127";
+ symbol = lexeme[+(~char_(exclude))];
+
+ integer = lexeme[no_case["0x"] >> hex]
+ | lexeme['0' >> oct]
+ | int_
+ ;
+
+ byte_str = lexeme[no_case['b'] >> +hex2];
+ }
+
+ rule<Iterator, white_space<Iterator>, utree()> start, list;
+ rule<Iterator, int()> integer;
+ rule<Iterator, utree()> atom;
+ rule<Iterator, utf8_symbol()> symbol;
+ rule<Iterator, binary_string()> byte_str;
+ scheme::input::string<Iterator> string;
+ };
+}}
+
+#endif

Added: branches/release/libs/spirit/example/scheme/out.txt
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/out.txt 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,2 @@
+success: (123.45 true false 255 63 "this is a € string" "Τη γλώσσα μου έδωσαν ελληνική" (92 ("another string" apple Sîne)))
+

Added: branches/release/libs/spirit/example/scheme/output/generate_sexpr.hpp
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/output/generate_sexpr.hpp 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,25 @@
+// Copyright (c) 2001-2010 Hartmut Kaiser
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+#if !defined(SCHEME_OUTPUT_GENERATE_SEXPR_MAR_29_2010_1210PM)
+#define SCHEME_OUTPUT_GENERATE_SEXPR_MAR_29_2010_1210PM
+
+#include "../utree.hpp"
+#include "../output/sexpr.hpp"
+
+namespace scheme { namespace output
+{
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename OutputStream>
+ bool generate_sexpr(OutputStream& os, utree const& tree);
+
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename OutputStream>
+ bool generate_sexpr_list(OutputStream& os, utree const& tree);
+}}
+
+#endif
+
+

Added: branches/release/libs/spirit/example/scheme/output/generate_sexpr_impl.hpp
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/output/generate_sexpr_impl.hpp 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,49 @@
+// Copyright (c) 2001-2010 Hartmut Kaiser
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+#if !defined(SCHEME_OUTPUT_GENERATE_SEXPR_IMPL_MAR_29_2010_1210PM)
+#define SCHEME_OUTPUT_GENERATE_SEXPR_MAR_IMPL_29_2010_1210PM
+
+#include <boost/spirit/include/karma_generate.hpp>
+#include <boost/spirit/include/karma_char.hpp>
+#include <boost/spirit/include/karma_list.hpp>
+#include <boost/spirit/include/support_ostream_iterator.hpp>
+
+#include "../output/sexpr.hpp"
+#include "../output/generate_sexpr.hpp"
+
+namespace scheme { namespace output
+{
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename OutputStream>
+ bool generate_sexpr(OutputStream& os, utree const& tree)
+ {
+ typedef boost::spirit::ostream_iterator output_iterator_type;
+ using boost::spirit::karma::space;
+
+ output_iterator_type sink(os);
+
+ scheme::output::sexpr<output_iterator_type> g;
+ return generate_delimited(sink, g, space, tree);
+ }
+
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename OutputStream>
+ bool generate_sexpr_list(OutputStream& os, utree const& tree)
+ {
+ typedef boost::spirit::ostream_iterator output_iterator_type;
+ using boost::spirit::karma::space;
+ using boost::spirit::karma::eol;
+
+ output_iterator_type sink(os);
+
+ scheme::output::sexpr<output_iterator_type> g;
+ return generate_delimited(sink, g % eol, space, tree);
+ }
+}}
+
+#endif
+
+

Added: branches/release/libs/spirit/example/scheme/output/sexpr.hpp
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/output/sexpr.hpp 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,185 @@
+// Copyright (c) 2001-2010 Hartmut Kaiser
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+#if !defined(SCHEME_OUTPUT_SEXPR_MAR_8_2010_829AM)
+#define SCHEME_OUTPUT_SEXPR_MAR_8_2010_829AM
+
+#include <string>
+
+#include <boost/cstdint.hpp>
+#include <boost/mpl/bool.hpp>
+#include <boost/spirit/include/karma.hpp>
+
+#include "../utree.hpp"
+#include "../detail/utree_detail3.hpp"
+
+///////////////////////////////////////////////////////////////////////////////
+namespace boost { namespace spirit { namespace traits
+{
+ // the specialization below tells Spirit to handle scheme::utree as if it
+ // where a 'real' variant
+ template <typename T>
+ struct not_is_variant;
+
+ template <>
+ struct not_is_variant<scheme::utree>
+ : mpl::false_ {};
+
+ // The specializations below tell Spirit to verify whether an attribute
+ // type is compatible with a given variant type
+ template <>
+ struct compute_compatible_component_variant<
+ boost::iterator_range<scheme::utree::iterator>, scheme::utree>
+ : mpl::true_
+ {
+ typedef boost::iterator_range<scheme::utree::iterator> compatible_type;
+ typedef mpl::int_<scheme::utree_type::list_type> distance;
+ };
+
+ template <>
+ struct compute_compatible_component_variant<
+ boost::iterator_range<scheme::utree::const_iterator>, scheme::utree>
+ : mpl::true_
+ {
+ typedef boost::iterator_range<scheme::utree::const_iterator> compatible_type;
+ typedef mpl::int_<scheme::utree_type::list_type> distance;
+ };
+
+ template <>
+ struct compute_compatible_component_variant<scheme::nil, scheme::utree>
+ : mpl::true_
+ {
+ typedef scheme::nil compatible_type;
+ typedef mpl::int_<scheme::utree_type::nil_type> distance;
+ };
+
+ template <>
+ struct compute_compatible_component_variant<bool, scheme::utree>
+ : mpl::true_
+ {
+ typedef bool compatible_type;
+ typedef mpl::int_<scheme::utree_type::bool_type> distance;
+ };
+
+ template <>
+ struct compute_compatible_component_variant<int, scheme::utree>
+ : mpl::true_
+ {
+ typedef int compatible_type;
+ typedef mpl::int_<scheme::utree_type::int_type> distance;
+ };
+
+ template <>
+ struct compute_compatible_component_variant<double, scheme::utree>
+ : mpl::true_
+ {
+ typedef double compatible_type;
+ typedef mpl::int_<scheme::utree_type::double_type> distance;
+ };
+
+ template <>
+ struct compute_compatible_component_variant<
+ scheme::utf8_string_range, scheme::utree>
+ : mpl::true_
+ {
+ typedef scheme::utf8_string_range compatible_type;
+ typedef mpl::int_<scheme::utree_type::string_type> distance;
+ };
+
+ template <>
+ struct compute_compatible_component_variant<
+ scheme::utf8_string, scheme::utree>
+ : mpl::true_
+ {
+ typedef scheme::utf8_string compatible_type;
+ typedef mpl::int_<scheme::utree_type::string_type> distance;
+ };
+
+ template <>
+ struct compute_compatible_component_variant<
+ scheme::utf8_symbol_range, scheme::utree>
+ : mpl::true_
+ {
+ typedef scheme::utf8_symbol_range compatible_type;
+ typedef mpl::int_<scheme::utree_type::symbol_type> distance;
+ };
+
+ template <>
+ struct compute_compatible_component_variant<
+ scheme::utf8_symbol, scheme::utree>
+ : mpl::true_
+ {
+ typedef scheme::utf8_symbol compatible_type;
+ typedef mpl::int_<scheme::utree_type::symbol_type> distance;
+ };
+
+ template <>
+ struct compute_compatible_component_variant<
+ scheme::binary_range, scheme::utree>
+ : mpl::true_
+ {
+ typedef scheme::binary_range compatible_type;
+ typedef mpl::int_<scheme::utree_type::binary_type> distance;
+ };
+
+ template <>
+ struct compute_compatible_component_variant<
+ scheme::binary_string, scheme::utree>
+ : mpl::true_
+ {
+ typedef scheme::binary_string compatible_type;
+ typedef mpl::int_<scheme::utree_type::binary_type> distance;
+ };
+}}}
+
+///////////////////////////////////////////////////////////////////////////////
+namespace scheme { namespace output
+{
+ using boost::spirit::karma::grammar;
+ using boost::spirit::karma::space_type;
+ using boost::spirit::karma::rule;
+ using boost::spirit::karma::double_;
+ using boost::spirit::karma::int_;
+ using boost::spirit::karma::char_;
+ using boost::spirit::karma::bool_;
+ using boost::spirit::karma::space_type;
+ using boost::spirit::karma::uint_generator;
+
+ typedef boost::uint32_t uchar; // a unicode code point
+
+ template <typename OutputIterator>
+ struct sexpr : grammar<OutputIterator, space_type, utree()>
+ {
+ sexpr() : sexpr::base_type(start)
+ {
+ uint_generator<unsigned char, 16> hex2;
+
+ start = double_
+ | int_
+ | bool_
+ | string
+ | symbol
+ | byte_str
+ | list
+ ;
+
+ list = '(' << *start << ')';
+
+ string = '"' << *char_ << '"';
+ symbol = *char_;
+ byte_str = 'b' << *hex2;
+ }
+
+ typedef boost::iterator_range<utree::const_iterator> utree_list;
+
+ rule<OutputIterator, space_type, utree()> start;
+ rule<OutputIterator, space_type, utree_list()> list;
+ rule<OutputIterator, utf8_symbol_range()> symbol;
+ rule<OutputIterator, utf8_string_range()> string;
+ rule<OutputIterator, binary_range()> byte_str;
+ };
+}}
+
+#endif

Added: branches/release/libs/spirit/example/scheme/sexpr.hpp
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/sexpr.hpp 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,179 @@
+/*=============================================================================
+ Copyright (c) 2001-2010 Joel de Guzman
+
+ Distributed under the Boost Software License, Version 1.0. (See accompanying
+ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+#if !defined(BOOST_SPIRIT_SEXPR)
+#define BOOST_SPIRIT_SEXPR
+
+#include <string>
+
+#define BOOST_SPIRIT_UNICODE // We'll use unicode (UTF8) all throughout
+
+#include <boost/config/warning_disable.hpp>
+#include <boost/cstdint.hpp>
+#include <boost/spirit/include/qi.hpp>
+#include <boost/spirit/include/phoenix_core.hpp>
+#include <boost/spirit/include/phoenix_container.hpp>
+#include <boost/spirit/include/phoenix_statement.hpp>
+#include <boost/spirit/include/phoenix_operator.hpp>
+#include <boost/regex/pending/unicode_iterator.hpp>
+
+#include "utree.hpp"
+
+namespace scheme
+{
+ using boost::spirit::unicode::char_;
+ using boost::spirit::unicode::space;
+ using boost::spirit::unicode::print;
+ using boost::spirit::qi::grammar;
+ using boost::spirit::qi::rule;
+ using boost::spirit::qi::eol;
+ using boost::spirit::qi::_val;
+ using boost::spirit::qi::_r1;
+ using boost::spirit::qi::_1;
+ using boost::spirit::qi::uint_parser;
+ using boost::spirit::qi::real_parser;
+ using boost::spirit::qi::strict_real_policies;
+ using boost::spirit::qi::char_set;
+ using boost::spirit::qi::int_;
+ using boost::spirit::qi::hex;
+ using boost::spirit::qi::oct;
+ using boost::spirit::qi::bool_;
+ using boost::spirit::qi::no_case;
+ using boost::spirit::qi::lexeme;
+ using boost::spirit::qi::lit;
+ using boost::phoenix::function;
+
+ typedef boost::spirit::char_encoding::unicode unicode;
+ typedef boost::uint32_t uchar; // a unicode code point
+
+ template <typename Iterator>
+ struct white_space : grammar<Iterator, unicode>
+ {
+ white_space() : white_space::base_type(start)
+ {
+ start =
+ space // tab/space/cr/lf
+ | ';' >> *(char_ - eol) >> eol // comments
+ ;
+ }
+
+ rule<Iterator, unicode> start;
+ };
+
+ namespace detail
+ {
+ struct push_utf8
+ {
+ template <typename S, typename C>
+ struct result { typedef void type; };
+
+ void operator()(std::string& utf8, uchar code_point) const
+ {
+ typedef std::back_insert_iterator<std::string> insert_iter;
+ insert_iter out_iter(utf8);
+ boost::utf8_output_iterator<insert_iter> utf8_iter(out_iter);
+ *utf8_iter++ = code_point;
+ }
+ };
+
+ struct push_symbol_utf8
+ {
+ template <typename S, typename C>
+ struct result { typedef void type; };
+
+ void operator()(std::string& utf8, uchar code_point) const
+ {
+ if (utf8.size() == 0)
+ utf8 += '\0'; // mark a symbol with prefix 0
+ // (a 0 byte at the beginning signifies a symbol)
+ push_utf8()(utf8, code_point);
+ }
+ };
+
+ struct push_esc
+ {
+ template <typename S, typename C>
+ struct result { typedef void type; };
+
+ void operator()(std::string& utf8, uchar c) const
+ {
+ switch (c)
+ {
+ case 'b': utf8 += '\b'; break;
+ case 't': utf8 += '\t'; break;
+ case 'n': utf8 += '\n'; break;
+ case 'f': utf8 += '\f'; break;
+ case 'r': utf8 += '\r'; break;
+ case '"': utf8 += '"'; break;
+ case '\\': utf8 += '\\'; break;
+ }
+ }
+ };
+ }
+
+ template <typename Iterator>
+ struct string : grammar<Iterator, unicode, std::string()>
+ {
+ string() : string::base_type(start)
+ {
+ uint_parser<uchar, 16> hex;
+ function<detail::push_utf8> push_utf8;
+ function<detail::push_esc> push_esc;
+
+ str_esc
+ = '\\'
+ >> ( ('u' >> hex) [push_utf8(_r1, _1)]
+ | char_("btnfr\\\"'") [push_esc(_r1, _1)]
+ )
+ ;
+
+ start
+ = '"'
+ >> *(str_esc(_val) | (char_ - '"') [push_utf8(_val, _1)])
+ >> '"'
+ ;
+ }
+
+ rule<Iterator, unicode, void(std::string&)> str_esc;
+ rule<Iterator, unicode, std::string()> start;
+ };
+
+ template <typename Iterator>
+ struct sexpr : grammar<Iterator, unicode, white_space<Iterator>, utree()>
+ {
+ sexpr() : sexpr::base_type(start)
+ {
+ real_parser<double, strict_real_policies<double> > strict_double;
+ function<detail::push_symbol_utf8> push_symbol_utf8;
+
+ start = atom | list;
+
+ list = '(' >> *start >> ')';
+
+ atom = number [_val = _1]
+ | bool_ [_val = _1]
+ | string [_val = _1]
+ | symbol [_val = _1]
+ ;
+
+ char const* exclude = " ();\"\n\r\t";
+ symbol = +lexeme[print - char_(exclude)] [push_symbol_utf8(_val, _1)];
+
+ number = strict_double [_val = _1]
+ | lexeme[no_case["0x"] >> hex] [_val = _1]
+ | lexeme['0' >> oct] [_val = _1]
+ | int_ [_val = _1]
+ ;
+ }
+
+ rule<Iterator, unicode, white_space<Iterator>, utree()> start, list;
+ rule<Iterator, unicode, utree()> atom, number;
+ rule<Iterator, unicode, std::string()> symbol;
+ scheme::string<Iterator> string;
+ };
+}
+
+#endif

Added: branches/release/libs/spirit/example/scheme/sexpr_test.cpp
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/sexpr_test.cpp 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,93 @@
+/*=============================================================================
+ Copyright (c) 2001-2010 Joel de Guzman
+
+ Distributed under the Boost Software License, Version 1.0. (See accompanying
+ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+#include "sexpr.hpp"
+#include "simple_print.hpp"
+#include <iostream>
+#include <fstream>
+
+namespace scheme
+{
+ inline std::ostream& operator<<(std::ostream& out, utree const& x)
+ {
+ using ::detail::println;
+ println(x);
+ return out;
+ }
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// Main program
+///////////////////////////////////////////////////////////////////////////////
+int main(int argc, char **argv)
+{
+ using ::detail::println;
+
+ char const* filename;
+ if (argc > 1)
+ {
+ filename = argv[1];
+ }
+ else
+ {
+ std::cerr << "Error: No input file provided." << std::endl;
+ return 1;
+ }
+
+ std::ifstream in(filename, std::ios_base::in);
+
+ if (!in)
+ {
+ std::cerr << "Error: Could not open input file: "
+ << filename << std::endl;
+ return 1;
+ }
+
+ // Ignore the BOM marking the beginning of a UTF-8 file in Windows
+ char c = in.peek();
+ if (c == '\xef')
+ {
+ char s[3];
+ in >> s[0] >> s[1] >> s[2];
+ s[3] = '\0';
+ if (s != std::string("\xef\xbb\xbf"))
+ {
+ std::cerr << "Error: Unexpected characters from input file: "
+ << filename << std::endl;
+ return 1;
+ }
+ }
+
+ std::string source_code; // We will read the contents here.
+ in.unsetf(std::ios::skipws); // No white space skipping!
+ std::copy(
+ std::istream_iterator<char>(in),
+ std::istream_iterator<char>(),
+ std::back_inserter(source_code));
+
+ typedef boost::u8_to_u32_iterator<std::string::const_iterator> iterator_type;
+ iterator_type first(source_code.begin());
+ iterator_type last(source_code.end());
+
+ scheme::sexpr<iterator_type> p;
+ scheme::white_space<iterator_type> ws;
+
+ scheme::utree result;
+ if (phrase_parse(first, last, p, ws, result))
+ {
+ std::cout << "success: ";
+ println(result);
+ std::cout << std::endl;
+ }
+ else
+ {
+ std::cout << "parse error" << std::endl;
+ }
+
+ return 0;
+}
+
+

Added: branches/release/libs/spirit/example/scheme/sexpr_test.txt
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/sexpr_test.txt 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,12 @@
+(
+ 123.45
+ true
+ false
+ 0xFF
+ 077
+ "this is a \u20AC string" ; A UTF-8 string
+ "Τη γλώσσα μου έδωσαν ελληνική" ; Another UTF-8 string
+ (
+ 92 ("another string" apple Sîne)
+ )
+)
\ No newline at end of file

Added: branches/release/libs/spirit/example/scheme/simple_print.hpp
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/simple_print.hpp 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,92 @@
+#if !defined(UTREE_SIMPLE_PRINT)
+#define UTREE_SIMPLE_PRINT
+
+#include "utree.hpp"
+#include <iostream>
+
+namespace detail
+{
+ ///////////////////////////////////////////////////////////////////////////
+ // simple utree printing facility prints the utree in a single line
+ ///////////////////////////////////////////////////////////////////////////
+ void print(char ch);
+ void print(scheme::utree const& val);
+ void println(scheme::utree const& val);
+
+ // simple_print visitor
+ struct simple_print
+ {
+ typedef void result_type;
+
+ void operator()(scheme::utree::nil) const
+ {
+ std::cout << "nil";
+ }
+
+ template <typename T>
+ void operator()(T val) const
+ {
+ std::cout << val;
+ }
+
+ void operator()(bool b) const
+ {
+ std::cout << (b ? "true" : "false");
+ }
+
+ template <typename Range> // for lists
+ void print_string_or_list(Range range, boost::mpl::false_) const
+ {
+ typedef typename Range::const_iterator iterator;
+ print('(');
+ for (iterator i = range.begin(); i != range.end(); ++i)
+ {
+ if (i != range.begin())
+ detail::print(' ');
+ detail::print(*i);
+ }
+ detail::print(')');
+ }
+
+ template <typename Range> // for strings
+ void print_string_or_list(Range range, boost::mpl::true_) const
+ {
+ typedef typename Range::const_iterator iterator;
+ iterator i = range.begin();
+ bool const is_symbol = *i == '\0'; // a 0 byte at the beginning signifies a symbol
+ if (!is_symbol)
+ detail::print('"');
+ else
+ ++i;
+ for (; i != range.end(); ++i)
+ detail::print(*i);
+ if (!is_symbol)
+ detail::print('"');
+ }
+
+ template <typename Iterator>
+ void operator()(boost::iterator_range<Iterator> const& range) const
+ {
+ // This code works for both strings and lists
+ print_string_or_list(range, boost::is_pointer<Iterator>());
+ }
+ };
+
+ inline void print(char ch)
+ {
+ std::cout << ch;
+ }
+
+ inline void print(scheme::utree const& val)
+ {
+ scheme::utree::visit(val, simple_print());
+ }
+
+ inline void println(scheme::utree const& val)
+ {
+ detail::print(val);
+ std::cout << std::endl;
+ }
+}
+
+#endif
\ No newline at end of file

Added: branches/release/libs/spirit/example/scheme/test/out.txt
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/test/out.txt 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,2 @@
+success: (123.45 true false 255 63 "this is a € string" "Τη γλώσσα μου έδωσαν ελληνική" b0123456789abcdef123456789abcdef (92 ("another string" apple Sîne)))
+

Added: branches/release/libs/spirit/example/scheme/test/scheme.cpp
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/test/scheme.cpp 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,148 @@
+/*=============================================================================
+ Copyright (c) 2001-2010 Joel de Guzman
+
+ Distributed under the Boost Software License, Version 1.0. (See accompanying
+ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+#include <boost/config/warning_disable.hpp>
+
+#include "../input/sexpr.hpp"
+#include "../input/parse_sexpr_impl.hpp"
+#include <iostream>
+#include <fstream>
+
+inline std::ostream& println(std::ostream& out, scheme::utree const& val)
+{
+ out << val << std::endl;
+ return out;
+}
+
+#include <boost/unordered_map.hpp>
+
+namespace scheme
+{
+ class environment
+ {
+ public:
+
+ environment(environment* parent = 0)
+ : bindings(), parent(parent) {}
+
+ void define(std::string const& name, utree const& def)
+ {
+ // check for duplicate names
+ BOOST_ASSERT(bindings.find(name) == bindings.end());
+ // $$$ TODO Use exceptions $$$
+ bindings[name] = def;
+ }
+
+ utree* find(std::string const& name)
+ {
+ map::iterator i = bindings.find(name);
+ if (i == bindings.end())
+ {
+ if (parent)
+ return parent->find(name);
+ return 0;
+ }
+ return &i->second;
+ }
+
+ private:
+
+ typedef boost::unordered_map<std::string, utree> map;
+ map bindings;
+ environment* parent;
+ };
+
+ ///////////////////////////////////////////////////////////////////////////
+ // Intrinsic functions
+ ///////////////////////////////////////////////////////////////////////////
+ struct arithmetic_function
+ {
+ typedef bool result_type;
+
+ template <typename A, typename B>
+ bool dispatch(const A&, const B&, boost::mpl::false_) const
+ {
+ return false; // cannot compare different types by default
+ }
+
+ template <typename A, typename B>
+ bool dispatch(const A& a, const B& b, boost::mpl::true_) const
+ {
+ return a == b; // for arithmetic types
+ }
+
+ template <typename A, typename B>
+ bool operator()(const A& a, const B& b) const
+ {
+ return dispatch(a, b,
+ boost::mpl::and_<
+ boost::is_arithmetic<A>,
+ boost::is_arithmetic<B> >());
+ }
+ };
+
+ utree plus(environment*, utree& args)
+ {
+
+ }
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// Main program
+///////////////////////////////////////////////////////////////////////////////
+int main(int argc, char **argv)
+{
+ char const* filename;
+ if (argc > 1)
+ {
+ filename = argv[1];
+ }
+ else
+ {
+ std::cerr << "Error: No input file provided." << std::endl;
+ return 1;
+ }
+
+ std::ifstream in(filename, std::ios_base::in);
+
+ if (!in)
+ {
+ std::cerr << "Error: Could not open input file: "
+ << filename << std::endl;
+ return 1;
+ }
+
+ // Ignore the BOM marking the beginning of a UTF-8 file in Windows
+ char c = in.peek();
+ if (c == '\xef')
+ {
+ char s[3];
+ in >> s[0] >> s[1] >> s[2];
+ s[3] = '\0';
+ if (s != std::string("\xef\xbb\xbf"))
+ {
+ std::cerr << "Error: Unexpected characters from input file: "
+ << filename << std::endl;
+ return 1;
+ }
+ }
+
+ scheme::utree result;
+ if (scheme::input::parse_sexpr(in, result))
+ {
+ std::cout << "success: ";
+ println(std::cout, result);
+ std::cout << std::endl;
+ }
+ else
+ {
+ std::cout << "parse error" << std::endl;
+ }
+
+ return 0;
+}
+
+

Added: branches/release/libs/spirit/example/scheme/test/sexpr_input_test.cpp
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/test/sexpr_input_test.cpp 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,75 @@
+/*=============================================================================
+ Copyright (c) 2001-2010 Joel de Guzman
+
+ Distributed under the Boost Software License, Version 1.0. (See accompanying
+ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+#include <boost/config/warning_disable.hpp>
+
+#include "../input/sexpr.hpp"
+#include "../input/parse_sexpr_impl.hpp"
+#include <iostream>
+#include <fstream>
+
+inline std::ostream& println(std::ostream& out, scheme::utree const& val)
+{
+ out << val << std::endl;
+ return out;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// Main program
+///////////////////////////////////////////////////////////////////////////////
+int main(int argc, char **argv)
+{
+ char const* filename;
+ if (argc > 1)
+ {
+ filename = argv[1];
+ }
+ else
+ {
+ std::cerr << "Error: No input file provided." << std::endl;
+ return 1;
+ }
+
+ std::ifstream in(filename, std::ios_base::in);
+
+ if (!in)
+ {
+ std::cerr << "Error: Could not open input file: "
+ << filename << std::endl;
+ return 1;
+ }
+
+ // Ignore the BOM marking the beginning of a UTF-8 file in Windows
+ char c = in.peek();
+ if (c == '\xef')
+ {
+ char s[3];
+ in >> s[0] >> s[1] >> s[2];
+ s[3] = '\0';
+ if (s != std::string("\xef\xbb\xbf"))
+ {
+ std::cerr << "Error: Unexpected characters from input file: "
+ << filename << std::endl;
+ return 1;
+ }
+ }
+
+ scheme::utree result;
+ if (scheme::input::parse_sexpr(in, result))
+ {
+ std::cout << "success: ";
+ println(std::cout, result);
+ std::cout << std::endl;
+ }
+ else
+ {
+ std::cout << "parse error" << std::endl;
+ }
+
+ return 0;
+}
+
+

Added: branches/release/libs/spirit/example/scheme/test/sexpr_output_test.cpp
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/test/sexpr_output_test.cpp 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,105 @@
+// Copyright (c) 2001-2010 Hartmut Kaiser
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+#include <boost/config/warning_disable.hpp>
+
+#define BOOST_SPIRIT_UNICODE
+
+#include <iostream>
+#include <fstream>
+#include <iterator>
+
+#include "../input/parse_sexpr_impl.hpp"
+#include "../output/generate_sexpr_impl.hpp"
+
+namespace client
+{
+ bool parse_sexpr_from_file(char const* filename, scheme::utree& result)
+ {
+ std::ifstream in(filename, std::ios_base::in);
+
+ if (!in)
+ {
+ std::cerr << "Error: Could not open input file: "
+ << filename << std::endl;
+ exit(-1);
+ }
+
+ // Ignore the BOM marking the beginning of a UTF-8 file in Windows
+ char c = in.peek();
+ if (c == '\xef')
+ {
+ char s[3];
+ in >> s[0] >> s[1] >> s[2];
+ s[3] = '\0';
+ if (s != std::string("\xef\xbb\xbf"))
+ {
+ std::cerr << "Error: Unexpected characters from input file: "
+ << filename << std::endl;
+ exit(-1);
+ }
+ }
+
+ return scheme::input::parse_sexpr(in, result);
+ }
+
+ bool generate_sexpr_to_file(scheme::utree const& tree, char const* filename)
+ {
+ std::ofstream out(filename);
+
+ if (!out)
+ {
+ std::cerr << "Error: Could not open output file: "
+ << filename << std::endl;
+ exit(-1);
+ }
+
+ return scheme::output::generate_sexpr(out, tree);
+ }
+}
+
+int main(int argc, char **argv)
+{
+ char const* filename_in;
+ if (argc > 1)
+ {
+ filename_in = argv[1];
+ }
+ else
+ {
+ std::cerr << "Error: No input file provided." << std::endl;
+ return -1;
+ }
+
+ char const* filename_out;
+ if (argc > 2)
+ {
+ filename_out = argv[2];
+ }
+ else
+ {
+ std::cerr << "Error: No output file provided." << std::endl;
+ return -1;
+ }
+
+ scheme::utree result;
+ if (client::parse_sexpr_from_file(filename_in, result))
+ {
+ if (client::generate_sexpr_to_file(result, filename_out))
+ {
+ std::cout << "success!" << std::endl;
+ }
+ else
+ {
+ std::cout << "generate error" << std::endl;
+ }
+ }
+ else
+ {
+ std::cout << "parse error" << std::endl;
+ }
+
+ return 0;
+}

Added: branches/release/libs/spirit/example/scheme/test/sexpr_test.txt
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/test/sexpr_test.txt 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,13 @@
+(
+ 123.45
+ true
+ false
+ 0xFF
+ 077
+ "this is a \u20AC string" ; A UTF-8 string
+ "Τη γλώσσα μου έδωσαν ελληνική" ; Another UTF-8 string
+ b0123456789ABCDEF0123456789abcdef ; A binary stream
+ (
+ 92 ("another string" apple Sîne)
+ )
+)
\ No newline at end of file

Added: branches/release/libs/spirit/example/scheme/test/utree_test.cpp
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/test/utree_test.cpp 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,224 @@
+/*=============================================================================
+ Copyright (c) 2001-2010 Joel de Guzman
+
+ Distributed under the Boost Software License, Version 1.0. (See accompanying
+ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+
+#include <boost/config/warning_disable.hpp>
+
+#include "../utree.hpp"
+#include "../utree_operators.hpp"
+#include <iostream>
+
+inline std::ostream& println(std::ostream& out, scheme::utree const& val)
+{
+ out << val << std::endl;
+ return out;
+}
+
+int main()
+{
+ using scheme::utree;
+
+ {
+ // test the size
+ std::cout << "size of utree is: "
+ << sizeof(scheme::utree) << " bytes" << std::endl;
+ }
+
+ {
+ utree val;
+ println(std::cout, val);
+ }
+
+ {
+ utree val(true);
+ println(std::cout, val);
+ }
+
+ {
+ utree val(123);
+ println(std::cout, val);
+ }
+
+ {
+ utree val(123.456);
+ println(std::cout, val);
+ }
+
+ {
+ utree val("Hello, World");
+ println(std::cout, val);
+ utree val2;
+ val2 = val;
+ println(std::cout, val2);
+ utree val3("Hello, World. Chuckie is back!!!");
+ val = val3;
+ println(std::cout, val);
+
+ utree val4("Apple");
+ utree val5("Apple");
+ BOOST_ASSERT(val4 == val5);
+
+ utree val6("ApplePie");
+ BOOST_ASSERT(val4 < val6);
+ }
+
+ {
+ utree val;
+ val.push_back(123);
+ val.push_back("Chuckie");
+ utree val2;
+ val2.push_back(123.456);
+ val2.push_back("Mah Doggie");
+ val.push_back(val2);
+ println(std::cout, val);
+ println(std::cout, val.front());
+
+ utree val3;
+ val3.swap(val);
+ println(std::cout, val);
+ val3.swap(val);
+ println(std::cout, val);
+ val.push_back("another string");
+ println(std::cout, val);
+ val.pop_front();
+ println(std::cout, val);
+ utree::iterator i = val.begin();
+ ++++i;
+ val.insert(i, "Right in the middle");
+ BOOST_ASSERT(val.size() == 4);
+ println(std::cout, val);
+ val.pop_back();
+ println(std::cout, val);
+ BOOST_ASSERT(val.size() == 3);
+ val.erase(val.end());
+ println(std::cout, val);
+ BOOST_ASSERT(val.size() == 2);
+
+ val.insert(val.begin(), val2.begin(), val2.end());
+ println(std::cout, val);
+ }
+
+ {
+ utree val;
+ val.insert(val.end(), 123);
+ val.insert(val.end(), "Mia");
+ val.insert(val.end(), "Chuckie");
+ val.insert(val.end(), "Poly");
+ val.insert(val.end(), "Mochi");
+ println(std::cout, val);
+ }
+
+ {
+ utree a, b;
+ BOOST_ASSERT(a == b);
+ a = 123;
+ BOOST_ASSERT(a != b);
+ b = 123;
+ BOOST_ASSERT(a == b);
+ a = 100.00;
+ BOOST_ASSERT(a < b);
+
+ b = a = utree();
+ BOOST_ASSERT(a == b);
+ a.push_back(1);
+ a.push_back("two");
+ a.push_back(3.0);
+ b.push_back(1);
+ b.push_back("two");
+ b.push_back(3.0);
+ BOOST_ASSERT(a == b);
+ b.push_back(4);
+ BOOST_ASSERT(a != b);
+ BOOST_ASSERT(a < b);
+ }
+
+ {
+ utree a;
+ a.push_back(1);
+ a.push_back(2);
+ a.push_back(3);
+ a.push_back(4);
+ a.push_back(5);
+ a.push_back(6);
+ a.push_back(7);
+ a.push_back(8);
+ a.push_back(9);
+ a.push_back(10);
+ a.push_back(11);
+ a.push_back(12);
+
+ BOOST_ASSERT(a[0] == utree(1));
+ BOOST_ASSERT(a[1] == utree(2));
+ BOOST_ASSERT(a[2] == utree(3));
+ BOOST_ASSERT(a[3] == utree(4));
+ BOOST_ASSERT(a[4] == utree(5));
+ BOOST_ASSERT(a[5] == utree(6));
+ BOOST_ASSERT(a[6] == utree(7));
+ BOOST_ASSERT(a[7] == utree(8));
+ BOOST_ASSERT(a[8] == utree(9));
+ BOOST_ASSERT(a[9] == utree(10));
+ BOOST_ASSERT(a[10] == utree(11));
+ BOOST_ASSERT(a[11] == utree(12));
+ }
+
+ { // test references
+ utree val(123);
+ utree ref(boost::ref(val));
+ println(std::cout, ref);
+ BOOST_ASSERT(ref == utree(123));
+
+ val.clear();
+ val.push_back(1);
+ val.push_back(2);
+ val.push_back(3);
+ val.push_back(4);
+ println(std::cout, ref);
+ BOOST_ASSERT(ref[0] == utree(1));
+ BOOST_ASSERT(ref[1] == utree(2));
+ BOOST_ASSERT(ref[2] == utree(3));
+ BOOST_ASSERT(ref[3] == utree(4));
+ }
+
+ { // put it in an array
+
+ utree vals[] = {
+ utree(123),
+ utree("Hello, World"),
+ utree(123.456)
+ };
+
+ println(std::cout, vals[0]);
+ println(std::cout, vals[1]);
+ println(std::cout, vals[2]);
+ }
+
+ { // operators
+
+ BOOST_ASSERT((utree(true) && utree(true)) == utree(true));
+ BOOST_ASSERT((utree(true) || utree(false)) == utree(true));
+ BOOST_ASSERT(!utree(true) == utree(false));
+
+ BOOST_ASSERT((utree(456) + utree(123)) == utree(456 + 123));
+ BOOST_ASSERT((utree(456) + utree(123.456)) == utree(456 + 123.456));
+ BOOST_ASSERT((utree(456) - utree(123)) == utree(456 - 123));
+ BOOST_ASSERT((utree(456) - utree(123.456)) == utree(456 - 123.456));
+ BOOST_ASSERT((utree(456) * utree(123)) == utree(456 * 123));
+ BOOST_ASSERT((utree(456) * utree(123.456)) == utree(456 * 123.456));
+ BOOST_ASSERT((utree(456) / utree(123)) == utree(456 / 123));
+ BOOST_ASSERT((utree(456) / utree(123.456)) == utree(456 / 123.456));
+ BOOST_ASSERT((utree(456) % utree(123)) == utree(456 % 123));
+ BOOST_ASSERT(-utree(456) == utree(-456));
+
+ BOOST_ASSERT((utree(456) & utree(123)) == utree(456 & 123));
+ BOOST_ASSERT((utree(456) | utree(123)) == utree(456 | 123));
+ BOOST_ASSERT((utree(456) ^ utree(123)) == utree(456 ^ 123));
+ BOOST_ASSERT((utree(456) << utree(3)) == utree(456 << 3));
+ BOOST_ASSERT((utree(456) >> utree(2)) == utree(456 >> 2));
+ BOOST_ASSERT(~utree(456) == utree(~456));
+ }
+
+ return 0;
+}

Added: branches/release/libs/spirit/example/scheme/utree.hpp
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/utree.hpp 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,299 @@
+/*=============================================================================
+ Copyright (c) 2001-2010 Joel de Guzman
+
+ Distributed under the Boost Software License, Version 1.0. (See accompanying
+ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+#if !defined(BOOST_SPIRIT_UTREE)
+#define BOOST_SPIRIT_UTREE
+
+#include <cstddef>
+#include <algorithm>
+#include <string>
+#include <ostream>
+
+#include <boost/assert.hpp>
+#include <boost/noncopyable.hpp>
+#include <boost/iterator/iterator_facade.hpp>
+#include <boost/range/iterator_range.hpp>
+#include <boost/ref.hpp>
+#include "detail/utree_detail1.hpp"
+
+namespace scheme
+{
+ ///////////////////////////////////////////////////////////////////////////
+ // Our utree can store these types. This enum tells us what type
+ // of data is stored in utree's discriminated union.
+ ///////////////////////////////////////////////////////////////////////////
+ struct utree_type
+ {
+ enum info
+ {
+ nil_type,
+ bool_type,
+ int_type,
+ double_type,
+ string_type,
+ symbol_type,
+ binary_type,
+ list_type,
+ reference_type,
+ function_type
+ };
+ };
+
+ ///////////////////////////////////////////////////////////////////////////
+ // The nil type
+ ///////////////////////////////////////////////////////////////////////////
+ struct nil {};
+
+ ///////////////////////////////////////////////////////////////////////////
+ // The environment (this is forward declared)
+ ///////////////////////////////////////////////////////////////////////////
+ class environment;
+
+ ///////////////////////////////////////////////////////////////////////////
+ // Function pointer
+ ///////////////////////////////////////////////////////////////////////////
+ class utree; // forward
+ typedef utree (*function_ptr)(environment* env, utree& args);
+
+ ///////////////////////////////////////////////////////////////////////////
+ // A typed string with parametric Base storage. The storage can be any
+ // range or (stl container) of chars.
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename Base, utree_type::info type_>
+ struct basic_string : Base
+ {
+ static utree_type::info const type = type_;
+
+ basic_string()
+ : Base() {}
+
+ basic_string(Base const& base)
+ : Base(base) {}
+
+ template <typename Iterator>
+ basic_string(Iterator bits, std::size_t len)
+ : Base(bits, bits + len) {}
+
+ template <typename Iterator>
+ basic_string(Iterator first, Iterator last)
+ : Base(first, last) {}
+
+ basic_string& operator=(basic_string const& other)
+ {
+ Base::operator=(other);
+ return *this;
+ }
+
+ basic_string& operator=(Base const& other)
+ {
+ Base::operator=(other);
+ return *this;
+ }
+ };
+
+ ///////////////////////////////////////////////////////////////////////////
+ // Binary string
+ ///////////////////////////////////////////////////////////////////////////
+ typedef basic_string<
+ boost::iterator_range<char const*>,
+ utree_type::binary_type>
+ binary_range;
+
+ typedef basic_string<
+ std::string,
+ utree_type::binary_type>
+ binary_string;
+
+ ///////////////////////////////////////////////////////////////////////////
+ // Our UTF-8 string
+ ///////////////////////////////////////////////////////////////////////////
+ typedef basic_string<
+ boost::iterator_range<char const*>,
+ utree_type::string_type>
+ utf8_string_range;
+
+ typedef basic_string<
+ std::string,
+ utree_type::string_type>
+ utf8_string;
+
+ ///////////////////////////////////////////////////////////////////////////
+ // Our UTF-8 symbol (for identifiers)
+ ///////////////////////////////////////////////////////////////////////////
+ typedef basic_string<
+ boost::iterator_range<char const*>,
+ utree_type::symbol_type>
+ utf8_symbol_range;
+
+ typedef basic_string<
+ std::string,
+ utree_type::symbol_type>
+ utf8_symbol;
+
+ ///////////////////////////////////////////////////////////////////////////
+ // The main utree (Universal Tree) class
+ // The utree is a hierarchical, dynamic type that can store:
+ // - a nil
+ // - a bool
+ // - an integer
+ // - a double
+ // - a string
+ // - a symbol (identifier)
+ // - binary data
+ // - a (doubly linked) list of utree
+ // - a reference to a utree
+ //
+ // The utree has minimal memory footprint. The data structure size is
+ // 16 bytes on a 32-bit platform. Being a container of itself, it can
+ // represent tree structures.
+ ///////////////////////////////////////////////////////////////////////////
+ class utree
+ {
+ public:
+
+ typedef utree value_type;
+ typedef detail::list::node_iterator<utree> iterator;
+ typedef detail::list::node_iterator<utree const> const_iterator;
+ typedef utree& reference;
+ typedef utree const& const_reference;
+ typedef std::ptrdiff_t difference_type;
+ typedef std::size_t size_type;
+
+ typedef boost::iterator_range<iterator> range;
+ typedef boost::iterator_range<const_iterator> const_range;
+
+ utree();
+ explicit utree(bool b);
+ explicit utree(unsigned int i);
+ explicit utree(int i);
+ explicit utree(double d);
+ explicit utree(char const* str);
+ explicit utree(char const* str, std::size_t len);
+ explicit utree(std::string const& str);
+ explicit utree(boost::reference_wrapper<utree> ref);
+ explicit utree(function_ptr fptr);
+
+ template <typename Base, utree_type::info type_>
+ explicit utree(basic_string<Base, type_> const& bin);
+
+ utree(utree const& other);
+ ~utree();
+
+ utree& operator=(utree const& other);
+ utree& operator=(bool b);
+ utree& operator=(unsigned int i);
+ utree& operator=(int i);
+ utree& operator=(double d);
+ utree& operator=(char const* s);
+ utree& operator=(std::string const& s);
+ utree& operator=(boost::reference_wrapper<utree> ref);
+ utree& operator=(function_ptr fptr);
+
+ template <typename Base, utree_type::info type_>
+ utree& operator=(basic_string<Base, type_> const& bin);
+
+ template <typename F>
+ typename F::result_type
+ static visit(utree const& x, F f);
+
+ template <typename F>
+ typename F::result_type
+ static visit(utree& x, F f);
+
+ template <typename F>
+ typename F::result_type
+ static visit(utree const& x, utree const& y, F f);
+
+ template <typename F>
+ typename F::result_type
+ static visit(utree& x, utree const& y, F f);
+
+ template <typename F>
+ typename F::result_type
+ static visit(utree const& x, utree& y, F f);
+
+ template <typename F>
+ typename F::result_type
+ static visit(utree& x, utree& y, F f);
+
+ template <typename T>
+ void push_back(T const& val);
+
+ template <typename T>
+ void push_front(T const& val);
+
+ template <typename T>
+ iterator insert(iterator pos, T const& x);
+
+ template <typename T>
+ void insert(iterator pos, std::size_t, T const& x);
+
+ template <typename Iter>
+ void insert(iterator pos, Iter first, Iter last);
+
+ template <typename Iter>
+ void assign(Iter first, Iter last);
+
+ void clear();
+ void pop_front();
+ void pop_back();
+ iterator erase(iterator pos);
+ iterator erase(iterator first, iterator last);
+
+ utree& front();
+ utree& back();
+ utree const& front() const;
+ utree const& back() const;
+
+ utree& operator[](std::size_t i);
+ utree const& operator[](std::size_t i) const;
+
+ void swap(utree& other);
+
+ iterator begin();
+ iterator end();
+ const_iterator begin() const;
+ const_iterator end() const;
+
+ bool empty() const;
+ std::size_t size() const;
+
+ int which() const;
+
+ private:
+
+ typedef utree_type type;
+
+ template <typename UTreeX, typename UTreeY>
+ friend struct detail::visit_impl;
+ friend struct detail::index_impl;
+ friend struct ulist;
+ template <typename T> friend struct detail::get_impl;
+
+ type::info get_type() const;
+ void set_type(type::info t);
+ void ensure_list_type();
+ void free();
+ void copy(utree const& other);
+
+ struct construct_list {};
+ utree(construct_list);
+
+ union
+ {
+ detail::fast_string s;
+ detail::list l;
+ bool b;
+ int i;
+ double d;
+ utree* p;
+ function_ptr f;
+ };
+ };
+}
+
+#include "detail/utree_detail2.hpp"
+#endif

Added: branches/release/libs/spirit/example/scheme/utree_operators.hpp
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/utree_operators.hpp 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,503 @@
+/*=============================================================================
+ Copyright (c) 2001-2010 Joel de Guzman
+
+ Distributed under the Boost Software License, Version 1.0. (See accompanying
+ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+#if !defined(BOOST_SPIRIT_UTREE_OPERATORS)
+#define BOOST_SPIRIT_UTREE_OPERATORS
+
+#include "utree.hpp"
+#include <boost/preprocessor/cat.hpp>
+#include <boost/type_traits/is_arithmetic.hpp>
+#include <boost/type_traits/is_integral.hpp>
+
+namespace scheme
+{
+ // Relational operators
+ bool operator==(utree const& a, utree const& b);
+ bool operator<(utree const& a, utree const& b);
+ bool operator!=(utree const& a, utree const& b);
+ bool operator>(utree const& a, utree const& b);
+ bool operator<=(utree const& a, utree const& b);
+ bool operator>=(utree const& a, utree const& b);
+
+ // Printing
+ std::ostream& operator<<(std::ostream& out, utree const& x);
+
+ // Logical operators
+ utree operator&&(utree const& a, utree const& b);
+ utree operator||(utree const& a, utree const& b);
+ utree operator!(utree const& a);
+
+ // Arithmetic operators
+ utree operator+(utree const& a, utree const& b);
+ utree operator-(utree const& a, utree const& b);
+ utree operator*(utree const& a, utree const& b);
+ utree operator/(utree const& a, utree const& b);
+ utree operator%(utree const& a, utree const& b);
+ utree operator-(utree const& a);
+
+ // Bitwise operators
+ utree operator&(utree const& a, utree const& b);
+ utree operator|(utree const& a, utree const& b);
+ utree operator^(utree const& a, utree const& b);
+ utree operator<<(utree const& a, utree const& b);
+ utree operator>>(utree const& a, utree const& b);
+ utree operator~(utree const& a);
+
+ // Implementation
+ struct utree_is_equal
+ {
+ typedef bool result_type;
+
+ template <typename A, typename B>
+ bool dispatch(const A&, const B&, boost::mpl::false_) const
+ {
+ return false; // cannot compare different types by default
+ }
+
+ template <typename A, typename B>
+ bool dispatch(const A& a, const B& b, boost::mpl::true_) const
+ {
+ return a == b; // for arithmetic types
+ }
+
+ template <typename A, typename B>
+ bool operator()(const A& a, const B& b) const
+ {
+ return dispatch(a, b,
+ boost::mpl::and_<
+ boost::is_arithmetic<A>,
+ boost::is_arithmetic<B> >());
+ }
+
+ template <typename T>
+ bool operator()(const T& a, const T& b) const
+ {
+ // This code works for lists
+ return a == b;
+ }
+
+ template <typename Base, utree_type::info type_>
+ bool operator()(
+ basic_string<Base, type_> const& a,
+ basic_string<Base, type_> const& b) const
+ {
+ return static_cast<Base const&>(a) == static_cast<Base const&>(b);
+ }
+
+ bool operator()(nil, nil) const
+ {
+ return true;
+ }
+ };
+
+ struct utree_is_less_than
+ {
+ typedef bool result_type;
+
+ template <typename A, typename B>
+ bool dispatch(const A&, const B&, boost::mpl::false_) const
+ {
+ return false; // cannot compare different types by default
+ }
+
+ template <typename A, typename B>
+ bool dispatch(const A& a, const B& b, boost::mpl::true_) const
+ {
+ return a < b; // for arithmetic types
+ }
+
+ template <typename A, typename B>
+ bool operator()(const A& a, const B& b) const
+ {
+ return dispatch(a, b,
+ boost::mpl::and_<
+ boost::is_arithmetic<A>,
+ boost::is_arithmetic<B> >());
+ }
+
+ template <typename T>
+ bool operator()(const T& a, const T& b) const
+ {
+ // This code works for lists
+ return a < b;
+ }
+
+ template <typename Base, utree_type::info type_>
+ bool operator()(
+ basic_string<Base, type_> const& a,
+ basic_string<Base, type_> const& b) const
+ {
+ return static_cast<Base const&>(a) < static_cast<Base const&>(b);
+ }
+
+ bool operator()(nil, nil) const
+ {
+ BOOST_ASSERT(false);
+ return false; // no less than comparison for nil
+ }
+ };
+
+ struct utree_print
+ {
+ typedef void result_type;
+
+ std::ostream& out;
+ utree_print(std::ostream& out) : out(out) {}
+
+ void operator()(scheme::nil) const
+ {
+ out << "nil";
+ }
+
+ template <typename T>
+ void operator()(T val) const
+ {
+ out << val;
+ }
+
+ void operator()(bool b) const
+ {
+ out << (b ? "true" : "false");
+ }
+
+ void operator()(binary_range const& b) const
+ {
+ out << "b";
+ out.width(2);
+ out.fill('0');
+
+ typedef binary_range::const_iterator iterator;
+ for (iterator i = b.begin(); i != b.end(); ++i)
+ out << std::hex << int((unsigned char)*i);
+ out << std::dec;
+ }
+
+ void operator()(utf8_string_range const& str) const
+ {
+ typedef utf8_string_range::const_iterator iterator;
+ iterator i = str.begin();
+ out << '"';
+ for (; i != str.end(); ++i)
+ out << *i;
+ out << '"';
+ }
+
+ void operator()(utf8_symbol_range const& str) const
+ {
+ typedef utf8_symbol_range::const_iterator iterator;
+ iterator i = str.begin();
+ for (; i != str.end(); ++i)
+ out << *i;
+ }
+
+ template <typename Iterator>
+ void operator()(boost::iterator_range<Iterator> const& range) const
+ {
+ typedef typename boost::iterator_range<Iterator>::const_iterator iterator;
+ (*this)('(');
+ for (iterator i = range.begin(); i != range.end(); ++i)
+ {
+ if (i != range.begin())
+ (*this)(' ');
+ scheme::utree::visit(*i, *this);
+ }
+ (*this)(')');
+ }
+ };
+
+ template <typename Base>
+ struct logical_function
+ {
+ typedef utree result_type;
+
+ // binary
+ utree operator()(bool a, bool b) const
+ {
+ return Base::eval(a, b); // for boolean types
+ }
+
+ // binary
+ template <typename A, typename B>
+ utree operator()(A const& a, B const& b) const
+ {
+ // $$$ Throw exception here? $$$
+ return utree(); // cannot apply to non booleans
+ }
+
+ // unary
+ utree operator()(bool a) const
+ {
+ return Base::eval(a); // for boolean types
+ }
+
+ // unary
+ template <typename A>
+ utree operator()(A const& a) const
+ {
+ // $$$ Throw exception here? $$$
+ return utree(); // cannot apply to non booleans
+ }
+ };
+
+ template <typename Base>
+ struct arithmetic_function
+ {
+ typedef utree result_type;
+
+ template <typename A, typename B>
+ utree dispatch(A&, B&, boost::mpl::false_) const
+ {
+ // $$$ Throw exception here? $$$
+ return utree(); // cannot apply to non-arithmetic types
+ }
+
+ template <typename A, typename B>
+ utree dispatch(A& a, B& b, boost::mpl::true_) const
+ {
+ return Base::eval(a, b); // for arithmetic types
+ }
+
+ // binary
+ template <typename A, typename B>
+ utree operator()(A& a, B& b) const
+ {
+ return dispatch(a, b,
+ boost::mpl::and_<
+ boost::is_arithmetic<A>,
+ boost::is_arithmetic<B> >());
+ }
+
+ template <typename A>
+ utree dispatch(A&, boost::mpl::false_) const
+ {
+ // $$$ Throw exception here? $$$
+ return utree(); // cannot apply to non-arithmetic types
+ }
+
+ template <typename A>
+ utree dispatch(A& a, boost::mpl::true_) const
+ {
+ return Base::eval(a); // for arithmetic types
+ }
+
+ // unary
+ template <typename A>
+ utree operator()(A& a) const
+ {
+ return dispatch(a, boost::is_arithmetic<A>());
+ }
+ };
+
+ template <typename Base>
+ struct integral_function
+ {
+ typedef utree result_type;
+
+ template <typename A, typename B>
+ utree dispatch(A&, B&, boost::mpl::false_) const
+ {
+ // $$$ Throw exception here? $$$
+ return utree(); // cannot apply to non-integral types
+ }
+
+ template <typename A, typename B>
+ utree dispatch(A& a, B& b, boost::mpl::true_) const
+ {
+ return Base::eval(a, b); // for integral types
+ }
+
+ // binary
+ template <typename A, typename B>
+ utree operator()(A& a, B& b) const
+ {
+ return dispatch(a, b,
+ boost::mpl::and_<
+ boost::is_integral<A>,
+ boost::is_integral<B> >());
+ }
+
+ template <typename A>
+ utree dispatch(A&, boost::mpl::false_) const
+ {
+ // $$$ Throw exception here? $$$
+ return utree(); // cannot apply to non-integral types
+ }
+
+ template <typename A>
+ utree dispatch(A& a, boost::mpl::true_) const
+ {
+ return Base::eval(a); // for integral types
+ }
+
+ // unary
+ template <typename A>
+ utree operator()(A& a) const
+ {
+ return dispatch(a, boost::is_integral<A>());
+ }
+ };
+
+#define SCHEME_CREATE_FUNCTION(name, expr, base) \
+ struct BOOST_PP_CAT(function_impl_, name) \
+ { \
+ template <typename A, typename B> \
+ static utree eval(A& a, B& b) \
+ { \
+ return utree(expr); \
+ } \
+ template <typename A> \
+ static utree eval(A& a) \
+ { \
+ return utree(expr); \
+ } \
+ }; \
+ base<BOOST_PP_CAT(function_impl_, name)> const \
+ BOOST_PP_CAT(base, BOOST_PP_CAT(_, name)) = {}; \
+ /***/
+
+#define SCHEME_CREATE_ARITHMETIC_FUNCTION(name, expr) \
+ SCHEME_CREATE_FUNCTION(name, expr, arithmetic_function) \
+ /***/
+
+#define SCHEME_CREATE_INTEGRAL_FUNCTION(name, expr) \
+ SCHEME_CREATE_FUNCTION(name, expr, integral_function) \
+ /***/
+
+#define SCHEME_CREATE_LOGICAL_FUNCTION(name, expr) \
+ SCHEME_CREATE_FUNCTION(name, expr, logical_function) \
+ /***/
+
+ inline bool operator==(utree const& a, utree const& b)
+ {
+ return utree::visit(a, b, utree_is_equal());
+ }
+
+ inline bool operator<(utree const& a, utree const& b)
+ {
+ return utree::visit(a, b, utree_is_less_than());
+ }
+
+ inline bool operator!=(utree const& a, utree const& b)
+ {
+ return !(a == b);
+ }
+
+ inline bool operator>(utree const& a, utree const& b)
+ {
+ return b < a;
+ }
+
+ inline bool operator<=(utree const& a, utree const& b)
+ {
+ return !(b < a);
+ }
+
+ inline bool operator>=(utree const& a, utree const& b)
+ {
+ return !(a < b);
+ }
+
+ inline std::ostream& operator<<(std::ostream& out, utree const& x)
+ {
+ utree::visit(x, utree_print(out));
+ return out;
+ }
+
+ SCHEME_CREATE_LOGICAL_FUNCTION(and_, a&&b);
+ SCHEME_CREATE_LOGICAL_FUNCTION(or_, a||b);
+ SCHEME_CREATE_LOGICAL_FUNCTION(not_, !a);
+
+ SCHEME_CREATE_ARITHMETIC_FUNCTION(plus, a+b);
+ SCHEME_CREATE_ARITHMETIC_FUNCTION(minus, a-b);
+ SCHEME_CREATE_ARITHMETIC_FUNCTION(times, a*b);
+ SCHEME_CREATE_ARITHMETIC_FUNCTION(divides, a/b);
+ SCHEME_CREATE_INTEGRAL_FUNCTION(modulus, a%b);
+ SCHEME_CREATE_ARITHMETIC_FUNCTION(negate, -a);
+
+ SCHEME_CREATE_INTEGRAL_FUNCTION(bitand_, a&b);
+ SCHEME_CREATE_INTEGRAL_FUNCTION(bitor_, a|b);
+ SCHEME_CREATE_INTEGRAL_FUNCTION(bitxor_, a^b);
+ SCHEME_CREATE_INTEGRAL_FUNCTION(shift_left, a<<b);
+ SCHEME_CREATE_INTEGRAL_FUNCTION(shift_right, a>>b);
+ SCHEME_CREATE_INTEGRAL_FUNCTION(invert, ~a);
+
+ inline utree operator&&(utree const& a, utree const& b)
+ {
+ return utree::visit(a, b, logical_function_and_);
+ }
+
+ inline utree operator||(utree const& a, utree const& b)
+ {
+ return utree::visit(a, b, logical_function_or_);
+ }
+
+ inline utree operator!(utree const& a)
+ {
+ return utree::visit(a, logical_function_not_);
+ }
+
+ inline utree operator+(utree const& a, utree const& b)
+ {
+ return utree::visit(a, b, arithmetic_function_plus);
+ }
+
+ inline utree operator-(utree const& a, utree const& b)
+ {
+ return utree::visit(a, b, arithmetic_function_minus);
+ }
+
+ inline utree operator*(utree const& a, utree const& b)
+ {
+ return utree::visit(a, b, arithmetic_function_times);
+ }
+
+ inline utree operator/(utree const& a, utree const& b)
+ {
+ return utree::visit(a, b, arithmetic_function_divides);
+ }
+
+ inline utree operator%(utree const& a, utree const& b)
+ {
+ return utree::visit(a, b, integral_function_modulus);
+ }
+
+ inline utree operator-(utree const& a)
+ {
+ return utree::visit(a, arithmetic_function_negate);
+ }
+
+ inline utree operator&(utree const& a, utree const& b)
+ {
+ return utree::visit(a, b, integral_function_bitand_);
+ }
+
+ inline utree operator|(utree const& a, utree const& b)
+ {
+ return utree::visit(a, b, integral_function_bitor_);
+ }
+
+ inline utree operator^(utree const& a, utree const& b)
+ {
+ return utree::visit(a, b, integral_function_bitxor_);
+ }
+
+ inline utree operator<<(utree const& a, utree const& b)
+ {
+ return utree::visit(a, b, integral_function_shift_left);
+ }
+
+ inline utree operator>>(utree const& a, utree const& b)
+ {
+ return utree::visit(a, b, integral_function_shift_right);
+ }
+
+ inline utree operator~(utree const& a)
+ {
+ return utree::visit(a, integral_function_invert);
+ }
+}
+
+#endif

Added: branches/release/libs/spirit/example/scheme/utree_test.cpp
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/scheme/utree_test.cpp 2010-07-04 14:04:41 EDT (Sun, 04 Jul 2010)
@@ -0,0 +1,155 @@
+#include "utree.hpp"
+#include "simple_print.hpp"
+#include <iostream>
+
+int main()
+{
+ using scheme::utree;
+ using scheme::ulist;
+ using ::detail::println;
+
+ {
+ // test the size
+ std::cout << "size of utree is: "
+ << sizeof(scheme::utree) << " bytes" << std::endl;
+ }
+
+ {
+ utree val;
+ println(val);
+ }
+
+ {
+ utree val(true);
+ println(val);
+ }
+
+ {
+ utree val(123);
+ println(val);
+ }
+
+ {
+ utree val(123.456);
+ println(val);
+ }
+
+ {
+ utree val("Hello, World");
+ println(val);
+ utree val2;
+ val2 = val;
+ println(val2);
+ utree val3("Hello, World. Chuckie is back!!!");
+ val = val3;
+ println(val);
+
+ utree val4("Apple");
+ utree val5("Apple");
+ BOOST_ASSERT(val4 == val5);
+
+ utree val6("ApplePie");
+ BOOST_ASSERT(val4 < val6);
+ }
+
+ {
+ utree val;
+ val.push_back(123);
+ val.push_back("Chuckie");
+ utree val2;
+ val2.push_back(123.456);
+ val2.push_back("Mah Doggie");
+ val.push_back(val2);
+ println(val);
+ println(val.front());
+
+ utree val3;
+ val3.swap(val);
+ println(val);
+ val3.swap(val);
+ println(val);
+ val.push_back("Ba Ba Black Sheep");
+ println(val);
+ val.pop_front();
+ println(val);
+ utree::iterator i = val.begin();
+ ++++i;
+ val.insert(i, "Right in the middle");
+ BOOST_ASSERT(val.size() == 4);
+ println(val);
+ val.pop_back();
+ println(val);
+ BOOST_ASSERT(val.size() == 3);
+ val.erase(val.end());
+ println(val);
+ BOOST_ASSERT(val.size() == 2);
+
+ val.insert(val.begin(), val2.begin(), val2.end());
+ println(val);
+ }
+
+ {
+ utree val;
+ val.insert(val.end(), 123);
+ val.insert(val.end(), "Mia");
+ val.insert(val.end(), "Chuckie");
+ val.insert(val.end(), "Poly");
+ val.insert(val.end(), "Mochi");
+ println(val);
+ }
+
+ {
+ utree a, b;
+ BOOST_ASSERT(a == b);
+ a = 123;
+ BOOST_ASSERT(a != b);
+ b = 123;
+ BOOST_ASSERT(a == b);
+ a = 100.00;
+ BOOST_ASSERT(a < b);
+
+ b = a = ulist();
+ BOOST_ASSERT(a == b);
+ a.push_back(1);
+ a.push_back("two");
+ a.push_back(3.0);
+ b.push_back(1);
+ b.push_back("two");
+ b.push_back(3.0);
+ BOOST_ASSERT(a == b);
+ b.push_back(4);
+ BOOST_ASSERT(a != b);
+ BOOST_ASSERT(a < b);
+ }
+
+ {
+ ulist a;
+ a.push_back(1);
+ a.push_back(2);
+ a.push_back(3);
+ a.push_back(4);
+ a.push_back(5);
+ a.push_back(6);
+ a.push_back(7);
+ a.push_back(8);
+ a.push_back(9);
+ a.push_back(10);
+ a.push_back(11);
+ a.push_back(12);
+
+ BOOST_ASSERT(a[0] == utree(1));
+ BOOST_ASSERT(a[1] == utree(2));
+ BOOST_ASSERT(a[2] == utree(3));
+ BOOST_ASSERT(a[3] == utree(4));
+ BOOST_ASSERT(a[4] == utree(5));
+ BOOST_ASSERT(a[5] == utree(6));
+ BOOST_ASSERT(a[6] == utree(7));
+ BOOST_ASSERT(a[7] == utree(8));
+ BOOST_ASSERT(a[8] == utree(9));
+ BOOST_ASSERT(a[9] == utree(10));
+ BOOST_ASSERT(a[10] == utree(11));
+ BOOST_ASSERT(a[11] == utree(12));
+ }
+
+ return 0;
+}


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk