14 #include "utils/logger.hpp"
16 namespace fs = std::filesystem;
22 : trace_scanner(strace)
23 , trace_parser(ptrace) {}
28 NmodlParser parser(scanner, *
this);
45 if (fs::is_directory(filename)) {
46 throw std::runtime_error(
"NMODL Parser Error : path " + filename.string() +
47 " appears to be a directory, please provide a file instead");
49 std::ifstream in(filename);
51 std::ostringstream oss;
53 oss <<
"NMODL Parser Error : ";
55 oss <<
"can not open file : " << filename;
59 throw std::runtime_error(oss.str());
65 auto absolute_path = fs::absolute(filename);
67 if (filename.is_absolute()) {
68 const auto path_prefix = filename.parent_path();
70 absolute_path = filename.string();
71 }
else if (!filename.has_parent_path()) {
74 const auto path_prefix = filename.parent_path();
75 const auto path = fs::absolute(path_prefix);
90 std::istringstream iss(input);
105 auto absolute_path =
name;
107 if (!directory_path.empty()) {
108 absolute_path = directory_path /
name;
112 auto already_included =
open_files.find(absolute_path.string());
114 std::ostringstream oss;
115 oss <<
name <<
": recursive inclusion.\n";
116 if (already_included->second !=
nullptr) {
117 oss << *already_included->second <<
": initial inclusion was here.";
122 std::shared_ptr<ast::Program> program;
128 auto filename_node = std::shared_ptr<ast::String>(
130 return std::shared_ptr<ast::Include>(
new ast::Include(filename_node, program->get_blocks()));
144 return var_it->second;
146 throw std::runtime_error(
"Trying to get undefined macro / define :" +
name);
150 std::ostringstream oss;
151 oss <<
"NMODL Parser Error : " << message <<
" [Location : " <<
location <<
"]";
152 throw std::runtime_error(oss.str());
157 const std::string& message) {
158 std::ostringstream oss;
159 oss <<
"NMODL Parser Error : " << message <<
" [Location : " <<
location <<
"]";
161 oss << std::string(
location.begin.column - 1,
'-');
165 throw std::runtime_error(oss.str());
169 const std::string& filename) {
170 if (filename.empty()) {
172 }
else if (filename.front() !=
'"' && filename.back() !=
'"') {
174 }
else if (filename.size() == 3) {
177 return filename.substr(1, filename.size() - 2);
std::string find_file(const std::filesystem::path &file)
Search a file.
void push_current_directory(const std::filesystem::path &path)
void pop_current_directory()
Represents an INCLUDE statement in NMODL.
std::shared_ptr< ast::Include > parse_include(const std::filesystem::path &filename, const location &loc)
void parse_error(const location &location, const std::string &message)
Emit a parsing error.
std::shared_ptr< ast::Program > astRoot
root of the ast
std::shared_ptr< ast::Program > parse_file(const std::filesystem::path &filename, const location *loc=nullptr)
parse NMODL file
FileLibrary library
The file library for IMPORT directives.
std::shared_ptr< ast::Program > parse_string(const std::string &input)
parser nmodl provided as string (used for testing)
bool trace_scanner
enable debug output in the flex scanner
int get_defined_var_value(const std::string &name) const
return variable's value defined as macro (always an integer)
void add_defined_var(const std::string &name, int value)
add macro definition and it's value (DEFINE keyword of nmodl)
std::string stream_name
file or input stream name (used by scanner for position), see todo
std::unordered_map< std::string, const location * > open_files
The list of open files, and the location of the request.
bool is_defined_var(const std::string &name) const
check if particular text is defined as macro
std::string check_include_argument(const location &location, const std::string &filename)
Ensure file argument given to the INCLUDE directive is valid:
std::unordered_map< std::string, int > defined_var
all macro defined in the mod file
bool trace_parser
enable debug output in the bison parser
std::ostringstream parser_stream
The stream where Bison will dump its logs.
std::shared_ptr< ast::Program > parse_stream(std::istream &in)
parse nmodl file provided as istream
Represent Lexer/Scanner class for NMODL language parsing.
void set_debug(bool b)
Enable debug output (via yyout) if compiled into the scanner.
std::string get_curr_line() const
Return current line as string.
static double location(void *v)
encapsulates code generation backend implementations