User Guide
json_printer.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2023 Blue Brain Project, EPFL.
3  * See the top-level LICENSE file for details.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #pragma once
9 
10 /**
11  * \file
12  * \brief \copybrief nmodl::printer::JSONPrinter
13  */
14 
15 #include <nlohmann/json_fwd.hpp>
16 
17 #include <fstream>
18 #include <iostream>
19 #include <stack>
20 
21 namespace nmodl {
22 namespace printer {
23 
25 
26 /**
27  * @addtogroup printer
28  * @{
29  */
30 
31 /**
32  * \class JSONPrinter
33  * \brief Helper class for printing AST in JSON form
34  *
35  * We need to print AST in human readable format for debugging or visualization
36  * of in memory structure. This printer class provides simple interface to
37  * construct JSON object from AST like data structures. We use nlohmann's json
38  * library which considerably simplify implementation.
39  *
40  * \todo We need to explicitly call `flush()` in order to get write/return
41  * results. We simply can't dump block in `popBlock()` because block itself will
42  * be part of other parent elements. Also we are writing results to file,
43  * `stringstream` and `cout`. And hence we can't simply reset/clear previously
44  * written text.
45  */
46 class JSONPrinter {
47  private:
48  std::ofstream ofs;
49  std::streambuf* sbuf = nullptr;
50 
51  /// common output stream for file, cout or stringstream
52  std::shared_ptr<std::ostream> result;
53 
54  /// single (current) nmodl block / statement
55  std::shared_ptr<json> block;
56 
57  /// stack that holds all parent blocks / statements
58  std::stack<std::shared_ptr<json>> stack;
59 
60  /// true if need to print json in compact format
61  bool compact = false;
62 
63  /// true if we need to expand keys i.e. add separate key/value
64  /// pair for node name and it's children
65  bool expand = false;
66 
67  /// json key for children
68  const std::string child_key = "children";
69 
70  public:
71  explicit JSONPrinter(const std::string& filename);
72 
73  /// By default dump output to std::cout
75  : result(new std::ostream(std::cout.rdbuf())) {}
76 
77  // Dump output to stringstream
78  explicit JSONPrinter(std::ostream& os)
79  : result(new std::ostream(os.rdbuf())) {}
80 
82  flush();
83  }
84 
85  void push_block(const std::string& value, const std::string& key = "name");
86  void add_node(std::string value, const std::string& key = "name");
87  void add_block_property(std::string const& name, const std::string& value);
88  void pop_block();
89  void flush();
90 
91  /// print json in compact mode
92  void compact_json(bool flag) {
93  compact = flag;
94  }
95 
96  void expand_keys(bool flag) {
97  expand = flag;
98  }
99 };
100 
101 /** @} */ // end of printer
102 
103 } // namespace printer
104 } // namespace nmodl
nmodl::printer::JSONPrinter::block
std::shared_ptr< json > block
single (current) nmodl block / statement
Definition: json_printer.hpp:55
nmodl::printer::JSONPrinter::sbuf
std::streambuf * sbuf
Definition: json_printer.hpp:49
nmodl::printer::JSONPrinter::JSONPrinter
JSONPrinter()
By default dump output to std::cout.
Definition: json_printer.hpp:74
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::printer::JSONPrinter::ofs
std::ofstream ofs
Definition: json_printer.hpp:48
nmodl::printer::JSONPrinter::result
std::shared_ptr< std::ostream > result
common output stream for file, cout or stringstream
Definition: json_printer.hpp:52
nmodl::printer::JSONPrinter::child_key
const std::string child_key
json key for children
Definition: json_printer.hpp:68
nmodl::printer::json
nlohmann::json json
Definition: json_printer.hpp:24
nmodl::printer::JSONPrinter::add_node
void add_node(std::string value, const std::string &key="name")
Add node to json (typically basic type)
Definition: json_printer.cpp:34
nmodl::printer::JSONPrinter
Helper class for printing AST in JSON form.
Definition: json_printer.hpp:46
nmodl::printer::JSONPrinter::expand
bool expand
true if we need to expand keys i.e.
Definition: json_printer.hpp:65
nmodl::printer::JSONPrinter::compact_json
void compact_json(bool flag)
print json in compact mode
Definition: json_printer.hpp:92
nmodl::printer::JSONPrinter::push_block
void push_block(const std::string &value, const std::string &key="name")
Add new json object (typically start of new block) name here is type of new block encountered.
Definition: json_printer.cpp:55
nmodl::printer::JSONPrinter::pop_block
void pop_block()
We finished processing a block, add processed block to it's parent block.
Definition: json_printer.cpp:71
nmodl::printer::JSONPrinter::JSONPrinter
JSONPrinter(std::ostream &os)
Definition: json_printer.hpp:78
nmodl::printer::JSONPrinter::compact
bool compact
true if need to print json in compact format
Definition: json_printer.hpp:61
json
nlohmann::json json
Definition: json.cpp:17
nmodl::printer::JSONPrinter::flush
void flush()
Dump json object to stream (typically at the end) nspaces is number of spaces used for indentation.
Definition: json_printer.cpp:82
nmodl::printer::JSONPrinter::add_block_property
void add_block_property(std::string const &name, const std::string &value)
Add property to the block which is added last.
Definition: json_printer.cpp:45
nmodl::printer::JSONPrinter::stack
std::stack< std::shared_ptr< json > > stack
stack that holds all parent blocks / statements
Definition: json_printer.hpp:58
nmodl::printer::JSONPrinter::~JSONPrinter
~JSONPrinter()
Definition: json_printer.hpp:81
nmodl::printer::JSONPrinter::expand_keys
void expand_keys(bool flag)
Definition: json_printer.hpp:96