ra4_draw  4bd0201e3d922d42bd545d4b045ed44db33454a4
Public Member Functions | Private Member Functions | Private Attributes | List of all members
FunctionParser Class Reference

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
 
FunctionParseroperator= (const FunctionParser &)=default
 
 FunctionParser (FunctionParser &&)=default
 
FunctionParseroperator= (FunctionParser &&)=default
 
 ~FunctionParser ()=default
 
const std::string & FunctionString () const
 Get string being parsed. More...
 
FunctionParserFunctionString (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< Tokentokens_
 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...
 

Detailed Description

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.

Constructor & Destructor Documentation

FunctionParser::FunctionParser ( )
default
FunctionParser::FunctionParser ( const std::string &  function_string)

Standard constructor from string representing a function.

Parameters
[in]function_stringString representing a number, variable, function, cut, etc.

Definition at line 41 of file function_parser.cpp.

References input_string_, and ReplaceAll().

FunctionParser::FunctionParser ( const FunctionParser )
default
FunctionParser::FunctionParser ( FunctionParser &&  )
default
FunctionParser::~FunctionParser ( )
default
FunctionParser::FunctionParser ( const std::vector< Token > &  tokens)
private

Constructs FunctionParser from list of Tokens.

Used by FunctionParser to recursively process lists of Tokens reprenting contents of parentheses or brackets

Parameters
[in]tokensList of Tokens. Should represent a single valid expression

Definition at line 101 of file function_parser.cpp.

References ConcatenateTokenStrings(), input_string_, and tokens_.

Member Function Documentation

void FunctionParser::AddAndSubtract ( ) const
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().

void FunctionParser::And ( ) const
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().

void FunctionParser::ApplySubscripts ( ) const
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().

void FunctionParser::ApplyUnary ( ) const
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().

void FunctionParser::CheckForUnknowns ( ) const
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().

void FunctionParser::CheckSolved ( ) const
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().

void FunctionParser::CleanupName ( ) const
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().

string FunctionParser::ConcatenateTokenStrings ( std::size_t  i_start,
std::size_t  i_end 
) const
private

Concatenates string representation of Tokens in range [i_start, i_end)

Parameters
[in]i_startPosition of starting Token (inclusive)
[in]i_endPosition of ending Token (exclusive)
Returns
Concatentation of string representations of Tokens in range [i_start, i_end)

Definition at line 628 of file function_parser.cpp.

References tokens_.

Referenced by ApplySubscripts(), FunctionParser(), and MergeParentheses().

void FunctionParser::CondenseTokens ( std::size_t  i_start,
std::size_t  i_end,
const Token replacement 
) const
private

Replace Tokens in range [i_start, i_end) with replacement.

Parameters
[in]i_startPosition of starting Token (inclusive)
[in]i_endPosition of ending Token (exclusive)
[in]replacementToken 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().

void FunctionParser::DisambiguatePlusMinus ( ) const
private
void FunctionParser::EqualOrNot ( ) const
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().

void FunctionParser::EvaluateGroupings ( ) const
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().

size_t FunctionParser::FindClose ( std::size_t  i_open_token) const
private

Find position of closing parenthesis/bracket corresponding to given opening partner.

Parameters
[in]i_open_tokenPosition in current list of Tokens of opening parenthesis/bracket
Returns
Position in current list of Tokens of closing 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.

Returns
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.

Parameters
[in]function_stringString to be parsed
Returns
Reference to *this

Definition at line 61 of file function_parser.cpp.

References input_string_, ReplaceAll(), solved_, and tokenized_.

void FunctionParser::LessGreater ( ) const
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().

void FunctionParser::MergeParentheses ( ) const
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().

void FunctionParser::MultiplyAndDivide ( ) const
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().

FunctionParser& FunctionParser::operator= ( const FunctionParser )
default
FunctionParser& FunctionParser::operator= ( FunctionParser &&  )
default
void FunctionParser::Or ( ) const
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().

void FunctionParser::ResolveVariables ( ) const
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().

void FunctionParser::Solve ( ) const
private
void FunctionParser::Tokenize ( ) const
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<<().

Member Data Documentation

std::string FunctionParser::input_string_
private

String being parsed.

Definition at line 31 of file function_parser.hpp.

Referenced by CheckForUnknowns(), CleanupName(), FunctionParser(), FunctionString(), ResolveAsNamedFunc(), and Tokenize().

bool FunctionParser::solved_
mutableprivate

String parsed into tokens, and all tokens succesfully merged.

Definition at line 34 of file function_parser.hpp.

Referenced by FunctionString(), and Solve().

bool FunctionParser::tokenized_
mutableprivate

String has been parsed into tokens.

Definition at line 33 of file function_parser.hpp.

Referenced by FunctionString(), and Tokenize().

std::vector<Token> FunctionParser::tokens_
mutableprivate

The documentation for this class was generated from the following files: