Boost logo

Boost-Commit :

From: hartmut.kaiser_at_[hidden]
Date: 2008-04-17 21:11:26


Author: hkaiser
Date: 2008-04-17 21:11:25 EDT (Thu, 17 Apr 2008)
New Revision: 44527
URL: http://svn.boost.org/trac/boost/changeset/44527

Log:
Spirit: added eol and end parsers.
Added:
   trunk/boost/spirit/home/qi/char/primitives.hpp (contents, props changed)
Text files modified:
   trunk/boost/spirit/home/qi/char.hpp | 1 +
   trunk/boost/spirit/home/qi/char/meta_grammar.hpp | 5 +++++
   trunk/boost/spirit/home/qi/parse.hpp | 6 +++---
   trunk/boost/spirit/home/support/placeholders.hpp | 7 +++++++
   trunk/libs/spirit/test/qi/char.cpp | 34 +++++++++++++++++++++++++++++++++-
   5 files changed, 49 insertions(+), 4 deletions(-)

Modified: trunk/boost/spirit/home/qi/char.hpp
==============================================================================
--- trunk/boost/spirit/home/qi/char.hpp (original)
+++ trunk/boost/spirit/home/qi/char.hpp 2008-04-17 21:11:25 EDT (Thu, 17 Apr 2008)
@@ -10,6 +10,7 @@
 #include <boost/spirit/home/qi/char/char_parser.hpp>
 #include <boost/spirit/home/qi/char/char.hpp>
 #include <boost/spirit/home/qi/char/char_class.hpp>
+#include <boost/spirit/home/qi/char/primitives.hpp>
 #include <boost/spirit/home/qi/char/meta_grammar.hpp>
 
 #endif

Modified: trunk/boost/spirit/home/qi/char/meta_grammar.hpp
==============================================================================
--- trunk/boost/spirit/home/qi/char/meta_grammar.hpp (original)
+++ trunk/boost/spirit/home/qi/char/meta_grammar.hpp 2008-04-17 21:11:25 EDT (Thu, 17 Apr 2008)
@@ -64,6 +64,9 @@
     template <typename Tag>
     struct char_class;
 
+ struct eol_director;
+ struct end_director;
+
     ///////////////////////////////////////////////////////////////////////////
     struct char_meta_grammar;
 
@@ -338,6 +341,8 @@
               , qi::domain
               , char_class<mpl::_>
>
+ , meta_grammar::terminal_rule<qi::domain, tag::eol, eol_director>
+ , meta_grammar::terminal_rule<qi::domain, tag::end, end_director>
>
     {};
 

