OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
ArgumentExtractor.cpp
Go to the documentation of this file.
2 
3 #include <stdexcept>
4 
5 namespace mslang {
6  ArgumentExtractor::ArgumentExtractor(const std::string &input):
7  inputArguments_m(input)
8  {
9  const unsigned int length = input.length();
10  unsigned int pos = 0;
11  unsigned short level = 1;
12  unsigned int start = 0;
13 
14  for (; pos < length; ++ pos) {
15  if (input[pos] == ')') {
16  -- level;
17  if (level == 0) {
18  argumentBoundaries_m.push_back(std::make_pair(start, pos - start));
19  break;
20  }
21  }
22  else if (input[pos] == '(')
23  ++ level;
24  else if ((input[pos] == ','
25  || input[pos] == ';')
26  && level == 1) {
27  argumentBoundaries_m.push_back(std::make_pair(start, pos - start));
28  start = pos + 1;
29  }
30  }
31 
32  inputArguments_m = inputArguments_m.substr(0, pos);
33  lengthConsumed_m = pos;
34  }
35 
36  std::string ArgumentExtractor::get(unsigned int i) const {
37  if (i >= argumentBoundaries_m.size())
38  throw std::out_of_range("function only has " + std::to_string(argumentBoundaries_m.size()) + " arguments");
39 
40  auto boundaries = argumentBoundaries_m[i];
41 
42  return inputArguments_m.substr(boundaries.first, boundaries.second);
43  }
44 
45  unsigned int ArgumentExtractor::getLengthConsumed() const {
46  return lengthConsumed_m;
47  }
48 
49  unsigned int ArgumentExtractor::getNumArguments() const {
50  return argumentBoundaries_m.size();
51  }
52 
53 }
unsigned int getNumArguments() const
unsigned int getLengthConsumed() const
ArgumentExtractor(const std::string &input)
std::string get(unsigned int i) const
std::vector< std::pair< unsigned int, unsigned int > > argumentBoundaries_m