9 namespace BooleanFunctionParser
58 static const std::map<ParserType, std::map<TokenType, unsigned>> parser2precedence = {
60 std::map<TokenType, unsigned>({
68 std::map<TokenType, unsigned>({
87 return this->
type == _type;
92 static const std::map<TokenType, std::string> type2str = {
106 os <<
"Token { " << type2str.at(token.
type);
110 os <<
", " << std::get<0>(token.
variable) <<
" (" << std::get<1>(token.
variable) <<
")";
116 for (
auto i = token.
constant.size(); i-- != 0;)
128 std::stringstream ss;
133 Token::Token(
TokenType _type, std::tuple<std::string, u16> _variable, std::vector<BooleanFunction::Value> _constant) :
type(_type), variable(_variable), constant(_constant)
139 std::stack<Token> operator_stack;
140 std::vector<Token>
output;
142 for (
auto&& token : tokens)
149 output.emplace_back(token);
161 output.emplace_back(operator_stack.top());
162 operator_stack.pop();
164 operator_stack.push(token);
181 operator_stack.push(token);
190 output.emplace_back(operator_stack.top());
191 operator_stack.pop();
194 operator_stack.pop();
201 while (!operator_stack.empty())
205 return ERR(
"could not translate '" + expression +
"' to reverse polish notation: bracket level is invalid");
208 output.emplace_back(operator_stack.top());
209 operator_stack.pop();
216 auto nodes = std::vector<BooleanFunction::Node>();
217 nodes.reserve(tokens.size());
219 for (
auto&& token : tokens)
223 return ERR(
"could not translate tokens: tokens are imbalanced, i.e., the operation comes before the operand nodes");
249 return ERR(
"could not translate tokens: unable to handle '" + token.to_string() +
"' in '" + expression +
"'");
255 return ERR_APPEND(res.get_error(),
"could not translate tokens: unable to build Boolean function from vector of nodes");
static Result< BooleanFunction > build(std::vector< Node > &&nodes)
#define ERR_APPEND(prev_error, message)
ParserType
ParserType refers to the parser identifier.
@ Liberty
refers to the 'Liberty' Boolean function parser
@ Standard
refers to the 'Standard' Boolean function parser
std::ostream & operator<<(std::ostream &os, const Token &token)
Result< BooleanFunction > translate(std::vector< Token > &&tokens, const std::string &expression)
TokenType
TokenType refers to a token identifier for a Boolean function string.
Result< std::vector< Token > > reverse_polish_notation(std::vector< Token > &&tokens, const std::string &expression, const ParserType &parser)
static Node Constant(const std::vector< BooleanFunction::Value > value)
static Node Operation(u16 type, u16 size)
static Node Variable(const std::string variable, u16 size)
Token refers to a token identifier and accompanied data.
static Token BracketOpen()
std::tuple< std::string, u16 > variable
optional value and bit-size in case token is a variable
static Token BracketClose()
std::string to_string() const
TokenType type
refers to the underlying token type identifier
static Token Constant(std::vector< BooleanFunction::Value > value)
Token(TokenType _type, std::tuple< std::string, u16 > variable, std::vector< BooleanFunction::Value > constant)
Construct a new initialized Token.
bool is(TokenType type) const
static Token Variable(std::string name, u16 size)
std::vector< BooleanFunction::Value > constant
optional value in case token is a constant
unsigned precedence(const ParserType &type) const