15f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 25f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// found in the LICENSE file. 45f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 55f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#ifndef TOOLS_GN_SUBSTITUTION_PATTERN_H_ 65f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#define TOOLS_GN_SUBSTITUTION_PATTERN_H_ 75f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 85f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include <string> 95f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include <vector> 105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "tools/gn/substitution_type.h" 125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 1303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)class BuildSettings; 145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)class Err; 155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)class ParseNode; 165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)class Value; 175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Represents a string with {{substitution_patterns}} in them. 195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)class SubstitutionPattern { 205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) public: 215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) struct Subrange { 225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) Subrange(); 235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) Subrange(SubstitutionType t, const std::string& l = std::string()); 245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) ~Subrange(); 255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 2603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) inline bool operator==(const Subrange& other) const { 2703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) return type == other.type && literal == other.literal; 2803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) } 2903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) SubstitutionType type; 315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // When type_ == LITERAL, this specifies the literal. 335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) std::string literal; 345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) }; 355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) SubstitutionPattern(); 375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) ~SubstitutionPattern(); 385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // Parses the given string and fills in the pattern. The pattern must only 405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // be initialized once. On failure, returns false and sets the error. 415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) bool Parse(const Value& value, Err* err); 425f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) bool Parse(const std::string& str, const ParseNode* origin, Err* err); 435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Makes a pattern given a hardcoded string. Will assert if the string is 451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // not a valid pattern. 461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci static SubstitutionPattern MakeForTest(const char* str); 471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 485f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // Returns the pattern as a string with substitutions in them. 495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) std::string AsString() const; 505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // Sets the bits in the given vector corresponding to the substitutions used 525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // by this pattern. SUBSTITUTION_LITERAL is ignored. 5303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) void FillRequiredTypes(SubstitutionBits* bits) const; 5403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 5503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) // Checks whether this pattern resolves to something in the output directory 5603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) // for the given build settings. If not, returns false and fills in the given 5703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) // error. 5803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) bool IsInOutputDir(const BuildSettings* build_settings, 5903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) Err* err) const; 605f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 615f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // Returns a vector listing the substitutions used by this pattern, not 625f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // counting SUBSTITUTION_LITERAL. 635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const std::vector<SubstitutionType>& required_types() const { 645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) return required_types_; 655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) } 665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const std::vector<Subrange>& ranges() const { return ranges_; } 685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) bool empty() const { return ranges_.empty(); } 695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 705f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) private: 715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) std::vector<Subrange> ranges_; 7203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) const ParseNode* origin_; 735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) std::vector<SubstitutionType> required_types_; 755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}; 765f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 775f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#endif // TOOLS_GN_SUBSTITUTION_PATTERN_H_ 78