diff options
| author | nyamatongwe <devnull@localhost> | 2000-11-12 06:41:10 +0000 | 
|---|---|---|
| committer | nyamatongwe <devnull@localhost> | 2000-11-12 06:41:10 +0000 | 
| commit | 6c1c135528a06da436d422ab6433ad1806c6334d (patch) | |
| tree | 0824c6aa9afa8f29af8c0ee8da075a41b6db6ceb /src/LexPython.cxx | |
| parent | 51dda77e8ef97324fd8f740fc2224e5a322d74d8 (diff) | |
| download | scintilla-mirror-6c1c135528a06da436d422ab6433ad1806c6334d.tar.gz | |
John's patch to handle Unicode u", raw r", and Unicode raw ur" strings.
Diffstat (limited to 'src/LexPython.cxx')
| -rw-r--r-- | src/LexPython.cxx | 134 | 
1 files changed, 100 insertions, 34 deletions
| diff --git a/src/LexPython.cxx b/src/LexPython.cxx index 492d21c7d..86ee5f9ef 100644 --- a/src/LexPython.cxx +++ b/src/LexPython.cxx @@ -17,6 +17,32 @@  #include "Scintilla.h"  #include "SciLexer.h" +/* Returns true if the "as" word that begins at start follows an import statement */ +static bool IsImportAs(unsigned int start, Accessor &styler) { +	unsigned int i; +	unsigned int j; +	char s[10]; + +	/* Find any import before start but after any statement terminator or quote */ +	i = start; +	while (i > 0) { +		char ch = styler[i - 1]; + +		if (ch == '\n' || ch == '\r' || ch == ';' || ch == '\'' || ch == '"' || ch == '`') +			break; +		if (ch == 't' && i > 5) { +			for (j = 0; j < 6; j++) +				s[j] = styler[(i - 6) + j]; +			s[j] = '\0'; +			if (strcmp(s, "import") == 0) +				return true; +		} +		i--; +	} + +        return false; +} +  static void ClassifyWordPy(unsigned int start, unsigned int end, WordList &keywords, Accessor &styler, char *prevWord) {  	char s[100];  	bool wordIsNumber = isdigit(styler[start]); @@ -33,6 +59,8 @@ static void ClassifyWordPy(unsigned int start, unsigned int end, WordList &keywo  		chAttr = SCE_P_NUMBER;  	else if (keywords.InList(s))  		chAttr = SCE_P_WORD; +	else if (strcmp(s, "as") == 0 && IsImportAs(start, styler)) +		chAttr = SCE_P_WORD;  	// make sure that dot-qualifiers inside the word are lexed correct  	else for (unsigned int i = 0; i < end - start + 1; i++) {  		if (styler[start + i] == '.') { @@ -48,6 +76,67 @@ static bool IsPyComment(Accessor &styler, int pos, int len) {  	return len>0 && styler[pos]=='#';  } +static bool IsPyStringStart(char ch, char chNext, char chNext2) { +	if (ch == '\'' || ch == '"') +		return true; +	if (ch == 'u' || ch == 'U') { +		if (chNext == '"' || chNext == '\'') +			return true; +		if ((chNext == 'r' || chNext == 'R') && (chNext2 == '"' || chNext2 == '\'')) +			return true; +	} +	if ((ch == 'r' || ch == 'R') && (chNext == '"' || chNext == '\'')) +		return true; + +	return false; +} + +static bool IsPyWordStart(char ch, char chNext, char chNext2) { +	return (iswordchar(ch) && !IsPyStringStart(ch, chNext, chNext2)); +} + +/* Return the state to use for the string starting at i; *nextIndex will be set to the first index following the quote(s) */ +static int GetPyStringState(Accessor &styler, int i, int *nextIndex) { +	char ch = styler.SafeGetCharAt(i); +	char chNext = styler.SafeGetCharAt(i + 1); + +	// Advance beyond r, u, or ur prefix, but bail if there are any unexpected chars +	if (ch == 'r' || ch == 'R') { +		i++; +		ch = styler.SafeGetCharAt(i); +		chNext = styler.SafeGetCharAt(i + 1); +	} +	else if (ch == 'u' || ch == 'U') { +		if (chNext == 'r' || chNext == 'R') +			i += 2; +		else +			i += 1; +		ch = styler.SafeGetCharAt(i); +		chNext = styler.SafeGetCharAt(i + 1); +	} + +	if (ch != '"' && ch != '\'') { +		*nextIndex = i + 1; +		return SCE_P_DEFAULT; +	} + +	if (ch == chNext && ch == styler.SafeGetCharAt(i + 2)) { +		*nextIndex = i + 3; + +		if (ch == '"') +			return SCE_P_TRIPLEDOUBLE; +		else +			return SCE_P_TRIPLE; +	} else { +		*nextIndex = i + 1; + +		if (ch == '"') +			return SCE_P_STRING; +		else +			return SCE_P_CHARACTER; +	} +} +  static void ColourisePyDoc(unsigned int startPos, int length, int initStyle,   						   WordList *keywordlists[], Accessor &styler) { @@ -85,6 +174,7 @@ static void ColourisePyDoc(unsigned int startPos, int length, int initStyle,  	if ((state == SCE_P_TRIPLE) || (state == SCE_P_TRIPLEDOUBLE))   		indentCurrent |= SC_FOLDLEVELWHITEFLAG; +	int nextIndex = 0;  	char chPrev = ' ';  	char chPrev2 = ' ';  	char chNext = styler[startPos]; @@ -160,33 +250,20 @@ static void ColourisePyDoc(unsigned int startPos, int length, int initStyle,  			}  		}  		if (state == SCE_P_DEFAULT) { -			if (iswordstart(ch)) { +			if (IsPyWordStart(ch, chNext, chNext2)) {  				styler.ColourTo(i - 1, state);  				state = SCE_P_WORD;  			} else if (ch == '#') {  				styler.ColourTo(i - 1, state);  				state = chNext == '#' ? SCE_P_COMMENTBLOCK : SCE_P_COMMENTLINE; -			} else if (ch == '\"') { -				styler.ColourTo(i - 1, state); -				if (chNext == '\"' && chNext2 == '\"') { -					i += 2; -					state = SCE_P_TRIPLEDOUBLE; -					ch = ' '; -					chPrev = ' '; -					chNext = styler.SafeGetCharAt(i + 1); -				} else { -					state = SCE_P_STRING; -				} -			} else if (ch == '\'') { +			} else if (IsPyStringStart(ch, chNext, chNext2)) {  				styler.ColourTo(i - 1, state); -				if (chNext == '\'' && chNext2 == '\'') { -					i += 2; -					state = SCE_P_TRIPLE; +				state = GetPyStringState(styler, i, &nextIndex); +				if (nextIndex != i + 1) { +					i = nextIndex - 1;  					ch = ' ';  					chPrev = ' ';  					chNext = styler.SafeGetCharAt(i + 1); -				} else { -					state = SCE_P_CHARACTER;  				}  			} else if (isoperator(ch)) {  				styler.ColourTo(i - 1, state); @@ -198,25 +275,14 @@ static void ColourisePyDoc(unsigned int startPos, int length, int initStyle,  				state = SCE_P_DEFAULT;  				if (ch == '#') {  					state = chNext == '#' ? SCE_P_COMMENTBLOCK : SCE_P_COMMENTLINE; -				} else if (ch == '\"') { -					if (chNext == '\"' && chNext2 == '\"') { -						i += 2; -						state = SCE_P_TRIPLEDOUBLE; -						ch = ' '; -						chPrev = ' '; -						chNext = styler.SafeGetCharAt(i + 1); -					} else { -						state = SCE_P_STRING; -					} -				} else if (ch == '\'') { -					if (chNext == '\'' && chNext2 == '\'') { -						i += 2; -						state = SCE_P_TRIPLE; +				} else if (IsPyStringStart(ch, chNext, chNext2)) { +					styler.ColourTo(i - 1, state); +					state = GetPyStringState(styler, i, &nextIndex); +					if (nextIndex != i + 1) { +						i = nextIndex - 1;  						ch = ' ';  						chPrev = ' ';  						chNext = styler.SafeGetCharAt(i + 1); -					} else { -						state = SCE_P_CHARACTER;  					}  				} else if (isoperator(ch)) {  					styler.ColourTo(i, SCE_P_OPERATOR); | 
