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