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