ra4_draw
4bd0201e3d922d42bd545d4b045ed44db33454a4
|
Converts a string into a NamedFunc. More...
#include <function_parser.hpp>
Public Member Functions | |
FunctionParser ()=default | |
FunctionParser (const std::string &function_string) | |
Standard constructor from string representing a function. More... | |
FunctionParser (const FunctionParser &)=default | |
FunctionParser & | operator= (const FunctionParser &)=default |
FunctionParser (FunctionParser &&)=default | |
FunctionParser & | operator= (FunctionParser &&)=default |
~FunctionParser ()=default | |
const std::string & | FunctionString () const |
Get string being parsed. More... | |
FunctionParser & | FunctionString (const std::string &function_string) |
Set string to be parsed. More... | |
const std::vector< Token > & | Tokens () const |
Get list of tokens as is at current stage of parsing. More... | |
Token | ResolveAsToken () const |
Parses provided string into a single Token. More... | |
NamedFunc | ResolveAsNamedFunc () const |
Parses provided string into a single NamedFunc. More... | |
Private Member Functions | |
FunctionParser (const std::vector< Token > &tokens) | |
Constructs FunctionParser from list of Tokens. More... | |
void | Tokenize () const |
Parses the string into a list of Tokens. More... | |
void | CheckForUnknowns () const |
Checks that all Tokens were understood. More... | |
void | ResolveVariables () const |
Generates NamedFunc for each Token representing a constant or Baby variable. More... | |
void | EvaluateGroupings () const |
Recursively evaluates contents of parenthesis and brackets. More... | |
void | MergeParentheses () const |
Merges parenthesis Tokens with the contents. More... | |
void | ApplySubscripts () const |
Merges subscript (bracket) Tokens with the contents. More... | |
void | DisambiguatePlusMinus () const |
Determines whether "+" and "-" Tokens correspond to unary or binary operators. More... | |
void | ApplyUnary () const |
Merges unary operator Tokens with their operands. More... | |
void | MultiplyAndDivide () const |
Merges "*" and "/" Tokens with their operands. More... | |
void | AddAndSubtract () const |
Merges binary "+" and "-" Tokens with their operands. More... | |
void | LessGreater () const |
Merges "<", "<=", ">", and ">=" Tokens with their operands. More... | |
void | EqualOrNot () const |
Merges "==" and "!=" Tokens with their operands. More... | |
void | And () const |
Merges "&&" Tokens with their operands. More... | |
void | Or () const |
Merges "||" Tokens with their operands. More... | |
void | CheckSolved () const |
Check that we have a single Token with a valid NamedFun. More... | |
void | CleanupName () const |
Restores string representation to initially provided string. More... | |
void | Solve () const |
Runs full parse from start to finish, caching result. More... | |
std::size_t | FindClose (std::size_t i_open_token) const |
Find position of closing parenthesis/bracket corresponding to given opening partner. More... | |
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) More... | |
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. More... | |
Private Attributes | |
std::string | input_string_ |
String being parsed. More... | |
std::vector< Token > | tokens_ |
List of tokens generated in parsing process. More... | |
bool | tokenized_ |
String has been parsed into tokens. More... | |
bool | solved_ |
String parsed into tokens, and all tokens succesfully merged. More... | |
Converts a string into a NamedFunc.
A FunctionParser is initialized with a string representing a number, variable, function, cut, etc. and converts it to a NamedFunc. It first decomposes the string into components representing single numbers, variables, operators, etc. The components are stored as Tokens. The Tokens representing constants and variables in Baby are processed to obtain valid NamedFuncs. Operators are successively applied following standard order of operations to merge the Tokens into a single Token instance containing a NamedFunc which can return the value represented by the initial string.
Parentheses and brackets are parsed recursively and can be arbitrarily nested.
Currently has support for the basic arithmetic, logical, and comparison operators. Future versions may support ROOT's function syntax, e.g. Sum$(jets_pt).
Definition at line 12 of file function_parser.hpp.
|
default |
FunctionParser::FunctionParser | ( | const std::string & | function_string | ) |
Standard constructor from string representing a function.
[in] | function_string | String representing a number, variable, function, cut, etc. |
Definition at line 41 of file function_parser.cpp.
References input_string_, and ReplaceAll().
|
default |
|
default |
|
default |
|
private |
Constructs FunctionParser from list of Tokens.
Used by FunctionParser to recursively process lists of Tokens reprenting contents of parentheses or brackets
[in] | tokens | List of Tokens. Should represent a single valid expression |
Definition at line 101 of file function_parser.cpp.
References ConcatenateTokenStrings(), input_string_, and tokens_.
|
private |
Merges binary "+" and "-" Tokens with their operands.
Searches for patten {value}{"+" or "-"}{value} and replaces with single Token
Definition at line 391 of file function_parser.cpp.
References Token::binary_minus, Token::binary_plus, CondenseTokens(), Token::function_, Token::resolved_scalar, Token::resolved_vector, tokens_, and Token::type_.
Referenced by Solve().
|
private |
Merges "&&" Tokens with their operands.
Searches for patten {value}{&&}{value} and replaces with single Token
Definition at line 477 of file function_parser.cpp.
References CondenseTokens(), Token::function_, Token::logical_and, Token::resolved_scalar, Token::resolved_vector, tokens_, and Token::type_.
Referenced by Solve().
|
private |
Merges subscript (bracket) Tokens with the contents.
Searches for patten {open bracket}{value}{close bracket} and replaces with single Token
Definition at line 242 of file function_parser.cpp.
References Token::close_square, ConcatenateTokenStrings(), CondenseTokens(), Token::function_, Token::open_square, Token::resolved_scalar, Token::resolved_vector, NamedFunc::ScalarFunction(), tokens_, Token::type_, and NamedFunc::VectorFunction().
Referenced by Solve().
|
private |
Merges unary operator Tokens with their operands.
Searches for patten {unary operator}{value} and replaces with single Token
Definition at line 334 of file function_parser.cpp.
References CondenseTokens(), Token::function_, Token::logical_not, Token::resolved_scalar, Token::resolved_vector, tokens_, Token::type_, Token::unary_minus, and Token::unary_plus.
Referenced by Solve().
|
private |
Checks that all Tokens were understood.
Definition at line 167 of file function_parser.cpp.
References ERROR, input_string_, tokens_, and Token::unknown.
Referenced by Solve().
|
private |
Check that we have a single Token with a valid NamedFun.
Definition at line 525 of file function_parser.cpp.
References DBG, ERROR, Token::resolved_scalar, Token::resolved_vector, and tokens_.
Referenced by Solve().
|
private |
Restores string representation to initially provided string.
Token merging process often generates unnecessary parentheses in string, so just use the original string
Definition at line 545 of file function_parser.cpp.
References input_string_, and tokens_.
Referenced by Solve().
|
private |
Concatenates string representation of Tokens in range [i_start, i_end)
[in] | i_start | Position of starting Token (inclusive) |
[in] | i_end | Position of ending Token (exclusive) |
Definition at line 628 of file function_parser.cpp.
References tokens_.
Referenced by ApplySubscripts(), FunctionParser(), and MergeParentheses().
|
private |
Replace Tokens in range [i_start, i_end) with replacement.
[in] | i_start | Position of starting Token (inclusive) |
[in] | i_end | Position of ending Token (exclusive) |
[in] | replacement | Token which replaces the range |
Definition at line 645 of file function_parser.cpp.
References tokens_.
Referenced by AddAndSubtract(), And(), ApplySubscripts(), ApplyUnary(), EqualOrNot(), EvaluateGroupings(), LessGreater(), MergeParentheses(), MultiplyAndDivide(), and Or().
|
private |
Determines whether "+" and "-" Tokens correspond to unary or binary operators.
Definition at line 271 of file function_parser.cpp.
References Token::ambiguous_minus, Token::ambiguous_plus, Token::binary_minus, Token::binary_plus, Token::close_paren, Token::close_square, Token::divide, Token::equal, Token::greater, Token::greater_equal, Token::less, Token::less_equal, Token::logical_and, Token::logical_not, Token::logical_or, Token::modulus, Token::multiply, Token::not_equal, Token::number, Token::open_paren, Token::open_square, Token::resolved_scalar, Token::resolved_vector, tokens_, Token::type_, Token::unary_minus, Token::unary_plus, Token::unknown, and Token::variable_name.
Referenced by Solve().
|
private |
Merges "==" and "!=" Tokens with their operands.
Searches for patten {value}{comparison}{value} and replaces with single Token
Definition at line 450 of file function_parser.cpp.
References CondenseTokens(), Token::equal, Token::function_, Token::not_equal, Token::resolved_scalar, Token::resolved_vector, tokens_, and Token::type_.
Referenced by Solve().
|
private |
Recursively evaluates contents of parenthesis and brackets.
Definition at line 197 of file function_parser.cpp.
References CondenseTokens(), FindClose(), ResolveAsToken(), and tokens_.
Referenced by Solve().
|
private |
Find position of closing parenthesis/bracket corresponding to given opening partner.
[in] | i_open_token | Position in current list of Tokens of opening parenthesis/bracket |
Definition at line 583 of file function_parser.cpp.
References Token::close_paren, Token::close_square, Token::open_paren, Token::open_square, and tokens_.
Referenced by EvaluateGroupings().
const string & FunctionParser::FunctionString | ( | ) | const |
Get string being parsed.
Definition at line 53 of file function_parser.cpp.
References input_string_.
Referenced by operator<<().
FunctionParser & FunctionParser::FunctionString | ( | const std::string & | function_string | ) |
Set string to be parsed.
[in] | function_string | String to be parsed |
Definition at line 61 of file function_parser.cpp.
References input_string_, ReplaceAll(), solved_, and tokenized_.
|
private |
Merges "<", "<=", ">", and ">=" Tokens with their operands.
Searches for patten {value}{comparison}{value} and replaces with single Token
Definition at line 419 of file function_parser.cpp.
References CondenseTokens(), Token::function_, Token::greater, Token::greater_equal, Token::less, Token::less_equal, Token::resolved_scalar, Token::resolved_vector, tokens_, and Token::type_.
Referenced by Solve().
|
private |
Merges parenthesis Tokens with the contents.
Searches for patten {open paren}{value}{close paren} and replaces with single Token
Definition at line 214 of file function_parser.cpp.
References Token::close_paren, ConcatenateTokenStrings(), CondenseTokens(), Token::function_, NamedFunc::Name(), Token::open_paren, Token::resolved_scalar, Token::resolved_vector, tokens_, and Token::type_.
Referenced by Solve().
|
private |
Merges "*" and "/" Tokens with their operands.
Searches for patten {value}{"*" or "/"}{value} and replaces with single Token
Definition at line 361 of file function_parser.cpp.
References CondenseTokens(), Token::divide, Token::function_, Token::modulus, Token::multiply, Token::resolved_scalar, Token::resolved_vector, tokens_, and Token::type_.
Referenced by Solve().
|
default |
|
default |
|
private |
Merges "||" Tokens with their operands.
Searches for patten {value}{||}{value} and replaces with single Token
Definition at line 502 of file function_parser.cpp.
References CondenseTokens(), Token::function_, Token::logical_or, Token::resolved_scalar, Token::resolved_vector, tokens_, and Token::type_.
Referenced by Solve().
NamedFunc FunctionParser::ResolveAsNamedFunc | ( | ) | const |
Parses provided string into a single NamedFunc.
Definition at line 84 of file function_parser.cpp.
References input_string_, Solve(), and tokens_.
Token FunctionParser::ResolveAsToken | ( | ) | const |
Parses provided string into a single Token.
Definition at line 77 of file function_parser.cpp.
References Solve(), and tokens_.
Referenced by EvaluateGroupings().
|
private |
Generates NamedFunc for each Token representing a constant or Baby variable.
Definition at line 178 of file function_parser.cpp.
References Baby::GetFunction(), Token::number, Token::resolved_scalar, Token::resolved_vector, tokens_, and Token::variable_name.
Referenced by Solve().
|
private |
Runs full parse from start to finish, caching result.
Definition at line 554 of file function_parser.cpp.
References AddAndSubtract(), And(), ApplySubscripts(), ApplyUnary(), CheckForUnknowns(), CheckSolved(), CleanupName(), DisambiguatePlusMinus(), EqualOrNot(), EvaluateGroupings(), LessGreater(), MergeParentheses(), MultiplyAndDivide(), Or(), ResolveVariables(), solved_, and Tokenize().
Referenced by ResolveAsNamedFunc(), and ResolveAsToken().
|
private |
Parses the string into a list of Tokens.
Definition at line 111 of file function_parser.cpp.
References Token::GetType(), Token::greater, input_string_, Token::less, Token::logical_not, Token::number, tokenized_, tokens_, Token::unknown, and Token::variable_name.
Referenced by Solve().
const vector< Token > & FunctionParser::Tokens | ( | ) | const |
Get list of tokens as is at current stage of parsing.
Definition at line 71 of file function_parser.cpp.
References tokens_.
Referenced by operator<<().
|
private |
String being parsed.
Definition at line 31 of file function_parser.hpp.
Referenced by CheckForUnknowns(), CleanupName(), FunctionParser(), FunctionString(), ResolveAsNamedFunc(), and Tokenize().
|
mutableprivate |
String parsed into tokens, and all tokens succesfully merged.
Definition at line 34 of file function_parser.hpp.
Referenced by FunctionString(), and Solve().
|
mutableprivate |
String has been parsed into tokens.
Definition at line 33 of file function_parser.hpp.
Referenced by FunctionString(), and Tokenize().
|
mutableprivate |
List of tokens generated in parsing process.
Definition at line 32 of file function_parser.hpp.
Referenced by AddAndSubtract(), And(), ApplySubscripts(), ApplyUnary(), CheckForUnknowns(), CheckSolved(), CleanupName(), ConcatenateTokenStrings(), CondenseTokens(), DisambiguatePlusMinus(), EqualOrNot(), EvaluateGroupings(), FindClose(), FunctionParser(), LessGreater(), MergeParentheses(), MultiplyAndDivide(), Or(), ResolveAsNamedFunc(), ResolveAsToken(), ResolveVariables(), Tokenize(), and Tokens().