-
Notifications
You must be signed in to change notification settings - Fork 0
/
cplusplus.cpp
336 lines (267 loc) · 7.12 KB
/
cplusplus.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "stdafx.h"
#include "SampleView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// C++ keywords (MSVC5.0 + POET5.0)
static LPTSTR s_apszCppKeywordList[] =
{
_T("sin"),
_T("cos"),
_T("tan"),
//_T("cot"),
_T("asin"),
_T("acos"),
_T("atan"),
_T("sinh"),
_T("cosh"),
_T("tanh"),
_T("ln"),
_T("log10"),
_T("log"),
_T("root"),
_T("abs"),
_T("exp"),
_T("sqr"),
_T("sqrt"),
_T("Error"),
_T("Divide_By_0"),
//_T("_???_"),
_T("deg2rad"),
_T("rad2deg"),
_T("remainder"),
_T("div"),
_T("fact"),
_T("nCr"),
_T("nPr"),
NULL
};
static BOOL IsCppKeyword(LPCTSTR pszChars, int nLength)
{
for (int L = 0; s_apszCppKeywordList[L] != NULL; L ++)
{
if (strncmp(s_apszCppKeywordList[L], pszChars, nLength) == 0
&& s_apszCppKeywordList[L][nLength] == 0)
return TRUE;
}
return FALSE;
}
static BOOL IsCppNumber(LPCTSTR pszChars, int nLength)
{
if (nLength==1) {
if ((pszChars[0]=='.') || (pszChars[0]=='-'))
return FALSE;
}
else if (nLength==2) {
if ((pszChars[0]=='-') && (pszChars[1]=='.'))
return FALSE;
}
//The first char cannot be a 'e', must only be digit or '.'
int state=0;
if (!isdigit(pszChars[0])) {
if (pszChars[0]=='.') {
state=1;
}
else if (pszChars[0]=='-') {
state=0;
}
else
return FALSE;
}
//else state=0;
//[whitespace] [sign] [digits] [.digits] [ {d | D | e | E }[sign]digits]
//the first must be a digit or dec point
//what about using atof itself to test?
int I=1;
while ((state==0) && (I<nLength)) {
if (pszChars[I]=='.') {
state=1;
I++;
break;
}
if (!isdigit(pszChars[I])) return FALSE;
I++;
}
//dec point detected
while ((state==1) && (I<nLength)) {
if (!isdigit(pszChars[I])) return FALSE;
I++;
}
return TRUE;
}
#define DEFINE_BLOCK(pos, colorindex) \
ASSERT((pos) >= 0 && (pos) <= nLength);\
if (pBuf != NULL)\
{\
if (nActualItems == 0 || pBuf[nActualItems - 1].m_nCharPos <= (pos)){\
pBuf[nActualItems].m_nCharPos = (pos);\
pBuf[nActualItems].m_nColorIndex = (colorindex);\
nActualItems ++;}\
}
#define COOKIE_COMMENT 0x0001
#define COOKIE_PREPROCESSOR 0x0002
#define COOKIE_EXT_COMMENT 0x0004
#define COOKIE_STRING 0x0008
#define COOKIE_CHAR 0x0010
DWORD CSampleView::ParseLine(DWORD dwCookie, int nLineIndex, TEXTBLOCK *pBuf, int &nActualItems)
{
int nLength = GetLineLength(nLineIndex);
if (nLength <= 0)
return dwCookie & COOKIE_EXT_COMMENT;
LPCTSTR pszChars = GetLineChars(nLineIndex);
BOOL bFirstChar = (dwCookie & ~COOKIE_EXT_COMMENT) == 0;
BOOL bRedefineBlock = TRUE;
BOOL bDecIndex = FALSE;
int nIdentBegin = -1;
for (int I = 0; ; I++)
{
if (bRedefineBlock)
{
int nPos = I;
if (bDecIndex)
nPos--;
if (dwCookie & (COOKIE_COMMENT | COOKIE_EXT_COMMENT))
{
DEFINE_BLOCK(nPos, COLORINDEX_COMMENT);
}
else
if (dwCookie & (COOKIE_STRING))
{
DEFINE_BLOCK(nPos, COLORINDEX_STRING);
}
else
{
DEFINE_BLOCK(nPos, COLORINDEX_NORMALTEXT);
}
bRedefineBlock = FALSE;
bDecIndex = FALSE;
}
if (I == nLength)
break;
if (dwCookie & COOKIE_COMMENT)
{
DEFINE_BLOCK(I, COLORINDEX_COMMENT);
dwCookie |= COOKIE_COMMENT;
break;
}
// String constant "...."
if (dwCookie & COOKIE_STRING)
{
if (pszChars[I] == '"' && (I == 0 || pszChars[I - 1] != '\\'))
{
dwCookie &= ~COOKIE_STRING;
bRedefineBlock = TRUE;
}
continue;
}
// Extended comment /*....*/
/*
if (dwCookie & COOKIE_EXT_COMMENT)
{
if (I > 0 && pszChars[I] == '/' && pszChars[I - 1] == '*')
{
dwCookie &= ~COOKIE_EXT_COMMENT;
bRedefineBlock = TRUE;
}
continue;
}
*/
if (dwCookie & COOKIE_EXT_COMMENT)
{
if ((I > 1 && pszChars[I] == '/' && pszChars[I - 1] == '*' && pszChars[I-2] !=
'/')|| (I==1 && pszChars[I] == '/' && pszChars[I - 1] == '*'))
{
dwCookie &= ~COOKIE_EXT_COMMENT;
bRedefineBlock = TRUE;
}
continue;
}
if (I > 0 && pszChars[I] == '/' && pszChars[I - 1] == '/')
{
DEFINE_BLOCK(I - 1, COLORINDEX_COMMENT);
dwCookie |= COOKIE_COMMENT;
break;
}
// Normal text
if (pszChars[I] == '"')
{
DEFINE_BLOCK(I, COLORINDEX_STRING);
dwCookie |= COOKIE_STRING;
continue;
}
if (I > 0 && pszChars[I] == '*' && pszChars[I - 1] == '/')
{
DEFINE_BLOCK(I - 1, COLORINDEX_COMMENT);
dwCookie |= COOKIE_EXT_COMMENT;
continue;
}
if (pBuf == NULL)
continue; // We don't need to extract keywords,
// for faster parsing skip the rest of loop
//q: this function is to be used to parse summation and stats
//but for express.cpp, only the isCppNumber should be used...
//only in very strict case is the - an identifier, else it is always a separator...
//logic: most of the time, the - is still treated as a separator
//only in certain case, where (1) the right is a . or digit will it be considered a indentifier
//even if (1) is true, we still need to check the left side. If it is not separator..then the - will still be considered a separator
//therefore, only invery strict case where the left side of the - is a separator and the right side is ok before it can be considered an identifier and become a unary minus
BOOL minusID=FALSE; //assume '-' is separator
if ((pszChars[I] == '-') && (I+1 != nLength)) {
if (isdigit(pszChars[I+1]) || (pszChars[I+1] == '.')) {
if ((I-1)>=0) {
//if ((isdigit(pszChars[I-1])) || (pszChars[I-1] == '.')) {
if (isalnum(pszChars[I-1]) || pszChars[I-1] == '_' || pszChars[I-1] == '.' || pszChars[I-1] == '-') //if there is anything on the left side that is not separator, it becomes separator
{
minusID=FALSE; //becomes separtor
}
else
minusID=TRUE;
}
else
minusID=TRUE; //'-' is not separator, but identifier
}
}
if (isalnum(pszChars[I]) || pszChars[I] == '_' || pszChars[I] == '.' || minusID)
{
if (nIdentBegin == -1)
nIdentBegin = I;
}
else
{ //here means a separator is encountered, whether it is comma or white space or etc..
//nindent =-1 indicates the start of a new word
if (nIdentBegin >= 0)
{
if (IsCppKeyword(pszChars + nIdentBegin, I - nIdentBegin))
{
DEFINE_BLOCK(nIdentBegin, COLORINDEX_KEYWORD);
}
else {
if (IsCppNumber(pszChars + nIdentBegin, I - nIdentBegin))
{
DEFINE_BLOCK(nIdentBegin, COLORINDEX_NUMBER);
}
}
bRedefineBlock = TRUE;
bDecIndex = TRUE;
nIdentBegin = -1;
}
}
}
if (nIdentBegin >= 0)
{
if (IsCppKeyword(pszChars + nIdentBegin, I - nIdentBegin))
{
DEFINE_BLOCK(nIdentBegin, COLORINDEX_KEYWORD);
}
else
if (IsCppNumber(pszChars + nIdentBegin, I - nIdentBegin))
{
DEFINE_BLOCK(nIdentBegin, COLORINDEX_NUMBER);
}
}
//if (pszChars[nLength - 1] != '\\')
dwCookie &= COOKIE_EXT_COMMENT;
return dwCookie;
}