Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / editors / sc-ide / widgets / code_editor / highlighter.hpp
blob9f08cd3b5acfb3e26b33f2a609def3be94084c9e
1 /*
2 SuperCollider Qt IDE
3 Copyright (c) 2012 Jakob Leben & Tim Blechmann
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef SCIDE_SC_SYNTAX_HIGHLIGHTER_HPP_INCLUDED
22 #define SCIDE_SC_SYNTAX_HIGHLIGHTER_HPP_INCLUDED
24 #include "tokens.hpp"
26 #include <QSyntaxHighlighter>
27 #include <QVector>
29 namespace ScIDE {
31 namespace Settings { class Manager; }
33 class Main;
35 enum SyntaxFormat
37 PlainFormat,
38 ClassFormat,
39 KeywordFormat,
40 BuiltinFormat,
41 PrimitiveFormat,
42 SymbolFormat,
43 StringFormat,
44 CharFormat,
45 NumberFormat,
46 EnvVarFormat,
47 CommentFormat,
49 FormatCount
52 struct SyntaxRule
54 SyntaxRule(): type(Token::Unknown) {}
55 SyntaxRule( Token::Type t, const QString &s ): type(t), expr(s) {}
57 Token::Type type;
58 QRegExp expr;
61 class SyntaxHighlighterGlobals : public QObject
63 Q_OBJECT
65 public:
66 SyntaxHighlighterGlobals( Main *, Settings::Manager * settings );
68 inline const QTextCharFormat * formats() const
70 return mFormats;
73 inline const QTextCharFormat & format( SyntaxFormat type ) const
75 return mFormats[type];
78 inline static const SyntaxHighlighterGlobals * instance() { Q_ASSERT(mInstance); return mInstance; }
80 public Q_SLOTS:
81 void applySettings( Settings::Manager * );
83 Q_SIGNALS:
84 void syntaxFormatsChanged();
86 private:
87 friend class SyntaxHighlighter;
89 void initSyntaxRules();
90 void initKeywords();
91 void initBuiltins();
92 void applySettings( Settings::Manager*, const QString &key, SyntaxFormat );
94 QTextCharFormat mFormats[FormatCount];
95 QVector<SyntaxRule> mInCodeRules;
96 QRegExp mInSymbolRegexp, mInStringRegexp;
98 static SyntaxHighlighterGlobals *mInstance;
101 class SyntaxHighlighter:
102 public QSyntaxHighlighter
104 Q_OBJECT
106 static const int inCode = 0;
107 static const int inString = 1;
108 static const int inSymbol = 2;
109 static const int inComment = 100;
110 // NOTE: Integers higher than inComment are reserved for multi line comments,
111 // and indicate the comment nesting level!
113 public:
114 SyntaxHighlighter(QTextDocument *parent = 0);
116 private:
117 void highlightBlock(const QString &text);
118 void highlightBlockInCode(const QString& text, int & currentIndex, int & currentState);
119 void highlightBlockInString(const QString& text, int & currentIndex, int & currentState);
120 void highlightBlockInSymbol(const QString& text, int & currentIndex, int & currentState);
121 void highlightBlockInComment(const QString& text, int & currentIndex, int & currentState);
123 Token::Type findMatchingRule(QString const & text, int & currentIndex, int & lengthOfMatch);
125 const SyntaxHighlighterGlobals *mGlobals;
130 #endif