1 | /* |
2 | * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com) |
3 | * |
4 | * This program is free software: you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation, either version 3 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
16 | */ |
17 | #ifndef SYNEDITCPPHIGHLIGHTER_H |
18 | #define SYNEDITCPPHIGHLIGHTER_H |
19 | #include "base.h" |
20 | #include <QSet> |
21 | |
22 | namespace QSynedit { |
23 | |
24 | class CppHighlighter: public Highlighter |
25 | { |
26 | enum TokenId { |
27 | Asm = 1, |
28 | , |
29 | Directive, |
30 | Identifier, |
31 | Key, |
32 | Null, |
33 | Number, |
34 | Space, |
35 | String, |
36 | StringEscapeSeq, |
37 | Symbol, |
38 | Unknown, |
39 | Char, |
40 | Float, |
41 | Hex, |
42 | HexFloat, |
43 | Octal, |
44 | RawString |
45 | }; |
46 | |
47 | enum class ExtTokenId { |
48 | Add, AddAssign, And, AndAssign, Arrow, Assign, |
49 | BitComplement, BraceClose, BraceOpen, Colon, Comma, |
50 | Decrement, Divide, DivideAssign, Ellipse, GreaterThan, |
51 | GreaterThanEqual, IncOr, IncOrAssign, Increment, LessThan, |
52 | LessThanEqual, LogAnd, LogComplement, LogEqual, LogOr, |
53 | Mod, ModAssign, MultiplyAssign, NotEqual, Point, PointerToMemberOfObject, |
54 | PointerToMemberOfPointer,Question, |
55 | RoundClose, RoundOpen, ScopeResolution, SemiColon, ShiftLeft, |
56 | ShiftLeftAssign, ShiftRight, ShiftRightAssign, SquareClose, |
57 | SquareOpen, Star, Subtract, SubtractAssign, Xor, |
58 | XorAssign, BackSlash |
59 | }; |
60 | |
61 | enum RangeState { |
62 | rsUnknown, rsAnsiC, rsAnsiCAsm, rsAnsiCAsmBlock, rsAsm, |
63 | rsAsmBlock, rsDirective, , , |
64 | rsMultiLineString, rsMultiLineDirective, , |
65 | , rsMultiLineStringEscapeSeq, |
66 | rsRawString, ,rsRawStringEscaping,rsRawStringNotEscaping,rsChar, |
67 | , rsDefineStart, rsDefineIdentifier, rsDefineRemaining |
68 | }; |
69 | |
70 | public: |
71 | explicit CppHighlighter(); |
72 | |
73 | PHighlighterAttribute asmAttribute() const; |
74 | |
75 | PHighlighterAttribute preprocessorAttribute() const; |
76 | |
77 | PHighlighterAttribute invalidAttribute() const; |
78 | |
79 | PHighlighterAttribute numberAttribute() const; |
80 | |
81 | PHighlighterAttribute floatAttribute() const; |
82 | |
83 | PHighlighterAttribute hexAttribute() const; |
84 | |
85 | PHighlighterAttribute octAttribute() const; |
86 | |
87 | PHighlighterAttribute stringEscapeSequenceAttribute() const; |
88 | |
89 | PHighlighterAttribute charAttribute() const; |
90 | |
91 | PHighlighterAttribute variableAttribute() const; |
92 | |
93 | PHighlighterAttribute functionAttribute() const; |
94 | |
95 | PHighlighterAttribute classAttribute() const; |
96 | |
97 | PHighlighterAttribute globalVarAttribute() const; |
98 | |
99 | PHighlighterAttribute localVarAttribute() const; |
100 | |
101 | static const QSet<QString> Keywords; |
102 | |
103 | ExtTokenId getExtTokenId(); |
104 | TokenKind getTokenId(); |
105 | private: |
106 | void andSymbolProc(); |
107 | void ansiCppProc(); |
108 | void ansiCProc(); |
109 | void asciiCharProc(); |
110 | void atSymbolProc(); |
111 | void braceCloseProc(); |
112 | void braceOpenProc(); |
113 | void colonProc(); |
114 | void commaProc(); |
115 | void directiveProc(); |
116 | void defineIdentProc(); |
117 | void defineRemainingProc(); |
118 | void directiveEndProc(); |
119 | void equalProc(); |
120 | void greaterProc(); |
121 | void identProc(); |
122 | void lowerProc(); |
123 | void minusProc(); |
124 | void modSymbolProc(); |
125 | void notSymbolProc(); |
126 | void nullProc(); |
127 | void numberProc(); |
128 | void orSymbolProc(); |
129 | void plusProc(); |
130 | void pointProc(); |
131 | void questionProc(); |
132 | void rawStringProc(); |
133 | void roundCloseProc(); |
134 | void roundOpenProc(); |
135 | void semiColonProc(); |
136 | void slashProc(); |
137 | void backSlashProc(); |
138 | void spaceProc(); |
139 | void squareCloseProc(); |
140 | void squareOpenProc(); |
141 | void starProc(); |
142 | void stringEndProc(); |
143 | void stringEscapeSeqProc(); |
144 | void stringProc(); |
145 | void stringStartProc(); |
146 | void tildeProc(); |
147 | void unknownProc(); |
148 | void xorSymbolProc(); |
149 | void processChar(); |
150 | void popIndents(int indentType); |
151 | void pushIndents(int indentType); |
152 | |
153 | private: |
154 | bool mAsmStart; |
155 | HighlighterState mRange; |
156 | // SynRangeState mSpaceRange; |
157 | QString mLineString; |
158 | QChar* mLine; |
159 | int mLineSize; |
160 | int mRun; |
161 | int mStringLen; |
162 | int mToIdent; |
163 | int mTokenPos; |
164 | int mTokenId; |
165 | ExtTokenId mExtTokenId; |
166 | int mLineNumber; |
167 | int mLeftBraces; |
168 | int mRightBraces; |
169 | |
170 | PHighlighterAttribute mAsmAttribute; |
171 | PHighlighterAttribute mPreprocessorAttribute; |
172 | PHighlighterAttribute mInvalidAttribute; |
173 | PHighlighterAttribute mNumberAttribute; |
174 | PHighlighterAttribute mFloatAttribute; |
175 | PHighlighterAttribute mHexAttribute; |
176 | PHighlighterAttribute mOctAttribute; |
177 | PHighlighterAttribute mStringEscapeSequenceAttribute; |
178 | PHighlighterAttribute mCharAttribute; |
179 | PHighlighterAttribute mVariableAttribute; |
180 | PHighlighterAttribute mFunctionAttribute; |
181 | PHighlighterAttribute mClassAttribute; |
182 | PHighlighterAttribute mGlobalVarAttribute; |
183 | PHighlighterAttribute mLocalVarAttribute; |
184 | |
185 | // SynHighligterBase interface |
186 | public: |
187 | bool getTokenFinished() const override; |
188 | bool (int state) const override; |
189 | bool isLastLineStringNotFinished(int state) const override; |
190 | bool eol() const override; |
191 | QString getToken() const override; |
192 | PHighlighterAttribute getTokenAttribute() const override; |
193 | TokenKind getTokenKind() override; |
194 | int getTokenPos() override; |
195 | void next() override; |
196 | void setLine(const QString &newLine, int lineNumber) override; |
197 | bool isKeyword(const QString &word) override; |
198 | TokenType getTokenType() override; |
199 | void setState(const HighlighterState& rangeState) override; |
200 | void resetState() override; |
201 | HighlighterClass getClass() const override; |
202 | QString getName() const override; |
203 | |
204 | QString languageName() override; |
205 | HighlighterLanguage language() override; |
206 | |
207 | // SynHighlighter interface |
208 | public: |
209 | HighlighterState getState() const override; |
210 | |
211 | // SynHighlighter interface |
212 | public: |
213 | bool isIdentChar(const QChar &ch) const override; |
214 | |
215 | // SynHighlighter interface |
216 | public: |
217 | QSet<QString> keywords() const override; |
218 | |
219 | // SynHighlighter interface |
220 | public: |
221 | QString foldString() override; |
222 | }; |
223 | |
224 | } |
225 | |
226 | #endif // SYNEDITCPPHIGHLIGHTER_H |
227 | |