blob: 795b7a6d41dcc6aac711517f81d5c5cea38110a1 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- StringMatcher.h - Generate a matcher for input strings ---*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the StringMatcher class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_TABLEGEN_STRINGMATCHER_H
14#define LLVM_TABLEGEN_STRINGMATCHER_H
15
16#include "llvm/ADT/StringRef.h"
17#include <string>
18#include <utility>
19#include <vector>
20
21namespace llvm {
22
23class raw_ostream;
24
Andrew Scull0372a572018-11-16 15:47:06 +000025/// Given a list of strings and code to execute when they match, output a
26/// simple switch tree to classify the input string.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010027///
Andrew Scull0372a572018-11-16 15:47:06 +000028/// If a match is found, the code in Matches[i].second is executed; control must
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010029/// not exit this code fragment. If nothing matches, execution falls through.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010030class StringMatcher {
31public:
32 using StringPair = std::pair<std::string, std::string>;
33
34private:
35 StringRef StrVariableName;
36 const std::vector<StringPair> &Matches;
37 raw_ostream &OS;
38
39public:
40 StringMatcher(StringRef strVariableName,
41 const std::vector<StringPair> &matches, raw_ostream &os)
42 : StrVariableName(strVariableName), Matches(matches), OS(os) {}
43
44 void Emit(unsigned Indent = 0, bool IgnoreDuplicates = false) const;
45
46private:
47 bool EmitStringMatcherForChar(const std::vector<const StringPair *> &Matches,
48 unsigned CharNo, unsigned IndentCount,
49 bool IgnoreDuplicates) const;
50};
51
52} // end namespace llvm
53
54#endif // LLVM_TABLEGEN_STRINGMATCHER_H