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