Shinichiro Hamaji | 1d545aa | 2015-06-23 15:29:13 +0900 | [diff] [blame] | 1 | // 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 Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 15 | // +build ignore |
| 16 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 17 | #include "parser.h" |
| 18 | |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 19 | #include <stack> |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 20 | #include <unordered_map> |
| 21 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 22 | #include "ast.h" |
| 23 | #include "file.h" |
| 24 | #include "loc.h" |
| 25 | #include "log.h" |
Shinichiro Hamaji | 6b1a11a | 2015-06-30 16:21:19 +0900 | [diff] [blame] | 26 | #include "stats.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 27 | #include "string_piece.h" |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 28 | #include "strutil.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 29 | #include "value.h" |
| 30 | |
| 31 | enum struct ParserState { |
| 32 | NOT_AFTER_RULE = 0, |
| 33 | AFTER_RULE, |
| 34 | MAYBE_AFTER_RULE, |
| 35 | }; |
| 36 | |
| 37 | class Parser { |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 38 | struct IfState { |
| 39 | IfAST* ast; |
| 40 | bool is_in_else; |
| 41 | int num_nest; |
| 42 | }; |
| 43 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 44 | typedef void (Parser::*DirectiveHandler)( |
| 45 | StringPiece line, StringPiece directive); |
| 46 | typedef unordered_map<StringPiece, DirectiveHandler> DirectiveMap; |
| 47 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 48 | public: |
| 49 | Parser(StringPiece buf, const char* filename, vector<AST*>* asts) |
| 50 | : buf_(buf), |
| 51 | state_(ParserState::NOT_AFTER_RULE), |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 52 | asts_(asts), |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 53 | out_asts_(asts), |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 54 | num_if_nest_(0), |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 55 | loc_(filename, 0), |
| 56 | fixed_lineno_(false) { |
| 57 | } |
| 58 | |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 59 | 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 Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 69 | ~Parser() { |
| 70 | } |
| 71 | |
| 72 | void Parse() { |
| 73 | l_ = 0; |
| 74 | |
| 75 | for (l_ = 0; l_ < buf_.size();) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 76 | size_t lf_cnt = 0; |
| 77 | size_t e = FindEndOfLine(&lf_cnt); |
Shinichiro Hamaji | 8ee8c37 | 2015-06-16 16:19:40 +0900 | [diff] [blame] | 78 | if (!fixed_lineno_) |
Shinichiro Hamaji | b3af68b | 2015-06-24 20:47:57 +0900 | [diff] [blame] | 79 | loc_.lineno++; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 80 | StringPiece line(buf_.data() + l_, e - l_); |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 81 | orig_line_with_directives_ = line; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 82 | ParseLine(line); |
Shinichiro Hamaji | b3af68b | 2015-06-24 20:47:57 +0900 | [diff] [blame] | 83 | if (!fixed_lineno_) |
| 84 | loc_.lineno += lf_cnt - 1; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 85 | if (e == buf_.size()) |
| 86 | break; |
| 87 | |
| 88 | l_ = e + 1; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 92 | static void Init() { |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 93 | make_directives_ = new DirectiveMap; |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 94 | (*make_directives_)["include"] = &Parser::ParseInclude; |
| 95 | (*make_directives_)["-include"] = &Parser::ParseInclude; |
| 96 | (*make_directives_)["sinclude"] = &Parser::ParseInclude; |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 97 | (*make_directives_)["define"] = &Parser::ParseDefine; |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 98 | (*make_directives_)["ifdef"] = &Parser::ParseIfdef; |
| 99 | (*make_directives_)["ifndef"] = &Parser::ParseIfdef; |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 100 | (*make_directives_)["ifeq"] = &Parser::ParseIfeq; |
| 101 | (*make_directives_)["ifneq"] = &Parser::ParseIfeq; |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 102 | (*make_directives_)["else"] = &Parser::ParseElse; |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 103 | (*make_directives_)["endif"] = &Parser::ParseEndif; |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 104 | (*make_directives_)["override"] = &Parser::ParseOverride; |
| 105 | (*make_directives_)["export"] = &Parser::ParseExport; |
| 106 | (*make_directives_)["unexport"] = &Parser::ParseUnexport; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 107 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 108 | else_if_directives_ = new DirectiveMap; |
| 109 | (*else_if_directives_)["ifdef"] = &Parser::ParseIfdef; |
| 110 | (*else_if_directives_)["ifndef"] = &Parser::ParseIfdef; |
Shinichiro Hamaji | 6aeab2a | 2015-06-26 09:12:01 +0900 | [diff] [blame] | 111 | (*else_if_directives_)["ifeq"] = &Parser::ParseIfeq; |
| 112 | (*else_if_directives_)["ifneq"] = &Parser::ParseIfeq; |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 113 | |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 114 | assign_directives_ = new DirectiveMap; |
| 115 | (*assign_directives_)["define"] = &Parser::ParseDefine; |
| 116 | (*assign_directives_)["export"] = &Parser::ParseExport; |
| 117 | (*assign_directives_)["override"] = &Parser::ParseOverride; |
| 118 | |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 119 | 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 Hamaji | 631a9f8 | 2015-07-05 14:18:15 +0900 | [diff] [blame] | 132 | void set_state(ParserState st) { state_ = st; } |
| 133 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 134 | private: |
| 135 | void Error(const string& msg) { |
Shinichiro Hamaji | 8ee8c37 | 2015-06-16 16:19:40 +0900 | [diff] [blame] | 136 | ERROR("%s:%d: %s", LOCF(loc_), msg.c_str()); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | size_t FindEndOfLine(size_t* lf_cnt) { |
Shinichiro Hamaji | 14bb279 | 2015-06-25 18:24:11 +0900 | [diff] [blame] | 140 | return ::FindEndOfLine(buf_, l_, lf_cnt); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 141 | } |
| 142 | |
Shinichiro Hamaji | 36b326f | 2015-06-26 08:56:13 +0900 | [diff] [blame] | 143 | Value* ParseExpr(StringPiece s, ParseExprOpt opt = ParseExprOpt::NORMAL) { |
| 144 | return ::ParseExpr(loc_, s, opt); |
| 145 | } |
| 146 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 147 | void ParseLine(StringPiece line) { |
| 148 | if (line.empty() || (line.size() == 1 && line[0] == '\r')) |
| 149 | return; |
| 150 | |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 151 | if (!define_name_.empty()) { |
| 152 | ParseInsideDefine(line); |
| 153 | return; |
| 154 | } |
| 155 | |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 156 | current_directive_ = AssignDirective::NONE; |
| 157 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 158 | if (line[0] == '\t' && state_ != ParserState::NOT_AFTER_RULE) { |
| 159 | CommandAST* ast = new CommandAST(); |
Shinichiro Hamaji | 861bd64 | 2015-06-19 16:59:13 +0900 | [diff] [blame] | 160 | ast->set_loc(loc_); |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 161 | ast->expr = ParseExpr(line.substr(1), ParseExprOpt::COMMAND); |
Shinichiro Hamaji | 631a9f8 | 2015-07-05 14:18:15 +0900 | [diff] [blame] | 162 | ast->orig = line; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 163 | out_asts_->push_back(ast); |
| 164 | return; |
| 165 | } |
| 166 | |
Shinichiro Hamaji | 3275062 | 2015-06-17 14:57:33 +0900 | [diff] [blame] | 167 | line = TrimLeftSpace(line); |
Shinichiro Hamaji | d4e8193 | 2015-06-17 04:40:45 +0900 | [diff] [blame] | 168 | |
| 169 | if (line[0] == '#') |
| 170 | return; |
| 171 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 172 | if (HandleDirective(line, make_directives_)) { |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 173 | return; |
| 174 | } |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 175 | |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 176 | ParseRuleOrAssign(line); |
| 177 | } |
| 178 | |
| 179 | void ParseRuleOrAssign(StringPiece line) { |
Shinichiro Hamaji | 415a9b1 | 2015-07-05 14:33:02 +0900 | [diff] [blame] | 180 | size_t sep = FindThreeOutsideParen(line, ':', '=', ';'); |
| 181 | if (sep == string::npos || line[sep] == ';') { |
| 182 | ParseRule(line, string::npos); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 183 | } else if (line[sep] == '=') { |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 184 | ParseAssign(line, sep); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 185 | } else if (line.get(sep+1) == '=') { |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 186 | ParseAssign(line, sep+1); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 187 | } else if (line[sep] == ':') { |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 188 | ParseRule(line, sep); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 189 | } else { |
| 190 | CHECK(false); |
| 191 | } |
| 192 | } |
| 193 | |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 194 | void ParseRule(StringPiece line, size_t sep) { |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 195 | if (current_directive_ != AssignDirective::NONE) { |
Shinichiro Hamaji | 45a0c76 | 2015-06-26 06:47:10 +0900 | [diff] [blame] | 196 | if (IsInExport()) |
| 197 | return; |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 198 | if (sep != string::npos) { |
| 199 | sep += orig_line_with_directives_.size() - line.size(); |
| 200 | } |
| 201 | line = orig_line_with_directives_; |
Shinichiro Hamaji | df066c3 | 2015-06-26 09:09:26 +0900 | [diff] [blame] | 202 | } |
| 203 | |
Shinichiro Hamaji | 6aeab2a | 2015-06-26 09:12:01 +0900 | [diff] [blame] | 204 | line = TrimLeftSpace(line); |
Shinichiro Hamaji | df066c3 | 2015-06-26 09:09:26 +0900 | [diff] [blame] | 205 | if (line.empty()) |
| 206 | return; |
| 207 | |
| 208 | if (orig_line_with_directives_[0] == '\t') { |
Shinichiro Hamaji | 2bed770 | 2015-06-26 07:56:28 +0900 | [diff] [blame] | 209 | Error("*** commands commence before first target."); |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 210 | } |
| 211 | |
Shinichiro Hamaji | 76ff983 | 2015-06-18 17:11:22 +0900 | [diff] [blame] | 212 | const bool is_rule = sep != string::npos && line[sep] == ':'; |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 213 | RuleAST* ast = new RuleAST(); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 214 | ast->set_loc(loc_); |
| 215 | |
Shinichiro Hamaji | 76ff983 | 2015-06-18 17:11:22 +0900 | [diff] [blame] | 216 | size_t found = FindTwoOutsideParen(line.substr(sep + 1), '=', ';'); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 217 | if (found != string::npos) { |
| 218 | found += sep + 1; |
| 219 | ast->term = line[found]; |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 220 | 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 Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 224 | } else { |
| 225 | ast->term = 0; |
| 226 | ast->after_term = NULL; |
Shinichiro Hamaji | df066c3 | 2015-06-26 09:09:26 +0900 | [diff] [blame] | 227 | ast->expr = ParseExpr(line); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 228 | } |
| 229 | out_asts_->push_back(ast); |
| 230 | state_ = is_rule ? ParserState::AFTER_RULE : ParserState::MAYBE_AFTER_RULE; |
| 231 | } |
| 232 | |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 233 | void ParseAssign(StringPiece line, size_t sep) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 234 | if (sep == 0) |
| 235 | Error("*** empty variable name ***"); |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 236 | StringPiece lhs; |
| 237 | StringPiece rhs; |
| 238 | AssignOp op; |
| 239 | ParseAssignStatement(line, sep, &lhs, &rhs, &op); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 240 | |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 241 | AssignAST* ast = new AssignAST(); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 242 | ast->set_loc(loc_); |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 243 | ast->lhs = ParseExpr(lhs); |
| 244 | ast->rhs = ParseExpr(rhs); |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 245 | ast->orig_rhs = rhs; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 246 | ast->op = op; |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 247 | ast->directive = current_directive_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 248 | out_asts_->push_back(ast); |
| 249 | state_ = ParserState::NOT_AFTER_RULE; |
| 250 | } |
| 251 | |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 252 | void ParseInclude(StringPiece line, StringPiece directive) { |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 253 | IncludeAST* ast = new IncludeAST(); |
Shinichiro Hamaji | 861bd64 | 2015-06-19 16:59:13 +0900 | [diff] [blame] | 254 | ast->set_loc(loc_); |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 255 | ast->expr = ParseExpr(line); |
Shinichiro Hamaji | efad2dd | 2015-06-17 03:08:02 +0900 | [diff] [blame] | 256 | ast->should_exist = directive[0] == 'i'; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 257 | out_asts_->push_back(ast); |
Shinichiro Hamaji | ff4584d | 2015-06-24 17:45:14 +0900 | [diff] [blame] | 258 | state_ = ParserState::NOT_AFTER_RULE; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 259 | } |
| 260 | |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 261 | 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 Hamaji | ff4584d | 2015-06-24 17:45:14 +0900 | [diff] [blame] | 268 | state_ = ParserState::NOT_AFTER_RULE; |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void ParseInsideDefine(StringPiece line) { |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 272 | line = TrimLeftSpace(line); |
| 273 | if (GetDirective(line) != "endef") { |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 274 | if (define_start_ == 0) |
| 275 | define_start_ = l_; |
| 276 | return; |
| 277 | } |
| 278 | |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 279 | 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 Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 285 | AssignAST* ast = new AssignAST(); |
| 286 | ast->set_loc(Loc(loc_.filename, define_start_line_)); |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 287 | ast->lhs = ParseExpr(define_name_); |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 288 | StringPiece rhs; |
| 289 | if (define_start_) |
Shinichiro Hamaji | a4d0ecb | 2015-06-24 22:08:30 +0900 | [diff] [blame] | 290 | rhs = buf_.substr(define_start_, l_ - define_start_ - 1); |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 291 | ast->rhs = ParseExpr(rhs, ParseExprOpt::DEFINE); |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 292 | ast->orig_rhs = rhs; |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 293 | ast->op = AssignOp::EQ; |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 294 | ast->directive = current_directive_; |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 295 | out_asts_->push_back(ast); |
| 296 | define_name_.clear(); |
| 297 | } |
| 298 | |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 299 | 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 Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 308 | 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 Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 312 | ast->lhs = ParseExpr(line); |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 313 | ast->rhs = NULL; |
| 314 | out_asts_->push_back(ast); |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 315 | EnterIf(ast); |
| 316 | } |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 317 | |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 318 | 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 Hamaji | 36b326f | 2015-06-26 08:56:13 +0900 | [diff] [blame] | 327 | ast->lhs = ParseExprImpl(loc_, s, terms, ParseExprOpt::NORMAL, &n, true); |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 328 | if (s[n] != ',') |
| 329 | return false; |
| 330 | s = TrimLeftSpace(s.substr(n+1)); |
Shinichiro Hamaji | 36b326f | 2015-06-26 08:56:13 +0900 | [diff] [blame] | 331 | ast->rhs = ParseExprImpl(loc_, s, NULL, ParseExprOpt::NORMAL, &n); |
Shinichiro Hamaji | adeea69 | 2015-06-17 17:57:31 +0900 | [diff] [blame] | 332 | s = TrimLeftSpace(s.substr(n)); |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 333 | } 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 Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 343 | Value* v = ParseExpr(s.substr(1, end - 1), ParseExprOpt::NORMAL); |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 344 | if (i == 0) |
| 345 | ast->lhs = v; |
| 346 | else |
| 347 | ast->rhs = v; |
| 348 | s = TrimLeftSpace(s.substr(end+1)); |
| 349 | } |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 350 | } |
Shinichiro Hamaji | adeea69 | 2015-06-17 17:57:31 +0900 | [diff] [blame] | 351 | if (!s.empty()) { |
| 352 | Error("extraneous text after `ifeq' directive"); |
| 353 | } |
| 354 | return true; |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 355 | } |
| 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 Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 368 | } |
| 369 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 370 | 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 Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 390 | CheckIfStack("endif"); |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 391 | if (!line.empty()) |
| 392 | Error("extraneous text after `endif` directive"); |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 393 | 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 Hamaji | 2847f09 | 2015-06-17 15:48:37 +0900 | [diff] [blame] | 402 | out_asts_ = &st->ast->false_asts; |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 403 | else |
Shinichiro Hamaji | 2847f09 | 2015-06-17 15:48:37 +0900 | [diff] [blame] | 404 | out_asts_ = &st->ast->true_asts; |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
Shinichiro Hamaji | 45a0c76 | 2015-06-26 06:47:10 +0900 | [diff] [blame] | 409 | 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 Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 423 | 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 Hamaji | 45a0c76 | 2015-06-26 06:47:10 +0900 | [diff] [blame] | 429 | if (IsInExport()) { |
| 430 | CreateExport(line, true); |
| 431 | } |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 432 | ParseRuleOrAssign(line); |
| 433 | } |
| 434 | |
| 435 | void ParseExport(StringPiece line, StringPiece) { |
Shinichiro Hamaji | 45a0c76 | 2015-06-26 06:47:10 +0900 | [diff] [blame] | 436 | 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 Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | void ParseUnexport(StringPiece line, StringPiece) { |
Shinichiro Hamaji | 45a0c76 | 2015-06-26 06:47:10 +0900 | [diff] [blame] | 447 | CreateExport(line, false); |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 448 | } |
| 449 | |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 450 | void CheckIfStack(const char* keyword) { |
| 451 | if (if_stack_.empty()) { |
| 452 | Error(StringPrintf("*** extraneous `%s'.", keyword)); |
| 453 | } |
| 454 | } |
| 455 | |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 456 | StringPiece RemoveComment(StringPiece line) { |
Shinichiro Hamaji | eafd052 | 2015-06-18 16:46:02 +0900 | [diff] [blame] | 457 | size_t i = FindOutsideParen(line, '#'); |
| 458 | if (i == string::npos) |
| 459 | return line; |
| 460 | return line.substr(0, i); |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | StringPiece GetDirective(StringPiece line) { |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 464 | if (line.size() < shortest_directive_len_) |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 465 | return StringPiece(); |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 466 | StringPiece prefix = line.substr(0, longest_directive_len_ + 1); |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 467 | 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 Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 473 | auto found = directive_map->find(directive); |
| 474 | if (found == directive_map->end()) |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 475 | return false; |
| 476 | |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 477 | StringPiece rest = TrimRightSpace(RemoveComment(TrimLeftSpace( |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 478 | line.substr(directive.size())))); |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 479 | (this->*found->second)(rest, directive); |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 480 | return true; |
| 481 | } |
| 482 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 483 | StringPiece buf_; |
| 484 | size_t l_; |
| 485 | ParserState state_; |
| 486 | |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 487 | vector<AST*>* asts_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 488 | vector<AST*>* out_asts_; |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 489 | |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 490 | StringPiece define_name_; |
| 491 | size_t define_start_; |
| 492 | int define_start_line_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 493 | |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 494 | StringPiece orig_line_with_directives_; |
| 495 | AssignDirective current_directive_; |
| 496 | |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 497 | int num_if_nest_; |
| 498 | stack<IfState*> if_stack_; |
| 499 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 500 | Loc loc_; |
| 501 | bool fixed_lineno_; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 502 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 503 | static DirectiveMap* make_directives_; |
| 504 | static DirectiveMap* else_if_directives_; |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 505 | static DirectiveMap* assign_directives_; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 506 | static size_t shortest_directive_len_; |
| 507 | static size_t longest_directive_len_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 508 | }; |
| 509 | |
| 510 | void Parse(Makefile* mk) { |
Shinichiro Hamaji | 6b1a11a | 2015-06-30 16:21:19 +0900 | [diff] [blame] | 511 | COLLECT_STATS("parse file time"); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 512 | Parser parser(StringPiece(mk->buf(), mk->len()), |
| 513 | mk->filename().c_str(), |
| 514 | mk->mutable_asts()); |
| 515 | parser.Parse(); |
| 516 | } |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 517 | |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 518 | void Parse(StringPiece buf, const Loc& loc, vector<AST*>* out_asts) { |
Shinichiro Hamaji | 6b1a11a | 2015-06-30 16:21:19 +0900 | [diff] [blame] | 519 | COLLECT_STATS("parse eval time"); |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 520 | Parser parser(buf, loc, out_asts); |
| 521 | parser.Parse(); |
| 522 | } |
| 523 | |
Shinichiro Hamaji | 631a9f8 | 2015-07-05 14:18:15 +0900 | [diff] [blame] | 524 | void 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 Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 531 | void InitParser() { |
| 532 | Parser::Init(); |
| 533 | } |
| 534 | |
| 535 | void QuitParser() { |
| 536 | Parser::Quit(); |
| 537 | } |
| 538 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 539 | Parser::DirectiveMap* Parser::make_directives_; |
| 540 | Parser::DirectiveMap* Parser::else_if_directives_; |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 541 | Parser::DirectiveMap* Parser::assign_directives_; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 542 | size_t Parser::shortest_directive_len_; |
| 543 | size_t Parser::longest_directive_len_; |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 544 | |
| 545 | void 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 | } |