blob: d8b33e4b606921fa408e6bb9e66f63baf9a660da [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 Hamajia06bee92015-08-05 17:13:05 +0900134 static vector<ParseErrorAST*> parse_errors;
135
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900136 private:
137 void Error(const string& msg) {
Shinichiro Hamaji56227862015-08-05 16:53:37 +0900138 ParseErrorAST* ast = new ParseErrorAST();
139 ast->set_loc(loc_);
140 ast->msg = msg;
141 out_asts_->push_back(ast);
Shinichiro Hamajia06bee92015-08-05 17:13:05 +0900142 parse_errors.push_back(ast);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900143 }
144
145 size_t FindEndOfLine(size_t* lf_cnt) {
Shinichiro Hamaji14bb2792015-06-25 18:24:11 +0900146 return ::FindEndOfLine(buf_, l_, lf_cnt);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900147 }
148
Shinichiro Hamaji36b326f2015-06-26 08:56:13 +0900149 Value* ParseExpr(StringPiece s, ParseExprOpt opt = ParseExprOpt::NORMAL) {
150 return ::ParseExpr(loc_, s, opt);
151 }
152
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900153 void ParseLine(StringPiece line) {
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900154 if (!define_name_.empty()) {
155 ParseInsideDefine(line);
156 return;
157 }
158
Shinichiro Hamajic6ac0b92015-07-06 16:24:10 +0900159 if (line.empty() || (line.size() == 1 && line[0] == '\r'))
160 return;
161
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900162 current_directive_ = AssignDirective::NONE;
163
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900164 if (line[0] == '\t' && state_ != ParserState::NOT_AFTER_RULE) {
165 CommandAST* ast = new CommandAST();
Shinichiro Hamaji861bd642015-06-19 16:59:13 +0900166 ast->set_loc(loc_);
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900167 ast->expr = ParseExpr(line.substr(1), ParseExprOpt::COMMAND);
Shinichiro Hamaji631a9f82015-07-05 14:18:15 +0900168 ast->orig = line;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900169 out_asts_->push_back(ast);
170 return;
171 }
172
Shinichiro Hamaji32750622015-06-17 14:57:33 +0900173 line = TrimLeftSpace(line);
Shinichiro Hamajid4e81932015-06-17 04:40:45 +0900174
175 if (line[0] == '#')
176 return;
177
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900178 if (HandleDirective(line, make_directives_)) {
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900179 return;
180 }
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900181
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900182 ParseRuleOrAssign(line);
183 }
184
185 void ParseRuleOrAssign(StringPiece line) {
Shinichiro Hamaji415a9b12015-07-05 14:33:02 +0900186 size_t sep = FindThreeOutsideParen(line, ':', '=', ';');
187 if (sep == string::npos || line[sep] == ';') {
188 ParseRule(line, string::npos);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900189 } else if (line[sep] == '=') {
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900190 ParseAssign(line, sep);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900191 } else if (line.get(sep+1) == '=') {
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900192 ParseAssign(line, sep+1);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900193 } else if (line[sep] == ':') {
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900194 ParseRule(line, sep);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900195 } else {
196 CHECK(false);
197 }
198 }
199
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900200 void ParseRule(StringPiece line, size_t sep) {
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900201 if (current_directive_ != AssignDirective::NONE) {
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900202 if (IsInExport())
203 return;
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900204 if (sep != string::npos) {
205 sep += orig_line_with_directives_.size() - line.size();
206 }
207 line = orig_line_with_directives_;
Shinichiro Hamajidf066c32015-06-26 09:09:26 +0900208 }
209
Shinichiro Hamaji6aeab2a2015-06-26 09:12:01 +0900210 line = TrimLeftSpace(line);
Shinichiro Hamajidf066c32015-06-26 09:09:26 +0900211 if (line.empty())
212 return;
213
214 if (orig_line_with_directives_[0] == '\t') {
Shinichiro Hamaji2bed7702015-06-26 07:56:28 +0900215 Error("*** commands commence before first target.");
Shinichiro Hamaji56227862015-08-05 16:53:37 +0900216 return;
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900217 }
218
Shinichiro Hamaji76ff9832015-06-18 17:11:22 +0900219 const bool is_rule = sep != string::npos && line[sep] == ':';
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900220 RuleAST* ast = new RuleAST();
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900221 ast->set_loc(loc_);
222
Shinichiro Hamaji76ff9832015-06-18 17:11:22 +0900223 size_t found = FindTwoOutsideParen(line.substr(sep + 1), '=', ';');
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900224 if (found != string::npos) {
225 found += sep + 1;
226 ast->term = line[found];
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900227 ParseExprOpt opt =
228 ast->term == ';' ? ParseExprOpt::COMMAND : ParseExprOpt::NORMAL;
229 ast->after_term = ParseExpr(TrimLeftSpace(line.substr(found + 1)), opt);
230 ast->expr = ParseExpr(TrimSpace(line.substr(0, found)));
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900231 } else {
232 ast->term = 0;
233 ast->after_term = NULL;
Shinichiro Hamajidf066c32015-06-26 09:09:26 +0900234 ast->expr = ParseExpr(line);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900235 }
236 out_asts_->push_back(ast);
237 state_ = is_rule ? ParserState::AFTER_RULE : ParserState::MAYBE_AFTER_RULE;
238 }
239
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900240 void ParseAssign(StringPiece line, size_t sep) {
Shinichiro Hamaji56227862015-08-05 16:53:37 +0900241 if (sep == 0) {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900242 Error("*** empty variable name ***");
Shinichiro Hamaji56227862015-08-05 16:53:37 +0900243 return;
244 }
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +0900245 StringPiece lhs;
246 StringPiece rhs;
247 AssignOp op;
248 ParseAssignStatement(line, sep, &lhs, &rhs, &op);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900249
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900250 AssignAST* ast = new AssignAST();
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900251 ast->set_loc(loc_);
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900252 ast->lhs = ParseExpr(lhs);
253 ast->rhs = ParseExpr(rhs);
Shinichiro Hamaji81699be2015-06-22 18:07:38 +0900254 ast->orig_rhs = rhs;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900255 ast->op = op;
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900256 ast->directive = current_directive_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900257 out_asts_->push_back(ast);
258 state_ = ParserState::NOT_AFTER_RULE;
259 }
260
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900261 void ParseInclude(StringPiece line, StringPiece directive) {
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900262 IncludeAST* ast = new IncludeAST();
Shinichiro Hamaji861bd642015-06-19 16:59:13 +0900263 ast->set_loc(loc_);
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900264 ast->expr = ParseExpr(line);
Shinichiro Hamajiefad2dd2015-06-17 03:08:02 +0900265 ast->should_exist = directive[0] == 'i';
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900266 out_asts_->push_back(ast);
Shinichiro Hamajiff4584d2015-06-24 17:45:14 +0900267 state_ = ParserState::NOT_AFTER_RULE;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900268 }
269
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900270 void ParseDefine(StringPiece line, StringPiece) {
271 if (line.empty()) {
272 Error("*** empty variable name.");
Shinichiro Hamaji56227862015-08-05 16:53:37 +0900273 return;
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900274 }
275 define_name_ = line;
276 define_start_ = 0;
277 define_start_line_ = loc_.lineno;
Shinichiro Hamajiff4584d2015-06-24 17:45:14 +0900278 state_ = ParserState::NOT_AFTER_RULE;
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900279 }
280
281 void ParseInsideDefine(StringPiece line) {
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900282 line = TrimLeftSpace(line);
283 if (GetDirective(line) != "endef") {
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900284 if (define_start_ == 0)
285 define_start_ = l_;
286 return;
287 }
288
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900289 StringPiece rest = TrimRightSpace(RemoveComment(TrimLeftSpace(
290 line.substr(sizeof("endef")))));
291 if (!rest.empty()) {
292 WARN("%s:%d: extraneous text after `endef' directive", LOCF(loc_));
293 }
294
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900295 AssignAST* ast = new AssignAST();
296 ast->set_loc(Loc(loc_.filename, define_start_line_));
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900297 ast->lhs = ParseExpr(define_name_);
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900298 StringPiece rhs;
299 if (define_start_)
Shinichiro Hamajia4d0ecb2015-06-24 22:08:30 +0900300 rhs = buf_.substr(define_start_, l_ - define_start_ - 1);
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900301 ast->rhs = ParseExpr(rhs, ParseExprOpt::DEFINE);
Shinichiro Hamaji81699be2015-06-22 18:07:38 +0900302 ast->orig_rhs = rhs;
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900303 ast->op = AssignOp::EQ;
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900304 ast->directive = current_directive_;
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900305 out_asts_->push_back(ast);
306 define_name_.clear();
307 }
308
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900309 void EnterIf(IfAST* ast) {
310 IfState* st = new IfState();
311 st->ast = ast;
312 st->is_in_else = false;
313 st->num_nest = num_if_nest_;
314 if_stack_.push(st);
315 out_asts_ = &ast->true_asts;
316 }
317
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900318 void ParseIfdef(StringPiece line, StringPiece directive) {
319 IfAST* ast = new IfAST();
320 ast->set_loc(loc_);
321 ast->op = directive[2] == 'n' ? CondOp::IFNDEF : CondOp::IFDEF;
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900322 ast->lhs = ParseExpr(line);
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900323 ast->rhs = NULL;
324 out_asts_->push_back(ast);
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900325 EnterIf(ast);
326 }
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900327
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900328 bool ParseIfEqCond(StringPiece s, IfAST* ast) {
329 if (s.empty()) {
330 return false;
331 }
332
333 if (s[0] == '(' && s[s.size() - 1] == ')') {
334 s = s.substr(1, s.size() - 2);
335 char terms[] = {',', '\0'};
336 size_t n;
Shinichiro Hamaji36b326f2015-06-26 08:56:13 +0900337 ast->lhs = ParseExprImpl(loc_, s, terms, ParseExprOpt::NORMAL, &n, true);
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900338 if (s[n] != ',')
339 return false;
340 s = TrimLeftSpace(s.substr(n+1));
Shinichiro Hamaji36b326f2015-06-26 08:56:13 +0900341 ast->rhs = ParseExprImpl(loc_, s, NULL, ParseExprOpt::NORMAL, &n);
Shinichiro Hamajiadeea692015-06-17 17:57:31 +0900342 s = TrimLeftSpace(s.substr(n));
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900343 } else {
344 for (int i = 0; i < 2; i++) {
345 if (s.empty())
346 return false;
347 char quote = s[0];
348 if (quote != '\'' && quote != '"')
349 return false;
350 size_t end = s.find(quote, 1);
351 if (end == string::npos)
352 return false;
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900353 Value* v = ParseExpr(s.substr(1, end - 1), ParseExprOpt::NORMAL);
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900354 if (i == 0)
355 ast->lhs = v;
356 else
357 ast->rhs = v;
358 s = TrimLeftSpace(s.substr(end+1));
359 }
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900360 }
Shinichiro Hamajiadeea692015-06-17 17:57:31 +0900361 if (!s.empty()) {
Shinichiro Hamaji00496aa2015-09-09 18:27:28 +0900362 WARN("%s:%d: extraneous text after `ifeq' directive", LOCF(loc_));
Shinichiro Hamaji56227862015-08-05 16:53:37 +0900363 return true;
Shinichiro Hamajiadeea692015-06-17 17:57:31 +0900364 }
365 return true;
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900366 }
367
368 void ParseIfeq(StringPiece line, StringPiece directive) {
369 IfAST* ast = new IfAST();
370 ast->set_loc(loc_);
371 ast->op = directive[2] == 'n' ? CondOp::IFNEQ : CondOp::IFEQ;
372
373 if (!ParseIfEqCond(line, ast)) {
374 Error("*** invalid syntax in conditional.");
Shinichiro Hamaji56227862015-08-05 16:53:37 +0900375 return;
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900376 }
377
378 out_asts_->push_back(ast);
379 EnterIf(ast);
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900380 }
381
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900382 void ParseElse(StringPiece line, StringPiece) {
Shinichiro Hamaji56227862015-08-05 16:53:37 +0900383 if (!CheckIfStack("else"))
384 return;
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900385 IfState* st = if_stack_.top();
Shinichiro Hamaji56227862015-08-05 16:53:37 +0900386 if (st->is_in_else) {
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900387 Error("*** only one `else' per conditional.");
Shinichiro Hamaji56227862015-08-05 16:53:37 +0900388 return;
389 }
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900390 st->is_in_else = true;
391 out_asts_ = &st->ast->false_asts;
392
393 StringPiece next_if = TrimLeftSpace(line);
394 if (next_if.empty())
395 return;
396
397 num_if_nest_ = st->num_nest + 1;
398 if (!HandleDirective(next_if, else_if_directives_)) {
399 WARN("%s:%d: extraneous text after `else' directive", LOCF(loc_));
400 }
401 num_if_nest_ = 0;
402 }
403
404 void ParseEndif(StringPiece line, StringPiece) {
Shinichiro Hamaji56227862015-08-05 16:53:37 +0900405 if (!CheckIfStack("endif"))
406 return;
407 if (!line.empty()) {
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900408 Error("extraneous text after `endif` directive");
Shinichiro Hamaji56227862015-08-05 16:53:37 +0900409 return;
410 }
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900411 IfState st = *if_stack_.top();
412 for (int t = 0; t <= st.num_nest; t++) {
413 delete if_stack_.top();
414 if_stack_.pop();
415 if (if_stack_.empty()) {
416 out_asts_ = asts_;
417 } else {
418 IfState* st = if_stack_.top();
419 if (st->is_in_else)
Shinichiro Hamaji2847f092015-06-17 15:48:37 +0900420 out_asts_ = &st->ast->false_asts;
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900421 else
Shinichiro Hamaji2847f092015-06-17 15:48:37 +0900422 out_asts_ = &st->ast->true_asts;
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900423 }
424 }
425 }
426
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900427 bool IsInExport() const {
428 return (static_cast<int>(current_directive_) &
429 static_cast<int>(AssignDirective::EXPORT));
430 }
431
432 void CreateExport(StringPiece line, bool is_export) {
433 ExportAST* ast = new ExportAST;
434 ast->set_loc(loc_);
435 ast->expr = ParseExpr(line);
436 ast->is_export = is_export;
437 out_asts_->push_back(ast);
438 }
439
440 void ParseOverride(StringPiece line, StringPiece) {
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900441 current_directive_ =
442 static_cast<AssignDirective>(
443 (static_cast<int>(current_directive_) |
444 static_cast<int>(AssignDirective::OVERRIDE)));
445 if (HandleDirective(line, assign_directives_))
446 return;
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900447 if (IsInExport()) {
448 CreateExport(line, true);
449 }
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900450 ParseRuleOrAssign(line);
451 }
452
453 void ParseExport(StringPiece line, StringPiece) {
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900454 current_directive_ =
455 static_cast<AssignDirective>(
456 (static_cast<int>(current_directive_) |
457 static_cast<int>(AssignDirective::EXPORT)));
458 if (HandleDirective(line, assign_directives_))
459 return;
460 CreateExport(line, true);
461 ParseRuleOrAssign(line);
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900462 }
463
464 void ParseUnexport(StringPiece line, StringPiece) {
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900465 CreateExport(line, false);
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900466 }
467
Shinichiro Hamaji56227862015-08-05 16:53:37 +0900468 bool CheckIfStack(const char* keyword) {
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900469 if (if_stack_.empty()) {
470 Error(StringPrintf("*** extraneous `%s'.", keyword));
Shinichiro Hamaji56227862015-08-05 16:53:37 +0900471 return false;
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900472 }
Shinichiro Hamaji56227862015-08-05 16:53:37 +0900473 return true;
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900474 }
475
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900476 StringPiece RemoveComment(StringPiece line) {
Shinichiro Hamajieafd0522015-06-18 16:46:02 +0900477 size_t i = FindOutsideParen(line, '#');
478 if (i == string::npos)
479 return line;
480 return line.substr(0, i);
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900481 }
482
483 StringPiece GetDirective(StringPiece line) {
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900484 if (line.size() < shortest_directive_len_)
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900485 return StringPiece();
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900486 StringPiece prefix = line.substr(0, longest_directive_len_ + 1);
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900487 size_t space_index = prefix.find_first_of(" \t#");
488 return prefix.substr(0, space_index);
489 }
490
491 bool HandleDirective(StringPiece line, const DirectiveMap* directive_map) {
492 StringPiece directive = GetDirective(line);
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900493 auto found = directive_map->find(directive);
494 if (found == directive_map->end())
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900495 return false;
496
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900497 StringPiece rest = TrimRightSpace(RemoveComment(TrimLeftSpace(
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900498 line.substr(directive.size()))));
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900499 (this->*found->second)(rest, directive);
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900500 return true;
501 }
502
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900503 StringPiece buf_;
504 size_t l_;
505 ParserState state_;
506
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900507 vector<AST*>* asts_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900508 vector<AST*>* out_asts_;
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900509
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900510 StringPiece define_name_;
511 size_t define_start_;
512 int define_start_line_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900513
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900514 StringPiece orig_line_with_directives_;
515 AssignDirective current_directive_;
516
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900517 int num_if_nest_;
518 stack<IfState*> if_stack_;
519
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900520 Loc loc_;
521 bool fixed_lineno_;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900522
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900523 static DirectiveMap* make_directives_;
524 static DirectiveMap* else_if_directives_;
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900525 static DirectiveMap* assign_directives_;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900526 static size_t shortest_directive_len_;
527 static size_t longest_directive_len_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900528};
529
530void Parse(Makefile* mk) {
Shinichiro Hamaji6b1a11a2015-06-30 16:21:19 +0900531 COLLECT_STATS("parse file time");
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900532 Parser parser(StringPiece(mk->buf(), mk->len()),
533 mk->filename().c_str(),
534 mk->mutable_asts());
535 parser.Parse();
536}
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900537
Shinichiro Hamaji80456fb2015-06-18 14:56:10 +0900538void Parse(StringPiece buf, const Loc& loc, vector<AST*>* out_asts) {
Shinichiro Hamaji6b1a11a2015-06-30 16:21:19 +0900539 COLLECT_STATS("parse eval time");
Shinichiro Hamaji80456fb2015-06-18 14:56:10 +0900540 Parser parser(buf, loc, out_asts);
541 parser.Parse();
542}
543
Shinichiro Hamaji631a9f82015-07-05 14:18:15 +0900544void ParseNotAfterRule(StringPiece buf, const Loc& loc,
545 vector<AST*>* out_asts) {
546 Parser parser(buf, loc, out_asts);
547 parser.set_state(ParserState::NOT_AFTER_RULE);
548 parser.Parse();
549}
550
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900551void InitParser() {
552 Parser::Init();
553}
554
555void QuitParser() {
556 Parser::Quit();
557}
558
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900559Parser::DirectiveMap* Parser::make_directives_;
560Parser::DirectiveMap* Parser::else_if_directives_;
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900561Parser::DirectiveMap* Parser::assign_directives_;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900562size_t Parser::shortest_directive_len_;
563size_t Parser::longest_directive_len_;
Shinichiro Hamajia06bee92015-08-05 17:13:05 +0900564vector<ParseErrorAST*> Parser::parse_errors;
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +0900565
566void ParseAssignStatement(StringPiece line, size_t sep,
567 StringPiece* lhs, StringPiece* rhs, AssignOp* op) {
568 CHECK(sep != 0);
569 *op = AssignOp::EQ;
570 size_t lhs_end = sep;
571 switch (line[sep-1]) {
572 case ':':
573 lhs_end--;
574 *op = AssignOp::COLON_EQ;
575 break;
576 case '+':
577 lhs_end--;
578 *op = AssignOp::PLUS_EQ;
579 break;
580 case '?':
581 lhs_end--;
582 *op = AssignOp::QUESTION_EQ;
583 break;
584 }
585 *lhs = TrimSpace(line.substr(0, lhs_end));
586 *rhs = TrimSpace(line.substr(sep + 1));
587}
Shinichiro Hamajia06bee92015-08-05 17:13:05 +0900588
589const vector<ParseErrorAST*>& GetParseErrors() {
590 return Parser::parse_errors;
591}