Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(222)

Side by Side Diff: tools/gn/substitution_pattern.cc

Issue 429423002: Refactor GN source expansions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/gn/substitution_pattern.h ('k') | tools/gn/substitution_pattern_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 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.
4
5 #include "tools/gn/substitution_pattern.h"
6
7 #include "base/strings/string_number_conversions.h"
8 #include "tools/gn/err.h"
9 #include "tools/gn/value.h"
10
11 SubstitutionPattern::Subrange::Subrange()
12 : type(SUBSTITUTION_LITERAL) {
13 }
14
15 SubstitutionPattern::Subrange::Subrange(SubstitutionType t,
16 const std::string& l)
17 : type(t),
18 literal(l) {
19 }
20
21 SubstitutionPattern::Subrange::~Subrange() {
22 }
23
24 SubstitutionPattern::SubstitutionPattern() {
25 }
26
27 SubstitutionPattern::~SubstitutionPattern() {
28 }
29
30 bool SubstitutionPattern::Parse(const Value& value, Err* err) {
31 if (!value.VerifyTypeIs(Value::STRING, err))
32 return false;
33 return Parse(value.string_value(), value.origin(), err);
34 }
35
36 bool SubstitutionPattern::Parse(const std::string& str,
37 const ParseNode* origin,
38 Err* err) {
39 DCHECK(ranges_.empty()); // Should only be called once.
40
41 size_t cur = 0;
42 while (true) {
43 size_t next = str.find("{{", cur);
44
45 // Pick up everything from the previous spot to here as a literal.
46 if (next == std::string::npos) {
47 if (cur != str.size())
48 ranges_.push_back(Subrange(SUBSTITUTION_LITERAL, str.substr(cur)));
49 break;
50 } else if (next > cur) {
51 ranges_.push_back(
52 Subrange(SUBSTITUTION_LITERAL, str.substr(cur, next - cur)));
53 }
54
55 // Find which specific pattern this corresponds to.
56 bool found_match = false;
57 for (size_t i = SUBSTITUTION_FIRST_PATTERN;
58 i < SUBSTITUTION_NUM_TYPES; i++) {
59 const char* cur_pattern = kSubstitutionNames[i];
60 size_t cur_len = strlen(cur_pattern);
61 if (str.compare(next, cur_len, cur_pattern) == 0) {
62 ranges_.push_back(Subrange(static_cast<SubstitutionType>(i)));
63 cur = next + cur_len;
64 found_match = true;
65 break;
66 }
67 }
68
69 // Expect all occurrances of {{ to resolve to a pattern.
70 if (!found_match) {
71 // Could make this error message more friendly if it comes up a lot. But
72 // most people will not be writing substitution patterns and the code
73 // to exactly indicate the error location is tricky.
74 *err = Err(origin, "Unknown substitution pattern",
75 "Found a {{ at offset " +
76 base::SizeTToString(next) +
77 " and did not find a known substitution following it.");
78 ranges_.clear();
79 return false;
80 }
81 }
82
83 // Fill required types vector.
84 bool required_type_bits[SUBSTITUTION_NUM_TYPES] = {0};
85 FillRequiredTypes(required_type_bits);
86
87 for (size_t i = SUBSTITUTION_FIRST_PATTERN; i < SUBSTITUTION_NUM_TYPES; i++) {
88 if (required_type_bits[i])
89 required_types_.push_back(static_cast<SubstitutionType>(i));
90 }
91 return true;
92 }
93
94 std::string SubstitutionPattern::AsString() const {
95 std::string result;
96 for (size_t i = 0; i < ranges_.size(); i++) {
97 if (ranges_[i].type == SUBSTITUTION_LITERAL)
98 result.append(ranges_[i].literal);
99 else
100 result.append(kSubstitutionNames[ranges_[i].type]);
101 }
102 return result;
103 }
104
105 void SubstitutionPattern::FillRequiredTypes(
106 bool required_types[SUBSTITUTION_NUM_TYPES]) const {
107 for (size_t i = 0; i < ranges_.size(); i++) {
108 if (ranges_[i].type != SUBSTITUTION_LITERAL)
109 required_types[static_cast<size_t>(ranges_[i].type)] = true;
110 }
111 }
OLDNEW
« no previous file with comments | « tools/gn/substitution_pattern.h ('k') | tools/gn/substitution_pattern_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698