P4C
The P4 Compiler
Loading...
Searching...
No Matches
p4_asserts_parser.h
1#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_BMV2_P4_ASSERTS_PARSER_H_
2#define BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_BMV2_P4_ASSERTS_PARSER_H_
3
4#include <cstddef>
5#include <iterator>
6#include <string_view>
7#include <vector>
8
9#include "backends/p4tools/common/lib/variables.h"
10#include "ir/ir.h"
11#include "ir/node.h"
12#include "ir/vector.h"
13#include "ir/visitor.h"
14#include "lib/cstring.h"
15
17
20using IdenitifierTypeMap = std::map<cstring, const IR::Type *>;
21
22class AssertsParser : public Transform {
24 ConstraintsVector &restrictionsVec;
25
26 public:
27 explicit AssertsParser(ConstraintsVector &output);
32 static std::vector<const IR::Expression *> genIRStructs(cstring tableName,
33 cstring restrictionString,
34 const IdenitifierTypeMap &typeMap);
35 const IR::Node *postorder(IR::P4Action *actionContext) override;
36 const IR::Node *postorder(IR::P4Table *tableContext) override;
37};
38
39class Token {
40 public:
41 enum class Kind {
42 Priority,
43 Text,
44 True,
45 False,
46 LineStatementClose, // \n
47 Id, // this, this.foo
48 Number, // 1, 2, -1, 5.2, -5.3
49 Minus, // -
50 Plus, // +
51 Dot, // .
52 FieldAcces, // :: (after key)
53 MetadataAccess, // :: (otherwise)
54 LeftParen, // (
55 RightParen, // )
56 Equal, // ==
57 NotEqual, // !=
58 GreaterThan, // >
59 GreaterEqual, // >=
60 LessThan, // <
61 LessEqual, // <=
62 LNot, // !
63 Colon, // :
64 Semicolon, // ;
65 Conjunction, // &&
66 Disjunction, // ||
67 Implication, // ->
68 Slash, // /
69 Percent, // %
70 Shr, // >>
71 Shl, // <<
72 Mul, // *
73 Comment, // //
74 Unknown,
75 EndString,
76 End,
77 };
78
79 Kind m_kind{};
80 std::string_view m_lexeme{};
81
82 explicit Token(Kind kind) noexcept : m_kind{kind} {}
83
84 Token(Kind kind, const char *beg, std::size_t len) noexcept
85 : m_kind{kind}, m_lexeme(beg, len) {}
86
87 Token(Kind kind, std::string_view lexeme) noexcept : m_kind{kind}, m_lexeme(lexeme) {}
88
89 Token(Kind kind, const char *beg, const char *end) noexcept
90 : m_kind{kind}, m_lexeme(beg, std::distance(beg, end)) {}
91
92 [[nodiscard]] Kind kind() const noexcept;
93
94 void kind(Kind kind) noexcept;
95
96 [[nodiscard]] bool is(Kind kind) const noexcept;
97
98 [[nodiscard]] bool isNot(Kind kind) const noexcept;
99
100 [[nodiscard]] bool isOneOf(Kind k1, Kind k2) const noexcept;
101
102 template <typename... Ts>
103 bool isOneOf(Kind k1, Kind k2, Ts... ks) const noexcept;
104
105 [[nodiscard]] std::string_view lexeme() const noexcept;
106
107 void lexeme(std::string_view lexeme) noexcept;
108};
109
110class Lexer {
111 public:
112 explicit Lexer(const char *beg) noexcept : mBeg{beg} {}
113
114 Token next() noexcept;
115 const char *mBeg = nullptr;
116
117 private:
118 Token atom(Token::Kind) noexcept;
119
120 [[nodiscard]] char peek() const noexcept;
121 char prev() noexcept;
122 char get() noexcept;
123};
124} // namespace P4::P4Tools::P4Testgen::Bmv2
125
126#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_BMV2_P4_ASSERTS_PARSER_H_ */
Definition node.h:94
Definition p4_asserts_parser.h:22
static std::vector< const IR::Expression * > genIRStructs(cstring tableName, cstring restrictionString, const IdenitifierTypeMap &typeMap)
Definition p4_asserts_parser.cpp:481
Definition p4_asserts_parser.h:110
Definition p4_asserts_parser.h:39
Kind kind() const noexcept
The function to get kind from token.
Definition p4_asserts_parser.cpp:36
bool isNot(Kind kind) const noexcept
Definition p4_asserts_parser.cpp:47
std::string_view lexeme() const noexcept
The function to get lexeme from token.
Definition p4_asserts_parser.cpp:58
bool isOneOf(Kind k1, Kind k2) const noexcept
Functions for multiple comparison kind.
Definition p4_asserts_parser.cpp:50
bool is(Kind kind) const noexcept
Definition p4_asserts_parser.cpp:43
Definition visitor.h:424
Definition cstring.h:85
Inja.
Definition targets/bmv2/cmd_stepper.cpp:33
std::map< cstring, const IR::Type * > IdenitifierTypeMap
Definition p4_asserts_parser.h:20
std::vector< const IR::Expression * > ConstraintsVector
Definition variables.h:17