Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r61668 - in trunk/libs/spirit/example/scheme: example/generate_qiexpr example/parse_qiexpr output qi utree/detail
From: hartmut.kaiser_at_[hidden]
Date: 2010-04-28 20:55:58


Author: hkaiser
Date: 2010-04-28 20:55:57 EDT (Wed, 28 Apr 2010)
New Revision: 61668
URL: http://svn.boost.org/trac/boost/changeset/61668

Log:
Spirit: one minor thing left in the Qi generator
Text files modified:
   trunk/libs/spirit/example/scheme/example/generate_qiexpr/generate_qi_test.cpp | 97 ++++++++++++++++++++++++++++++++++-----
   trunk/libs/spirit/example/scheme/example/generate_qiexpr/generate_qiexpr.cpp | 2
   trunk/libs/spirit/example/scheme/example/generate_qiexpr/input.txt | 5 +
   trunk/libs/spirit/example/scheme/example/parse_qiexpr/parse_qi_test.cpp | 5 -
   trunk/libs/spirit/example/scheme/output/generate_sexpr.hpp | 4
   trunk/libs/spirit/example/scheme/qi/generate_qiexpr.hpp | 2
   trunk/libs/spirit/example/scheme/qi/generate_qiexpr_impl.hpp | 2
   trunk/libs/spirit/example/scheme/qi/qiexpr_generator.hpp | 34 +++++++++----
   trunk/libs/spirit/example/scheme/utree/detail/utree_detail2.hpp | 5 +
   9 files changed, 122 insertions(+), 34 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-28 20:55:57 EDT (Wed, 28 Apr 2010)
@@ -4,6 +4,8 @@
 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 
 #include <boost/config/warning_disable.hpp>
+#include <boost/detail/lightweight_test.hpp>
+#include <boost/lexical_cast.hpp>
 
 #define BOOST_SPIRIT_UNICODE
 
@@ -13,14 +15,83 @@
 
 #include <utree/utree.hpp>
 #include <utree/operators.hpp>
+#include <output/generate_sexpr.hpp>
 #include <qi/parse_qiexpr.hpp>
 #include <qi/generate_qiexpr.hpp>
 
 ///////////////////////////////////////////////////////////////////////////////
