1 // Scintilla source code edit control
2 /** @file LexAccessor.h
3 ** Interfaces between Scintilla and lexers.
5 // Copyright 1998-2010 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
13 enum EncodingType
{ enc8bit
, encUnicode
, encDBCS
};
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
;
28 enum EncodingType encodingType
;
30 char styleBuf
[bufferSize
];
31 Sci_Position validLen
;
32 Sci_PositionU startSeg
;
33 Sci_Position startPosStyling
;
36 void Fill(Sci_Position position
) {
37 startPos
= position
- slopSize
;
38 if (startPos
+ bufferSize
> lenDoc
)
39 startPos
= lenDoc
- bufferSize
;
42 endPos
= startPos
+ bufferSize
;
46 pAccess
->GetCharRange(buf
, startPos
, endPos
-startPos
);
47 buf
[endPos
-startPos
] = '\0';
51 explicit LexAccessor(IDocument
*pAccess_
) :
52 pAccess(pAccess_
), startPos(extremePosition
), endPos(0),
53 codePage(pAccess
->CodePage()),
54 encodingType(enc8bit
),
55 lenDoc(pAccess
->Length()),
57 startSeg(0), startPosStyling(0),
58 documentVersion(pAccess
->Version()) {
59 // Prevent warnings by static analyzers about uninitialized buf and styleBuf.
64 encodingType
= encUnicode
;
71 encodingType
= encDBCS
;
74 char operator[](Sci_Position position
) {
75 if (position
< startPos
|| position
>= endPos
) {
78 return buf
[position
- startPos
];
80 IDocument
*MultiByteAccess() const {
83 /** Safe version of operator[], returning a defined value for invalid position. */
84 char SafeGetCharAt(Sci_Position position
, char chDefault
=' ') {
85 if (position
< startPos
|| position
>= endPos
) {
87 if (position
< startPos
|| position
>= endPos
) {
88 // Position is outside range of document
92 return buf
[position
- startPos
];
94 bool IsLeadByte(char ch
) const {
95 return pAccess
->IsDBCSLeadByte(ch
);
97 EncodingType
Encoding() const {
100 bool Match(Sci_Position pos
, const char *s
) {
101 for (int i
=0; *s
; i
++) {
102 if (*s
!= SafeGetCharAt(pos
+i
))
108 char StyleAt(Sci_Position position
) const {
109 return static_cast<char>(pAccess
->StyleAt(position
));
111 Sci_Position
GetLine(Sci_Position position
) const {
112 return pAccess
->LineFromPosition(position
);
114 Sci_Position
LineStart(Sci_Position line
) const {
115 return pAccess
->LineStart(line
);
117 Sci_Position
LineEnd(Sci_Position line
) {
118 return pAccess
->LineEnd(line
);
120 int LevelAt(Sci_Position line
) const {
121 return pAccess
->GetLevel(line
);
123 Sci_Position
Length() const {
128 pAccess
->SetStyles(validLen
, styleBuf
);
129 startPosStyling
+= validLen
;
133 int GetLineState(Sci_Position line
) const {
134 return pAccess
->GetLineState(line
);
136 int SetLineState(Sci_Position line
, int state
) {
137 return pAccess
->SetLineState(line
, state
);
140 void StartAt(Sci_PositionU start
) {
141 pAccess
->StartStyling(start
);
142 startPosStyling
= start
;
144 Sci_PositionU
GetStartSegment() const {
147 void StartSegment(Sci_PositionU pos
) {
150 void ColourTo(Sci_PositionU pos
, int chAttr
) {
151 // Only perform styling if non empty range
152 if (pos
!= startSeg
- 1) {
153 assert(pos
>= startSeg
);
154 if (pos
< startSeg
) {
158 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
)
160 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
) {
161 // Too big for buffer so send directly
162 pAccess
->SetStyleFor(pos
- startSeg
+ 1, static_cast<char>(chAttr
));
164 for (Sci_PositionU i
= startSeg
; i
<= pos
; i
++) {
165 assert((startPosStyling
+ validLen
) < Length());
166 styleBuf
[validLen
++] = static_cast<char>(chAttr
);
172 void SetLevel(Sci_Position line
, int level
) {
173 pAccess
->SetLevel(line
, level
);
175 void IndicatorFill(Sci_Position start
, Sci_Position end
, int indicator
, int value
) {
176 pAccess
->DecorationSetCurrentIndicator(indicator
);
177 pAccess
->DecorationFillRange(start
, value
, end
- start
);
180 void ChangeLexerState(Sci_Position start
, Sci_Position end
) {
181 pAccess
->ChangeLexerState(start
, end
);
185 struct LexicalClass
{
189 const char *description
;