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_); |
| 80 | ParseLine(line); |
Shinichiro Hamaji | b3af68b | 2015-06-24 20:47:57 +0900 | [diff] [blame] | 81 | if (!fixed_lineno_) |
| 82 | loc_.lineno += lf_cnt - 1; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 83 | if (e == buf_.size()) |
| 84 | break; |
| 85 | |
| 86 | l_ = e + 1; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 90 | static void Init() { |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 91 | make_directives_ = new DirectiveMap; |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 92 | (*make_directives_)["include"] = &Parser::ParseInclude; |
| 93 | (*make_directives_)["-include"] = &Parser::ParseInclude; |
| 94 | (*make_directives_)["sinclude"] = &Parser::ParseInclude; |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 95 | (*make_directives_)["define"] = &Parser::ParseDefine; |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 96 | (*make_directives_)["ifdef"] = &Parser::ParseIfdef; |
| 97 | (*make_directives_)["ifndef"] = &Parser::ParseIfdef; |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 98 | (*make_directives_)["ifeq"] = &Parser::ParseIfeq; |
| 99 | (*make_directives_)["ifneq"] = &Parser::ParseIfeq; |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 100 | (*make_directives_)["else"] = &Parser::ParseElse; |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 101 | (*make_directives_)["endif"] = &Parser::ParseEndif; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 102 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 103 | else_if_directives_ = new DirectiveMap; |
| 104 | (*else_if_directives_)["ifdef"] = &Parser::ParseIfdef; |
| 105 | (*else_if_directives_)["ifndef"] = &Parser::ParseIfdef; |
| 106 | |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 107 | 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 Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 120 | private: |
| 121 | void Error(const string& msg) { |
Shinichiro Hamaji | 8ee8c37 | 2015-06-16 16:19:40 +0900 | [diff] [blame] | 122 | ERROR("%s:%d: %s", LOCF(loc_), msg.c_str()); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 123 | } |
| 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 Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 148 | if (!define_name_.empty()) { |
| 149 | ParseInsideDefine(line); |
| 150 | return; |
| 151 | } |
| 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 | 76ff983 | 2015-06-18 17:11:22 +0900 | [diff] [blame] | 170 | size_t sep = FindTwoOutsideParen(line, ':', '='); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 171 | if (sep == string::npos) { |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 172 | ParseRule(line, sep); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 173 | } else if (line[sep] == '=') { |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 174 | ParseAssign(line, sep); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 175 | } else if (line.get(sep+1) == '=') { |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 176 | ParseAssign(line, sep+1); |
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 | ParseRule(line, sep); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 179 | } else { |
| 180 | CHECK(false); |
| 181 | } |
| 182 | } |
| 183 | |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 184 | void ParseRule(StringPiece line, size_t sep) { |
Shinichiro Hamaji | 76ff983 | 2015-06-18 17:11:22 +0900 | [diff] [blame] | 185 | const bool is_rule = sep != string::npos && line[sep] == ':'; |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 186 | RuleAST* ast = new RuleAST(); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 187 | ast->set_loc(loc_); |
| 188 | |
Shinichiro Hamaji | 76ff983 | 2015-06-18 17:11:22 +0900 | [diff] [blame] | 189 | size_t found = FindTwoOutsideParen(line.substr(sep + 1), '=', ';'); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 190 | if (found != string::npos) { |
| 191 | found += sep + 1; |
| 192 | ast->term = line[found]; |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 193 | 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 Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 197 | } else { |
| 198 | ast->term = 0; |
| 199 | ast->after_term = NULL; |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 200 | ast->expr = ParseExpr(TrimSpace(line)); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 201 | } |
| 202 | out_asts_->push_back(ast); |
| 203 | state_ = is_rule ? ParserState::AFTER_RULE : ParserState::MAYBE_AFTER_RULE; |
| 204 | } |
| 205 | |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 206 | void ParseAssign(StringPiece line, size_t sep) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 207 | if (sep == 0) |
| 208 | Error("*** empty variable name ***"); |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 209 | StringPiece lhs; |
| 210 | StringPiece rhs; |
| 211 | AssignOp op; |
| 212 | ParseAssignStatement(line, sep, &lhs, &rhs, &op); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 213 | |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 214 | AssignAST* ast = new AssignAST(); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 215 | ast->set_loc(loc_); |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 216 | ast->lhs = ParseExpr(lhs); |
| 217 | ast->rhs = ParseExpr(rhs); |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 218 | ast->orig_rhs = rhs; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 219 | ast->op = op; |
| 220 | ast->directive = AssignDirective::NONE; |
| 221 | out_asts_->push_back(ast); |
| 222 | state_ = ParserState::NOT_AFTER_RULE; |
| 223 | } |
| 224 | |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 225 | void ParseInclude(StringPiece line, StringPiece directive) { |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 226 | IncludeAST* ast = new IncludeAST(); |
Shinichiro Hamaji | 861bd64 | 2015-06-19 16:59:13 +0900 | [diff] [blame] | 227 | ast->set_loc(loc_); |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 228 | ast->expr = ParseExpr(line); |
Shinichiro Hamaji | efad2dd | 2015-06-17 03:08:02 +0900 | [diff] [blame] | 229 | ast->should_exist = directive[0] == 'i'; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 230 | out_asts_->push_back(ast); |
Shinichiro Hamaji | ff4584d | 2015-06-24 17:45:14 +0900 | [diff] [blame] | 231 | state_ = ParserState::NOT_AFTER_RULE; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 232 | } |
| 233 | |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 234 | 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 Hamaji | ff4584d | 2015-06-24 17:45:14 +0900 | [diff] [blame] | 241 | state_ = ParserState::NOT_AFTER_RULE; |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | void ParseInsideDefine(StringPiece line) { |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 245 | line = TrimLeftSpace(line); |
| 246 | if (GetDirective(line) != "endef") { |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 247 | if (define_start_ == 0) |
| 248 | define_start_ = l_; |
| 249 | return; |
| 250 | } |
| 251 | |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 252 | 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 Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 258 | AssignAST* ast = new AssignAST(); |
| 259 | ast->set_loc(Loc(loc_.filename, define_start_line_)); |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 260 | ast->lhs = ParseExpr(define_name_); |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 261 | StringPiece rhs; |
| 262 | if (define_start_) |
Shinichiro Hamaji | a4d0ecb | 2015-06-24 22:08:30 +0900 | [diff] [blame] | 263 | rhs = buf_.substr(define_start_, l_ - define_start_ - 1); |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 264 | ast->rhs = ParseExpr(rhs, ParseExprOpt::DEFINE); |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 265 | ast->orig_rhs = rhs; |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 266 | ast->op = AssignOp::EQ; |
| 267 | ast->directive = AssignDirective::NONE; |
| 268 | out_asts_->push_back(ast); |
| 269 | define_name_.clear(); |
| 270 | } |
| 271 | |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 272 | 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 Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 281 | 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 Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 285 | ast->lhs = ParseExpr(line); |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 286 | ast->rhs = NULL; |
| 287 | out_asts_->push_back(ast); |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 288 | EnterIf(ast); |
| 289 | } |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 290 | |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 291 | 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 Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 300 | ast->lhs = ParseExprImpl(s, terms, ParseExprOpt::NORMAL, &n, true); |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 301 | if (s[n] != ',') |
| 302 | return false; |
| 303 | s = TrimLeftSpace(s.substr(n+1)); |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 304 | ast->rhs = ParseExprImpl(s, NULL, ParseExprOpt::NORMAL, &n); |
Shinichiro Hamaji | adeea69 | 2015-06-17 17:57:31 +0900 | [diff] [blame] | 305 | s = TrimLeftSpace(s.substr(n)); |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 306 | } 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 Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 316 | Value* v = ParseExpr(s.substr(1, end - 1), ParseExprOpt::NORMAL); |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 317 | if (i == 0) |
| 318 | ast->lhs = v; |
| 319 | else |
| 320 | ast->rhs = v; |
| 321 | s = TrimLeftSpace(s.substr(end+1)); |
| 322 | } |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 323 | } |
Shinichiro Hamaji | adeea69 | 2015-06-17 17:57:31 +0900 | [diff] [blame] | 324 | if (!s.empty()) { |
| 325 | Error("extraneous text after `ifeq' directive"); |
| 326 | } |
| 327 | return true; |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 328 | } |
| 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 Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 341 | } |
| 342 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 343 | 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 Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 363 | CheckIfStack("endif"); |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 364 | if (!line.empty()) |
| 365 | Error("extraneous text after `endif` directive"); |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 366 | 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 Hamaji | 2847f09 | 2015-06-17 15:48:37 +0900 | [diff] [blame] | 375 | out_asts_ = &st->ast->false_asts; |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 376 | else |
Shinichiro Hamaji | 2847f09 | 2015-06-17 15:48:37 +0900 | [diff] [blame] | 377 | out_asts_ = &st->ast->true_asts; |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void CheckIfStack(const char* keyword) { |
| 383 | if (if_stack_.empty()) { |
| 384 | Error(StringPrintf("*** extraneous `%s'.", keyword)); |
| 385 | } |
| 386 | } |
| 387 | |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 388 | StringPiece RemoveComment(StringPiece line) { |
Shinichiro Hamaji | eafd052 | 2015-06-18 16:46:02 +0900 | [diff] [blame] | 389 | size_t i = FindOutsideParen(line, '#'); |
| 390 | if (i == string::npos) |
| 391 | return line; |
| 392 | return line.substr(0, i); |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | StringPiece GetDirective(StringPiece line) { |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 396 | if (line.size() < shortest_directive_len_) |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 397 | return StringPiece(); |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 398 | StringPiece prefix = line.substr(0, longest_directive_len_ + 1); |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 399 | 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 Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 405 | auto found = directive_map->find(directive); |
| 406 | if (found == directive_map->end()) |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 407 | return false; |
| 408 | |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 409 | StringPiece rest = TrimRightSpace(RemoveComment(TrimLeftSpace( |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 410 | line.substr(directive.size())))); |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 411 | (this->*found->second)(rest, directive); |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 412 | return true; |
| 413 | } |
| 414 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 415 | StringPiece buf_; |
| 416 | size_t l_; |
| 417 | ParserState state_; |
| 418 | |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 419 | vector<AST*>* asts_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 420 | vector<AST*>* out_asts_; |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 421 | |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 422 | StringPiece define_name_; |
| 423 | size_t define_start_; |
| 424 | int define_start_line_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 425 | |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 426 | int num_if_nest_; |
| 427 | stack<IfState*> if_stack_; |
| 428 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 429 | Loc loc_; |
| 430 | bool fixed_lineno_; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 431 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 432 | static DirectiveMap* make_directives_; |
| 433 | static DirectiveMap* else_if_directives_; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 434 | static size_t shortest_directive_len_; |
| 435 | static size_t longest_directive_len_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 436 | }; |
| 437 | |
| 438 | void 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 Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 444 | |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 445 | void Parse(StringPiece buf, const Loc& loc, vector<AST*>* out_asts) { |
| 446 | Parser parser(buf, loc, out_asts); |
| 447 | parser.Parse(); |
| 448 | } |
| 449 | |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 450 | void InitParser() { |
| 451 | Parser::Init(); |
| 452 | } |
| 453 | |
| 454 | void QuitParser() { |
| 455 | Parser::Quit(); |
| 456 | } |
| 457 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 458 | Parser::DirectiveMap* Parser::make_directives_; |
| 459 | Parser::DirectiveMap* Parser::else_if_directives_; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 460 | size_t Parser::shortest_directive_len_; |
| 461 | size_t Parser::longest_directive_len_; |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 462 | |
| 463 | void 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 | } |