blob: 19108a9a6e6a5b81468e3d0853a99e19daf814e3 [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"
26#include "string_piece.h"
Shinichiro Hamaji810fd032015-06-17 04:38:03 +090027#include "strutil.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090028#include "value.h"
29
30enum struct ParserState {
31 NOT_AFTER_RULE = 0,
32 AFTER_RULE,
33 MAYBE_AFTER_RULE,
34};
35
36class Parser {
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +090037 struct IfState {
38 IfAST* ast;
39 bool is_in_else;
40 int num_nest;
41 };
42
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +090043 typedef void (Parser::*DirectiveHandler)(
44 StringPiece line, StringPiece directive);
45 typedef unordered_map<StringPiece, DirectiveHandler> DirectiveMap;
46
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090047 public:
48 Parser(StringPiece buf, const char* filename, vector<AST*>* asts)
49 : buf_(buf),
50 state_(ParserState::NOT_AFTER_RULE),
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +090051 asts_(asts),
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090052 out_asts_(asts),
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +090053 num_if_nest_(0),
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090054 loc_(filename, 0),
55 fixed_lineno_(false) {
56 }
57
Shinichiro Hamaji80456fb2015-06-18 14:56:10 +090058 Parser(StringPiece buf, const Loc& loc, vector<AST*>* asts)
59 : buf_(buf),
60 state_(ParserState::NOT_AFTER_RULE),
61 asts_(asts),
62 out_asts_(asts),
63 num_if_nest_(0),
64 loc_(loc),
65 fixed_lineno_(true) {
66 }
67
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090068 ~Parser() {
69 }
70
71 void Parse() {
72 l_ = 0;
73
74 for (l_ = 0; l_ < buf_.size();) {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090075 size_t lf_cnt = 0;
76 size_t e = FindEndOfLine(&lf_cnt);
Shinichiro Hamaji8ee8c372015-06-16 16:19:40 +090077 if (!fixed_lineno_)
Shinichiro Hamajib3af68b2015-06-24 20:47:57 +090078 loc_.lineno++;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090079 StringPiece line(buf_.data() + l_, e - l_);
Shinichiro Hamaji420f7752015-06-26 04:02:02 +090080 orig_line_with_directives_ = line;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090081 ParseLine(line);
Shinichiro Hamajib3af68b2015-06-24 20:47:57 +090082 if (!fixed_lineno_)
83 loc_.lineno += lf_cnt - 1;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090084 if (e == buf_.size())
85 break;
86
87 l_ = e + 1;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090088 }
89 }
90
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +090091 static void Init() {
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +090092 make_directives_ = new DirectiveMap;
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +090093 (*make_directives_)["include"] = &Parser::ParseInclude;
94 (*make_directives_)["-include"] = &Parser::ParseInclude;
95 (*make_directives_)["sinclude"] = &Parser::ParseInclude;
Shinichiro Hamaji810fd032015-06-17 04:38:03 +090096 (*make_directives_)["define"] = &Parser::ParseDefine;
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +090097 (*make_directives_)["ifdef"] = &Parser::ParseIfdef;
98 (*make_directives_)["ifndef"] = &Parser::ParseIfdef;
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +090099 (*make_directives_)["ifeq"] = &Parser::ParseIfeq;
100 (*make_directives_)["ifneq"] = &Parser::ParseIfeq;
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900101 (*make_directives_)["else"] = &Parser::ParseElse;
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900102 (*make_directives_)["endif"] = &Parser::ParseEndif;
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900103 (*make_directives_)["override"] = &Parser::ParseOverride;
104 (*make_directives_)["export"] = &Parser::ParseExport;
105 (*make_directives_)["unexport"] = &Parser::ParseUnexport;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900106
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900107 else_if_directives_ = new DirectiveMap;
108 (*else_if_directives_)["ifdef"] = &Parser::ParseIfdef;
109 (*else_if_directives_)["ifndef"] = &Parser::ParseIfdef;
110
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900111 assign_directives_ = new DirectiveMap;
112 (*assign_directives_)["define"] = &Parser::ParseDefine;
113 (*assign_directives_)["export"] = &Parser::ParseExport;
114 (*assign_directives_)["override"] = &Parser::ParseOverride;
115
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900116 shortest_directive_len_ = 9999;
117 longest_directive_len_ = 0;
118 for (auto p : *make_directives_) {
119 size_t len = p.first.size();
120 shortest_directive_len_ = min(len, shortest_directive_len_);
121 longest_directive_len_ = max(len, longest_directive_len_);
122 }
123 }
124
125 static void Quit() {
126 delete make_directives_;
127 }
128
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900129 private:
130 void Error(const string& msg) {
Shinichiro Hamaji8ee8c372015-06-16 16:19:40 +0900131 ERROR("%s:%d: %s", LOCF(loc_), msg.c_str());
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900132 }
133
134 size_t FindEndOfLine(size_t* lf_cnt) {
Shinichiro Hamaji14bb2792015-06-25 18:24:11 +0900135 return ::FindEndOfLine(buf_, l_, lf_cnt);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900136 }
137
Shinichiro Hamaji36b326f2015-06-26 08:56:13 +0900138 Value* ParseExpr(StringPiece s, ParseExprOpt opt = ParseExprOpt::NORMAL) {
139 return ::ParseExpr(loc_, s, opt);
140 }
141
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900142 void ParseLine(StringPiece line) {
143 if (line.empty() || (line.size() == 1 && line[0] == '\r'))
144 return;
145
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900146 if (!define_name_.empty()) {
147 ParseInsideDefine(line);
148 return;
149 }
150
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900151 current_directive_ = AssignDirective::NONE;
152
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900153 if (line[0] == '\t' && state_ != ParserState::NOT_AFTER_RULE) {
154 CommandAST* ast = new CommandAST();
Shinichiro Hamaji861bd642015-06-19 16:59:13 +0900155 ast->set_loc(loc_);
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900156 ast->expr = ParseExpr(line.substr(1), ParseExprOpt::COMMAND);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900157 out_asts_->push_back(ast);
158 return;
159 }
160
Shinichiro Hamaji32750622015-06-17 14:57:33 +0900161 line = TrimLeftSpace(line);
Shinichiro Hamajid4e81932015-06-17 04:40:45 +0900162
163 if (line[0] == '#')
164 return;
165
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900166 if (HandleDirective(line, make_directives_)) {
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900167 return;
168 }
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900169
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900170 ParseRuleOrAssign(line);
171 }
172
173 void ParseRuleOrAssign(StringPiece line) {
Shinichiro Hamaji76ff9832015-06-18 17:11:22 +0900174 size_t sep = FindTwoOutsideParen(line, ':', '=');
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900175 if (sep == string::npos) {
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900176 ParseRule(line, sep);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900177 } else if (line[sep] == '=') {
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900178 ParseAssign(line, sep);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900179 } else if (line.get(sep+1) == '=') {
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900180 ParseAssign(line, sep+1);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900181 } else if (line[sep] == ':') {
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900182 ParseRule(line, sep);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900183 } else {
184 CHECK(false);
185 }
186 }
187
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900188 void ParseRule(StringPiece line, size_t sep) {
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900189 if (current_directive_ != AssignDirective::NONE) {
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900190 if (IsInExport())
191 return;
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900192 if (sep != string::npos) {
193 sep += orig_line_with_directives_.size() - line.size();
194 }
195 line = orig_line_with_directives_;
Shinichiro Hamaji2bed7702015-06-26 07:56:28 +0900196 } else if (orig_line_with_directives_[0] == '\t') {
197 Error("*** commands commence before first target.");
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900198 }
199
Shinichiro Hamaji76ff9832015-06-18 17:11:22 +0900200 const bool is_rule = sep != string::npos && line[sep] == ':';
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900201 RuleAST* ast = new RuleAST();
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900202 ast->set_loc(loc_);
203
Shinichiro Hamaji76ff9832015-06-18 17:11:22 +0900204 size_t found = FindTwoOutsideParen(line.substr(sep + 1), '=', ';');
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900205 if (found != string::npos) {
206 found += sep + 1;
207 ast->term = line[found];
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900208 ParseExprOpt opt =
209 ast->term == ';' ? ParseExprOpt::COMMAND : ParseExprOpt::NORMAL;
210 ast->after_term = ParseExpr(TrimLeftSpace(line.substr(found + 1)), opt);
211 ast->expr = ParseExpr(TrimSpace(line.substr(0, found)));
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900212 } else {
213 ast->term = 0;
214 ast->after_term = NULL;
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900215 ast->expr = ParseExpr(TrimSpace(line));
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900216 }
217 out_asts_->push_back(ast);
218 state_ = is_rule ? ParserState::AFTER_RULE : ParserState::MAYBE_AFTER_RULE;
219 }
220
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900221 void ParseAssign(StringPiece line, size_t sep) {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900222 if (sep == 0)
223 Error("*** empty variable name ***");
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +0900224 StringPiece lhs;
225 StringPiece rhs;
226 AssignOp op;
227 ParseAssignStatement(line, sep, &lhs, &rhs, &op);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900228
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900229 AssignAST* ast = new AssignAST();
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900230 ast->set_loc(loc_);
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900231 ast->lhs = ParseExpr(lhs);
232 ast->rhs = ParseExpr(rhs);
Shinichiro Hamaji81699be2015-06-22 18:07:38 +0900233 ast->orig_rhs = rhs;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900234 ast->op = op;
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900235 ast->directive = current_directive_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900236 out_asts_->push_back(ast);
237 state_ = ParserState::NOT_AFTER_RULE;
238 }
239
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900240 void ParseInclude(StringPiece line, StringPiece directive) {
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900241 IncludeAST* ast = new IncludeAST();
Shinichiro Hamaji861bd642015-06-19 16:59:13 +0900242 ast->set_loc(loc_);
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900243 ast->expr = ParseExpr(line);
Shinichiro Hamajiefad2dd2015-06-17 03:08:02 +0900244 ast->should_exist = directive[0] == 'i';
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900245 out_asts_->push_back(ast);
Shinichiro Hamajiff4584d2015-06-24 17:45:14 +0900246 state_ = ParserState::NOT_AFTER_RULE;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900247 }
248
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900249 void ParseDefine(StringPiece line, StringPiece) {
250 if (line.empty()) {
251 Error("*** empty variable name.");
252 }
253 define_name_ = line;
254 define_start_ = 0;
255 define_start_line_ = loc_.lineno;
Shinichiro Hamajiff4584d2015-06-24 17:45:14 +0900256 state_ = ParserState::NOT_AFTER_RULE;
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900257 }
258
259 void ParseInsideDefine(StringPiece line) {
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900260 line = TrimLeftSpace(line);
261 if (GetDirective(line) != "endef") {
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900262 if (define_start_ == 0)
263 define_start_ = l_;
264 return;
265 }
266
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900267 StringPiece rest = TrimRightSpace(RemoveComment(TrimLeftSpace(
268 line.substr(sizeof("endef")))));
269 if (!rest.empty()) {
270 WARN("%s:%d: extraneous text after `endef' directive", LOCF(loc_));
271 }
272
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900273 AssignAST* ast = new AssignAST();
274 ast->set_loc(Loc(loc_.filename, define_start_line_));
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900275 ast->lhs = ParseExpr(define_name_);
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900276 StringPiece rhs;
277 if (define_start_)
Shinichiro Hamajia4d0ecb2015-06-24 22:08:30 +0900278 rhs = buf_.substr(define_start_, l_ - define_start_ - 1);
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900279 ast->rhs = ParseExpr(rhs, ParseExprOpt::DEFINE);
Shinichiro Hamaji81699be2015-06-22 18:07:38 +0900280 ast->orig_rhs = rhs;
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900281 ast->op = AssignOp::EQ;
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900282 ast->directive = current_directive_;
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900283 out_asts_->push_back(ast);
284 define_name_.clear();
285 }
286
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900287 void EnterIf(IfAST* ast) {
288 IfState* st = new IfState();
289 st->ast = ast;
290 st->is_in_else = false;
291 st->num_nest = num_if_nest_;
292 if_stack_.push(st);
293 out_asts_ = &ast->true_asts;
294 }
295
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900296 void ParseIfdef(StringPiece line, StringPiece directive) {
297 IfAST* ast = new IfAST();
298 ast->set_loc(loc_);
299 ast->op = directive[2] == 'n' ? CondOp::IFNDEF : CondOp::IFDEF;
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900300 ast->lhs = ParseExpr(line);
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900301 ast->rhs = NULL;
302 out_asts_->push_back(ast);
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900303 EnterIf(ast);
304 }
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900305
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900306 bool ParseIfEqCond(StringPiece s, IfAST* ast) {
307 if (s.empty()) {
308 return false;
309 }
310
311 if (s[0] == '(' && s[s.size() - 1] == ')') {
312 s = s.substr(1, s.size() - 2);
313 char terms[] = {',', '\0'};
314 size_t n;
Shinichiro Hamaji36b326f2015-06-26 08:56:13 +0900315 ast->lhs = ParseExprImpl(loc_, s, terms, ParseExprOpt::NORMAL, &n, true);
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900316 if (s[n] != ',')
317 return false;
318 s = TrimLeftSpace(s.substr(n+1));
Shinichiro Hamaji36b326f2015-06-26 08:56:13 +0900319 ast->rhs = ParseExprImpl(loc_, s, NULL, ParseExprOpt::NORMAL, &n);
Shinichiro Hamajiadeea692015-06-17 17:57:31 +0900320 s = TrimLeftSpace(s.substr(n));
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900321 } else {
322 for (int i = 0; i < 2; i++) {
323 if (s.empty())
324 return false;
325 char quote = s[0];
326 if (quote != '\'' && quote != '"')
327 return false;
328 size_t end = s.find(quote, 1);
329 if (end == string::npos)
330 return false;
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900331 Value* v = ParseExpr(s.substr(1, end - 1), ParseExprOpt::NORMAL);
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900332 if (i == 0)
333 ast->lhs = v;
334 else
335 ast->rhs = v;
336 s = TrimLeftSpace(s.substr(end+1));
337 }
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900338 }
Shinichiro Hamajiadeea692015-06-17 17:57:31 +0900339 if (!s.empty()) {
340 Error("extraneous text after `ifeq' directive");
341 }
342 return true;
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900343 }
344
345 void ParseIfeq(StringPiece line, StringPiece directive) {
346 IfAST* ast = new IfAST();
347 ast->set_loc(loc_);
348 ast->op = directive[2] == 'n' ? CondOp::IFNEQ : CondOp::IFEQ;
349
350 if (!ParseIfEqCond(line, ast)) {
351 Error("*** invalid syntax in conditional.");
352 }
353
354 out_asts_->push_back(ast);
355 EnterIf(ast);
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900356 }
357
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900358 void ParseElse(StringPiece line, StringPiece) {
359 CheckIfStack("else");
360 IfState* st = if_stack_.top();
361 if (st->is_in_else)
362 Error("*** only one `else' per conditional.");
363 st->is_in_else = true;
364 out_asts_ = &st->ast->false_asts;
365
366 StringPiece next_if = TrimLeftSpace(line);
367 if (next_if.empty())
368 return;
369
370 num_if_nest_ = st->num_nest + 1;
371 if (!HandleDirective(next_if, else_if_directives_)) {
372 WARN("%s:%d: extraneous text after `else' directive", LOCF(loc_));
373 }
374 num_if_nest_ = 0;
375 }
376
377 void ParseEndif(StringPiece line, StringPiece) {
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900378 CheckIfStack("endif");
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900379 if (!line.empty())
380 Error("extraneous text after `endif` directive");
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900381 IfState st = *if_stack_.top();
382 for (int t = 0; t <= st.num_nest; t++) {
383 delete if_stack_.top();
384 if_stack_.pop();
385 if (if_stack_.empty()) {
386 out_asts_ = asts_;
387 } else {
388 IfState* st = if_stack_.top();
389 if (st->is_in_else)
Shinichiro Hamaji2847f092015-06-17 15:48:37 +0900390 out_asts_ = &st->ast->false_asts;
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900391 else
Shinichiro Hamaji2847f092015-06-17 15:48:37 +0900392 out_asts_ = &st->ast->true_asts;
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900393 }
394 }
395 }
396
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900397 bool IsInExport() const {
398 return (static_cast<int>(current_directive_) &
399 static_cast<int>(AssignDirective::EXPORT));
400 }
401
402 void CreateExport(StringPiece line, bool is_export) {
403 ExportAST* ast = new ExportAST;
404 ast->set_loc(loc_);
405 ast->expr = ParseExpr(line);
406 ast->is_export = is_export;
407 out_asts_->push_back(ast);
408 }
409
410 void ParseOverride(StringPiece line, StringPiece) {
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900411 current_directive_ =
412 static_cast<AssignDirective>(
413 (static_cast<int>(current_directive_) |
414 static_cast<int>(AssignDirective::OVERRIDE)));
415 if (HandleDirective(line, assign_directives_))
416 return;
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900417 if (IsInExport()) {
418 CreateExport(line, true);
419 }
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900420 ParseRuleOrAssign(line);
421 }
422
423 void ParseExport(StringPiece line, StringPiece) {
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900424 current_directive_ =
425 static_cast<AssignDirective>(
426 (static_cast<int>(current_directive_) |
427 static_cast<int>(AssignDirective::EXPORT)));
428 if (HandleDirective(line, assign_directives_))
429 return;
430 CreateExport(line, true);
431 ParseRuleOrAssign(line);
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900432 }
433
434 void ParseUnexport(StringPiece line, StringPiece) {
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900435 CreateExport(line, false);
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900436 }
437
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900438 void CheckIfStack(const char* keyword) {
439 if (if_stack_.empty()) {
440 Error(StringPrintf("*** extraneous `%s'.", keyword));
441 }
442 }
443
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900444 StringPiece RemoveComment(StringPiece line) {
Shinichiro Hamajieafd0522015-06-18 16:46:02 +0900445 size_t i = FindOutsideParen(line, '#');
446 if (i == string::npos)
447 return line;
448 return line.substr(0, i);
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900449 }
450
451 StringPiece GetDirective(StringPiece line) {
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900452 if (line.size() < shortest_directive_len_)
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900453 return StringPiece();
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900454 StringPiece prefix = line.substr(0, longest_directive_len_ + 1);
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900455 size_t space_index = prefix.find_first_of(" \t#");
456 return prefix.substr(0, space_index);
457 }
458
459 bool HandleDirective(StringPiece line, const DirectiveMap* directive_map) {
460 StringPiece directive = GetDirective(line);
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900461 auto found = directive_map->find(directive);
462 if (found == directive_map->end())
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900463 return false;
464
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900465 StringPiece rest = TrimRightSpace(RemoveComment(TrimLeftSpace(
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900466 line.substr(directive.size()))));
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900467 (this->*found->second)(rest, directive);
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900468 return true;
469 }
470
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900471 StringPiece buf_;
472 size_t l_;
473 ParserState state_;
474
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900475 vector<AST*>* asts_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900476 vector<AST*>* out_asts_;
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900477
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900478 StringPiece define_name_;
479 size_t define_start_;
480 int define_start_line_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900481
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900482 StringPiece orig_line_with_directives_;
483 AssignDirective current_directive_;
484
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900485 int num_if_nest_;
486 stack<IfState*> if_stack_;
487
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900488 Loc loc_;
489 bool fixed_lineno_;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900490
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900491 static DirectiveMap* make_directives_;
492 static DirectiveMap* else_if_directives_;
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900493 static DirectiveMap* assign_directives_;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900494 static size_t shortest_directive_len_;
495 static size_t longest_directive_len_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900496};
497
498void Parse(Makefile* mk) {
499 Parser parser(StringPiece(mk->buf(), mk->len()),
500 mk->filename().c_str(),
501 mk->mutable_asts());
502 parser.Parse();
503}
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900504
Shinichiro Hamaji80456fb2015-06-18 14:56:10 +0900505void Parse(StringPiece buf, const Loc& loc, vector<AST*>* out_asts) {
506 Parser parser(buf, loc, out_asts);
507 parser.Parse();
508}
509
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900510void InitParser() {
511 Parser::Init();
512}
513
514void QuitParser() {
515 Parser::Quit();
516}
517
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900518Parser::DirectiveMap* Parser::make_directives_;
519Parser::DirectiveMap* Parser::else_if_directives_;
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900520Parser::DirectiveMap* Parser::assign_directives_;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900521size_t Parser::shortest_directive_len_;
522size_t Parser::longest_directive_len_;
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +0900523
524void ParseAssignStatement(StringPiece line, size_t sep,
525 StringPiece* lhs, StringPiece* rhs, AssignOp* op) {
526 CHECK(sep != 0);
527 *op = AssignOp::EQ;
528 size_t lhs_end = sep;
529 switch (line[sep-1]) {
530 case ':':
531 lhs_end--;
532 *op = AssignOp::COLON_EQ;
533 break;
534 case '+':
535 lhs_end--;
536 *op = AssignOp::PLUS_EQ;
537 break;
538 case '?':
539 lhs_end--;
540 *op = AssignOp::QUESTION_EQ;
541 break;
542 }
543 *lhs = TrimSpace(line.substr(0, lhs_end));
544 *rhs = TrimSpace(line.substr(sep + 1));
545}