Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r61217 - in trunk/libs/spirit/example/scheme: . detail input output test/parse_qiexpr
From: hartmut.kaiser_at_[hidden]
Date: 2010-04-11 21:36:01


Author: hkaiser
Date: 2010-04-11 21:35:59 EDT (Sun, 11 Apr 2010)
New Revision: 61217
URL: http://svn.boost.org/trac/boost/changeset/61217

Log:
Spirit: updates to scheme example
Text files modified:
   trunk/libs/spirit/example/scheme/detail/utree_detail3.hpp | 19 ++++++++++++---
   trunk/libs/spirit/example/scheme/input/parse_sexpr.hpp | 18 +++++++++++---
   trunk/libs/spirit/example/scheme/input/parse_sexpr_impl.hpp | 40 +++++++++++++++++++++++++++----
   trunk/libs/spirit/example/scheme/input/qiexpr.hpp | 8 ++++-
   trunk/libs/spirit/example/scheme/output/generate_sexpr.hpp | 16 +++++++++---
   trunk/libs/spirit/example/scheme/output/generate_sexpr_impl.hpp | 50 ++++++++++++++++++++++++++++++++-------
   trunk/libs/spirit/example/scheme/output/sexpr.hpp | 35 ++++++++++++++++++++--------
   trunk/libs/spirit/example/scheme/scheme_compiler.hpp | 4 +-
   trunk/libs/spirit/example/scheme/scheme_interpreter.hpp | 2
   trunk/libs/spirit/example/scheme/test/parse_qiexpr/generate_sexpr_to_ostream.cpp | 4 +-
   10 files changed, 152 insertions(+), 44 deletions(-)

Modified: trunk/libs/spirit/example/scheme/detail/utree_detail3.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/detail/utree_detail3.hpp (original)
+++ trunk/libs/spirit/example/scheme/detail/utree_detail3.hpp 2010-04-11 21:35:59 EDT (Sun, 11 Apr 2010)
@@ -32,6 +32,7 @@
             utree_type::list_type);
         SCHEME_GET_UTREE_TYPE(boost::iterator_range<utree::const_iterator>,
             utree_type::list_type);
+ SCHEME_GET_UTREE_TYPE(function_ptr, utree_type::function_type);
 
 #undef SCHEME_GET_UTREE_TYPE
 
@@ -118,16 +119,26 @@
                 return type(x.s.str(), x.s.size());
             }
         };
+
+ template <>
+ struct get_impl<function_ptr>
+ {
+ typedef function_ptr type;
+ static type call(utree const& x)
+ {
+ return type(x.f);
+ }
+ };
     }
 
     template <typename T>
- typename detail::get_impl<T>::type
- get(utree const& x)
+ typename scheme::detail::get_impl<T>::type
+ get(scheme::utree const& x)
     {
- if (x.which() != detail::get_utree_type<T>::value)
+ if (x.which() != scheme::detail::get_utree_type<T>::value)
             throw boost::bad_get();
 
- return detail::get_impl<T>::call(x);
+ return scheme::detail::get_impl<T>::call(x);
     }
 }
 

Modified: trunk/libs/spirit/example/scheme/input/parse_sexpr.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/input/parse_sexpr.hpp (original)
+++ trunk/libs/spirit/example/scheme/input/parse_sexpr.hpp 2010-04-11 21:35:59 EDT (Sun, 11 Apr 2010)
@@ -8,14 +8,24 @@
 
 #include "../utree.hpp"
 #include "../input/sexpr.hpp"
+#include <iosfwd>
+#include <string>
 
 namespace scheme { namespace input
 {
- template <typename InputStream>
- bool parse_sexpr(InputStream& is, utree& result);
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename Char>
+ bool parse_sexpr(std::basic_istream<Char>& is, utree& result);
+
+ template <typename Char>
+ bool parse_sexpr_list(std::basic_istream<Char>& is, utree& result);
+
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename Char>
+ bool parse_sexpr(std::basic_string<Char>& str, utree& result);
 
- template <typename InputStream>
- bool parse_sexpr_list(InputStream& is, utree& result);
+ template <typename Char>
+ bool parse_sexpr_list(std::basic_string<Char>& str, utree& result);
 }}
 
 #endif

