Imported Upstream version 1.51.0
[platform/upstream/boost.git] / libs / spirit / example / scheme / input / parse_sexpr_impl.hpp
1 //  Copyright (c) 2001-2010 Hartmut Kaiser
2 //  Copyright (c) 2001-2010 Joel de Guzman
3 //
4 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
5 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 #if !defined(BOOST_SPIRIT_PARSE_SEXPR_IMPL)
8 #define BOOST_SPIRIT_PARSE_SEXPR_IMPL
9
10 #include <iostream>
11 #include <string>
12 #include <boost/spirit/include/support_istream_iterator.hpp>
13 #include <boost/spirit/include/support_line_pos_iterator.hpp>
14 #include <boost/spirit/include/qi_parse.hpp>
15
16 #include <input/sexpr.hpp>
17 #include <input/parse_sexpr.hpp>
18
19 namespace scheme { namespace input
20 {
21     ///////////////////////////////////////////////////////////////////////////
22     template <typename Char>
23     bool parse_sexpr(
24         std::basic_istream<Char>& is,
25         utree& result,
26         std::string const& source_file)
27     {
28         // no white space skipping in the stream!
29         is.unsetf(std::ios::skipws);
30
31         typedef
32             boost::spirit::basic_istream_iterator<Char>
33         stream_iterator_type;
34         stream_iterator_type sfirst(is);
35         stream_iterator_type slast;
36
37         typedef boost::spirit::line_pos_iterator<stream_iterator_type>
38           iterator_type;
39         iterator_type first(sfirst);
40         iterator_type last(slast);
41
42         scheme::input::sexpr<iterator_type> p(source_file);
43         scheme::input::sexpr_white_space<iterator_type> ws;
44
45         using boost::spirit::qi::phrase_parse;
46         return phrase_parse(first, last, p, ws, result);
47     }
48
49     ///////////////////////////////////////////////////////////////////////////
50     template <typename Char>
51     bool parse_sexpr_list(
52         std::basic_istream<Char>& is,
53         utree& result,
54         std::string const& source_file)
55     {
56         // no white space skipping in the stream!
57         is.unsetf(std::ios::skipws);
58
59         typedef
60             boost::spirit::basic_istream_iterator<Char>
61         stream_iterator_type;
62         stream_iterator_type sfirst(is);
63         stream_iterator_type slast;
64
65         typedef boost::spirit::line_pos_iterator<stream_iterator_type>
66           iterator_type;
67         iterator_type first(sfirst);
68         iterator_type last(slast);
69
70         scheme::input::sexpr<iterator_type> p(source_file);
71         scheme::input::sexpr_white_space<iterator_type> ws;
72
73         using boost::spirit::qi::phrase_parse;
74         bool ok = phrase_parse(first, last, +p, ws, result);
75         result.tag(1); // line
76         return ok;
77     }
78
79     ///////////////////////////////////////////////////////////////////////////
80     template <typename Range>
81     typename boost::disable_if<boost::is_base_of<std::ios_base, Range>, bool>::type
82     parse_sexpr(
83         Range const& rng,
84         utree& result,
85         std::string const& source_file)
86     {
87         typedef boost::spirit::line_pos_iterator<typename Range::const_iterator>
88           iterator_type;
89
90         scheme::input::sexpr<iterator_type> p(source_file);
91         scheme::input::sexpr_white_space<iterator_type> ws;
92
93         iterator_type first(rng.begin());
94         iterator_type last(rng.end());
95
96         using boost::spirit::qi::phrase_parse;
97         return phrase_parse(first, last, p, ws, result);
98     }
99
100     template <typename Range>
101     typename boost::disable_if<boost::is_base_of<std::ios_base, Range>, bool>::type
102     parse_sexpr_list(
103         Range const& rng,
104         utree& result,
105         std::string const& source_file)
106     {
107         typedef boost::spirit::line_pos_iterator<typename Range::const_iterator>
108           iterator_type;
109
110         scheme::input::sexpr<iterator_type> p(source_file);
111         scheme::input::sexpr_white_space<iterator_type> ws;
112
113         iterator_type first(rng.begin());
114         iterator_type last(rng.end());
115
116         using boost::spirit::qi::phrase_parse;
117         bool ok = phrase_parse(first, last, +p, ws, result);
118         result.tag(1); // line
119         return ok;
120     }
121
122     ///////////////////////////////////////////////////////////////////////////
123     bool parse_sexpr(
124         utree const& in,
125         utree& result,
126         std::string const& source_file)
127     {
128         return parse_sexpr(in.get<utf8_string_range_type>(), result, source_file);
129     }
130
131     bool parse_sexpr_list(
132         utree const& in,
133         utree& result,
134         std::string const& source_file)
135     {
136         return parse_sexpr_list(in.get<utf8_string_range_type>(), result, source_file);
137     }
138 }}
139
140 #endif
141