![]() |
User Guide
|
Helper class for printing AST in JSON form. More...
Helper class for printing AST in JSON form.
We need to print AST in human readable format for debugging or visualization of in memory structure. This printer class provides simple interface to construct JSON object from AST like data structures. We use nlohmann's json library which considerably simplify implementation.
flush()
in order to get write/return results. We simply can't dump block in popBlock()
because block itself will be part of other parent elements. Also we are writing results to file, stringstream
and cout
. And hence we can't simply reset/clear previously written text. Definition at line 46 of file json_printer.hpp.
#include <json_printer.hpp>
Public Member Functions | |
JSONPrinter (const std::string &filename) | |
Dump output to provided file. More... | |
JSONPrinter () | |
By default dump output to std::cout. More... | |
JSONPrinter (std::ostream &os) | |
~JSONPrinter () | |
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. More... | |
void | add_node (std::string value, const std::string &key="name") |
Add node to json (typically basic type) More... | |
void | add_block_property (std::string const &name, const std::string &value) |
Add property to the block which is added last. More... | |
void | pop_block () |
We finished processing a block, add processed block to it's parent block. More... | |
void | flush () |
Dump json object to stream (typically at the end) nspaces is number of spaces used for indentation. More... | |
void | compact_json (bool flag) |
print json in compact mode More... | |
void | expand_keys (bool flag) |
Private Attributes | |
std::ofstream | ofs |
std::streambuf * | sbuf = nullptr |
std::shared_ptr< std::ostream > | result |
common output stream for file, cout or stringstream More... | |
std::shared_ptr< json > | block |
single (current) nmodl block / statement More... | |
std::stack< std::shared_ptr< json > > | stack |
stack that holds all parent blocks / statements More... | |
bool | compact = false |
true if need to print json in compact format More... | |
bool | expand = false |
true if we need to expand keys i.e. More... | |
const std::string | child_key = "children" |
json key for children More... | |
|
explicit |
Dump output to provided file.
Definition at line 17 of file json_printer.cpp.
|
inline |
By default dump output to std::cout.
Definition at line 74 of file json_printer.hpp.
|
inlineexplicit |
Definition at line 78 of file json_printer.hpp.
|
inline |
Definition at line 81 of file json_printer.hpp.
void nmodl::printer::JSONPrinter::add_block_property | ( | std::string const & | name, |
const std::string & | value | ||
) |
Add property to the block which is added last.
Definition at line 45 of file json_printer.cpp.
void nmodl::printer::JSONPrinter::add_node | ( | std::string | value, |
const std::string & | key = "name" |
||
) |
Add node to json (typically basic type)
Definition at line 34 of file json_printer.cpp.
|
inline |
print json in compact mode
Definition at line 92 of file json_printer.hpp.
|
inline |
Definition at line 96 of file json_printer.hpp.
void nmodl::printer::JSONPrinter::flush | ( | ) |
Dump json object to stream (typically at the end) nspaces is number of spaces used for indentation.
Definition at line 82 of file json_printer.cpp.
void nmodl::printer::JSONPrinter::pop_block | ( | ) |
We finished processing a block, add processed block to it's parent block.
Definition at line 71 of file json_printer.cpp.
void nmodl::printer::JSONPrinter::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 at line 55 of file json_printer.cpp.
|
private |
single (current) nmodl block / statement
Definition at line 55 of file json_printer.hpp.
|
private |
json key for children
Definition at line 68 of file json_printer.hpp.
|
private |
true if need to print json in compact format
Definition at line 61 of file json_printer.hpp.
|
private |
true if we need to expand keys i.e.
add separate key/value pair for node name and it's children
Definition at line 65 of file json_printer.hpp.
|
private |
Definition at line 48 of file json_printer.hpp.
|
private |
common output stream for file, cout or stringstream
Definition at line 52 of file json_printer.hpp.
|
private |
Definition at line 49 of file json_printer.hpp.
|
private |
stack that holds all parent blocks / statements
Definition at line 58 of file json_printer.hpp.