Modified: trunk/libs/spirit/example/scheme/input/parse_sexpr_impl.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/input/parse_sexpr_impl.hpp (original)
+++ trunk/libs/spirit/example/scheme/input/parse_sexpr_impl.hpp 2010-04-11 21:35:59 EDT (Sun, 11 Apr 2010)
@@ -7,6 +7,7 @@
 #define BOOST_SPIRIT_PARSE_SEXPR_IMPL
 
 #include <iostream>
+#include <string>
 #include <boost/spirit/include/support_istream_iterator.hpp>
 #include <boost/spirit/include/qi_parse.hpp>
 
@@ -16,38 +17,65 @@
 namespace scheme { namespace input
 {
     ///////////////////////////////////////////////////////////////////////////
- template <typename InputStream>
- bool parse_sexpr(InputStream& is, utree& result)
+ template <typename Char>
+ bool parse_sexpr(std::basic_istream<Char>& is, utree& result)
     {
         // no white space skipping in the stream!
         is.unsetf(std::ios::skipws);
 
- typedef boost::spirit::basic_istream_iterator<char> iterator_type;
+ typedef boost::spirit::basic_istream_iterator<Char> iterator_type;
         iterator_type first(is);
         iterator_type last;
 
         scheme::input::sexpr<iterator_type> p;
         scheme::input::sexpr_white_space<iterator_type> ws;
 
+ using boost::spirit::qi::phrase_parse;
         return phrase_parse(first, last, p, ws, result);
     }
 
     ///////////////////////////////////////////////////////////////////////////
- template <typename InputStream>
- bool parse_sexpr_list(InputStream& is, utree& result)
+ template <typename Char>
+ bool parse_sexpr_list(std::basic_istream<Char>& is, utree& result)
     {
         // no white space skipping in the stream!
         is.unsetf(std::ios::skipws);
 
- typedef boost::spirit::basic_istream_iterator<char> iterator_type;
+ typedef boost::spirit::basic_istream_iterator<Char> iterator_type;
         iterator_type first(is);
         iterator_type last;
 
         scheme::input::sexpr<iterator_type> p;
         scheme::input::sexpr_white_space<iterator_type> ws;
 
+ using boost::spirit::qi::phrase_parse;
         return phrase_parse(first, last, +p, ws, result);
     }
+
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename Char>
+ bool parse_sexpr(std::basic_string<Char>& str, utree& result)
+ {
+ typedef typename std::basic_string<Char>::const_iterator iterator_type;
+
+ scheme::input::sexpr<iterator_type> p;
+ scheme::input::sexpr_white_space<iterator_type> ws;
+
+ using boost::spirit::qi::phrase_parse;
+ return phrase_parse(str.begin(), str.end(), p, ws, result);
+ }
+
+ template <typename Char>
+ bool parse_sexpr_list(std::basic_string<Char>& str, utree& result)
+ {
+ typedef typename std::basic_string<Char>::const_iterator iterator_type;
+
+ scheme::input::sexpr<iterator_type> p;
+ scheme::input::sexpr_white_space<iterator_type> ws;
+
+ using boost::spirit::qi::phrase_parse;
+ return phrase_parse(str.begin(), str.end(), +p, ws, result);
+ }
 }}
 
 #endif

Modified: trunk/libs/spirit/example/scheme/input/qiexpr.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/input/qiexpr.hpp (original)
+++ trunk/libs/spirit/example/scheme/input/qiexpr.hpp 2010-04-11 21:35:59 EDT (Sun, 11 Apr 2010)
@@ -40,8 +40,6 @@
                 | "//" >> *(char_ - eol) >> eol // comments
                 | "/*" >> *(char_ - "*/") >> "*/"
                 ;
-
-// start.name("ws"); debug(start);
         }
 
         rule<Iterator> start;
