From 3e36fa6f3ea27bb64ce42bdd1a5b50b52dc6bb04 Mon Sep 17 00:00:00 2001 From: nyamatongwe Date: Fri, 13 Apr 2001 04:55:04 +0000 Subject: Start of new lexer infrastructure. Lexers can have a fold function as well as a lexer function. They can be identified by string name as well as an integer ID and may ask to be automatically assigned that ID. --- src/KeyWords.cxx | 64 +++++++++++++++++++++++++++++++++++++++++---------- src/LexCPP.cxx | 3 +-- src/ScintillaBase.cxx | 31 +++++++++++++++++++++++-- src/ScintillaBase.h | 3 +++ 4 files changed, 85 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/KeyWords.cxx b/src/KeyWords.cxx index a275c1963..f28fcecfb 100644 --- a/src/KeyWords.cxx +++ b/src/KeyWords.cxx @@ -20,32 +20,70 @@ #include "SciLexer.h" LexerModule *LexerModule::base = 0; +int LexerModule::nextLanguage = SCLEX_AUTOMATIC+1; -LexerModule::LexerModule(int language_, LexerFunction fn_) : - language(language_), fn(fn_) { +LexerModule::LexerModule(int language_, LexerFunction fnLexer_, + const char *languageName_, LexerFunction fnFolder_) : + language(language_), + languageName(languageName_), + fnLexer(fnLexer_), + fnFolder(fnFolder_) { next = base; base = this; + if (language == SCLEX_AUTOMATIC) { + language = nextLanguage; + nextLanguage++; + } } -void LexerModule::Colourise(unsigned int startPos, int lengthDoc, int initStyle, - int language, WordList *keywordlists[], Accessor &styler) { +LexerModule *LexerModule::Find(int language) { LexerModule *lm = base; while (lm) { if (lm->language == language) { - lm->fn(startPos, lengthDoc, initStyle, keywordlists, styler); - return; + return lm; } lm = lm->next; } - // Unknown language + return 0; +} + +LexerModule *LexerModule::Find(const char *languageName) { + if (languageName) { + LexerModule *lm = base; + while (lm) { + if (lm->languageName && 0 == strcmp(lm->languageName, languageName)) { + return lm; + } + lm = lm->next; + } + } + return 0; +} + +void LexerModule::Lex(unsigned int startPos, int lengthDoc, int initStyle, + WordList *keywordlists[], Accessor &styler) { + if (fnLexer) + fnLexer(startPos, lengthDoc, initStyle, keywordlists, styler); +} + +void LexerModule::Fold(unsigned int startPos, int lengthDoc, int initStyle, + WordList *keywordlists[], Accessor &styler) { + if (fnFolder) + fnFolder(startPos, lengthDoc, initStyle, keywordlists, styler); +} + +static void ColouriseNullDoc(unsigned int startPos, int length, int, WordList *[], + Accessor &styler) { // Null language means all style bytes are 0 so just mark the end - no need to fill in. - if (lengthDoc > 0) { - styler.StartAt(startPos + lengthDoc - 1); - styler.StartSegment(startPos + lengthDoc - 1); - styler.ColourTo(startPos + lengthDoc - 1, 0); + if (length > 0) { + styler.StartAt(startPos + length - 1); + styler.StartSegment(startPos + length - 1); + styler.ColourTo(startPos + length - 1, 0); } } +LexerModule lmNull(SCLEX_NULL, ColouriseNullDoc, "null"); + #ifdef __vms // The following code forces a reference to all of the Scintilla lexers. @@ -55,6 +93,7 @@ void LexerModule::Colourise(unsigned int startPos, int lengthDoc, int initStyle, // Taken from wxWindow's stc.cpp. Walter. int wxForceScintillaLexers(void) { + extern LexerModule lmAda; extern LexerModule lmCPP; extern LexerModule lmHTML; extern LexerModule lmXML; @@ -68,7 +107,8 @@ int wxForceScintillaLexers(void) { extern LexerModule lmVB; if ( - &lmCPP + &lmAda + && &lmCPP && &lmHTML && &lmXML && &lmProps diff --git a/src/LexCPP.cxx b/src/LexCPP.cxx index 966601ed3..826ac259f 100644 --- a/src/LexCPP.cxx +++ b/src/LexCPP.cxx @@ -328,7 +328,6 @@ static void ColouriseCppDoc(unsigned int startPos, int length, int initStyle, Wo } styler.ColourTo(lengthDoc - 1, state); styler.Flush(); - FoldCppDoc(startPos, length, initStyle, keywordlists, styler); } -LexerModule lmCPP(SCLEX_CPP, ColouriseCppDoc); +LexerModule lmCPP(SCLEX_CPP, ColouriseCppDoc, "cpp", FoldCppDoc); diff --git a/src/ScintillaBase.cxx b/src/ScintillaBase.cxx index fd2bef184..4eb0cf7a0 100644 --- a/src/ScintillaBase.cxx +++ b/src/ScintillaBase.cxx @@ -38,6 +38,7 @@ ScintillaBase::ScintillaBase() { listType = 0; #ifdef SCI_LEXER lexLanguage = SCLEX_CONTAINER; + lexCurrent = 0; for (int wl = 0;wl < numWordLists;wl++) keyWordLists[wl] = new WordList; #endif @@ -338,6 +339,22 @@ void ScintillaBase::ButtonDown(Point pt, unsigned int curTime, bool shift, bool } #ifdef SCI_LEXER +void ScintillaBase::SetLexer(uptr_t wParam) { + lexLanguage = wParam; + lexCurrent = LexerModule::Find(lexLanguage); + if (!lexCurrent) + lexCurrent = LexerModule::Find(SCLEX_NULL); +} + +void ScintillaBase::SetLexerLanguage(const char *languageName) { + lexLanguage = SCLEX_CONTAINER; + lexCurrent = LexerModule::Find(languageName); + if (!lexCurrent) + lexCurrent = LexerModule::Find(SCLEX_NULL); + if (lexCurrent) + lexLanguage = lexCurrent->GetLanguage(); +} + void ScintillaBase::Colourise(int start, int end) { int lengthDoc = pdoc->Length(); if (end == -1) @@ -352,8 +369,12 @@ void ScintillaBase::Colourise(int start, int end) { styleStart = styler.StyleAt(start - 1); styler.SetCodePage(pdoc->dbcsCodePage); - LexerModule::Colourise(start, len, styleStart, lexLanguage, keyWordLists, styler); - styler.Flush(); + if (lexCurrent) { // Should always succeed as null lexer should always be available + lexCurrent->Lex(start, len, styleStart, keyWordLists, styler); + styler.Flush(); + lexCurrent->Fold(start, len, styleStart, keyWordLists, styler); + styler.Flush(); + } } #endif @@ -482,6 +503,7 @@ sptr_t ScintillaBase::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lPara #ifdef SCI_LEXER case SCI_SETLEXER: + SetLexer(wParam); lexLanguage = wParam; break; @@ -504,6 +526,11 @@ sptr_t ScintillaBase::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lPara keyWordLists[wParam]->Set(reinterpret_cast(lParam)); } break; + + case SCI_SETLEXERLANGUAGE: + SetLexerLanguage(reinterpret_cast(lParam)); + break; + #endif default: diff --git a/src/ScintillaBase.h b/src/ScintillaBase.h index 758f75a4c..13172031a 100644 --- a/src/ScintillaBase.h +++ b/src/ScintillaBase.h @@ -40,9 +40,12 @@ protected: #ifdef SCI_LEXER int lexLanguage; + LexerModule *lexCurrent; PropSet props; enum {numWordLists=5}; WordList *keyWordLists[numWordLists]; + void SetLexer(uptr_t wParam); + void SetLexerLanguage(const char *languageName); void Colourise(int start, int end); #endif -- cgit v1.2.3