P4C
The P4 Compiler
Loading...
Searching...
No Matches
sourceCodeBuilder.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef LIB_SOURCECODEBUILDER_H_
18#define LIB_SOURCECODEBUILDER_H_
19
20#include <ctype.h>
21
22#include "absl/strings/cord.h"
23#include "absl/strings/str_format.h"
24#include "lib/cstring.h"
25#include "lib/exceptions.h"
26#include "lib/stringify.h"
27
28namespace P4::Util {
30 int indentLevel; // current indent level
31 unsigned indentAmount;
32
33 absl::Cord buffer;
34 bool endsInSpace;
35 bool supressSemi = false;
36
37 public:
38 SourceCodeBuilder() : indentLevel(0), indentAmount(4), endsInSpace(false) {}
39
40 void increaseIndent() { indentLevel += indentAmount; }
41 void decreaseIndent() {
42 indentLevel -= indentAmount;
43 if (indentLevel < 0) BUG("Negative indent");
44 }
45 void newline() {
46 buffer.Append("\n");
47 endsInSpace = true;
48 }
49 void spc() {
50 if (!endsInSpace) buffer.Append(" ");
51 endsInSpace = true;
52 }
53
54 void append(cstring str) { append(str.c_str()); }
55 void appendLine(const char *str) {
56 append(str);
57 newline();
58 }
59 void appendLine(cstring str) {
60 append(str);
61 newline();
62 }
63 void append(const std::string &str) {
64 if (str.empty()) return;
65 endsInSpace = ::isspace(str.back());
66 buffer.Append(str);
67 }
68 [[deprecated("use string / char* version instead")]]
69 void append(char c) {
70 std::string str(1, c);
71 append(str);
72 }
73 void append(const char *str) {
74 if (str == nullptr) BUG("Null argument to append");
75 if (strlen(str) == 0) return;
76 endsInSpace = ::isspace(str[strlen(str) - 1]);
77 buffer.Append(str);
78 }
79
80 template <typename... Args>
81 void appendFormat(const absl::FormatSpec<Args...> &format, Args &&...args) {
82 // FIXME: Sink directly to cord
83 append(absl::StrFormat(format, std::forward<Args>(args)...));
84 }
85 void append(unsigned u) { appendFormat("%d", u); }
86 void append(int u) { appendFormat("%d", u); }
87
88 void endOfStatement(bool addNl = false) {
89 if (!supressSemi) append(";");
90 supressSemi = false;
91 if (addNl) newline();
92 }
93 void supressStatementSemi() { supressSemi = true; }
94
95 void blockStart() {
96 append("{");
97 newline();
98 increaseIndent();
99 }
100
101 void emitIndent() {
102 buffer.Append(std::string(indentLevel, ' '));
103 if (indentLevel > 0) endsInSpace = true;
104 }
105
106 void blockEnd(bool nl) {
107 decreaseIndent();
108 emitIndent();
109 append("}");
110 if (nl) newline();
111 }
112
113 std::string toString() const { return std::string(buffer); }
114 void commentStart() { append("/* "); }
115 void commentEnd() { append(" */"); }
116 bool lastIsSpace() const { return endsInSpace; }
117};
118} // namespace P4::Util
119
120#endif /* LIB_SOURCECODEBUILDER_H_ */
Definition sourceCodeBuilder.h:29
Definition cstring.h:85