summaryrefslogtreecommitdiffstats
path: root/manifest_parser.h
blob: 0dc0c8e71e3a0f73c4b6300f1ca6c5582a2d9223 (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
279
#include <errno.h>
#include <stdio.h>
#include <string.h>

struct Parser {
  Parser() : line_number_(1) {}

  void Start(const char* start, const char* end);
  bool Error(const string& message, string* err);

  const string& token() const { return token_; }
  bool eof() const { return cur_ >= end_; }

  void SkipWhitespace(bool newline=false);
  bool Newline(string* err);
  bool Token(const string& expected, string* err);
  bool ReadToken(string* out);
  bool ReadToNewline(string* text, string* err);

  bool PeekToken();
  void AdvanceToken();

  const char* cur_line_;
  const char* cur_;
  const char* end_;
  int line_number_;
  string token_;
  const char* err_start_;
};

void Parser::Start(const char* start, const char* end) {
  cur_line_ = cur_ = start;
  end_ = end;
}

bool Parser::Error(const string& message, string* err) {
  char buf[1024];
  sprintf(buf, "line %d, col %d: %s",
          line_number_,
          (int)(err_start_ - cur_line_) + 1,
          message.c_str());
  err->assign(buf);
  return false;
}

void Parser::SkipWhitespace(bool newline) {
  while (cur_ < end_) {
    if (*cur_ == ' ') {
      ++cur_;
    } else if (newline && *cur_ == '\n') {
      Newline(NULL);
    } else {
      break;
    }
  }
}

bool Parser::Newline(string* err) {
  err_start_ = cur_;
  if (cur_ < end_ && *cur_ == '\n') {
    ++cur_;
    cur_line_ = cur_;
    ++line_number_;
    token_.clear();
    return true;
  }

  if (err) {
    if (cur_ >= end_)
      return Error("expected newline, got eof", err);
    else
      return Error(string("expected newline, got '") + *cur_ + string("'"), err);
  }
  return false;
}

static bool IsIdentChar(char c) {
  return
    ('a' <= c && c <= 'z') ||
    ('+' <= c && c <= '9') ||  // +,-./ and numbers
    ('A' <= c && c <= 'Z') ||
    (c == '_');
}

bool Parser::Token(const string& expected, string* err) {
  PeekToken();
  if (token_ != expected)
    return Error("expected '" + expected + "', got '" + token_ + "'", err);
  AdvanceToken();
  return true;
}

bool Parser::ReadToken(string* out) {
  if (!PeekToken())
    return false;
  out->assign(token_);
  AdvanceToken();
  return true;
}

bool Parser::ReadToNewline(string* text, string* err) {
  token_.clear();
  while (cur_ < end_ && *cur_ != '\n') {
    text->push_back(*cur_);
    ++cur_;
  }
  return Newline(err);
}

bool Parser::PeekToken() {
  if (!token_.empty())
    return true;

  if (cur_ >= end_)
    return false;

  err_start_ = cur_;
  if (IsIdentChar(*cur_)) {
    while (cur_ < end_ && IsIdentChar(*cur_)) {
      token_.push_back(*cur_);
      ++cur_;
    }
  } else if (*cur_ == ':' || *cur_ == '=') {
    token_ = *cur_;
    ++cur_;
  }

  SkipWhitespace();

  return !token_.empty();
}

void Parser::AdvanceToken() {
  token_.clear();
}

struct ManifestParser {
  ManifestParser(State* state) : state_(state) {}
  bool Load(const string& filename, string* err);
  bool Parse(const string& input, string* err);

  bool ParseRule(string* err);
  bool ParseLet(string* err);
  bool ParseEdge(string* err);

  State* state_;
  Parser parser_;
};

bool ManifestParser::Load(const string& filename, string* err) {
  FILE* f = fopen(filename.c_str(), "r");
  if (!f) {
    err->assign(strerror(errno));
    return false;
  }

  string text;
  char buf[64 << 10];
  size_t len;
  while ((len = fread(buf, 1, sizeof(buf), f)) > 0) {
    text.append(buf, len);
  }
  if (ferror(f)) {
    err->assign(strerror(errno));
    fclose(f);
    return false;
  }
  fclose(f);

  return Parse(text, err);
}

bool ManifestParser::Parse(const string& input, string* err) {
  parser_.Start(input.data(), input.data() + input.size());

  parser_.SkipWhitespace(true);

  while (parser_.PeekToken()) {
    if (parser_.token() == "rule") {
      if (!ParseRule(err))
        return false;
    } else if (parser_.token() == "let") {
      if (!ParseLet(err))
        return false;
    } else if (parser_.token() == "build") {
      if (!ParseEdge(err))
        return false;
    } else {
      return parser_.Error("unknown token: " + parser_.token(), err);
    }
    parser_.SkipWhitespace(true);
  }

  if (!parser_.eof())
    return parser_.Error("expected eof", err);

  return true;
}

bool ManifestParser::ParseRule(string* err) {
  if (!parser_.Token("rule", err))
    return false;
  string name;
  if (!parser_.ReadToken(&name))
    return parser_.Error("expected rule name", err);
  if (!parser_.Newline(err))
    return false;

  if (!parser_.Token("command", err))
    return false;
  string command;
  if (!parser_.ReadToNewline(&command, err))
    return false;

  state_->AddRule(name, command);

  return true;
}

bool ManifestParser::ParseLet(string* err) {
  if (!parser_.Token("let", err))
    return false;

  string name;
  if (!parser_.ReadToken(&name))
    return parser_.Error("expected variable name", err);

  if (!parser_.Token("=", err))
    return false;

  string value;
  if (!parser_.ReadToNewline(&value, err))
    return false;

  state_->AddBinding(name, value);

  return true;
}

bool ManifestParser::ParseEdge(string* err) {
  vector<string> ins, outs;

  if (!parser_.Token("build", err))
    return false;

  for (;;) {
    string out;
    if (!parser_.ReadToken(&out))
      return parser_.Error("expected output file list", err);
    if (out == ":")
      break;
    outs.push_back(out);
  }

  string rule;
  if (!parser_.ReadToken(&rule))
    return parser_.Error("expected build command name", err);

  for (;;) {
    string in;
    if (!parser_.ReadToken(&in))
      break;
    ins.push_back(in);
  }

  if (!parser_.Newline(err))
    return false;

  Edge* edge = state_->AddEdge(rule);
  if (!edge)
    return parser_.Error("unknown build rule name name", err);
  for (vector<string>::iterator i = ins.begin(); i != ins.end(); ++i)
    state_->AddInOut(edge, Edge::IN, *i);
  for (vector<string>::iterator i = outs.begin(); i != outs.end(); ++i)
    state_->AddInOut(edge, Edge::OUT, *i);

  return true;
}