| 1 |
// Scintilla source code edit control |
| 2 |
/** @file StyleContext.h |
| 3 |
** Lexer infrastructure. |
| 4 |
**/ |
| 5 |
// Copyright 1998-2004 by Neil Hodgson <[email protected]> |
| 6 |
// This file is in the public domain. |
| 7 |
|
| 8 |
#ifndef STYLECONTEXT_H |
| 9 |
#define STYLECONTEXT_H |
| 10 |
|
| 11 |
namespace Lexilla { |
| 12 |
|
| 13 |
// All languages handled so far can treat all characters >= 0x80 as one class |
| 14 |
// which just continues the current token or starts an identifier if in default. |
| 15 |
// DBCS treated specially as the second character can be < 0x80 and hence |
| 16 |
// syntactically significant. UTF-8 avoids this as all trail bytes are >= 0x80 |
| 17 |
class StyleContext { |
| 18 |
LexAccessor &styler; |
| 19 |
Scintilla::IDocument * const multiByteAccess; |
| 20 |
const Sci_PositionU lengthDocument; |
| 21 |
const Sci_PositionU endPos; |
| 22 |
const Sci_Position lineDocEnd; |
| 23 |
|
| 24 |
// Used for optimizing GetRelativeCharacter |
| 25 |
Sci_PositionU posRelative = 0; |
| 26 |
Sci_PositionU currentPosLastRelative; |
| 27 |
Sci_Position offsetRelative = 0; |
| 28 |
|
| 29 |
void GetNextChar() { |
| 30 |
if (multiByteAccess) { |
| 31 |
chNext = multiByteAccess->GetCharacterAndWidth(currentPos+width, &widthNext); |
| 32 |
} else { |
| 33 |
const unsigned char charNext = styler.SafeGetCharAt(currentPos + width, 0); |
| 34 |
chNext = charNext; |
| 35 |
} |
| 36 |
// End of line determined from line end position, allowing CR, LF, |
| 37 |
// CRLF and Unicode line ends as set by document. |
| 38 |
const Sci_Position currentPosSigned = currentPos; |
| 39 |
if (currentLine < lineDocEnd) |
| 40 |
atLineEnd = currentPosSigned >= (lineStartNext-1); |
| 41 |
else // Last line |
| 42 |
atLineEnd = currentPosSigned >= lineStartNext; |
| 43 |
} |
| 44 |
|
| 45 |
public: |
| 46 |
Sci_PositionU currentPos; |
| 47 |
Sci_Position currentLine; |
| 48 |
Sci_Position lineEnd; |
| 49 |
Sci_Position lineStartNext; |
| 50 |
bool atLineStart; |
| 51 |
bool atLineEnd = false; |
| 52 |
int state; |
| 53 |
int chPrev = 0; |
| 54 |
int ch = 0; |
| 55 |
Sci_Position width = 0; |
| 56 |
int chNext = 0; |
| 57 |
Sci_Position widthNext = 1; |
| 58 |
|
| 59 |
StyleContext(Sci_PositionU startPos, Sci_PositionU length, |
| 60 |
int initStyle, LexAccessor &styler_, char chMask = '\377'); |
| 61 |
// Deleted so StyleContext objects can not be copied. |
| 62 |
StyleContext(const StyleContext &) = delete; |
| 63 |
StyleContext &operator=(const StyleContext &) = delete; |
| 64 |
void Complete() { |
| 65 |
styler.ColourTo(currentPos - ((currentPos > lengthDocument) ? 2 : 1), state); |
| 66 |
styler.Flush(); |
| 67 |
} |
| 68 |
bool More() const noexcept { |
| 69 |
return currentPos < endPos; |
| 70 |
} |
| 71 |
void Forward() { |
| 72 |
if (currentPos < endPos) { |
| 73 |
atLineStart = atLineEnd; |
| 74 |
if (atLineStart) { |
| 75 |
currentLine++; |
| 76 |
lineEnd = styler.LineEnd(currentLine); |
| 77 |
lineStartNext = styler.LineStart(currentLine+1); |
| 78 |
} |
| 79 |
chPrev = ch; |
| 80 |
currentPos += width; |
| 81 |
ch = chNext; |
| 82 |
width = widthNext; |
| 83 |
GetNextChar(); |
| 84 |
} else { |
| 85 |
atLineStart = false; |
| 86 |
chPrev = ' '; |
| 87 |
ch = ' '; |
| 88 |
chNext = ' '; |
| 89 |
atLineEnd = true; |
| 90 |
} |
| 91 |
} |
| 92 |
void Forward(Sci_Position nb) { |
| 93 |
for (Sci_Position i = 0; i < nb; i++) { |
| 94 |
Forward(); |
| 95 |
} |
| 96 |
} |
| 97 |
void ForwardBytes(Sci_Position nb) { |
| 98 |
const Sci_PositionU forwardPos = currentPos + nb; |
| 99 |
while (forwardPos > currentPos) { |
| 100 |
const Sci_PositionU currentPosStart = currentPos; |
| 101 |
Forward(); |
| 102 |
if (currentPos == currentPosStart) { |
| 103 |
// Reached end |
| 104 |
return; |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
void ChangeState(int state_) noexcept { |
| 109 |
state = state_; |
| 110 |
} |
| 111 |
void SetState(int state_) { |
| 112 |
styler.ColourTo(currentPos - ((currentPos > lengthDocument) ? 2 : 1), state); |
| 113 |
state = state_; |
| 114 |
} |
| 115 |
void ForwardSetState(int state_) { |
| 116 |
Forward(); |
| 117 |
styler.ColourTo(currentPos - ((currentPos > lengthDocument) ? 2 : 1), state); |
| 118 |
state = state_; |
| 119 |
} |
| 120 |
Sci_Position LengthCurrent() const noexcept { |
| 121 |
return currentPos - styler.GetStartSegment(); |
| 122 |
} |
| 123 |
char GetRelativeChar(Sci_Position n, char chDefault='\0') { |
| 124 |
return styler.SafeGetCharAt(currentPos + n, chDefault); |
| 125 |
} |
| 126 |
int GetRelative(Sci_Position n, char chDefault='\0') { |
| 127 |
const unsigned char chRelative = styler.SafeGetCharAt(currentPos + n, chDefault); |
| 128 |
return chRelative; |
| 129 |
} |
| 130 |
int GetRelativeCharacter(Sci_Position n) { |
| 131 |
if (n == 0) |
| 132 |
return ch; |
| 133 |
if (multiByteAccess) { |
| 134 |
if ((currentPosLastRelative != currentPos) || |
| 135 |
((n > 0) && ((offsetRelative < 0) || (n < offsetRelative))) || |
| 136 |
((n < 0) && ((offsetRelative > 0) || (n > offsetRelative)))) { |
| 137 |
posRelative = currentPos; |
| 138 |
offsetRelative = 0; |
| 139 |
} |
| 140 |
const Sci_Position diffRelative = n - offsetRelative; |
| 141 |
const Sci_Position posNew = multiByteAccess->GetRelativePosition(posRelative, diffRelative); |
| 142 |
const int chReturn = multiByteAccess->GetCharacterAndWidth(posNew, nullptr); |
| 143 |
posRelative = posNew; |
| 144 |
currentPosLastRelative = currentPos; |
| 145 |
offsetRelative = n; |
| 146 |
return chReturn; |
| 147 |
} else { |
| 148 |
// fast version for single byte encodings |
| 149 |
const unsigned char chRelative = styler.SafeGetCharAt(currentPos + n, 0); |
| 150 |
return chRelative; |
| 151 |
} |
| 152 |
} |
| 153 |
bool MatchLineEnd() const noexcept { |
| 154 |
const Sci_Position currentPosSigned = currentPos; |
| 155 |
return currentPosSigned == lineEnd; |
| 156 |
} |
| 157 |
bool Match(char ch0) const noexcept { |
| 158 |
const unsigned char uch0 = ch0; |
| 159 |
return ch == uch0; |
| 160 |
} |
| 161 |
bool Match(char ch0, char ch1) const noexcept { |
| 162 |
const unsigned char uch0 = ch0; |
| 163 |
const unsigned char uch1 = ch1; |
| 164 |
return (ch == uch0) && (chNext == uch1); |
| 165 |
} |
| 166 |
bool Match(const char *s) { |
| 167 |
const unsigned char su = *s; |
| 168 |
if (ch != su) |
| 169 |
return false; |
| 170 |
s++; |
| 171 |
if (!*s) |
| 172 |
return true; |
| 173 |
const unsigned char sNext = *s; |
| 174 |
if (chNext != sNext) |
| 175 |
return false; |
| 176 |
s++; |
| 177 |
for (int n=2; *s; n++) { |
| 178 |
if (*s != styler.SafeGetCharAt(currentPos+n, 0)) |
| 179 |
return false; |
| 180 |
s++; |
| 181 |
} |
| 182 |
return true; |
| 183 |
} |
| 184 |
// Non-inline |
| 185 |
bool MatchIgnoreCase(const char *s); |
| 186 |
void GetCurrent(char *s, Sci_PositionU len) const; |
| 187 |
void GetCurrentLowered(char *s, Sci_PositionU len) const; |
| 188 |
enum class Transform { none, lower }; |
| 189 |
void GetCurrentString(std::string &string, Transform transform) const; |
| 190 |
}; |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
#endif |