Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 1 | #include "parser.h" |
| 2 | |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 3 | #include <stack> |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 4 | #include <unordered_map> |
| 5 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 6 | #include "ast.h" |
| 7 | #include "file.h" |
| 8 | #include "loc.h" |
| 9 | #include "log.h" |
| 10 | #include "string_piece.h" |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 11 | #include "strutil.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 12 | #include "value.h" |
| 13 | |
| 14 | enum struct ParserState { |
| 15 | NOT_AFTER_RULE = 0, |
| 16 | AFTER_RULE, |
| 17 | MAYBE_AFTER_RULE, |
| 18 | }; |
| 19 | |
| 20 | class Parser { |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 21 | struct IfState { |
| 22 | IfAST* ast; |
| 23 | bool is_in_else; |
| 24 | int num_nest; |
| 25 | }; |
| 26 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 27 | typedef void (Parser::*DirectiveHandler)( |
| 28 | StringPiece line, StringPiece directive); |
| 29 | typedef unordered_map<StringPiece, DirectiveHandler> DirectiveMap; |
| 30 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 31 | public: |
| 32 | Parser(StringPiece buf, const char* filename, vector<AST*>* asts) |
| 33 | : buf_(buf), |
| 34 | state_(ParserState::NOT_AFTER_RULE), |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 35 | asts_(asts), |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 36 | out_asts_(asts), |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 37 | num_if_nest_(0), |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 38 | loc_(filename, 0), |
| 39 | fixed_lineno_(false) { |
| 40 | } |
| 41 | |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 42 | Parser(StringPiece buf, const Loc& loc, vector<AST*>* asts) |
| 43 | : buf_(buf), |
| 44 | state_(ParserState::NOT_AFTER_RULE), |
| 45 | asts_(asts), |
| 46 | out_asts_(asts), |
| 47 | num_if_nest_(0), |
| 48 | loc_(loc), |
| 49 | fixed_lineno_(true) { |
| 50 | } |
| 51 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 52 | ~Parser() { |
| 53 | } |
| 54 | |
| 55 | void Parse() { |
| 56 | l_ = 0; |
| 57 | |
| 58 | for (l_ = 0; l_ < buf_.size();) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 59 | size_t lf_cnt = 0; |
| 60 | size_t e = FindEndOfLine(&lf_cnt); |
Shinichiro Hamaji | 8ee8c37 | 2015-06-16 16:19:40 +0900 | [diff] [blame] | 61 | if (!fixed_lineno_) |
| 62 | loc_.lineno += lf_cnt; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 63 | StringPiece line(buf_.data() + l_, e - l_); |
| 64 | ParseLine(line); |
| 65 | if (e == buf_.size()) |
| 66 | break; |
| 67 | |
| 68 | l_ = e + 1; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 72 | static void Init() { |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 73 | make_directives_ = new DirectiveMap; |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 74 | (*make_directives_)["include"] = &Parser::ParseInclude; |
| 75 | (*make_directives_)["-include"] = &Parser::ParseInclude; |
| 76 | (*make_directives_)["sinclude"] = &Parser::ParseInclude; |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 77 | (*make_directives_)["define"] = &Parser::ParseDefine; |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 78 | (*make_directives_)["ifdef"] = &Parser::ParseIfdef; |
| 79 | (*make_directives_)["ifndef"] = &Parser::ParseIfdef; |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 80 | (*make_directives_)["ifeq"] = &Parser::ParseIfeq; |
| 81 | (*make_directives_)["ifneq"] = &Parser::ParseIfeq; |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 82 | (*make_directives_)["else"] = &Parser::ParseElse; |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 83 | (*make_directives_)["endif"] = &Parser::ParseEndif; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 84 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 85 | else_if_directives_ = new DirectiveMap; |
| 86 | (*else_if_directives_)["ifdef"] = &Parser::ParseIfdef; |
| 87 | (*else_if_directives_)["ifndef"] = &Parser::ParseIfdef; |
| 88 | |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 89 | shortest_directive_len_ = 9999; |
| 90 | longest_directive_len_ = 0; |
| 91 | for (auto p : *make_directives_) { |
| 92 | size_t len = p.first.size(); |
| 93 | shortest_directive_len_ = min(len, shortest_directive_len_); |
| 94 | longest_directive_len_ = max(len, longest_directive_len_); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | static void Quit() { |
| 99 | delete make_directives_; |
| 100 | } |
| 101 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 102 | private: |
| 103 | void Error(const string& msg) { |
Shinichiro Hamaji | 8ee8c37 | 2015-06-16 16:19:40 +0900 | [diff] [blame] | 104 | ERROR("%s:%d: %s", LOCF(loc_), msg.c_str()); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | size_t FindEndOfLine(size_t* lf_cnt) { |
| 108 | size_t e = l_; |
| 109 | bool prev_backslash = false; |
| 110 | for (; e < buf_.size(); e++) { |
| 111 | char c = buf_[e]; |
| 112 | if (c == '\\') { |
| 113 | prev_backslash = !prev_backslash; |
| 114 | } else if (c == '\n') { |
| 115 | ++*lf_cnt; |
| 116 | if (!prev_backslash) { |
| 117 | return e; |
| 118 | } |
| 119 | } else if (c != '\r') { |
| 120 | prev_backslash = false; |
| 121 | } |
| 122 | } |
| 123 | return e; |
| 124 | } |
| 125 | |
| 126 | void ParseLine(StringPiece line) { |
| 127 | if (line.empty() || (line.size() == 1 && line[0] == '\r')) |
| 128 | return; |
| 129 | |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 130 | if (!define_name_.empty()) { |
| 131 | ParseInsideDefine(line); |
| 132 | return; |
| 133 | } |
| 134 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 135 | if (line[0] == '\t' && state_ != ParserState::NOT_AFTER_RULE) { |
| 136 | CommandAST* ast = new CommandAST(); |
| 137 | ast->expr = ParseExpr(line.substr(1), true); |
| 138 | out_asts_->push_back(ast); |
| 139 | return; |
| 140 | } |
| 141 | |
Shinichiro Hamaji | 3275062 | 2015-06-17 14:57:33 +0900 | [diff] [blame] | 142 | line = TrimLeftSpace(line); |
Shinichiro Hamaji | d4e8193 | 2015-06-17 04:40:45 +0900 | [diff] [blame] | 143 | |
| 144 | if (line[0] == '#') |
| 145 | return; |
| 146 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 147 | if (HandleDirective(line, make_directives_)) { |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 148 | return; |
| 149 | } |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 150 | |
| 151 | size_t sep = line.find_first_of(STRING_PIECE("=:")); |
| 152 | if (sep == string::npos) { |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 153 | ParseRule(line, sep); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 154 | } else if (line[sep] == '=') { |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 155 | ParseAssign(line, sep); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 156 | } else if (line.get(sep+1) == '=') { |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 157 | ParseAssign(line, sep+1); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 158 | } else if (line[sep] == ':') { |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 159 | ParseRule(line, sep); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 160 | } else { |
| 161 | CHECK(false); |
| 162 | } |
| 163 | } |
| 164 | |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 165 | void ParseRule(StringPiece line, size_t sep) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 166 | const bool is_rule = line.find(':') != string::npos; |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 167 | RuleAST* ast = new RuleAST(); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 168 | ast->set_loc(loc_); |
| 169 | |
| 170 | size_t found = line.substr(sep + 1).find_first_of("=;"); |
| 171 | if (found != string::npos) { |
| 172 | found += sep + 1; |
| 173 | ast->term = line[found]; |
Shinichiro Hamaji | 3275062 | 2015-06-17 14:57:33 +0900 | [diff] [blame] | 174 | ast->after_term = ParseExpr(TrimLeftSpace(line.substr(found + 1)), |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 175 | ast->term == ';'); |
Shinichiro Hamaji | 3275062 | 2015-06-17 14:57:33 +0900 | [diff] [blame] | 176 | ast->expr = ParseExpr(TrimSpace(line.substr(0, found)), false); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 177 | } else { |
| 178 | ast->term = 0; |
| 179 | ast->after_term = NULL; |
Shinichiro Hamaji | 3275062 | 2015-06-17 14:57:33 +0900 | [diff] [blame] | 180 | ast->expr = ParseExpr(TrimSpace(line), false); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 181 | } |
| 182 | out_asts_->push_back(ast); |
| 183 | state_ = is_rule ? ParserState::AFTER_RULE : ParserState::MAYBE_AFTER_RULE; |
| 184 | } |
| 185 | |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 186 | void ParseAssign(StringPiece line, size_t sep) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 187 | if (sep == 0) |
| 188 | Error("*** empty variable name ***"); |
| 189 | AssignOp op = AssignOp::EQ; |
| 190 | size_t lhs_end = sep; |
| 191 | switch (line[sep-1]) { |
| 192 | case ':': |
| 193 | lhs_end--; |
| 194 | op = AssignOp::COLON_EQ; |
| 195 | break; |
| 196 | case '+': |
| 197 | lhs_end--; |
| 198 | op = AssignOp::PLUS_EQ; |
| 199 | break; |
| 200 | case '?': |
| 201 | lhs_end--; |
| 202 | op = AssignOp::QUESTION_EQ; |
| 203 | break; |
| 204 | } |
| 205 | |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 206 | AssignAST* ast = new AssignAST(); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 207 | ast->set_loc(loc_); |
Shinichiro Hamaji | 3275062 | 2015-06-17 14:57:33 +0900 | [diff] [blame] | 208 | ast->lhs = ParseExpr(TrimSpace(line.substr(0, lhs_end)), false); |
| 209 | ast->rhs = ParseExpr(TrimSpace(line.substr(sep + 1)), false); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 210 | ast->op = op; |
| 211 | ast->directive = AssignDirective::NONE; |
| 212 | out_asts_->push_back(ast); |
| 213 | state_ = ParserState::NOT_AFTER_RULE; |
| 214 | } |
| 215 | |
Shinichiro Hamaji | 14b8bea | 2015-06-17 03:14:28 +0900 | [diff] [blame] | 216 | void ParseInclude(StringPiece line, StringPiece directive) { |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 217 | IncludeAST* ast = new IncludeAST(); |
| 218 | ast->expr = ParseExpr(line, false); |
Shinichiro Hamaji | efad2dd | 2015-06-17 03:08:02 +0900 | [diff] [blame] | 219 | ast->should_exist = directive[0] == 'i'; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 220 | out_asts_->push_back(ast); |
| 221 | } |
| 222 | |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 223 | void ParseDefine(StringPiece line, StringPiece) { |
| 224 | if (line.empty()) { |
| 225 | Error("*** empty variable name."); |
| 226 | } |
| 227 | define_name_ = line; |
| 228 | define_start_ = 0; |
| 229 | define_start_line_ = loc_.lineno; |
| 230 | } |
| 231 | |
| 232 | void ParseInsideDefine(StringPiece line) { |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 233 | line = TrimLeftSpace(line); |
| 234 | if (GetDirective(line) != "endef") { |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 235 | if (define_start_ == 0) |
| 236 | define_start_ = l_; |
| 237 | return; |
| 238 | } |
| 239 | |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 240 | StringPiece rest = TrimRightSpace(RemoveComment(TrimLeftSpace( |
| 241 | line.substr(sizeof("endef"))))); |
| 242 | if (!rest.empty()) { |
| 243 | WARN("%s:%d: extraneous text after `endef' directive", LOCF(loc_)); |
| 244 | } |
| 245 | |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 246 | AssignAST* ast = new AssignAST(); |
| 247 | ast->set_loc(Loc(loc_.filename, define_start_line_)); |
| 248 | ast->lhs = ParseExpr(define_name_, false); |
| 249 | StringPiece rhs; |
| 250 | if (define_start_) |
Shinichiro Hamaji | 3275062 | 2015-06-17 14:57:33 +0900 | [diff] [blame] | 251 | rhs = TrimRightSpace(buf_.substr(define_start_, l_ - define_start_)); |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 252 | ast->rhs = ParseExpr(rhs, false); |
| 253 | ast->op = AssignOp::EQ; |
| 254 | ast->directive = AssignDirective::NONE; |
| 255 | out_asts_->push_back(ast); |
| 256 | define_name_.clear(); |
| 257 | } |
| 258 | |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 259 | void EnterIf(IfAST* ast) { |
| 260 | IfState* st = new IfState(); |
| 261 | st->ast = ast; |
| 262 | st->is_in_else = false; |
| 263 | st->num_nest = num_if_nest_; |
| 264 | if_stack_.push(st); |
| 265 | out_asts_ = &ast->true_asts; |
| 266 | } |
| 267 | |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 268 | void ParseIfdef(StringPiece line, StringPiece directive) { |
| 269 | IfAST* ast = new IfAST(); |
| 270 | ast->set_loc(loc_); |
| 271 | ast->op = directive[2] == 'n' ? CondOp::IFNDEF : CondOp::IFDEF; |
| 272 | ast->lhs = ParseExpr(line, false); |
| 273 | ast->rhs = NULL; |
| 274 | out_asts_->push_back(ast); |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 275 | EnterIf(ast); |
| 276 | } |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 277 | |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 278 | bool ParseIfEqCond(StringPiece s, IfAST* ast) { |
| 279 | if (s.empty()) { |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | if (s[0] == '(' && s[s.size() - 1] == ')') { |
| 284 | s = s.substr(1, s.size() - 2); |
| 285 | char terms[] = {',', '\0'}; |
| 286 | size_t n; |
| 287 | ast->lhs = ParseExprImpl(s, terms, false, &n, true); |
| 288 | if (s[n] != ',') |
| 289 | return false; |
| 290 | s = TrimLeftSpace(s.substr(n+1)); |
| 291 | ast->rhs = ParseExprImpl(s, NULL, false, &n); |
Shinichiro Hamaji | adeea69 | 2015-06-17 17:57:31 +0900 | [diff] [blame] | 292 | s = TrimLeftSpace(s.substr(n)); |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 293 | } else { |
| 294 | for (int i = 0; i < 2; i++) { |
| 295 | if (s.empty()) |
| 296 | return false; |
| 297 | char quote = s[0]; |
| 298 | if (quote != '\'' && quote != '"') |
| 299 | return false; |
| 300 | size_t end = s.find(quote, 1); |
| 301 | if (end == string::npos) |
| 302 | return false; |
| 303 | Value* v = ParseExpr(s.substr(1, end - 1), false); |
| 304 | if (i == 0) |
| 305 | ast->lhs = v; |
| 306 | else |
| 307 | ast->rhs = v; |
| 308 | s = TrimLeftSpace(s.substr(end+1)); |
| 309 | } |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 310 | } |
Shinichiro Hamaji | adeea69 | 2015-06-17 17:57:31 +0900 | [diff] [blame] | 311 | if (!s.empty()) { |
| 312 | Error("extraneous text after `ifeq' directive"); |
| 313 | } |
| 314 | return true; |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | void ParseIfeq(StringPiece line, StringPiece directive) { |
| 318 | IfAST* ast = new IfAST(); |
| 319 | ast->set_loc(loc_); |
| 320 | ast->op = directive[2] == 'n' ? CondOp::IFNEQ : CondOp::IFEQ; |
| 321 | |
| 322 | if (!ParseIfEqCond(line, ast)) { |
| 323 | Error("*** invalid syntax in conditional."); |
| 324 | } |
| 325 | |
| 326 | out_asts_->push_back(ast); |
| 327 | EnterIf(ast); |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 328 | } |
| 329 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 330 | void ParseElse(StringPiece line, StringPiece) { |
| 331 | CheckIfStack("else"); |
| 332 | IfState* st = if_stack_.top(); |
| 333 | if (st->is_in_else) |
| 334 | Error("*** only one `else' per conditional."); |
| 335 | st->is_in_else = true; |
| 336 | out_asts_ = &st->ast->false_asts; |
| 337 | |
| 338 | StringPiece next_if = TrimLeftSpace(line); |
| 339 | if (next_if.empty()) |
| 340 | return; |
| 341 | |
| 342 | num_if_nest_ = st->num_nest + 1; |
| 343 | if (!HandleDirective(next_if, else_if_directives_)) { |
| 344 | WARN("%s:%d: extraneous text after `else' directive", LOCF(loc_)); |
| 345 | } |
| 346 | num_if_nest_ = 0; |
| 347 | } |
| 348 | |
| 349 | void ParseEndif(StringPiece line, StringPiece) { |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 350 | CheckIfStack("endif"); |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 351 | if (!line.empty()) |
| 352 | Error("extraneous text after `endif` directive"); |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 353 | IfState st = *if_stack_.top(); |
| 354 | for (int t = 0; t <= st.num_nest; t++) { |
| 355 | delete if_stack_.top(); |
| 356 | if_stack_.pop(); |
| 357 | if (if_stack_.empty()) { |
| 358 | out_asts_ = asts_; |
| 359 | } else { |
| 360 | IfState* st = if_stack_.top(); |
| 361 | if (st->is_in_else) |
Shinichiro Hamaji | 2847f09 | 2015-06-17 15:48:37 +0900 | [diff] [blame] | 362 | out_asts_ = &st->ast->false_asts; |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 363 | else |
Shinichiro Hamaji | 2847f09 | 2015-06-17 15:48:37 +0900 | [diff] [blame] | 364 | out_asts_ = &st->ast->true_asts; |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | void CheckIfStack(const char* keyword) { |
| 370 | if (if_stack_.empty()) { |
| 371 | Error(StringPrintf("*** extraneous `%s'.", keyword)); |
| 372 | } |
| 373 | } |
| 374 | |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 375 | StringPiece RemoveComment(StringPiece line) { |
Shinichiro Hamaji | eafd052 | 2015-06-18 16:46:02 +0900 | [diff] [blame^] | 376 | size_t i = FindOutsideParen(line, '#'); |
| 377 | if (i == string::npos) |
| 378 | return line; |
| 379 | return line.substr(0, i); |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | StringPiece GetDirective(StringPiece line) { |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 383 | if (line.size() < shortest_directive_len_) |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 384 | return StringPiece(); |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 385 | StringPiece prefix = line.substr(0, longest_directive_len_ + 1); |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 386 | size_t space_index = prefix.find_first_of(" \t#"); |
| 387 | return prefix.substr(0, space_index); |
| 388 | } |
| 389 | |
| 390 | bool HandleDirective(StringPiece line, const DirectiveMap* directive_map) { |
| 391 | StringPiece directive = GetDirective(line); |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 392 | auto found = directive_map->find(directive); |
| 393 | if (found == directive_map->end()) |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 394 | return false; |
| 395 | |
Shinichiro Hamaji | 5562caf | 2015-06-17 17:08:40 +0900 | [diff] [blame] | 396 | StringPiece rest = TrimRightSpace(RemoveComment(TrimLeftSpace( |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 397 | line.substr(directive.size())))); |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 398 | (this->*found->second)(rest, directive); |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 399 | return true; |
| 400 | } |
| 401 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 402 | StringPiece buf_; |
| 403 | size_t l_; |
| 404 | ParserState state_; |
| 405 | |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 406 | vector<AST*>* asts_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 407 | vector<AST*>* out_asts_; |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 408 | |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 409 | StringPiece define_name_; |
| 410 | size_t define_start_; |
| 411 | int define_start_line_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 412 | |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 413 | int num_if_nest_; |
| 414 | stack<IfState*> if_stack_; |
| 415 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 416 | Loc loc_; |
| 417 | bool fixed_lineno_; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 418 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 419 | static DirectiveMap* make_directives_; |
| 420 | static DirectiveMap* else_if_directives_; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 421 | static size_t shortest_directive_len_; |
| 422 | static size_t longest_directive_len_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 423 | }; |
| 424 | |
| 425 | void Parse(Makefile* mk) { |
| 426 | Parser parser(StringPiece(mk->buf(), mk->len()), |
| 427 | mk->filename().c_str(), |
| 428 | mk->mutable_asts()); |
| 429 | parser.Parse(); |
| 430 | } |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 431 | |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 432 | void Parse(StringPiece buf, const Loc& loc, vector<AST*>* out_asts) { |
| 433 | Parser parser(buf, loc, out_asts); |
| 434 | parser.Parse(); |
| 435 | } |
| 436 | |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 437 | void InitParser() { |
| 438 | Parser::Init(); |
| 439 | } |
| 440 | |
| 441 | void QuitParser() { |
| 442 | Parser::Quit(); |
| 443 | } |
| 444 | |
Shinichiro Hamaji | 6cb1c25 | 2015-06-17 16:22:51 +0900 | [diff] [blame] | 445 | Parser::DirectiveMap* Parser::make_directives_; |
| 446 | Parser::DirectiveMap* Parser::else_if_directives_; |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 447 | size_t Parser::shortest_directive_len_; |
| 448 | size_t Parser::longest_directive_len_; |