@@ -51,9 +49,15 @@
     // a list of names for all supported parser primitives taking no parameters
     static char const* const primitives0[] =
     {
+ // character parsers
         "char_"
+ , "alnum", "alpha", "blank", "cntrl", "digit", "graph", "print", "punct"
+ , "space", "xdigit"
+ , "lower", "upper"
+ // numerics
       , "long_long", "long_", "int_", "short_"
       , "ulong_long", "ulong_", "uint_", "ushort_"
+ , "bin", "oct", "hex"
       , "bool_", "true_", "false_"
       , "long_double", "double_", "float_"
       , 0

Modified: trunk/libs/spirit/example/scheme/output/generate_sexpr.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/output/generate_sexpr.hpp (original)
+++ trunk/libs/spirit/example/scheme/output/generate_sexpr.hpp 2010-04-11 21:35:59 EDT (Sun, 11 Apr 2010)
@@ -12,12 +12,20 @@
 namespace scheme { namespace output
 {
     ///////////////////////////////////////////////////////////////////////////
- template <typename OutputStream>
- bool generate_sexpr(OutputStream& os, utree const& tree);
+ template <typename Char>
+ bool generate_sexpr(std::basic_ostream<Char>& os, utree const& tree);
 
     ///////////////////////////////////////////////////////////////////////////
- template <typename OutputStream>
- bool generate_sexpr_list(OutputStream& os, utree const& tree);
+ template <typename Char>
+ bool generate_sexpr_list(std::basic_ostream<Char>& os, utree const& tree);
+
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename Char>
+ bool generate_sexpr(std::basic_string<Char>& os, utree const& tree);
+
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename Char>
+ bool generate_sexpr_list(std::basic_string<Char>& os, utree const& tree);
 }}
 
 #endif

Modified: trunk/libs/spirit/example/scheme/output/generate_sexpr_impl.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/output/generate_sexpr_impl.hpp (original)
+++ trunk/libs/spirit/example/scheme/output/generate_sexpr_impl.hpp 2010-04-11 21:35:59 EDT (Sun, 11 Apr 2010)
@@ -17,30 +17,62 @@
 namespace scheme { namespace output
 {
     ///////////////////////////////////////////////////////////////////////////
- template <typename OutputStream>
- bool generate_sexpr(OutputStream& os, utree const& tree)
+ template <typename Char>
+ bool generate_sexpr(std::basic_ostream<Char>& os, utree const& tree)
     {
         typedef boost::spirit::ostream_iterator output_iterator_type;
- using boost::spirit::karma::space;
 
- output_iterator_type sink(os);
+ using boost::spirit::karma::space;
+ using boost::spirit::karma::generate_delimited;
 
         scheme::output::sexpr<output_iterator_type> g;
- return generate_delimited(sink, g, space, tree);
+
+ return generate_delimited(output_iterator_type(os), g, space, tree);
     }
 
     ///////////////////////////////////////////////////////////////////////////
- template <typename OutputStream>
- bool generate_sexpr_list(OutputStream& os, utree const& tree)
+ template <typename Char>
+ bool generate_sexpr_list(std::basic_ostream<Char>& os, utree const& tree)
     {
         typedef boost::spirit::ostream_iterator output_iterator_type;
+
         using boost::spirit::karma::space;
         using boost::spirit::karma::eol;
+ using boost::spirit::karma::generate_delimited;
+
+ scheme::output::sexpr<output_iterator_type> g;
+
+ return generate_delimited(output_iterator_type(os), g % eol, space, tree);
+ }
+
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename Char>
+ bool generate_sexpr(std::basic_string<Char>& os, utree const& tree)
+ {
+ typedef std::basic_string<Char> string_type;
+ typedef std::back_insert_iterator<string_type> output_iterator_type;
 
- output_iterator_type sink(os);
+ using boost::spirit::karma::space;
+ using boost::spirit::karma::generate_delimited;
 
         scheme::output::sexpr<output_iterator_type> g;
- return generate_delimited(sink, g % eol, space, tree);
+ return generate_delimited(output_iterator_type(os), g, space, tree);
+ }
+
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename Char>
+ bool generate_sexpr_list(std::basic_string<Char>& os, utree const& tree)
+ {
+ typedef std::basic_string<Char> string_type;
+ typedef std::back_insert_iterator<string_type> output_iterator_type;
+
+ using boost::spirit::karma::space;
+ using boost::spirit::karma::eol;
+ using boost::spirit::karma::generate_delimited;
+
+ scheme::output::sexpr<output_iterator_type> g;
+
+ return generate_delimited(output_iterator_type(os), g % eol, space, tree);
     }
 }}
 

Modified: trunk/libs/spirit/example/scheme/output/sexpr.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/output/sexpr.hpp (original)
+++ trunk/libs/spirit/example/scheme/output/sexpr.hpp 2010-04-11 21:35:59 EDT (Sun, 11 Apr 2010)
@@ -6,15 +6,15 @@
 #if !defined(SCHEME_OUTPUT_SEXPR_MAR_8_2010_829AM)
 #define SCHEME_OUTPUT_SEXPR_MAR_8_2010_829AM
 
+#include "../utree.hpp"
+#include "../detail/utree_detail3.hpp"
+
 #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
 {
@@ -132,6 +132,15 @@
         typedef scheme::binary_string compatible_type;
         typedef mpl::int_<scheme::utree_type::binary_type> distance;
     };
+
+ template <>
+ struct compute_compatible_component_variant<
+ scheme::function_ptr, scheme::utree>
+ : mpl::true_
+ {
+ typedef scheme::function_ptr compatible_type;
+ typedef mpl::int_<scheme::utree_type::function_type> distance;
+ };
 }}}
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -143,12 +152,15 @@
     using boost::spirit::karma::double_;
     using boost::spirit::karma::int_;
     using boost::spirit::karma::char_;
