summaryrefslogtreecommitdiffstats
path: root/tools/gn/substitution_pattern.h
blob: a3d6f7e633400c524492f6c2fb682a303edd9ed9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// 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.

#ifndef TOOLS_GN_SUBSTITUTION_PATTERN_H_
#define TOOLS_GN_SUBSTITUTION_PATTERN_H_

#include <string>
#include <vector>

#include "tools/gn/substitution_type.h"

class BuildSettings;
class Err;
class ParseNode;
class Value;

// Represents a string with {{substitution_patterns}} in them.
class SubstitutionPattern {
 public:
  struct Subrange {
    Subrange();
    Subrange(SubstitutionType t, const std::string& l = std::string());
    ~Subrange();

    inline bool operator==(const Subrange& other) const {
      return type == other.type && literal == other.literal;
    }

    SubstitutionType type;

    // When type_ == LITERAL, this specifies the literal.
    std::string literal;
  };

  SubstitutionPattern();
  ~SubstitutionPattern();

  // 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. SUBSTITUTION_LITERAL 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 SUBSTITUTION_LITERAL.
  const std::vector<SubstitutionType>& required_types() const {
    return required_types_;
  }

  const std::vector<Subrange>& ranges() const { return ranges_; }
  bool empty() const { return ranges_.empty(); }

 private:
  std::vector<Subrange> ranges_;
  const ParseNode* origin_;

  std::vector<SubstitutionType> required_types_;
};

#endif  // TOOLS_GN_SUBSTITUTION_PATTERN_H_