diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/LexLua.cxx | 334 | 
1 files changed, 150 insertions, 184 deletions
| diff --git a/src/LexLua.cxx b/src/LexLua.cxx index b9303ad41..dfa5d848e 100644 --- a/src/LexLua.cxx +++ b/src/LexLua.cxx @@ -18,212 +18,176 @@  #include "PropSet.h"  #include "Accessor.h" +#include "StyleContext.h"  #include "KeyWords.h"  #include "Scintilla.h"  #include "SciLexer.h" + +static inline bool IsAWordChar(const int ch) { +	return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_'); +} + +inline bool IsAWordStart(const int ch) { +	return (ch < 0x80) && (isalnum(ch) || ch == '_'); +} + +  inline bool isLuaOperator(char ch) {  	if (isalnum(ch))  		return false;  	// '.' left out as it is used to make up numbers  	if (ch == '*' || ch == '/' || ch == '-' || ch == '+' || -	        ch == '(' || ch == ')' || ch == '=' || -	        ch == '{' || ch == '}' || ch == '~' || -	        ch == '[' || ch == ']' || ch == ';' || -	        ch == '<' || ch == '>' || ch == ',' || -	        ch == '.' || ch == '^' || ch == '%' || ch == ':') +		ch == '(' || ch == ')' || ch == '=' || +		ch == '{' || ch == '}' || ch == '~' || +		ch == '[' || ch == ']' || ch == ';' || +		ch == '<' || ch == '>' || ch == ',' || +		ch == '.' || ch == '^' || ch == '%' || ch == ':')  		return true;  	return false;  } -static void ColouriseLuaDoc(unsigned int startPos, -                            int length, -                            int initStyle, -                            WordList *keywordLists[], -                            Accessor &styler) { - -	WordList &keywords = *keywordLists[0]; -	WordList &keywords2 = *keywordLists[1]; -	WordList &keywords3 = *keywordLists[2]; -	WordList &keywords4 = *keywordLists[3]; -	WordList &keywords5 = *keywordLists[4]; -	WordList &keywords6 = *keywordLists[5]; - -	styler.StartAt(startPos); -	styler.GetLine(startPos); -	int state = initStyle; -	char chPrev = ' '; -	char chNext = styler[startPos]; -	unsigned int lengthDoc = startPos + length; -	bool firstChar = true; +static void ColouriseLuaDoc(unsigned int startPos, int length, int initStyle, WordList *keywordlists[], +                            Accessor &styler) { -	/* Must initialize the literalString level, if we are inside such a string. -	 * Note: this isn't enough, because literal strings can be nested, -	 * we should go back to see at what level we are... -	 */ -	int literalString = (initStyle == SCE_LUA_LITERALSTRING) ? 1 : 0; +	WordList &keywords = *keywordlists[0]; +	WordList &keywords2 = *keywordlists[1]; +	WordList &keywords3 = *keywordlists[2]; +	WordList &keywords4 = *keywordlists[3]; +	WordList &keywords5 = *keywordlists[4]; +	WordList &keywords6 = *keywordlists[5]; +	int literalString = 0; +	int literalStringFlag =0; -	styler.StartSegment(startPos); -	for (unsigned int i = startPos; i <= lengthDoc; i++) { -		char ch = chNext; -		chNext = styler.SafeGetCharAt(i + 1); +	// Do not leak onto next line +	if (initStyle == SCE_LUA_STRINGEOL) +		initStyle = SCE_LUA_DEFAULT; -		if (styler.IsLeadByte(ch)) { -			chNext = styler.SafeGetCharAt(i + 2); -			chPrev = ' '; -			i += 1; -			continue; -		} +	int chPrevNonWhite = ' '; +	int visibleChars = 0; -		if (state == SCE_LUA_STRINGEOL) { -			if (ch != '\r' && ch != '\n') { -				styler.ColourTo(i - 1, state); -				state = SCE_LUA_DEFAULT; +	StyleContext sc(startPos, length, initStyle, styler); +	if(startPos == 0 && sc.ch == '#') sc.SetState(SCE_LUA_COMMENTLINE); +	for (; sc.More(); sc.Forward()) { +	 +		// Handle line continuation generically. +		if (sc.ch == '\\') { +			if (sc.Match("\\\n")) { +				sc.Forward(); +				sc.Forward(); +				continue; +			} +			if (sc.Match("\\\r\n")) { +				sc.Forward(); +				sc.Forward(); +				sc.Forward(); +				continue;  			}  		} -		if (state == SCE_LUA_DEFAULT) { -			if (ch == '-' && chNext == '-') { -				styler.ColourTo(i - 1, state); -				state = SCE_LUA_COMMENTLINE; -			} else if (ch == '[' && chNext == '[') { -				state = SCE_LUA_LITERALSTRING; -				literalString = 1; -			} else if (ch == '\"') { -				styler.ColourTo(i - 1, state); -				state = SCE_LUA_STRING; -			} else if (ch == '\'') { -				styler.ColourTo(i - 1, state); -				state = SCE_LUA_CHARACTER; -			} else if (ch == '$' && firstChar) { -				styler.ColourTo(i - 1, state); -				state = SCE_LUA_PREPROCESSOR; -			} else if (ch == '#' && firstChar) {	// Should be only on the first line of the file! Cannot be tested here -				styler.ColourTo(i - 1, state); -				state = SCE_LUA_COMMENTLINE; -			} else if (isLuaOperator(ch)) { -				styler.ColourTo(i - 1, state); -				styler.ColourTo(i, SCE_LUA_OPERATOR); -			} else if (isdigit(ch) || (ch == '.')) { -				styler.ColourTo(i - 1, state); -				state = SCE_LUA_NUMBER; -			} else if (iswordstart(ch)) { -				styler.ColourTo(i - 1, state); -				state = SCE_LUA_WORD; +		// Determine if the current state should terminate. +		if (sc.state == SCE_LUA_OPERATOR) { +			sc.SetState(SCE_LUA_DEFAULT); +		} else if (sc.state == SCE_LUA_NUMBER) { +			if (!IsAWordChar(sc.ch)) { +				sc.SetState(SCE_LUA_DEFAULT);  			} -		} else { -			if (state == SCE_LUA_NUMBER) { -				if (!iswordchar(ch)) { -					styler.ColourTo(i - 1, SCE_LUA_NUMBER); -					state = SCE_LUA_DEFAULT; -				} -			} else if (state == SCE_LUA_WORD) { -				if (!iswordchar(ch) || (ch == '.')) { -					char s[100]; -					unsigned int start = styler.GetStartSegment(); -					unsigned int end = i - 1; -	 -					for (unsigned int n = 0; n < end - start + 1 && n < 30; n++) { -						s[n] = styler[start + n]; -						s[n + 1] = '\0'; -					} -	 -					char chAttr = SCE_LUA_IDENTIFIER; -					if (keywords.InList(s)) { -						chAttr = SCE_LUA_WORD; -					} else if (keywords2.InList(s)) { -						chAttr = SCE_LUA_WORD2; -					} else if (keywords3.InList(s)) { -						chAttr = SCE_LUA_WORD3 ; -					} else if (keywords4.InList(s)) { -						chAttr = SCE_LUA_WORD4 ; -					} else if (keywords5.InList(s)) { -						chAttr = SCE_LUA_WORD5 ; -					} else if (keywords6.InList(s)) { -						chAttr = SCE_LUA_WORD6 ; -					} -					styler.ColourTo(end, chAttr); -					state = SCE_LUA_DEFAULT; -				} -			} else if (state == SCE_LUA_LITERALSTRING) { -				if (ch == '[' && chNext == '[') { -					literalString++; -				} else if (ch == ']' && (chPrev == ']') && (--literalString == 0)) { -					styler.ColourTo(i, state); -					state = SCE_LUA_DEFAULT; -				} -			} else if (state == SCE_LUA_PREPROCESSOR) { -				if ((ch == '\r' || ch == '\n') && (chPrev != '\\')) { -					styler.ColourTo(i - 1, state); -					state = SCE_LUA_DEFAULT; +		} else if (sc.state == SCE_LUA_IDENTIFIER) { +			if (!IsAWordChar(sc.ch) || (sc.ch == '.')) { +				char s[100]; +				sc.GetCurrent(s, sizeof(s)); +				if (keywords.InList(s)) { +					sc.ChangeState(SCE_LUA_WORD); +				} else if (keywords2.InList(s)) { +					sc.ChangeState(SCE_LUA_WORD2); +				} else if (keywords3.InList(s)) { +					sc.ChangeState(SCE_LUA_WORD3); +				} else if (keywords4.InList(s)) { +					sc.ChangeState(SCE_LUA_WORD4); +				} else if (keywords5.InList(s)) { +					sc.ChangeState(SCE_LUA_WORD5); +				} else if (keywords6.InList(s)) { +					sc.ChangeState(SCE_LUA_WORD6);  				} -			} else if (state == SCE_LUA_COMMENTLINE) { -				if (ch == '\r' || ch == '\n') { -					styler.ColourTo(i - 1, state); -					state = SCE_LUA_DEFAULT; -				} -			} else if (state == SCE_LUA_STRING) { -				if ((ch == '\r' || ch == '\n') && (chPrev != '\\')) { -					styler.ColourTo(i - 1, state); -					state = SCE_LUA_STRINGEOL; -				} else if (ch == '\\') { -					if (chNext == '\"' || chNext == '\\') { -						i++; -						ch = chNext; -						chNext = styler.SafeGetCharAt(i + 1); -					} -				} else if (ch == '\"') { -					styler.ColourTo(i, state); -					state = SCE_LUA_DEFAULT; -					i++; -					ch = chNext; -					chNext = styler.SafeGetCharAt(i + 1); -				} -			} else if (state == SCE_LUA_CHARACTER) { -				if ((ch == '\r' || ch == '\n') && (chPrev != '\\')) { -					styler.ColourTo(i - 1, state); -					state = SCE_LUA_STRINGEOL; -				} else if (ch == '\\') { -					if (chNext == '\'' || chNext == '\\') { -						i++; -						ch = chNext; -						chNext = styler.SafeGetCharAt(i + 1); -					} -				} else if (ch == '\'') { -					styler.ColourTo(i, state); -					state = SCE_LUA_DEFAULT; -					i++; -					ch = chNext; -					chNext = styler.SafeGetCharAt(i + 1); +				sc.SetState(SCE_LUA_DEFAULT); +			} +  + +		} else if (sc.state == SCE_LUA_COMMENTLINE ) { +			if (sc.atLineEnd) { +				sc.SetState(SCE_LUA_DEFAULT); +				visibleChars = 0; +			} +		} else if (sc.state == SCE_LUA_STRING) { +			if (sc.ch == '\\') { +				if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') { +					sc.Forward();  				} +			} else if (sc.ch == '\"') { +				sc.ForwardSetState(SCE_LUA_DEFAULT); +			} else if (sc.atLineEnd) { +				sc.ChangeState(SCE_LUA_STRINGEOL); +				sc.ForwardSetState(SCE_LUA_DEFAULT); +				visibleChars = 0;  			} -	 -			if (state == SCE_LUA_DEFAULT) { -				if (ch == '-' && chNext == '-') { -					state = SCE_LUA_COMMENTLINE; -				} else if (ch == '[' && chNext == '[') { -					literalString = 1; -					state = SCE_LUA_LITERALSTRING; -				} else if (ch == '\"') { -					state = SCE_LUA_STRING; -				} else if (ch == '\'') { -					state = SCE_LUA_CHARACTER; -				} else if (ch == '$' && firstChar) { -					state = SCE_LUA_PREPROCESSOR; -				} else if (isdigit(ch) || (ch == '.')) { -					state = SCE_LUA_NUMBER; -				} else if (iswordstart(ch)) { -					state = SCE_LUA_WORD; -				} else if (isLuaOperator(ch)) { -					styler.ColourTo(i, SCE_LUA_OPERATOR); +			 +		} else if (sc.state == SCE_LUA_CHARACTER) { +			if (sc.ch == '\\') { +				if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') { +					sc.Forward();  				} +			} else if (sc.ch == '\'') { +				sc.ForwardSetState(SCE_LUA_DEFAULT); +			} else if (sc.atLineEnd) { +				sc.ChangeState(SCE_LUA_STRINGEOL); +				sc.ForwardSetState(SCE_LUA_DEFAULT); +				visibleChars = 0; +			} +		} else if (sc.state == SCE_LUA_LITERALSTRING) { +			if (sc.chPrev == '[' && sc.ch == '[' && literalStringFlag != 1) 	{ +				literalString++; +				literalStringFlag = 1; +			} +			else if (sc.chPrev == ']' && sc.ch == ']' && literalStringFlag != 2 ) { +				if((--literalString == 1)) +					sc.ForwardSetState(SCE_LUA_DEFAULT); +				literalStringFlag = 2;  			} +			else literalStringFlag = 0; +		}		 +		// Determine if a new state should be entered. +		if (sc.state == SCE_LUA_DEFAULT) { +			if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) { +					sc.SetState(SCE_LUA_NUMBER); +			} else if (IsAWordStart(sc.ch) || (sc.ch == '@')) { +					sc.SetState(SCE_LUA_IDENTIFIER); +			} else if (sc.ch == '\"') { +				sc.SetState(SCE_LUA_STRING); +			} else if (sc.ch == '\'') { +				sc.SetState(SCE_LUA_CHARACTER); +			} else if (sc.ch == '[' && sc.chNext == '[') { +				sc.SetState(SCE_LUA_LITERALSTRING); +				literalString = 1; +			} else if (sc.ch == '-' && sc.chNext == '-') { +				sc.SetState(SCE_LUA_COMMENTLINE); +			}  else if (isLuaOperator(static_cast<char>(sc.ch))) { +				sc.SetState(SCE_LUA_OPERATOR); +			} +		} +		 +		if (sc.atLineEnd) { +			// Reset states to begining of colourise so no surprises  +			// if different sets of lines lexed. +			chPrevNonWhite = ' '; +			visibleChars = 0; +		} +		if (!IsASpace(sc.ch)) { +			chPrevNonWhite = sc.ch; +			visibleChars++;  		} -		chPrev = ch; -		firstChar = (ch == '\r' || ch == '\n');  	} -	styler.ColourTo(lengthDoc - 1, state); +	sc.Complete();  } @@ -252,21 +216,23 @@ static void FoldLuaDoc(unsigned int startPos, int length, int /* initStyle */, W  					s[j] = styler[i + j];  					s[j + 1] = '\0';  				} - +				  				if ((strcmp(s, "if") == 0) || (strcmp(s, "do") == 0) -				        || (strcmp(s, "function") == 0)) +					|| (strcmp(s, "function") == 0))  					levelCurrent++;  				if ((strcmp(s, "end") == 0) || (strcmp(s, "elseif") == 0))  					levelCurrent--; - +				  			} -		} else if (style == SCE_LUA_OPERATOR) { -			if (ch == '{' || ch == '(') +		} +		else if (style == SCE_LUA_OPERATOR) +		{ +			if(ch == '{' || ch == '(')  				levelCurrent++; -			else if (ch == '}' || ch == ')') +			else if(ch == '}' || ch == ')')  				levelCurrent--;  		} - +			  		if (atEOL) {  			int lev = levelPrev;  			if (visibleChars == 0 && foldCompact) | 
