]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/context/example/v1/parser.cpp
2 // Copyright Oliver Kowalke 2014.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
14 #include <boost/context/all.hpp>
19 * E ---> T {('+'|'-') T}
20 * T ---> S {('*'|'/') S}
21 * S ---> digit | '(' E ')'
26 std::function
<void(char)> cb
;
29 return std::char_traits
<char>::to_char_type(is
.get());
40 Parser(std::istream
& is_
,std::function
<void(char)> cb_
) :
41 next(), is(is_
), cb(cb_
)
52 while (next
=='+'||next
=='-'){
61 while (next
=='*'||next
=='/'){
81 throw std::runtime_error("parsing failed");
85 throw std::runtime_error("parsing failed");
92 std::istringstream
is("1+1");
94 std::exception_ptr except
;
96 // create handle to main execution context
97 auto sink(boost::context::execution_context::current());
98 // execute parser in new execution context
99 boost::context::execution_context
source(
100 [&sink
,&is
,&done
,&except
](void*){
101 // create parser with callback function
104 // resume main execution context
108 // start recursive parsing
111 // store other exceptions in exception-pointer
112 except
= std::current_exception();
114 // set termination flag
116 // resume main execution context
120 // user-code pulls parsed data from parser
121 // invert control flow
124 std::rethrow_exception(except
);
127 printf("Parsed: %c\n",* static_cast<char*>(vp
));
130 std::rethrow_exception(except
);
133 std::cout
<< "main: done" << std::endl
;
135 } catch ( std::exception
const& e
) {
136 std::cerr
<< "exception: " << e
.what() << std::endl
;