Partially fix compilation of media_unittests with Xcode 7 (OS X 10.11 SDK).
[chromium-blink-merge.git] / tools / gn / input_conversion.cc
blob57a8038968abcc70e8708e7d7aa8100191dd54b2
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
3 // found in the LICENSE file.
5 #include "tools/gn/input_conversion.h"
7 #include "base/strings/string_split.h"
8 #include "base/strings/string_util.h"
9 #include "tools/gn/build_settings.h"
10 #include "tools/gn/err.h"
11 #include "tools/gn/input_file.h"
12 #include "tools/gn/label.h"
13 #include "tools/gn/parse_tree.h"
14 #include "tools/gn/parser.h"
15 #include "tools/gn/scheduler.h"
16 #include "tools/gn/scope.h"
17 #include "tools/gn/settings.h"
18 #include "tools/gn/tokenizer.h"
19 #include "tools/gn/value.h"
21 namespace {
23 enum ValueOrScope {
24 PARSE_VALUE, // Treat the input as an expression.
25 PARSE_SCOPE, // Treat the input as code and return the resulting scope.
28 // Sets the origin of the value and any nested values with the given node.
29 Value ParseValueOrScope(const Settings* settings,
30 const std::string& input,
31 ValueOrScope what,
32 const ParseNode* origin,
33 Err* err) {
34 // The memory for these will be kept around by the input file manager
35 // so the origin parse nodes for the values will be preserved.
36 InputFile* input_file;
37 std::vector<Token>* tokens;
38 scoped_ptr<ParseNode>* parse_root_ptr;
39 g_scheduler->input_file_manager()->AddDynamicInput(
40 SourceFile(), &input_file, &tokens, &parse_root_ptr);
42 input_file->SetContents(input);
43 if (origin) {
44 // This description will be the blame for any error messages caused by
45 // script parsing or if a value is blamed. It will say
46 // "Error at <...>:line:char" so here we try to make a string for <...>
47 // that reads well in this context.
48 input_file->set_friendly_name(
49 "dynamically parsed input that " +
50 origin->GetRange().begin().Describe(true) +
51 " loaded ");
52 } else {
53 input_file->set_friendly_name("dynamic input");
56 *tokens = Tokenizer::Tokenize(input_file, err);
57 if (err->has_error())
58 return Value();
60 // Parse the file according to what we're looking for.
61 if (what == PARSE_VALUE)
62 *parse_root_ptr = Parser::ParseValue(*tokens, err);
63 else
64 *parse_root_ptr = Parser::Parse(*tokens, err); // Will return a Block.
65 if (err->has_error())
66 return Value();
67 ParseNode* parse_root = parse_root_ptr->get(); // For nicer syntax below.
69 // It's valid for the result to be a null pointer, this just means that the
70 // script returned nothing.
71 if (!parse_root)
72 return Value();
74 scoped_ptr<Scope> scope(new Scope(settings));
75 Value result = parse_root->Execute(scope.get(), err);
76 if (err->has_error())
77 return Value();
79 // When we want the result as a scope, the result is actually the scope
80 // we made, rather than the result of running the block (which will be empty).
81 if (what == PARSE_SCOPE) {
82 DCHECK(result.type() == Value::NONE);
83 result = Value(origin, scope.Pass());
85 return result;
88 Value ParseList(const std::string& input, const ParseNode* origin, Err* err) {
89 Value ret(origin, Value::LIST);
90 std::vector<std::string> as_lines = base::SplitString(
91 input, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
93 // Trim one empty line from the end since the last line might end in a
94 // newline. If the user wants more trimming, they'll specify "trim" in the
95 // input conversion options.
96 if (!as_lines.empty() && as_lines[as_lines.size() - 1].empty())
97 as_lines.resize(as_lines.size() - 1);
99 ret.list_value().reserve(as_lines.size());
100 for (const auto& line : as_lines)
101 ret.list_value().push_back(Value(origin, line));
102 return ret;
105 // Backend for ConvertInputToValue, this takes the extracted string for the
106 // input conversion so we can recursively call ourselves to handle the optional
107 // "trim" prefix. This original value is also kept for the purposes of throwing
108 // errors.
109 Value DoConvertInputToValue(const Settings* settings,
110 const std::string& input,
111 const ParseNode* origin,
112 const Value& original_input_conversion,
113 const std::string& input_conversion,
114 Err* err) {
115 if (input_conversion.empty())
116 return Value(); // Empty string means discard the result.
118 const char kTrimPrefix[] = "trim ";
119 if (base::StartsWith(input_conversion, kTrimPrefix,
120 base::CompareCase::SENSITIVE)) {
121 std::string trimmed;
122 base::TrimWhitespaceASCII(input, base::TRIM_ALL, &trimmed);
124 // Remove "trim" prefix from the input conversion and re-run.
125 return DoConvertInputToValue(
126 settings, trimmed, origin, original_input_conversion,
127 input_conversion.substr(arraysize(kTrimPrefix) - 1), err);
130 if (input_conversion == "value")
131 return ParseValueOrScope(settings, input, PARSE_VALUE, origin, err);
132 if (input_conversion == "string")
133 return Value(origin, input);
134 if (input_conversion == "list lines")
135 return ParseList(input, origin, err);
136 if (input_conversion == "scope")
137 return ParseValueOrScope(settings, input, PARSE_SCOPE, origin, err);
139 *err = Err(original_input_conversion, "Not a valid input_conversion.",
140 "Have you considered a career in retail?");
141 return Value();
144 } // namespace
146 extern const char kInputConversion_Help[] =
147 "input_conversion: Specifies how to transform input to a variable.\n"
148 "\n"
149 " input_conversion is an argument to read_file and exec_script that\n"
150 " specifies how the result of the read operation should be converted\n"
151 " into a variable.\n"
152 "\n"
153 " \"\" (the default)\n"
154 " Discard the result and return None.\n"
155 "\n"
156 " \"list lines\"\n"
157 " Return the file contents as a list, with a string for each line.\n"
158 " The newlines will not be present in the result. The last line may\n"
159 " or may not end in a newline.\n"
160 "\n"
161 " After splitting, each individual line will be trimmed of\n"
162 " whitespace on both ends.\n"
163 "\n"
164 " \"scope\"\n"
165 " Execute the block as GN code and return a scope with the\n"
166 " resulting values in it. If the input was:\n"
167 " a = [ \"hello.cc\", \"world.cc\" ]\n"
168 " b = 26\n"
169 " and you read the result into a variable named \"val\", then you\n"
170 " could access contents the \".\" operator on \"val\":\n"
171 " sources = val.a\n"
172 " some_count = val.b\n"
173 "\n"
174 " \"string\"\n"
175 " Return the file contents into a single string.\n"
176 "\n"
177 " \"value\"\n"
178 " Parse the input as if it was a literal rvalue in a buildfile.\n"
179 " Examples of typical program output using this mode:\n"
180 " [ \"foo\", \"bar\" ] (result will be a list)\n"
181 " or\n"
182 " \"foo bar\" (result will be a string)\n"
183 " or\n"
184 " 5 (result will be an integer)\n"
185 "\n"
186 " Note that if the input is empty, the result will be a null value\n"
187 " which will produce an error if assigned to a variable.\n"
188 "\n"
189 " \"trim ...\"\n"
190 " Prefixing any of the other transformations with the word \"trim\"\n"
191 " will result in whitespace being trimmed from the beginning and end\n"
192 " of the result before processing.\n"
193 "\n"
194 " Examples: \"trim string\" or \"trim list lines\"\n"
195 "\n"
196 " Note that \"trim value\" is useless because the value parser skips\n"
197 " whitespace anyway.\n";
199 Value ConvertInputToValue(const Settings* settings,
200 const std::string& input,
201 const ParseNode* origin,
202 const Value& input_conversion_value,
203 Err* err) {
204 if (input_conversion_value.type() == Value::NONE)
205 return Value(); // Allow null inputs to mean discard the result.
206 if (!input_conversion_value.VerifyTypeIs(Value::STRING, err))
207 return Value();
208 return DoConvertInputToValue(settings, input, origin, input_conversion_value,
209 input_conversion_value.string_value(), err);