-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCrystalScriptStream.h
371 lines (328 loc) · 8.54 KB
/
CrystalScriptStream.h
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#pragma once
#include "crysedit/CCrystalTextBuffer.h"
#include "Compile/CompileInterfaces.h"
class ISyntaxParserCallback
{
public:
virtual void Done() = 0;
};
//
// Limits the text buffer to a particular line/char
// (used for autocomplete and tooltips)
//
class CScriptStreamLimiter
{
public:
CScriptStreamLimiter(CCrystalTextBuffer *pBuffer)
{
_pBuffer = pBuffer;
// By default it's not limited:
_nLineLimit = pBuffer->GetLineCount();
_nCharLimit = pBuffer->GetLineLength(_nLineLimit - 1);
_hDoAC = NULL;
_hACDone = NULL;
_fCancel = false;
_id = 0;
_pCallback = NULL;
}
~CScriptStreamLimiter()
{
if (_hDoAC)
{
CloseHandle(_hDoAC);
}
if (_hACDone)
{
CloseHandle(_hACDone);
}
}
int GetId() { return _id; }
int *GetIdPtr() { return &_id; }
void InvalidateBuffer()
{
OutputDebugString("ID INCREMENT\n");
_id++;
}
void Limit(LineCol dwPos)
{
_nLineLimit = dwPos.Line();
_nCharLimit = dwPos.Column();
_fCancel = false;
/*
if (!_hDoAC)
{
_hDoAC = CreateEvent(NULL, FALSE, FALSE, NULL);
_hACDone = CreateEvent(NULL, FALSE, FALSE, NULL);
}*/
}
void SetCallback(ISyntaxParserCallback *pCallback)
{
_pCallback = pCallback;
}
// Bail on parsing (called on UI thread)
void Bail()
{
_fCancel = true;
Continue();
}
// Continue parsing (called on UI thread)
void Continue()
{
SetEvent(_hDoAC);
}
// Wait for parsing to be done (called on UI thread)
void Wait()
{
if (WAIT_OBJECT_0 == WaitForSingleObject(_hACDone, INFINITE))
{
}
else
{
ASSERT(FALSE);
}
}
// Reflect some methods on CCrystalTextBuffer:
int GetLineCount()
{
// + 1 since we really want to include the current line in the calc
return min(_nLineLimit + 1, _pBuffer->GetLineCount());
}
int GetLineLength(int nLine)
{
int iLength = 0;
if (nLine == _nLineLimit + 1)
{
ASSERT(FALSE);
}
if (nLine == _nLineLimit)
{
// Last line. Limit it.
// nCharLimit + 1, since we really want to include the point *after* the cursor pos.
iLength = min(_nCharLimit, _pBuffer->GetLineLength(nLine));
}
else
{
iLength = _pBuffer->GetLineLength(nLine);
}
return iLength;
}
LPTSTR GetLineChars(int nLine) { return _pBuffer->GetLineChars(nLine); } // Just fwd through. We're limited by _nCharLimit
void GetMoreData(int &nChar, int &nLine, int &nLength, PCSTR &pszLine);
void GetText(int nStartLine, int nStartChar, int nEndLine, int nEndChar, CString &text)
{
_pBuffer->GetText(nStartLine, nStartChar, nEndLine, nEndChar, text);
}
private:
CCrystalTextBuffer *_pBuffer;
int _nLineLimit; // Line limit
int _nCharLimit; // Char limit for last line
// Optional things for doing autocomplete
HANDLE _hDoAC;
HANDLE _hACDone;
bool _fCancel;
int _id;
// Or tooltips
ISyntaxParserCallback *_pCallback;
};
class CCrystalScriptStream
{
public:
CCrystalScriptStream(CScriptStreamLimiter *pBuffer);
class const_iterator
{
public:
const_iterator() : _pBuffer(NULL), _pidStream(NULL), _id(0) {}
const_iterator(CScriptStreamLimiter *pBuffer, LineCol dwPos = LineCol());
char operator*();
const_iterator& operator++();
bool operator<(const const_iterator& _Right) const;
std::string tostring() const;
LineCol GetPosition() const;
int GetLineNumber() const;
int GetColumnNumber() const;
bool operator==(const const_iterator& value) const;
bool operator!=(const const_iterator& value) const;
private:
CScriptStreamLimiter *_pBuffer;
int _nLine;
int _nChar;
PCTSTR _pszLine;
int _nLength;
int _id;
int *_pidStream;
};
const_iterator begin() { return const_iterator(_pBuffer); }
const_iterator get_at(LineCol dwPos) { return const_iterator(_pBuffer, dwPos); }
protected:
CScriptStreamLimiter *_pBuffer;
};
//
// For tokenizing for CPP syntax parser
//
enum SyntaxToken
{
// REVIEW: I commented some of these out... they result in us tokenizing things when we shouldn't.
// e.g. x = scriptNumber. the parser expects scriptNumber to be an alphanumeric thing. Not a "token".
// So I don't tokenize it. An alternative is that we could match some tokens to alphanumeric stuff...
// but they would have to be marked specially (e.g. we don't want to map "include" for instance).
TokenComment = 128,
TokenAlphanum,
//TokenBool,
TokenSaidString,
TokenString,
TokenSaid,
TokenBreak,
TokenCase,
TokenClass2,
TokenDefault,
TokenDo,
TokenElse,
TokenFor,
TokenIf,
TokenInclude,
TokenInstance2,
//TokenInt,
//TokenUInt,
TokenPublic,
TokenPrivate,
TokenRest,
TokenRes,
TokenReturn,
//TokenScriptNumber,
TokenScript,
//TokenSelf2,
//TokenSuper,
TokenSwitch,
TokenUse,
TokenDefine,
TokenSynonym,
//TokenVoid,
//TokenVar,
TokenWhile,
TokenEllipsis,
TokenRightAssign,
TokenLeftAssign,
TokenAddAssign,
TokenSubAssign,
TokenMulAssign,
TokenDivAssign,
TokenModAssign,
TokenAndAssign,
TokenOrAssign,
TokenXorAssign,
TokenRightOp,
TokenLeftOp,
TokenIncOp,
TokenDecOp,
TokenAndOp,
TokenOrOp,
TokenLeOp,
TokenGeOp,
TokenEqOp,
TokenNeOp,
TokenQuotedString,
TokenSingleQuotedString,
TokenInteger,
// etc...
TokenCast,
TokenInvalid,
TokenEnd,
};
class TokenInfo
{
public:
TokenInfo()
{
type = TokenInvalid;
integer = 0;
intFlags = IFNone;
}
SyntaxToken type;
LineCol start; // Start position
LineCol end; // One after the end position
int integer;
IntegerFlags intFlags;
std::string string;
};
SyntaxToken SyntaxTokenFromString(const char *psz);
const char *StringFromSyntaxToken(SyntaxToken token);
//std::string StringFromSyntaxTokenFallback(SyntaxToken token);
namespace sci
{
class Script;
};
//
// This aggregates a CCrystalScriptStream while being a stream itself.
// It basically provides a tokenized stream.
//
class CrystalScriptTokenStream
{
public:
CrystalScriptTokenStream(CCrystalScriptStream &buffer) : _stream(buffer) {}
void Tokenize(sci::Script *pScriptForComments = NULL);
void DebugPrint();
class const_iterator
{
private:
std::vector<TokenInfo>::const_iterator _it;
std::vector<TokenInfo>::const_iterator _itEnd;
public:
const_iterator(std::vector<TokenInfo> &tokens, size_t iIndex = 0) :
_it(&tokens[iIndex]), _itEnd(tokens.end())
{
}
TokenInfo operator*()
{
return *_it;
}
const_iterator& operator++()
{
ASSERT((*_it).type != TokenEnd); // ASSERT no one advanced past end.
_it++;
return *this;
}
bool operator<(const const_iterator& _Right) const
{
return _it < _Right._it;
}
//std::string tostring() const;
LineCol GetPosition() const
{
return _it->start;
}
LineCol GetEndPosition() const
{
return _it->end;
}
int GetLineNumber() const
{
return GetPosition().Line();
}
int GetColumnNumber() const
{
return GetPosition().Column();
}
bool operator==(const const_iterator& value) const
{
return (_it == value._it);
}
bool operator!=(const const_iterator& value) const
{
return (_it != value._it);
}
const_iterator &operator=(const const_iterator& value)
{
_itEnd = value._itEnd;
_it = value._it;
return *this;
}
};
const_iterator begin() { return const_iterator(_tokens); }
private:
void _ConvertAliasedTokens(std::string &token);
// PERF: provide good starting size based on script size (e.g. reserve).
// Cost: about 2% of compile time.
std::vector<TokenInfo> _tokens;
CCrystalScriptStream &_stream;
};
std::string GetTokenAlias(const std::string &token);