*** empty log message ***
[anjuta-git-plugin.git] / scintilla / StyleContext.h
blobaedebbc012cba8ecb134fb735d37aa87dd333e35
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 // 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 unsigned int endPos;
15 StyleContext& operator=(const StyleContext&) {
16 return *this;
18 void GetNextChar(unsigned int pos) {
19 chNext = static_cast<unsigned char>(styler.SafeGetCharAt(pos+1));
20 if (styler.IsLeadByte(static_cast<char>(chNext))) {
21 chNext = chNext << 8;
22 chNext |= static_cast<unsigned char>(styler.SafeGetCharAt(pos+2));
24 // End of line?
25 // Trigger on CR only (Mac style) or either on LF from CR+LF (Dos/Win)
26 // or on LF alone (Unix). Avoid triggering two times on Dos/Win.
27 atLineEnd = (ch == '\r' && chNext != '\n') ||
28 (ch == '\n') ||
29 (currentPos >= endPos);
32 public:
33 unsigned int currentPos;
34 bool atLineStart;
35 bool atLineEnd;
36 int state;
37 int chPrev;
38 int ch;
39 int chNext;
41 StyleContext(unsigned int startPos, unsigned int length,
42 int initStyle, Accessor &styler_, char chMask=31) :
43 styler(styler_),
44 endPos(startPos + length),
45 currentPos(startPos),
46 atLineStart(true),
47 atLineEnd(false),
48 state(initStyle),
49 chPrev(0),
50 ch(0),
51 chNext(0) {
52 styler.StartAt(startPos, chMask);
53 styler.StartSegment(startPos);
54 unsigned int pos = currentPos;
55 ch = static_cast<unsigned char>(styler.SafeGetCharAt(pos));
56 if (styler.IsLeadByte(static_cast<char>(ch))) {
57 pos++;
58 ch = ch << 8;
59 ch |= static_cast<unsigned char>(styler.SafeGetCharAt(pos));
61 GetNextChar(pos);
63 void Complete() {
64 styler.ColourTo(currentPos - 1, state);
66 bool More() {
67 return currentPos < endPos;
69 void Forward() {
70 if (currentPos < endPos) {
71 atLineStart = atLineEnd;
72 chPrev = ch;
73 currentPos++;
74 if (ch >= 0x100)
75 currentPos++;
76 ch = chNext;
77 GetNextChar(currentPos + ((ch >= 0x100) ? 1 : 0));
78 } else {
79 atLineStart = false;
80 chPrev = ' ';
81 ch = ' ';
82 chNext = ' ';
83 atLineEnd = true;
86 void Forward(int nb) {
87 for (int i = 0; i < nb; i++) {
88 Forward();
91 void ChangeState(int state_) {
92 state = state_;
94 void SetState(int state_) {
95 styler.ColourTo(currentPos - 1, state);
96 state = state_;
98 void ForwardSetState(int state_) {
99 Forward();
100 styler.ColourTo(currentPos - 1, state);
101 state = state_;
103 int LengthCurrent() {
104 return currentPos - styler.GetStartSegment();
106 int GetRelative(int n) {
107 return styler.SafeGetCharAt(currentPos+n);
109 bool Match(char ch0) {
110 return ch == ch0;
112 bool Match(char ch0, char ch1) {
113 return (ch == ch0) && (chNext == ch1);
115 bool Match(const char *s) {
116 if (ch != *s)
117 return false;
118 s++;
119 if (chNext != *s)
120 return false;
121 s++;
122 for (int n=2; *s; n++) {
123 if (*s != styler.SafeGetCharAt(currentPos+n))
124 return false;
125 s++;
127 return true;
129 bool MatchIgnoreCase(const char *s) {
130 if (tolower(ch) != *s)
131 return false;
132 s++;
133 if (tolower(chNext) != *s)
134 return false;
135 s++;
136 for (int n=2; *s; n++) {
137 if (*s != tolower((styler.SafeGetCharAt(currentPos+n))))
138 return false;
139 s++;
141 return true;
143 // Non-inline
144 void GetCurrent(char *s, unsigned int len);
145 void GetCurrentLowered(char *s, unsigned int len);
148 inline bool IsASpace(unsigned int ch) {
149 return (ch == ' ') || ((ch >= 0x09) && (ch <= 0x0d));
152 inline bool IsASpaceOrTab(unsigned int ch) {
153 return (ch == ' ') || (ch == '\t');
156 inline bool IsADigit(unsigned int ch) {
157 return (ch >= '0') && (ch <= '9');
160 inline bool IsADigit(unsigned int ch, unsigned int base) {
161 if (base <= 10) {
162 return (ch >= '0') && (ch < '0' + base);
163 } else {
164 return ((ch >= '0') && (ch <= '9')) ||
165 ((ch >= 'A') && (ch < 'A' + base - 10)) ||
166 ((ch >= 'a') && (ch < 'a' + base - 10));