blob: ab3b93cc3fb665832097bb81367e39b4cde94ecd [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_);
80 ParseLine(line);
Shinichiro Hamajib3af68b2015-06-24 20:47:57 +090081 if (!fixed_lineno_)
82 loc_.lineno += lf_cnt - 1;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090083 if (e == buf_.size())
84 break;
85
86 l_ = e + 1;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090087 }
88 }
89
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +090090 static void Init() {
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +090091 make_directives_ = new DirectiveMap;
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +090092 (*make_directives_)["include"] = &Parser::ParseInclude;
93 (*make_directives_)["-include"] = &Parser::ParseInclude;
94 (*make_directives_)["sinclude"] = &Parser::ParseInclude;
Shinichiro Hamaji810fd032015-06-17 04:38:03 +090095 (*make_directives_)["define"] = &Parser::ParseDefine;
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +090096 (*make_directives_)["ifdef"] = &Parser::ParseIfdef;
97 (*make_directives_)["ifndef"] = &Parser::ParseIfdef;
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +090098 (*make_directives_)["ifeq"] = &Parser::ParseIfeq;
99 (*make_directives_)["ifneq"] = &Parser::ParseIfeq;
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900100 (*make_directives_)["else"] = &Parser::ParseElse;
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900101 (*make_directives_)["endif"] = &Parser::ParseEndif;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900102
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900103 else_if_directives_ = new DirectiveMap;
104 (*else_if_directives_)["ifdef"] = &Parser::ParseIfdef;
105 (*else_if_directives_)["ifndef"] = &Parser::ParseIfdef;
106
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900107 shortest_directive_len_ = 9999;
108 longest_directive_len_ = 0;
109 for (auto p : *make_directives_) {
110 size_t len = p.first.size();
111 shortest_directive_len_ = min(len, shortest_directive_len_);
112 longest_directive_len_ = max(len, longest_directive_len_);
113 }
114 }
115
116 static void Quit() {
117 delete make_directives_;
118 }
119
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900120 private:
121 void Error(const string& msg) {
Shinichiro Hamaji8ee8c372015-06-16 16:19:40 +0900122 ERROR("%s:%d: %s", LOCF(loc_), msg.c_str());
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900123 }
124
125 size_t FindEndOfLine(size_t* lf_cnt) {
126 size_t e = l_;
127 bool prev_backslash = false;
128 for (; e < buf_.size(); e++) {
129 char c = buf_[e];
130 if (c == '\\') {
131 prev_backslash = !prev_backslash;
132 } else if (c == '\n') {
133 ++*lf_cnt;
134 if (!prev_backslash) {
135 return e;
136 }
137 } else if (c != '\r') {
138 prev_backslash = false;
139 }
140 }
141 return e;
142 }
143
144 void ParseLine(StringPiece line) {
145 if (line.empty() || (line.size() == 1 && line[0] == '\r'))
146 return;
147
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900148 if (!define_name_.empty()) {
149 ParseInsideDefine(line);
150 return;
151 }
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 Hamaji76ff9832015-06-18 17:11:22 +0900170 size_t sep = FindTwoOutsideParen(line, ':', '=');
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900171 if (sep == string::npos) {
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900172 ParseRule(line, sep);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900173 } else if (line[sep] == '=') {
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900174 ParseAssign(line, sep);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900175 } else if (line.get(sep+1) == '=') {
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900176 ParseAssign(line, sep+1);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900177 } else if (line[sep] == ':') {
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900178 ParseRule(line, sep);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900179 } else {
180 CHECK(false);
181 }
182 }
183
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900184 void ParseRule(StringPiece line, size_t sep) {
Shinichiro Hamaji76ff9832015-06-18 17:11:22 +0900185 const bool is_rule = sep != string::npos && line[sep] == ':';
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900186 RuleAST* ast = new RuleAST();
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900187 ast->set_loc(loc_);
188
Shinichiro Hamaji76ff9832015-06-18 17:11:22 +0900189 size_t found = FindTwoOutsideParen(line.substr(sep + 1), '=', ';');
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900190 if (found != string::npos) {
191 found += sep + 1;
192 ast->term = line[found];
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900193 ParseExprOpt opt =
194 ast->term == ';' ? ParseExprOpt::COMMAND : ParseExprOpt::NORMAL;
195 ast->after_term = ParseExpr(TrimLeftSpace(line.substr(found + 1)), opt);
196 ast->expr = ParseExpr(TrimSpace(line.substr(0, found)));
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900197 } else {
198 ast->term = 0;
199 ast->after_term = NULL;
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900200 ast->expr = ParseExpr(TrimSpace(line));
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900201 }
202 out_asts_->push_back(ast);
203 state_ = is_rule ? ParserState::AFTER_RULE : ParserState::MAYBE_AFTER_RULE;
204 }
205
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900206 void ParseAssign(StringPiece line, size_t sep) {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900207 if (sep == 0)
208 Error("*** empty variable name ***");
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +0900209 StringPiece lhs;
210 StringPiece rhs;
211 AssignOp op;
212 ParseAssignStatement(line, sep, &lhs, &rhs, &op);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900213
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900214 AssignAST* ast = new AssignAST();
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900215 ast->set_loc(loc_);
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900216 ast->lhs = ParseExpr(lhs);
217 ast->rhs = ParseExpr(rhs);
Shinichiro Hamaji81699be2015-06-22 18:07:38 +0900218 ast->orig_rhs = rhs;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900219 ast->op = op;
220 ast->directive = AssignDirective::NONE;
221 out_asts_->push_back(ast);
222 state_ = ParserState::NOT_AFTER_RULE;
223 }
224
Shinichiro Hamaji14b8bea2015-06-17 03:14:28 +0900225 void ParseInclude(StringPiece line, StringPiece directive) {
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900226 IncludeAST* ast = new IncludeAST();
Shinichiro Hamaji861bd642015-06-19 16:59:13 +0900227 ast->set_loc(loc_);
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900228 ast->expr = ParseExpr(line);
Shinichiro Hamajiefad2dd2015-06-17 03:08:02 +0900229 ast->should_exist = directive[0] == 'i';
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900230 out_asts_->push_back(ast);
Shinichiro Hamajiff4584d2015-06-24 17:45:14 +0900231 state_ = ParserState::NOT_AFTER_RULE;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900232 }
233
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900234 void ParseDefine(StringPiece line, StringPiece) {
235 if (line.empty()) {
236 Error("*** empty variable name.");
237 }
238 define_name_ = line;
239 define_start_ = 0;
240 define_start_line_ = loc_.lineno;
Shinichiro Hamajiff4584d2015-06-24 17:45:14 +0900241 state_ = ParserState::NOT_AFTER_RULE;
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900242 }
243
244 void ParseInsideDefine(StringPiece line) {
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900245 line = TrimLeftSpace(line);
246 if (GetDirective(line) != "endef") {
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900247 if (define_start_ == 0)
248 define_start_ = l_;
249 return;
250 }
251
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900252 StringPiece rest = TrimRightSpace(RemoveComment(TrimLeftSpace(
253 line.substr(sizeof("endef")))));
254 if (!rest.empty()) {
255 WARN("%s:%d: extraneous text after `endef' directive", LOCF(loc_));
256 }
257
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900258 AssignAST* ast = new AssignAST();
259 ast->set_loc(Loc(loc_.filename, define_start_line_));
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900260 ast->lhs = ParseExpr(define_name_);
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900261 StringPiece rhs;
262 if (define_start_)
Shinichiro Hamajia4d0ecb2015-06-24 22:08:30 +0900263 rhs = buf_.substr(define_start_, l_ - define_start_ - 1);
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900264 ast->rhs = ParseExpr(rhs, ParseExprOpt::DEFINE);
Shinichiro Hamaji81699be2015-06-22 18:07:38 +0900265 ast->orig_rhs = rhs;
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900266 ast->op = AssignOp::EQ;
267 ast->directive = AssignDirective::NONE;
268 out_asts_->push_back(ast);
269 define_name_.clear();
270 }
271
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900272 void EnterIf(IfAST* ast) {
273 IfState* st = new IfState();
274 st->ast = ast;
275 st->is_in_else = false;
276 st->num_nest = num_if_nest_;
277 if_stack_.push(st);
278 out_asts_ = &ast->true_asts;
279 }
280
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900281 void ParseIfdef(StringPiece line, StringPiece directive) {
282 IfAST* ast = new IfAST();
283 ast->set_loc(loc_);
284 ast->op = directive[2] == 'n' ? CondOp::IFNDEF : CondOp::IFDEF;
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900285 ast->lhs = ParseExpr(line);
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900286 ast->rhs = NULL;
287 out_asts_->push_back(ast);
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900288 EnterIf(ast);
289 }
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900290
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900291 bool ParseIfEqCond(StringPiece s, IfAST* ast) {
292 if (s.empty()) {
293 return false;
294 }
295
296 if (s[0] == '(' && s[s.size() - 1] == ')') {
297 s = s.substr(1, s.size() - 2);
298 char terms[] = {',', '\0'};
299 size_t n;
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900300 ast->lhs = ParseExprImpl(s, terms, ParseExprOpt::NORMAL, &n, true);
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900301 if (s[n] != ',')
302 return false;
303 s = TrimLeftSpace(s.substr(n+1));
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900304 ast->rhs = ParseExprImpl(s, NULL, ParseExprOpt::NORMAL, &n);
Shinichiro Hamajiadeea692015-06-17 17:57:31 +0900305 s = TrimLeftSpace(s.substr(n));
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900306 } else {
307 for (int i = 0; i < 2; i++) {
308 if (s.empty())
309 return false;
310 char quote = s[0];
311 if (quote != '\'' && quote != '"')
312 return false;
313 size_t end = s.find(quote, 1);
314 if (end == string::npos)
315 return false;
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +0900316 Value* v = ParseExpr(s.substr(1, end - 1), ParseExprOpt::NORMAL);
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900317 if (i == 0)
318 ast->lhs = v;
319 else
320 ast->rhs = v;
321 s = TrimLeftSpace(s.substr(end+1));
322 }
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900323 }
Shinichiro Hamajiadeea692015-06-17 17:57:31 +0900324 if (!s.empty()) {
325 Error("extraneous text after `ifeq' directive");
326 }
327 return true;
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900328 }
329
330 void ParseIfeq(StringPiece line, StringPiece directive) {
331 IfAST* ast = new IfAST();
332 ast->set_loc(loc_);
333 ast->op = directive[2] == 'n' ? CondOp::IFNEQ : CondOp::IFEQ;
334
335 if (!ParseIfEqCond(line, ast)) {
336 Error("*** invalid syntax in conditional.");
337 }
338
339 out_asts_->push_back(ast);
340 EnterIf(ast);
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900341 }
342
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900343 void ParseElse(StringPiece line, StringPiece) {
344 CheckIfStack("else");
345 IfState* st = if_stack_.top();
346 if (st->is_in_else)
347 Error("*** only one `else' per conditional.");
348 st->is_in_else = true;
349 out_asts_ = &st->ast->false_asts;
350
351 StringPiece next_if = TrimLeftSpace(line);
352 if (next_if.empty())
353 return;
354
355 num_if_nest_ = st->num_nest + 1;
356 if (!HandleDirective(next_if, else_if_directives_)) {
357 WARN("%s:%d: extraneous text after `else' directive", LOCF(loc_));
358 }
359 num_if_nest_ = 0;
360 }
361
362 void ParseEndif(StringPiece line, StringPiece) {
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900363 CheckIfStack("endif");
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900364 if (!line.empty())
365 Error("extraneous text after `endif` directive");
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900366 IfState st = *if_stack_.top();
367 for (int t = 0; t <= st.num_nest; t++) {
368 delete if_stack_.top();
369 if_stack_.pop();
370 if (if_stack_.empty()) {
371 out_asts_ = asts_;
372 } else {
373 IfState* st = if_stack_.top();
374 if (st->is_in_else)
Shinichiro Hamaji2847f092015-06-17 15:48:37 +0900375 out_asts_ = &st->ast->false_asts;
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900376 else
Shinichiro Hamaji2847f092015-06-17 15:48:37 +0900377 out_asts_ = &st->ast->true_asts;
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900378 }
379 }
380 }
381
382 void CheckIfStack(const char* keyword) {
383 if (if_stack_.empty()) {
384 Error(StringPrintf("*** extraneous `%s'.", keyword));
385 }
386 }
387
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900388 StringPiece RemoveComment(StringPiece line) {
Shinichiro Hamajieafd0522015-06-18 16:46:02 +0900389 size_t i = FindOutsideParen(line, '#');
390 if (i == string::npos)
391 return line;
392 return line.substr(0, i);
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900393 }
394
395 StringPiece GetDirective(StringPiece line) {
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900396 if (line.size() < shortest_directive_len_)
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900397 return StringPiece();
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900398 StringPiece prefix = line.substr(0, longest_directive_len_ + 1);
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900399 size_t space_index = prefix.find_first_of(" \t#");
400 return prefix.substr(0, space_index);
401 }
402
403 bool HandleDirective(StringPiece line, const DirectiveMap* directive_map) {
404 StringPiece directive = GetDirective(line);
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900405 auto found = directive_map->find(directive);
406 if (found == directive_map->end())
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900407 return false;
408
Shinichiro Hamaji5562caf2015-06-17 17:08:40 +0900409 StringPiece rest = TrimRightSpace(RemoveComment(TrimLeftSpace(
Shinichiro Hamajid146f4c2015-06-17 17:51:24 +0900410 line.substr(directive.size()))));
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900411 (this->*found->second)(rest, directive);
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900412 return true;
413 }
414
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900415 StringPiece buf_;
416 size_t l_;
417 ParserState state_;
418
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900419 vector<AST*>* asts_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900420 vector<AST*>* out_asts_;
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900421
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900422 StringPiece define_name_;
423 size_t define_start_;
424 int define_start_line_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900425
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900426 int num_if_nest_;
427 stack<IfState*> if_stack_;
428
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900429 Loc loc_;
430 bool fixed_lineno_;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900431
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900432 static DirectiveMap* make_directives_;
433 static DirectiveMap* else_if_directives_;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900434 static size_t shortest_directive_len_;
435 static size_t longest_directive_len_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900436};
437
438void Parse(Makefile* mk) {
439 Parser parser(StringPiece(mk->buf(), mk->len()),
440 mk->filename().c_str(),
441 mk->mutable_asts());
442 parser.Parse();
443}
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900444
Shinichiro Hamaji80456fb2015-06-18 14:56:10 +0900445void Parse(StringPiece buf, const Loc& loc, vector<AST*>* out_asts) {
446 Parser parser(buf, loc, out_asts);
447 parser.Parse();
448}
449
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900450void InitParser() {
451 Parser::Init();
452}
453
454void QuitParser() {
455 Parser::Quit();
456}
457
Shinichiro Hamaji6cb1c252015-06-17 16:22:51 +0900458Parser::DirectiveMap* Parser::make_directives_;
459Parser::DirectiveMap* Parser::else_if_directives_;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900460size_t Parser::shortest_directive_len_;
461size_t Parser::longest_directive_len_;
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +0900462
463void ParseAssignStatement(StringPiece line, size_t sep,
464 StringPiece* lhs, StringPiece* rhs, AssignOp* op) {
465 CHECK(sep != 0);
466 *op = AssignOp::EQ;
467 size_t lhs_end = sep;
468 switch (line[sep-1]) {
469 case ':':
470 lhs_end--;
471 *op = AssignOp::COLON_EQ;
472 break;
473 case '+':
474 lhs_end--;
475 *op = AssignOp::PLUS_EQ;
476 break;
477 case '?':
478 lhs_end--;
479 *op = AssignOp::QUESTION_EQ;
480 break;
481 }
482 *lhs = TrimSpace(line.substr(0, lhs_end));
483 *rhs = TrimSpace(line.substr(sep + 1));
484}