User Guide
nmodl_visitor_helper.ipp
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 
11 
13 
14 
15 namespace nmodl {
16 namespace visitor {
17 
18 /** Helper function to visit vector elements
19  *
20  * @tparam T
21  * @param elements vector of nodes/elements
22  * @param separator separator to print for individual vector element
23  * @param program true if provided elements belong to program node
24  * @param statement true if elements in vector of statement type
25  */
26 
27 template <typename T>
28 void NmodlPrintVisitor::visit_element(const std::vector<T>& elements,
29  const std::string& separator,
30  bool program,
31  bool statement) {
32  for (auto iter = elements.begin(); iter != elements.end(); iter++) {
33  /// statements need indentation at the start
34  if (statement) {
35  printer->add_indent();
36  }
37 
38  (*iter)->accept(*this);
39 
40  /// print separator (e.g. comma, space)
41  if (!separator.empty() && !utils::is_last(iter, elements)) {
42  printer->add_element(separator);
43  }
44 
45  /// newline at the end of statement
46  if (statement) {
47  printer->add_newline();
48  }
49 
50  /// if there are multiple inline comments then we want them to be
51  /// contiguous and only last comment should have extra line.
52  bool extra_newline = false;
53  if (!utils::is_last(iter, elements)) {
54  extra_newline = true;
55  if ((*iter)->is_line_comment() && (*(iter + 1))->is_line_comment()) {
56  extra_newline = false;
57  }
58  }
59 
60  /// program blocks need two newlines except last one
61  if (program) {
62  printer->add_newline();
63  if (extra_newline) {
64  printer->add_newline();
65  }
66  }
67  }
68 }
69 
70 } // namespace visitor
71 } // namespace nmodl
nmodl::utils::is_last
bool is_last(Iter iter, const Cont &cont)
Check if the iterator is pointing to last element in the container.
Definition: common_utils.hpp:37
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
visitor_utils.hpp
Utility functions for visitors implementation.
nmodl::visitor::NmodlPrintVisitor::visit_element
void visit_element(const std::vector< T > &elements, const std::string &separator, bool program, bool statement)
Helper function to visit vector elements.
Definition: nmodl_visitor_helper.ipp:28
nmodl::visitor::NmodlPrintVisitor::printer
std::unique_ptr< printer::NMODLPrinter > printer
Definition: nmodl_visitor.hpp:46
nmodl_visitor.hpp
THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.