1 #include <sngcpp/pp/InitDone.hpp>
  2 #include <sngcpp/ast/InitDone.hpp>
  3 #include <soulng/util/InitDone.hpp>
  4 #include <soulng/util/Path.hpp>
  5 #include <soulng/util/TextUtils.hpp>
  6 #include <soulng/util/Unicode.hpp>
  7 #include <soulng/util/CodeFormatter.hpp>
  8 #include <sngcpp/pp/PP.hpp>
  9 #include <iostream>
 10 #include <stdexcept>
 11 #include <vector>
 12 
 13 using namespace soulng::util;
 14 using namespace soulng::unicode;
 15 using namespace sngcpp::pp;
 16 
 17 struct Initializer 
 18 {
 19     Initializer()
 20     {
 21         soulng::util::Init();
 22         sngcpp::pp::Init();
 23         sngcpp::ast::Init();
 24     }
 25     ~Initializer()
 26     {
 27         sngcpp::ast::Done();
 28         sngcpp::pp::Done();
 29         soulng::util::Done();
 30     }
 31 };
 32 
 33 void PrintUsage()
 34 {
 35     std::cout << "sngcpp preprocessor tester" << std::endl;
 36     std::cout << "usage: pptester [options] { <file.cpp> | <file.hpp> }" << std::endl;
 37 }
 38 
 39 int main(int argcconst char** argv)
 40 {
 41     Initializer initializer;
 42     try
 43     {
 44         std::vector<std::string> fileNames;
 45         std::string root;
 46         std::string includePath;
 47         bool verbose = false;
 48         for (int i = 1; i < argc; ++i)
 49         {
 50             std::string arg = argv[i];
 51             if (StartsWith(arg"--"))
 52             {
 53                 if (arg == "--help")
 54                 {
 55                     PrintUsage();
 56                     return 1;
 57                 }
 58                 else if (arg == "--verbose")
 59                 {
 60                     verbose = true;
 61                 }
 62                 else if (arg == "--root")
 63                 {
 64                     ++i;
 65                     if (i < argc)
 66                     {
 67                         arg = argv[i];
 68                         root = GetFullPath(arg);
 69                     }
 70                     else
 71                     {
 72                         throw std::runtime_error("too few arguments");
 73                     }
 74                 }
 75                 else if (arg == "--include")
 76                 {
 77                     ++i;
 78                     if (i < argc)
 79                     {
 80                         arg = argv[i];
 81                         includePath = GetFullPath(arg);
 82                     }
 83                     else
 84                     {
 85                         throw std::runtime_error("too few arguments");
 86                     }
 87                 }
 88                 else
 89                 {
 90                     throw std::runtime_error("unknown option '" + arg + "'");
 91                 }
 92             }
 93             else if (StartsWith(arg"-"))
 94             {
 95                 std::string options = arg.substr(1);
 96                 if (options.empty())
 97                 {
 98                     throw std::runtime_error("unknown option '" + arg + "'");
 99                 }
100                 for (char o : options)
101                 {
102                     if (o == 'h')
103                     {
104                         PrintUsage();
105                         return 1;
106                     }
107                     else if (o == 'v')
108                     {
109                         verbose = true;
110                     }
111                     else
112                     {
113                         throw std::runtime_error("unknown option '-" + std::string(1o) + "'");
114                     }
115                 }
116             }
117             else
118             {
119                 fileNames.push_back(GetFullPath(arg));
120             }
121         }
122         EvaluationContext evaluationContext;
123         for (const std::string& fileName : fileNames)
124         {
125             PP pp(evaluationContext);
126             if (!root.empty())
127             {
128                 pp.root = root;
129                 pp.rootMode = true;
130             }
131             if (!includePath.empty())
132             {
133                 pp.includePath = Split(includePath';');
134             }
135             pp.Define(sngcpp::pp::ndebug);
136             if (verbose)
137             {
138                 pp.verbose = true;
139             }
140             Preprocess(fileName&pp);
141             WriteUtf8(std::coutToUtf8(pp.text));
142             std::cout << std::endl;
143         }
144     }
145     catch (const std::exception& ex;)
146     {
147         std::cerr << ex.what() << std::endl;
148         return 1;
149     }
150     return 0;
151 }