blob: 35557584a385f2f6662ca7ebb6e2b4756b91eaae [file] [log] [blame]
Shinichiro Hamaji1d545aa2015-06-23 15:29:13 +09001// Copyright 2015 Google Inc. All rights reserved
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Fumitoshi Ukai744bb2b2015-06-25 00:10:52 +090015// +build ignore
16
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090017#include "parser.h"
18
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +090019#include <stack>
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +090020#include <unordered_map>
21
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090022#include "ast.h"
23#include "file.h"
24#include "loc.h"
25#include "log.h"
Shinichiro Hamaji6b1a11a2015-06-30 16:21:19 +090026#include "stats.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090027#include "string_piece.h"
Shinichiro Hamaji810fd032015-06-17 04:38:03 +090028#include "strutil.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090029#include "value.h"
30
31enum struct ParserState {
32 NOT_AFTER_RULE = 0,
33 AFTER_RULE,
34 MAYBE_AFTER_RULE,
35};
36
37class Parser {
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +090038 struct IfState {
39 IfAST* ast;
40 bool is_in_else;
41 int num_nest;
42 };
43
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +090044 typedef void (Parser::*DirectiveHandler)(
45 StringPiece line, StringPiece directive);
46 typedef unordered_map<StringPiece, DirectiveHandler> DirectiveMap;
47
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090048 public:
49 Parser(StringPiece buf, const char* filename, vector<AST*>* asts)
50 : buf_(buf),
51 state_(ParserState::NOT_AFTER_RULE),
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +090052 asts_(asts),
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090053 out_asts_(asts),
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +090054 num_if_nest_(0),
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090055 loc_(filename, 0),
56 fixed_lineno_(false) {
57 }
58
Shinichiro Hamaji80456fb2015-06-18 14:56:10 +090059 Parser(StringPiece buf, const Loc& loc, vector<AST*>* asts)
60 : buf_(buf),
61 state_(ParserState::NOT_AFTER_RULE),
62 asts_(asts),
63 out_asts_(asts),
64 num_if_nest_(0),
65 loc_(loc),
66 fixed_lineno_(true) {
67 }
68
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090069 ~Parser() {
70 }
71
72 void Parse() {
73 l_ = 0;
74
75 for (l_ = 0; l_ < buf_.size();) {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090076 size_t lf_cnt = 0;
77 size_t e = FindEndOfLine(&lf_cnt);
Shinichiro Hamaji8ee8c372015-06-16 16:19:40 +090078 if (!fixed_lineno_)
Shinichiro Hamajib3af68b2015-06-24 20:47:57 +090079 loc_.lineno++;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090080 StringPiece line(buf_.data() + l_, e - l_);
Shinichiro Hamaji420f7752015-06-26 04:02:02 +090081 orig_line_with_directives_ = line;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090082 ParseLine(line);
Shinichiro Hamajib3af68b2015-06-24 20:47:57 +090083 if (!fixed_lineno_)
84 loc_.lineno += lf_cnt - 1;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090085 if (e == buf_.size())
86 break;
87
88 l_ = e + 1;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090089 }
90 }
91
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +090092 static void Init() {
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +090093 make_directives_ = new DirectiveMap;
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +090094 (*make_directives_)["include"] = &Parser::ParseInclude;
95 (*make_directives_)["-include"] = &Parser::ParseInclude;
96 (*make_directives_)["sinclude"] = &Parser::ParseInclude;
Shinichiro Hamaji810fd032015-06-17 04:38:03 +090097 (*make_directives_)["define"] = &Parser::ParseDefine;
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +090098 (*make_directives_)["ifdef"] = &Parser::ParseIfdef;
99 (*make_directives_)["ifndef"] = &Parser::ParseIfdef;
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900100 (*make_directives_)["ifeq"] = &Parser::ParseIfeq;
101 (*make_directives_)["ifneq"] = &Parser::ParseIfeq;
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900102 (*make_directives_)["else"] = &Parser::ParseElse;
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900103 (*make_directives_)["endif"] = &Parser::ParseEndif;
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900104 (*make_directives_)["override"] = &Parser::ParseOverride;
105 (*make_directives_)["export"] = &Parser::ParseExport;
106 (*make_directives_)["unexport"] = &Parser::ParseUnexport;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900107
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900108 else_if_directives_ = new DirectiveMap;
109 (*else_if_directives_)["ifdef"] = &Parser::ParseIfdef;
110 (*else_if_directives_)["ifndef"] = &Parser::ParseIfdef;
Shinichiro Hamaji6aeab2a2015-06-26 09:12:01 +0900111 (*else_if_directives_)["ifeq"] = &Parser::ParseIfeq;
112 (*else_if_directives_)["ifneq"] = &Parser::ParseIfeq;
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900113
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900114 assign_directives_ = new DirectiveMap;
115 (*assign_directives_)["define"] = &Parser::ParseDefine;
116 (*assign_directives_)["export"] = &Parser::ParseExport;
117 (*assign_directives_)["override"] = &Parser::ParseOverride;
118
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900119 shortest_directive_len_ = 9999;
120 longest_directive_len_ = 0;
121 for (auto p : *make_directives_) {
122 size_t len = p.first.size();
123 shortest_directive_len_ = min(len, shortest_directive_len_);
124 longest_directive_len_ = max(len, longest_directive_len_);
125 }
126 }
127
128 static void Quit() {
129 delete make_directives_;
130 }
131
Shinichiro Hamaji631a9f82015-07-05 14:18:15 +0900132 void set_state(ParserState st) { state_ = st; }
133
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900134 private:
135 void Error(const string& msg) {
Shinichiro Hamaji8ee8c372015-06-16 16:19:40 +0900136 ERROR("%s:%d: %s", LOCF(loc_), msg.c_str());
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900137 }
138
139 size_t FindEndOfLine(size_t* lf_cnt) {
Shinichiro Hamaji14bb2792015-06-25 18:24:11 +0900140 return ::FindEndOfLine(buf_, l_, lf_cnt);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900141 }
142
Shinichiro Hamaji36b326f2015-06-26 08:56:13 +0900143 Value* ParseExpr(StringPiece s, ParseExprOpt opt = ParseExprOpt::NORMAL) {
144 return ::ParseExpr(loc_, s, opt);
145 }
146
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900147 void ParseLine(StringPiece line) {
148 if (line.empty() || (line.size() == 1 && line[0] == '\r'))
149 return;
150
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900151 if (!define_name_.empty()) {
152 ParseInsideDefine(line);
153 return;
154 }
155
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900156 current_directive_ = AssignDirective::NONE;
157
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900158 if (line[0] == '\t' && state_ != ParserState::NOT_AFTER_RULE) {
159 CommandAST* ast = new CommandAST();
Shinichiro Hamaji861bd642015-06-19 16:59:13 +0900160 ast->set_loc(loc_);
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900161 ast->expr = ParseExpr(line.substr(1), ParseExprOpt::COMMAND);
Shinichiro Hamaji631a9f82015-07-05 14:18:15 +0900162 ast->orig = line;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900163 out_asts_->push_back(ast);
164 return;
165 }
166
Shinichiro Hamaji32750622015-06-17 14:57:33 +0900167 line = TrimLeftSpace(line);
Shinichiro Hamajid4e81932015-06-17 04:40:45 +0900168
169 if (line[0] == '#')
170 return;
171
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900172 if (HandleDirective(line, make_directives_)) {
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900173 return;
174 }
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900175
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900176 ParseRuleOrAssign(line);
177 }
178
179 void ParseRuleOrAssign(StringPiece line) {
Shinichiro Hamaji415a9b12015-07-05 14:33:02 +0900180 size_t sep = FindThreeOutsideParen(line, ':', '=', ';');
181 if (sep == string::npos || line[sep] == ';') {
182 ParseRule(line, string::npos);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900183 } else if (line[sep] == '=') {
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900184 ParseAssign(line, sep);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900185 } else if (line.get(sep+1) == '=') {
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900186 ParseAssign(line, sep+1);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900187 } else if (line[sep] == ':') {
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900188 ParseRule(line, sep);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900189 } else {
190 CHECK(false);
191 }
192 }
193
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900194 void ParseRule(StringPiece line, size_t sep) {
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900195 if (current_directive_ != AssignDirective::NONE) {
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900196 if (IsInExport())
197 return;
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900198 if (sep != string::npos) {
199 sep += orig_line_with_directives_.size() - line.size();
200 }
201 line = orig_line_with_directives_;
Shinichiro Hamajidf066c32015-06-26 09:09:26 +0900202 }
203
Shinichiro Hamaji6aeab2a2015-06-26 09:12:01 +0900204 line = TrimLeftSpace(line);
Shinichiro Hamajidf066c32015-06-26 09:09:26 +0900205 if (line.empty())
206 return;
207
208 if (orig_line_with_directives_[0] == '\t') {
Shinichiro Hamaji2bed7702015-06-26 07:56:28 +0900209 Error("*** commands commence before first target.");
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900210 }
211
Shinichiro Hamaji76ff9832015-06-18 17:11:22 +0900212 const bool is_rule = sep != string::npos && line[sep] == ':';
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900213 RuleAST* ast = new RuleAST();
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900214 ast->set_loc(loc_);
215
Shinichiro Hamaji76ff9832015-06-18 17:11:22 +0900216 size_t found = FindTwoOutsideParen(line.substr(sep + 1), '=', ';');
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900217 if (found != string::npos) {
218 found += sep + 1;
219 ast->term = line[found];
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900220 ParseExprOpt opt =
221 ast->term == ';' ? ParseExprOpt::COMMAND : ParseExprOpt::NORMAL;
222 ast->after_term = ParseExpr(TrimLeftSpace(line.substr(found + 1)), opt);
223 ast->expr = ParseExpr(TrimSpace(line.substr(0, found)));
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900224 } else {
225 ast->term = 0;
226 ast->after_term = NULL;
Shinichiro Hamajidf066c32015-06-26 09:09:26 +0900227 ast->expr = ParseExpr(line);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900228 }
229 out_asts_->push_back(ast);
230 state_ = is_rule ? ParserState::AFTER_RULE : ParserState::MAYBE_AFTER_RULE;
231 }
232
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900233 void ParseAssign(StringPiece line, size_t sep) {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900234 if (sep == 0)
235 Error("*** empty variable name ***");
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +0900236 StringPiece lhs;
237 StringPiece rhs;
238 AssignOp op;
239 ParseAssignStatement(line, sep, &lhs, &rhs, &op);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900240
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900241 AssignAST* ast = new AssignAST();
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900242 ast->set_loc(loc_);
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900243 ast->lhs = ParseExpr(lhs);
244 ast->rhs = ParseExpr(rhs);
Shinichiro Hamaji81699be2015-06-22 18:07:38 +0900245 ast->orig_rhs = rhs;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900246 ast->op = op;
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900247 ast->directive = current_directive_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900248 out_asts_->push_back(ast);
249 state_ = ParserState::NOT_AFTER_RULE;
250 }
251
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900252 void ParseInclude(StringPiece line, StringPiece directive) {
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900253 IncludeAST* ast = new IncludeAST();
Shinichiro Hamaji861bd642015-06-19 16:59:13 +0900254 ast->set_loc(loc_);
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900255 ast->expr = ParseExpr(line);
Shinichiro Hamajiefad2dd2015-06-17 03:08:02 +0900256 ast->should_exist = directive[0] == 'i';
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900257 out_asts_->push_back(ast);
Shinichiro Hamajiff4584d2015-06-24 17:45:14 +0900258 state_ = ParserState::NOT_AFTER_RULE;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900259 }
260
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900261 void ParseDefine(StringPiece line, StringPiece) {
262 if (line.empty()) {
263 Error("*** empty variable name.");
264 }
265 define_name_ = line;
266 define_start_ = 0;
267 define_start_line_ = loc_.lineno;
Shinichiro Hamajiff4584d2015-06-24 17:45:14 +0900268 state_ = ParserState::NOT_AFTER_RULE;
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900269 }
270
271 void ParseInsideDefine(StringPiece line) {
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900272 line = TrimLeftSpace(line);
273 if (GetDirective(line) != "endef") {
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900274 if (define_start_ == 0)
275 define_start_ = l_;
276 return;
277 }
278
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900279 StringPiece rest = TrimRightSpace(RemoveComment(TrimLeftSpace(
280 line.substr(sizeof("endef")))));
281 if (!rest.empty()) {
282 WARN("%s:%d: extraneous text after `endef' directive", LOCF(loc_));
283 }
284
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900285 AssignAST* ast = new AssignAST();
286 ast->set_loc(Loc(loc_.filename, define_start_line_));
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900287 ast->lhs = ParseExpr(define_name_);
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900288 StringPiece rhs;
289 if (define_start_)
Shinichiro Hamajia4d0ecb2015-06-24 22:08:30 +0900290 rhs = buf_.substr(define_start_, l_ - define_start_ - 1);
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900291 ast->rhs = ParseExpr(rhs, ParseExprOpt::DEFINE);
Shinichiro Hamaji81699be2015-06-22 18:07:38 +0900292 ast->orig_rhs = rhs;
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900293 ast->op = AssignOp::EQ;
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900294 ast->directive = current_directive_;
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900295 out_asts_->push_back(ast);
296 define_name_.clear();
297 }
298
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900299 void EnterIf(IfAST* ast) {
300 IfState* st = new IfState();
301 st->ast = ast;
302 st->is_in_else = false;
303 st->num_nest = num_if_nest_;
304 if_stack_.push(st);
305 out_asts_ = &ast->true_asts;
306 }
307
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900308 void ParseIfdef(StringPiece line, StringPiece directive) {
309 IfAST* ast = new IfAST();
310 ast->set_loc(loc_);
311 ast->op = directive[2] == 'n' ? CondOp::IFNDEF : CondOp::IFDEF;
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900312 ast->lhs = ParseExpr(line);
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900313 ast->rhs = NULL;
314 out_asts_->push_back(ast);
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900315 EnterIf(ast);
316 }
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900317
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900318 bool ParseIfEqCond(StringPiece s, IfAST* ast) {
319 if (s.empty()) {
320 return false;
321 }
322
323 if (s[0] == '(' && s[s.size() - 1] == ')') {
324 s = s.substr(1, s.size() - 2);
325 char terms[] = {',', '\0'};
326 size_t n;
Shinichiro Hamaji36b326f2015-06-26 08:56:13 +0900327 ast->lhs = ParseExprImpl(loc_, s, terms, ParseExprOpt::NORMAL, &n, true);
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900328 if (s[n] != ',')
329 return false;
330 s = TrimLeftSpace(s.substr(n+1));
Shinichiro Hamaji36b326f2015-06-26 08:56:13 +0900331 ast->rhs = ParseExprImpl(loc_, s, NULL, ParseExprOpt::NORMAL, &n);
Shinichiro Hamajiadeea692015-06-17 17:57:31 +0900332 s = TrimLeftSpace(s.substr(n));
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900333 } else {
334 for (int i = 0; i < 2; i++) {
335 if (s.empty())
336 return false;
337 char quote = s[0];
338 if (quote != '\'' && quote != '"')
339 return false;
340 size_t end = s.find(quote, 1);
341 if (end == string::npos)
342 return false;
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900343 Value* v = ParseExpr(s.substr(1, end - 1), ParseExprOpt::NORMAL);
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900344 if (i == 0)
345 ast->lhs = v;
346 else
347 ast->rhs = v;
348 s = TrimLeftSpace(s.substr(end+1));
349 }
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900350 }
Shinichiro Hamajiadeea692015-06-17 17:57:31 +0900351 if (!s.empty()) {
352 Error("extraneous text after `ifeq' directive");
353 }
354 return true;
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900355 }
356
357 void ParseIfeq(StringPiece line, StringPiece directive) {
358 IfAST* ast = new IfAST();
359 ast->set_loc(loc_);
360 ast->op = directive[2] == 'n' ? CondOp::IFNEQ : CondOp::IFEQ;
361
362 if (!ParseIfEqCond(line, ast)) {
363 Error("*** invalid syntax in conditional.");
364 }
365
366 out_asts_->push_back(ast);
367 EnterIf(ast);
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900368 }
369
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900370 void ParseElse(StringPiece line, StringPiece) {
371 CheckIfStack("else");
372 IfState* st = if_stack_.top();
373 if (st->is_in_else)
374 Error("*** only one `else' per conditional.");
375 st->is_in_else = true;
376 out_asts_ = &st->ast->false_asts;
377
378 StringPiece next_if = TrimLeftSpace(line);
379 if (next_if.empty())
380 return;
381
382 num_if_nest_ = st->num_nest + 1;
383 if (!HandleDirective(next_if, else_if_directives_)) {
384 WARN("%s:%d: extraneous text after `else' directive", LOCF(loc_));
385 }
386 num_if_nest_ = 0;
387 }
388
389 void ParseEndif(StringPiece line, StringPiece) {
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900390 CheckIfStack("endif");
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900391 if (!line.empty())
392 Error("extraneous text after `endif` directive");
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900393 IfState st = *if_stack_.top();
394 for (int t = 0; t <= st.num_nest; t++) {
395 delete if_stack_.top();
396 if_stack_.pop();
397 if (if_stack_.empty()) {
398 out_asts_ = asts_;
399 } else {
400 IfState* st = if_stack_.top();
401 if (st->is_in_else)
Shinichiro Hamaji2847f092015-06-17 15:48:37 +0900402 out_asts_ = &st->ast->false_asts;
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900403 else
Shinichiro Hamaji2847f092015-06-17 15:48:37 +0900404 out_asts_ = &st->ast->true_asts;
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900405 }
406 }
407 }
408
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900409 bool IsInExport() const {
410 return (static_cast<int>(current_directive_) &
411 static_cast<int>(AssignDirective::EXPORT));
412 }
413
414 void CreateExport(StringPiece line, bool is_export) {
415 ExportAST* ast = new ExportAST;
416 ast->set_loc(loc_);
417 ast->expr = ParseExpr(line);
418 ast->is_export = is_export;
419 out_asts_->push_back(ast);
420 }
421
422 void ParseOverride(StringPiece line, StringPiece) {
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900423 current_directive_ =
424 static_cast<AssignDirective>(
425 (static_cast<int>(current_directive_) |
426 static_cast<int>(AssignDirective::OVERRIDE)));
427 if (HandleDirective(line, assign_directives_))
428 return;
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900429 if (IsInExport()) {
430 CreateExport(line, true);
431 }
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900432 ParseRuleOrAssign(line);
433 }
434
435 void ParseExport(StringPiece line, StringPiece) {
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900436 current_directive_ =
437 static_cast<AssignDirective>(
438 (static_cast<int>(current_directive_) |
439 static_cast<int>(AssignDirective::EXPORT)));
440 if (HandleDirective(line, assign_directives_))
441 return;
442 CreateExport(line, true);
443 ParseRuleOrAssign(line);
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900444 }
445
446 void ParseUnexport(StringPiece line, StringPiece) {
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900447 CreateExport(line, false);
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900448 }
449
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900450 void CheckIfStack(const char* keyword) {
451 if (if_stack_.empty()) {
452 Error(StringPrintf("*** extraneous `%s'.", keyword));
453 }
454 }
455
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900456 StringPiece RemoveComment(StringPiece line) {
Shinichiro Hamajieafd0522015-06-18 16:46:02 +0900457 size_t i = FindOutsideParen(line, '#');
458 if (i == string::npos)
459 return line;
460 return line.substr(0, i);
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900461 }
462
463 StringPiece GetDirective(StringPiece line) {
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900464 if (line.size() < shortest_directive_len_)
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900465 return StringPiece();
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900466 StringPiece prefix = line.substr(0, longest_directive_len_ + 1);
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900467 size_t space_index = prefix.find_first_of(" \t#");
468 return prefix.substr(0, space_index);
469 }
470
471 bool HandleDirective(StringPiece line, const DirectiveMap* directive_map) {
472 StringPiece directive = GetDirective(line);
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900473 auto found = directive_map->find(directive);
474 if (found == directive_map->end())
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900475 return false;
476
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900477 StringPiece rest = TrimRightSpace(RemoveComment(TrimLeftSpace(
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900478 line.substr(directive.size()))));
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900479 (this->*found->second)(rest, directive);
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900480 return true;
481 }
482
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900483 StringPiece buf_;
484 size_t l_;
485 ParserState state_;
486
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900487 vector<AST*>* asts_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900488 vector<AST*>* out_asts_;
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900489
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900490 StringPiece define_name_;
491 size_t define_start_;
492 int define_start_line_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900493
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900494 StringPiece orig_line_with_directives_;
495 AssignDirective current_directive_;
496
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900497 int num_if_nest_;
498 stack<IfState*> if_stack_;
499
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900500 Loc loc_;
501 bool fixed_lineno_;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900502
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900503 static DirectiveMap* make_directives_;
504 static DirectiveMap* else_if_directives_;
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900505 static DirectiveMap* assign_directives_;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900506 static size_t shortest_directive_len_;
507 static size_t longest_directive_len_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900508};
509
510void Parse(Makefile* mk) {
Shinichiro Hamaji6b1a11a2015-06-30 16:21:19 +0900511 COLLECT_STATS("parse file time");
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900512 Parser parser(StringPiece(mk->buf(), mk->len()),
513 mk->filename().c_str(),
514 mk->mutable_asts());
515 parser.Parse();
516}
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900517
Shinichiro Hamaji80456fb2015-06-18 14:56:10 +0900518void Parse(StringPiece buf, const Loc& loc, vector<AST*>* out_asts) {
Shinichiro Hamaji6b1a11a2015-06-30 16:21:19 +0900519 COLLECT_STATS("parse eval time");
Shinichiro Hamaji80456fb2015-06-18 14:56:10 +0900520 Parser parser(buf, loc, out_asts);
521 parser.Parse();
522}
523
Shinichiro Hamaji631a9f82015-07-05 14:18:15 +0900524void ParseNotAfterRule(StringPiece buf, const Loc& loc,
525 vector<AST*>* out_asts) {
526 Parser parser(buf, loc, out_asts);
527 parser.set_state(ParserState::NOT_AFTER_RULE);
528 parser.Parse();
529}
530
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900531void InitParser() {
532 Parser::Init();
533}
534
535void QuitParser() {
536 Parser::Quit();
537}
538
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900539Parser::DirectiveMap* Parser::make_directives_;
540Parser::DirectiveMap* Parser::else_if_directives_;
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900541Parser::DirectiveMap* Parser::assign_directives_;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900542size_t Parser::shortest_directive_len_;
543size_t Parser::longest_directive_len_;
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +0900544
545void ParseAssignStatement(StringPiece line, size_t sep,
546 StringPiece* lhs, StringPiece* rhs, AssignOp* op) {
547 CHECK(sep != 0);
548 *op = AssignOp::EQ;
549 size_t lhs_end = sep;
550 switch (line[sep-1]) {
551 case ':':
552 lhs_end--;
553 *op = AssignOp::COLON_EQ;
554 break;
555 case '+':
556 lhs_end--;
557 *op = AssignOp::PLUS_EQ;
558 break;
559 case '?':
560 lhs_end--;
561 *op = AssignOp::QUESTION_EQ;
562 break;
563 }
564 *lhs = TrimSpace(line.substr(0, lhs_end));
565 *rhs = TrimSpace(line.substr(sep + 1));
566}