User Guide
nmodl::printer::JSONPrinter Class Reference

Helper class for printing AST in JSON form. More...

Detailed Description

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.

Todo:
We need to explicitly call 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< jsonblock
 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...
 

Constructor & Destructor Documentation

◆ JSONPrinter() [1/3]

nmodl::printer::JSONPrinter::JSONPrinter ( const std::string &  filename)
explicit

Dump output to provided file.

Definition at line 16 of file json_printer.cpp.

◆ JSONPrinter() [2/3]

nmodl::printer::JSONPrinter::JSONPrinter ( )
inline

By default dump output to std::cout.

Definition at line 74 of file json_printer.hpp.

◆ JSONPrinter() [3/3]

nmodl::printer::JSONPrinter::JSONPrinter ( std::ostream &  os)
inlineexplicit

Definition at line 78 of file json_printer.hpp.

◆ ~JSONPrinter()

nmodl::printer::JSONPrinter::~JSONPrinter ( )
inline

Definition at line 81 of file json_printer.hpp.

Member Function Documentation

◆ add_block_property()

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 44 of file json_printer.cpp.

◆ add_node()

void nmodl::printer::JSONPrinter::add_node ( std::string  value,
const std::string &  key = "name" 
)

Add node to json (typically basic type)

Definition at line 33 of file json_printer.cpp.

◆ compact_json()

void nmodl::printer::JSONPrinter::compact_json ( bool  flag)
inline

print json in compact mode

Definition at line 92 of file json_printer.hpp.

◆ expand_keys()

void nmodl::printer::JSONPrinter::expand_keys ( bool  flag)
inline

Definition at line 96 of file json_printer.hpp.

◆ flush()

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 81 of file json_printer.cpp.

◆ pop_block()

void nmodl::printer::JSONPrinter::pop_block ( )

We finished processing a block, add processed block to it's parent block.

Definition at line 70 of file json_printer.cpp.

◆ push_block()

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 54 of file json_printer.cpp.

Member Data Documentation

◆ block

std::shared_ptr<json> nmodl::printer::JSONPrinter::block
private

single (current) nmodl block / statement

Definition at line 55 of file json_printer.hpp.

◆ child_key

const std::string nmodl::printer::JSONPrinter::child_key = "children"
private

json key for children

Definition at line 68 of file json_printer.hpp.

◆ compact

bool nmodl::printer::JSONPrinter::compact = false
private

true if need to print json in compact format

Definition at line 61 of file json_printer.hpp.

◆ expand

bool nmodl::printer::JSONPrinter::expand = false
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.

◆ ofs

std::ofstream nmodl::printer::JSONPrinter::ofs
private

Definition at line 48 of file json_printer.hpp.

◆ result

std::shared_ptr<std::ostream> nmodl::printer::JSONPrinter::result
private

common output stream for file, cout or stringstream

Definition at line 52 of file json_printer.hpp.

◆ sbuf

std::streambuf* nmodl::printer::JSONPrinter::sbuf = nullptr
private

Definition at line 49 of file json_printer.hpp.

◆ stack

std::stack<std::shared_ptr<json> > nmodl::printer::JSONPrinter::stack
private

stack that holds all parent blocks / statements

Definition at line 58 of file json_printer.hpp.


The documentation for this class was generated from the following files: