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 <sstream>
23
24#include "lib/cstring.h"
25#include "lib/exceptions.h"
26#include "lib/stringify.h"
27
28namespace Util {
30 int indentLevel; // current indent level
31 unsigned indentAmount;
32
33 std::stringstream buffer;
34 const std::string nl = "\n";
35 bool endsInSpace;
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 << nl;
47 endsInSpace = true;
48 }
49 void spc() {
50 if (!endsInSpace) buffer << " ";
51 endsInSpace = true;
52 }
53
54 void append(cstring str) { append(str.c_str()); }
55 void appendLine(cstring str) {
56 append(str);
57 newline();
58 }
59 void append(const std::string &str) {
60 if (str.empty()) return;
61 endsInSpace = ::isspace(str.at(str.size() - 1));
62 buffer << str;
63 }
64 void append(char c) {
65 endsInSpace = ::isspace(c);
66 buffer << c;
67 }
68 void append(const char *str) {
69 if (str == nullptr) BUG("Null argument to append");
70 if (strlen(str) == 0) return;
71 endsInSpace = ::isspace(str[strlen(str) - 1]);
72 buffer << str;
73 }
74 void appendFormat(const char *format, ...) {
75 va_list ap;
76 va_start(ap, format);
77 cstring str = Util::vprintf_format(format, ap);
78 va_end(ap);
79 append(str);
80 }
81 void append(unsigned u) { appendFormat("%d", u); }
82 void append(int u) { appendFormat("%d", u); }
83
84 void endOfStatement(bool addNl = false) {
85 append(";");
86 if (addNl) newline();
87 }
88
89 void blockStart() {
90 append("{");
91 newline();
92 increaseIndent();
93 }
94
95 void emitIndent() {
96 buffer << std::string(indentLevel, ' ');
97 if (indentLevel > 0) endsInSpace = true;
98 }
99
100 void blockEnd(bool nl) {
101 decreaseIndent();
102 emitIndent();
103 append("}");
104 if (nl) newline();
105 }
106
107 std::string toString() const { return buffer.str(); }
108 void commentStart() { append("/* "); }
109 void commentEnd() { append(" */"); }
110 bool lastIsSpace() const { return endsInSpace; }
111};
112} // namespace Util
113
114#endif /* LIB_SOURCECODEBUILDER_H_ */
Definition sourceCodeBuilder.h:29
Definition cstring.h:72