Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r61524 - in trunk/libs/spirit/example/scheme: example/generate_qiexpr example/parse_qiexpr output qi utree utree/detail
From: hartmut.kaiser_at_[hidden]
Date: 2010-04-23 21:28:14


Author: hkaiser
Date: 2010-04-23 21:28:13 EDT (Fri, 23 Apr 2010)
New Revision: 61524
URL: http://svn.boost.org/trac/boost/changeset/61524

Log:
Spirit: updated Qi parser and generator
Text files modified:
   trunk/libs/spirit/example/scheme/example/generate_qiexpr/generate_qi_test.cpp | 3 +
   trunk/libs/spirit/example/scheme/example/generate_qiexpr/generate_qiexpr.cpp | 8 +++++
   trunk/libs/spirit/example/scheme/example/generate_qiexpr/input.txt | 8 ++--
   trunk/libs/spirit/example/scheme/example/parse_qiexpr/generate_sexpr_to_ostream.cpp | 2 -
   trunk/libs/spirit/example/scheme/example/parse_qiexpr/input.txt | 2 +
   trunk/libs/spirit/example/scheme/output/utree_traits.hpp | 11 +++++++
   trunk/libs/spirit/example/scheme/qi/qiexpr_generator.hpp | 62 +++++++++++++++++++++++++++------------
   trunk/libs/spirit/example/scheme/qi/qiexpr_parser.hpp | 42 +++++++++++++-------------
   trunk/libs/spirit/example/scheme/utree/detail/utree_detail2.hpp | 3 -
   trunk/libs/spirit/example/scheme/utree/operators.hpp | 12 +++++++
   10 files changed, 103 insertions(+), 50 deletions(-)

Modified: trunk/libs/spirit/example/scheme/example/generate_qiexpr/generate_qi_test.cpp
==============================================================================
--- trunk/libs/spirit/example/scheme/example/generate_qiexpr/generate_qi_test.cpp (original)
+++ trunk/libs/spirit/example/scheme/example/generate_qiexpr/generate_qi_test.cpp 2010-04-23 21:28:13 EDT (Fri, 23 Apr 2010)
@@ -12,6 +12,7 @@
 #include <iterator>
 
 #include <utree/utree.hpp>
+#include <utree/operators.hpp>
 #include <qi/parse_qiexpr.hpp>
 #include <qi/generate_qiexpr.hpp>
 
@@ -37,7 +38,7 @@
             }
             else
             {
- std::cout << "generate error" << std::endl;
+ std::cout << "generate error: " << result << std::endl;
             }
         }
         else

Modified: trunk/libs/spirit/example/scheme/example/generate_qiexpr/generate_qiexpr.cpp
==============================================================================
--- trunk/libs/spirit/example/scheme/example/generate_qiexpr/generate_qiexpr.cpp (original)
+++ trunk/libs/spirit/example/scheme/example/generate_qiexpr/generate_qiexpr.cpp 2010-04-23 21:28:13 EDT (Fri, 23 Apr 2010)
@@ -3,6 +3,10 @@
 // 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)
 
+// #define BOOST_SPIRIT_KARMA_DEBUG
+
+#include <utree/operators.hpp>
+
 #include <qi/generate_qiexpr.hpp>
 #include <qi/generate_qiexpr_impl.hpp>
 
@@ -12,3 +16,7 @@
     template bool generate_qiexpr(utree& u, std::string& str);
 }}
 
+namespace scheme
+{
+ std::ostream& operator<<(std::ostream& out, nil const& x);
+}

Modified: trunk/libs/spirit/example/scheme/example/generate_qiexpr/input.txt
==============================================================================
--- trunk/libs/spirit/example/scheme/example/generate_qiexpr/input.txt (original)
+++ trunk/libs/spirit/example/scheme/example/generate_qiexpr/input.txt 2010-04-23 21:28:13 EDT (Fri, 23 Apr 2010)
@@ -31,12 +31,12 @@
 +*double_
 +long_
 !+long_
-// &int_ >> double_
-// !int_ >> *double_
-// char_ >> *(',' >> char_)
+&int_ >> double_
+!int_ >> *double_
+char_ >> *(',' >> char_)
  
 // directives
-// lexeme[*double_]
+lexeme[*double_]
 
 
 

