blob: 48f4eb79cceea9f99f44d8a26bb36a04805d5ace [file] [log] [blame]
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include <vector>
#include "gn/substitution_type.h"
class BuildSettings;
class Err;
class ParseNode;
class Value;
// Represents a string with {{substitution_patterns}} in them.
class SubstitutionPattern {
struct Subrange {
explicit Subrange(const Substitution* t, const std::string& l = std::string());
inline bool operator==(const Subrange& other) const {
return type == other.type && literal == other.literal;
const Substitution* type;
// When type_ == LITERAL, this specifies the literal.
std::string literal;
SubstitutionPattern(const SubstitutionPattern& other);
SubstitutionPattern& operator=(const SubstitutionPattern&) = default;
// Parses the given string and fills in the pattern. The pattern must only
// be initialized once. On failure, returns false and sets the error.
bool Parse(const Value& value, Err* err);
bool Parse(const std::string& str, const ParseNode* origin, Err* err);
// Makes a pattern given a hardcoded string. Will assert if the string is
// not a valid pattern.
static SubstitutionPattern MakeForTest(const char* str);
// Returns the pattern as a string with substitutions in them.
std::string AsString() const;
// Sets the bits in the given vector corresponding to the substitutions used
// by this pattern. SubstitutionLiteral is ignored.
void FillRequiredTypes(SubstitutionBits* bits) const;
// Checks whether this pattern resolves to something in the output directory
// for the given build settings. If not, returns false and fills in the given
// error.
bool IsInOutputDir(const BuildSettings* build_settings, Err* err) const;
// Returns a vector listing the substitutions used by this pattern, not
// counting SubstitutionLiteral.
const std::vector<const Substitution*>& required_types() const {
return required_types_;
const std::vector<Subrange>& ranges() const { return ranges_; }
bool empty() const { return ranges_.empty(); }
const ParseNode* origin() const { return origin_; }
std::vector<Subrange> ranges_;
const ParseNode* origin_;
std::vector<const Substitution*> required_types_;