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 #ifndef TOOLS_GN_SUBSTITUTION_PATTERN_H_ 6 #define TOOLS_GN_SUBSTITUTION_PATTERN_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "gn/substitution_type.h" 12 13 class BuildSettings; 14 class Err; 15 class ParseNode; 16 class Value; 17 18 // Represents a string with {{substitution_patterns}} in them. 19 class SubstitutionPattern { 20 public: 21 struct Subrange { 22 Subrange(); 23 explicit Subrange(const Substitution* t, 24 const std::string& l = std::string()); 25 ~Subrange(); 26 27 inline bool operator==(const Subrange& other) const { 28 return type == other.type && literal == other.literal; 29 } 30 31 const Substitution* type; 32 33 // When type_ == LITERAL, this specifies the literal. 34 std::string literal; 35 }; 36 37 SubstitutionPattern(); 38 SubstitutionPattern(const SubstitutionPattern& other); 39 ~SubstitutionPattern(); 40 41 SubstitutionPattern& operator=(const SubstitutionPattern&) = default; 42 43 // Parses the given string and fills in the pattern. The pattern must only 44 // be initialized once. On failure, returns false and sets the error. 45 bool Parse(const Value& value, Err* err); 46 bool Parse(const std::string& str, const ParseNode* origin, Err* err); 47 48 // Makes a pattern given a hardcoded string. Will assert if the string is 49 // not a valid pattern. 50 static SubstitutionPattern MakeForTest(const char* str); 51 52 // Returns the pattern as a string with substitutions in them. 53 std::string AsString() const; 54 55 // Sets the bits in the given vector corresponding to the substitutions used 56 // by this pattern. SubstitutionLiteral is ignored. 57 void FillRequiredTypes(SubstitutionBits* bits) const; 58 59 // Checks whether this pattern resolves to something in the output directory 60 // for the given build settings. If not, returns false and fills in the given 61 // error. 62 bool IsInOutputDir(const BuildSettings* build_settings, Err* err) const; 63 64 // Returns a vector listing the substitutions used by this pattern, not 65 // counting SubstitutionLiteral. required_types()66 const std::vector<const Substitution*>& required_types() const { 67 return required_types_; 68 } 69 ranges()70 const std::vector<Subrange>& ranges() const { return ranges_; } empty()71 bool empty() const { return ranges_.empty(); } 72 origin()73 const ParseNode* origin() const { return origin_; } 74 75 private: 76 std::vector<Subrange> ranges_; 77 const ParseNode* origin_; 78 79 std::vector<const Substitution*> required_types_; 80 }; 81 82 #endif // TOOLS_GN_SUBSTITUTION_PATTERN_H_ 83