Modified: trunk/libs/spirit/example/scheme/example/parse_qiexpr/generate_sexpr_to_ostream.cpp
==============================================================================
--- trunk/libs/spirit/example/scheme/example/parse_qiexpr/generate_sexpr_to_ostream.cpp (original)
+++ trunk/libs/spirit/example/scheme/example/parse_qiexpr/generate_sexpr_to_ostream.cpp 2010-04-23 21:28:13 EDT (Fri, 23 Apr 2010)
@@ -17,7 +17,6 @@
 
 ///////////////////////////////////////////////////////////////////////////////
 // this is needed if grammar debugging is on
-#if defined(BOOST_SPIRIT_DEBUG)
 namespace boost { namespace spirit { namespace traits
 {
     void print_attribute(std::ostream& out, scheme::utree const& val)
@@ -25,4 +24,3 @@
         scheme::output::generate_sexpr(out, val);
     }
 }}}
-#endif

Modified: trunk/libs/spirit/example/scheme/example/parse_qiexpr/input.txt
==============================================================================
--- trunk/libs/spirit/example/scheme/example/parse_qiexpr/input.txt (original)
+++ trunk/libs/spirit/example/scheme/example/parse_qiexpr/input.txt 2010-04-23 21:28:13 EDT (Fri, 23 Apr 2010)
@@ -28,7 +28,9 @@
  
 // unary operators
 *double_
++*double_
 +long_
+!+long_
 &int_ >> double_
 !int_ >> *double_
 char_ >> *(',' >> char_)

Modified: trunk/libs/spirit/example/scheme/output/utree_traits.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/output/utree_traits.hpp (original)
+++ trunk/libs/spirit/example/scheme/output/utree_traits.hpp 2010-04-23 21:28:13 EDT (Fri, 23 Apr 2010)
@@ -203,6 +203,17 @@
         }
     };
 
+ template <>
+ struct symbols_lookup<scheme::utf8_symbol, scheme::utf8_symbol>
+ {
+ typedef std::string type;
+
+ static type call(scheme::utf8_symbol const& t)
+ {
+ return t;
+ }
+ };
+
     ///////////////////////////////////////////////////////////////////////////
     template <>
     struct extract_from_attribute<scheme::utree, scheme::utf8_symbol>

Modified: trunk/libs/spirit/example/scheme/qi/qiexpr_generator.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/qi/qiexpr_generator.hpp (original)
+++ trunk/libs/spirit/example/scheme/qi/qiexpr_generator.hpp 2010-04-23 21:28:13 EDT (Fri, 23 Apr 2010)
@@ -10,6 +10,7 @@
 
 #include <boost/cstdint.hpp>
 #include <boost/spirit/include/karma.hpp>
+#include <boost/spirit/include/phoenix.hpp>
 
 #include <utree/utree.hpp>
 #include <utree/operators.hpp>
@@ -17,6 +18,13 @@
 #include <qi/component_names.hpp>
 
 ///////////////////////////////////////////////////////////////////////////////
