-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cpp
60 lines (51 loc) · 1.37 KB
/
parser.cpp
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
#include "parser.h"
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
// trim from start
static inline std::string <rim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end
static inline std::string &rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
// trim from both ends
static inline std::string &trim(std::string &s) {
return ltrim(rtrim(s));
}
Parser::Parser(istream& inputFile) : inputStream(inputFile)
{
}
vector<Target> Parser::parse()
{
vector<Target> resultTargets;
string line;
bool in_target = false;
while (!inputStream.eof())
{
getline (inputStream,line);
Target t;
int pos = line.find(":");
if (pos != string::npos)
{
t.name = line.substr(0,pos);
string dep = line.substr(pos+1);
t.dep_string = trim(dep);
in_target = true;
resultTargets.push_back(t);
continue;
}
// if we are in a target an the line starts
// with a tab this must be part of the rule
if (in_target && line[0] == '\t')
{
Target ¤t_target = resultTargets.back();
current_target.rule += trim(line);
}
}
return resultTargets;
}