User Guide
c11_driver.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 #include <algorithm>
11 #include <map>
12 #include <memory>
13 #include <string>
14 #include <vector>
15 
16 
17 namespace nmodl {
18 namespace parser {
19 
20 /// flex generated scanner class (extends base lexer class of flex)
21 class CLexer;
22 
23 /// parser class generated by bison
24 class CParser;
25 
26 class location;
27 
28 /**
29  * \addtogroup parser
30  * \{
31  */
32 
33 /**
34  * \class CDriver
35  * \brief Class that binds all pieces together for parsing C verbatim blocks
36  */
37 class CDriver {
38  private:
39  /// all typedefs
40  std::map<std::string, std::string> typedefs;
41 
42  /// constants defined in enum
43  std::vector<std::string> enum_constants;
44 
45  /// all tokens encountered
46  std::vector<std::string> tokens;
47 
48  /// enable debug output in the flex scanner
49  bool trace_scanner = false;
50 
51  /// enable debug output in the bison parser
52  bool trace_parser = false;
53 
54  /// pointer to the lexer instance being used
55  std::unique_ptr<CLexer> lexer;
56 
57  /// pointer to the parser instance being used
58  std::unique_ptr<CParser> parser;
59 
60  /// print messages from lexer/parser
61  bool verbose = false;
62 
63  public:
64  /// file or input stream name (used by scanner for position), see todo
65  std::string streamname;
66 
67  CDriver();
68  CDriver(bool strace, bool ptrace);
69  ~CDriver();
70 
71  static void error(const std::string& m);
72 
73  bool parse_stream(std::istream& in);
74  bool parse_string(const std::string& input);
75  bool parse_file(const std::string& filename);
76  void scan_string(const std::string& text);
77  void add_token(const std::string&);
78 
79  static void error(const std::string& m, const location& l);
80 
81  void set_verbose(bool b) noexcept {
82  verbose = b;
83  }
84 
85  bool is_verbose() const noexcept {
86  return verbose;
87  }
88 
89  bool is_typedef(const std::string& type) const noexcept {
90  return typedefs.find(type) != typedefs.end();
91  }
92 
93  bool is_enum_constant(const std::string& constant) const noexcept {
94  return std::find(enum_constants.begin(), enum_constants.end(), constant) !=
95  enum_constants.end();
96  }
97 
98  const std::vector<std::string>& all_tokens() const noexcept {
99  return tokens;
100  }
101 
102  bool has_token(const std::string& token) const noexcept {
103  if (std::find(tokens.begin(), tokens.end(), token) != tokens.end()) {
104  return true;
105  }
106  return false;
107  }
108 };
109 
110 /** \} */ // end of parser
111 
112 } // namespace parser
113 } // namespace nmodl
nmodl::parser::CDriver::is_verbose
bool is_verbose() const noexcept
Definition: c11_driver.hpp:85
nmodl::parser::CDriver::parse_string
bool parse_string(const std::string &input)
parser c provided as string (used for testing)
Definition: c11_driver.cpp:47
nmodl::parser::CDriver::is_enum_constant
bool is_enum_constant(const std::string &constant) const noexcept
Definition: c11_driver.hpp:93
nmodl::parser::CDriver::CDriver
CDriver()
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::parser::CDriver::parse_stream
bool parse_stream(std::istream &in)
parse c file provided as istream
Definition: c11_driver.cpp:26
nmodl::parser::CDriver::set_verbose
void set_verbose(bool b) noexcept
Definition: c11_driver.hpp:81
nmodl::parser::CDriver
Class that binds all pieces together for parsing C verbatim blocks.
Definition: c11_driver.hpp:37
nmodl::parser::CDriver::enum_constants
std::vector< std::string > enum_constants
constants defined in enum
Definition: c11_driver.hpp:43
nmodl::parser::CDriver::has_token
bool has_token(const std::string &token) const noexcept
Definition: c11_driver.hpp:102
nmodl::parser::CDriver::parse_file
bool parse_file(const std::string &filename)
Definition: c11_driver.cpp:36
nmodl::parser::CDriver::streamname
std::string streamname
file or input stream name (used by scanner for position), see todo
Definition: c11_driver.hpp:65
nmodl::parser::CDriver::all_tokens
const std::vector< std::string > & all_tokens() const noexcept
Definition: c11_driver.hpp:98
nmodl::parser::CDriver::error
static void error(const std::string &m)
Definition: c11_driver.cpp:52
nmodl::parser::CDriver::trace_scanner
bool trace_scanner
enable debug output in the flex scanner
Definition: c11_driver.hpp:49
nmodl::parser::CDriver::parser
std::unique_ptr< CParser > parser
pointer to the parser instance being used
Definition: c11_driver.hpp:58
nmodl::parser::CDriver::tokens
std::vector< std::string > tokens
all tokens encountered
Definition: c11_driver.hpp:46
nmodl::parser::CDriver::trace_parser
bool trace_parser
enable debug output in the bison parser
Definition: c11_driver.hpp:52
nmodl::parser::CDriver::typedefs
std::map< std::string, std::string > typedefs
all typedefs
Definition: c11_driver.hpp:40
nmodl::parser::CDriver::is_typedef
bool is_typedef(const std::string &type) const noexcept
Definition: c11_driver.hpp:89
nmodl::parser::CDriver::~CDriver
~CDriver()
nmodl::parser::CDriver::lexer
std::unique_ptr< CLexer > lexer
pointer to the lexer instance being used
Definition: c11_driver.hpp:55
nmodl::parser::CDriver::verbose
bool verbose
print messages from lexer/parser
Definition: c11_driver.hpp:61
nmodl::parser::CDriver::scan_string
void scan_string(const std::string &text)
Definition: c11_driver.cpp:65
nmodl::parser::CDriver::add_token
void add_token(const std::string &)
Definition: c11_driver.cpp:56