PropertyAstToString.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2017 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_LANG_CSS_UTIL__PROPERTY_AST_TO_STRING
18 #define COM_BORA_SOFTWARE__BALAU_LANG_CSS_UTIL__PROPERTY_AST_TO_STRING
19 
22 
24 
29  private: const std::string indentString = "\t";
30 
31  private: std::ostream & builder;
32  private: size_t indentCount = 0;
33 
34  public: explicit PropertyAstToStringPayload(std::ostream & stream) : builder(stream) {}
35 
36  public: void write(const std::string_view & text) {
37  builder << text;
38  }
39 
40  public: void incrementIndent() {
41  ++indentCount;
42  }
43 
44  public: void decrementIndent() {
45  --indentCount;
46  }
47 
48  public: void writeIndent() {
49  for (size_t m = 0; m < indentCount; m++) {
50  builder << indentString;
51  }
52  }
53 };
54 
59  public: void visit(Payload & payload, const Properties & object) override {
60  for (auto & node : object.getNodes()) {
61  node->visit(payload, *this);
62  }
63  }
64 
65  public: void visit(Payload & payload, const ValueProperty & object) override {
66  auto & pl = static_cast<PropertyAstToStringPayload &>(payload);
67  pl.writeIndent();
68  pl.write(object.getName());
69  pl.write(" = ");
70  pl.write(object.getValue());
71  pl.write("\n");
72  }
73 
74  public: void visit(Payload & payload, const CompositeProperty & object) override {
75  auto & pl = static_cast<PropertyAstToStringPayload &>(payload);
76  pl.writeIndent();
77  pl.write(object.getName());
78  pl.write(" {\n");
79  pl.incrementIndent();
80 
81  for (const auto & node : object.getNodes()) {
82  node->visit(payload, *this);
83  }
84 
85  pl.decrementIndent();
86  pl.writeIndent();
87  pl.write("}\n");
88  }
89 
90  public: void visit(Payload & payload, const IncludePropertyNode & object) override {
91  auto & pl = static_cast<PropertyAstToStringPayload &>(payload);
92  pl.writeIndent();
93  pl.write("@");
94  pl.write(object.getText());
95  pl.write("\n");
96  }
97 
98  public: void visit(Payload & payload, const CommentPropertyNode & object) override {
99  auto & pl = static_cast<PropertyAstToStringPayload &>(payload);
100  pl.writeIndent();
101  pl.write("#");
102  pl.write(object.getText());
103  pl.write("\n");
104  }
105 
106  public: void visit(Payload & payload, const EmptyLinePropertyNode & object) override {
107  auto & pl = static_cast<PropertyAstToStringPayload &>(payload);
108  pl.write("\n");
109  }
110 };
111 
115 inline std::ostream & operator << (std::ostream & stream, const PropertyNode & object) {
116  PropertyAstToStringPayload payload(stream);
117  PropertyAstToString astToString;
118  object.visit(payload, astToString);
119  return stream;
120 }
121 
129 inline std::string toString(const PropertyNode & object) {
130  std::ostringstream stream;
131  stream << object;
132  return stream.str();
133 }
134 
142 template <typename AllocatorT>
143 inline Balau::U8String<AllocatorT> toString(const PropertyNode & object) {
145  stream << object;
146  return stream.str();
147 }
148 
149 } // namespace Balau::Lang::Property::AST
150 
151 #endif // COM_BORA_SOFTWARE__BALAU_LANG_CSS_UTIL__PROPERTY_AST_TO_STRING
void visit(Payload &payload, const IncludePropertyNode &object) override
Visit an include property node.
Definition: PropertyAstToString.hpp:90
The Property parser AST node classes.
void visit(Payload &payload, const EmptyLinePropertyNode &object) override
Visit a blank line property node.
Definition: PropertyAstToString.hpp:106
Simple name-value property node.
Definition: PropertyAst.hpp:221
void visit(Payload &payload, const CompositeProperty &object) override
Visit a composite property node.
Definition: PropertyAstToString.hpp:74
void visit(Payload &payload, const ValueProperty &object) override
Visit a value property node.
Definition: PropertyAstToString.hpp:65
Visitor interface for property AST nodes.
Definition: PropertyVisitor.hpp:49
The property AST visitor used in the property AST toString implementation.
Definition: PropertyAstToString.hpp:58
Abstract base class of Property AST node classes.
Definition: PropertyAst.hpp:35
The payload used in the property AST toString implementation.
Definition: PropertyAstToString.hpp:28
Represents a blank line in a property file.
Definition: PropertyAst.hpp:465
Composite name-value property node.
Definition: PropertyAst.hpp:288
Base class of property visitor payloads.
Definition: PropertyVisitor.hpp:39
std::basic_ostringstream< char, std::char_traits< char >, AllocatorT > U8OStringStream
UTF-8 output string stream type with selectable allocator.
Definition: ToStringA.hpp:59
void visit(Payload &payload, const Properties &object) override
Visit a Properties node.
Definition: PropertyAstToString.hpp:59
std::string toString(const PropertyNode &object)
The property AST toString function.
Definition: PropertyAstToString.hpp:129
Base Balau exception classes.
std::ostream & operator<<(std::ostream &stream, const PropertyNode &object)
Pretty print the supplied AST to the output stream.
Definition: PropertyAstToString.hpp:115
Include property node.
Definition: PropertyAst.hpp:357
std::basic_string< char, std::char_traits< char >, AllocatorT > U8String
UTF-8 string type with selectable allocator.
Definition: ToStringA.hpp:41
Comment property node.
Definition: PropertyAst.hpp:411
void visit(Payload &payload, const CommentPropertyNode &object) override
Visit a comment property node.
Definition: PropertyAstToString.hpp:98
The Property parser abstract syntax tree node classes.
Definition: PropertyAst.hpp:30
The outer structure.
Definition: PropertyAst.hpp:175