diff options
Diffstat (limited to 'lexers/LexD.cxx')
| -rw-r--r-- | lexers/LexD.cxx | 223 | 
1 files changed, 173 insertions, 50 deletions
| diff --git a/lexers/LexD.cxx b/lexers/LexD.cxx index 563395411..c9ef39343 100644 --- a/lexers/LexD.cxx +++ b/lexers/LexD.cxx @@ -2,16 +2,24 @@   ** Lexer for D.   **   ** Copyright (c) 2006 by Waldemar Augustyn <waldemar@wdmsys.com> + ** Converted to lexer object by "Udo Lechner" <dlchnr(at)gmx(dot)net>   **/  // Copyright 1998-2005 by Neil Hodgson <neilh@scintilla.org>  // The License.txt file describes the conditions under which this software may be distributed.  #include <stdlib.h>  #include <string.h> +#include <ctype.h>  #include <stdio.h>  #include <stdarg.h>  #include <assert.h> -#include <ctype.h> + +#ifdef _MSC_VER +#pragma warning(disable: 4786) +#endif + +#include <string> +#include <map>  #include "ILexer.h"  #include "Scintilla.h" @@ -23,6 +31,7 @@  #include "StyleContext.h"  #include "CharacterSet.h"  #include "LexerModule.h" +#include "OptionSet.h"  #ifdef SCI_NAMESPACE  using namespace Scintilla; @@ -58,17 +67,157 @@ static bool IsStringSuffix(int ch) {  	return ch == 'c' || ch == 'w' || ch == 'd';  } +static bool IsStreamCommentStyle(int style) { +	return style == SCE_D_COMMENT || +		style == SCE_D_COMMENTDOC || +		style == SCE_D_COMMENTDOCKEYWORD || +		style == SCE_D_COMMENTDOCKEYWORDERROR; +} + +// An individual named option for use in an OptionSet + +// Options used for LexerD +struct OptionsD { +	bool fold; +	bool foldComment; +	bool foldCompact; +	int  foldAtElseInt; +	bool foldAtElse; +	OptionsD() { +		fold = false; +		foldComment = false; +		foldCompact = true; +		foldAtElseInt = -1; +		foldAtElse = false; +	} +}; + +static const char * const dWordLists[] = { +			"Primary keywords and identifiers", +			"Secondary keywords and identifiers", +			"Documentation comment keywords", +			"Type definitions and aliases", +			"Keywords 5", +			"Keywords 6", +			"Keywords 7", +			0, +		}; + +struct OptionSetD : public OptionSet<OptionsD> { +	OptionSetD() { +		DefineProperty("fold", &OptionsD::fold); + +		DefineProperty("fold.comment", &OptionsD::foldComment); + +		DefineProperty("fold.compact", &OptionsD::foldCompact); + +		DefineProperty("lexer.d.fold.at.else", &OptionsD::foldAtElseInt, +			"This option enables D folding on a \"} else {\" line of an if statement."); + +		DefineProperty("fold.at.else", &OptionsD::foldAtElse); + +		DefineWordListSets(dWordLists); +	} +}; + +class LexerD : public ILexer { +	bool caseSensitive; +	WordList keywords; +	WordList keywords2; +	WordList keywords3; +	WordList keywords4; +	WordList keywords5; +	WordList keywords6; +	WordList keywords7; +	OptionsD options; +	OptionSetD osD; +public: +	LexerD(bool caseSensitive_) : +		caseSensitive(caseSensitive_) { +	} +	~LexerD() { +	} +	void SCI_METHOD Release() { +		delete this; +	} +	int SCI_METHOD Version() const { +		return lvOriginal; +	} +	const char * SCI_METHOD PropertyNames() { +		return osD.PropertyNames(); +	} +	int SCI_METHOD PropertyType(const char *name) { +		return osD.PropertyType(name); +	} +	const char * SCI_METHOD DescribeProperty(const char *name) { +		return osD.DescribeProperty(name); +	} +	int SCI_METHOD PropertySet(const char *key, const char *val); +	const char * SCI_METHOD DescribeWordListSets() { +		return osD.DescribeWordListSets(); +	} +	int SCI_METHOD WordListSet(int n, const char *wl); +	void SCI_METHOD Lex(unsigned int startPos, int length, int initStyle, IDocument *pAccess); +	void SCI_METHOD Fold(unsigned int startPos, int length, int initStyle, IDocument *pAccess); + +	void * SCI_METHOD PrivateCall(int, void *) { +		return 0; +	} + +	static ILexer *LexerFactoryD() { +		return new LexerD(true); +	} +	static ILexer *LexerFactoryDInsensitive() { +		return new LexerD(false); +	} +}; + +int SCI_METHOD LexerD::PropertySet(const char *key, const char *val) { +	if (osD.PropertySet(&options, key, val)) { +		return 0; +	} +	return -1; +} -static void ColouriseDoc(unsigned int startPos, int length, int initStyle, -	WordList *keywordlists[], Accessor &styler, bool caseSensitive) { +int SCI_METHOD LexerD::WordListSet(int n, const char *wl) { +	WordList *wordListN = 0; +	switch (n) { +	case 0: +		wordListN = &keywords; +		break; +	case 1: +		wordListN = &keywords2; +		break; +	case 2: +		wordListN = &keywords3; +		break; +	case 3: +		wordListN = &keywords4; +		break; +	case 4: +		wordListN = &keywords5; +		break; +	case 5: +		wordListN = &keywords6; +		break; +	case 6: +		wordListN = &keywords7; +		break; +	} +	int firstModification = -1; +	if (wordListN) { +		WordList wlNew; +		wlNew.Set(wl); +		if (*wordListN != wlNew) { +			wordListN->Set(wl); +			firstModification = 0; +		} +	} +	return firstModification; +} -	WordList &keywords  = *keywordlists[0]; -	WordList &keywords2 = *keywordlists[1]; -	WordList &keywords3 = *keywordlists[2]; //doxygen -	WordList &keywords4 = *keywordlists[3]; -	WordList &keywords5 = *keywordlists[4]; -	WordList &keywords6 = *keywordlists[5]; -	WordList &keywords7 = *keywordlists[6]; +void SCI_METHOD LexerD::Lex(unsigned int startPos, int length, int initStyle, IDocument *pAccess) { +	LexAccessor styler(pAccess);  	int styleBeforeDCKeyword = SCE_D_DEFAULT; @@ -293,24 +442,18 @@ static void ColouriseDoc(unsigned int startPos, int length, int initStyle,  	sc.Complete();  } -static bool IsStreamCommentStyle(int style) { -	return style == SCE_D_COMMENT || -		style == SCE_D_COMMENTDOC || -		style == SCE_D_COMMENTDOCKEYWORD || -		style == SCE_D_COMMENTDOCKEYWORDERROR; -} -  // Store both the current line's fold level and the next lines in the  // level store to make it easy to pick up with each increment  // and to make it possible to fiddle the current level for "} else {". -static void FoldDoc(unsigned int startPos, int length, int initStyle, Accessor &styler) { -	bool foldComment = styler.GetPropertyInt("fold.comment") != 0; -	bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0; - -	// property lexer.d.fold.at.else -	//  This option enables D folding on a "} else {" line of an if statement. -	bool foldAtElse = styler.GetPropertyInt("lexer.d.fold.at.else", -		styler.GetPropertyInt("fold.at.else", 0)) != 0; + +void SCI_METHOD LexerD::Fold(unsigned int startPos, int length, int initStyle, IDocument *pAccess) { + + +	if (!options.fold) +		return; + +	LexAccessor styler(pAccess); +  	unsigned int endPos = startPos + length;  	int visibleChars = 0;  	int lineCurrent = styler.GetLine(startPos); @@ -322,6 +465,7 @@ static void FoldDoc(unsigned int startPos, int length, int initStyle, Accessor &  	char chNext = styler[startPos];  	int styleNext = styler.StyleAt(startPos);  	int style = initStyle; +	bool foldAtElse = options.foldAtElseInt >= 0 ? options.foldAtElseInt != 0 : options.foldAtElse;  	for (unsigned int i = startPos; i < endPos; i++) {  		char ch = chNext;  		chNext = styler.SafeGetCharAt(i + 1); @@ -329,7 +473,7 @@ static void FoldDoc(unsigned int startPos, int length, int initStyle, Accessor &  		style = styleNext;  		styleNext = styler.StyleAt(i + 1);  		bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n'); -		if (foldComment && IsStreamCommentStyle(style)) { +		if (options.foldComment && IsStreamCommentStyle(style)) {  			if (!IsStreamCommentStyle(stylePrev)) {  				levelNext++;  			} else if (!IsStreamCommentStyle(styleNext) && !atEOL) { @@ -350,7 +494,7 @@ static void FoldDoc(unsigned int startPos, int length, int initStyle, Accessor &  			}  		}  		if (atEOL) { -			if (foldComment) {  // Handle nested comments +			if (options.foldComment) {  // Handle nested comments  				int nc;  				nc =  styler.GetLineState(lineCurrent);  				nc -= lineCurrent>0? styler.GetLineState(lineCurrent-1): 0; @@ -361,7 +505,7 @@ static void FoldDoc(unsigned int startPos, int length, int initStyle, Accessor &  				levelUse = levelMinCurrent;  			}  			int lev = levelUse | levelNext << 16; -			if (visibleChars == 0 && foldCompact) +			if (visibleChars == 0 && options.foldCompact)  				lev |= SC_FOLDLEVELWHITEFLAG;  			if (levelUse < levelNext)  				lev |= SC_FOLDLEVELHEADERFLAG; @@ -378,25 +522,4 @@ static void FoldDoc(unsigned int startPos, int length, int initStyle, Accessor &  	}  } -static void FoldDDoc(unsigned int startPos, int length, int initStyle, -	WordList *[], Accessor &styler) { -		FoldDoc(startPos, length, initStyle, styler); -} - -static const char * const dWordLists[] = { -			"Primary keywords and identifiers", -			"Secondary keywords and identifiers", -			"Documentation comment keywords", -			"Type definitions and aliases", -			"Keywords 5", -			"Keywords 6", -			"Keywords 7", -			0, -		}; - -static void ColouriseDDoc(unsigned int startPos, int length, -	int initStyle, WordList *keywordlists[], Accessor &styler) { -		ColouriseDoc(startPos, length, initStyle, keywordlists, styler, true); -} - -LexerModule lmD(SCLEX_D, ColouriseDDoc, "d", FoldDDoc, dWordLists); +LexerModule lmD(SCLEX_D, LexerD::LexerFactoryD, "d", dWordLists); | 
