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