StringMatcher.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //===- StringMatcher.h - Generate a matcher for input strings ---*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the StringMatcher class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_TABLEGEN_STRINGMATCHER_H
  13. #define LLVM_TABLEGEN_STRINGMATCHER_H
  14. #include "llvm/ADT/StringRef.h"
  15. #include <string>
  16. #include <utility>
  17. #include <vector>
  18. namespace llvm {
  19. class raw_ostream;
  20. /// Given a list of strings and code to execute when they match, output a
  21. /// simple switch tree to classify the input string.
  22. ///
  23. /// If a match is found, the code in Matches[i].second is executed; control must
  24. /// not exit this code fragment. If nothing matches, execution falls through.
  25. class StringMatcher {
  26. public:
  27. using StringPair = std::pair<std::string, std::string>;
  28. private:
  29. StringRef StrVariableName;
  30. const std::vector<StringPair> &Matches;
  31. raw_ostream &OS;
  32. public:
  33. StringMatcher(StringRef strVariableName,
  34. const std::vector<StringPair> &matches, raw_ostream &os)
  35. : StrVariableName(strVariableName), Matches(matches), OS(os) {}
  36. void Emit(unsigned Indent = 0, bool IgnoreDuplicates = false) const;
  37. private:
  38. bool EmitStringMatcherForChar(const std::vector<const StringPair *> &Matches,
  39. unsigned CharNo, unsigned IndentCount,
  40. bool IgnoreDuplicates) const;
  41. };
  42. } // end namespace llvm
  43. #endif // LLVM_TABLEGEN_STRINGMATCHER_H