HAL
parser.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 
29 
30 namespace hal
31 {
32  namespace BooleanFunctionParser
33  {
35  enum class ParserType
36  {
38  Liberty,
40  Standard,
43  };
44 
46  enum class TokenType
47  {
48 
49  And,
50  Not,
51  NotSuffix,
52  Or,
53  Xor,
55  Variable,
56  Constant,
58  BracketOpen,
59  BracketClose,
60  };
61 
63  struct Token
64  {
66  // Member
68 
72  std::tuple<std::string, u16> variable{};
74  std::vector<BooleanFunction::Value> constant{};
75 
77  // Constructors, Destructors, Operators
79 
88  Token(TokenType _type, std::tuple<std::string, u16> variable, std::vector<BooleanFunction::Value> constant);
89 
95  static Token And();
96 
102  static Token Not();
103 
109  static Token NotSuffix();
110 
116  static Token Or();
117 
123  static Token Xor();
124 
132  static Token Variable(std::string name, u16 size);
133 
140  static Token Constant(std::vector<BooleanFunction::Value> value);
141 
147  static Token BracketOpen();
148 
154  static Token BracketClose();
155 
157  // Interface
159 
166  unsigned precedence(const ParserType& type) const;
167 
174  bool is(TokenType type) const;
175 
183  friend std::ostream& operator<<(std::ostream& os, const Token& token);
184 
190  std::string to_string() const;
191  };
192 
199  Result<std::vector<Token>> parse_with_standard_grammar(const std::string& expression);
200 
208  Result<std::vector<Token>> parse_with_liberty_grammar(const std::string& expression);
209 
224  Result<std::vector<Token>> reverse_polish_notation(std::vector<Token>&& tokens, const std::string& expression, const ParserType& parser);
225 
234  Result<BooleanFunction> translate(std::vector<Token>&& tokens, const std::string& expression);
235 
236  } // namespace BooleanFunctionParser
237 } // namespace hal
uint16_t u16
Definition: defines.h:40
parser
Definition: control.py:13
Result< std::vector< Token > > parse_with_standard_grammar(const std::string &expression)
Result< std::vector< Token > > parse_with_liberty_grammar(const std::string &expression)
ParserType
ParserType refers to the parser identifier.
Definition: parser.h:36
@ Liberty
refers to the 'Liberty' Boolean function parser
@ LibertyNoSpace
as 'Liberty' above, but removing space first
@ Standard
refers to the 'Standard' Boolean function parser
Result< BooleanFunction > translate(std::vector< Token > &&tokens, const std::string &expression)
TokenType
TokenType refers to a token identifier for a Boolean function string.
Definition: parser.h:47
Result< std::vector< Token > > reverse_polish_notation(std::vector< Token > &&tokens, const std::string &expression, const ParserType &parser)
std::string name
Token refers to a token identifier and accompanied data.
Definition: parser.h:64
std::tuple< std::string, u16 > variable
optional value and bit-size in case token is a variable
Definition: parser.h:72
TokenType type
refers to the underlying token type identifier
Definition: parser.h:70
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)
friend std::ostream & operator<<(std::ostream &os, const Token &token)
std::vector< BooleanFunction::Value > constant
optional value in case token is a constant
Definition: parser.h:74
unsigned precedence(const ParserType &type) const