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