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
|
// Scintilla source code edit control
/** @file ExternalLexer.cxx
** Support external lexers in DLLs.
**/
// Copyright 2001 Simon Steele <ss@pnotepad.org>, portions copyright Neil Hodgson.
// The License.txt file describes the conditions under which this software may be distributed.
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "SciLexer.h"
#include "Platform.h"
#include "PropSet.h"
#include "Accessor.h"
#include "DocumentAccessor.h"
#include "KeyWords.h"
#include "ExternalLexer.h"
// Initialise the static vars...
int LexerManager::UseCount = 0;
LexerLibrary *LexerManager::first = NULL;
LexerLibrary *LexerManager::last = NULL;
LexerManager *LexerManager::firstlm = NULL;
//------------------------------------------
//
// ExternalLexerModule
//
//------------------------------------------
char **WordListsToStrings(WordList *val[]) {
int dim = 0;
while (val[dim])
dim++;
char **wls = new char * [dim + 1];
for (int i = 0;i < dim;i++) {
SString words;
words = "";
for (int n = 0; n < val[i]->len; n++) {
words += val[i]->words[n];
if (n != val[i]->len - 1)
words += " ";
}
wls[i] = new char[words.length() + 1];
strcpy(wls[i], words.c_str());
}
wls[dim] = 0;
return wls;
}
void DeleteWLStrings(char *strs[]) {
int dim = 0;
while (strs[dim]) {
delete strs[dim];
dim++;
}
delete [] strs;
}
void ExternalLexerModule::Lex(unsigned int startPos, int lengthDoc, int initStyle,
WordList *keywordlists[], Accessor &styler) {
if (!fneLexer)
return ;
char **kwds = WordListsToStrings(keywordlists);
char *ps = styler.GetProperties();
// The accessor passed in is always a DocumentAccessor so this cast and the subsequent
// access will work. Can not use the stricter dynamic_cast as that requires RTTI.
DocumentAccessor &da = static_cast<DocumentAccessor &>(styler);
WindowID wID = da.GetWindow();
fneLexer(externalLanguage, startPos, lengthDoc, initStyle, kwds, wID, ps);
delete ps;
DeleteWLStrings(kwds);
}
void ExternalLexerModule::Fold(unsigned int startPos, int lengthDoc, int initStyle,
WordList *keywordlists[], Accessor &styler) {
if (!fneFolder)
return ;
char **kwds = WordListsToStrings(keywordlists);
char *ps = styler.GetProperties();
// The accessor passed in is always a DocumentAccessor so this cast and the subsequent
// access will work. Can not use the stricter dynamic_cast as that requires RTTI.
DocumentAccessor &da = static_cast<DocumentAccessor &>(styler);
WindowID wID = da.GetWindow();
fneFolder(externalLanguage, startPos, lengthDoc, initStyle, kwds, wID, ps);
delete ps;
DeleteWLStrings(kwds);
}
void ExternalLexerModule::SetExternal(ExtLexerFunction fLexer, ExtFoldFunction fFolder, int index) {
fneLexer = fLexer;
fneFolder = fFolder;
externalLanguage = index;
}
//------------------------------------------
//
// LexerLibrary
//
//------------------------------------------
LexerLibrary::LexerLibrary(LPCTSTR ModuleName) {
// Initialise some members...
first = NULL;
last = NULL;
// Load the DLL
m_hModule = LoadLibrary(ModuleName);
if (m_hModule) {
m_sModuleName = ModuleName;
GetLexerCountFn GetLexerCount = (GetLexerCountFn)GetProcAddress(m_hModule, "GetLexerCount");
if (GetLexerCount) {
ExternalLexerModule *lex;
LexerMinder *lm;
// Find functions in the DLL
GetLexerNameFn GetLexerName = (GetLexerNameFn)GetProcAddress(m_hModule, "GetLexerName");
ExtLexerFunction Lexer = (ExtLexerFunction)GetProcAddress(m_hModule, "Lex");
ExtFoldFunction Folder = (ExtFoldFunction)GetProcAddress(m_hModule, "Fold");
// Assign a buffer for the lexer name.
char lexname[100];
strcpy(lexname, "");
int nl = GetLexerCount();
for (int i = 0; i < nl; i++) {
GetLexerName(i, lexname, 100);
lex = new ExternalLexerModule(SCLEX_AUTOMATIC, NULL, lexname, NULL);
// Create a LexerMinder so we don't leak the ExternalLexerModule...
lm = new LexerMinder;
lm->self = lex;
lm->next = NULL;
if (first != NULL) {
last->next = lm;
last = lm;
} else {
first = lm;
last = lm;
}
// The external lexer needs to know how to call into its DLL to
// do its lexing and folding, we tell it here. Folder may be null.
lex->SetExternal(Lexer, Folder, i);
}
}
}
next = NULL;
}
LexerLibrary::~LexerLibrary() {
Release();
}
void LexerLibrary::Release() {
//TODO maintain a list of lexers created, and delete them!
LexerMinder *lm;
LexerMinder *next;
lm = first;
while (NULL != lm) {
next = lm->next;
delete lm->self;
delete lm;
lm = next;
}
first = NULL;
last = NULL;
// Release the DLL
if (NULL != m_hModule) {
FreeLibrary(m_hModule);
}
}
//------------------------------------------
//
// LexerManager
//
//------------------------------------------
int FindLastSlash(char *inp) {
int i;
int ret = -1;
for (i = strlen(inp) - 1; i >= 0; i--) {
if (inp[i] == '\\' || inp[i] == '/') {
// if you don't like break:
/*
if (ret == -1)
*/
ret = i;
break;
}
}
return ret;
}
LexerManager::LexerManager() {
UseCount++;
if (1 == UseCount) {
firstlm = this;
m_bLoaded = false;
}
}
void LexerManager::EnumerateLexers() {
HANDLE hFind;
WIN32_FIND_DATA FindFileData;
char path[MAX_PATH + 1];
GetModuleFileName(GetModuleHandle(NULL), path, MAX_PATH);
int i = FindLastSlash(path);
if (i == -1)
i = strlen(path);
SString sPath(path, 0, i);
// Ensure a trailing slash...
if ( sPath[sPath.size() - 1] != '/' && sPath[sPath.size() - 1] != '\\' ) {
sPath += '\\';
}
SString sPattern(sPath);
sPattern.append("*.lexer");
hFind = FindFirstFile(sPattern.c_str(), &FindFileData);
if (hFind != INVALID_HANDLE_VALUE) {
//Found the first file...
BOOL found = TRUE;
SString to_open;
while (found) {
to_open.assign(sPath);
to_open += FindFileData.cFileName;
LexerLibrary *lib = new LexerLibrary(to_open.c_str());
if (NULL != first) {
last->next = lib;
last = lib;
} else {
first = lib;
last = lib;
}
found = FindNextFile(hFind, &FindFileData);
}
FindClose(hFind);
}
}
LexerManager::~LexerManager() {
// If this is the last LexerManager to be freed then
// we delete all of our LexerLibrarys.
UseCount--;
if (0 == UseCount) {
if (NULL != first) {
LexerLibrary *cur = first;
LexerLibrary *next = first->next;
while (cur) {
delete cur;
cur = next;
}
first = NULL;
last = NULL;
}
}
if (this == firstlm)
firstlm = NULL;
}
void LexerManager::Load()
{
if(!m_bLoaded)
{
m_bLoaded = true;
EnumerateLexers();
}
}
// Return a LexerManager, or create one and then return it.
LexerManager *LexerManager::GetInstance() {
if(!firstlm)
firstlm = new LexerManager;
return firstlm;
}
LMMinder::~LMMinder()
{
LexerManager *rem = LexerManager::firstlm;
if(rem)
delete rem;
}
LMMinder minder;
|