+bool test_rhs(std::string const& str, scheme::utree& result)
+{
+ if (scheme::input::parse_qi_expr(str, result))
+ {
+// std::string scheme_str;
+// scheme::output::generate_sexpr(scheme_str, result);
+
+ std::string strout;
+ if (scheme::output::generate_qi_expr(result, strout))
+ {
+ std::cout << strout << std::endl;
+ return true;
+ }
+ else
+ {
+ std::cout << "generate error: " << result << std::endl;
+ }
+ }
+ else
+ {
+ std::cout << "parse error" << std::endl;
+ }
+ return false;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+bool test_rule(std::string str)
+{
+ // construct a rule
+ str = "name = " + str;
+
+ // parse it
+ scheme::utree result;
+ BOOST_TEST(scheme::input::parse_qi_rule(str, result));
+
+ std::string strout;
+ if (scheme::output::generate_qi_expr(result, strout))
+ {
+ std::cout << strout << std::endl;
+ return true;
+ }
+ return false;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+bool test_grammar(std::string str)
+{
+ // parse it
+ scheme::utree result;
+ if (scheme::input::parse_qi_grammar(str, result))
+ {
+ std::string scheme_str;
+ scheme::output::generate_sexpr(scheme_str, result);
+
+ std::string strout;
+ if (scheme::output::generate_qi_expr(result, strout))
+ {
+ std::cout << strout << std::endl;
+ return true;
+ }
+ }
+ return false;
+}
+
+///////////////////////////////////////////////////////////////////////////////
 // Main program
 ///////////////////////////////////////////////////////////////////////////////
 int main(int argc, char **argv)
 {
+ std::string rules;
+ int i = 0;
+
     std::string str;
     while (std::getline(std::cin, str))
     {
@@ -28,23 +99,23 @@
             break;
         str += '\n';
 
+ bool r = false;
         scheme::utree result;
- if (scheme::input::parse_qi_expr(str, result))
+ BOOST_TEST(r = test_rhs(str, result));
+
+ if (r && result.which() != scheme::utree_type::nil_type)
         {
- std::string str;
- if (scheme::output::generate_qiexpr(result, str))
- {
- std::cout << str << std::endl;
- }
- else
+ BOOST_TEST(r = test_rule(str));
+ if (r)
             {
- std::cout << "generate error: " << result << std::endl;
+ rules += "rule" + boost::lexical_cast<std::string>(++i)
+ + " = " + str + "\n";
             }
         }
- else
- {
- std::cout << "parse error" << std::endl;
- }
     }
- return 0;
+
+ // now test grammar rule
+// BOOST_TEST(test_grammar(rules));
+
+ return boost::report_errors();
 }

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-28 20:55:57 EDT (Wed, 28 Apr 2010)
@@ -13,7 +13,7 @@
 // explicit template instantiation for the function generate_qiexpr
 namespace scheme { namespace output
 {
- template bool generate_qiexpr(utree& u, std::string& str);
+ template bool generate_qi_expr(utree& u, std::string& str);
 }}
 
 namespace scheme

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-28 20:55:57 EDT (Wed, 28 Apr 2010)
@@ -1,5 +1,3 @@
-int_ >> *(('+' >> int_) | ('-' >> int_))
-
 // Copyright (c) 2001-2010 Hartmut Kaiser
 //
 // Distributed under the Boost Software License, Version 1.0. (See accompanying
@@ -39,6 +37,9 @@
  
 // directives
 lexeme[*double_]
+
+// calculator :-P
+int_ >> *(('+' >> int_) | ('-' >> int_))
 
 
 

Modified: trunk/libs/spirit/example/scheme/example/parse_qiexpr/parse_qi_test.cpp
==============================================================================
--- trunk/libs/spirit/example/scheme/example/parse_qiexpr/parse_qi_test.cpp (original)
+++ trunk/libs/spirit/example/scheme/example/parse_qiexpr/parse_qi_test.cpp 2010-04-28 20:55:57 EDT (Wed, 28 Apr 2010)
@@ -4,6 +4,8 @@
 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 
 #include <boost/config/warning_disable.hpp>
+#include <boost/detail/lightweight_test.hpp>
+#include <boost/lexical_cast.hpp>
 
 #define BOOST_SPIRIT_UNICODE
 
@@ -11,9 +13,6 @@
 #include <fstream>
 #include <iterator>
 
-#include <boost/detail/lightweight_test.hpp>
-#include <boost/lexical_cast.hpp>
-
 #include <utree/utree.hpp>
 #include <qi/parse_qiexpr.hpp>
 #include <output/generate_sexpr.hpp>

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-28 20:55:57 EDT (Wed, 28 Apr 2010)
@@ -21,11 +21,11 @@
 
     ///////////////////////////////////////////////////////////////////////////
     template <typename Char>
- bool generate_sexpr(std::basic_string<Char>& os, utree const& tree);
+ bool generate_sexpr(std::basic_string<Char>& str, utree const& tree);
 
     ///////////////////////////////////////////////////////////////////////////
     template <typename Char>
- bool generate_sexpr_list(std::basic_string<Char>& os, utree const& tree);
+ bool generate_sexpr_list(std::basic_string<Char>& str, utree const& tree);
 }}
 
 #endif

Modified: trunk/libs/spirit/example/scheme/qi/generate_qiexpr.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/qi/generate_qiexpr.hpp (original)
+++ trunk/libs/spirit/example/scheme/qi/generate_qiexpr.hpp 2010-04-28 20:55:57 EDT (Wed, 28 Apr 2010)
@@ -11,7 +11,7 @@
 namespace scheme { namespace output
 {
     template <typename String>
- bool generate_qiexpr(utree& result, String& str);
+ bool generate_qi_expr(utree& result, String& str);
 }}
 
 #endif

