| 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/err.h" | 5 #include "tools/gn/err.h" |
| 6 #include "tools/gn/functions.h" | 6 #include "tools/gn/functions.h" |
| 7 #include "tools/gn/parse_node_value_adapter.h" | 7 #include "tools/gn/parse_node_value_adapter.h" |
| 8 #include "tools/gn/parse_tree.h" | 8 #include "tools/gn/parse_tree.h" |
| 9 #include "tools/gn/scope.h" | 9 #include "tools/gn/scope.h" |
| 10 | 10 |
| 11 namespace functions { | 11 namespace functions { |
| 12 | 12 |
| 13 const char kForEach[] = "foreach"; | 13 const char kForEach[] = "foreach"; |
| 14 const char kForEach_HelpShort[] = | 14 const char kForEach_HelpShort[] = |
| 15 "foreach: Iterate over a list."; | 15 "foreach: Iterate over a list."; |
| 16 const char kForEach_Help[] = | 16 const char kForEach_Help[] = |
| 17 "foreach: Iterate over a list.\n" | 17 R"(foreach: Iterate over a list. |
| 18 "\n" | 18 |
| 19 " foreach(<loop_var>, <list>) {\n" | 19 foreach(<loop_var>, <list>) { |
| 20 " <loop contents>\n" | 20 <loop contents> |
| 21 " }\n" | 21 } |
| 22 "\n" | 22 |
| 23 " Executes the loop contents block over each item in the list,\n" | 23 Executes the loop contents block over each item in the list, assigning the |
| 24 " assigning the loop_var to each item in sequence. The loop_var will be\n" | 24 loop_var to each item in sequence. The loop_var will be a copy so assigning |
| 25 " a copy so assigning to it will not mutate the list.\n" | 25 to it will not mutate the list. |
| 26 "\n" | 26 |
| 27 " The block does not introduce a new scope, so that variable assignments\n" | 27 The block does not introduce a new scope, so that variable assignments inside |
| 28 " inside the loop will be visible once the loop terminates.\n" | 28 the loop will be visible once the loop terminates. |
| 29 "\n" | 29 |
| 30 " The loop variable will temporarily shadow any existing variables with\n" | 30 The loop variable will temporarily shadow any existing variables with the |
| 31 " the same name for the duration of the loop. After the loop terminates\n" | 31 same name for the duration of the loop. After the loop terminates the loop |
| 32 " the loop variable will no longer be in scope, and the previous value\n" | 32 variable will no longer be in scope, and the previous value (if any) will be |
| 33 " (if any) will be restored.\n" | 33 restored. |
| 34 "\n" | 34 |
| 35 "Example\n" | 35 Example |
| 36 "\n" | 36 |
| 37 " mylist = [ \"a\", \"b\", \"c\" ]\n" | 37 mylist = [ "a", "b", "c" ] |
| 38 " foreach(i, mylist) {\n" | 38 foreach(i, mylist) { |
| 39 " print(i)\n" | 39 print(i) |
| 40 " }\n" | 40 } |
| 41 "\n" | 41 |
| 42 " Prints:\n" | 42 Prints: |
| 43 " a\n" | 43 a |
| 44 " b\n" | 44 b |
| 45 " c\n"; | 45 c)"; |
| 46 | 46 |
| 47 Value RunForEach(Scope* scope, | 47 Value RunForEach(Scope* scope, |
| 48 const FunctionCallNode* function, | 48 const FunctionCallNode* function, |
| 49 const ListNode* args_list, | 49 const ListNode* args_list, |
| 50 Err* err) { | 50 Err* err) { |
| 51 const auto& args_vector = args_list->contents(); | 51 const auto& args_vector = args_list->contents(); |
| 52 if (args_vector.size() != 2) { | 52 if (args_vector.size() != 2) { |
| 53 *err = Err(function, "Wrong number of arguments to foreach().", | 53 *err = Err(function, "Wrong number of arguments to foreach().", |
| 54 "Expecting exactly two."); | 54 "Expecting exactly two."); |
| 55 return Value(); | 55 return Value(); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 old_loop_value.origin()); | 99 old_loop_value.origin()); |
| 100 } else { | 100 } else { |
| 101 // Loop variable was undefined before loop, delete it. | 101 // Loop variable was undefined before loop, delete it. |
| 102 scope->RemoveIdentifier(loop_var); | 102 scope->RemoveIdentifier(loop_var); |
| 103 } | 103 } |
| 104 | 104 |
| 105 return Value(); | 105 return Value(); |
| 106 } | 106 } |
| 107 | 107 |
| 108 } // namespace functions | 108 } // namespace functions |
| OLD | NEW |