]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/LexEiffel.cxx
e9e154cd5f64b8eb8051aa11c7ccbf8ad08a0cda
[wxWidgets.git] / contrib / src / stc / scintilla / src / LexEiffel.cxx
1 // Scintilla source code edit control
2 /** @file LexEiffel.cxx
3 ** Lexer for Eiffel.
4 **/
5 // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include <stdarg.h>
12 #include <stdio.h>
13
14 #include "Platform.h"
15
16 #include "PropSet.h"
17 #include "Accessor.h"
18 #include "StyleContext.h"
19 #include "KeyWords.h"
20 #include "Scintilla.h"
21 #include "SciLexer.h"
22
23 static inline bool isEiffelOperator(unsigned int ch) {
24 // '.' left out as it is used to make up numbers
25 return ch == '*' || ch == '/' || ch == '\\' || ch == '-' || ch == '+' ||
26 ch == '(' || ch == ')' || ch == '=' ||
27 ch == '{' || ch == '}' || ch == '~' ||
28 ch == '[' || ch == ']' || ch == ';' ||
29 ch == '<' || ch == '>' || ch == ',' ||
30 ch == '.' || ch == '^' || ch == '%' || ch == ':' ||
31 ch == '!' || ch == '@' || ch == '?';
32 }
33
34 static inline bool IsAWordChar(unsigned int ch) {
35 return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_');
36 }
37
38 static inline bool IsAWordStart(unsigned int ch) {
39 return (ch < 0x80) && (isalnum(ch) || ch == '_');
40 }
41
42 static void ColouriseEiffelDoc(unsigned int startPos,
43 int length,
44 int initStyle,
45 WordList *keywordlists[],
46 Accessor &styler) {
47
48 WordList &keywords = *keywordlists[0];
49
50 StyleContext sc(startPos, length, initStyle, styler);
51
52 for (; sc.More(); sc.Forward()) {
53
54 if (sc.state == SCE_EIFFEL_STRINGEOL) {
55 if (sc.ch != '\r' && sc.ch != '\n') {
56 sc.SetState(SCE_EIFFEL_DEFAULT);
57 }
58 } else if (sc.state == SCE_EIFFEL_OPERATOR) {
59 sc.SetState(SCE_EIFFEL_DEFAULT);
60 } else if (sc.state == SCE_EIFFEL_WORD) {
61 if (!IsAWordChar(sc.ch)) {
62 char s[100];
63 sc.GetCurrentLowered(s, sizeof(s));
64 if (!keywords.InList(s)) {
65 sc.ChangeState(SCE_EIFFEL_IDENTIFIER);
66 }
67 sc.SetState(SCE_EIFFEL_DEFAULT);
68 }
69 } else if (sc.state == SCE_EIFFEL_NUMBER) {
70 if (!IsAWordChar(sc.ch)) {
71 sc.SetState(SCE_EIFFEL_DEFAULT);
72 }
73 } else if (sc.state == SCE_EIFFEL_COMMENTLINE) {
74 if (sc.ch == '\r' || sc.ch == '\n') {
75 sc.SetState(SCE_EIFFEL_DEFAULT);
76 }
77 } else if (sc.state == SCE_EIFFEL_STRING) {
78 if (sc.ch == '%') {
79 sc.Forward();
80 } else if (sc.ch == '\"') {
81 sc.Forward();
82 sc.SetState(SCE_EIFFEL_DEFAULT);
83 }
84 } else if (sc.state == SCE_EIFFEL_CHARACTER) {
85 if (sc.ch == '\r' || sc.ch == '\n') {
86 sc.SetState(SCE_EIFFEL_STRINGEOL);
87 } else if (sc.ch == '%') {
88 sc.Forward();
89 } else if (sc.ch == '\'') {
90 sc.Forward();
91 sc.SetState(SCE_EIFFEL_DEFAULT);
92 }
93 }
94
95 if (sc.state == SCE_EIFFEL_DEFAULT) {
96 if (sc.ch == '-' && sc.chNext == '-') {
97 sc.SetState(SCE_EIFFEL_COMMENTLINE);
98 } else if (sc.ch == '\"') {
99 sc.SetState(SCE_EIFFEL_STRING);
100 } else if (sc.ch == '\'') {
101 sc.SetState(SCE_EIFFEL_CHARACTER);
102 } else if (IsADigit(sc.ch) || (sc.ch == '.')) {
103 sc.SetState(SCE_EIFFEL_NUMBER);
104 } else if (IsAWordStart(sc.ch)) {
105 sc.SetState(SCE_EIFFEL_WORD);
106 } else if (isEiffelOperator(sc.ch)) {
107 sc.SetState(SCE_EIFFEL_OPERATOR);
108 }
109 }
110 }
111 sc.Complete();
112 }
113
114 static bool IsEiffelComment(Accessor &styler, int pos, int len) {
115 return len>1 && styler[pos]=='-' && styler[pos+1]=='-';
116 }
117
118 static void FoldEiffelDocIndent(unsigned int startPos, int length, int,
119 WordList *[], Accessor &styler) {
120 int lengthDoc = startPos + length;
121
122 // Backtrack to previous line in case need to fix its fold status
123 int lineCurrent = styler.GetLine(startPos);
124 if (startPos > 0) {
125 if (lineCurrent > 0) {
126 lineCurrent--;
127 startPos = styler.LineStart(lineCurrent);
128 }
129 }
130 int spaceFlags = 0;
131 int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, IsEiffelComment);
132 char chNext = styler[startPos];
133 for (int i = startPos; i < lengthDoc; i++) {
134 char ch = chNext;
135 chNext = styler.SafeGetCharAt(i + 1);
136
137 if ((ch == '\r' && chNext != '\n') || (ch == '\n') || (i == lengthDoc)) {
138 int lev = indentCurrent;
139 int indentNext = styler.IndentAmount(lineCurrent + 1, &spaceFlags, IsEiffelComment);
140 if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG)) {
141 // Only non whitespace lines can be headers
142 if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext & SC_FOLDLEVELNUMBERMASK)) {
143 lev |= SC_FOLDLEVELHEADERFLAG;
144 } else if (indentNext & SC_FOLDLEVELWHITEFLAG) {
145 // Line after is blank so check the next - maybe should continue further?
146 int spaceFlags2 = 0;
147 int indentNext2 = styler.IndentAmount(lineCurrent + 2, &spaceFlags2, IsEiffelComment);
148 if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext2 & SC_FOLDLEVELNUMBERMASK)) {
149 lev |= SC_FOLDLEVELHEADERFLAG;
150 }
151 }
152 }
153 indentCurrent = indentNext;
154 styler.SetLevel(lineCurrent, lev);
155 lineCurrent++;
156 }
157 }
158 }
159
160 static void FoldEiffelDocKeyWords(unsigned int startPos, int length, int /* initStyle */, WordList *[],
161 Accessor &styler) {
162 unsigned int lengthDoc = startPos + length;
163 int visibleChars = 0;
164 int lineCurrent = styler.GetLine(startPos);
165 int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
166 int levelCurrent = levelPrev;
167 char chNext = styler[startPos];
168 int stylePrev = 0;
169 int styleNext = styler.StyleAt(startPos);
170 // lastDeferred should be determined by looking back to last keyword in case
171 // the "deferred" is on a line before "class"
172 bool lastDeferred = false;
173 for (unsigned int i = startPos; i < lengthDoc; i++) {
174 char ch = chNext;
175 chNext = styler.SafeGetCharAt(i + 1);
176 int style = styleNext;
177 styleNext = styler.StyleAt(i + 1);
178 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
179 if ((stylePrev != SCE_EIFFEL_WORD) && (style == SCE_EIFFEL_WORD)) {
180 char s[20];
181 unsigned int j = 0;
182 while ((j < (sizeof(s) - 1)) && (iswordchar(styler[i + j]))) {
183 s[j] = styler[i + j];
184 j++;
185 }
186 s[j] = '\0';
187
188 if (
189 (strcmp(s, "check") == 0) ||
190 (strcmp(s, "debug") == 0) ||
191 (strcmp(s, "deferred") == 0) ||
192 (strcmp(s, "do") == 0) ||
193 (strcmp(s, "from") == 0) ||
194 (strcmp(s, "if") == 0) ||
195 (strcmp(s, "inspect") == 0) ||
196 (strcmp(s, "once") == 0)
197 )
198 levelCurrent++;
199 if (!lastDeferred && (strcmp(s, "class") == 0))
200 levelCurrent++;
201 if (strcmp(s, "end") == 0)
202 levelCurrent--;
203 lastDeferred = strcmp(s, "deferred") == 0;
204 }
205
206 if (atEOL) {
207 int lev = levelPrev;
208 if (visibleChars == 0)
209 lev |= SC_FOLDLEVELWHITEFLAG;
210 if ((levelCurrent > levelPrev) && (visibleChars > 0))
211 lev |= SC_FOLDLEVELHEADERFLAG;
212 if (lev != styler.LevelAt(lineCurrent)) {
213 styler.SetLevel(lineCurrent, lev);
214 }
215 lineCurrent++;
216 levelPrev = levelCurrent;
217 visibleChars = 0;
218 }
219 if (!isspacechar(ch))
220 visibleChars++;
221 stylePrev = style;
222 }
223 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
224 int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
225 styler.SetLevel(lineCurrent, levelPrev | flagsNext);
226 }
227
228 static const char * const eiffelWordListDesc[] = {
229 "Keywords",
230 0
231 };
232
233 LexerModule lmEiffel(SCLEX_EIFFEL, ColouriseEiffelDoc, "eiffel", FoldEiffelDocIndent, eiffelWordListDesc);
234 LexerModule lmEiffelkw(SCLEX_EIFFELKW, ColouriseEiffelDoc, "eiffelkw", FoldEiffelDocKeyWords, eiffelWordListDesc);