NEURON
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
Helper class for printing AST in JSON form.
void flush()
Dump json object to stream (typically at the end) nspaces is number of spaces used for indentation.
const std::string child_key
json key for children
bool expand
true if we need to expand keys i.e.
void add_node(std::string value, const std::string &key="name")
Add node to json (typically basic type)
void compact_json(bool flag)
print json in compact mode
void add_block_property(std::string const &name, const std::string &value)
Add property to the block which is added last.
std::shared_ptr< json > block
single (current) nmodl block / statement
std::shared_ptr< std::ostream > result
common output stream for file, cout or stringstream
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.
void pop_block()
We finished processing a block, add processed block to it's parent block.
JSONPrinter(std::ostream &os)
JSONPrinter()
By default dump output to std::cout.
bool compact
true if need to print json in compact format
std::stack< std::shared_ptr< json > > stack
stack that holds all parent blocks / statements
#define key
Definition: tqueue.hpp:45
nlohmann::json json
Definition: json.cpp:17
const char * name
Definition: init.cpp:16
nlohmann::json json
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
static uint32_t value
Definition: scoprand.cpp:25