OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "tools/gn/input_conversion.h" | 5 #include "tools/gn/input_conversion.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 return ParseValueOrScope(settings, input, PARSE_SCOPE, origin, err); | 140 return ParseValueOrScope(settings, input, PARSE_SCOPE, origin, err); |
141 | 141 |
142 *err = Err(original_input_conversion, "Not a valid input_conversion.", | 142 *err = Err(original_input_conversion, "Not a valid input_conversion.", |
143 "Run gn help input_conversion to see your options."); | 143 "Run gn help input_conversion to see your options."); |
144 return Value(); | 144 return Value(); |
145 } | 145 } |
146 | 146 |
147 } // namespace | 147 } // namespace |
148 | 148 |
149 extern const char kInputConversion_Help[] = | 149 extern const char kInputConversion_Help[] = |
150 "input_conversion: Specifies how to transform input to a variable.\n" | 150 R"(input_conversion: Specifies how to transform input to a variable. |
151 "\n" | 151 |
152 " input_conversion is an argument to read_file and exec_script that\n" | 152 input_conversion is an argument to read_file and exec_script that specifies |
153 " specifies how the result of the read operation should be converted\n" | 153 how the result of the read operation should be converted into a variable. |
154 " into a variable.\n" | 154 |
155 "\n" | 155 "" (the default) |
156 " \"\" (the default)\n" | 156 Discard the result and return None. |
157 " Discard the result and return None.\n" | 157 |
158 "\n" | 158 "list lines" |
159 " \"list lines\"\n" | 159 Return the file contents as a list, with a string for each line. The |
160 " Return the file contents as a list, with a string for each line.\n" | 160 newlines will not be present in the result. The last line may or may not |
161 " The newlines will not be present in the result. The last line may\n" | 161 end in a newline. |
162 " or may not end in a newline.\n" | 162 |
163 "\n" | 163 After splitting, each individual line will be trimmed of whitespace on |
164 " After splitting, each individual line will be trimmed of\n" | 164 both ends. |
165 " whitespace on both ends.\n" | 165 |
166 "\n" | 166 "scope" |
167 " \"scope\"\n" | 167 Execute the block as GN code and return a scope with the resulting values |
168 " Execute the block as GN code and return a scope with the\n" | 168 in it. If the input was: |
169 " resulting values in it. If the input was:\n" | 169 a = [ "hello.cc", "world.cc" ] |
170 " a = [ \"hello.cc\", \"world.cc\" ]\n" | 170 b = 26 |
171 " b = 26\n" | 171 and you read the result into a variable named "val", then you could |
172 " and you read the result into a variable named \"val\", then you\n" | 172 access contents the "." operator on "val": |
173 " could access contents the \".\" operator on \"val\":\n" | 173 sources = val.a |
174 " sources = val.a\n" | 174 some_count = val.b |
175 " some_count = val.b\n" | 175 |
176 "\n" | 176 "string" |
177 " \"string\"\n" | 177 Return the file contents into a single string. |
178 " Return the file contents into a single string.\n" | 178 |
179 "\n" | 179 "value" |
180 " \"value\"\n" | 180 Parse the input as if it was a literal rvalue in a buildfile. Examples of |
181 " Parse the input as if it was a literal rvalue in a buildfile.\n" | 181 typical program output using this mode: |
182 " Examples of typical program output using this mode:\n" | 182 [ "foo", "bar" ] (result will be a list) |
183 " [ \"foo\", \"bar\" ] (result will be a list)\n" | 183 or |
184 " or\n" | 184 "foo bar" (result will be a string) |
185 " \"foo bar\" (result will be a string)\n" | 185 or |
186 " or\n" | 186 5 (result will be an integer) |
187 " 5 (result will be an integer)\n" | 187 |
188 "\n" | 188 Note that if the input is empty, the result will be a null value which |
189 " Note that if the input is empty, the result will be a null value\n" | 189 will produce an error if assigned to a variable. |
190 " which will produce an error if assigned to a variable.\n" | 190 |
191 "\n" | 191 "trim ..." |
192 " \"trim ...\"\n" | 192 Prefixing any of the other transformations with the word "trim" will |
193 " Prefixing any of the other transformations with the word \"trim\"\n" | 193 result in whitespace being trimmed from the beginning and end of the |
194 " will result in whitespace being trimmed from the beginning and end\n" | 194 result before processing. |
195 " of the result before processing.\n" | 195 |
196 "\n" | 196 Examples: "trim string" or "trim list lines" |
197 " Examples: \"trim string\" or \"trim list lines\"\n" | 197 |
198 "\n" | 198 Note that "trim value" is useless because the value parser skips |
199 " Note that \"trim value\" is useless because the value parser skips\n" | 199 whitespace anyway. |
200 " whitespace anyway.\n"; | 200 )"; |
201 | 201 |
202 Value ConvertInputToValue(const Settings* settings, | 202 Value ConvertInputToValue(const Settings* settings, |
203 const std::string& input, | 203 const std::string& input, |
204 const ParseNode* origin, | 204 const ParseNode* origin, |
205 const Value& input_conversion_value, | 205 const Value& input_conversion_value, |
206 Err* err) { | 206 Err* err) { |
207 if (input_conversion_value.type() == Value::NONE) | 207 if (input_conversion_value.type() == Value::NONE) |
208 return Value(); // Allow null inputs to mean discard the result. | 208 return Value(); // Allow null inputs to mean discard the result. |
209 if (!input_conversion_value.VerifyTypeIs(Value::STRING, err)) | 209 if (!input_conversion_value.VerifyTypeIs(Value::STRING, err)) |
210 return Value(); | 210 return Value(); |
211 return DoConvertInputToValue(settings, input, origin, input_conversion_value, | 211 return DoConvertInputToValue(settings, input, origin, input_conversion_value, |
212 input_conversion_value.string_value(), err); | 212 input_conversion_value.string_value(), err); |
213 } | 213 } |
OLD | NEW |