Added: trunk/boost/spirit/home/qi/char/primitives.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/spirit/home/qi/char/primitives.hpp 2008-04-17 21:11:25 EDT (Thu, 17 Apr 2008)
@@ -0,0 +1,96 @@
+/*=============================================================================
+ Copyright (c) 2001-2008 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_PRIMITIVES_APR_17_2008_0751PM)
+#define BOOST_SPIRIT_PRIMITIVES_APR_17_2008_0751PM
+
+///////////////////////////////////////////////////////////////////////////////
+namespace boost { namespace spirit { namespace qi
+{
+ ///////////////////////////////////////////////////////////////////////////
+ // the eol_director matches line endings
+ ///////////////////////////////////////////////////////////////////////////
+ struct eol_director
+ {
+ template <typename Component, typename Context, typename Iterator>
+ struct attribute
+ {
+ typedef unused_type type;
+ };
+
+ template <
+ typename Component
+ , typename Iterator, typename Context
+ , typename Skipper, typename Attribute>
+ static bool parse(
+ Component const& component
+ , Iterator& first, Iterator const& last
+ , Context& context, Skipper const& skipper
+ , Attribute& attr)
+ {
+ qi::skip(first, last, skipper);
+
+ bool matched = false;
+ if (first != last && *first == '\r') // CR
+ {
+ matched = true;
+ ++first;
+ }
+ if (first != last && *first == '\n') // LF
+ {
+ matched = true;
+ ++first;
+ }
+
+ return matched;
+ }
+
+ template <typename Component>
+ static std::string what(Component const&)
+ {
+ return "eol";
+ }
+ };
+
+ ///////////////////////////////////////////////////////////////////////////
+ // the end_director matches the end of the input
+ ///////////////////////////////////////////////////////////////////////////
+ struct end_director
+ {
+ template <typename Component, typename Context, typename Iterator>
+ struct attribute
+ {
+ typedef unused_type type;
+ };
+
+ template <
+ typename Component
+ , typename Iterator, typename Context
+ , typename Skipper, typename Attribute>
+ static bool parse(
+ Component const& component
+ , Iterator& first, Iterator const& last
+ , Context& context, Skipper const& skipper
+ , Attribute& attr)
+ {
+ qi::skip(first, last, skipper);
+ return first == last;
+ }
+
+ template <typename Component>
+ static std::string what(Component const&)
+ {
+ return "end";
+ }
+ };
+
+///////////////////////////////////////////////////////////////////////////////
+}}}
+
+#endif
+
+

Modified: trunk/boost/spirit/home/qi/parse.hpp
==============================================================================
--- trunk/boost/spirit/home/qi/parse.hpp (original)
+++ trunk/boost/spirit/home/qi/parse.hpp 2008-04-17 21:11:25 EDT (Thu, 17 Apr 2008)
@@ -95,9 +95,9 @@
         typedef spirit::traits::skipper_is_compatible<Expr, Skipper>
             skipper_is_compatible;
             
- BOOST_MPL_ASSERT_MSG(
- skipper_is_compatible::value,
- skipper_is_not_compatible_with_parser, (Iterator, Expr, Skipper));
+// BOOST_MPL_ASSERT_MSG(
+// skipper_is_compatible::value,
+// skipper_is_not_compatible_with_parser, (Iterator, Expr, Skipper));
         
         typedef typename result_of::as_component<qi::domain, Expr>::type component;
         typedef typename component::director director;

Modified: trunk/boost/spirit/home/support/placeholders.hpp
==============================================================================
--- trunk/boost/spirit/home/support/placeholders.hpp (original)
+++ trunk/boost/spirit/home/support/placeholders.hpp 2008-04-17 21:11:25 EDT (Thu, 17 Apr 2008)
@@ -23,6 +23,8 @@
         struct wchar {};
         struct lit {};
         struct wlit {};
+ struct eol {};
+ struct end {};
 
         struct bin {};
         struct oct {};
@@ -80,6 +82,8 @@
     typedef proto::terminal<tag::wchar>::type wchar_type;
     typedef proto::terminal<tag::lit>::type lit_type;
     typedef proto::terminal<tag::wlit>::type wlit_type;
+ typedef proto::terminal<tag::eol>::type eol_type;
+ typedef proto::terminal<tag::end>::type end_type;
     
     typedef proto::terminal<tag::bin>::type bin_type;
     typedef proto::terminal<tag::oct>::type oct_type;
@@ -136,6 +140,8 @@
     proto::terminal<tag::wchar>::type const wchar = {{}};
     proto::terminal<tag::lit>::type const lit = {{}};
     proto::terminal<tag::wlit>::type const wlit = {{}};
+ proto::terminal<tag::eol>::type const eol = {{}};
+ proto::terminal<tag::end>::type const end = {{}};
     
     proto::terminal<tag::bin>::type const bin = {{}};
     proto::terminal<tag::oct>::type const oct = {{}};
@@ -197,6 +203,7 @@
     inline void silence_unused_warnings__placeholders()
     {
         (void) char_; (void) wchar; (void) lit; (void) wlit;
+ (void) eol; (void) end;
         (void) bin; (void) oct; (void) hex;
         (void) byte; (void) word; (void) dword;
         (void) big_word; (void) big_dword;

Modified: trunk/libs/spirit/test/qi/char.cpp
==============================================================================
--- trunk/libs/spirit/test/qi/char.cpp (original)
+++ trunk/libs/spirit/test/qi/char.cpp 2008-04-17 21:11:25 EDT (Thu, 17 Apr 2008)
@@ -23,7 +23,9 @@
     using namespace boost::spirit::ascii;
     using boost::spirit::char_;
     using boost::spirit::wchar;
-
+ using boost::spirit::eol;
+ using boost::spirit::end;
+
     {
         BOOST_TEST(test("x", 'x'));
         BOOST_TEST(test(L"x", L'x'));
@@ -115,5 +117,35 @@
         BOOST_TEST((test("h", char_(val('a'), val('n')))));
     }
 
+ { // eol
+ BOOST_TEST(test("\r", eol));
+ BOOST_TEST(test("\r\n", eol));
+ BOOST_TEST(test("\n", eol));
+ BOOST_TEST(!test("\b", eol));
+
+ BOOST_TEST(test(" \r", eol, char_(' ')));
+ BOOST_TEST(test(" \r\n", eol, char_(' ')));
+ BOOST_TEST(test(" \n", eol, char_(' ')));
+ BOOST_TEST(!test(" \b", eol, char_(' ')));
+
+ BOOST_TEST(test(L"\r", eol));
+ BOOST_TEST(test(L"\r\n", eol));
+ BOOST_TEST(test(L"\n", eol));
+ BOOST_TEST(!test(L"\b", eol));
+
+ BOOST_TEST(test(L" \r", eol, wchar(L' ')));
+ BOOST_TEST(test(L" \r\n", eol, wchar(L' ')));
+ BOOST_TEST(test(L" \n", eol, wchar(L' ')));
+ BOOST_TEST(!test(L" \b", eol, wchar(L' ')));
+ }
+
+ { // end
+ BOOST_TEST(test("", end));
+ BOOST_TEST(!test("a", end));
+
+ BOOST_TEST(test(" ", end, space));
+ BOOST_TEST(!test(" a", end, space));
+ }
+
     return boost::report_errors();
 }


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