Debian Codegraph @

src/highlighter.h

raw symbols SCIP
1 /*
2 Copyright (C) 2008, 2009 Andres Cabrera
3 [email protected]
4
5 This file is part of CsoundQt.
6
7 CsoundQt is free software; you can redistribute it
8 and/or modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 CsoundQt is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with Csound; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA
21 */
22
23 #ifndef HIGHLIGHTER_H
24 #define HIGHLIGHTER_H
25
26 #include <QSyntaxHighlighter>
27
28 #include <QHash>
29 #include <QTextCharFormat>
30 #include <QStringList>
31 #include <QRegularExpression>
32
33 #include <QTextDocument>
34
35 enum CsdSection { UnknownSection, OptionsSection, OrchestraSection, ScoreSection };
36
37 struct ParenthesisInfo
38 {
39 char character;
40 int position;
41 };
42
43 class TextBlockData : public QTextBlockUserData
44 {
45 public:
46 TextBlockData();
47
48 QVector<ParenthesisInfo *> parentheses();
49 void insert(ParenthesisInfo *info);
50 CsdSection section;
51 void clearParenthesis() { m_parentheses.clear(); }
52
53 private:
54 QVector<ParenthesisInfo *> m_parentheses;
55
56 };
57
58 class Highlighter : public QSyntaxHighlighter
59 {
60 Q_OBJECT
61
62 //for parentheses matching:
63
64
65 public:
66 Highlighter(QTextDocument *parent = 0);
67 ~Highlighter();
68 void setOpcodeNameList(QStringList list);
69 void setDisabledOpcodes(QStringList list);
70 void setColorVariables(bool color);
71 void setMode(int mode);
72 void setTheme(const QString &theme);
73 void enableScoreSyntaxHighlighting(bool status) {
74 m_scoreSyntaxHighlighting = status;
75 }
76 QTextCharFormat getFormat(QString tag);
77
78 // for html
79 enum Construct {
80 Entity,
81 Tag,
82 Comment,
83 LastConstruct = Comment
84 };
85
86 void setFormatFor(Construct construct, const QTextCharFormat &format);
87 QTextCharFormat formatFor(Construct construct) const
88 { return m_formats[construct]; }
89
90 void setUDOs(QStringList udos);
91
92 protected:
93 enum State {
94 NormalState = -1,
95 InComment,
96 InTag
97 };
98
99
100 protected:
101 void highlightBlock(const QString &text);
102 void highlightCsoundBlock(const QString &text);
103 void highlightPythonBlock(const QString &text);
104 void highlightXmlBlock(const QString &text);
105 void highlightHtmlBlock(const QString &text);
106 void highlightScore(const QString &text, int start, int end);
107 int findOpcode(QString opcodeName, int start = 0, int end = -1);
108 bool isOpcode(QString name);
109
110 private:
111 struct HighlightingRule
112 {
113 QRegularExpression pattern;
114 QTextCharFormat format;
115 int group;
116 };
117 QVector<HighlightingRule> highlightingRules;
118 QVector<HighlightingRule> lastHighlightingRules;
119
120 QRegularExpression commentStartExpression;
121 QRegularExpression commentEndExpression;
122 QRegularExpression rxScoreLetter;
123 QRegularExpression rxQuotation;
124
125 QTextCharFormat csdtagFormat, instFormat, headerFormat;
126 QTextCharFormat irateFormat, krateFormat, arateFormat, girateFormat, gkrateFormat, garateFormat;
127 QTextCharFormat stringVarFormat, gstringVarFormat, fsigFormat, gfsigFormat;
128 QTextCharFormat opcodeFormat, udoFormat, macroDefineFormat, pfieldFormat;
129 QTextCharFormat singleLineCommentFormat, importantCommentFormat;
130 QTextCharFormat multiLineCommentFormat;
131 QTextCharFormat quotationFormat;
132 QTextCharFormat functionFormat;
133 QTextCharFormat nameFormat;
134 QTextCharFormat ioFormat;
135 QTextCharFormat deprecatedFormat;
136 QTextCharFormat operatorFormat;
137 QTextCharFormat scoreLetterFormat;
138 QTextCharFormat errorFormat;
139
140 QTextCharFormat labelFormat;
141 QTextCharFormat csoundOptionFormat;
142 QTextCharFormat defaultFormat;
143
144 QStringList tagPatterns, headerPatterns, instPatterns,
145 keywordLiterals, htmlKeywords, javascriptKeywords,
146 csoundOptions, ioPatterns; //Csound
147 QStringList operatorPatterns;
148
149 QSet<QString> deprecatedOpcodes;
150
151
152 QStringList pythonKeywords; //Python
153 QTextCharFormat keywordFormat;
154 QRegularExpression csoundOptionsRx, csoundOptionsRx2;
155
156 // void setFirstRules();
157 void setLastRules();
158
159 QStringList m_opcodeList;
160 QSet<QString> m_opcodesSet;
161
162 bool colorVariables;
163 // TODO this is duplicated in documentview class. Should it be unified?
164 int m_mode; //type of text 0=csound 1=python 2=xml 3=orc 4=sco -1=anything else
165
166 bool m_scoreSyntaxHighlighting;
167
168 QString m_theme;
169
170 // for html
171 QTextCharFormat jsKeywordFormat, htmlTagFormat;
172 QTextCharFormat m_formats[LastConstruct + 1];
173
174 QStringList m_parsedUDOs;
175 };
176
177 #endif