50 using Var = std::shared_ptr<Variable>;
51 using Nd = std::shared_ptr<Node>;
77 template<
typename Iterator,
typename Skipper = ascii::space_type>
84 function_parser_(&encountered_symbols_)
86 namespace phx = boost::phoenix;
94 using boost::spirit::lexeme;
97 declarative_symbols_.add(
"variable_group",0);
98 declarative_symbols_.add(
"hom_variable_group",1);
99 declarative_symbols_.add(
"variable",2);
100 declarative_symbols_.add(
"function",3);
101 declarative_symbols_.add(
"constant",4);
102 declarative_symbols_.add(
"parameter",5);
103 declarative_symbols_.add(
"implicit_parameter",6);
104 declarative_symbols_.add(
"pathvariable",7);
105 declarative_symbols_.add(
"random",8);
106 declarative_symbols_.add(
"random_real",9);
113 special_numbers_.add(
"pi",
node::Pi());
114 special_numbers_.add(
"Pi", special_numbers_.at(
"pi"));
116 special_numbers_.add(
"e",
node::E());
117 special_numbers_.add(
"E", special_numbers_.at(
"e"));
120 special_numbers_.add(
"i",
node::I());
121 special_numbers_.add(
"I", special_numbers_.at(
"i"));
125 encountered_symbols_.add(
"pi", special_numbers_.at(
"pi"));
126 encountered_symbols_.add(
"Pi", special_numbers_.at(
"pi"));
128 encountered_symbols_.add(
"e", special_numbers_.at(
"e"));
129 encountered_symbols_.add(
"E", special_numbers_.at(
"e"));
132 encountered_symbols_.add(
"i", special_numbers_.at(
"i"));
133 encountered_symbols_.add(
"I", special_numbers_.at(
"i"));
146 root_rule_.name(
"system_parsing");
155 functions_ [phx::bind([
this](std::vector<std::string>
const& names){ this->CollectFunctionNames(names); }, _1)]
157 constants_ [phx::bind([
this](std::vector<std::string>
const& names){ this->CollectConstantNames(names); }, _1)]
159 parameters_ [phx::bind([
this](std::vector<std::string>
const& names){ this->CollectParameterNames(names); }, _1)]
163 path_variable_ [phx::bind([
this](
System& s,
Var const& v){ this->AddPathVariableChecked(s, v); }, _val, _1)]
171 qi::omit[subfunction_]
179 variables_.name(
"variables_"); hom_variable_group_.name(
"hom_variable_group_"); variable_group_.name(
"variable_group_"); implicit_parameters_.name(
"implicit_parameters_");
182 variables_ =
"variable" > genericvargp_ >
';';
183 hom_variable_group_ =
"hom_variable_group" > genericvargp_ >
';';
184 variable_group_ =
"variable_group" > genericvargp_ >
';';
185 implicit_parameters_ =
"implicit_parameter" > genericvargp_ >
';';
189 path_variable_.name(
"path_variable_");
190 path_variable_ =
"pathvariable" > new_variable_ >
';';
194 genericvargp_.name(
"genericvargp_");
195 genericvargp_ = new_variable_ %
',';
197 new_variable_.name(
"new_variable_");
198 new_variable_ = unencountered_symbol_ [boost::phoenix::bind( [
this](
Var & V, std::string str)
200 MakeAndAddVariable(V,str);
205 functions_.name(
"functions_"); constants_.name(
"constants_"); parameters_.name(
"parameters_");
207 functions_ =
"function" > genericfuncgp_ >
';';
208 constants_ =
"constant" > genericfuncgp_ >
';';
209 parameters_ =
"parameter" > genericfuncgp_ >
';';
212 genericfuncgp_.name(
"genericfuncgp_");
213 genericfuncgp_ = new_function_ %
',';
216 new_function_.name(
"new_function_");
217 new_function_ = unencountered_symbol_ [boost::phoenix::bind( [
this](std::string & name, std::string str)
219 MakeAndDeclareName(name,str);
226 unencountered_symbol_.name(
"unencountered_symbol_");
227 unencountered_symbol_ = valid_variable_name_ - lexeme[( declarative_symbols_ | encountered_symbols_ )];
237 valid_variable_name_.name(
"valid_variable_name_");
238 valid_variable_name_ = utf8_identifier_parser();
243 definition_.name(
"definition_");
244 definition_ = (encountered_functions_ >
'=' > function_parser_ >
';') [phx::bind( [
this](std::string
const& name,
const Nd & expr)
252 subfunction_.name(
"subfunction");
253 subfunction_ = unencountered_symbol_ [_a = _1] >
'=' >
254 function_parser_ [phx::bind( [
this](
Nd & result, std::string
const& name,
const Nd & expr)
257 encountered_symbols_.add(name, ne);
301 qi::on_error<qi::fail>(
315 qi::rule<Iterator,
System(), Skipper > root_rule_;
318 qi::rule<Iterator,
VariableGroup(), Skipper > variable_group_, hom_variable_group_, variables_, implicit_parameters_;
321 qi::rule<Iterator,
Var(), Skipper> path_variable_;
322 qi::rule<Iterator,
Var()> new_variable_;
327 qi::rule<Iterator, std::vector<std::string>(), Skipper > functions_, constants_, parameters_;
328 qi::rule<Iterator, std::vector<std::string>(), Skipper > genericfuncgp_;
329 qi::rule<Iterator,
Nd(), Skipper, qi::locals<std::string> > subfunction_;
331 qi::rule<Iterator, std::string()> new_function_;
334 qi::rule<Iterator, std::string()> unencountered_symbol_;
338 qi::rule<Iterator, std::string()> valid_variable_name_;
340 qi::rule<Iterator, Skipper, qi::unused_type> definition_;
343 qi::symbols<char,Nd> encountered_symbols_;
344 qi::symbols<char,int> declarative_symbols_;
345 qi::symbols<char,std::string> encountered_functions_;
346 qi::symbols<char,Nd> special_numbers_;
354 void MakeAndDeclareName(std::string & name, std::string str)
363 encountered_functions_.add(str, str);
370 void MakeAndAddVariable(
Var & V, std::string str)
373 encountered_symbols_.add(str, V);
385 {
for (
auto const& n : names) declared_function_names_.push_back(n); }
388 {
for (
auto const& n : names) declared_constant_names_.push_back(n); }
391 {
for (
auto const& n : names) declared_parameter_names_.push_back(n); }
399 encountered_symbols_.add(name, ne);
400 definitions_[name] = ne;
413 if (have_path_variable_declaration_)
414 throw std::runtime_error(
415 "[SystemParser] more than one `pathvariable` declared: a system has "
416 "exactly one path variable. Declare only the one you mean.");
417 have_path_variable_declaration_ =
true;
430 for (
auto const& n : declared_function_names_)
431 s.
AddFunction(DefinitionOf(n,
"function")->EntryNode());
432 for (
auto const& n : declared_constant_names_)
434 for (
auto const& n : declared_parameter_names_)
443 std::shared_ptr<node::NamedExpression>
const& DefinitionOf(std::string
const& name,
444 std::string
const& kind)
const
446 auto const found = definitions_.find(name);
447 if (found == definitions_.end())
448 throw std::runtime_error(
449 "[SystemParser] " + kind +
" `" + name +
"` was declared but never defined. "
450 "Every declared name needs a defining line, as in `" + name +
" = ...;`.");
451 return found->second;
454 bool have_path_variable_declaration_ =
false;
455 std::vector<std::string> declared_function_names_, declared_constant_names_, declared_parameter_names_;
456 std::map<std::string, std::shared_ptr<node::NamedExpression>> definitions_;
The fundamental polynomial system class for Bertini2.
Definition system.hpp:106
void AddParameter(NE const &F)
Add an explicit parameter to the system.
void AddImplicitParameters(VariableGroup const &v)
Add some implicit parameters to the system.
void AddUngroupedVariables(VariableGroup const &v)
Add variables to the system which are in neither a regular variable group, nor in a homogeneous group...
void AddFunction(Nd const &F)
Add a function to the system, as a bare expression.
void AddConstant(NE const &C)
Add a constant function to the system.
void AddHomVariableGroup(VariableGroup const &v)
Add a homogeneous (projective) variable group to the system.
void AddPathVariable(Var const &v)
Add a variable as the Path Variable to a System.
void AddVariableGroup(VariableGroup const &v)
Add a variable group to the system.
static std::shared_ptr< NamedExpression > Make(Ts &&...ts)
Construct (and intern) a NamedExpression node.
Definition named_expression.hpp:57
An interface for all nodes in a function tree, and for a function object as well.
Definition node.hpp:114
Represents variable leaves in the function tree.
Definition variable.hpp:48
static std::shared_ptr< Variable > Make(Ts &&...ts)
Construct (and intern) a Variable node.
Definition variable.hpp:56
Provides the parsing rules for functions in bertini2.
std::shared_ptr< Node > E()
Construct a shared pointer to .
std::shared_ptr< Node > Pi()
Construct a shared pointer to .
std::shared_ptr< Node > I()
Construct a shared pointer to .
Definition classic_utilities.hpp:41
void ReportParseError(std::string::const_iterator begin, std::string::const_iterator end, std::string::const_iterator err_pos, boost::spirit::info const &what, std::string const &parser_name)
Report a parse error (via FormatParseError) to the log, for use in a Spirit on_error handler.
Definition qi_files.hpp:199
std::shared_ptr< Variable > Var
Shorthand for a shared pointer to a variable node.
Definition system_rules.hpp:50
node::Node Node
The generic expression-tree node type.
Definition system_rules.hpp:47
std::shared_ptr< Node > Nd
Shorthand for a shared pointer to a generic node.
Definition system_rules.hpp:51
node::Variable Variable
The variable node type.
Definition system_rules.hpp:46
Definition classic_utilities.hpp:40
Definition algorithm_builder.hpp:36
std::vector< std::shared_ptr< node::Variable > > VariableGroup
An ordered group of variables (the unit of homogenization / variable grouping).
Definition node.hpp:69
Provides all the include files needed to develop a parsing file that uses boost qi.
A Qi grammar parser for parsing text into function trees.
Definition function_rules.hpp:145
void EmitDeclaredFunctions(System &s) const
Emit the collected declared functions/constants/parameters to the System.
Definition system_rules.hpp:428
void CollectParameterNames(std::vector< std::string > const &names)
Record the declared parameter names.
Definition system_rules.hpp:390
void DefineNamed(std::string const &name, Nd const &expr)
Bind a declared name to its (immutable) NamedExpression definition.
Definition system_rules.hpp:396
void CollectFunctionNames(std::vector< std::string > const &names)
Record the declared function names (in declaration order).
Definition system_rules.hpp:384
SystemParser()
Definition system_rules.hpp:82
void CollectConstantNames(std::vector< std::string > const &names)
Record the declared constant names.
Definition system_rules.hpp:387
void AddPathVariableChecked(System &s, Var const &v)
Add the path variable, refusing a second declaration.
Definition system_rules.hpp:411
Provides the bertini::System class.