|
Boost-Commit : |
From: joel_at_[hidden]
Date: 2008-07-09 16:11:34
Author: djowel
Date: 2008-07-09 16:11:33 EDT (Wed, 09 Jul 2008)
New Revision: 47276
URL: http://svn.boost.org/trac/boost/changeset/47276
Log:
tutorials
Added:
branches/release/libs/spirit/example/qi/num_list1.cpp
- copied, changed from r47257, /branches/release/libs/spirit/example/qi/num_list.cpp
branches/release/libs/spirit/example/qi/num_list4.cpp (contents, props changed)
Removed:
branches/release/libs/spirit/example/qi/num_list.cpp
Text files modified:
branches/release/libs/spirit/example/qi/Jamfile | 3 ++-
branches/release/libs/spirit/example/qi/num_list1.cpp | 23 +++++------------------
branches/release/libs/spirit/example/qi/num_list2.cpp | 3 ++-
branches/release/libs/spirit/example/qi/num_list3.cpp | 4 ++--
branches/release/libs/spirit/example/qi/sum.cpp | 3 +++
5 files changed, 14 insertions(+), 22 deletions(-)
Modified: branches/release/libs/spirit/example/qi/Jamfile
==============================================================================
--- branches/release/libs/spirit/example/qi/Jamfile (original)
+++ branches/release/libs/spirit/example/qi/Jamfile 2008-07-09 16:11:33 EDT (Wed, 09 Jul 2008)
@@ -13,9 +13,10 @@
exe roman : roman.cpp ;
exe mini_xml1 : mini_xml1.cpp ;
exe mini_xml2 : mini_xml2.cpp ;
-exe num_list : num_list.cpp ;
+exe num_list1 : num_list1.cpp ;
exe num_list2 : num_list2.cpp ;
exe num_list3 : num_list3.cpp ;
+exe num_list4 : num_list4.cpp ;
exe calc1 : calc1.cpp ;
exe calc2 : calc2.cpp ;
Deleted: branches/release/libs/spirit/example/qi/num_list.cpp
==============================================================================
--- branches/release/libs/spirit/example/qi/num_list.cpp 2008-07-09 16:11:33 EDT (Wed, 09 Jul 2008)
+++ (empty file)
@@ -1,100 +0,0 @@
-/*=============================================================================
- Copyright (c) 2002-2007 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)
-=============================================================================*/
-///////////////////////////////////////////////////////////////////////////////
-//
-// This sample demontrates a parser for a comma separated list of numbers.
-// The numbers are inserted in a vector using phoenix.
-//
-// [ JDG May 10, 2002 ] spirit1
-// [ JDG March 24, 2007 ] spirit2
-//
-///////////////////////////////////////////////////////////////////////////////
-
-#include <boost/config/warning_disable.hpp>
-#include <boost/spirit/include/qi.hpp>
-#include <boost/spirit/include/phoenix_core.hpp>
-#include <boost/spirit/include/phoenix_operator.hpp>
-#include <boost/spirit/include/phoenix_stl.hpp>
-
-#include <iostream>
-#include <string>
-#include <vector>
-
-using namespace boost::phoenix;
-using namespace boost::spirit;
-using namespace boost::spirit::qi;
-using namespace boost::spirit::ascii;
-using namespace boost::spirit::arg_names;
-
-///////////////////////////////////////////////////////////////////////////////
-// Our number list compiler
-///////////////////////////////////////////////////////////////////////////////
-template <typename Iterator>
-bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v)
-{
- bool r = phrase_parse(first, last,
-
- // Begin grammar
- (
- double_[push_back(ref(v), _1)]
- >> *(',' >> double_[push_back(ref(v), _1)])
- )
- ,
- // End grammar
-
- space);
-
- if (first != last) // fail if we did not get a full match
- return false;
- return r;
-}
-
-////////////////////////////////////////////////////////////////////////////
-// Main program
-////////////////////////////////////////////////////////////////////////////
-int
-main()
-{
- std::cout << "/////////////////////////////////////////////////////////\n\n";
- std::cout << "\t\tA comma separated list parser for Spirit...\n\n";
- std::cout << "/////////////////////////////////////////////////////////\n\n";
-
- std::cout << "Give me a comma separated list of numbers.\n";
- std::cout << "The numbers will be inserted in a vector of numbers\n";
- std::cout << "Type [q or Q] to quit\n\n";
-
- std::string str;
- while (getline(std::cin, str))
- {
- if (str.empty() || str[0] == 'q' || str[0] == 'Q')
- break;
-
- std::vector<double> v;
- if (parse_numbers(str.begin(), str.end(), v))
- {
- std::cout << "-------------------------\n";
- std::cout << "Parsing succeeded\n";
- std::cout << str << " Parses OK: " << std::endl;
-
- for (std::vector<double>::size_type i = 0; i < v.size(); ++i)
- std::cout << i << ": " << v[i] << std::endl;
-
- std::cout << "\n-------------------------\n";
- }
- else
- {
- std::cout << "-------------------------\n";
- std::cout << "Parsing failed\n";
- std::cout << "-------------------------\n";
- }
- }
-
- std::cout << "Bye... :-) \n\n";
- return 0;
-}
-
-
Copied: branches/release/libs/spirit/example/qi/num_list1.cpp (from r47257, /branches/release/libs/spirit/example/qi/num_list.cpp)
==============================================================================
--- /branches/release/libs/spirit/example/qi/num_list.cpp (original)
+++ branches/release/libs/spirit/example/qi/num_list1.cpp 2008-07-09 16:11:33 EDT (Wed, 09 Jul 2008)
@@ -7,7 +7,7 @@
///////////////////////////////////////////////////////////////////////////////
//
// This sample demontrates a parser for a comma separated list of numbers.
-// The numbers are inserted in a vector using phoenix.
+// No actions.
//
// [ JDG May 10, 2002 ] spirit1
// [ JDG March 24, 2007 ] spirit2
@@ -16,9 +16,6 @@
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
-#include <boost/spirit/include/phoenix_core.hpp>
-#include <boost/spirit/include/phoenix_operator.hpp>
-#include <boost/spirit/include/phoenix_stl.hpp>
#include <iostream>
#include <string>
@@ -31,27 +28,18 @@
using namespace boost::spirit::arg_names;
///////////////////////////////////////////////////////////////////////////////
-// Our number list compiler
+// Our number list parser
///////////////////////////////////////////////////////////////////////////////
+//[tutorial_numlist1
template <typename Iterator>
bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v)
{
- bool r = phrase_parse(first, last,
-
- // Begin grammar
- (
- double_[push_back(ref(v), _1)]
- >> *(',' >> double_[push_back(ref(v), _1)])
- )
- ,
- // End grammar
-
- space);
-
+ bool r = phrase_parse(first, last, double_ >> *(',' >> double_), space);
if (first != last) // fail if we did not get a full match
return false;
return r;
}
+//]
////////////////////////////////////////////////////////////////////////////
// Main program
@@ -64,7 +52,6 @@
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Give me a comma separated list of numbers.\n";
- std::cout << "The numbers will be inserted in a vector of numbers\n";
std::cout << "Type [q or Q] to quit\n\n";
std::string str;
Modified: branches/release/libs/spirit/example/qi/num_list2.cpp
==============================================================================
--- branches/release/libs/spirit/example/qi/num_list2.cpp (original)
+++ branches/release/libs/spirit/example/qi/num_list2.cpp 2008-07-09 16:11:33 EDT (Wed, 09 Jul 2008)
@@ -40,7 +40,8 @@
// Begin grammar
(
- double_[push_back(ref(v), _1)] % ','
+ double_[push_back(ref(v), _1)]
+ >> *(',' >> double_[push_back(ref(v), _1)])
)
,
// End grammar
Modified: branches/release/libs/spirit/example/qi/num_list3.cpp
==============================================================================
--- branches/release/libs/spirit/example/qi/num_list3.cpp (original)
+++ branches/release/libs/spirit/example/qi/num_list3.cpp 2008-07-09 16:11:33 EDT (Wed, 09 Jul 2008)
@@ -40,12 +40,12 @@
// Begin grammar
(
- double_ % ','
+ double_[push_back(ref(v), _1)] % ','
)
,
// End grammar
- v, space);
+ space);
if (first != last) // fail if we did not get a full match
return false;
Added: branches/release/libs/spirit/example/qi/num_list4.cpp
==============================================================================
--- (empty file)
+++ branches/release/libs/spirit/example/qi/num_list4.cpp 2008-07-09 16:11:33 EDT (Wed, 09 Jul 2008)
@@ -0,0 +1,99 @@
+/*=============================================================================
+ Copyright (c) 2002-2007 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)
+=============================================================================*/
+///////////////////////////////////////////////////////////////////////////////
+//
+// This sample demontrates a parser for a comma separated list of numbers.
+// The numbers are inserted in a vector using phoenix.
+//
+// [ JDG May 10, 2002 ] spirit1
+// [ JDG March 24, 2007 ] spirit2
+//
+///////////////////////////////////////////////////////////////////////////////
+
+#include <boost/config/warning_disable.hpp>
+#include <boost/spirit/include/qi.hpp>
+#include <boost/spirit/include/phoenix_core.hpp>
+#include <boost/spirit/include/phoenix_operator.hpp>
+#include <boost/spirit/include/phoenix_stl.hpp>
+
+#include <iostream>
+#include <string>
+#include <vector>
+
+using namespace boost::phoenix;
+using namespace boost::spirit;
+using namespace boost::spirit::qi;
+using namespace boost::spirit::ascii;
+using namespace boost::spirit::arg_names;
+
+///////////////////////////////////////////////////////////////////////////////
+// Our number list compiler
+///////////////////////////////////////////////////////////////////////////////
+template <typename Iterator>
+bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v)
+{
+ bool r = phrase_parse(first, last,
+
+ // Begin grammar
+ (
+ double_ % ','
+ )
+ ,
+ // End grammar
+
+ v, space);
+
+ if (first != last) // fail if we did not get a full match
+ return false;
+ return r;
+}
+
+////////////////////////////////////////////////////////////////////////////
+// Main program
+////////////////////////////////////////////////////////////////////////////
+int
+main()
+{
+ std::cout << "/////////////////////////////////////////////////////////\n\n";
+ std::cout << "\t\tA comma separated list parser for Spirit...\n\n";
+ std::cout << "/////////////////////////////////////////////////////////\n\n";
+
+ std::cout << "Give me a comma separated list of numbers.\n";
+ std::cout << "The numbers will be inserted in a vector of numbers\n";
+ std::cout << "Type [q or Q] to quit\n\n";
+
+ std::string str;
+ while (getline(std::cin, str))
+ {
+ if (str.empty() || str[0] == 'q' || str[0] == 'Q')
+ break;
+
+ std::vector<double> v;
+ if (parse_numbers(str.begin(), str.end(), v))
+ {
+ std::cout << "-------------------------\n";
+ std::cout << "Parsing succeeded\n";
+ std::cout << str << " Parses OK: " << std::endl;
+
+ for (std::vector<double>::size_type i = 0; i < v.size(); ++i)
+ std::cout << i << ": " << v[i] << std::endl;
+
+ std::cout << "\n-------------------------\n";
+ }
+ else
+ {
+ std::cout << "-------------------------\n";
+ std::cout << "Parsing failed\n";
+ std::cout << "-------------------------\n";
+ }
+ }
+
+ std::cout << "Bye... :-) \n\n";
+ return 0;
+}
+
+
Modified: branches/release/libs/spirit/example/qi/sum.cpp
==============================================================================
--- branches/release/libs/spirit/example/qi/sum.cpp (original)
+++ branches/release/libs/spirit/example/qi/sum.cpp 2008-07-09 16:11:33 EDT (Wed, 09 Jul 2008)
@@ -30,6 +30,8 @@
///////////////////////////////////////////////////////////////////////////////
// Our adder
///////////////////////////////////////////////////////////////////////////////
+
+//[tutorial_adder
template <typename Iterator>
bool adder(Iterator first, Iterator last, double& n)
{
@@ -48,6 +50,7 @@
return false;
return r;
}
+//]
////////////////////////////////////////////////////////////////////////////
// Main program
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