User Guide
main_c.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 
8 #include <sstream>
9 
10 #include "config/config.h"
11 #include "lexer/c11_lexer.hpp"
12 #include "parser/c11_driver.hpp"
13 #include "utils/logger.hpp"
14 
15 // CLI11 includes Windows specific headers with macros that will interfere with the lexer, include
16 // it last.
17 #include <CLI/CLI.hpp>
18 
19 /**
20  * \file
21  * \brief Example of standalone lexer program for C code
22  *
23  * This example demonstrate use of CLexer and CDriver classes
24  * to scan arbitrary C code.
25  */
26 
27 using namespace nmodl;
28 using Token = parser::CParser::token;
29 
30 void scan_c_code(std::istream& in) {
32  nmodl::parser::CLexer scanner(driver, &in);
33 
34  /// parse C file and print token until EOF
35  while (true) {
36  auto sym = scanner.next_token();
37  auto token_type = sym.type_get();
38  if (token_type == parser::CParser::by_type(Token::END).type_get()) {
39  break;
40  }
41  std::cout << sym.value.as<std::string>() << std::endl;
42  }
43 }
44 
45 
46 int main(int argc, const char* argv[]) {
47  CLI::App app{fmt::format("C-Lexer : Standalone Lexer for C Code({})", Version::to_string())};
48 
49  std::vector<std::string> c_files;
50  std::vector<std::string> c_codes;
51 
52  app.add_option("file", c_files, "One or more C files to process")->check(CLI::ExistingFile);
53  app.add_option("--text", c_codes, "One or more C code as text");
54 
55  CLI11_PARSE(app, argc, argv);
56 
57  for (const auto& file: c_files) {
58  nmodl::logger->info("Processing {}", file);
59  std::ifstream in(file);
60  scan_c_code(in);
61  }
62 
63  for (const auto& code: c_codes) {
64  nmodl::logger->info("Processing {}", code);
65  std::istringstream in(code);
66  scan_c_code(in);
67  }
68 
69  return 0;
70 }
nmodl::parser::CLexer
Represent Lexer/Scanner class for C (11) language parsing.
Definition: c11_lexer.hpp:50
main
int main(int argc, const char *argv[])
Definition: main_c.cpp:46
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::logger
logger_type logger
Definition: logger.cpp:34
nmodl::parser::CDriver
Class that binds all pieces together for parsing C verbatim blocks.
Definition: c11_driver.hpp:37
driver
nmodl::parser::UnitDriver driver
Definition: parser.cpp:28
c11_driver.hpp
nmodl::Version::to_string
static std::string to_string()
return version string (version + git id) as a string
Definition: config.h:39
Token
parser::CParser::token Token
Definition: main_c.cpp:28
scan_c_code
void scan_c_code(std::istream &in)
Definition: main_c.cpp:30
logger.hpp
Implement logger based on spdlog library.
config.h
Version information and units file path.
nmodl::parser::CLexer::next_token
virtual CParser::symbol_type next_token()
Function for lexer to scan token (replacement for yylex())
c11_lexer.hpp
nmodl::token_type
TokenType token_type(const std::string &name)
Return token type for given token name.
Definition: token_mapping.cpp:284