Skip to content
Snippets Groups Projects
Commit 145e2d9e authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Improve output of AST

now one can select some details (useful for unit tests)
parent 840ec759
Branches
Tags
1 merge request!37Feature/language
#include <ASTPrinter.hpp> #include <ASTPrinter.hpp>
#include <EscapedString.hpp> #include <EscapedString.hpp>
#include <PEGGrammar.hpp>
namespace language namespace language
{ {
void void
...@@ -12,9 +14,20 @@ ASTPrinter::_print(std::ostream& os, const Node& node) const ...@@ -12,9 +14,20 @@ ASTPrinter::_print(std::ostream& os, const Node& node) const
} else { } else {
os << node.name(); os << node.name();
} }
os << rang::fg::reset << ':'; os << rang::fg::reset;
os << dataTypeName(node.m_data_type) << ':';
if (node.is<language::name>() or node.is<language::literal>() or node.is<language::integer>() or
node.is<language::real>()) {
os << ':' << rang::fgB::green << node.string() << rang::fg::reset;
}
if (m_info & static_cast<InfoBaseType>(Info::data_type)) {
os << ':';
os << dataTypeName(node.m_data_type) << rang::fg::reset;
}
if (m_info & static_cast<InfoBaseType>(Info::data_value)) {
os << ':';
os << rang::fgB::cyan; os << rang::fgB::cyan;
std::visit( std::visit(
[&](const auto& value) { [&](const auto& value) {
...@@ -28,6 +41,7 @@ ASTPrinter::_print(std::ostream& os, const Node& node) const ...@@ -28,6 +41,7 @@ ASTPrinter::_print(std::ostream& os, const Node& node) const
} }
}, },
node.m_value); node.m_value);
}
os << rang::fg::reset << ")\n"; os << rang::fg::reset << ")\n";
if (not node.children.empty()) { if (not node.children.empty()) {
...@@ -71,7 +85,8 @@ operator<<(std::ostream& os, const ASTPrinter& ast_printer) ...@@ -71,7 +85,8 @@ operator<<(std::ostream& os, const ASTPrinter& ast_printer)
return os; return os;
} }
ASTPrinter::ASTPrinter(const language::Node& node, Format format) : m_node{node} ASTPrinter::ASTPrinter(const language::Node& node, Format format, std::initializer_list<Info> initializer_list)
: m_node{node}
{ {
if (format == Format::pretty) { if (format == Format::pretty) {
T_junction = " \u251c\u2500\u2500"; T_junction = " \u251c\u2500\u2500";
...@@ -85,5 +100,10 @@ ASTPrinter::ASTPrinter(const language::Node& node, Format format) : m_node{node} ...@@ -85,5 +100,10 @@ ASTPrinter::ASTPrinter(const language::Node& node, Format format) : m_node{node}
pipe_space = " | "; pipe_space = " | ";
space_space = " "; space_space = " ";
} }
m_info = 0;
for (auto i : initializer_list) {
m_info |= static_cast<InfoBaseType>(i);
}
} }
} // namespace language } // namespace language
...@@ -14,9 +14,21 @@ class ASTPrinter ...@@ -14,9 +14,21 @@ class ASTPrinter
pretty pretty
}; };
using InfoBaseType = uint32_t;
enum class Info : InfoBaseType
{
none = 0,
data_type = 1 << 0,
data_value = 1 << 1,
all = std::numeric_limits<InfoBaseType>::max()
};
private: private:
const Node& m_node; const Node& m_node;
InfoBaseType m_info;
mutable std::string prefix; mutable std::string prefix;
mutable std::vector<int> last_prefix_size; mutable std::vector<int> last_prefix_size;
...@@ -34,7 +46,9 @@ class ASTPrinter ...@@ -34,7 +46,9 @@ class ASTPrinter
public: public:
friend std::ostream& operator<<(std::ostream& os, const ASTPrinter& ast_printer); friend std::ostream& operator<<(std::ostream& os, const ASTPrinter& ast_printer);
ASTPrinter(const Node& node, Format format = Format::pretty); ASTPrinter(const Node& node,
Format format = Format::pretty,
std::initializer_list<Info> initializer_list = {Info::all});
ASTPrinter(const ASTPrinter&) = delete; ASTPrinter(const ASTPrinter&) = delete;
......
...@@ -484,7 +484,7 @@ parser(const std::string& filename) ...@@ -484,7 +484,7 @@ parser(const std::string& filename)
language::build_node_type(*root_node); language::build_node_type(*root_node);
std::cout << language::ASTPrinter{*root_node, language::ASTPrinter::Format::raw} << '\n'; std::cout << language::ASTPrinter{*root_node} << '\n';
language::ExecUntilBreakOrContinue exec_all; language::ExecUntilBreakOrContinue exec_all;
root_node->execute(exec_all); root_node->execute(exec_all);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment