Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r61330 - trunk/libs/spirit/example/scheme/input
From: joel_at_[hidden]
Date: 2010-04-17 01:09:38


Author: djowel
Date: 2010-04-17 01:09:37 EDT (Sat, 17 Apr 2010)
New Revision: 61330
URL: http://svn.boost.org/trac/boost/changeset/61330

Log:
more error handling
Added:
   trunk/libs/spirit/example/scheme/input/error_handler.hpp (contents, props changed)
Text files modified:
   trunk/libs/spirit/example/scheme/input/sexpr.hpp | 39 ++++-----------------------------------
   trunk/libs/spirit/example/scheme/input/string.hpp | 24 ++++++++++++++----------
   2 files changed, 18 insertions(+), 45 deletions(-)

Added: trunk/libs/spirit/example/scheme/input/error_handler.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/scheme/input/error_handler.hpp 2010-04-17 01:09:37 EDT (Sat, 17 Apr 2010)
@@ -0,0 +1,64 @@
+/*=============================================================================
+ Copyright (c) 2001-2010 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)
+=============================================================================*/
+#if !defined(BOOST_SPIRIT_SEXPR_ERROR_HANDLER)
+#define BOOST_SPIRIT_SEXPR_ERROR_HANDLER
+
+#include <boost/spirit/home/support/info.hpp>
+#include <boost/spirit/include/phoenix_core.hpp>
+#include <boost/function.hpp>
+#include <iostream>
+
+namespace scheme { namespace input
+{
+ template <typename Iterator>
+ struct error_handler
+ {
+ typedef
+ boost::function<
+ void(
+ Iterator, Iterator, Iterator,
+ boost::spirit::info const&
+ )>
+ errorf_type;
+
+ errorf_type errorf;
+ error_handler(errorf_type errorf)
+ : errorf(errorf) {}
+
+ template <typename, typename, typename, typename>
+ struct result { typedef void type; };
+
+ void operator()(
+ Iterator first, Iterator last,
+ Iterator err_pos, boost::spirit::info const& what) const
+ {
+ if (!errorf.empty())
+ {
+ // "Overridden"
+ errorf(first, last, err_pos, what);
+ }
+ else
+ {
+ // Default handler
+ Iterator eol = err_pos;
+ while (eol != last && *eol != '\n' && *eol != '\r')
+ ++eol;
+
+ std::cerr
+ << "Error! Expecting "
+ << what
+ << " here: \""
+ << std::string(err_pos, eol)
+ << "\""
+ << std::endl
+ ;
+ }
+ }
+ };
+}}
+
+#endif

Modified: trunk/libs/spirit/example/scheme/input/sexpr.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/input/sexpr.hpp (original)
+++ trunk/libs/spirit/example/scheme/input/sexpr.hpp 2010-04-17 01:09:37 EDT (Sat, 17 Apr 2010)
@@ -20,6 +20,7 @@
 #include <utree/utree.hpp>
 #include <utree/operators.hpp>
 #include <input/string.hpp>
+#include <input/error_handler.hpp>
 
 namespace scheme { namespace input
 {
@@ -44,7 +45,6 @@
     using boost::spirit::qi::_3;
     using boost::spirit::qi::_4;
     using boost::spirit::info;
- using boost::phoenix::function;
 
     typedef boost::uint32_t uchar; // a unicode code point
 
@@ -65,8 +65,8 @@
     template <typename Iterator>
     struct sexpr : grammar<Iterator, sexpr_white_space<Iterator>, utree()>
     {
- typedef
- boost::function<void(Iterator, Iterator, Iterator, info const&)>
+ typedef typename
+ input::error_handler<Iterator>::errorf_type
         errorf_type;
 
         sexpr(errorf_type errorf = errorf_type())
@@ -113,38 +113,7 @@
         rule<Iterator, binary_string()> byte_str;
         scheme::input::string<Iterator> string;
 
- struct error_handler_
- {
- errorf_type errorf;
- error_handler_(errorf_type errorf)
- : errorf(errorf) {}
-
- template <typename, typename, typename, typename>
- struct result { typedef void type; };
-
- void operator()(
- Iterator first, Iterator last,
- Iterator err_pos, info const& what) const
- {
- if (!errorf.empty())
- {
- errorf(first, last, err_pos, what);
- }
- else
- {
- std::cout
- << "Error! Expecting "
- << what // what failed?
- << " here: \""
- << std::string(err_pos, last) // iterators to error-pos, end
- << "\""
- << std::endl
- ;
- }
- }
- };
-
- function<error_handler_> const error_handler;
+ function<input::error_handler<Iterator> > const error_handler;
     };
 }}
 

Modified: trunk/libs/spirit/example/scheme/input/string.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/input/string.hpp (original)
+++ trunk/libs/spirit/example/scheme/input/string.hpp 2010-04-17 01:09:37 EDT (Sat, 17 Apr 2010)
@@ -81,24 +81,28 @@
             function<detail::push_esc> push_esc;
 
             char_esc
- = '\\'
- >> ( ('u' >> hex4) [push_utf8(_r1, _1)]
- | ('U' >> hex8) [push_utf8(_r1, _1)]
- | char_("btnfr\\\"'") [push_esc(_r1, _1)]
- )
+ = '\\'
+ > ( ('u' > hex4) [push_utf8(_r1, _1)]
+ | ('U' > hex8) [push_utf8(_r1, _1)]
+ | char_("btnfr\\\"'") [push_esc(_r1, _1)]
+ )
                 ;
 
             char_lit
- = '\''
- >> (char_esc(_val) | (~char_('\'')) [_val += _1])
- >> '\''
+ = '\''
+ > (char_esc(_val) | (~char_('\'')) [_val += _1])
+ > '\''
                 ;
 
             start
                 = '"'
- >> *(char_esc(_val) | (~char_('"')) [_val += _1])
- >> '"'
+ > *(char_esc(_val) | (~char_('"')) [_val += _1])
+ > '"'
                 ;
+
+ char_esc.name("char_esc");
+ char_lit.name("char_lit");
+ start.name("string");
         }
 
         rule<Iterator, void(std::string&)> char_esc;


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