| 1 |
// Scintilla source code edit control |
| 2 |
/** @file LexAccessor.h |
| 3 |
** Interfaces between Scintilla and lexers. |
| 4 |
**/ |
| 5 |
// Copyright 1998-2010 by Neil Hodgson <[email protected]> |
| 6 |
// The License.txt file describes the conditions under which this software may be distributed. |
| 7 |
|
| 8 |
#ifndef LEXACCESSOR_H |
| 9 |
#define LEXACCESSOR_H |
| 10 |
|
| 11 |
namespace Lexilla { |
| 12 |
|
| 13 |
enum class EncodingType { eightBit, unicode, dbcs }; |
| 14 |
|
| 15 |
class LexAccessor { |
| 16 |
private: |
| 17 |
Scintilla::IDocument *pAccess; |
| 18 |
enum {extremePosition=0x7FFFFFFF}; |
| 19 |
/** @a bufferSize is a trade off between time taken to copy the characters |
| 20 |
* and retrieval overhead. |
| 21 |
* @a slopSize positions the buffer before the desired position |
| 22 |
* in case there is some backtracking. */ |
| 23 |
enum {bufferSize=4000, slopSize=bufferSize/8}; |
| 24 |
char buf[bufferSize+1]; |
| 25 |
Sci_Position startPos; |
| 26 |
Sci_Position endPos; |
| 27 |
int codePage; |
| 28 |
enum EncodingType encodingType; |
| 29 |
Sci_Position lenDoc; |
| 30 |
char styleBuf[bufferSize]; |
| 31 |
Sci_Position validLen; |
| 32 |
Sci_PositionU startSeg; |
| 33 |
Sci_Position startPosStyling; |
| 34 |
int documentVersion; |
| 35 |
|
| 36 |
void Fill(Sci_Position position) { |
| 37 |
startPos = position - slopSize; |
| 38 |
if (startPos + bufferSize > lenDoc) |
| 39 |
startPos = lenDoc - bufferSize; |
| 40 |
if (startPos < 0) |
| 41 |
startPos = 0; |
| 42 |
endPos = startPos + bufferSize; |
| 43 |
if (endPos > lenDoc) |
| 44 |
endPos = lenDoc; |
| 45 |
|
| 46 |
pAccess->GetCharRange(buf, startPos, endPos-startPos); |
| 47 |
buf[endPos-startPos] = '\0'; |
| 48 |
} |
| 49 |
|
| 50 |
public: |
| 51 |
explicit LexAccessor(Scintilla::IDocument *pAccess_) : |
| 52 |
pAccess(pAccess_), startPos(extremePosition), endPos(0), |
| 53 |
codePage(pAccess->CodePage()), |
| 54 |
encodingType(EncodingType::eightBit), |
| 55 |
lenDoc(pAccess->Length()), |
| 56 |
validLen(0), |
| 57 |
startSeg(0), startPosStyling(0), |
| 58 |
documentVersion(pAccess->Version()) { |
| 59 |
// Prevent warnings by static analyzers about uninitialized buf and styleBuf. |
| 60 |
buf[0] = 0; |
| 61 |
styleBuf[0] = 0; |
| 62 |
switch (codePage) { |
| 63 |
case 65001: |
| 64 |
encodingType = EncodingType::unicode; |
| 65 |
break; |
| 66 |
case 932: |
| 67 |
case 936: |
| 68 |
case 949: |
| 69 |
case 950: |
| 70 |
case 1361: |
| 71 |
encodingType = EncodingType::dbcs; |
| 72 |
break; |
| 73 |
default: |
| 74 |
break; |
| 75 |
} |
| 76 |
} |
| 77 |
char operator[](Sci_Position position) { |
| 78 |
if (position < startPos || position >= endPos) { |
| 79 |
Fill(position); |
| 80 |
} |
| 81 |
return buf[position - startPos]; |
| 82 |
} |
| 83 |
Scintilla::IDocument *MultiByteAccess() const noexcept { |
| 84 |
return pAccess; |
| 85 |
} |
| 86 |
/** Safe version of operator[], returning a defined value for invalid position. */ |
| 87 |
char SafeGetCharAt(Sci_Position position, char chDefault=' ') { |
| 88 |
if (position < startPos || position >= endPos) { |
| 89 |
Fill(position); |
| 90 |
if (position < startPos || position >= endPos) { |
| 91 |
// Position is outside range of document |
| 92 |
return chDefault; |
| 93 |
} |
| 94 |
} |
| 95 |
return buf[position - startPos]; |
| 96 |
} |
| 97 |
bool IsLeadByte(char ch) const { |
| 98 |
const unsigned char uch = ch; |
| 99 |
return |
| 100 |
(uch >= 0x80) && // non-ASCII |
| 101 |
(encodingType == EncodingType::dbcs) && // IsDBCSLeadByte only for DBCS |
| 102 |
pAccess->IsDBCSLeadByte(ch); |
| 103 |
} |
| 104 |
EncodingType Encoding() const noexcept { |
| 105 |
return encodingType; |
| 106 |
} |
| 107 |
bool Match(Sci_Position pos, const char *s) { |
| 108 |
assert(s); |
| 109 |
for (int i=0; *s; i++) { |
| 110 |
if (*s != SafeGetCharAt(pos+i)) |
| 111 |
return false; |
| 112 |
s++; |
| 113 |
} |
| 114 |
return true; |
| 115 |
} |
| 116 |
bool MatchIgnoreCase(Sci_Position pos, const char *s); |
| 117 |
|
| 118 |
// Get first len - 1 characters in range [startPos_, endPos_). |
| 119 |
void GetRange(Sci_PositionU startPos_, Sci_PositionU endPos_, char *s, Sci_PositionU len) const; |
| 120 |
void GetRangeLowered(Sci_PositionU startPos_, Sci_PositionU endPos_, char *s, Sci_PositionU len) const; |
| 121 |
// Get all characters in range [startPos_, endPos_). |
| 122 |
std::string GetRange(Sci_PositionU startPos_, Sci_PositionU endPos_) const; |
| 123 |
std::string GetRangeLowered(Sci_PositionU startPos_, Sci_PositionU endPos_) const; |
| 124 |
|
| 125 |
char StyleAt(Sci_Position position) const { |
| 126 |
return pAccess->StyleAt(position); |
| 127 |
} |
| 128 |
int StyleIndexAt(Sci_Position position) const { |
| 129 |
const unsigned char style = pAccess->StyleAt(position); |
| 130 |
return style; |
| 131 |
} |
| 132 |
// Return style value from buffer when in buffer, else retrieve from document. |
| 133 |
// This is faster and can avoid calls to Flush() as that may be expensive. |
| 134 |
int BufferStyleAt(Sci_Position position) const { |
| 135 |
const Sci_Position index = position - startPosStyling; |
| 136 |
if (index >= 0 && index < validLen) { |
| 137 |
const unsigned char style = styleBuf[index]; |
| 138 |
return style; |
| 139 |
} |
| 140 |
const unsigned char style = pAccess->StyleAt(position); |
| 141 |
return style; |
| 142 |
} |
| 143 |
Sci_Position GetLine(Sci_Position position) const { |
| 144 |
return pAccess->LineFromPosition(position); |
| 145 |
} |
| 146 |
Sci_Position LineStart(Sci_Position line) const { |
| 147 |
return pAccess->LineStart(line); |
| 148 |
} |
| 149 |
Sci_Position LineEnd(Sci_Position line) const { |
| 150 |
return pAccess->LineEnd(line); |
| 151 |
} |
| 152 |
int LevelAt(Sci_Position line) const { |
| 153 |
return pAccess->GetLevel(line); |
| 154 |
} |
| 155 |
Sci_Position Length() const noexcept { |
| 156 |
return lenDoc; |
| 157 |
} |
| 158 |
void Flush() { |
| 159 |
if (validLen > 0) { |
| 160 |
pAccess->SetStyles(validLen, styleBuf); |
| 161 |
startPosStyling += validLen; |
| 162 |
validLen = 0; |
| 163 |
} |
| 164 |
} |
| 165 |
int GetLineState(Sci_Position line) const { |
| 166 |
return pAccess->GetLineState(line); |
| 167 |
} |
| 168 |
int SetLineState(Sci_Position line, int state) { |
| 169 |
return pAccess->SetLineState(line, state); |
| 170 |
} |
| 171 |
// Style setting |
| 172 |
void StartAt(Sci_PositionU start) { |
| 173 |
pAccess->StartStyling(start); |
| 174 |
startPosStyling = start; |
| 175 |
} |
| 176 |
Sci_PositionU GetStartSegment() const noexcept { |
| 177 |
return startSeg; |
| 178 |
} |
| 179 |
void StartSegment(Sci_PositionU pos) noexcept { |
| 180 |
startSeg = pos; |
| 181 |
} |
| 182 |
void ColourTo(Sci_PositionU pos, int chAttr) { |
| 183 |
// Only perform styling if non empty range |
| 184 |
if (pos != startSeg - 1) { |
| 185 |
assert(pos >= startSeg); |
| 186 |
if (pos < startSeg) { |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
if (validLen + (pos - startSeg + 1) >= bufferSize) |
| 191 |
Flush(); |
| 192 |
const unsigned char attr = chAttr & 0xffU; |
| 193 |
if (validLen + (pos - startSeg + 1) >= bufferSize) { |
| 194 |
// Too big for buffer so send directly |
| 195 |
pAccess->SetStyleFor(pos - startSeg + 1, attr); |
| 196 |
} else { |
| 197 |
for (Sci_PositionU i = startSeg; i <= pos; i++) { |
| 198 |
assert((startPosStyling + validLen) < Length()); |
| 199 |
styleBuf[validLen++] = attr; |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
startSeg = pos+1; |
| 204 |
} |
| 205 |
void SetLevel(Sci_Position line, int level) { |
| 206 |
pAccess->SetLevel(line, level); |
| 207 |
} |
| 208 |
void IndicatorFill(Sci_Position start, Sci_Position end, int indicator, int value) { |
| 209 |
pAccess->DecorationSetCurrentIndicator(indicator); |
| 210 |
pAccess->DecorationFillRange(start, value, end - start); |
| 211 |
} |
| 212 |
|
| 213 |
void ChangeLexerState(Sci_Position start, Sci_Position end) { |
| 214 |
pAccess->ChangeLexerState(start, end); |
| 215 |
} |
| 216 |
}; |
| 217 |
|
| 218 |
struct LexicalClass { |
| 219 |
int value; |
| 220 |
const char *name; |
| 221 |
const char *tags; |
| 222 |
const char *description; |
| 223 |
}; |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
#endif |