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