1 // Scintilla source code edit control
2 /** @file ExternalLexer.cxx
3 ** Support external lexers in DLLs.
5 // Copyright 2001 Simon Steele <ss@pnotepad.org>, portions copyright Neil Hodgson.
6 // The License.txt file describes the conditions under which this software may be distributed.
19 #include "Scintilla.h"
22 #include "LexerModule.h"
23 #include "Catalogue.h"
24 #include "ExternalLexer.h"
27 using namespace Scintilla
;
30 LexerManager
*LexerManager::theInstance
= NULL
;
32 //------------------------------------------
34 // ExternalLexerModule
36 //------------------------------------------
38 void ExternalLexerModule::SetExternal(GetLexerFactoryFunction fFactory
, int index
) {
39 fneFactory
= fFactory
;
40 fnFactory
= fFactory(index
);
41 externalLanguage
= index
;
44 //------------------------------------------
48 //------------------------------------------
50 LexerLibrary::LexerLibrary(const char *ModuleName
) {
51 // Initialise some members...
56 lib
= DynamicLibrary::Load(ModuleName
);
58 m_sModuleName
= ModuleName
;
59 //Cannot use reinterpret_cast because: ANSI C++ forbids casting between pointers to functions and objects
60 GetLexerCountFn GetLexerCount
= (GetLexerCountFn
)(sptr_t
)lib
->FindFunction("GetLexerCount");
63 ExternalLexerModule
*lex
;
66 // Find functions in the DLL
67 GetLexerNameFn GetLexerName
= (GetLexerNameFn
)(sptr_t
)lib
->FindFunction("GetLexerName");
68 GetLexerFactoryFunction fnFactory
= (GetLexerFactoryFunction
)(sptr_t
)lib
->FindFunction("GetLexerFactory");
70 // Assign a buffer for the lexer name.
74 int nl
= GetLexerCount();
76 for (int i
= 0; i
< nl
; i
++) {
77 GetLexerName(i
, lexname
, 100);
78 lex
= new ExternalLexerModule(SCLEX_AUTOMATIC
, NULL
, lexname
, NULL
);
79 Catalogue::AddLexerModule(lex
);
81 // Create a LexerMinder so we don't leak the ExternalLexerModule...
93 // The external lexer needs to know how to call into its DLL to
94 // do its lexing and folding, we tell it here.
95 lex
->SetExternal(fnFactory
, i
);
102 LexerLibrary::~LexerLibrary() {
107 void LexerLibrary::Release() {
122 //------------------------------------------
126 //------------------------------------------
128 /// Return the single LexerManager instance...
129 LexerManager
*LexerManager::GetInstance() {
131 theInstance
= new LexerManager
;
135 /// Delete any LexerManager instance...
136 void LexerManager::DeleteInstance() {
141 /// protected constructor - this is a singleton...
142 LexerManager::LexerManager() {
147 LexerManager::~LexerManager() {
151 void LexerManager::Load(const char *path
) {
152 LoadLexerLibrary(path
);
155 void LexerManager::LoadLexerLibrary(const char *module
) {
156 for (LexerLibrary
*ll
= first
; ll
; ll
= ll
->next
) {
157 if (strcmp(ll
->m_sModuleName
.c_str(), module
) == 0)
160 LexerLibrary
*lib
= new LexerLibrary(module
);
170 void LexerManager::Clear() {
172 LexerLibrary
*cur
= first
;
184 //------------------------------------------
188 //------------------------------------------
190 LMMinder::~LMMinder() {
191 LexerManager::DeleteInstance();