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/string_utils.h"
7 #include "tools/gn/err.h"
8 #include "tools/gn/scope.h"
9 #include "tools/gn/token.h"
10 #include "tools/gn/tokenizer.h"
11 #include "tools/gn/value.h"
15 // Constructs an Err indicating a range inside a string. We assume that the
16 // token has quotes around it that are not counted by the offset.
17 Err
ErrInsideStringToken(const Token
& token
, size_t offset
, size_t size
,
18 const std::string
& msg
,
19 const std::string
& help
= std::string()) {
20 // The "+1" is skipping over the " at the beginning of the token.
21 int int_offset
= static_cast<int>(offset
);
22 Location
begin_loc(token
.location().file(),
23 token
.location().line_number(),
24 token
.location().char_offset() + int_offset
+ 1);
25 Location
end_loc(token
.location().file(),
26 token
.location().line_number(),
27 token
.location().char_offset() + int_offset
+ 1 +
28 static_cast<int>(size
));
29 return Err(LocationRange(begin_loc
, end_loc
), msg
, help
);
32 // Given the character input[i] indicating the $ in a string, locates the
33 // identifier and places its range in |*identifier|, and updates |*i| to
34 // point to the last character consumed.
36 // On error returns false and sets the error.
37 bool LocateInlineIdenfitier(const Token
& token
,
38 const char* input
, size_t size
,
40 base::StringPiece
* identifier
,
42 size_t dollars_index
= *i
;
45 *err
= ErrInsideStringToken(token
, dollars_index
, 1, "$ at end of string.",
46 "I was expecting an identifier after the $.");
51 if (input
[*i
] == '{') {
54 *err
= ErrInsideStringToken(token
, dollars_index
, 2,
55 "${ at end of string.",
56 "I was expecting an identifier inside the ${...}.");
64 // First char is special.
65 if (!Tokenizer::IsIdentifierFirstChar(input
[*i
])) {
66 *err
= ErrInsideStringToken(
67 token
, dollars_index
, *i
- dollars_index
+ 1,
68 "$ not followed by an identifier char.",
69 "It you want a literal $ use \"\\$\".");
72 size_t begin_offset
= *i
;
75 // Find the first non-identifier char following the string.
76 while (*i
< size
&& Tokenizer::IsIdentifierContinuingChar(input
[*i
]))
78 size_t end_offset
= *i
;
80 // If we started with a bracket, validate that there's an ending one. Leave
81 // *i pointing to the last char we consumed (backing up one).
84 *err
= ErrInsideStringToken(token
, dollars_index
, *i
- dollars_index
,
85 "Unterminated ${...");
87 } else if (input
[*i
] != '}') {
88 *err
= ErrInsideStringToken(token
, *i
, 1, "Not an identifier in string expansion.",
89 "The contents of ${...} should be an identifier. "
90 "This character is out of sorts.");
93 // We want to consume the bracket but also back up one, so *i is unchanged.
98 *identifier
= base::StringPiece(&input
[begin_offset
],
99 end_offset
- begin_offset
);
103 bool AppendIdentifierValue(Scope
* scope
,
105 const base::StringPiece
& identifier
,
108 const Value
* value
= scope
->GetValue(identifier
, true);
110 // We assume the identifier points inside the token.
111 *err
= ErrInsideStringToken(
112 token
, identifier
.data() - token
.value().data() - 1, identifier
.size(),
113 "Undefined identifier in string expansion.",
114 std::string("\"") + identifier
+ "\" is not currently in scope.");
118 output
->append(value
->ToString(false));
124 bool ExpandStringLiteral(Scope
* scope
,
125 const Token
& literal
,
128 DCHECK(literal
.type() == Token::STRING
);
129 DCHECK(literal
.value().size() > 1); // Should include quotes.
130 DCHECK(result
->type() == Value::STRING
); // Should be already set.
132 // The token includes the surrounding quotes, so strip those off.
133 const char* input
= &literal
.value().data()[1];
134 size_t size
= literal
.value().size() - 2;
136 std::string
& output
= result
->string_value();
137 output
.reserve(size
);
138 for (size_t i
= 0; i
< size
; i
++) {
139 if (input
[i
] == '\\') {
141 switch (input
[i
+ 1]) {
145 output
.push_back(input
[i
+ 1]);
148 default: // Everything else has no meaning: pass the literal.
152 output
.push_back(input
[i
]);
153 } else if (input
[i
] == '$') {
154 base::StringPiece identifier
;
155 if (!LocateInlineIdenfitier(literal
, input
, size
, &i
, &identifier
, err
))
157 if (!AppendIdentifierValue(scope
, literal
, identifier
, &output
, err
))
160 output
.push_back(input
[i
]);
166 std::string
RemovePrefix(const std::string
& str
, const std::string
& prefix
) {
167 CHECK(str
.size() >= prefix
.size() &&
168 str
.compare(0, prefix
.size(), prefix
) == 0);
169 return str
.substr(prefix
.size());