User Guide
units_visitor.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::visitor::UnitsVisitor
13  */
14 
15 #include <sstream>
16 #include <string>
17 #include <vector>
18 
19 #include "parser/unit_driver.hpp"
20 #include "visitors/ast_visitor.hpp"
22 
23 namespace nmodl {
24 namespace visitor {
25 
26 /**
27  * \addtogroup visitor_classes
28  * \{
29  */
30 
31 /**
32  * \class UnitsVisitor
33  * \brief Visitor for Units blocks of AST
34  *
35  * This is simple example of visitor that uses base AstVisitor
36  * interface. We override AstVisitor::visit_program, AstVisitor::visit_unit_def
37  * and AstVisitor::visit_factor_def method. Furthermore it keeps the
38  * parser::UnitDriver to parse the units file and the strings generated by the
39  * units in the mod files.
40  */
41 
42 class UnitsVisitor: public AstVisitor {
43  private:
44  /// Units Driver needed to parse the units file and the string produces by
45  /// mod files' units
47 
48  /// Directory of units lib file that defines all the basic units
49  std::string units_dir;
50 
51  /// Declaration of `fuzz` constant unit, which is the equivilant of `1`
52  /// in mod files UNITS definitions
53  const std::string UNIT_FUZZ = "fuzz";
54 
55  public:
56  /// \name Ctor & dtor
57  /// \{
58 
59  /// Default UnitsVisitor constructor
60  UnitsVisitor() = default;
61 
62  /// UnitsVisitor constructor that takes as argument the units file to parse
63  /// the units from
64  explicit UnitsVisitor(std::string t_units_dir)
65  : units_dir(std::move(t_units_dir)) {}
66 
67  /// \}
68 
69  /// Function to visit all the ast::UnitDef nodes and parse the units defined as
70  /// ast::UnitDef in the UNITS block of mod files
71  void visit_unit_def(ast::UnitDef& node) override;
72 
73  /// Function to visit all the ast::FactorDef nodes and parse the units defined
74  /// as ast::FactorDef in the UNITS block of mod files
75  void visit_factor_def(ast::FactorDef& node) override;
76 
77  /// Override visit_program function to parse the \c nrnunits.lib unit file
78  /// before starting visiting the AST to parse the units defined in mod files
79  void visit_program(ast::Program& node) override;
80 
81  /// Get the parser::UnitDriver to be able to use it outside the visitor::UnitsVisitor
82  /// scope keeping the same units::UnitTable
83  const parser::UnitDriver& get_unit_driver() const noexcept {
84  return units_driver;
85  }
86 };
87 
88 /** \} */ // end of visitor_classes
89 
90 } // namespace visitor
91 } // namespace nmodl
nmodl::visitor::UnitsVisitor::UNIT_FUZZ
const std::string UNIT_FUZZ
Declaration of fuzz constant unit, which is the equivilant of 1 in mod files UNITS definitions.
Definition: units_visitor.hpp:53
nmodl::ast::FactorDef
TODO.
Definition: factor_def.hpp:38
nmodl::visitor::UnitsVisitor::visit_program
void visit_program(ast::Program &node) override
Override visit_program function to parse the nrnunits.lib unit file before starting visiting the AST ...
Definition: units_visitor.cpp:22
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
unit_driver.hpp
nmodl::visitor::UnitsVisitor::units_driver
parser::UnitDriver units_driver
Units Driver needed to parse the units file and the string produces by mod files' units.
Definition: units_visitor.hpp:46
nmodl::ast::UnitDef
TODO.
Definition: unit_def.hpp:38
visitor_utils.hpp
Utility functions for visitors implementation.
nmodl::visitor::UnitsVisitor::visit_factor_def
void visit_factor_def(ast::FactorDef &node) override
Function to visit all the ast::FactorDef nodes and parse the units defined as ast::FactorDef in the U...
Definition: units_visitor.cpp:83
nmodl::visitor::UnitsVisitor::UnitsVisitor
UnitsVisitor()=default
Default UnitsVisitor constructor.
nmodl::visitor::AstVisitor
Concrete visitor for all AST classes.
Definition: ast_visitor.hpp:37
nmodl::visitor::UnitsVisitor::get_unit_driver
const parser::UnitDriver & get_unit_driver() const noexcept
Get the parser::UnitDriver to be able to use it outside the visitor::UnitsVisitor scope keeping the s...
Definition: units_visitor.hpp:83
nmodl::visitor::UnitsVisitor::units_dir
std::string units_dir
Directory of units lib file that defines all the basic units.
Definition: units_visitor.hpp:49
nmodl::visitor::UnitsVisitor::visit_unit_def
void visit_unit_def(ast::UnitDef &node) override
Function to visit all the ast::UnitDef nodes and parse the units defined as ast::UnitDef in the UNITS...
Definition: units_visitor.cpp:38
nmodl::visitor::UnitsVisitor
Visitor for Units blocks of AST.
Definition: units_visitor.hpp:42
nmodl::ast::Program
Represents top level AST node for whole NMODL input.
Definition: program.hpp:39
nmodl::parser::UnitDriver
Class that binds all pieces together for parsing C units.
Definition: unit_driver.hpp:39
nmodl::visitor::UnitsVisitor::UnitsVisitor
UnitsVisitor(std::string t_units_dir)
UnitsVisitor constructor that takes as argument the units file to parse the units from.
Definition: units_visitor.hpp:64
ast_visitor.hpp
Concrete visitor for all AST classes.