Chatterino
Tokenizer.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include <QMap>
6 #include <QRegularExpression>
7 #include <QString>
8 
9 namespace filterparser {
10 
11 static const QMap<QString, QString> validIdentifiersMap = {
12  {"author.badges", "author badges"},
13  {"author.color", "author color"},
14  {"author.name", "author name"},
15  {"author.no_color", "author has no color?"},
16  {"author.subbed", "author subscribed?"},
17  {"author.sub_length", "author sub length"},
18  {"channel.name", "channel name"},
19  {"channel.watching", "/watching channel?"},
20  {"channel.live", "Channel live?"},
21  {"flags.highlighted", "highlighted?"},
22  {"flags.points_redeemed", "redeemed points?"},
23  {"flags.sub_message", "sub/resub message?"},
24  {"flags.system_message", "system message?"},
25  {"flags.reward_message", "channel point reward message?"},
26  {"flags.first_message", "first message?"},
27  {"flags.elevated_message", "elevated message?"},
28  {"flags.cheer_message", "cheer message?"},
29  {"flags.whisper", "whisper message?"},
30  {"flags.reply", "reply message?"},
31  {"flags.automod", "automod message?"},
32  {"message.content", "message text"},
33  {"message.length", "message length"}};
34 
35 // clang-format off
36 static const QRegularExpression tokenRegex(
37  QString("((r|ri)?\\\")((\\\\\")|[^\\\"])*\\\"|") + // String/Regex literal
38  QString("[\\w\\.]+|") + // Identifier or reserved keyword
39  QString("(<=?|>=?|!=?|==|\\|\\||&&|\\+|-|\\*|\\/|%)+|") + // Operator
40  QString("[\\(\\)]|") + // Parentheses
41  QString("[{},]") // List
42 );
43 // clang-format on
44 
45 class Tokenizer
46 {
47 public:
48  Tokenizer(const QString &text);
49 
50  bool hasNext() const;
51  QString next();
52  QString current() const;
53  QString preview() const;
54  TokenType nextTokenType() const;
55  TokenType tokenType() const;
56 
57  bool nextTokenIsOp() const;
58  bool nextTokenIsBinaryOp() const;
59  bool nextTokenIsUnaryOp() const;
60  bool nextTokenIsMathOp() const;
61 
62  void debug();
63  const QStringList allTokens();
64 
65  static bool typeIsOp(TokenType token);
66  static bool typeIsBinaryOp(TokenType token);
67  static bool typeIsUnaryOp(TokenType token);
68  static bool typeIsMathOp(TokenType token);
69 
70 private:
71  int i_ = 0;
72  QStringList tokens_;
73  QList<TokenType> tokenTypes_;
74 
75  TokenType tokenize(const QString &text);
76 };
77 } // namespace filterparser
TokenType tokenType() const
Definition: Tokenizer.cpp:45
bool nextTokenIsUnaryOp() const
Definition: Tokenizer.cpp:60
TokenType nextTokenType() const
Definition: Tokenizer.cpp:40
Definition: FilterParser.cpp:8
Definition: Tokenizer.hpp:45
void debug()
Definition: Tokenizer.cpp:70
static bool typeIsOp(TokenType token)
Definition: Tokenizer.cpp:170
bool nextTokenIsOp() const
Definition: Tokenizer.cpp:50
static bool typeIsMathOp(TokenType token)
Definition: Tokenizer.cpp:187
const QStringList allTokens()
Definition: Tokenizer.cpp:95
static bool typeIsBinaryOp(TokenType token)
Definition: Tokenizer.cpp:177
bool hasNext() const
Definition: Tokenizer.cpp:17
QString next()
Definition: Tokenizer.cpp:22
TokenType
Definition: Types.hpp:12
static bool typeIsUnaryOp(TokenType token)
Definition: Tokenizer.cpp:182
bool nextTokenIsBinaryOp() const
Definition: Tokenizer.cpp:55
bool nextTokenIsMathOp() const
Definition: Tokenizer.cpp:65
QString current() const
Definition: Tokenizer.cpp:28
Tokenizer(const QString &text)
Definition: Tokenizer.cpp:6
QString preview() const
Definition: Tokenizer.cpp:33