+namespace boost { namespace spirit { namespace traits
+{
+ template <typename Out>
+ void print_attribute(Out& out, scheme::utree const& val);
+}}}
+
+///////////////////////////////////////////////////////////////////////////////
 namespace scheme { namespace qi
 {
     using boost::spirit::karma::grammar;
@@ -30,11 +38,14 @@
     {
         qiexpr_generator() : qiexpr_generator::base_type(start)
         {
+ namespace phoenix = boost::phoenix;
+
             using boost::spirit::karma::eps;
             using boost::spirit::karma::string;
             using boost::spirit::karma::omit;
             using boost::spirit::karma::repeat;
             using boost::spirit::karma::_r1;
+ using boost::spirit::karma::strict;
 
             start =
                     nil
@@ -42,31 +53,38 @@
                 ;
 
             alternative =
- &symbol(std::string("|")) << '(' << permutation % '|' << ')'
+ &symbol(std::string("|"))
+ << '(' << strict[permutation % '|'] << ')'
                 | permutation
                 ;
 
             permutation =
- &symbol(std::string("^")) << '(' << sequence % '^' << ')'
+ &symbol(std::string("^"))
+ << '(' << strict[sequence % '^'] << ')'
                 | sequence
                 ;
 
             sequence =
- &symbol(std::string(">>")) << '(' << term % ">>" << ')'
+ &symbol(std::string(">>"))
+ << '(' << strict[term % ">>"] << ')'
                 | term
                 ;
 
- term =
- unary << repeat(1)[alternative]
+ term %=
+ strict[
+ unary << '(' << repeat(1)[alternative] << ')'
                 | primitive2 << '(' << literal << ',' << literal << ')'
                 | primitive1 << '(' << literal << ')'
- | primitive0 << -omit[node]
- | repeat(1)[alternative]
- ;
+ | primitive0_rule
+ | directive0 << '[' << repeat(1)[alternative] << ']'
+ | alternative_rule
+ ];
+
+ primitive0_rule = strict[repeat(1)[primitive0]];
+ alternative_rule = alternative;
 
             symbol = string(_r1);
             literal = '"' << string << '"';
- node = eps;
             nil = eps;
 
             // fill the symbol tables with all known primitive parser names
@@ -82,27 +100,31 @@
             for (char const* const* p = unary_names; *p; ++p)
                 unary.add(utf8_symbol(*p));
 
- start.name("start");
- alternative.name("alternative");
- permutation.name("permutation");
- sequence.name("sequence");
- term.name("term");
- node.name("node");
- literal.name("string");
- nil.name("nil");
+ for (char const* const* p = directives0; *p; ++p)
+ directive0.add(utf8_symbol(*p));
+
+ BOOST_SPIRIT_DEBUG_NODE(start);
+ BOOST_SPIRIT_DEBUG_NODE(alternative);
+ BOOST_SPIRIT_DEBUG_NODE(permutation);
+ BOOST_SPIRIT_DEBUG_NODE(sequence);
+ BOOST_SPIRIT_DEBUG_NODE(term);
+ BOOST_SPIRIT_DEBUG_NODE(nil);
+ BOOST_SPIRIT_DEBUG_NODE(literal);
+ BOOST_SPIRIT_DEBUG_NODE(symbol);
+ BOOST_SPIRIT_DEBUG_NODE(primitive0_rule);
+ BOOST_SPIRIT_DEBUG_NODE(alternative_rule);
         }
 
         typedef rule<OutputIterator, space_type, utree()> delimiting_rule_type;
- typedef rule<OutputIterator, utree()> rule_type;
 
         delimiting_rule_type start, alternative, permutation, sequence, term;
- rule_type node;
+ delimiting_rule_type primitive0_rule, alternative_rule;
         rule<OutputIterator, nil()> nil;
         rule<OutputIterator, scheme::utf8_string()> literal;
         rule<OutputIterator, scheme::utf8_symbol(std::string)> symbol;
 
+ symbols<scheme::utf8_symbol> unary, directive0;
         symbols<scheme::utf8_symbol> primitive0, primitive1, primitive2;
- symbols<scheme::utf8_symbol> unary;
     };
 }}
 

Modified: trunk/libs/spirit/example/scheme/qi/qiexpr_parser.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/qi/qiexpr_parser.hpp (original)
+++ trunk/libs/spirit/example/scheme/qi/qiexpr_parser.hpp 2010-04-23 21:28:13 EDT (Fri, 23 Apr 2010)
@@ -21,9 +21,11 @@
 #include <input/string.hpp>
 #include <qi/component_names.hpp>
 
+///////////////////////////////////////////////////////////////////////////////
 namespace boost { namespace spirit { namespace traits
 {
- void print_attribute(std::ostream& out, scheme::utree const& val);
+ template <typename Out>
+ void print_attribute(Out& out, scheme::utree const& val);
 }}}
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -39,7 +41,6 @@
     using boost::spirit::qi::_1;
     using boost::spirit::qi::_2;
     using boost::phoenix::push_back;
- using boost::phoenix::function;
 
     ///////////////////////////////////////////////////////////////////////////
     template <typename Iterator>
