Chatterino
Types.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "messages/Message.hpp"
4 
5 #include <QRegularExpression>
6 
7 namespace filterparser {
8 
9 using MessagePtr = std::shared_ptr<const chatterino::Message>;
10 using ContextMap = QMap<QString, QVariant>;
11 
12 enum TokenType {
13  // control
15  AND = 1,
16  OR = 2,
17  LP = 3,
18  RP = 4,
20  LIST_END = 6,
21  COMMA = 7,
23 
24  // binary operator
26  EQ = 21,
27  NEQ = 22,
28  LT = 23,
29  GT = 24,
30  LTE = 25,
31  GTE = 26,
32  CONTAINS = 27,
34  ENDS_WITH = 29,
35  MATCH = 30,
36  BINARY_END = 49,
37 
38  // unary operator
40  NOT = 51,
41  UNARY_END = 99,
42 
43  // math operators
44  MATH_START = 100,
45  PLUS = 101,
46  MINUS = 102,
47  MULTIPLY = 103,
48  DIVIDE = 104,
49  MOD = 105,
50  MATH_END = 149,
51 
52  // other types
53  OTHER_START = 150,
54  STRING = 151,
55  INT = 152,
56  IDENTIFIER = 153,
58 
59  NONE = 200
60 };
61 
62 bool convertVariantTypes(QVariant &a, QVariant &b, int type);
63 QString tokenTypeToInfoString(TokenType type);
64 
66 {
67 public:
68  virtual ~Expression() = default;
69 
70  virtual QVariant execute(const ContextMap &) const
71  {
72  return false;
73  }
74 
75  virtual QString debug() const
76  {
77  return "(false)";
78  }
79 
80  virtual QString filterString() const
81  {
82  return "";
83  }
84 };
85 
86 using ExpressionPtr = std::unique_ptr<Expression>;
87 
89 {
90 public:
91  ValueExpression(QVariant value, TokenType type);
92  TokenType type();
93 
94  QVariant execute(const ContextMap &context) const override;
95  QString debug() const override;
96  QString filterString() const override;
97 
98 private:
99  QVariant value_;
100  TokenType type_;
101 };
102 
104 {
105 public:
106  RegexExpression(QString regex, bool caseInsensitive);
107 
108  QVariant execute(const ContextMap &context) const override;
109  QString debug() const override;
110  QString filterString() const override;
111 
112 private:
113  QString regexString_;
114  bool caseInsensitive_;
115  QRegularExpression regex_;
116 };
117 
118 using ExpressionList = std::vector<std::unique_ptr<Expression>>;
119 
121 {
122 public:
124 
125  QVariant execute(const ContextMap &context) const override;
126  QString debug() const override;
127  QString filterString() const override;
128 
129 private:
130  ExpressionList list_;
131 };
132 
134 {
135 public:
137 
138  QVariant execute(const ContextMap &context) const override;
139  QString debug() const override;
140  QString filterString() const override;
141 
142 private:
143  TokenType op_;
144  ExpressionPtr left_;
145  ExpressionPtr right_;
146 };
147 
149 {
150 public:
152 
153  QVariant execute(const ContextMap &context) const override;
154  QString debug() const override;
155  QString filterString() const override;
156 
157 private:
158  TokenType op_;
159  ExpressionPtr right_;
160 };
161 
162 } // namespace filterparser
Definition: Types.hpp:16
Definition: Types.hpp:15
Definition: Types.hpp:17
Definition: Types.hpp:45
Definition: Types.hpp:26
Definition: Types.hpp:20
virtual ~Expression()=default
Definition: Types.hpp:41
Definition: Types.hpp:22
Definition: Types.hpp:49
QString tokenTypeToInfoString(TokenType type)
Definition: Types.cpp:15
virtual QString debug() const
Definition: Types.hpp:75
bool convertVariantTypes(QVariant &a, QVariant &b, int type)
Definition: Types.cpp:5
std::unique_ptr< Expression > ExpressionPtr
Definition: Types.hpp:86
Definition: FilterParser.cpp:8
Definition: Types.hpp:27
Definition: Types.hpp:14
std::vector< std::unique_ptr< Expression > > ExpressionList
Definition: Types.hpp:118
Definition: Types.hpp:40
Definition: Types.hpp:55
Definition: Types.hpp:30
Definition: Types.hpp:103
Definition: Types.hpp:56
Definition: Types.hpp:65
Definition: Types.hpp:28
Definition: Types.hpp:18
Definition: Types.hpp:88
Definition: Types.hpp:19
QMap< QString, QVariant > ContextMap
Definition: Types.hpp:10
Definition: Types.hpp:54
Definition: Types.hpp:29
Definition: Types.hpp:47
Definition: Types.hpp:39
Definition: Types.hpp:32
Definition: Types.hpp:25
TokenType
Definition: Types.hpp:12
Definition: Types.hpp:44
Definition: Types.hpp:59
Definition: Types.hpp:148
Definition: Types.hpp:21
Definition: Types.hpp:35
Definition: Types.hpp:48
Definition: Types.hpp:34
virtual QString filterString() const
Definition: Types.hpp:80
std::shared_ptr< const chatterino::Message > MessagePtr
Definition: Types.hpp:9
Definition: Types.hpp:50
Definition: Types.hpp:46
Definition: Types.hpp:36
Definition: Types.hpp:120
Definition: Types.hpp:33
Definition: Types.hpp:31
virtual QVariant execute(const ContextMap &) const
Definition: Types.hpp:70
Definition: Types.hpp:133
Definition: Types.hpp:53
Definition: Types.hpp:57