Home | History | Annotate | Download | only in gn
      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.
      4 
      5 #include "tools/gn/string_utils.h"
      6 
      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"
     12 
     13 namespace {
     14 
     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                      token.location().byte() + int_offset + 1);
     26   Location end_loc(
     27       token.location().file(),
     28       token.location().line_number(),
     29       token.location().char_offset() + int_offset + 1 + static_cast<int>(size),
     30       token.location().byte() + int_offset + 1 + static_cast<int>(size));
     31   return Err(LocationRange(begin_loc, end_loc), msg, help);
     32 }
     33 
     34 // Given the character input[i] indicating the $ in a string, locates the
     35 // identifier and places its range in |*identifier|, and updates |*i| to
     36 // point to the last character consumed.
     37 //
     38 // On error returns false and sets the error.
     39 bool LocateInlineIdenfitier(const Token& token,
     40                             const char* input, size_t size,
     41                             size_t* i,
     42                             base::StringPiece* identifier,
     43                             Err* err) {
     44   size_t dollars_index = *i;
     45   (*i)++;
     46   if (*i == size) {
     47     *err = ErrInsideStringToken(token, dollars_index, 1, "$ at end of string.",
     48         "I was expecting an identifier after the $.");
     49     return false;
     50   }
     51 
     52   bool has_brackets;
     53   if (input[*i] == '{') {
     54     (*i)++;
     55     if (*i == size) {
     56       *err = ErrInsideStringToken(token, dollars_index, 2,
     57           "${ at end of string.",
     58           "I was expecting an identifier inside the ${...}.");
     59       return false;
     60     }
     61     has_brackets = true;
     62   } else {
     63     has_brackets = false;
     64   }
     65 
     66   // First char is special.
     67   if (!Tokenizer::IsIdentifierFirstChar(input[*i])) {
     68     *err = ErrInsideStringToken(
     69         token, dollars_index, *i - dollars_index + 1,
     70         "$ not followed by an identifier char.",
     71         "It you want a literal $ use \"\\$\".");
     72     return false;
     73   }
     74   size_t begin_offset = *i;
     75   (*i)++;
     76 
     77   // Find the first non-identifier char following the string.
     78   while (*i < size && Tokenizer::IsIdentifierContinuingChar(input[*i]))
     79     (*i)++;
     80   size_t end_offset = *i;
     81 
     82   // If we started with a bracket, validate that there's an ending one. Leave
     83   // *i pointing to the last char we consumed (backing up one).
     84   if (has_brackets) {
     85     if (*i == size) {
     86       *err = ErrInsideStringToken(token, dollars_index, *i - dollars_index,
     87                                   "Unterminated ${...");
     88       return false;
     89     } else if (input[*i] != '}') {
     90       *err = ErrInsideStringToken(token, *i, 1, "Not an identifier in string expansion.",
     91           "The contents of ${...} should be an identifier. "
     92           "This character is out of sorts.");
     93       return false;
     94     }
     95     // We want to consume the bracket but also back up one, so *i is unchanged.
     96   } else {
     97     (*i)--;
     98   }
     99 
    100   *identifier = base::StringPiece(&input[begin_offset],
    101                                   end_offset - begin_offset);
    102   return true;
    103 }
    104 
    105 bool AppendIdentifierValue(Scope* scope,
    106                            const Token& token,
    107                            const base::StringPiece& identifier,
    108                            std::string* output,
    109                            Err* err) {
    110   const Value* value = scope->GetValue(identifier, true);
    111   if (!value) {
    112     // We assume the identifier points inside the token.
    113     *err = ErrInsideStringToken(
    114         token, identifier.data() - token.value().data() - 1, identifier.size(),
    115         "Undefined identifier in string expansion.",
    116         std::string("\"") + identifier + "\" is not currently in scope.");
    117     return false;
    118   }
    119 
    120   output->append(value->ToString(false));
    121   return true;
    122 }
    123 
    124 }  // namespace
    125 
    126 bool ExpandStringLiteral(Scope* scope,
    127                          const Token& literal,
    128                          Value* result,
    129                          Err* err) {
    130   DCHECK(literal.type() == Token::STRING);
    131   DCHECK(literal.value().size() > 1);  // Should include quotes.
    132   DCHECK(result->type() == Value::STRING);  // Should be already set.
    133 
    134   // The token includes the surrounding quotes, so strip those off.
    135   const char* input = &literal.value().data()[1];
    136   size_t size = literal.value().size() - 2;
    137 
    138   std::string& output = result->string_value();
    139   output.reserve(size);
    140   for (size_t i = 0; i < size; i++) {
    141     if (input[i] == '\\') {
    142       if (i < size - 1) {
    143         switch (input[i + 1]) {
    144           case '\\':
    145           case '"':
    146           case '$':
    147             output.push_back(input[i + 1]);
    148             i++;
    149             continue;
    150           default:  // Everything else has no meaning: pass the literal.
    151             break;
    152         }
    153       }
    154       output.push_back(input[i]);
    155     } else if (input[i] == '$') {
    156       base::StringPiece identifier;
    157       if (!LocateInlineIdenfitier(literal, input, size, &i, &identifier, err))
    158         return false;
    159       if (!AppendIdentifierValue(scope, literal, identifier, &output, err))
    160         return false;
    161     } else {
    162       output.push_back(input[i]);
    163     }
    164   }
    165   return true;
    166 }
    167 
    168 std::string RemovePrefix(const std::string& str, const std::string& prefix) {
    169   CHECK(str.size() >= prefix.size() &&
    170         str.compare(0, prefix.size(), prefix) == 0);
    171   return str.substr(prefix.size());
    172 }
    173 
    174 void TrimTrailingSlash(std::string* str) {
    175   if (!str->empty()) {
    176     DCHECK((*str)[str->size() - 1] == '/');
    177     str->resize(str->size() - 1);
    178   }
    179 }
    180