shushus

Спринт 12. Свой препроцессор с регулярными выражениями и сырыми литералами.

Apr 6th, 2023 (edited)
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.92 KB | None | 0 0
  1. #include <cassert>
  2. #include <filesystem>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <regex>
  6. #include <sstream>
  7. #include <string>
  8. #include <string_view>
  9. #include <vector>
  10.  
  11. using namespace std;
  12. using filesystem::path;
  13.  
  14. const regex LIBRE{R"/(\s*#\s*include\s*<([^>]*)>\s*)/"};
  15. const regex HEADER{R"/(\s*#\s*include\s*"([^"]*)"\s*)/"};
  16.  
  17. path operator""_p(const char* data, std::size_t sz) {return path(data, data + sz);}
  18.  
  19. bool Preprocess(const path& file_path, const vector<path>& include_directories, istream& source, ostream& destination) {
  20.    string line = ""s;
  21.    int line_number = 1;
  22.  
  23.    while (getline(source, line)) {
  24.        smatch m, m_1;
  25.        
  26.        if (!regex_match(line, m_1, LIBRE) && !regex_match(line, m, HEADER)) {
  27.            destination << line << endl;
  28.            
  29.        } else {
  30.            
  31.            ifstream input;          
  32.            path path, path_1;
  33.  
  34.            if (m.empty()) {
  35.                path = string(m_1[1]);
  36.                
  37.            } else {
  38.                path = string(m[1]);
  39.                path_1 = file_path.parent_path()/path;
  40.                input.open(path_1);
  41.            }
  42.  
  43.            for (const auto& entry_dir : include_directories) {
  44.                
  45.                if (input.is_open() && input) {
  46.                    break;
  47.                }
  48.                
  49.                path_1 = entry_dir / path;
  50.                input.open(path_1);
  51.            }
  52.  
  53.            if (!input) {
  54.                cout << "unknown include file "sv << path.string()
  55.                     << " at file "sv << file_path.string()
  56.                     << " at line "sv << line_number << endl;
  57.                
  58.                return false;
  59.            }
  60.            
  61.            if (!Preprocess(path_1, include_directories, input, destination)) {
  62.                return false;
  63.            }
  64.        }
  65.        
  66.        ++line_number;
  67.    }
  68.  
  69.    return true;
  70. }
  71.  
  72. bool Preprocess(const path& in_file, const path& out_file, const vector<path>& include_directories) {
  73.    
  74.    ifstream input(in_file);
  75.    if (!input) {
  76.        cerr << in_file << endl;
  77.        return false;
  78.    }
  79.    
  80.    ofstream output(out_file);
  81.    if (!output) {
  82.        cerr << out_file << endl;
  83.        return false;
  84.    }
  85.  
  86.    return Preprocess(in_file, include_directories, input, output);
  87. }
  88.  
  89. string GetFileContents(string file) {
  90.    ifstream stream(file);
  91.  
  92.    return {(istreambuf_iterator<char>(stream)), istreambuf_iterator<char>()};
  93. }
  94.  
  95. void Test() {
  96.    error_code err;
  97.    filesystem::remove_all("sources"_p, err);
  98.    filesystem::create_directories("sources"_p / "include2"_p / "lib"_p, err);
  99.    filesystem::create_directories("sources"_p / "include1"_p, err);
  100.    filesystem::create_directories("sources"_p / "dir1"_p / "subdir"_p, err);
  101.  
  102.    {
  103.        ofstream file("sources/a.cpp");
  104.        file << "// this comment before include\n"
  105.                 "#include \"dir1/b.h\"\n"
  106.                 "// text between b.h and c.h\n"
  107.                 "#include \"dir1/d.h\"\n"
  108.                 "\n"
  109.                 "int SayHello() {\n"
  110.                 "    cout << \"hello, world!\" << endl;\n"
  111.                 "#   include<dummy.txt>\n"
  112.                 "}\n"sv;
  113.     }
  114.     {
  115.         ofstream file("sources/dir1/b.h");
  116.         file << "// text from b.h before include\n"
  117.                 "#include \"subdir/c.h\"\n"
  118.                 "// text from b.h after include"sv;
  119.     }
  120.     {
  121.         ofstream file("sources/dir1/subdir/c.h");
  122.         file << "// text from c.h before include\n"
  123.                 "#include <std1.h>\n"
  124.                 "// text from c.h after include\n"sv;
  125.     }
  126.     {
  127.         ofstream file("sources/dir1/d.h");
  128.         file << "// text from d.h before include\n"
  129.                 "#include \"lib/std2.h\"\n"
  130.                 "// text from d.h after include\n"sv;
  131.     }
  132.     {
  133.         ofstream file("sources/include1/std1.h");
  134.         file << "// std1\n"sv;
  135.     }
  136.     {
  137.         ofstream file("sources/include2/lib/std2.h");
  138.         file << "// std2\n"sv;
  139.     }
  140.  
  141.     assert((!Preprocess("sources"_p / "a.cpp"_p, "sources"_p / "a.in"_p,
  142.                                   {"sources"_p / "include1"_p,"sources"_p / "include2"_p})));
  143.  
  144.     ostringstream test_out;
  145.     test_out << "// this comment before include\n"
  146.                 "// text from b.h before include\n"
  147.                 "// text from c.h before include\n"
  148.                 "// std1\n"
  149.                 "// text from c.h after include\n"
  150.                 "// text from b.h after include\n"
  151.                 "// text between b.h and c.h\n"
  152.                 "// text from d.h before include\n"
  153.                 "// std2\n"
  154.                 "// text from d.h after include\n"
  155.                 "\n"
  156.                 "int SayHello() {\n"
  157.                 "    cout << \"hello, world!\" << endl;\n"sv;
  158.  
  159.     assert(GetFileContents("sources/a.in"s) == test_out.str());
  160. }
  161.  
  162. int main() {
  163.     Test();
  164. }
Advertisement
Add Comment
Please, Sign In to add comment