Spicy
formatter.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <string>
6 #include <vector>
7 
8 #include <hilti/base/code-formatter.h>
9 #include <hilti/compiler/detail/cxx/elements.h>
10 
11 namespace hilti::detail::cxx {
12 
13 class Unit;
14 
16 class Formatter : public CodeFormatter {
17 public:
26  void pushNamespace(std::string relative_ns);
27 
29  void popNamespace();
30 
43  void enterNamespace(const std::string& absolute_ns);
44 
46  void leaveNamespace();
47 
54  std::optional<std::string> namespace_(int level = -1) const;
55 
65  cxx::ID relativeID(const cxx::ID& id, int level) const;
66 
67  bool ensure_braces_for_block = true;
68  bool compact_block = true;
69  bool eos_after_block = false;
70  bool sep_after_block = true;
71 
72 private:
73  std::vector<std::string> _namespaces;
74 };
75 
76 // TODO(robin): Can we factor out these operators into code-formatter.h?
77 template<typename T, IF_DERIVED_FROM(T, code_formatter::isManipulator)>
78 inline Formatter& operator<<(Formatter& f, const T& t) {
79  return t(f);
80 }
81 
82 template<typename T, typename std::enable_if_t<std::is_scalar<T>::value>* = nullptr>
83 inline Formatter& operator<<(Formatter& f, const T& t) {
84  f.next();
85  f.stream() << t;
86  return f;
87 }
88 
89 inline Formatter& operator<<(Formatter& f, const std::string& s) {
90  f.printString(s);
91  return f;
92 }
93 inline Formatter& operator<<(Formatter& f, const char* s) {
94  f.printString(s);
95  return f;
96 }
97 
98 namespace formatter {
99 using dedent = hilti::code_formatter::dedent<Formatter>;
100 using eol = hilti::code_formatter::eol<Formatter>;
101 using eos = hilti::code_formatter::eos<Formatter>;
102 using indent = hilti::code_formatter::indent<Formatter>;
103 using separator = hilti::code_formatter::separator<Formatter>;
104 using quoted = hilti::code_formatter::quoted<Formatter>;
105 using comment = hilti::code_formatter::comment<Formatter>;
106 } // namespace formatter
107 
108 } // namespace hilti::detail::cxx
Definition: formatter.h:16
void leaveNamespace()
Definition: formatter.cc:87
void popNamespace()
Definition: formatter.cc:59
void quoted(const std::string &s)
Definition: code-formatter.cc:40
void comment(const std::string &s)
Definition: code-formatter.cc:45
void enterNamespace(const std::string &absolute_ns)
Definition: formatter.cc:34
Definition: elements.h:18
void pushNamespace(std::string relative_ns)
Definition: formatter.cc:11
cxx::ID relativeID(const cxx::ID &id, int level) const
Definition: formatter.cc:92
void eol()
Definition: code-formatter.cc:27
void dedent()
Definition: code-formatter.h:56
auto & stream()
Definition: code-formatter.h:59
Definition: elements.h:66
Definition: code-formatter.h:22
void indent()
Definition: code-formatter.h:53
std::optional< std::string > namespace_(int level=-1) const
Definition: formatter.cc:80
CodeFormatter & printString(const std::string &s)
Definition: code-formatter.cc:55
void next()
Definition: code-formatter.cc:9
void separator()
Definition: code-formatter.cc:17