+ using boost::spirit::karma::string;
     using boost::spirit::karma::bool_;
     using boost::spirit::karma::eps;
     using boost::spirit::karma::space_type;
     using boost::spirit::karma::uint_generator;
-
- typedef boost::uint32_t uchar; // a unicode code point
+ using boost::spirit::karma::verbatim;
+ using boost::spirit::karma::delimit;
+ using boost::spirit::karma::hex;
+ using boost::spirit::karma::right_align;
 
     template <typename OutputIterator>
     struct sexpr : grammar<OutputIterator, space_type, utree()>
@@ -160,18 +172,20 @@
             start = double_
                       | int_
                       | bool_
- | string
+ | string_
                       | symbol
                       | byte_str
                       | list
+ | function_
                       | nil_
                       ;
 
             list = '(' << *start << ')';
 
- string = '"' << *char_ << '"';
- symbol = *char_;
- byte_str = 'b' << *hex2;
+ string_ = '"' << string << '"';
+ symbol = string;
+ byte_str = 'b' << *right_align(2, '0')[hex2];
+ function_ = verbatim["function_ptr(" << hex << ")"];
             nil_ = eps;
         }
 
@@ -180,8 +194,9 @@
         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, utf8_string_range()> string_;
         rule<OutputIterator, binary_range()> byte_str;
+ rule<OutputIterator, function_ptr()> function_;
         rule<OutputIterator, nil()> nil_;
     };
 }}

Modified: trunk/libs/spirit/example/scheme/scheme_compiler.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/scheme_compiler.hpp (original)
+++ trunk/libs/spirit/example/scheme/scheme_compiler.hpp 2010-04-11 21:35:59 EDT (Sun, 11 Apr 2010)
@@ -4,8 +4,8 @@
     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_SCHEME_INTERPRETER)
-#define BOOST_SPIRIT_SCHEME_INTERPRETERnamespace scheme
+#if !defined(BOOST_SPIRIT_SCHEME_COMPILER)
+#define BOOST_SPIRIT_SCHEME_COMPILER
 
 #include <vector>
 #include <map>

Modified: trunk/libs/spirit/example/scheme/scheme_interpreter.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/scheme_interpreter.hpp (original)
+++ trunk/libs/spirit/example/scheme/scheme_interpreter.hpp 2010-04-11 21:35:59 EDT (Sun, 11 Apr 2010)
@@ -5,7 +5,7 @@
     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 =============================================================================*/
 #if !defined(BOOST_SPIRIT_SCHEME_INTERPRETER)
-#define BOOST_SPIRIT_SCHEME_INTERPRETERnamespace scheme
+#define BOOST_SPIRIT_SCHEME_INTERPRETER
 
 #include <list>
 #include <boost/function.hpp>

Modified: trunk/libs/spirit/example/scheme/test/parse_qiexpr/generate_sexpr_to_ostream.cpp
==============================================================================
--- trunk/libs/spirit/example/scheme/test/parse_qiexpr/generate_sexpr_to_ostream.cpp (original)
+++ trunk/libs/spirit/example/scheme/test/parse_qiexpr/generate_sexpr_to_ostream.cpp 2010-04-11 21:35:59 EDT (Sun, 11 Apr 2010)
@@ -3,11 +3,11 @@
 // 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 <fstream>
-
 #include "../../output/generate_sexpr.hpp"
 #include "../../output/generate_sexpr_impl.hpp"
 
+#include <fstream>
+
 // explicit template instantiation for the function parse_sexpr
 namespace scheme { namespace output
 {


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