Bertini 2 - C++ Core
Software for Numerical Algebraic Geometry
Loading...
Searching...
No Matches
configIni_parse.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//configIni_parser.hpp is free software: you can redistribute it and/or modify
4//it under the terms of the GNU General Public License as published by
5//the Free Software Foundation, either version 3 of the License, or
6//(at your option) any later version.
7//
8//configIni_parser.hpp is distributed in the hope that it will be useful,
9//but WITHOUT ANY WARRANTY; without even the implied warranty of
10//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11//GNU General Public License for more details.
12//
13//You should have received a copy of the GNU General Public License
14//along with configIni_parser.hpp. If not, see <http://www.gnu.org/licenses/>.
15//
16// Copyright(C) 2015 - 2017 by Bertini2 Development Team
17//
18// See <http://www.gnu.org/licenses/> for a copy of the license,
19// as well as COPYING. Bertini2 is provided with permitted
20// additional terms in the b2/licenses/ directory.
21
22// individual authors of this file include:
23// James Collins
24// West Texas A&M University
25// Spring, Summer 2015
26
30
31
32#ifndef BERTINI_CONFIG_INI_PARSE_HPP
33#define BERTINI_CONFIG_INI_PARSE_HPP
34
35
36#include <boost/fusion/adapted.hpp>
37#include <boost/fusion/include/adapted.hpp>
38
39#define BOOST_SPIRIT_USE_PHOENIX_V3 1
40
41
42#include <boost/spirit/include/qi_core.hpp>
43#include <boost/spirit/include/qi.hpp>
44#include <boost/phoenix/core.hpp>
45#include <boost/phoenix/operator.hpp>
46
47#include <boost/spirit/include/support_istream_iterator.hpp>
48
49#include <boost/algorithm/string.hpp>
50
51
52#include <iostream>
53#include <string>
54
55
56
57// this solution for *lazy* to_lower is adapted from the SO forum, user sehe.
58// https://stackoverflow.com/questions/21516201/how-to-create-boost-phoenix-make-shared
59// post found using google search terms `phoenix construct shared_ptr`
60namespace {
61 struct to_lower_f
62 {
63 template <typename... A> struct result
64 { typedef std::string type; };
65
66 template <typename A>
67 std::string operator()(A a) const {
68 return boost::algorithm::to_lower_copy(a);
69 }
70 };
71
72 using lazy_to_lower_ = boost::phoenix::function<to_lower_f >;
73}
74
75
76
77
78
79namespace bertini
80{
81 namespace settings
82 {
83 namespace parsing
84 {
107
108 template<typename Iterator, typename Skipper = ascii::space_type> //boost::spirit::unused_type
109 struct ConfigToIni : qi::grammar<Iterator, std::string(), Skipper>
110 {
111
112
113 ConfigToIni() : ConfigToIni::base_type(root_rule_, "ConfigToIni")
114 {
115 namespace phx = boost::phoenix;
116 using qi::_1;
117 using qi::_2;
118 using qi::_3;
119 using qi::_4;
120 using qi::_val;
121 using qi::eol;
122 using qi::eoi;
123 using qi::eps;
124 using qi::lit;
125 using qi::char_;
126 using qi::omit;
127 using boost::spirit::lexeme;
128 using boost::spirit::as_string;
129
130 root_rule_.name("ConfigToIni_root_rule");
131
132 root_rule_ = eps[_val = ""] >> *line_[_val = _val + _1+"\n"] >> -last_line_[_val = _val + _1];
133
134
135 line_.name("line_of_settings_input");
136 line_ = (eps >> colon_ >> yes_eol_)[_val = lazy_to_lower_()(_1) + "=" + _2];
137
138 last_line_.name("line_of_settings_input_with_no_eol");
139 last_line_ = (eps >> colon_ >> no_eol_)[_val = lazy_to_lower_()(_1) + "=" + _2];
140
141 colon_.name("Set of characters with no : or eol");
142 colon_ = lexeme[*(char_ - ":") >> omit[":"] ];
143
144 yes_eol_.name("Set of characters with eol");
145 yes_eol_ = lexeme[*(char_ - eol - ";") >> omit[-(char_(';') >> *(char_ - eol) )] >> eol];
146
147 no_eol_.name("Set of characters with no eol");
148 no_eol_ = lexeme[*(char_ - ";") >> omit[-(char_(';') >> *(char_ ) )]];
149
150// debug(root_rule_);
151// debug(line_);
152// debug(last_line_);
153// debug(yes_eol_);
154// debug(no_eol_);
155//
156// BOOST_SPIRIT_DEBUG_NODES((root_rule_)
157// (line_)(last_line_)(yes_eol_)(no_eol_) )
158
159
160
161 using phx::val;
162 using phx::construct;
163 using namespace qi::labels;
164 qi::on_error<qi::fail>
165 ( root_rule_ ,
166 std::cout<<
167 val("config/input split parser could not complete parsing. Expecting ")<<
168 _4<<
169 val(" here: ")<<
170 construct<std::string>(_3,_2)<<
171 std::endl
172 );
173 }
174
175 private:
176 qi::rule<Iterator, std::string(), ascii::space_type > root_rule_;
177 qi::rule<Iterator, ascii::space_type, std::string()> line_, last_line_, colon_, yes_eol_, no_eol_;
178
179 }; //end struct ConfigToIni
180
181 } // end parsing namespace
182 }// end settings namespace
183}// end bertini namespace
184
185
186
187
188
189
190#endif