Bertini 2 d882924 (2026-10-06 19:56 UTC)
Numerical algebraic geometry library (C++ core)
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) 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
25
26
27#ifndef BERTINI_CONFIG_INI_PARSE_HPP
28#define BERTINI_CONFIG_INI_PARSE_HPP
29
30
31#include <boost/fusion/adapted.hpp>
32#include <boost/fusion/include/adapted.hpp>
33
35#define BOOST_SPIRIT_USE_PHOENIX_V3 1
36
37
38#include <boost/spirit/include/qi_core.hpp>
39#include <boost/spirit/include/qi.hpp>
40#include <boost/phoenix/core.hpp>
41#include <boost/phoenix/operator.hpp>
42
43#include <boost/spirit/include/support_istream_iterator.hpp>
44
45#include <boost/algorithm/string.hpp>
46
47
48#include <iostream>
49#include <string>
50
51
52
53// this solution for *lazy* to_lower is adapted from the SO forum, user sehe.
54// https://stackoverflow.com/questions/21516201/how-to-create-boost-phoenix-make-shared
55// post found using google search terms `phoenix construct shared_ptr`
56namespace {
57 struct to_lower_f
58 {
59 template <typename... A> struct result
60 { typedef std::string type; };
61
62 template <typename A>
63 std::string operator()(A a) const {
64 return boost::algorithm::to_lower_copy(a);
65 }
66 };
67
68 using lazy_to_lower_ = boost::phoenix::function<to_lower_f >;
69}
70
71
72
73
74
75namespace bertini
76{
77 namespace settings
78 {
79 namespace parsing
80 {
103
104 template<typename Iterator, typename Skipper = ascii::space_type> //boost::spirit::unused_type
105 struct ConfigToIni : qi::grammar<Iterator, std::string(), Skipper>
106 {
107
108
109 ConfigToIni() : ConfigToIni::base_type(root_rule_, "ConfigToIni")
110 {
111 namespace phx = boost::phoenix;
112 using qi::_1;
113 using qi::_2;
114 using qi::_3;
115 using qi::_4;
116 using qi::_val;
117 using qi::eol;
118 using qi::eoi;
119 using qi::eps;
120 using qi::lit;
121 using qi::char_;
122 using qi::omit;
123 using boost::spirit::lexeme;
124 using boost::spirit::as_string;
125
126 root_rule_.name("ConfigToIni_root_rule");
127
128 root_rule_ = eps[_val = ""] >> *line_[_val = _val + _1+"\n"] >> -last_line_[_val = _val + _1];
129
130
131 line_.name("line_of_settings_input");
132 line_ = (eps >> colon_ >> yes_eol_)[_val = lazy_to_lower_()(_1) + "=" + _2];
133
134 last_line_.name("line_of_settings_input_with_no_eol");
135 last_line_ = (eps >> colon_ >> no_eol_)[_val = lazy_to_lower_()(_1) + "=" + _2];
136
137 colon_.name("Set of characters with no : or eol");
138 colon_ = lexeme[*(char_ - ":") >> omit[":"] ];
139
140 yes_eol_.name("Set of characters with eol");
141 yes_eol_ = lexeme[*(char_ - eol - ";") >> omit[-(char_(';') >> *(char_ - eol) )] >> eol];
142
143 no_eol_.name("Set of characters with no eol");
144 no_eol_ = lexeme[*(char_ - ";") >> omit[-(char_(';') >> *(char_ ) )]];
145
146// debug(root_rule_);
147// debug(line_);
148// debug(last_line_);
149// debug(yes_eol_);
150// debug(no_eol_);
151//
152// BOOST_SPIRIT_DEBUG_NODES((root_rule_)
153// (line_)(last_line_)(yes_eol_)(no_eol_) )
154
155
156
157 using phx::val;
158 using phx::construct;
159 using namespace qi::labels;
160 qi::on_error<qi::fail>
161 ( root_rule_ ,
162 std::cout<<
163 val("config/input split parser could not complete parsing. Expecting ")<<
164 _4<<
165 val(" here: ")<<
166 construct<std::string>(_3,_2)<<
167 std::endl
168 );
169 }
170
171 private:
172 qi::rule<Iterator, std::string(), ascii::space_type > root_rule_;
173 qi::rule<Iterator, ascii::space_type, std::string()> line_, last_line_, colon_, yes_eol_, no_eol_;
174
175 }; //end struct ConfigToIni
176
177 } // end parsing namespace
178 }// end settings namespace
179}// end bertini namespace
180
181
182
183
184
185
186#endif
Definition configIni_parse.hpp:80
Definition configIni_parse.hpp:78
Definition algorithm_builder.hpp:36
ConfigToIni()
Definition configIni_parse.hpp:109