r5116 | eht16 | 2010-08-05 22:13:47 +0100 (Thu, 05 Aug 2010) | 3 lines
[geany-mirror.git] / scintilla / StyleContext.h
blob4e175bc29552381f4e14ea4f5db3ce57c2b15f28
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.
8 #ifdef SCI_NAMESPACE
9 namespace Scintilla {
10 #endif
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 void GetNextChar(unsigned int pos) {
21 chNext = static_cast<unsigned char>(styler.SafeGetCharAt(pos+1));
22 if (styler.IsLeadByte(static_cast<char>(chNext))) {
23 chNext = chNext << 8;
24 chNext |= static_cast<unsigned char>(styler.SafeGetCharAt(pos+2));
26 // End of line?
27 // Trigger on CR only (Mac style) or either on LF from CR+LF (Dos/Win)
28 // or on LF alone (Unix). Avoid triggering two times on Dos/Win.
29 atLineEnd = (ch == '\r' && chNext != '\n') ||
30 (ch == '\n') ||
31 (currentPos >= endPos);
34 public:
35 unsigned int currentPos;
36 bool atLineStart;
37 bool atLineEnd;
38 int state;
39 int chPrev;
40 int ch;
41 int chNext;
43 StyleContext(unsigned int startPos, unsigned int length,
44 int initStyle, Accessor &styler_, char chMask=31) :
45 styler(styler_),
46 endPos(startPos + length),
47 currentPos(startPos),
48 atLineStart(true),
49 atLineEnd(false),
50 state(initStyle & chMask), // Mask off all bits which aren't in the chMask.
51 chPrev(0),
52 ch(0),
53 chNext(0) {
54 styler.StartAt(startPos, chMask);
55 styler.StartSegment(startPos);
56 unsigned int pos = currentPos;
57 ch = static_cast<unsigned char>(styler.SafeGetCharAt(pos));
58 if (styler.IsLeadByte(static_cast<char>(ch))) {
59 pos++;
60 ch = ch << 8;
61 ch |= static_cast<unsigned char>(styler.SafeGetCharAt(pos));
63 GetNextChar(pos);
65 void Complete() {
66 styler.ColourTo(currentPos - 1, state);
68 bool More() const {
69 return currentPos < endPos;
71 void Forward() {
72 if (currentPos < endPos) {
73 atLineStart = atLineEnd;
74 chPrev = ch;
75 currentPos++;
76 if (ch >= 0x100)
77 currentPos++;
78 ch = chNext;
79 GetNextChar(currentPos + ((ch >= 0x100) ? 1 : 0));
80 } else {
81 atLineStart = false;
82 chPrev = ' ';
83 ch = ' ';
84 chNext = ' ';
85 atLineEnd = true;
88 void Forward(int nb) {
89 for (int i = 0; i < nb; i++) {
90 Forward();
93 void ChangeState(int state_) {
94 state = state_;
96 void SetState(int state_) {
97 styler.ColourTo(currentPos - 1, state);
98 state = state_;
100 void ForwardSetState(int state_) {
101 Forward();
102 styler.ColourTo(currentPos - 1, state);
103 state = state_;
105 int LengthCurrent() {
106 return currentPos - styler.GetStartSegment();
108 int GetRelative(int n) {
109 return static_cast<unsigned char>(styler.SafeGetCharAt(currentPos+n));
111 bool Match(char ch0) const {
112 return ch == static_cast<unsigned char>(ch0);
114 bool Match(char ch0, char ch1) const {
115 return (ch == static_cast<unsigned char>(ch0)) && (chNext == static_cast<unsigned char>(ch1));
117 bool Match(const char *s) {
118 if (ch != static_cast<unsigned char>(*s))
119 return false;
120 s++;
121 if (!*s)
122 return true;
123 if (chNext != static_cast<unsigned char>(*s))
124 return false;
125 s++;
126 for (int n=2; *s; n++) {
127 if (*s != styler.SafeGetCharAt(currentPos+n))
128 return false;
129 s++;
131 return true;
133 bool MatchIgnoreCase(const char *s) {
134 if (tolower(ch) != static_cast<unsigned char>(*s))
135 return false;
136 s++;
137 if (tolower(chNext) != static_cast<unsigned char>(*s))
138 return false;
139 s++;
140 for (int n=2; *s; n++) {
141 if (static_cast<unsigned char>(*s) !=
142 tolower(static_cast<unsigned char>(styler.SafeGetCharAt(currentPos+n))))
143 return false;
144 s++;
146 return true;
148 // Non-inline
149 void GetCurrent(char *s, unsigned int len);
150 void GetCurrentLowered(char *s, unsigned int len);
153 #ifdef SCI_NAMESPACE
155 #endif
157 inline bool IsASpace(unsigned int ch) {
158 return (ch == ' ') || ((ch >= 0x09) && (ch <= 0x0d));
161 inline bool IsASpaceOrTab(unsigned int ch) {
162 return (ch == ' ') || (ch == '\t');
165 inline bool IsADigit(unsigned int ch) {
166 return (ch >= '0') && (ch <= '9');
169 inline bool IsADigit(unsigned int ch, unsigned int base) {
170 if (base <= 10) {
171 return (ch >= '0') && (ch < '0' + base);
172 } else {
173 return ((ch >= '0') && (ch <= '9')) ||
174 ((ch >= 'A') && (ch < 'A' + base - 10)) ||
175 ((ch >= 'a') && (ch < 'a' + base - 10));