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

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: Clang warning 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
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 <string.h>
8
9 #include "base/strings/string_number_conversions.h"
10 #include "tools/gn/err.h"
11 #include "tools/gn/value.h"
12
13 SubstitutionPattern::Subrange::Subrange()
14 : type(SUBSTITUTION_LITERAL) {
15 }
16
17 SubstitutionPattern::Subrange::Subrange(SubstitutionType t,
18 const std::string& l)
19 : type(t),
20 literal(l) {
21 }
22
23 SubstitutionPattern::Subrange::~Subrange() {
24 }
25
26 SubstitutionPattern::SubstitutionPattern() {
27 }
28
29 SubstitutionPattern::~SubstitutionPattern() {
30 }
31
32 bool SubstitutionPattern::Parse(const Value& value, Err* err) {
33 if (!value.VerifyTypeIs(Value::STRING, err))
34 return false;
35 return Parse(value.string_value(), value.origin(), err);
36 }
37
38 bool SubstitutionPattern::Parse(const std::string& str,
39 const ParseNode* origin,
40 Err* err) {
41 DCHECK(ranges_.empty()); // Should only be called once.
42
43 size_t cur = 0;
44 while (true) {
45 size_t next = str.find("{{", cur);
46
47 // Pick up everything from the previous spot to here as a literal.
48 if (next == std::string::npos) {
49 if (cur != str.size())
50 ranges_.push_back(Subrange(SUBSTITUTION_LITERAL, str.substr(cur)));
51 break;
52 } else if (next > cur) {
53 ranges_.push_back(
54 Subrange(SUBSTITUTION_LITERAL, str.substr(cur, next - cur)));
55 }
56
57 // Find which specific pattern this corresponds to.
58 bool found_match = false;
59 for (size_t i = SUBSTITUTION_FIRST_PATTERN;
60 i < SUBSTITUTION_NUM_TYPES; i++) {
61 const char* cur_pattern = kSubstitutionNames[i];
62 size_t cur_len = strlen(cur_pattern);
63 if (str.compare(next, cur_len, cur_pattern) == 0) {
64 ranges_.push_back(Subrange(static_cast<SubstitutionType>(i)));
65 cur = next + cur_len;
66 found_match = true;
67 break;
68 }
69 }
70
71 // Expect all occurrances of {{ to resolve to a pattern.
scottmg 2014/08/05 22:30:32 nit; "occurrences"
72 if (!found_match) {
73 // Could make this error message more friendly if it comes up a lot. But
74 // most people will not be writing substitution patterns and the code
75 // to exactly indicate the error location is tricky.
76 *err = Err(origin, "Unknown substitution pattern",
77 "Found a {{ at offset " +
78 base::SizeTToString(next) +
79 " and did not find a known substitution following it.");
80 ranges_.clear();
81 return false;
82 }
83 }
84
85 // Fill required types vector.
86 bool required_type_bits[SUBSTITUTION_NUM_TYPES];
87 memset(&required_type_bits, 0, SUBSTITUTION_NUM_TYPES);
scottmg 2014/08/05 22:30:32 sizeof bool != 1 => sadness. "bool required_type_b
88 FillRequiredTypes(required_type_bits);
89
90 for (size_t i = SUBSTITUTION_FIRST_PATTERN; i < SUBSTITUTION_NUM_TYPES; i++) {
91 if (required_type_bits[i])
92 required_types_.push_back(static_cast<SubstitutionType>(i));
93 }
94 return true;
95 }
96
97 std::string SubstitutionPattern::AsString() const {
98 std::string result;
99 for (size_t i = 0; i < ranges_.size(); i++) {
100 if (ranges_[i].type == SUBSTITUTION_LITERAL)
101 result.append(ranges_[i].literal);
102 else
103 result.append(kSubstitutionNames[ranges_[i].type]);
104 }
105 return result;
106 }
107
108 void SubstitutionPattern::FillRequiredTypes(
109 bool required_types[SUBSTITUTION_NUM_TYPES]) const {
110 for (size_t i = 0; i < ranges_.size(); i++) {
111 if (ranges_[i].type != SUBSTITUTION_LITERAL)
112 required_types[static_cast<size_t>(ranges_[i].type)] = true;
113 }
114 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698