diff options
author | Cole Faust <colefaust@google.com> | 2023-01-17 11:26:20 -0800 |
---|---|---|
committer | Cole Faust <colecfaust@gmail.com> | 2023-01-31 10:31:48 -0800 |
commit | 30588e18f58f57716205fb3a8ba8d0913bbeca58 (patch) | |
tree | d7931cc06c58cd8445ee2c12c2f4f664d744db07 | |
parent | 535ec5d0d6991e647087a8e411d737163a31603a (diff) | |
download | kati-30588e18f58f57716205fb3a8ba8d0913bbeca58.tar.gz |
Remove ParserState
ParserState was never compared to anything
except for NOT_AFTER_RULE, so it could be
a boolean instead.
-rw-r--r-- | src/eval.cc | 2 | ||||
-rw-r--r-- | src/parser.cc | 46 | ||||
-rw-r--r-- | src/parser.h | 6 |
3 files changed, 20 insertions, 34 deletions
diff --git a/src/eval.cc b/src/eval.cc index 84de857..fd78456 100644 --- a/src/eval.cc +++ b/src/eval.cc @@ -553,7 +553,7 @@ void Evaluator::EvalCommand(const CommandStmt* stmt) { if (!last_rule_) { std::vector<Stmt*> stmts; - ParseNotAfterRule(stmt->orig, stmt->loc(), &stmts); + ParseNoStats(stmt->orig, stmt->loc(), &stmts); for (Stmt* a : stmts) a->Eval(this); return; diff --git a/src/parser.cc b/src/parser.cc index 11da3c7..a67813a 100644 --- a/src/parser.cc +++ b/src/parser.cc @@ -28,12 +28,6 @@ #include "stmt.h" #include "strutil.h" -enum struct ParserState { - NOT_AFTER_RULE = 0, - AFTER_RULE, - MAYBE_AFTER_RULE, -}; - class Parser { struct IfState { IfStmt* stmt; @@ -48,20 +42,15 @@ class Parser { public: Parser(std::string_view buf, const char* filename, std::vector<Stmt*>* stmts) : buf_(buf), - state_(ParserState::NOT_AFTER_RULE), stmts_(stmts), out_stmts_(stmts), - num_define_nest_(0), - num_if_nest_(0), loc_(filename, 0), fixed_lineno_(false) {} Parser(std::string_view buf, const Loc& loc, std::vector<Stmt*>* stmts) : buf_(buf), - state_(ParserState::NOT_AFTER_RULE), stmts_(stmts), out_stmts_(stmts), - num_if_nest_(0), loc_(loc), fixed_lineno_(true) {} @@ -93,8 +82,6 @@ class Parser { "*** missing `endef', unterminated `define'."); } - void set_state(ParserState st) { state_ = st; } - static std::vector<ParseErrorStmt*> parse_errors; private: @@ -127,7 +114,7 @@ class Parser { current_directive_ = AssignDirective::NONE; - if (line[0] == '\t' && state_ != ParserState::NOT_AFTER_RULE) { + if (line[0] == '\t' && after_rule_) { CommandStmt* stmt = new CommandStmt(); stmt->set_loc(loc_); Loc mutable_loc(loc_); @@ -184,7 +171,6 @@ class Parser { return; } - const bool is_rule = sep != std::string::npos && line[sep] == ':'; RuleStmt* rule_stmt = new RuleStmt(); rule_stmt->set_loc(loc_); @@ -216,7 +202,7 @@ class Parser { rule_stmt->rhs = NULL; } out_stmts_->push_back(rule_stmt); - state_ = is_rule ? ParserState::AFTER_RULE : ParserState::MAYBE_AFTER_RULE; + after_rule_ = true; } void ParseAssign(std::string_view line, size_t separator_pos) { @@ -249,7 +235,7 @@ class Parser { stmt->directive = current_directive_; stmt->is_final = is_final; out_stmts_->push_back(stmt); - state_ = ParserState::NOT_AFTER_RULE; + after_rule_ = false; } void ParseInclude(std::string_view line, std::string_view directive) { @@ -259,7 +245,7 @@ class Parser { stmt->expr = ParseExpr(&mutable_loc, line); stmt->should_exist = directive[0] == 'i'; out_stmts_->push_back(stmt); - state_ = ParserState::NOT_AFTER_RULE; + after_rule_ = false; } void ParseDefine(std::string_view line, std::string_view) { @@ -271,7 +257,7 @@ class Parser { num_define_nest_ = 1; define_start_ = 0; define_start_line_ = loc_.lineno; - state_ = ParserState::NOT_AFTER_RULE; + after_rule_ = false; } void ParseInsideDefine(std::string_view line) { @@ -509,20 +495,23 @@ class Parser { std::string_view buf_; size_t l_; - ParserState state_; + // Represents if we just parsed a rule or an expression. + // Expressions are included because they can expand into + // a rule, see testcase/rule_in_var.mk. + bool after_rule_ = false; std::vector<Stmt*>* stmts_; std::vector<Stmt*>* out_stmts_; std::string_view define_name_; - int num_define_nest_; + int num_define_nest_ = 0; size_t define_start_; int define_start_line_; std::string_view orig_line_with_directives_; AssignDirective current_directive_; - int num_if_nest_; + int num_if_nest_ = 0; std::stack<IfState*> if_stack_; Loc loc_; @@ -537,8 +526,7 @@ class Parser { void Parse(Makefile* mk) { COLLECT_STATS("parse file time"); - Parser parser(std::string_view(mk->buf()), mk->filename().c_str(), - mk->mutable_stmts()); + Parser parser(mk->buf(), mk->filename().c_str(), mk->mutable_stmts()); parser.Parse(); } @@ -546,15 +534,13 @@ void Parse(std::string_view buf, const Loc& loc, std::vector<Stmt*>* out_stmts) { COLLECT_STATS("parse eval time"); - Parser parser(buf, loc, out_stmts); - parser.Parse(); + ParseNoStats(buf, loc, out_stmts); } -void ParseNotAfterRule(std::string_view buf, - const Loc& loc, - std::vector<Stmt*>* out_stmts) { +void ParseNoStats(std::string_view buf, + const Loc& loc, + std::vector<Stmt*>* out_stmts) { Parser parser(buf, loc, out_stmts); - parser.set_state(ParserState::NOT_AFTER_RULE); parser.Parse(); } diff --git a/src/parser.h b/src/parser.h index d0457d2..21673b3 100644 --- a/src/parser.h +++ b/src/parser.h @@ -25,9 +25,9 @@ class Makefile; void Parse(Makefile* mk); void Parse(std::string_view buf, const Loc& loc, std::vector<Stmt*>* out_asts); -void ParseNotAfterRule(std::string_view buf, - const Loc& loc, - std::vector<Stmt*>* out_asts); +void ParseNoStats(std::string_view buf, + const Loc& loc, + std::vector<Stmt*>* out_asts); void ParseAssignStatement(std::string_view line, size_t sep, |