No Matches
Go to the documentation of this file.
1// Mantid Repository :
3// Copyright © 2009 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
9#include "MantidAPI/DllConfig.h"
11#ifndef Q_MOC_RUN
12#include <memory>
15#include <map>
16#include <stdexcept>
17#include <string>
18#include <unordered_set>
19#include <vector>
21namespace Mantid {
22namespace API {
36class MANTID_API_DLL Expression {
39 class ParsingError : public std::runtime_error {
40 public:
41 ParsingError(const std::string &msg, const std::string &expr, size_t i);
42 ParsingError(const std::string &msg);
43 };
46 Expression();
48 Expression(const std::vector<std::string> &ops);
50 Expression(const std::vector<std::string> &binary, const std::unordered_set<std::string> &unary);
52 Expression(const Expression &expr);
59 void parse(const std::string &str);
64 void logPrint(const std::string &pads = "") const;
67 std::string str() const;
69 bool isFunct() const { return !m_terms.empty(); }
71 std::string name() const { return m_funct; }
74 std::string operator_name() const { return m_op; }
77 const std::vector<Expression> &terms() const { return m_terms; }
79 size_t size() const { return m_terms.size(); }
81 using iterator = std::vector<Expression>::const_iterator;
84 iterator begin() const { return m_terms.begin(); }
86 iterator end() const { return m_terms.end(); }
92 const Expression &operator[](size_t i) const { return; }
96 const Expression &bracketsRemoved() const;
98 std::unordered_set<std::string> getVariables() const;
104 void renameAll(const std::string &oldName, const std::string &newName);
109 void rename(const std::string &newName);
117 void toList(const std::string &sep = ",");
119 static const std::vector<std::string> DEFAULT_OPS_STR;
123 Expression(const Expression *pexpr);
131 struct Token {
142 Token(size_t i, size_t j, size_t k, size_t p) : is(i), ie(j), is1(k), prec(p) {}
143 size_t is;
144 size_t ie;
145 size_t is1;
146 size_t prec;
147 };
149 using Tokens = std::vector<Token>;
151 std::string GetToken(size_t i);
153 std::string GetOp(size_t i);
156 void tokenize();
162 void setFunct(const std::string &name);
167 static void trim(std::string &str);
170 std::string m_expr;
172 std::string m_funct;
173 std::string m_op;
174 std::vector<Expression> m_terms;
179 struct Operators {
180 std::vector<std::string> binary;
181 std::unordered_set<std::string> unary;
182 std::map<std::string, size_t> precedence;
183 std::unordered_set<char> symbols;
184 std::map<std::string, char> op_number;
185 };
192 size_t op_prec(const std::string &op) const;
197 void add_operators(const std::vector<std::string> &ops);
202 void add_unary(const std::unordered_set<std::string> &ops);
208 bool is_unary(const std::string &op) const;
214 bool is_op_symbol(const char c) const;
215 std::shared_ptr<Operators> m_operators;
218} // namespace API
219} // namespace Mantid
Specialised exception for parsing errors.
Definition: Expression.h:39
This class represents an expression made up of names, binary operators and brackets.
Definition: Expression.h:36
std::string name() const
Returns the name of the expression which is a function or variable name.
Definition: Expression.h:71
std::string m_expr
Saved expression string.
Definition: Expression.h:170
const std::vector< Expression > & terms() const
Returns the top level terms of the expression (function arguments).
Definition: Expression.h:77
std::shared_ptr< Operators > m_operators
pointer ot the operators
Definition: Expression.h:215
std::string operator_name() const
Returns the the expression's binary operator on its left.
Definition: Expression.h:74
std::string m_op
Operator connecting this expression to its sibling on the left.
Definition: Expression.h:173
std::string m_funct
Function name.
Definition: Expression.h:172
iterator end() const
An iterator pointing to the end of the expressions.
Definition: Expression.h:86
Expression & operator=(const Expression &expr)
Assignment operator.
bool isFunct() const
Returns true if the expression is a function (i.e. has arguments)
Definition: Expression.h:69
const Expression & operator[](size_t i) const
Gets the Expression at the specified index.
Definition: Expression.h:92
std::vector< Token > Tokens
The container type.
Definition: Expression.h:149
iterator begin() const
An iterator pointing to the start of the expressions.
Definition: Expression.h:84
std::vector< Expression >::const_iterator iterator
Const Iterator tpyedef.
Definition: Expression.h:81
Tokens m_tokens
The container for the token markers.
Definition: Expression.h:169
size_t size() const
Returns the number of argumens.
Definition: Expression.h:79
std::vector< Expression > m_terms
Child expressions (function arguments)
Definition: Expression.h:174
static const std::vector< std::string > DEFAULT_OPS_STR
Definition: Expression.h:119
Helper class which provides the Collimation Length for SANS instruments.
Keeps operator that can be used in an expression.
Definition: Expression.h:179
std::map< std::string, size_t > precedence
Map of the operator precedence order.
Definition: Expression.h:182
std::vector< std::string > binary
Binary operators in reverse precedence order.
Definition: Expression.h:180
std::unordered_set< std::string > unary
Unary operators.
Definition: Expression.h:181
std::unordered_set< char > symbols
All the symbols that are used in the binary operators.
Definition: Expression.h:183
std::map< std::string, char > op_number
map of operators
Definition: Expression.h:184
This is a struct to mark a token in a string expression.
Definition: Expression.h:131
size_t prec
The precedence of the connecting operator.
Definition: Expression.h:146
size_t is1
The index of the first symbol of the next token.
Definition: Expression.h:145
size_t is
The index of the first symbol of the token.
Definition: Expression.h:143
size_t ie
The index of the last symbol of the token.
Definition: Expression.h:144
Token(size_t i, size_t j, size_t k, size_t p)
Definition: Expression.h:142