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   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);
     30 }
     31 
     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.
     35 //
     36 // On error returns false and sets the error.
     37 bool LocateInlineIdenfitier(const Token& token,
     38                             const char* input, size_t size,
     39                             size_t* i,
     40                             base::StringPiece* identifier,
     41                             Err* err) {
     42   size_t dollars_index = *i;
     43   (*i)++;
     44   if (*i == size) {
     45     *err = ErrInsideStringToken(token, dollars_index, 1, "$ at end of string.",
     46         "I was expecting an identifier after the $.");
     47     return false;
     48   }
     49 
     50   bool has_brackets;
     51   if (input[*i] == '{') {
     52     (*i)++;
     53     if (*i == size) {
     54       *err = ErrInsideStringToken(token, dollars_index, 2,
     55           "${ at end of string.",
     56           "I was expecting an identifier inside the ${...}.");
     57       return false;
     58     }
     59     has_brackets = true;
     60   } else {
     61     has_brackets = false;
     62   }
     63 
     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 \"\\$\".");
     70     return false;
     71   }
     72   size_t begin_offset = *i;
     73   (*i)++;
     74 
     75   // Find the first non-identifier char following the string.
     76   while (*i < size && Tokenizer::IsIdentifierContinuingChar(input[*i]))
     77     (*i)++;
     78   size_t end_offset = *i;
     79 
     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).
     82   if (has_brackets) {
     83     if (*i == size) {
     84       *err = ErrInsideStringToken(token, dollars_index, *i - dollars_index,
     85                                   "Unterminated ${...");
     86       return false;
     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.");
     91       return false;
     92     }
     93     // We want to consume the bracket but also back up one, so *i is unchanged.
     94   } else {
     95     (*i)--;
     96   }
     97 
     98   *identifier = base::StringPiece(&input[begin_offset],
     99                                   end_offset - begin_offset);
    100   return true;
    101 }
    102 
    103 bool AppendIdentifierValue(Scope* scope,
    104                            const Token& token,
    105                            const base::StringPiece& identifier,
    106                            std::string* output,
    107                            Err* err) {
    108   const Value* value = scope->GetValue(identifier, true);
    109   if (!value) {
    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.");
    115     return false;
    116   }
    117 
    118   output->append(value->ToString(false));
    119   return true;
    120 }
    121 
    122 }  // namespace
    123 
    124 bool ExpandStringLiteral(Scope* scope,
    125                          const Token& literal,
    126                          Value* result,
    127                          Err* err) {
    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.
    131 
    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;
    135 
    136   std::string& output = result->string_value();
    137   output.reserve(size);
    138   for (size_t i = 0; i < size; i++) {
    139     if (input[i] == '\\') {
    140       if (i < size - 1) {
    141         switch (input[i + 1]) {
    142           case '\\':
    143           case '"':
    144           case '$':
    145             output.push_back(input[i + 1]);
    146             i++;
    147             continue;
    148           default:  // Everything else has no meaning: pass the literal.
    149             break;
    150         }
    151       }
    152       output.push_back(input[i]);
    153     } else if (input[i] == '$') {
    154       base::StringPiece identifier;
    155       if (!LocateInlineIdenfitier(literal, input, size, &i, &identifier, err))
    156         return false;
    157       if (!AppendIdentifierValue(scope, literal, identifier, &output, err))
    158         return false;
    159     } else {
    160       output.push_back(input[i]);
    161     }
    162   }
    163   return true;
    164 }
    165 
    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());
    170 }
    171