@@ -145,7 +146,8 @@
 
         qiexpr_parser() : qiexpr_parser::base_type(start)
         {
- typedef function<detail::make_list_node> make_list_type;
+ namespace phoenix = boost::phoenix;
+ typedef phoenix::function<detail::make_list_node> make_list_type;
 
             make_list_type make_directive = detail::make_list_node("");
 
@@ -183,11 +185,11 @@
 
             // unary operators
             unary_term =
- '*' >> term [ make_kleene(_val, _1) ]
- | '+' >> term [ make_plus(_val, _1) ]
- | '-' >> term [ make_optional(_val, _1) ]
- | '&' >> term [ make_and_pred(_val, _1) ]
- | '!' >> term [ make_not_pred(_val, _1) ]
+ '*' >> unary_term [ make_kleene(_val, _1) ]
+ | '+' >> unary_term [ make_plus(_val, _1) ]
+ | '-' >> unary_term [ make_optional(_val, _1) ]
+ | '&' >> unary_term [ make_and_pred(_val, _1) ]
+ | '!' >> unary_term [ make_not_pred(_val, _1) ]
                 | term [ _val = _1 ]
                 ;
 
@@ -216,8 +218,8 @@
 
             // a literal (either 'x' or "abc")
             literal =
- string_lit [ push_back(_val, _1) ]
- | string_lit.char_lit [ push_back(_val, _1) ]
+ string_lit [ phoenix::push_back(_val, _1) ]
+ | string_lit.char_lit [ phoenix::push_back(_val, _1) ]
                 ;
 
             // fill the symbol tables with all known primitive parser names
@@ -248,17 +250,15 @@
                 directive0.add(*p, u);
             }
 
-#if defined(BOOST_SPIRIT_DEBUG)
- start.name("name"); debug(start);
- directive.name("directive"); debug(directive);
- primitive.name("primitive"); debug(primitive);
- unary_term.name("unary_term"); debug(unary_term);
- term.name("term"); debug(term);
- literal.name("literal"); debug(literal);
- alternative.name("alternative"); debug(alternative);
- permutation.name("permutation"); debug(permutation);
- sequence.name("sequence"); debug(sequence);
-#endif
+ BOOST_SPIRIT_DEBUG_NODE(start);
+ BOOST_SPIRIT_DEBUG_NODE(directive);
+ BOOST_SPIRIT_DEBUG_NODE(primitive);
+ BOOST_SPIRIT_DEBUG_NODE(unary_term);
+ BOOST_SPIRIT_DEBUG_NODE(term);
+ BOOST_SPIRIT_DEBUG_NODE(literal);
+ BOOST_SPIRIT_DEBUG_NODE(alternative);
+ BOOST_SPIRIT_DEBUG_NODE(permutation);
+ BOOST_SPIRIT_DEBUG_NODE(sequence);
         }
 
         typedef rule<Iterator, qiexpr_white_space<Iterator>, utree()> rule_type;

Modified: trunk/libs/spirit/example/scheme/utree/detail/utree_detail2.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/utree/detail/utree_detail2.hpp (original)
+++ trunk/libs/spirit/example/scheme/utree/detail/utree_detail2.hpp 2010-04-23 21:28:13 EDT (Fri, 23 Apr 2010)
@@ -1044,8 +1044,7 @@
             return r.first == 0;
         else if (get_type() == type::list_type)
             return l.size == 0;
- BOOST_ASSERT(get_type() == type::nil_type);
- return true;
+ return get_type() == type::nil_type;
     }
 
     inline std::size_t utree::size() const

Modified: trunk/libs/spirit/example/scheme/utree/operators.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/utree/operators.hpp (original)
+++ trunk/libs/spirit/example/scheme/utree/operators.hpp 2010-04-23 21:28:13 EDT (Fri, 23 Apr 2010)
@@ -7,6 +7,11 @@
 #if !defined(BOOST_SPIRIT_UTREE_OPERATORS)
 #define BOOST_SPIRIT_UTREE_OPERATORS
 
+#include <utree/utree.hpp>
+#include <boost/preprocessor/cat.hpp>
+#include <boost/type_traits/is_arithmetic.hpp>
+#include <boost/type_traits/is_integral.hpp>
+
 #if defined(BOOST_MSVC)
 # pragma warning(push)
 # pragma warning(disable: 4804)
@@ -51,6 +56,8 @@
     std::ostream& operator<<(std::ostream& out, utree const& x);
     std::istream& operator>>(std::istream& in, utree& x);
 
+ std::ostream& operator<<(std::ostream& out, nil const& x);
+
     // Logical operators
     utree operator&&(utree const& a, utree const& b);
     utree operator||(utree const& a, utree const& b);
@@ -467,6 +474,11 @@
     }
 #endif
 
+ inline std::ostream& operator<<(std::ostream& out, nil const& x)
+ {
+ return out;
+ }
+
     SCHEME_CREATE_LOGICAL_FUNCTION(and_, a&&b);
     SCHEME_CREATE_LOGICAL_FUNCTION(or_, a||b);
     SCHEME_CREATE_LOGICAL_FUNCTION(not_, !a);


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