ACAV f0ba4b7c9529
Abstract Syntax Tree (AST) visualization tool for C, C++, and Objective-C
Loading...
Searching...
No Matches
CppSyntaxHighlighter.h
Go to the documentation of this file.
1/*$!{
2* Aurora Clang AST Viewer (ACAV)
3*
4* Copyright (c) 2026 Min Liu
5* Copyright (c) 2026 Michael David Adams
6*
7* SPDX-License-Identifier: GPL-2.0-or-later
8*
9* This program is free software; you can redistribute it and/or modify
10* it under the terms of the GNU General Public License as published by
11* the Free Software Foundation; either version 2 of the License, or
12* (at your option) any later version.
13*
14* This program is distributed in the hope that it will be useful,
15* but WITHOUT ANY WARRANTY; without even the implied warranty of
16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17* GNU General Public License for more details.
18*
19* You should have received a copy of the GNU General Public License along
20* with this program; if not, see <https://www.gnu.org/licenses/>.
21}$!*/
22
25#pragma once
26
27#include <QRegularExpression>
28#include <QSet>
29#include <QSyntaxHighlighter>
30#include <QTextCharFormat>
31#include <vector>
32
33namespace acav {
34
37class CppSyntaxHighlighter : public QSyntaxHighlighter {
38 Q_OBJECT
39
40public:
41 explicit CppSyntaxHighlighter(QTextDocument *parent);
42
43protected:
44 void highlightBlock(const QString &text) override;
45
46private:
47 enum BlockState {
48 NormalState = 0,
49 InBlockCommentState = 1,
50 };
51
52 QTextCharFormat commentFormat_;
53 QTextCharFormat stringFormat_;
54 QTextCharFormat keywordFormat_;
55 QTextCharFormat functionFormat_;
56 QTextCharFormat preprocessorFormat_;
57
58 std::vector<QRegularExpression> keywordPatterns_;
59 QRegularExpression functionPattern_;
60 QSet<QString> nonFunctionWords_;
61
62 void markCommentRange(const QString &text, int start, int length,
63 std::vector<bool> *masked);
64 void markStringOrCharLiteral(const QString &text, int start,
65 std::vector<bool> *masked, int *nextIndex);
66 bool isUnmaskedRange(const std::vector<bool> &masked, int start,
67 int length) const;
68 void applyKeywordRules(const QString &text, const std::vector<bool> &masked);
69 void applyFunctionRules(const QString &text, const std::vector<bool> &masked);
70 void applyPreprocessorRules(const QString &text, std::vector<bool> *masked);
71};
72
73} // namespace acav