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" |
| 26 | #include "string_piece.h" |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 27 | #include "strutil.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 28 | #include "value.h" |
| 29 | |
| 30 | enum struct ParserState { |
| 31 | NOT_AFTER_RULE = 0, |
| 32 | AFTER_RULE, |
| 33 | MAYBE_AFTER_RULE, |
| 34 | }; |
| 35 | |
| 36 | class Parser { |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 37 | struct IfState { |
| 38 | IfAST* ast; |
| 39 | bool is_in_else; |
| 40 | int num_nest; |
| 41 | }; |
| 42 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 43 | typedef void (Parser::*DirectiveHandler)( |
| 44 | StringPiece line, StringPiece directive); |
| 45 | typedef unordered_map<StringPiece, DirectiveHandler> DirectiveMap; |
| 46 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 47 | public: |
| 48 | Parser(StringPiece buf, const char* filename, vector<AST*>* asts) |
| 49 | : buf_(buf), |
| 50 | state_(ParserState::NOT_AFTER_RULE), |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 51 | asts_(asts), |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 52 | out_asts_(asts), |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 53 | num_if_nest_(0), |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 54 | loc_(filename, 0), |
| 55 | fixed_lineno_(false) { |
| 56 | } |
| 57 | |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 58 | 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 Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 68 | ~Parser() { |
| 69 | } |
| 70 | |
| 71 | void Parse() { |
| 72 | l_ = 0; |
| 73 | |
| 74 | for (l_ = 0; l_ < buf_.size();) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 75 | size_t lf_cnt = 0; |
| 76 | size_t e = FindEndOfLine(&lf_cnt); |
Shinichiro Hamaji | 8ee8c37 | 2015-06-16 16:19:40 +0900 | [diff] [blame] | 77 | if (!fixed_lineno_) |
Shinichiro Hamaji | b3af68b | 2015-06-24 20:47:57 +0900 | [diff] [blame] | 78 | loc_.lineno++; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 79 | StringPiece line(buf_.data() + l_, e - l_); |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 80 | orig_line_with_directives_ = line; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 81 | ParseLine(line); |
Shinichiro Hamaji | b3af68b | 2015-06-24 20:47:57 +0900 | [diff] [blame] | 82 | if (!fixed_lineno_) |
| 83 | loc_.lineno += lf_cnt - 1; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 84 | if (e == buf_.size()) |
| 85 | break; |
| 86 | |
| 87 | l_ = e + 1; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 91 | static void Init() { |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 92 | make_directives_ = new DirectiveMap; |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 93 | (*make_directives_)["include"] = &Parser::ParseInclude; |
| 94 | (*make_directives_)["-include"] = &Parser::ParseInclude; |
| 95 | (*make_directives_)["sinclude"] = &Parser::ParseInclude; |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 96 | (*make_directives_)["define"] = &Parser::ParseDefine; |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 97 | (*make_directives_)["ifdef"] = &Parser::ParseIfdef; |
| 98 | (*make_directives_)["ifndef"] = &Parser::ParseIfdef; |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 99 | (*make_directives_)["ifeq"] = &Parser::ParseIfeq; |
| 100 | (*make_directives_)["ifneq"] = &Parser::ParseIfeq; |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 101 | (*make_directives_)["else"] = &Parser::ParseElse; |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 102 | (*make_directives_)["endif"] = &Parser::ParseEndif; |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 103 | (*make_directives_)["override"] = &Parser::ParseOverride; |
| 104 | (*make_directives_)["export"] = &Parser::ParseExport; |
| 105 | (*make_directives_)["unexport"] = &Parser::ParseUnexport; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 106 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 107 | else_if_directives_ = new DirectiveMap; |
| 108 | (*else_if_directives_)["ifdef"] = &Parser::ParseIfdef; |
| 109 | (*else_if_directives_)["ifndef"] = &Parser::ParseIfdef; |
| 110 | |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 111 | assign_directives_ = new DirectiveMap; |
| 112 | (*assign_directives_)["define"] = &Parser::ParseDefine; |
| 113 | (*assign_directives_)["export"] = &Parser::ParseExport; |
| 114 | (*assign_directives_)["override"] = &Parser::ParseOverride; |
| 115 | |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 116 | 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 Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 129 | private: |
| 130 | void Error(const string& msg) { |
Shinichiro Hamaji | 8ee8c37 | 2015-06-16 16:19:40 +0900 | [diff] [blame] | 131 | ERROR("%s:%d: %s", LOCF(loc_), msg.c_str()); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | size_t FindEndOfLine(size_t* lf_cnt) { |
Shinichiro Hamaji | 14bb279 | 2015-06-25 18:24:11 +0900 | [diff] [blame] | 135 | return ::FindEndOfLine(buf_, l_, lf_cnt); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 136 | } |
| 137 | |
Shinichiro Hamaji | 36b326f | 2015-06-26 08:56:13 +0900 | [diff] [blame^] | 138 | Value* ParseExpr(StringPiece s, ParseExprOpt opt = ParseExprOpt::NORMAL) { |
| 139 | return ::ParseExpr(loc_, s, opt); |
| 140 | } |
| 141 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 142 | void ParseLine(StringPiece line) { |
| 143 | if (line.empty() || (line.size() == 1 && line[0] == '\r')) |
| 144 | return; |
| 145 | |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 146 | if (!define_name_.empty()) { |
| 147 | ParseInsideDefine(line); |
| 148 | return; |
| 149 | } |
| 150 | |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 151 | current_directive_ = AssignDirective::NONE; |
| 152 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 153 | if (line[0] == '\t' && state_ != ParserState::NOT_AFTER_RULE) { |
| 154 | CommandAST* ast = new CommandAST(); |
Shinichiro Hamaji | 861bd64 | 2015-06-19 16:59:13 +0900 | [diff] [blame] | 155 | ast->set_loc(loc_); |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 156 | ast->expr = ParseExpr(line.substr(1), ParseExprOpt::COMMAND); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 157 | out_asts_->push_back(ast); |
| 158 | return; |
| 159 | } |
| 160 | |
Shinichiro Hamaji | 3275062 | 2015-06-17 14:57:33 +0900 | [diff] [blame] | 161 | line = TrimLeftSpace(line); |
Shinichiro Hamaji | d4e8193 | 2015-06-17 04:40:45 +0900 | [diff] [blame] | 162 | |
| 163 | if (line[0] == '#') |
| 164 | return; |
| 165 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 166 | if (HandleDirective(line, make_directives_)) { |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 167 | return; |
| 168 | } |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 169 | |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 170 | ParseRuleOrAssign(line); |
| 171 | } |
| 172 | |
| 173 | void ParseRuleOrAssign(StringPiece line) { |
Shinichiro Hamaji | 76ff983 | 2015-06-18 17:11:22 +0900 | [diff] [blame] | 174 | size_t sep = FindTwoOutsideParen(line, ':', '='); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 175 | if (sep == string::npos) { |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 176 | ParseRule(line, sep); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 177 | } else if (line[sep] == '=') { |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 178 | ParseAssign(line, sep); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 179 | } else if (line.get(sep+1) == '=') { |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 180 | ParseAssign(line, sep+1); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 181 | } else if (line[sep] == ':') { |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 182 | ParseRule(line, sep); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 183 | } else { |
| 184 | CHECK(false); |
| 185 | } |
| 186 | } |
| 187 | |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 188 | void ParseRule(StringPiece line, size_t sep) { |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 189 | if (current_directive_ != AssignDirective::NONE) { |
Shinichiro Hamaji | 45a0c76 | 2015-06-26 06:47:10 +0900 | [diff] [blame] | 190 | if (IsInExport()) |
| 191 | return; |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 192 | if (sep != string::npos) { |
| 193 | sep += orig_line_with_directives_.size() - line.size(); |
| 194 | } |
| 195 | line = orig_line_with_directives_; |
Shinichiro Hamaji | 2bed770 | 2015-06-26 07:56:28 +0900 | [diff] [blame] | 196 | } else if (orig_line_with_directives_[0] == '\t') { |
| 197 | Error("*** commands commence before first target."); |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 198 | } |
| 199 | |
Shinichiro Hamaji | 76ff983 | 2015-06-18 17:11:22 +0900 | [diff] [blame] | 200 | const bool is_rule = sep != string::npos && line[sep] == ':'; |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 201 | RuleAST* ast = new RuleAST(); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 202 | ast->set_loc(loc_); |
| 203 | |
Shinichiro Hamaji | 76ff983 | 2015-06-18 17:11:22 +0900 | [diff] [blame] | 204 | size_t found = FindTwoOutsideParen(line.substr(sep + 1), '=', ';'); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 205 | if (found != string::npos) { |
| 206 | found += sep + 1; |
| 207 | ast->term = line[found]; |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 208 | 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 Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 212 | } else { |
| 213 | ast->term = 0; |
| 214 | ast->after_term = NULL; |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 215 | ast->expr = ParseExpr(TrimSpace(line)); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 216 | } |
| 217 | out_asts_->push_back(ast); |
| 218 | state_ = is_rule ? ParserState::AFTER_RULE : ParserState::MAYBE_AFTER_RULE; |
| 219 | } |
| 220 | |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 221 | void ParseAssign(StringPiece line, size_t sep) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 222 | if (sep == 0) |
| 223 | Error("*** empty variable name ***"); |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 224 | StringPiece lhs; |
| 225 | StringPiece rhs; |
| 226 | AssignOp op; |
| 227 | ParseAssignStatement(line, sep, &lhs, &rhs, &op); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 228 | |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 229 | AssignAST* ast = new AssignAST(); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 230 | ast->set_loc(loc_); |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 231 | ast->lhs = ParseExpr(lhs); |
| 232 | ast->rhs = ParseExpr(rhs); |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 233 | ast->orig_rhs = rhs; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 234 | ast->op = op; |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 235 | ast->directive = current_directive_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 236 | out_asts_->push_back(ast); |
| 237 | state_ = ParserState::NOT_AFTER_RULE; |
| 238 | } |
| 239 | |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 240 | void ParseInclude(StringPiece line, StringPiece directive) { |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 241 | IncludeAST* ast = new IncludeAST(); |
Shinichiro Hamaji | 861bd64 | 2015-06-19 16:59:13 +0900 | [diff] [blame] | 242 | ast->set_loc(loc_); |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 243 | ast->expr = ParseExpr(line); |
Shinichiro Hamaji | efad2dd | 2015-06-17 03:08:02 +0900 | [diff] [blame] | 244 | ast->should_exist = directive[0] == 'i'; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 245 | out_asts_->push_back(ast); |
Shinichiro Hamaji | ff4584d | 2015-06-24 17:45:14 +0900 | [diff] [blame] | 246 | state_ = ParserState::NOT_AFTER_RULE; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 247 | } |
| 248 | |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 249 | 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 Hamaji | ff4584d | 2015-06-24 17:45:14 +0900 | [diff] [blame] | 256 | state_ = ParserState::NOT_AFTER_RULE; |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | void ParseInsideDefine(StringPiece line) { |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 260 | line = TrimLeftSpace(line); |
| 261 | if (GetDirective(line) != "endef") { |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 262 | if (define_start_ == 0) |
| 263 | define_start_ = l_; |
| 264 | return; |
| 265 | } |
| 266 | |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 267 | 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 Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 273 | AssignAST* ast = new AssignAST(); |
| 274 | ast->set_loc(Loc(loc_.filename, define_start_line_)); |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 275 | ast->lhs = ParseExpr(define_name_); |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 276 | StringPiece rhs; |
| 277 | if (define_start_) |
Shinichiro Hamaji | a4d0ecb | 2015-06-24 22:08:30 +0900 | [diff] [blame] | 278 | rhs = buf_.substr(define_start_, l_ - define_start_ - 1); |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 279 | ast->rhs = ParseExpr(rhs, ParseExprOpt::DEFINE); |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 280 | ast->orig_rhs = rhs; |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 281 | ast->op = AssignOp::EQ; |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 282 | ast->directive = current_directive_; |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 283 | out_asts_->push_back(ast); |
| 284 | define_name_.clear(); |
| 285 | } |
| 286 | |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 287 | 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 Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 296 | 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 Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 300 | ast->lhs = ParseExpr(line); |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 301 | ast->rhs = NULL; |
| 302 | out_asts_->push_back(ast); |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 303 | EnterIf(ast); |
| 304 | } |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 305 | |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 306 | 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 Hamaji | 36b326f | 2015-06-26 08:56:13 +0900 | [diff] [blame^] | 315 | ast->lhs = ParseExprImpl(loc_, s, terms, ParseExprOpt::NORMAL, &n, true); |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 316 | if (s[n] != ',') |
| 317 | return false; |
| 318 | s = TrimLeftSpace(s.substr(n+1)); |
Shinichiro Hamaji | 36b326f | 2015-06-26 08:56:13 +0900 | [diff] [blame^] | 319 | ast->rhs = ParseExprImpl(loc_, s, NULL, ParseExprOpt::NORMAL, &n); |
Shinichiro Hamaji | adeea69 | 2015-06-17 17:57:31 +0900 | [diff] [blame] | 320 | s = TrimLeftSpace(s.substr(n)); |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 321 | } 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 Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 331 | Value* v = ParseExpr(s.substr(1, end - 1), ParseExprOpt::NORMAL); |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 332 | if (i == 0) |
| 333 | ast->lhs = v; |
| 334 | else |
| 335 | ast->rhs = v; |
| 336 | s = TrimLeftSpace(s.substr(end+1)); |
| 337 | } |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 338 | } |
Shinichiro Hamaji | adeea69 | 2015-06-17 17:57:31 +0900 | [diff] [blame] | 339 | if (!s.empty()) { |
| 340 | Error("extraneous text after `ifeq' directive"); |
| 341 | } |
| 342 | return true; |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 343 | } |
| 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 Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 356 | } |
| 357 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 358 | 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 Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 378 | CheckIfStack("endif"); |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 379 | if (!line.empty()) |
| 380 | Error("extraneous text after `endif` directive"); |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 381 | 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 Hamaji | 2847f09 | 2015-06-17 15:48:37 +0900 | [diff] [blame] | 390 | out_asts_ = &st->ast->false_asts; |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 391 | else |
Shinichiro Hamaji | 2847f09 | 2015-06-17 15:48:37 +0900 | [diff] [blame] | 392 | out_asts_ = &st->ast->true_asts; |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
Shinichiro Hamaji | 45a0c76 | 2015-06-26 06:47:10 +0900 | [diff] [blame] | 397 | 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 Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 411 | 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 Hamaji | 45a0c76 | 2015-06-26 06:47:10 +0900 | [diff] [blame] | 417 | if (IsInExport()) { |
| 418 | CreateExport(line, true); |
| 419 | } |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 420 | ParseRuleOrAssign(line); |
| 421 | } |
| 422 | |
| 423 | void ParseExport(StringPiece line, StringPiece) { |
Shinichiro Hamaji | 45a0c76 | 2015-06-26 06:47:10 +0900 | [diff] [blame] | 424 | 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 Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | void ParseUnexport(StringPiece line, StringPiece) { |
Shinichiro Hamaji | 45a0c76 | 2015-06-26 06:47:10 +0900 | [diff] [blame] | 435 | CreateExport(line, false); |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 436 | } |
| 437 | |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 438 | void CheckIfStack(const char* keyword) { |
| 439 | if (if_stack_.empty()) { |
| 440 | Error(StringPrintf("*** extraneous `%s'.", keyword)); |
| 441 | } |
| 442 | } |
| 443 | |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 444 | StringPiece RemoveComment(StringPiece line) { |
Shinichiro Hamaji | eafd052 | 2015-06-18 16:46:02 +0900 | [diff] [blame] | 445 | size_t i = FindOutsideParen(line, '#'); |
| 446 | if (i == string::npos) |
| 447 | return line; |
| 448 | return line.substr(0, i); |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | StringPiece GetDirective(StringPiece line) { |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 452 | if (line.size() < shortest_directive_len_) |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 453 | return StringPiece(); |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 454 | StringPiece prefix = line.substr(0, longest_directive_len_ + 1); |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 455 | 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 Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 461 | auto found = directive_map->find(directive); |
| 462 | if (found == directive_map->end()) |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 463 | return false; |
| 464 | |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 465 | StringPiece rest = TrimRightSpace(RemoveComment(TrimLeftSpace( |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 466 | line.substr(directive.size())))); |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 467 | (this->*found->second)(rest, directive); |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 468 | return true; |
| 469 | } |
| 470 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 471 | StringPiece buf_; |
| 472 | size_t l_; |
| 473 | ParserState state_; |
| 474 | |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 475 | vector<AST*>* asts_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 476 | vector<AST*>* out_asts_; |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 477 | |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 478 | StringPiece define_name_; |
| 479 | size_t define_start_; |
| 480 | int define_start_line_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 481 | |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 482 | StringPiece orig_line_with_directives_; |
| 483 | AssignDirective current_directive_; |
| 484 | |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 485 | int num_if_nest_; |
| 486 | stack<IfState*> if_stack_; |
| 487 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 488 | Loc loc_; |
| 489 | bool fixed_lineno_; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 490 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 491 | static DirectiveMap* make_directives_; |
| 492 | static DirectiveMap* else_if_directives_; |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 493 | static DirectiveMap* assign_directives_; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 494 | static size_t shortest_directive_len_; |
| 495 | static size_t longest_directive_len_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 496 | }; |
| 497 | |
| 498 | void 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 Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 504 | |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 505 | void Parse(StringPiece buf, const Loc& loc, vector<AST*>* out_asts) { |
| 506 | Parser parser(buf, loc, out_asts); |
| 507 | parser.Parse(); |
| 508 | } |
| 509 | |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 510 | void InitParser() { |
| 511 | Parser::Init(); |
| 512 | } |
| 513 | |
| 514 | void QuitParser() { |
| 515 | Parser::Quit(); |
| 516 | } |
| 517 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 518 | Parser::DirectiveMap* Parser::make_directives_; |
| 519 | Parser::DirectiveMap* Parser::else_if_directives_; |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 520 | Parser::DirectiveMap* Parser::assign_directives_; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 521 | size_t Parser::shortest_directive_len_; |
| 522 | size_t Parser::longest_directive_len_; |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 523 | |
| 524 | void 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 | } |