blob: 3aa3540d616da60a19d51291af98201d457410f9 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- StringMatcher.h - Generate a matcher for input strings ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the StringMatcher class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TABLEGEN_STRINGMATCHER_H
15#define LLVM_TABLEGEN_STRINGMATCHER_H
16
17#include "llvm/ADT/StringRef.h"
18#include <string>
19#include <utility>
20#include <vector>
21
22namespace llvm {
23
24class raw_ostream;
25
Andrew Scull0372a572018-11-16 15:47:06 +000026/// Given a list of strings and code to execute when they match, output a
27/// simple switch tree to classify the input string.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010028///
Andrew Scull0372a572018-11-16 15:47:06 +000029/// If a match is found, the code in Matches[i].second is executed; control must
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010030/// not exit this code fragment. If nothing matches, execution falls through.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010031class StringMatcher {
32public:
33 using StringPair = std::pair<std::string, std::string>;
34
35private:
36 StringRef StrVariableName;
37 const std::vector<StringPair> &Matches;
38 raw_ostream &OS;
39
40public:
41 StringMatcher(StringRef strVariableName,
42 const std::vector<StringPair> &matches, raw_ostream &os)
43 : StrVariableName(strVariableName), Matches(matches), OS(os) {}
44
45 void Emit(unsigned Indent = 0, bool IgnoreDuplicates = false) const;
46
47private:
48 bool EmitStringMatcherForChar(const std::vector<const StringPair *> &Matches,
49 unsigned CharNo, unsigned IndentCount,
50 bool IgnoreDuplicates) const;
51};
52
53} // end namespace llvm
54
55#endif // LLVM_TABLEGEN_STRINGMATCHER_H