|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r61316 - in trunk/libs/spirit/example/scheme: input scheme test/scheme
From: joel_at_[hidden]
Date: 2010-04-16 09:09:24
Author: djowel
Date: 2010-04-16 09:09:23 EDT (Fri, 16 Apr 2010)
New Revision: 61316
URL: http://svn.boost.org/trac/boost/changeset/61316
Log:
error handling
Text files modified:
trunk/libs/spirit/example/scheme/input/sexpr.hpp | 65 ++++++++++++++++++++++++++++++++++++---
trunk/libs/spirit/example/scheme/input/string.hpp | 2
trunk/libs/spirit/example/scheme/scheme/compiler.hpp | 7 ++++
trunk/libs/spirit/example/scheme/test/scheme/scheme_test1.scm | 2
4 files changed, 68 insertions(+), 8 deletions(-)
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-16 09:09:23 EDT (Fri, 16 Apr 2010)
@@ -23,8 +23,8 @@
namespace scheme { namespace input
{
- using boost::spirit::ascii::char_;
- using boost::spirit::ascii::space;
+ using boost::spirit::standard::char_;
+ using boost::spirit::standard::space;
using boost::spirit::qi::grammar;
using boost::spirit::qi::rule;
using boost::spirit::qi::eol;
@@ -37,6 +37,13 @@
using boost::spirit::qi::bool_;
using boost::spirit::qi::no_case;
using boost::spirit::qi::lexeme;
+ using boost::spirit::qi::on_error;
+ using boost::spirit::qi::fail;
+ using boost::spirit::qi::_1;
+ using boost::spirit::qi::_2;
+ 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
@@ -58,14 +65,19 @@
template <typename Iterator>
struct sexpr : grammar<Iterator, sexpr_white_space<Iterator>, utree()>
{
- sexpr() : sexpr::base_type(start)
+ typedef
+ boost::function<void(Iterator, Iterator, Iterator, info const&)>
+ errorf_type;
+
+ sexpr(errorf_type errorf = errorf_type())
+ : sexpr::base_type(start), error_handler(errorf)
{
real_parser<double, strict_real_policies<double> > strict_double;
uint_parser<unsigned char, 16, 2, 2> hex2;
start = atom | list;
- list = '(' >> *start >> ')';
+ list = '(' > *start > ')';
atom = strict_double
| integer
@@ -78,12 +90,20 @@
std::string exclude = std::string(" ();\"\x01-\x1f\x7f") + '\0';
symbol = lexeme[+(~char_(exclude))];
- integer = lexeme[no_case["0x"] >> hex]
+ integer = lexeme[no_case["0x"] > hex]
| lexeme['0' >> oct]
| int_
;
- byte_str = lexeme[no_case['b'] >> +hex2];
+ byte_str = lexeme[no_case['b'] > +hex2];
+
+ start.name("sexpr");
+ list.name("list");
+ atom.name("atom");
+ symbol.name("symbol");
+ integer.name("integer");
+ byte_str.name("byte_str");
+ on_error<fail>(start, error_handler(_1, _2, _3, _4));
}
rule<Iterator, sexpr_white_space<Iterator>, utree()> start, list;
@@ -92,6 +112,39 @@
rule<Iterator, utf8_symbol()> symbol;
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 err_pos,
+ Iterator last, info const& what) const
+ {
+ if (!errorf.empty())
+ {
+ errorf(first, err_pos, last, 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;
};
}}
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-16 09:09:23 EDT (Fri, 16 Apr 2010)
@@ -22,7 +22,7 @@
namespace scheme { namespace input
{
- using boost::spirit::ascii::char_;
+ using boost::spirit::standard::char_;
using boost::spirit::qi::grammar;
using boost::spirit::qi::rule;
using boost::spirit::qi::_val;
Modified: trunk/libs/spirit/example/scheme/scheme/compiler.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/scheme/compiler.hpp (original)
+++ trunk/libs/spirit/example/scheme/scheme/compiler.hpp 2010-04-16 09:09:23 EDT (Fri, 16 Apr 2010)
@@ -246,6 +246,13 @@
}
}
+ interpreter(utree const& program, environment* outer = 0)
+ {
+ if (outer == 0)
+ build_basic_environment(env);
+ compile_all(program, env, flist, fragments);
+ }
+
utree eval(args_type args) const
{
return flist.back()(args);
Modified: trunk/libs/spirit/example/scheme/test/scheme/scheme_test1.scm
==============================================================================
--- trunk/libs/spirit/example/scheme/test/scheme/scheme_test1.scm (original)
+++ trunk/libs/spirit/example/scheme/test/scheme/scheme_test1.scm 2010-04-16 09:09:23 EDT (Fri, 16 Apr 2010)
@@ -10,7 +10,7 @@
; The hello-world for interpreters ;-)
(define (factorial n)
(if (<= n 0) 1
- (* n (factorial (- n 1)))))
+ (* n (factorial (- n 1))))
(factorial 10) ; 3628800
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