summaryrefslogtreecommitdiff
path: root/elpa/irony-20220110.849/server/src/Command.cpp
blob: 363b6cb0b5f4237eaec36cd862de12e6e9d43efe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
 * \file
 * \author Guillaume Papin <guillaume.papin@epitech.eu>
 *
 * \brief Command parser definitions.
 *
 * This file is distributed under the GNU General Public License. See
 * COPYING for details.
 */

#include "Command.h"

#include "support/CommandLineParser.h"

#include <algorithm>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <limits>
#include <map>


namespace {

struct StringConverter {
  StringConverter(std::string *dest) : dest_(dest) {
  }

  bool operator()(const std::string &str) {
    *dest_ = str;
    return true;
  }

private:
  std::string *dest_;
};

struct UnsignedIntConverter {
  UnsignedIntConverter(unsigned *dest) : dest_(dest) {
  }

  bool operator()(const std::string &str) {
    char *end;
    long num = std::strtol(str.c_str(), &end, 10);

    if (end != (str.c_str() + str.size()))
      return false;

    if (errno == ERANGE)
      return false;

    if (num < 0)
      return false;

    unsigned long unum = static_cast<unsigned long>(num);
    if (unum > std::numeric_limits<unsigned>::max())
      return false;

    *dest_ = unum;
    return true;
  }

private:
  unsigned *dest_;
};

/// Convert "on" and "off" to a boolean
struct OptionConverter {
  OptionConverter(bool *dest) : dest_(dest) {
  }

  bool operator()(const std::string &str) {
    if (str == "on") {
      *dest_ = true;
    } else if (str == "off") {
      *dest_ = false;
    } else {
      return false;
    }
    return true;
  }

private:
  bool *dest_;
};

const std::map<std::string, PrefixMatchStyle> PREFIX_MATCH_STYLE_MAP = {
  { "exact", PrefixMatchStyle::Exact },
  { "case-insensitive", PrefixMatchStyle::CaseInsensitive },
  { "smart-case", PrefixMatchStyle::SmartCase},
};

/// Convert style to a PrefixMatchStyle
struct PrefixMatchStyleConverter {
  PrefixMatchStyleConverter(PrefixMatchStyle *dest) : dest_(dest) {
  }

  bool operator()(const std::string &str) {
    auto res = PREFIX_MATCH_STYLE_MAP.find(str);

    if (res == PREFIX_MATCH_STYLE_MAP.cend()) {
      return false;
    }
    *dest_ = res->second;
    return true;
  }

private:
  PrefixMatchStyle *dest_;
};

std::ostream &operator<<(std::ostream &os, PrefixMatchStyle style) {
  for (auto it : PREFIX_MATCH_STYLE_MAP) {
    if (it.second == style) {
      os << it.first;
      return os;
    }
  }
  os << "UnknownStyle";
  return os;
}


} // unnamed namespace

std::ostream &operator<<(std::ostream &os, const Command::Action &action) {
  os << "Command::";

  switch (action) {
#define X(sym, str, help)                                                      \
  case Command::sym:                                                           \
    os << #sym;                                                                \
    break;
#include "Commands.def"
  }
  return os;
}

std::ostream &operator<<(std::ostream &os, const Command &command) {
  os << "Command{action=" << command.action << ", "
     << "file='" << command.file << "', "
     << "unsavedFile='" << command.unsavedFile << "', "
     << "dir='" << command.dir << "', "
     << "line=" << command.line << ", "
     << "column=" << command.column << ", "
     << "prefix='" << command.prefix << "', "
     << "caseStyle='" << command.style << "', "
     << "flags=[";
  bool first = true;
  for (const std::string &flag : command.flags) {
    if (!first)
      os << ", ";
    os << "'" << flag << "'";
    first = false;
  }
  os << "], "
     << "opt=" << (command.opt ? "on" : "off");

  return os << "}";
}

static Command::Action actionFromString(const std::string &actionStr) {
#define X(sym, str, help)                                                      \
  if (actionStr == str)                                                        \
    return Command::sym;

#include "Commands.def"

  return Command::Unknown;
}

CommandParser::CommandParser() : tempFile_("irony-server") {
}

Command *CommandParser::parse(const std::vector<std::string> &argv) {
  command_.clear();

  if (argv.begin() == argv.end()) {
    std::clog << "error: no command specified.\n"
                 "See 'irony-server help' to list available commands\n";
    return 0;
  }

  const std::string &actionStr = argv[0];

  command_.action = actionFromString(actionStr);

  bool readCompileOptions = false;
  std::vector<std::function<bool(const std::string &)>> positionalArgs;

  switch (command_.action) {
  case Command::SetDebug:
    positionalArgs.push_back(OptionConverter(&command_.opt));
    break;

  case Command::Parse:
    positionalArgs.push_back(StringConverter(&command_.file));
    readCompileOptions = true;
    break;

  case Command::Complete:
    positionalArgs.push_back(StringConverter(&command_.file));
    positionalArgs.push_back(UnsignedIntConverter(&command_.line));
    positionalArgs.push_back(UnsignedIntConverter(&command_.column));
    readCompileOptions = true;
    break;

  case Command::GetType:
    positionalArgs.push_back(UnsignedIntConverter(&command_.line));
    positionalArgs.push_back(UnsignedIntConverter(&command_.column));
    break;

  case Command::SetUnsaved:
    positionalArgs.push_back(StringConverter(&command_.file));
    positionalArgs.push_back(StringConverter(&command_.unsavedFile));
    break;

  case Command::ResetUnsaved:
    positionalArgs.push_back(StringConverter(&command_.file));
    break;

  case Command::Candidates:
    positionalArgs.push_back(StringConverter(&command_.prefix));
    positionalArgs.push_back(PrefixMatchStyleConverter(&command_.style));
    break;
  case Command::CompletionDiagnostics:
  case Command::Diagnostics:
  case Command::Help:
  case Command::Exit:
    // no-arguments commands
    break;

  case Command::GetCompileOptions:
    positionalArgs.push_back(StringConverter(&command_.dir));
    positionalArgs.push_back(StringConverter(&command_.file));
    break;

  case Command::Unknown:
    std::clog << "error: invalid command specified: " << actionStr << "\n";
    return 0;
  }

  auto argsBegin = argv.begin() + 1;
  const auto argsEnd = std::find(argsBegin, argv.end(), "--");
  const int argCount = std::distance(argsBegin, argsEnd);

  // compile options are provided after '--'
  if (readCompileOptions && argsEnd != argv.end()) {
    command_.flags.assign(std::next(argsEnd), argv.end());
  }

  if (argCount != static_cast<int>(positionalArgs.size())) {
    std::clog << "error: invalid number of arguments for '" << actionStr
              << "' (requires " << positionalArgs.size() << " got " << argCount
              << ")\n";
    return 0;
  }

  for (auto fn : positionalArgs) {
    if (!fn(*argsBegin)) {
      std::clog << "error: parsing command '" << actionStr
                << "': invalid argument '" << *argsBegin << "'\n";
      return 0;
    }
    ++argsBegin;
  }

  // '-' is used as a special file to inform that the buffer hasn't been saved
  // on disk and only the buffer content is available. libclang needs a file, so
  // this is treated as a special value for irony-server to create a temporary
  // file for this. note that libclang will gladly accept '-' as a filename but
  // we don't want to let this happen since irony already reads stdin.
  if (command_.file == "-") {
    command_.file = tempFile_.getPath();
  }

  return &command_;
}