]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/StyleContext.h
fix compilation without wxUSE_STREAMS (closes #10900)
[wxWidgets.git] / src / stc / scintilla / src / StyleContext.h
1 // Scintilla source code edit control
2 /** @file StyleContext.cxx
3 ** Lexer infrastructure.
4 **/
5 // Copyright 1998-2004 by Neil Hodgson <neilh@scintilla.org>
6 // This file is in the public domain.
7
8 #ifdef SCI_NAMESPACE
9 namespace Scintilla {
10 #endif
11
12 // All languages handled so far can treat all characters >= 0x80 as one class
13 // which just continues the current token or starts an identifier if in default.
14 // DBCS treated specially as the second character can be < 0x80 and hence
15 // syntactically significant. UTF-8 avoids this as all trail bytes are >= 0x80
16 class StyleContext {
17 Accessor &styler;
18 unsigned int endPos;
19 StyleContext& operator=(const StyleContext&) {
20 return *this;
21 }
22 void GetNextChar(unsigned int pos) {
23 chNext = static_cast<unsigned char>(styler.SafeGetCharAt(pos+1));
24 if (styler.IsLeadByte(static_cast<char>(chNext))) {
25 chNext = chNext << 8;
26 chNext |= static_cast<unsigned char>(styler.SafeGetCharAt(pos+2));
27 }
28 // End of line?
29 // Trigger on CR only (Mac style) or either on LF from CR+LF (Dos/Win)
30 // or on LF alone (Unix). Avoid triggering two times on Dos/Win.
31 atLineEnd = (ch == '\r' && chNext != '\n') ||
32 (ch == '\n') ||
33 (currentPos >= endPos);
34 }
35
36 public:
37 unsigned int currentPos;
38 bool atLineStart;
39 bool atLineEnd;
40 int state;
41 int chPrev;
42 int ch;
43 int chNext;
44
45 StyleContext(unsigned int startPos, unsigned int length,
46 int initStyle, Accessor &styler_, char chMask=31) :
47 styler(styler_),
48 endPos(startPos + length),
49 currentPos(startPos),
50 atLineStart(true),
51 atLineEnd(false),
52 state(initStyle & chMask), // Mask off all bits which aren't in the chMask.
53 chPrev(0),
54 ch(0),
55 chNext(0) {
56 styler.StartAt(startPos, chMask);
57 styler.StartSegment(startPos);
58 unsigned int pos = currentPos;
59 ch = static_cast<unsigned char>(styler.SafeGetCharAt(pos));
60 if (styler.IsLeadByte(static_cast<char>(ch))) {
61 pos++;
62 ch = ch << 8;
63 ch |= static_cast<unsigned char>(styler.SafeGetCharAt(pos));
64 }
65 GetNextChar(pos);
66 }
67 void Complete() {
68 styler.ColourTo(currentPos - 1, state);
69 }
70 bool More() {
71 return currentPos < endPos;
72 }
73 void Forward() {
74 if (currentPos < endPos) {
75 atLineStart = atLineEnd;
76 chPrev = ch;
77 currentPos++;
78 if (ch >= 0x100)
79 currentPos++;
80 ch = chNext;
81 GetNextChar(currentPos + ((ch >= 0x100) ? 1 : 0));
82 } else {
83 atLineStart = false;
84 chPrev = ' ';
85 ch = ' ';
86 chNext = ' ';
87 atLineEnd = true;
88 }
89 }
90 void Forward(int nb) {
91 for (int i = 0; i < nb; i++) {
92 Forward();
93 }
94 }
95 void ChangeState(int state_) {
96 state = state_;
97 }
98 void SetState(int state_) {
99 styler.ColourTo(currentPos - 1, state);
100 state = state_;
101 }
102 void ForwardSetState(int state_) {
103 Forward();
104 styler.ColourTo(currentPos - 1, state);
105 state = state_;
106 }
107 int LengthCurrent() {
108 return currentPos - styler.GetStartSegment();
109 }
110 int GetRelative(int n) {
111 return static_cast<unsigned char>(styler.SafeGetCharAt(currentPos+n));
112 }
113 bool Match(char ch0) {
114 return ch == static_cast<unsigned char>(ch0);
115 }
116 bool Match(char ch0, char ch1) {
117 return (ch == static_cast<unsigned char>(ch0)) && (chNext == static_cast<unsigned char>(ch1));
118 }
119 bool Match(const char *s) {
120 if (ch != static_cast<unsigned char>(*s))
121 return false;
122 s++;
123 if (!*s)
124 return true;
125 if (chNext != static_cast<unsigned char>(*s))
126 return false;
127 s++;
128 for (int n=2; *s; n++) {
129 if (*s != styler.SafeGetCharAt(currentPos+n))
130 return false;
131 s++;
132 }
133 return true;
134 }
135 bool MatchIgnoreCase(const char *s) {
136 if (tolower(ch) != static_cast<unsigned char>(*s))
137 return false;
138 s++;
139 if (tolower(chNext) != static_cast<unsigned char>(*s))
140 return false;
141 s++;
142 for (int n=2; *s; n++) {
143 if (static_cast<unsigned char>(*s) !=
144 tolower(static_cast<unsigned char>(styler.SafeGetCharAt(currentPos+n))))
145 return false;
146 s++;
147 }
148 return true;
149 }
150 // Non-inline
151 void GetCurrent(char *s, unsigned int len);
152 void GetCurrentLowered(char *s, unsigned int len);
153 };
154
155 #ifdef SCI_NAMESPACE
156 }
157 #endif
158
159 inline bool IsASpace(unsigned int ch) {
160 return (ch == ' ') || ((ch >= 0x09) && (ch <= 0x0d));
161 }
162
163 inline bool IsASpaceOrTab(unsigned int ch) {
164 return (ch == ' ') || (ch == '\t');
165 }
166
167 inline bool IsADigit(unsigned int ch) {
168 return (ch >= '0') && (ch <= '9');
169 }
170
171 inline bool IsADigit(unsigned int ch, unsigned int base) {
172 if (base <= 10) {
173 return (ch >= '0') && (ch < '0' + base);
174 } else {
175 return ((ch >= '0') && (ch <= '9')) ||
176 ((ch >= 'A') && (ch < 'A' + base - 10)) ||
177 ((ch >= 'a') && (ch < 'a' + base - 10));
178 }
179 }