NEURON
nmodl_printer.cpp
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 
9 #include "utils/string_utils.hpp"
10 
11 namespace nmodl {
12 namespace printer {
13 
14 NMODLPrinter::NMODLPrinter(const std::string& filename) {
15  if (filename.empty()) {
16  throw std::runtime_error("Empty filename for NMODLPrinter");
17  }
18 
19  ofs.open(filename.c_str());
20 
21  if (ofs.fail()) {
22  auto msg = "Error while opening file '" + filename + "' for NMODLPrinter";
23  throw std::runtime_error(msg);
24  }
25 
26  sbuf = ofs.rdbuf();
27  result = std::make_shared<std::ostream>(sbuf);
28 }
29 
31  indent_level++;
32  *result << "{";
33  add_newline();
34 }
35 
37  *result << std::string(indent_level * 4, ' ');
38 }
39 
40 void NMODLPrinter::add_element(const std::string& name) {
41  *result << name << "";
42 }
43 
45  *result << std::endl;
46 }
47 
49  indent_level--;
50  add_indent();
51  *result << "}";
52 }
53 
54 } // namespace printer
55 } // namespace nmodl
void pop_level()
end of current block scope (i.e.
void add_indent()
print whitespaces for indentation
void add_element(const std::string &)
std::shared_ptr< std::ostream > result
void push_level()
start of new block scope (i.e.
const char * name
Definition: init.cpp:16
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
Helper class for printing AST back to NMDOL test.
Implement string manipulation functions.