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 "rule.h" |
| 18 | |
| 19 | #include "log.h" |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 20 | #include "parser.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 21 | #include "stringprintf.h" |
| 22 | #include "strutil.h" |
| 23 | #include "value.h" |
| 24 | |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 25 | namespace { |
| 26 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 27 | static void ParseInputs(Rule* r, StringPiece s) { |
| 28 | bool is_order_only = false; |
| 29 | for (StringPiece input : WordScanner(s)) { |
| 30 | if (input == "|") { |
| 31 | is_order_only = true; |
| 32 | continue; |
| 33 | } |
| 34 | input = Intern(TrimLeadingCurdir(input)); |
| 35 | if (is_order_only) { |
| 36 | r->order_only_inputs.push_back(input); |
| 37 | } else { |
| 38 | r->inputs.push_back(input); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 43 | bool IsPatternRule(StringPiece s) { |
| 44 | return s.find('%') != string::npos; |
| 45 | } |
| 46 | |
| 47 | } // namespace |
| 48 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 49 | Rule::Rule() |
| 50 | : is_double_colon(false), |
| 51 | is_suffix_rule(false), |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 52 | cmd_lineno(0) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 53 | } |
| 54 | |
Shinichiro Hamaji | 2928f46 | 2015-06-23 20:24:53 +0900 | [diff] [blame] | 55 | void ParseRule(Loc& loc, StringPiece line, char term, |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 56 | Rule** out_rule, RuleVarAssignment* rule_var) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 57 | size_t index = line.find(':'); |
| 58 | if (index == string::npos) { |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 59 | ERROR("%s:%d: *** missing separator.", LOCF(loc)); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | StringPiece first = line.substr(0, index); |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 63 | vector<StringPiece> outputs; |
| 64 | for (StringPiece tok : WordScanner(first)) { |
| 65 | outputs.push_back(Intern(TrimLeadingCurdir(tok))); |
| 66 | } |
| 67 | |
Shinichiro Hamaji | e646984 | 2015-06-24 14:46:28 +0900 | [diff] [blame] | 68 | const bool is_first_pattern = !outputs.empty() && IsPatternRule(outputs[0]); |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 69 | if (is_first_pattern) { |
| 70 | if (outputs.size() > 1) { |
| 71 | // TODO: Multiple output patterns are not supported yet. |
| 72 | ERROR("%s:%d: *** mixed implicit and normal rules: deprecated syntax", |
| 73 | LOCF(loc)); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 77 | bool is_double_colon = false; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 78 | index++; |
| 79 | if (line.get(index) == ':') { |
| 80 | is_double_colon = true; |
| 81 | index++; |
| 82 | } |
| 83 | |
| 84 | StringPiece rest = line.substr(index); |
Shinichiro Hamaji | 2928f46 | 2015-06-23 20:24:53 +0900 | [diff] [blame] | 85 | size_t term_index = rest.find_first_of("=;"); |
| 86 | if ((term_index != string::npos && rest[term_index] == '=') || |
| 87 | (term_index == string::npos && term == '=')) { |
| 88 | if (term_index == string::npos) |
| 89 | term_index = rest.size(); |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 90 | rule_var->outputs.swap(outputs); |
Shinichiro Hamaji | 2928f46 | 2015-06-23 20:24:53 +0900 | [diff] [blame] | 91 | ParseAssignStatement(rest, term_index, |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 92 | &rule_var->lhs, &rule_var->rhs, &rule_var->op); |
| 93 | *out_rule = NULL; |
| 94 | return; |
| 95 | } |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 96 | |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 97 | Rule* rule = new Rule(); |
| 98 | *out_rule = rule; |
| 99 | rule->loc = loc; |
| 100 | rule->is_double_colon = is_double_colon; |
| 101 | if (is_first_pattern) { |
| 102 | rule->output_patterns.swap(outputs); |
| 103 | } else { |
| 104 | rule->outputs.swap(outputs); |
| 105 | } |
Shinichiro Hamaji | 2928f46 | 2015-06-23 20:24:53 +0900 | [diff] [blame] | 106 | if (term_index != string::npos && term != ';') { |
| 107 | CHECK(rest[term_index] == ';'); |
| 108 | // TODO: Maybe better to avoid Intern here? |
| 109 | rule->cmds.push_back( |
| 110 | NewLiteral(Intern(TrimLeftSpace(rest.substr(term_index + 1))))); |
| 111 | rest = rest.substr(0, term_index); |
| 112 | } |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 113 | |
| 114 | index = rest.find(':'); |
| 115 | if (index == string::npos) { |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 116 | ParseInputs(rule, rest); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 117 | return; |
| 118 | } |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 119 | |
| 120 | if (is_first_pattern) { |
| 121 | ERROR("%s:%d: *** mixed implicit and normal rules: deprecated syntax", |
| 122 | LOCF(loc)); |
| 123 | } |
| 124 | |
| 125 | StringPiece second = rest.substr(0, index); |
| 126 | StringPiece third = rest.substr(index+1); |
| 127 | |
| 128 | for (StringPiece tok : WordScanner(second)) { |
Shinichiro Hamaji | 2e23e4a | 2015-06-26 07:33:16 +0900 | [diff] [blame] | 129 | rule->output_patterns.push_back(Intern(tok)); |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | if (rule->output_patterns.empty()) { |
| 133 | ERROR("%s:%d: *** missing target pattern.", LOCF(loc)); |
| 134 | } |
| 135 | if (rule->output_patterns.size() > 1) { |
| 136 | ERROR("%s:%d: *** multiple target patterns.", LOCF(loc)); |
| 137 | } |
| 138 | if (!IsPatternRule(rule->output_patterns[0])) { |
| 139 | ERROR("%s:%d: *** target pattern contains no '%%'.", LOCF(loc)); |
| 140 | } |
| 141 | ParseInputs(rule, third); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | string Rule::DebugString() const { |
| 145 | vector<string> v; |
| 146 | v.push_back(StringPrintf("outputs=[%s]", JoinStrings(outputs, ",").c_str())); |
| 147 | v.push_back(StringPrintf("inputs=[%s]", JoinStrings(inputs, ",").c_str())); |
| 148 | if (!order_only_inputs.empty()) { |
| 149 | v.push_back(StringPrintf("order_only_inputs=[%s]", |
| 150 | JoinStrings(order_only_inputs, ",").c_str())); |
| 151 | } |
Shinichiro Hamaji | 2e23e4a | 2015-06-26 07:33:16 +0900 | [diff] [blame] | 152 | if (!output_patterns.empty()) { |
| 153 | v.push_back(StringPrintf("output_patterns=[%s]", |
| 154 | JoinStrings(output_patterns, ",").c_str())); |
| 155 | } |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 156 | if (is_double_colon) |
| 157 | v.push_back("is_double_colon"); |
| 158 | if (is_suffix_rule) |
| 159 | v.push_back("is_suffix_rule"); |
| 160 | if (!cmds.empty()) { |
| 161 | v.push_back(StringPrintf("cmds=[%s]", JoinValues(cmds, ",").c_str())); |
| 162 | } |
| 163 | return JoinStrings(v, " "); |
| 164 | } |