Modified: trunk/libs/spirit/example/scheme/qi/generate_qiexpr_impl.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/qi/generate_qiexpr_impl.hpp (original)
+++ trunk/libs/spirit/example/scheme/qi/generate_qiexpr_impl.hpp 2010-04-28 20:55:57 EDT (Wed, 28 Apr 2010)
@@ -16,7 +16,7 @@
 {
     ///////////////////////////////////////////////////////////////////////////
     template <typename String>
- bool generate_qiexpr(utree& u, String& str)
+ bool generate_qi_expr(utree& u, String& str)
     {
         using boost::spirit::karma::space;
 

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-28 20:55:57 EDT (Wed, 28 Apr 2010)
@@ -41,14 +41,22 @@
             namespace phoenix = boost::phoenix;
 
             using boost::spirit::karma::eps;
- using boost::spirit::karma::string;
+ using boost::spirit::karma::ascii::string;
             using boost::spirit::karma::omit;
             using boost::spirit::karma::repeat;
             using boost::spirit::karma::_r1;
             using boost::spirit::karma::strict;
+ using boost::spirit::karma::eol;
 
             start =
                     nil
+ | rule_ % eol
+ | rule_
+ ;
+
+ rule_ =
+ &symbol(std::string("define"))
+ << repeat(1)[rule_name] << '=' << repeat(1)[alternative]
                 | alternative
                 ;
 
@@ -70,19 +78,21 @@
                 | term
                 ;
 
- term =
- strict[
- unary << '(' << repeat(1)[alternative] << ')'
- | primitive2 << '(' << literal << ',' << literal << ')'
- | primitive1 << '(' << literal << ')'
- | primitive0_rule
- | directive0 << '[' << repeat(1)[alternative] << ']'
- | alternative_rule
+ term = strict[
+ unary << '(' << repeat(1)[alternative] << ')'
+ | primitive2 << '(' << literal << ',' << literal << ')'
+ | primitive1 << '(' << literal << ')'
+ | primitive0_rule
+ | directive0 << '[' << repeat(1)[alternative] << ']'
+ | alternative_rule
                 ];
 
             primitive0_rule = strict[repeat(1)[primitive0]];
             alternative_rule = alternative;
 
+ rule_name = strict[repeat(1)[any_symbol]];
+
+ any_symbol = string;
             symbol = string(_r1);
             literal = '"' << string << '"';
             nil = eps;
@@ -105,6 +115,8 @@
                 directive0.add(utf8_symbol(name + *p));
 
             BOOST_SPIRIT_DEBUG_NODE(start);
+ BOOST_SPIRIT_DEBUG_NODE(rule_);
+ BOOST_SPIRIT_DEBUG_NODE(rule_name);
             BOOST_SPIRIT_DEBUG_NODE(alternative);
             BOOST_SPIRIT_DEBUG_NODE(permutation);
             BOOST_SPIRIT_DEBUG_NODE(sequence);
@@ -112,6 +124,7 @@
             BOOST_SPIRIT_DEBUG_NODE(nil);
             BOOST_SPIRIT_DEBUG_NODE(literal);
             BOOST_SPIRIT_DEBUG_NODE(symbol);
+ BOOST_SPIRIT_DEBUG_NODE(any_symbol);
             BOOST_SPIRIT_DEBUG_NODE(primitive0_rule);
             BOOST_SPIRIT_DEBUG_NODE(alternative_rule);
         }
@@ -119,10 +132,11 @@
         typedef rule<OutputIterator, space_type, utree()> delimiting_rule_type;
 
         delimiting_rule_type start, alternative, permutation, sequence, term;
- delimiting_rule_type primitive0_rule, alternative_rule;
+ delimiting_rule_type rule_, rule_name, primitive0_rule, alternative_rule;
         rule<OutputIterator, nil()> nil;
         rule<OutputIterator, scheme::utf8_string()> literal;
         rule<OutputIterator, scheme::utf8_symbol(std::string)> symbol;
+ rule<OutputIterator, scheme::utf8_symbol()> any_symbol;
 
         symbols<scheme::utf8_symbol> unary, directive0;
         symbols<scheme::utf8_symbol> primitive0, primitive1, primitive2;

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-28 20:55:57 EDT (Wed, 28 Apr 2010)
@@ -943,7 +943,10 @@
         ensure_list_type();
         clear();
         while (first != last)
- push_back(*first++);
+ {
+ push_back(*first);
+ ++first;
+ }
     }
 
     inline void utree::clear()


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