42 input_string_(function_string),
123 }
else if(start_char==
'<'){
127 }
else if(start_char==
'>'){
131 }
else if(start_char==
'!'){
135 }
else if(isalpha(start_char) || start_char ==
'_'){
143 }
else if(isdigit(start_char) || start_char ==
'.'){
146 double val = strtod(&from_start[0], &cp);
147 string remaining = cp;
148 if(val != 0. || remaining != from_start){
149 size_t length = from_start.size() - remaining.size();
150 if(length < 1) length = 1;
168 for(
const auto &token:
tokens_){
170 ERROR(
"Function string \""+
input_string_+
"\" contains unknown token \""+token.string_rep_+
"\".");
186 token.function_ =
NamedFunc(token.string_rep_,
198 for(
size_t i_open = 0; i_open <
tokens_.size(); ++i_open){
200 if(i_close <= i_open || i_close >=
tokens_.size())
continue;
215 for(
size_t i = 0; i+2 <
tokens_.size(); ++i){
229 merged_func.
Name(name);
230 Token merged(merged_func);
243 for(
size_t i = 0; i+3 <
tokens_.size(); ++i){
258 function<ScalarFunc>
function = [vec_func,sub_func](
const Baby &b){
259 return vec_func(b).at(sub_func(b));
272 for(
size_t i = 0; i <
tokens_.size(); ++i){
298 cur.
type_ = binary_type;
320 cur.
type_ = unary_type;
324 cur.
type_ = ambiguous_type;
335 for(
size_t i = 0; i+1 <
tokens_.size(); ++i){
362 for(
size_t i = 0; i+2 <
tokens_.size(); ++i){
392 for(
size_t i = 0; i+2 <
tokens_.size(); ++i){
420 for(
size_t i = 0; i+2 <
tokens_.size(); ++i){
451 for(
size_t i = 0; i+2 <
tokens_.size(); ++i){
478 for(
size_t i = 0; i+2 <
tokens_.size(); ++i){
503 for(
size_t i = 0; i+2 <
tokens_.size(); ++i){
527 DBG(
"No tokens found for " << (*
this) <<
".");
530 oss <<
"Could not condense " << (*this) <<
"." << flush;
535 oss <<
"Unknown token in " << (*this) <<
"." << flush;
586 const auto &open_token =
tokens_.at(i_open_token);
602 size_t i_close_token;
603 for(i_close_token = i_open_token+1; i_close_token <
tokens_.size() && open_count > 0; ++i_close_token){
604 const auto &token =
tokens_.at(i_close_token);
605 if(token.type_ == open_type){
607 }
else if(token.type_ == close_type){
612 return --i_close_token;
630 for(;i_start < i_end; ++i_start){
631 result +=
tokens_.at(i_start).string_rep_;
646 if(i_end < i_start)
return;
647 size_t num_replaced = i_end-i_start;
648 vector<Token> new_tokens(
tokens_.size()+1-num_replaced);
649 for(
size_t i = 0; i < i_start; ++i){
650 new_tokens.at(i) =
tokens_.at(i);
652 new_tokens.at(i_start) = replacement;
653 for(
size_t i = i_start + 1; i < new_tokens.size(); ++i){
654 new_tokens.at(i) =
tokens_.at(i+num_replaced-1);
669 const auto &tokens = fp.
Tokens();
670 for(
auto token = tokens.cbegin();
671 token != tokens.cend();
673 if(token != tokens.cbegin()) stream <<
", ";
std::vector< Token > tokens_
List of tokens generated in parsing process.
NamedFunc ResolveAsNamedFunc() const
Parses provided string into a single NamedFunc.
std::vector< ScalarType > VectorType
Token ResolveAsToken() const
Parses provided string into a single Token.
void ApplySubscripts() const
Merges subscript (bracket) Tokens with the contents.
void CleanupName() const
Restores string representation to initially provided string.
NamedFunc::VectorType VectorType
static NamedFunc GetFunction(const std::string &var_name)
Get a NamedFunc accessing specified variable.
const std::string & Name() const
Get the string representation of this function.
void Solve() const
Runs full parse from start to finish, caching result.
Abstract base class for access to ntuple variables.
void AddAndSubtract() const
Merges binary "+" and "-" Tokens with their operands.
void ReplaceAll(std::string &str, const std::string &orig, const std::string &rep)
ScalarType(const Baby &) ScalarFunc
void DisambiguatePlusMinus() const
Determines whether "+" and "-" Tokens correspond to unary or binary operators.
Combines a callable function taking a Baby and returning a scalar or vector with its string represent...
void ResolveVariables() const
Generates NamedFunc for each Token representing a constant or Baby variable.
void LessGreater() const
Merges "<", "<=", ">", and ">=" Tokens with their operands.
void ApplyUnary() const
Merges unary operator Tokens with their operands.
void EqualOrNot() const
Merges "==" and "!=" Tokens with their operands.
void EvaluateGroupings() const
Recursively evaluates contents of parenthesis and brackets.
ostream & operator<<(ostream &stream, const FunctionParser &fp)
Print FunctionParser to output stream.
static Type GetType(char x)
VectorType(const Baby &) VectorFunc
const std::vector< Token > & Tokens() const
Get list of tokens as is at current stage of parsing.
std::string input_string_
String being parsed.
void CheckSolved() const
Check that we have a single Token with a valid NamedFun.
NamedFunc::VectorFunc VectorFunc
void MultiplyAndDivide() const
Merges "*" and "/" Tokens with their operands.
std::size_t FindClose(std::size_t i_open_token) const
Find position of closing parenthesis/bracket corresponding to given opening partner.
void Or() const
Merges "||" Tokens with their operands.
void MergeParentheses() const
Merges parenthesis Tokens with the contents.
void And() const
Merges "&&" Tokens with their operands.
void CheckForUnknowns() const
Checks that all Tokens were understood.
bool solved_
String parsed into tokens, and all tokens succesfully merged.
bool tokenized_
String has been parsed into tokens.
const std::function< VectorFunc > & VectorFunction() const
Return the (possibly invalid) vector function.
void Tokenize() const
Parses the string into a list of Tokens.
NamedFunc::ScalarFunc ScalarFunc
std::string ConcatenateTokenStrings(std::size_t i_start, std::size_t i_end) const
Concatenates string representation of Tokens in range [i_start, i_end)
const std::string & FunctionString() const
Get string being parsed.
void CondenseTokens(std::size_t i_start, std::size_t i_end, const Token &replacement) const
Replace Tokens in range [i_start, i_end) with replacement.
Converts a string into a NamedFunc.
const std::function< ScalarFunc > & ScalarFunction() const
Return the (possibly invalid) scalar function.
NamedFunc::ScalarType ScalarType