StringMatcher.h 1.9 KB

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