2005-11-22 12:26:26 +00:00
|
|
|
// Scintilla source code edit control
|
|
|
|
/** @file StyleContext.cxx
|
|
|
|
** Lexer infrastructure.
|
|
|
|
**/
|
|
|
|
// Copyright 1998-2004 by Neil Hodgson <neilh@scintilla.org>
|
|
|
|
// This file is in the public domain.
|
|
|
|
|
2010-11-24 21:23:05 +00:00
|
|
|
#ifndef STYLECONTEXT_H
|
|
|
|
#define STYLECONTEXT_H
|
|
|
|
|
2007-06-18 13:02:34 +00:00
|
|
|
#ifdef SCI_NAMESPACE
|
|
|
|
namespace Scintilla {
|
|
|
|
#endif
|
|
|
|
|
2010-11-24 21:23:05 +00:00
|
|
|
static inline int MakeLowerCase(int ch) {
|
|
|
|
if (ch < 'A' || ch > 'Z')
|
|
|
|
return ch;
|
|
|
|
else
|
|
|
|
return ch - 'A' + 'a';
|
|
|
|
}
|
|
|
|
|
2013-04-27 16:09:29 +02:00
|
|
|
inline int UnicodeCodePoint(const unsigned char *us) {
|
|
|
|
if (us[0] < 0xC2) {
|
|
|
|
return us[0];
|
|
|
|
} else if (us[0] < 0xE0) {
|
|
|
|
return ((us[0] & 0x1F) << 6) + (us[1] & 0x3F);
|
|
|
|
} else if (us[0] < 0xF0) {
|
|
|
|
return ((us[0] & 0xF) << 12) + ((us[1] & 0x3F) << 6) + (us[2] & 0x3F);
|
|
|
|
} else if (us[0] < 0xF5) {
|
|
|
|
return ((us[0] & 0x7) << 18) + ((us[1] & 0x3F) << 12) + ((us[2] & 0x3F) << 6) + (us[3] & 0x3F);
|
|
|
|
}
|
|
|
|
return us[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int BytesInUnicodeCodePoint(int codePoint) {
|
|
|
|
if (codePoint < 0x80)
|
|
|
|
return 1;
|
|
|
|
else if (codePoint < 0x800)
|
|
|
|
return 2;
|
|
|
|
else if (codePoint < 0x10000)
|
|
|
|
return 3;
|
|
|
|
else
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
// All languages handled so far can treat all characters >= 0x80 as one class
|
|
|
|
// which just continues the current token or starts an identifier if in default.
|
|
|
|
// DBCS treated specially as the second character can be < 0x80 and hence
|
|
|
|
// syntactically significant. UTF-8 avoids this as all trail bytes are >= 0x80
|
|
|
|
class StyleContext {
|
2010-11-24 21:23:05 +00:00
|
|
|
LexAccessor &styler;
|
2005-11-22 12:26:26 +00:00
|
|
|
unsigned int endPos;
|
2013-04-27 16:09:29 +02:00
|
|
|
unsigned int lengthDocument;
|
2010-05-16 17:44:24 +00:00
|
|
|
StyleContext &operator=(const StyleContext &);
|
2013-04-27 16:09:29 +02:00
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
void GetNextChar(unsigned int pos) {
|
2013-04-27 16:09:29 +02:00
|
|
|
chNext = static_cast<unsigned char>(styler.SafeGetCharAt(pos+1, 0));
|
|
|
|
if (styler.Encoding() == encUnicode) {
|
|
|
|
if (chNext >= 0x80) {
|
|
|
|
unsigned char bytes[4] = { static_cast<unsigned char>(chNext), 0, 0, 0 };
|
|
|
|
for (int trail=1; trail<3; trail++) {
|
|
|
|
bytes[trail] = static_cast<unsigned char>(styler.SafeGetCharAt(pos+1+trail, 0));
|
|
|
|
if (!((bytes[trail] >= 0x80) && (bytes[trail] < 0xc0))) {
|
|
|
|
bytes[trail] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chNext = UnicodeCodePoint(bytes);
|
|
|
|
}
|
|
|
|
} else if (styler.Encoding() == encDBCS) {
|
|
|
|
if (styler.IsLeadByte(static_cast<char>(chNext))) {
|
|
|
|
chNext = chNext << 8;
|
|
|
|
chNext |= static_cast<unsigned char>(styler.SafeGetCharAt(pos+2, 0));
|
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
// End of line?
|
|
|
|
// Trigger on CR only (Mac style) or either on LF from CR+LF (Dos/Win)
|
|
|
|
// or on LF alone (Unix). Avoid triggering two times on Dos/Win.
|
2013-05-22 03:23:57 +02:00
|
|
|
if (currentLine < lineDocEnd)
|
2013-04-27 16:09:29 +02:00
|
|
|
atLineEnd = static_cast<int>(pos) >= (lineStartNext-1);
|
|
|
|
else // Last line
|
|
|
|
atLineEnd = static_cast<int>(pos) >= lineStartNext;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
unsigned int currentPos;
|
2013-04-27 16:09:29 +02:00
|
|
|
int currentLine;
|
2013-05-22 03:23:57 +02:00
|
|
|
int lineDocEnd;
|
2013-04-27 16:09:29 +02:00
|
|
|
int lineStartNext;
|
2005-11-22 12:26:26 +00:00
|
|
|
bool atLineStart;
|
|
|
|
bool atLineEnd;
|
|
|
|
int state;
|
|
|
|
int chPrev;
|
|
|
|
int ch;
|
|
|
|
int chNext;
|
|
|
|
|
|
|
|
StyleContext(unsigned int startPos, unsigned int length,
|
2010-11-24 21:23:05 +00:00
|
|
|
int initStyle, LexAccessor &styler_, char chMask=31) :
|
2005-11-22 12:26:26 +00:00
|
|
|
styler(styler_),
|
|
|
|
endPos(startPos + length),
|
|
|
|
currentPos(startPos),
|
2013-04-27 16:09:29 +02:00
|
|
|
currentLine(-1),
|
|
|
|
lineStartNext(-1),
|
2005-11-22 12:26:26 +00:00
|
|
|
atLineEnd(false),
|
|
|
|
state(initStyle & chMask), // Mask off all bits which aren't in the chMask.
|
|
|
|
chPrev(0),
|
|
|
|
ch(0),
|
|
|
|
chNext(0) {
|
|
|
|
styler.StartAt(startPos, chMask);
|
|
|
|
styler.StartSegment(startPos);
|
2013-04-27 16:09:29 +02:00
|
|
|
currentLine = styler.GetLine(startPos);
|
|
|
|
lineStartNext = styler.LineStart(currentLine+1);
|
|
|
|
lengthDocument = static_cast<unsigned int>(styler.Length());
|
|
|
|
if (endPos == lengthDocument)
|
|
|
|
endPos++;
|
2013-05-22 03:23:57 +02:00
|
|
|
lineDocEnd = styler.GetLine(lengthDocument);
|
2013-04-27 16:09:29 +02:00
|
|
|
atLineStart = static_cast<unsigned int>(styler.LineStart(currentLine)) == startPos;
|
2005-11-22 12:26:26 +00:00
|
|
|
unsigned int pos = currentPos;
|
2013-04-27 16:09:29 +02:00
|
|
|
ch = static_cast<unsigned char>(styler.SafeGetCharAt(pos, 0));
|
|
|
|
if (styler.Encoding() == encUnicode) {
|
|
|
|
// Get the current char
|
|
|
|
GetNextChar(pos-1);
|
|
|
|
ch = chNext;
|
|
|
|
pos += BytesInUnicodeCodePoint(ch) - 1;
|
|
|
|
} else if (styler.Encoding() == encDBCS) {
|
|
|
|
if (styler.IsLeadByte(static_cast<char>(ch))) {
|
|
|
|
pos++;
|
|
|
|
ch = ch << 8;
|
|
|
|
ch |= static_cast<unsigned char>(styler.SafeGetCharAt(pos, 0));
|
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
GetNextChar(pos);
|
|
|
|
}
|
|
|
|
void Complete() {
|
2013-04-27 16:09:29 +02:00
|
|
|
styler.ColourTo(currentPos - ((currentPos > lengthDocument) ? 2 : 1), state);
|
2010-11-24 21:23:05 +00:00
|
|
|
styler.Flush();
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2010-05-16 17:44:24 +00:00
|
|
|
bool More() const {
|
2005-11-22 12:26:26 +00:00
|
|
|
return currentPos < endPos;
|
|
|
|
}
|
|
|
|
void Forward() {
|
|
|
|
if (currentPos < endPos) {
|
|
|
|
atLineStart = atLineEnd;
|
2013-04-27 16:09:29 +02:00
|
|
|
if (atLineStart) {
|
|
|
|
currentLine++;
|
|
|
|
lineStartNext = styler.LineStart(currentLine+1);
|
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
chPrev = ch;
|
2013-04-27 16:09:29 +02:00
|
|
|
if (styler.Encoding() == encUnicode) {
|
|
|
|
currentPos += BytesInUnicodeCodePoint(ch);
|
|
|
|
} else if (styler.Encoding() == encDBCS) {
|
|
|
|
currentPos++;
|
|
|
|
if (ch >= 0x100)
|
|
|
|
currentPos++;
|
|
|
|
} else {
|
2005-11-22 12:26:26 +00:00
|
|
|
currentPos++;
|
2013-04-27 16:09:29 +02:00
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
ch = chNext;
|
2013-04-27 16:09:29 +02:00
|
|
|
if (styler.Encoding() == encUnicode) {
|
|
|
|
GetNextChar(currentPos + BytesInUnicodeCodePoint(ch)-1);
|
|
|
|
} else if (styler.Encoding() == encDBCS) {
|
|
|
|
GetNextChar(currentPos + ((ch >= 0x100) ? 1 : 0));
|
|
|
|
} else {
|
|
|
|
GetNextChar(currentPos);
|
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
} else {
|
|
|
|
atLineStart = false;
|
|
|
|
chPrev = ' ';
|
|
|
|
ch = ' ';
|
|
|
|
chNext = ' ';
|
|
|
|
atLineEnd = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Forward(int nb) {
|
|
|
|
for (int i = 0; i < nb; i++) {
|
|
|
|
Forward();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void ChangeState(int state_) {
|
|
|
|
state = state_;
|
|
|
|
}
|
|
|
|
void SetState(int state_) {
|
2013-04-27 16:09:29 +02:00
|
|
|
styler.ColourTo(currentPos - ((currentPos > lengthDocument) ? 2 : 1), state);
|
2005-11-22 12:26:26 +00:00
|
|
|
state = state_;
|
|
|
|
}
|
|
|
|
void ForwardSetState(int state_) {
|
|
|
|
Forward();
|
2013-04-27 16:09:29 +02:00
|
|
|
styler.ColourTo(currentPos - ((currentPos > lengthDocument) ? 2 : 1), state);
|
2005-11-22 12:26:26 +00:00
|
|
|
state = state_;
|
|
|
|
}
|
|
|
|
int LengthCurrent() {
|
|
|
|
return currentPos - styler.GetStartSegment();
|
|
|
|
}
|
|
|
|
int GetRelative(int n) {
|
2013-04-27 16:09:29 +02:00
|
|
|
return static_cast<unsigned char>(styler.SafeGetCharAt(currentPos+n, 0));
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2010-05-16 17:44:24 +00:00
|
|
|
bool Match(char ch0) const {
|
2006-08-21 17:21:50 +00:00
|
|
|
return ch == static_cast<unsigned char>(ch0);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2010-05-16 17:44:24 +00:00
|
|
|
bool Match(char ch0, char ch1) const {
|
2006-08-21 17:21:50 +00:00
|
|
|
return (ch == static_cast<unsigned char>(ch0)) && (chNext == static_cast<unsigned char>(ch1));
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
bool Match(const char *s) {
|
2006-08-21 17:21:50 +00:00
|
|
|
if (ch != static_cast<unsigned char>(*s))
|
2005-11-22 12:26:26 +00:00
|
|
|
return false;
|
|
|
|
s++;
|
2007-06-18 13:02:34 +00:00
|
|
|
if (!*s)
|
|
|
|
return true;
|
2006-08-21 17:21:50 +00:00
|
|
|
if (chNext != static_cast<unsigned char>(*s))
|
2005-11-22 12:26:26 +00:00
|
|
|
return false;
|
|
|
|
s++;
|
|
|
|
for (int n=2; *s; n++) {
|
2013-04-27 16:09:29 +02:00
|
|
|
if (*s != styler.SafeGetCharAt(currentPos+n, 0))
|
2005-11-22 12:26:26 +00:00
|
|
|
return false;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool MatchIgnoreCase(const char *s) {
|
2010-11-24 21:23:05 +00:00
|
|
|
if (MakeLowerCase(ch) != static_cast<unsigned char>(*s))
|
2005-11-22 12:26:26 +00:00
|
|
|
return false;
|
|
|
|
s++;
|
2010-11-24 21:23:05 +00:00
|
|
|
if (MakeLowerCase(chNext) != static_cast<unsigned char>(*s))
|
2005-11-22 12:26:26 +00:00
|
|
|
return false;
|
|
|
|
s++;
|
|
|
|
for (int n=2; *s; n++) {
|
2006-08-21 17:21:50 +00:00
|
|
|
if (static_cast<unsigned char>(*s) !=
|
2013-04-27 16:09:29 +02:00
|
|
|
MakeLowerCase(static_cast<unsigned char>(styler.SafeGetCharAt(currentPos+n, 0))))
|
2005-11-22 12:26:26 +00:00
|
|
|
return false;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Non-inline
|
|
|
|
void GetCurrent(char *s, unsigned int len);
|
|
|
|
void GetCurrentLowered(char *s, unsigned int len);
|
|
|
|
};
|
|
|
|
|
2007-06-18 13:02:34 +00:00
|
|
|
#ifdef SCI_NAMESPACE
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-11-24 21:23:05 +00:00
|
|
|
#endif
|